af_unix: Fix msg_controllen test in scm_pidfd_recv() for MSG_CMSG_COMPAT.
Subsystems (2)
PATCH 1/1 af_unix: Fix msg_controllen test in scm_pidfd_recv() for MSG_CMSG_COMPAT.
Heiko Carstens reported that SCM_PIDFD does not work with MSG_CMSG_COMPAT because scm_pidfd_recv() always checks msg_controllen against sizeof(struct cmsghdr).
We need to use sizeof(struct compat_cmsghdr) for the compat case.
--- include/net/scm.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
include/net/scm.h +9 -5
@@ -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)
@@ -123,14 +124,17 @@ static inline bool scm_has_secdata(struct socket *sock)static __inline__void scm_pidfd_recv (struct msghdr *msg,struct scm_cookie *scm) {struct file *pidfd_file =NULL ; -int pidfd; +int len, 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 )) { +if (msg->msg_flags &MSG_CMSG_COMPAT ) + len =sizeof (struct cmsghdr ) +sizeof (int ); +else + len =sizeof (struct compat_cmsghdr ) +sizeof (int ); + +if (msg->msg_controllen < len) { msg->msg_flags |=MSG_CTRUNC ;return ; }
Signature · 1 line
2.30.2
From: Kuniyuki Iwashima <[email protected]>
Date: Fri, 1 Sep 2023 15:20:33 -0700
Quoted 36 lines
Heiko Carstens reported that
SCM_PIDFDdoes not work withMSG_CMSG_COMPATbecausescm_pidfd_recv()always checksmsg_controllenagainst sizeof(struct cmsghdr).We need to use
sizeof(struct compat_cmsghdr)for the compat case.Fixes 5e2ff6704a27 ("scm: add SO_PASSPIDFD and SCM_PIDFD")Reported-by Heiko Carstens <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>Tested-by Heiko Carstens <[email protected]>Reviewed-by Alexander Mikhalitsyn <[email protected]>--- include/net/scm.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)include/net/scm.h +2
-2@@ -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) @@ -123,14 +124,17 @@ static inline bool scm_has_secdata(struct socket *sock)static __inline__void scm_pidfd_recv (struct msghdr *msg,struct scm_cookie *scm) {struct file *pidfd_file =NULL ; -int pidfd; +int len, pidfd; -/*
include/net/scm.h static inline bool scm_has_secdata(struct socket *sock) · line 135
Quoted 10 lines
include/net/scm.h +5
-3-* 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 )) { +if (msg->msg_flags &MSG_CMSG_COMPAT ) + len =sizeof (struct cmsghdr ) +sizeof (int ); +else + len =sizeof (struct compat_cmsghdr ) +sizeof (int );
Ugh.. apparently I did wrong copy-and-paste while refactoring. Will fix in v2.
pw-bot: cr
Quoted 7 lines
include/net/scm.h +2
-0+ +if (msg->msg_controllen < len) { msg->msg_flags |=MSG_CTRUNC ;return ; }Signature · 1 line
2.30.2