sock: Use unsafe_memcpy() for sock_copy()
authorKees Cook <keescook@chromium.org>
Mon, 4 Mar 2024 21:29:31 +0000 (13:29 -0800)
committerJakub Kicinski <kuba@kernel.org>
Wed, 6 Mar 2024 02:35:12 +0000 (18:35 -0800)
While testing for places where zero-sized destinations were still showing
up in the kernel, sock_copy() and inet_reqsk_clone() were found, which
are using very specific memcpy() offsets for both avoiding a portion of
struct sock, and copying beyond the end of it (since struct sock is really
just a common header before the protocol-specific allocation). Instead
of trying to unravel this historical lack of container_of(), just switch
to unsafe_memcpy(), since that's effectively what was happening already
(memcpy() wasn't checking 0-sized destinations while the code base was
being converted away from fake flexible arrays).

Avoid the following false positive warning with future changes to
CONFIG_FORTIFY_SOURCE:

  memcpy: detected field-spanning write (size 3068) of destination "&nsk->__sk_common.skc_dontcopy_end" at net/core/sock.c:2057 (size 0)

Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://lore.kernel.org/r/20240304212928.make.772-kees@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/sock.c
net/ipv4/inet_connection_sock.c

index df2ac54a8f74e75881eb3dcf59e150a55b36eb99..43bf3818c19e829b47d3989d36e2e1b3bf985438 100644 (file)
@@ -2053,8 +2053,9 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
 
        memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
 
-       memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
-              prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
+       unsafe_memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
+                     prot->obj_size - offsetof(struct sock, sk_dontcopy_end),
+                     /* alloc is larger than struct, see sk_prot_alloc() */);
 
 #ifdef CONFIG_SECURITY_NETWORK
        nsk->sk_security = sptr;
index 747ed7344cbe4555860d81802dced6f99d2efc1d..7d8090f109ef4e794a13fb6ab5d180b16bafb59d 100644 (file)
@@ -906,8 +906,9 @@ static struct request_sock *inet_reqsk_clone(struct request_sock *req,
 
        memcpy(nreq_sk, req_sk,
               offsetof(struct sock, sk_dontcopy_begin));
-       memcpy(&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end,
-              req->rsk_ops->obj_size - offsetof(struct sock, sk_dontcopy_end));
+       unsafe_memcpy(&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end,
+                     req->rsk_ops->obj_size - offsetof(struct sock, sk_dontcopy_end),
+                     /* alloc is larger than struct, see above */);
 
        sk_node_init(&nreq_sk->sk_node);
        nreq_sk->sk_tx_queue_mapping = req_sk->sk_tx_queue_mapping;