On 15-09-2023 08:56, Harry Ramsey wrote:
The tcp_zerocopy_receive struct is used to create a shared memory region between userspace and the kernel in which received network packets are directly copied from the network interface card.
In PCuABI, a user pointer is a 129 bit capability, the __u64 type is not sufficient enough to store it. Instead __kernel_uinptr_t is used instead which is sufficient enough to store a capability whilst remaining 64-bits on other architectures.
Additional checks have been added to ensure that the owning capability has read access in order to read the received network packets from the shared memory region.
Well done for adding the checks as well!
Signed-off-by: Harry Ramsey harry.ramsey@arm.com
include/linux/sockptr.h | 14 ++++++++++++++ include/uapi/linux/tcp.h | 20 ++++++++++---------- net/ipv4/tcp.c | 26 ++++++++++++++++++-------- 3 files changed, 42 insertions(+), 18 deletions(-)
diff --git a/include/linux/sockptr.h b/include/linux/sockptr.h index bae5e2369b4f..c81355d61d07 100644 --- a/include/linux/sockptr.h +++ b/include/linux/sockptr.h @@ -41,6 +41,20 @@ static inline bool sockptr_is_null(sockptr_t sockptr) return !sockptr.user; } +static inline int copy_from_sockptr_offset_with_ptr(void *dst, sockptr_t src,
size_t offset, size_t size)
+{
- if (!sockptr_is_kernel(src))
return copy_from_user_with_ptr(dst, src.user + offset, size);
- memcpy(dst, src.kernel + offset, size);
- return 0;
+}
+static inline int copy_from_sockptr_with_ptr(void *dst, sockptr_t src, size_t size) +{
- return copy_from_sockptr_offset_with_ptr(dst, src, 0, size);
+}
- static inline int copy_from_sockptr_offset(void *dst, sockptr_t src, size_t offset, size_t size) {
diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 879eeb0a084b..3854b7ace73e 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -352,17 +352,17 @@ struct tcp_diag_md5sig { #define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 struct tcp_zerocopy_receive {
- __u64 address; /* in: address of mapping */
- __u32 length; /* in/out: number of bytes to map/mapped */
- __u32 recv_skip_hint; /* out: amount of bytes to skip */
- __u32 inq; /* out: amount of bytes in read queue */
- __s32 err; /* out: socket error */
- __u64 copybuf_address; /* in: copybuf address (small reads) */
- __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
- __u32 flags; /* in: flags */
- __u64 msg_control; /* ancillary data */
- __kernel_uintptr_t address; /* in: address of mapping */
- __u32 length; /* in/out: number of bytes to map/mapped */
- __u32 recv_skip_hint; /* out: amount of bytes to skip */
- __u32 inq; /* out: amount of bytes in read queue */
- __s32 err; /* out: socket error */
- __kernel_uintptr_t copybuf_address; /* in: copybuf address (small reads) */
- __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
- __u32 flags; /* in: flags */
- __kernel_uintptr_t msg_control; /* ancillary data */ __u64 msg_controllen; __u32 msg_flags;
- __u32 reserved; /* set to 0 for now */
- __u32 reserved; /* set to 0 for now */
nit: the situation with the comments is interesting... I am not sure if it's worth aligning them. I would especially leave the comment for `reserved` where it was so that it doesn't hinder future rebases.
}; #endif /* _UAPI_LINUX_TCP_H */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 90526625671b..440b00e763e6 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -267,6 +267,7 @@ #include <linux/errqueue.h> #include <linux/static_key.h> #include <linux/btf.h> +#include <linux/user_ptr.h> #include <net/icmp.h> #include <net/inet_common.h> @@ -1984,7 +1985,7 @@ static int receive_fallback_to_copy(struct sock *sk, struct tcp_zerocopy_receive *zc, int inq, struct scm_timestamping_internal *tss) {
- unsigned long copy_address = (unsigned long)zc->copybuf_address;
- __kernel_uintptr_t copy_address = zc->copybuf_address; struct msghdr msg = {}; struct iovec iov; int err;
@@ -1995,6 +1996,9 @@ static int receive_fallback_to_copy(struct sock *sk, if (copy_address != zc->copybuf_address) return -EINVAL;
- if (check_user_ptr_read((void __user *)zc->copybuf_address, zc->copybuf_len))
return -EFAULT;
- err = import_single_range(ITER_DEST, uaddr_to_user_ptr(copy_address), inq, &iov, &msg.msg_iter);
uaddr_to_user_ptr weakens the enforcement of the capability model as it creates a capability from an arbitrary user address. You already have a valid capability here, so don't need to to us uaddr_to_user_ptr. Ideally we get remove most uaddr_to_user_ptr instances. In this case, you already propagated the capability, so don't need to use it at all. There are 2 more instances in this file that you can just remove them. Well done!
Thanks, Tudor
if (err) @@ -2021,7 +2025,7 @@ static int tcp_copy_straggler_data(struct tcp_zerocopy_receive *zc, struct sk_buff *skb, u32 copylen, u32 *offset, u32 *seq) {
- unsigned long copy_address = (unsigned long)zc->copybuf_address;
- __kernel_uintptr_t copy_address = zc->copybuf_address; struct msghdr msg = {}; struct iovec iov; int err;
@@ -2029,6 +2033,9 @@ static int tcp_copy_straggler_data(struct tcp_zerocopy_receive *zc, if (copy_address != zc->copybuf_address) return -EINVAL;
- if (check_user_ptr_read((void __user *)zc->copybuf_address, zc->copybuf_len))
return -EFAULT;
- err = import_single_range(ITER_DEST, uaddr_to_user_ptr(copy_address), copylen, &iov, &msg.msg_iter); if (err)
@@ -2072,7 +2079,7 @@ static int tcp_zc_handle_leftover(struct tcp_zerocopy_receive *zc, static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, struct page **pending_pages, unsigned long pages_remaining,
unsigned long *address,
__kernel_uintptr_t *address, u32 *length, u32 *seq, struct tcp_zerocopy_receive *zc,
@@ -2120,7 +2127,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, static int tcp_zerocopy_vm_insert_batch(struct vm_area_struct *vma, struct page **pages, unsigned int pages_to_map,
unsigned long *address,
__kernel_uintptr_t *address, u32 *length, u32 *seq, struct tcp_zerocopy_receive *zc,
@@ -2154,10 +2161,10 @@ static void tcp_zc_finalize_rx_tstamp(struct sock *sk, struct tcp_zerocopy_receive *zc, struct scm_timestamping_internal *tss) {
- unsigned long msg_control_addr;
- __kernel_uintptr_t msg_control_addr; struct msghdr cmsg_dummy;
- msg_control_addr = (unsigned long)zc->msg_control;
- msg_control_addr = zc->msg_control; cmsg_dummy.msg_control_user = uaddr_to_user_ptr(msg_control_addr); cmsg_dummy.msg_controllen = (__kernel_size_t)zc->msg_controllen;
@@ -2182,7 +2189,7 @@ static int tcp_zerocopy_receive(struct sock *sk, struct scm_timestamping_internal *tss) { u32 length = 0, offset, vma_len, avail_len, copylen = 0;
- unsigned long address = (unsigned long)zc->address;
- __kernel_uintptr_t address = zc->address; struct page *pages[TCP_ZEROCOPY_PAGE_BATCH_SIZE]; s32 copybuf_len = zc->copybuf_len; struct tcp_sock *tp = tcp_sk(sk);
@@ -2198,6 +2205,9 @@ static int tcp_zerocopy_receive(struct sock *sk, zc->copybuf_len = 0; zc->msg_flags = 0;
- if (!check_user_ptr_read((void __user *)zc->address, zc->length))
return -EFAULT;
- if (address & (PAGE_SIZE - 1) || address != zc->address) return -EINVAL;
@@ -4332,7 +4342,7 @@ int do_tcp_getsockopt(struct sock *sk, int level, if (copy_to_sockptr(optlen, &len, sizeof(int))) return -EFAULT; }
if (copy_from_sockptr(&zc, optval, len))
if (zc.reserved) return -EINVAL;if (copy_from_sockptr_with_ptr(&zc, optval, len)) return -EFAULT;