Skip to contentSkip to conversation

net:socket: set msg_namelen to 0 if msg_name is passed as NULL in msghdr struct from userland.

PATCH Applied Ani Sinha 2 participants 3 messages Sep 8, 2014 – Sep 10, 2014

Subsystems (3)
lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH 1/1 net:socket: set msg_namelen to 0 if msg_name is passed as NULL in msghdr struct from userland.

Linux manpage for recvmsg and sendmsg calls does not explicitly mention setting msg_namelen to 0 when msg_name passed set as NULL. When developers don't set msg_namelen member in msghdr, it might contain garbage value which will fail the validation check and sendmsg and recvmsg calls from kernel will return EINVAL. This will break old binaries and any code for which there is no access to source code.
To fix this, we set msg_namelen to 0 when msg_name is passed as NULL from userland.

---
 net/socket.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
net/socket.c +3 -0
index 95ee7d8..457be6a 100644
@@ -1997,6 +1997,9 @@ static int copy_msghdr_from_user(struct msghdr *kmsg,
 	if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
 		return -EFAULT;
 
+	if (kmsg->msg_name == NULL)
+		kmsg->msg_namelen = 0;
+
 	if (kmsg->msg_namelen < 0)
 		return -EINVAL;
 
Signature · 1 line
1.7.4.4
author
#1 lore raw

Any feedback on this patch? Any concerns?

Quoted 28 lines · Ani Sinha

Linux manpage for recvmsg and sendmsg calls does not explicitly mention setting msg_namelen to 0 when msg_name passed set as NULL. When developers don't set msg_namelen member in msghdr, it might contain garbage value which will fail the validation check and sendmsg and recvmsg calls from kernel will return EINVAL. This will break old binaries and any code for which there is no access to source code.
To fix this, we set msg_namelen to 0 when msg_name is passed as NULL from userland.

---
 net/socket.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
net/socket.c +3 -1
index 95ee7d8..457be6a 100644
@@ -1997,6 +1997,9 @@ static int copy_msghdr_from_user(struct msghdr *kmsg,
        if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
                return -EFAULT;
 
+       if (kmsg->msg_name == NULL)
+               kmsg->msg_namelen = 0;
+
        if (kmsg->msg_namelen < 0)
                return -EINVAL;
 
--

1.7.4.4

maintainer Applied
#2 lore raw
In reply to Ani Sinha

From: Ani Sinha <[email protected]>
Date: Mon, 8 Sep 2014 14:49:59 -0700

Quoted 7 lines

Linux manpage for recvmsg and sendmsg calls does not explicitly mention setting msg_namelen to 0 when msg_name passed set as NULL. When developers don't set msg_namelen member in msghdr, it might contain garbage value which will fail the validation check and sendmsg and recvmsg calls from kernel will return EINVAL. This will break old binaries and any code for which there is no access to source code.
To fix this, we set msg_namelen to 0 when msg_name is passed as NULL from userland.

Applied, thanks.