Skip to content

Commit 6331004

Browse files
edumazetgregkh
authored andcommitted
af_packet: avoid a false positive warning in packet_setsockopt()
[ Upstream commit 86d43e2 ] 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]>
1 parent 2e48d73 commit 6331004

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

net/packet/af_packet.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,28 +3791,30 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
37913791
case PACKET_TX_RING:
37923792
{
37933793
union tpacket_req_u req_u;
3794-
int len;
37953794

3795+
ret = -EINVAL;
37963796
lock_sock(sk);
37973797
switch (po->tp_version) {
37983798
case TPACKET_V1:
37993799
case TPACKET_V2:
3800-
len = sizeof(req_u.req);
3800+
if (optlen < sizeof(req_u.req))
3801+
break;
3802+
ret = copy_from_sockptr(&req_u.req, optval,
3803+
sizeof(req_u.req)) ?
3804+
-EINVAL : 0;
38013805
break;
38023806
case TPACKET_V3:
38033807
default:
3804-
len = sizeof(req_u.req3);
3808+
if (optlen < sizeof(req_u.req3))
3809+
break;
3810+
ret = copy_from_sockptr(&req_u.req3, optval,
3811+
sizeof(req_u.req3)) ?
3812+
-EINVAL : 0;
38053813
break;
38063814
}
3807-
if (optlen < len) {
3808-
ret = -EINVAL;
3809-
} else {
3810-
if (copy_from_sockptr(&req_u.req, optval, len))
3811-
ret = -EFAULT;
3812-
else
3813-
ret = packet_set_ring(sk, &req_u, 0,
3814-
optname == PACKET_TX_RING);
3815-
}
3815+
if (!ret)
3816+
ret = packet_set_ring(sk, &req_u, 0,
3817+
optname == PACKET_TX_RING);
38163818
release_sock(sk);
38173819
return ret;
38183820
}

0 commit comments

Comments
 (0)