Skip to content

Commit 3c5a2fd

Browse files
arjunroydavem330
authored andcommitted
tcp: Sanitize CMSG flags and reserved args in tcp_zerocopy_receive.
Explicitly define reserved field and require it and any subsequent fields to be zero-valued for now. Additionally, limit the valid CMSG flags that tcp_zerocopy_receive accepts. Fixes: 7eeba17 ("tcp: Add receive timestamp support for receive zerocopy.") Signed-off-by: Arjun Roy <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: Soheil Hassas Yeganeh <[email protected]> Suggested-by: David Ahern <[email protected]> Suggested-by: Leon Romanovsky <[email protected]> Suggested-by: Jakub Kicinski <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9fbb4a7 commit 3c5a2fd

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

include/uapi/linux/tcp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,6 @@ struct tcp_zerocopy_receive {
357357
__u64 msg_control; /* ancillary data */
358358
__u64 msg_controllen;
359359
__u32 msg_flags;
360-
/* __u32 hole; Next we must add >1 u32 otherwise length checks fail. */
360+
__u32 reserved; /* set to 0 for now */
361361
};
362362
#endif /* _UAPI_LINUX_TCP_H */

net/ipv4/tcp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,7 @@ static int tcp_zerocopy_vm_insert_batch(struct vm_area_struct *vma,
20302030
err);
20312031
}
20322032

2033+
#define TCP_VALID_ZC_MSG_FLAGS (TCP_CMSG_TS)
20332034
static void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
20342035
struct scm_timestamping_internal *tss);
20352036
static void tcp_zc_finalize_rx_tstamp(struct sock *sk,
@@ -4152,13 +4153,21 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
41524153
return -EFAULT;
41534154
if (len < offsetofend(struct tcp_zerocopy_receive, length))
41544155
return -EINVAL;
4155-
if (len > sizeof(zc)) {
4156+
if (unlikely(len > sizeof(zc))) {
4157+
err = check_zeroed_user(optval + sizeof(zc),
4158+
len - sizeof(zc));
4159+
if (err < 1)
4160+
return err == 0 ? -EINVAL : err;
41564161
len = sizeof(zc);
41574162
if (put_user(len, optlen))
41584163
return -EFAULT;
41594164
}
41604165
if (copy_from_user(&zc, optval, len))
41614166
return -EFAULT;
4167+
if (zc.reserved)
4168+
return -EINVAL;
4169+
if (zc.msg_flags & ~(TCP_VALID_ZC_MSG_FLAGS))
4170+
return -EINVAL;
41624171
lock_sock(sk);
41634172
err = tcp_zerocopy_receive(sk, &zc, &tss);
41644173
release_sock(sk);

0 commit comments

Comments
 (0)