From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id 6B4225A027F for ; Mon, 22 May 2023 10:50:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684745447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gMe9vhU+tQuur2bFaKpgbJsKxfgLeamgVSkb9FJY70I=; b=TzrjC+IXlZ0Hs6sswIg9yXGTDCdWA0UHeZH19ubcSzk3Xa+gApeZjcNgL2aGjEW7azF8CA 6tYd2H9kXg0dB8JWV+TZIXE/eGNs+y/inQUTmZeaFOurODGoRCQU0TBVhBpk4oXTRedI7Q 1R51iBX7wmKlAUPuAzH4+LeH4JQMPjA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-646-LrYcwLjvMNWJE6I0YXRoRg-1; Mon, 22 May 2023 04:50:46 -0400 X-MC-Unique: LrYcwLjvMNWJE6I0YXRoRg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B75068030D6; Mon, 22 May 2023 08:50:45 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.35]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 17EE32166B25; Mon, 22 May 2023 08:50:44 +0000 (UTC) Date: Mon, 22 May 2023 10:50:42 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 1/3] util, conf: Add and use ns_is_init() helper Message-ID: <20230522105042.1852c78d@elisabeth> In-Reply-To: References: <20230521234158.2769867-1-sbrivio@redhat.com> <20230521234158.2769867-2-sbrivio@redhat.com> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: BCFSGAYJLQ3NHDR36CJMN7GXLEGNHWRA X-Message-ID-Hash: BCFSGAYJLQ3NHDR36CJMN7GXLEGNHWRA X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Mon, 22 May 2023 15:41:17 +1000 David Gibson wrote: > On Mon, May 22, 2023 at 01:41:56AM +0200, Stefano Brivio wrote: > > We'll need this in isolate_initial(). While at it, don't rely on > > BUFSIZ: the earlier issue we had with musl reminded me it's not a > > magic "everything will fit" value. Size the read buffer to what we > > actually need from uid_map. > > > > Signed-off-by: Stefano Brivio > > Reviewed-by: David Gibson > > Although... > > > --- > > conf.c | 16 +--------------- > > util.c | 25 +++++++++++++++++++++++++ > > util.h | 2 ++ > > 3 files changed, 28 insertions(+), 15 deletions(-) > > > > diff --git a/conf.c b/conf.c > > index 447b000..984c3ce 100644 > > --- a/conf.c > > +++ b/conf.c > > @@ -1096,10 +1096,6 @@ static int conf_runas(char *opt, unsigned int *uid, unsigned int *gid) > > */ > > static void conf_ugid(char *runas, uid_t *uid, gid_t *gid) > > { > > - const char root_uid_map[] = " 0 0 4294967295"; > > - char buf[BUFSIZ]; > > - int fd; > > - > > /* If user has specified --runas, that takes precedence... */ > > if (runas) { > > if (conf_runas(runas, uid, gid)) > > @@ -1116,18 +1112,8 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid) > > return; > > > > /* ...or at least not root in the init namespace... */ > > - if ((fd = open("/proc/self/uid_map", O_RDONLY | O_CLOEXEC)) < 0) { > > - die("Can't determine if we're in init namespace: %s", > > - strerror(errno)); > > - } > > - > > - if (read(fd, buf, BUFSIZ) != sizeof(root_uid_map) || > > - strncmp(buf, root_uid_map, sizeof(root_uid_map) - 1)) { > > - close(fd); > > + if (!ns_is_init()) > > return; > > - } > > - > > - close(fd); > > > > /* ...otherwise use nobody:nobody */ > > warn("Don't run as root. Changing to nobody..."); > > diff --git a/util.c b/util.c > > index c3e3471..5ec8a6c 100644 > > --- a/util.c > > +++ b/util.c > > @@ -390,6 +390,31 @@ int ns_enter(const struct ctx *c) > > return 0; > > } > > > > +/** > > + * ns_is_init() - Is the caller running in the "init" user namespace? > > + * > > + * Return: true if caller is in init, false otherwise, won't return on failure > > + */ > > +bool ns_is_init(void) > > +{ > > + const char root_uid_map[] = " 0 0 4294967295"; > > + char buf[sizeof(root_uid_map) + 1]; > > + bool ret = true; > > + int fd; > > + > > + if ((fd = open("/proc/self/uid_map", O_RDONLY | O_CLOEXEC)) < 0) { > > + die("Can't determine if we're in init namespace: %s", > > + strerror(errno)); > > + } > > + > > + if (read(fd, buf, sizeof(root_uid_map)) != sizeof(root_uid_map) || > > I don't think it can go bad in practice, but I think you want to pass > a slightly larger buffer than root_uid_map[], otherwise this test will > succeed if the uid_map contains the expected thing for init, followed > by something else. Ah, yes, I thought of doing that, then I also thought that with 4294967295 as range size in the first line, there can't be other lines. Then I (clumsily) went back to the original idea, because... go figure, let's say it gets extended with something one day... But now it's inconsistent (and probably confusing): the buffer is long enough, but I don't use it: const char root_uid_map[] = " 0 0 4294967295"; 32 bytes string length, sizeof() is 33 char buf[sizeof(root_uid_map) + 1]; 34 bytes (could be 33) read(fd, buf, sizeof(root_uid_map)) read at most 33 bytes != sizeof(root_uid_map) || isn't 33 bytes (should be 32) strncmp(buf, root_uid_map, sizeof(root_uid_map) - 1)) or the first 32 bytes, up to "5", differ (we should compare one more) and, by the way, uid_map has _lines_: $ hexdump -C /proc/self/uid_map 00000000 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 | 0 | 00000010 20 20 20 20 30 20 34 32 39 34 39 36 37 32 39 35 | 0 4294967295| 00000020 0a |.| so it kind of works, but not for the reason I intended. Respinning now... -- Stefano