Skip to content

Commit 86d43e2

Browse files
edumazetdavem330
authored andcommitted
af_packet: avoid a false positive warning in packet_setsockopt()
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]>
1 parent a29689e commit 86d43e2

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
@@ -3800,28 +3800,30 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
38003800
case PACKET_TX_RING:
38013801
{
38023802
union tpacket_req_u req_u;
3803-
int len;
38043803

3804+
ret = -EINVAL;
38053805
lock_sock(sk);
38063806
switch (po->tp_version) {
38073807
case TPACKET_V1:
38083808
case TPACKET_V2:
3809-
len = sizeof(req_u.req);
3809+
if (optlen < sizeof(req_u.req))
3810+
break;
3811+
ret = copy_from_sockptr(&req_u.req, optval,
3812+
sizeof(req_u.req)) ?
3813+
-EINVAL : 0;
38103814
break;
38113815
case TPACKET_V3:
38123816
default:
3813-
len = sizeof(req_u.req3);
3817+
if (optlen < sizeof(req_u.req3))
3818+
break;
3819+
ret = copy_from_sockptr(&req_u.req3, optval,
3820+
sizeof(req_u.req3)) ?
3821+
-EINVAL : 0;
38143822
break;
38153823
}
3816-
if (optlen < len) {
3817-
ret = -EINVAL;
3818-
} else {
3819-
if (copy_from_sockptr(&req_u.req, optval, len))
3820-
ret = -EFAULT;
3821-
else
3822-
ret = packet_set_ring(sk, &req_u, 0,
3823-
optname == PACKET_TX_RING);
3824-
}
3824+
if (!ret)
3825+
ret = packet_set_ring(sk, &req_u, 0,
3826+
optname == PACKET_TX_RING);
38253827
release_sock(sk);
38263828
return ret;
38273829
}

0 commit comments

Comments
 (0)