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 0CA9A5A0275 for ; Mon, 19 Feb 2024 12:02:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708340576; 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=96nQcSmG1L8JOXjitlfL7k5y5Ai4JuQmd+Aa/934aMI=; b=YGZrgHZpdph4nc6JWx90fGS3jgtlMkIuDGkDky0R9jx4nIIiC3gYpIuLn8yN3oc6q4NPeH v8xSyW46hsJzbP7GbWsBa2f9lCP7rKnzDC43X6DH1UCjWoCsrYJmZEsqACI9v0H5saMFct mjCktlOyJxHMCT0U/Uko+d4lioMHXa0= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-272-JEuCzglXNfylhmPv_hMxHg-1; Mon, 19 Feb 2024 06:02:54 -0500 X-MC-Unique: JEuCzglXNfylhmPv_hMxHg-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a3158fbb375so158051266b.3 for ; Mon, 19 Feb 2024 03:02:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708340573; x=1708945373; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=96nQcSmG1L8JOXjitlfL7k5y5Ai4JuQmd+Aa/934aMI=; b=ZrQ6xerKvtWv5j87h/xFpBEHQgJHj01FPEwgh2WLzBnVHfJZ/D2jEcpuZZzdyoGINF IBv7sq/r/AvFQaQdcp1c4+UWuKoADBC78Y+2k5pnEeN26ztmpLC7IMd1Vjj2smhKRvDT 61A6vHaxrgp8Qv3bgPoArr8fYVO/poUYioL3N9r/DUcN0rizLMIgIK6UsXbBOiin5bm3 8LX/NHPCex8NB1xCX75LVcyX0ld2RMemruyFn/E0wm4Lp7VWKDzGsjntb/TXZLtDVPly Jc2BIxhpFOcDgOq7TodRlPoHn16i2dL20A4nbcWR7VA/LG5mEy9/hiefjWioFeB0nFWL uxmA== X-Gm-Message-State: AOJu0YzNnH1qO6y9Fi/ZZ4wragQU9chS1jkwu/CjD3kYuXp2zc6A/Flj //NIAU/4L4qMOqkLk839PgDZSKM+w4QliluAUzH0zYIb3LoihtN2k7oCRWhRJOqZZOmbWZQrwD9 JjTb3jNqNrKp9bcu/xzvOL2VpDonqP9bcZ5REmjbqV5Tt+PNhbQ== X-Received: by 2002:a17:906:ff53:b0:a3e:c1ec:7bff with SMTP id zo19-20020a170906ff5300b00a3ec1ec7bffmr684126ejb.68.1708340573298; Mon, 19 Feb 2024 03:02:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxom3LaEJTou8sjtFtELRbGriDjBzpQSHhMXGbNFrLPBPLe1vmh5X/GSSq64aTMkDHGCYzuA== X-Received: by 2002:a17:906:ff53:b0:a3e:c1ec:7bff with SMTP id zo19-20020a170906ff5300b00a3ec1ec7bffmr684118ejb.68.1708340572892; Mon, 19 Feb 2024 03:02:52 -0800 (PST) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id h22-20020a1709063b5600b00a3e274e2e14sm2328582ejf.33.2024.02.19.03.02.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Feb 2024 03:02:52 -0800 (PST) Date: Mon, 19 Feb 2024 12:01:54 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH] pasta: Don't try to watch namespaces in procfs with inotify, use timer instead Message-ID: <20240219120154.3a07f61b@elisabeth> In-Reply-To: References: <20240217133457.2331118-1-sbrivio@redhat.com> <20240219090505.60f01ab7@elisabeth> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.36; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: CNK6NTVEBNK2WYISI4HIGHTLVI7XIN33 X-Message-ID-Hash: CNK6NTVEBNK2WYISI4HIGHTLVI7XIN33 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, Paul Holzinger 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, 19 Feb 2024 19:18:47 +1100 David Gibson wrote: > On Mon, Feb 19, 2024 at 09:05:05AM +0100, Stefano Brivio wrote: > > On Mon, 19 Feb 2024 13:35:48 +1100 > > David Gibson wrote: > > > > > On Sat, Feb 17, 2024 at 02:34:57PM +0100, Stefano Brivio wrote: > > > > We watch network namespace entries to detect when we should quit > > > > (unless --no-netns-quit is passed), and these might stored in a tmpfs > > > > typically mounted at /run/user/UID or /var/run/user/UID, or found in > > > > procfs at /proc/PID/ns/. > > > > > > > > Currently, we try to use inotify for any possible location of those > > > > entries, but inotify, of course, doesn't work on pseudo-filesystems > > > > (see inotify(7)). > > > > > > > > The man page reflects this: the description of --no-netns-quit > > > > implies that we won't quit anyway if the namespace is not "bound to > > > > the filesystem". > > > > > > > > Well, we won't quit, but, since commit 9e0dbc894813 ("More > > > > deterministic detection of whether argument is a PID, PATH or NAME"), > > > > we try. And, indeed, this is harmless, as the caveat from that > > > > commit message states. > > > > > > > > Now, it turns out that Buildah, a tool to create container images, > > > > sharing its codebase with Podman, passes a procfs entry to pasta, and > > > > expects pasta to exit once the network namespace is not needed > > > > anymore, that is, once the original Buildah process terminates. > > > > > > > > Get this to work by using the timer fallback mechanism if the > > > > namespace name is passed as a path belonging to a pseudo-filesystem. > > > > This is expected to be procfs, but I covered sysfs and devpts > > > > pseudo-filesystems as well, because nothing actually prevents > > > > creating this kind of directory structure and links there. > > > > > > > > Note that statfs(), according to some versions of man pages, was > > > > apparently "deprecated" by the LSB. My reasoning for using it is > > > > essentially this: > > > > https://lore.kernel.org/linux-man/f54kudgblgk643u32tb6at4cd3kkzha6hslahv24szs4raroaz@ogivjbfdaqtb/t/#u > > > > > > > > ...that is, there was no such thing as an LSB deprecation, and > > > > anyway there's no other way to get the filesystem type. > > > > > > Huh, weird. > > > > > > > Also note that, while it might sound more robust to detect the > > > > filesystem type using fstatfs() on the file descriptor > > > > (c->pasta_netns_fd), the reported filesystem type for it is nsfs, no > > > > matter what path was given to pasta. If we use the parent directory, > > > > we'll typically have either tmpfs or procfs reported. > > > > > > We could, however, move the opening of the parent directory earlier > > > and use fstatfs() on that instead of statfs() on the path. > > > > Done in v3. > > > > > > The timer, however, still uses the file descriptor of the parent > > > > directory later, as it has no access to the filesystem, and that > > > > avoids possible races if the PID is recycled: if the original process > > > > terminates, the handle we have on /proc/PID/ns still refers to it, > > > > not to any other process with the same PID. > > > > > > True, but it's not obvious to me how that's relevant to this patch. > > > > > > > We could have used pidfd_open() to get a handle on the parent > > > > process. But it's not guaranteed that the parent process is actually > > > > the one associated to the network namespace we operate on, and if we > > > > get a PID file descriptor for a PID (parent or not) or path that was > > > > given on our command line, this inherently causes a race condition as > > > > that PID might have been recycled by the time we call pidfd_open(). > > > > > > Again, not really sure of the relevance of this to the patch at hand. > > > > Right, sorry, in both paragraphs I took the context of the Podman issue > > for granted. Changed in v3. > > > > > > Even assuming the process we want to watch is the parent process, and > > > > a race-free usage of pidfd_open() would have been possible, I'm not > > > > very enthusiastic about enabling yet another system call in the > > > > seccomp profile just for this, while openat() is needed anyway. > > > > > > > > Update the man page to reflect that, even if the target network > > > > namespace is passed as a procfs path or a PID, we'll now quit when > > > > the procfs entry is gone. > > > > > > > > Reported-by: Paul Holzinger > > > > Link: https://github.com/containers/podman/pull/21563#issuecomment-1948200214 > > > > Signed-off-by: Stefano Brivio > > > > --- > > > > passt.1 | 8 ++++++-- > > > > pasta.c | 15 +++++++++++++-- > > > > 2 files changed, 19 insertions(+), 4 deletions(-) > > > > > > > > diff --git a/passt.1 b/passt.1 > > > > index dc2d719..de6e3bf 100644 > > > > --- a/passt.1 > > > > +++ b/passt.1 > > > > @@ -550,8 +550,12 @@ without \-\-userns. > > > > > > > > .TP > > > > .BR \-\-no-netns-quit > > > > -If the target network namespace is bound to the filesystem (that is, if PATH or > > > > -NAME are given as target), do not exit once the network namespace is deleted. > > > > +If the target network namespace is bound to the filesystem, do not exit once > > > > +that path is deleted. > > > > + > > > > +If the target network namespace is represented by a procfs entry, do not exit > > > > +once that entry is removed from procfs (representing the fact that a process > > > > +with the given PID terminated). > > > > > > Can't you now simplify this all to "Do not exit when the target > > > network namespace ends"? > > > > That was my first attempt, but as I was writing something of that sort, > > I realised it's actually false: as long as pasta is using the network > > namespace, the namespace exists and networking is still up and running. > > > > One could delete the entry in tmpfs, or the original process could > > terminate, join the network namespace after that, and keep using it. > > Ah, true. I wish there were a more succinct way to express this, but > I can't think of it either. > > > So I guess it's better to clarify what are our conditions for > > terminating (and, most likely, terminating the target namespace as a > > consequence). > > > > > > .TP > > > > .BR \-\-config-net > > > > diff --git a/pasta.c b/pasta.c > > > > index 01d1511..4110917 100644 > > > > --- a/pasta.c > > > > +++ b/pasta.c > > > > @@ -33,6 +33,7 @@ > > > > #include > > > > #include > > > > #include > > > > +#include > > > > #include > > > > #include > > > > #include > > > > @@ -41,6 +42,7 @@ > > > > #include > > > > #include > > > > #include > > > > +#include > > > > > > > > #include "util.h" > > > > #include "passt.h" > > > > @@ -390,12 +392,18 @@ void pasta_netns_quit_init(const struct ctx *c) > > > > union epoll_ref ref = { .type = EPOLL_TYPE_NSQUIT_INOTIFY }; > > > > struct epoll_event ev = { .events = EPOLLIN }; > > > > int flags = O_NONBLOCK | O_CLOEXEC; > > > > - int fd; > > > > + struct statfs s = { 0 }; > > > > > > I don't think you need to initialise this, since you should only be > > > reading it in the case that statfs() succeeds. > > > > There's no functional need, but I don't want to expose our stack memory > > to the kernel. > > That seems... pointlessly paranoid to me. Probably yes, but... > The syscall just takes a > pointer, which implies that the kernel can access our memory anyway. at least in theory, perhaps a bit more likely with lesser-used system calls, there might be a vulnerability in the kernel that shares data from a pointed buffer with other processes. An attacker doesn't necessarily need to achieve arbitrary execution in the kernel, then. > > Even though the kernel is generally able to control the process, I feel > > like it's nice to avoid it, and I think it's consistent with most of > > our system call usage. > > Hrm, not generally in places I've been working at any rate. I > generally dislike initialization's that aren't essential, because it > eliminates the possibility of compiler or checker warnings about > uninitialised memory if we later mess up the logic below. Also true. I'm kind of undecided at this point. I'd tend to leave that because it's fstatfs() and not something more commonly used/audited... but yes, I admit it's probably pointless at a practical level. -- Stefano