Skip to contentSkip to conversation

af_unix: Fix msg_controllen test in scm_pidfd_recv() for MSG_CMSG_COMPAT.

PATCH Applied Kuniyuki Iwashima 4 participants 4 messages Sep 1, 2023 – Sep 4, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v2:
* Correct len in compat/non-compat case

v1: https://lore.kernel.org/netdev/[email protected]/T/#u

---
 include/net/scm.h | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
include/net/scm.h +9 -5
index c5bcdf65f55c..e8c76b4be2fe 100644
@@ -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 compat_cmsghdr) + sizeof(int);
+	else
+		len = sizeof(struct cmsghdr) + sizeof(int);
+
+	if (msg->msg_controllen < len) {
 		msg->msg_flags |= MSG_CTRUNC;
 		return;
 	}
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 46 lines · Kuniyuki Iwashima

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.

---
v2:
* Correct len in compat/non-compat case

v1: https://lore.kernel.org/netdev/[email protected]/T/#u

---
 include/net/scm.h | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
include/net/scm.h +3 -4
index c5bcdf65f55c..e8c76b4be2fe 100644
@@ -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)) ||

include/net/scm.h static inline bool scm_has_secdata(struct socket *sock) · line 140

Quoted 10 lines
include/net/scm.h +6 -1
-	    (msg->msg_controllen - sizeof(struct cmsghdr)) < sizeof(int)) {
+	if (msg->msg_flags & MSG_CMSG_COMPAT)
+		len = sizeof(struct compat_cmsghdr) + sizeof(int);
+	else
+		len = sizeof(struct cmsghdr) + sizeof(int);
+
+	if (msg->msg_controllen < len) {
 		msg->msg_flags |= MSG_CTRUNC;
 		return;
 	}

Seems fine, you can point out in commit message that you are merging both conditions into one (size comparation and this sizeof(int))

Thanks

Quoted 2 lines
Signature · 1 line
2.30.2
Acked-by on v2 1/1
#2 lore raw
In reply to Kuniyuki Iwashima
Quoted 12 lines · Kuniyuki Iwashima

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.

Quoted 6 lines

---
v2:
* Correct len in compat/non-compat case

v1: https://lore.kernel.org/netdev/[email protected]/T/#u
---

#3 lore raw
In reply to Kuniyuki Iwashima
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 6 lines · On Fri, 1 Sep 2023 16:46:04 -0700 you wrote:

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.

Quoted 8 lines

[...]

Here is the summary with links:
- [v2,net] af_unix: Fix msg_controllen test in scm_pidfd_recv() for MSG_CMSG_COMPAT.

    https://git.kernel.org/netdev/net/c/718e6b51298e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html