diff mbox

net/packet: check length in getsockopt() called with PACKET_HDRLEN

Message ID 20170425165146.25075-1-glider@google.com
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Alexander Potapenko April 25, 2017, 4:51 p.m. UTC
In the case getsockopt() is called with PACKET_HDRLEN and optlen < 4
|val| remains uninitialized and the syscall may behave differently
depending on its value, and even copy garbage to userspace on certain
architectures. To fix this we now return -EINVAL if optlen is too small.

This bug has been detected with KMSAN.

Signed-off-by: Alexander Potapenko <glider@google.com>

---
The previous versions of this patch were called "net/packet: initialize
val in packet_getsockopt()"

v3: - change patch summary, return -EINVAL for optlen < sizeof(int)
v2: - if len < sizeof(int), make it 0

---
 net/packet/af_packet.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

David Miller April 25, 2017, 6:06 p.m. UTC | #1
From: Alexander Potapenko <glider@google.com>
Date: Tue, 25 Apr 2017 18:51:46 +0200

> In the case getsockopt() is called with PACKET_HDRLEN and optlen < 4
> |val| remains uninitialized and the syscall may behave differently
> depending on its value, and even copy garbage to userspace on certain
> architectures. To fix this we now return -EINVAL if optlen is too small.
> 
> This bug has been detected with KMSAN.
> 
> Signed-off-by: Alexander Potapenko <glider@google.com>

Applied, thanks.
diff mbox

Patch

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 8489beff5c25..ea81ccf3c7d6 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3836,6 +3836,8 @@  static int packet_getsockopt(struct socket *sock, int level, int optname,
 	case PACKET_HDRLEN:
 		if (len > sizeof(int))
 			len = sizeof(int);
+		if (len < sizeof(int))
+			return -EINVAL;
 		if (copy_from_user(&val, optval, len))
 			return -EFAULT;
 		switch (val) {