netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <hca@linux.ibm.com>
Cc: <aleksandr.mikhalitsyn@canonical.com>, <arnd@arndb.de>,
	<bluca@debian.org>, <brauner@kernel.org>, <davem@davemloft.net>,
	<dsahern@kernel.org>, <edumazet@google.com>,
	<keescook@chromium.org>, <kuba@kernel.org>, <kuniyu@amazon.com>,
	<ldv@strace.io>, <leon@kernel.org>, <linux-arch@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <mzxreary@0pointer.de>,
	<netdev@vger.kernel.org>, <pabeni@redhat.com>
Subject: Re: [PATCH net-next v7 1/4] scm: add SO_PASSPIDFD and SCM_PIDFD
Date: Fri, 1 Sep 2023 13:33:22 -0700	[thread overview]
Message-ID: <20230901203322.56399-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20230901200517.8742-A-hca@linux.ibm.com>

From: Heiko Carstens <hca@linux.ibm.com>
Date: Fri, 1 Sep 2023 22:05:17 +0200
> On Thu, Jun 08, 2023 at 10:26:25PM +0200, Alexander Mikhalitsyn wrote:
> > Implement SCM_PIDFD, a new type of CMSG type analogical to SCM_CREDENTIALS,
> > but it contains pidfd instead of plain pid, which allows programmers not
> > to care about PID reuse problem.
> > 
> > We mask SO_PASSPIDFD feature if CONFIG_UNIX is not builtin because
> > it depends on a pidfd_prepare() API which is not exported to the kernel
> > modules.
> > 
> > Idea comes from UAPI kernel group:
> > https://uapi-group.org/kernel-features/
> > 
> > Big thanks to Christian Brauner and Lennart Poettering for productive
> > discussions about this.
> > 
> > Cc: "David S. Miller" <davem@davemloft.net>
> > Cc: Eric Dumazet <edumazet@google.com>
> > Cc: Jakub Kicinski <kuba@kernel.org>
> > Cc: Paolo Abeni <pabeni@redhat.com>
> > Cc: Leon Romanovsky <leon@kernel.org>
> > Cc: David Ahern <dsahern@kernel.org>
> > Cc: Arnd Bergmann <arnd@arndb.de>
> > Cc: Kees Cook <keescook@chromium.org>
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> > Cc: Lennart Poettering <mzxreary@0pointer.de>
> > Cc: Luca Boccassi <bluca@debian.org>
> > Cc: linux-kernel@vger.kernel.org
> > Cc: netdev@vger.kernel.org
> > Cc: linux-arch@vger.kernel.org
> > Tested-by: Luca Boccassi <bluca@debian.org>
> > Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > Reviewed-by: Christian Brauner <brauner@kernel.org>
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  arch/alpha/include/uapi/asm/socket.h    |  2 ++
> >  arch/mips/include/uapi/asm/socket.h     |  2 ++
> >  arch/parisc/include/uapi/asm/socket.h   |  2 ++
> >  arch/sparc/include/uapi/asm/socket.h    |  2 ++
> >  include/linux/net.h                     |  1 +
> >  include/linux/socket.h                  |  1 +
> >  include/net/scm.h                       | 39 +++++++++++++++++++++++--
> >  include/uapi/asm-generic/socket.h       |  2 ++
> >  net/core/sock.c                         | 11 +++++++
> >  net/mptcp/sockopt.c                     |  1 +
> >  net/unix/af_unix.c                      | 18 ++++++++----
> >  tools/include/uapi/asm-generic/socket.h |  2 ++
> >  12 files changed, 76 insertions(+), 7 deletions(-)
> ...
> > +static __inline__ void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
> > +{
> > +	struct file *pidfd_file = NULL;
> > +	int pidfd;
> > +
> > +	/*
> > +	 * put_cmsg() doesn't return an error if CMSG is truncated,
> > +	 * that's why we need to opencode these checks here.
> > +	 */
> > +	if ((msg->msg_controllen <= sizeof(struct cmsghdr)) ||
> > +	    (msg->msg_controllen - sizeof(struct cmsghdr)) < sizeof(int)) {
> > +		msg->msg_flags |= MSG_CTRUNC;
> > +		return;
> > +	}
> 
> This does not work for compat tasks since the size of struct cmsghdr (aka
> struct compat_cmsghdr) is differently. If the check from put_cmsg() is
> open-coded here, then also a different check for compat tasks needs to be
> added.
> 
> Discovered this because I was wondering why strace compat tests fail; it
> seems because of this.
> 
> See https://github.com/strace/strace/blob/master/tests/scm_pidfd.c
> 
> For compat tasks recvmsg() returns with msg_flags=MSG_CTRUNC since the
> above code expects a larger buffer than is necessary.

Can you test this ?

---8<---
diff --git a/include/net/scm.h b/include/net/scm.h
index c5bcdf65f55c..099497ce4aee 100644
--- a/include/net/scm.h
+++ b/include/net/scm.h
@@ -9,6 +9,7 @@
 #include <linux/pid.h>
 #include <linux/nsproxy.h>
 #include <linux/sched/signal.h>
+#include <net/compat.h>
 
 /* Well, we should have at least one descriptor open
  * to accept passed FDs 8)
@@ -125,14 +126,19 @@ static __inline__ void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm
 	struct file *pidfd_file = NULL;
 	int pidfd;
 
-	/*
-	 * put_cmsg() doesn't return an error if CMSG is truncated,
+	/* put_cmsg() doesn't return an error if CMSG is truncated,
 	 * that's why we need to opencode these checks here.
 	 */
-	if ((msg->msg_controllen <= sizeof(struct cmsghdr)) ||
-	    (msg->msg_controllen - sizeof(struct cmsghdr)) < sizeof(int)) {
-		msg->msg_flags |= MSG_CTRUNC;
-		return;
+	if (msg->msg_flags & MSG_CMSG_COMPAT) {
+		if (msg->msg_controllen < sizeof(struct compat_cmsghdr) + sizeof(int)) {
+			msg->msg_flags |= MSG_CTRUNC;
+			return;
+		}
+	} else {
+		if (msg->msg_controllen < sizeof(struct cmsghdr) + sizeof(int)) {
+			msg->msg_flags |= MSG_CTRUNC;
+			return;
+		}
 	}
 
 	if (!scm->pid)
---8<---

  reply	other threads:[~2023-09-01 20:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 20:26 [PATCH net-next v7 0/4] Add SCM_PIDFD and SO_PEERPIDFD Alexander Mikhalitsyn
2023-06-08 20:26 ` [PATCH net-next v7 1/4] scm: add SO_PASSPIDFD and SCM_PIDFD Alexander Mikhalitsyn
2023-06-12  9:19   ` Eric Dumazet
2023-06-12  9:26     ` Aleksandr Mikhalitsyn
2023-09-01 20:05   ` Heiko Carstens
2023-09-01 20:33     ` Kuniyuki Iwashima [this message]
2023-09-01 20:51       ` Heiko Carstens
2023-09-01 20:56         ` Kuniyuki Iwashima
2023-09-01 21:11           ` Aleksandr Mikhalitsyn
2023-06-08 20:26 ` [PATCH net-next v7 2/4] net: core: add getsockopt SO_PEERPIDFD Alexander Mikhalitsyn
2023-06-12  9:22   ` Eric Dumazet
2023-06-08 20:26 ` [PATCH net-next v7 3/4] selftests: net: add SCM_PIDFD / SO_PEERPIDFD test Alexander Mikhalitsyn
2023-06-08 20:26 ` [PATCH net-next v7 4/4] af_unix: Kconfig: make CONFIG_UNIX bool Alexander Mikhalitsyn
2023-06-09  8:06   ` Christian Brauner
2023-06-12  9:25   ` Eric Dumazet
2023-06-12  9:50 ` [PATCH net-next v7 0/4] Add SCM_PIDFD and SO_PEERPIDFD patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230901203322.56399-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=aleksandr.mikhalitsyn@canonical.com \
    --cc=arnd@arndb.de \
    --cc=bluca@debian.org \
    --cc=brauner@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=hca@linux.ibm.com \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=ldv@strace.io \
    --cc=leon@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mzxreary@0pointer.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).