Skip to content

Commit

Permalink
af_packet: avoid a false positive warning in packet_setsockopt()
Browse files Browse the repository at this point in the history
BugLink: https://bugs.launchpad.net/bugs/2073765

[ Upstream commit 86d43e2bf93ccac88ef71cee36a23282ebd9e427 ]

Although the code is correct, the following line

	copy_from_sockptr(&req_u.req, optval, len));

triggers this warning :

memcpy: detected field-spanning write (size 28) of single field "dst" at include/linux/sockptr.h:49 (size 16)

Refactor the code to be more explicit.

Reported-by: syzbot <[email protected]>
Signed-off-by: Eric Dumazet <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Willem de Bruijn <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Reviewed-by: Willem de Bruijn <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
Signed-off-by: Portia Stephens <[email protected]>
Signed-off-by: Roxana Nicolescu <[email protected]>
  • Loading branch information
Eric Dumazet authored and roxanan1996 committed Aug 2, 2024
1 parent fba0d4d commit 61beb4c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions net/packet/af_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -3757,28 +3757,30 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
case PACKET_TX_RING:
{
union tpacket_req_u req_u;
int len;

ret = -EINVAL;
lock_sock(sk);
switch (po->tp_version) {
case TPACKET_V1:
case TPACKET_V2:
len = sizeof(req_u.req);
if (optlen < sizeof(req_u.req))
break;
ret = copy_from_sockptr(&req_u.req, optval,
sizeof(req_u.req)) ?
-EINVAL : 0;
break;
case TPACKET_V3:
default:
len = sizeof(req_u.req3);
if (optlen < sizeof(req_u.req3))
break;
ret = copy_from_sockptr(&req_u.req3, optval,
sizeof(req_u.req3)) ?
-EINVAL : 0;
break;
}
if (optlen < len) {
ret = -EINVAL;
} else {
if (copy_from_sockptr(&req_u.req, optval, len))
ret = -EFAULT;
else
ret = packet_set_ring(sk, &req_u, 0,
optname == PACKET_TX_RING);
}
if (!ret)
ret = packet_set_ring(sk, &req_u, 0,
optname == PACKET_TX_RING);
release_sock(sk);
return ret;
}
Expand Down

0 comments on commit 61beb4c

Please sign in to comment.