sctp: Define usercopy region in SCTP proto slab cache
authorDavid Windsor <dave@nullcore.net>
Thu, 24 Aug 2017 23:57:57 +0000 (16:57 -0700)
committerKees Cook <keescook@chromium.org>
Mon, 15 Jan 2018 20:08:00 +0000 (12:08 -0800)
The SCTP socket event notification subscription information need to be
copied to/from userspace. In support of usercopy hardening, this patch
defines a region in the struct proto slab cache in which userspace copy
operations are allowed. Additionally moves the usercopy fields to be
adjacent for the region to cover both.

example usage trace:

    net/sctp/socket.c:
        sctp_getsockopt_events(...):
            ...
            copy_to_user(..., &sctp_sk(sk)->subscribe, len)

        sctp_setsockopt_events(...):
            ...
            copy_from_user(&sctp_sk(sk)->subscribe, ..., optlen)

        sctp_getsockopt_initmsg(...):
            ...
            copy_to_user(..., &sctp_sk(sk)->initmsg, len)

This region is known as the slab cache's usercopy region. Slab caches
can now check that each dynamically sized copy operation involving
cache-managed memory falls entirely within the slab's usercopy region.

This patch is modified from Brad Spengler/PaX Team's PAX_USERCOPY
whitelisting code in the last public patch of grsecurity/PaX based on my
understanding of the code. Changes or omissions from the original code are
mine and don't reflect the original grsecurity/PaX code.

Signed-off-by: David Windsor <dave@nullcore.net>
[kees: split from network patch, move struct members adjacent]
[kees: add SCTPv6 struct whitelist, provide usage trace]
Cc: Vlad Yasevich <vyasevich@gmail.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: linux-sctp@vger.kernel.org
Cc: netdev@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
include/net/sctp/structs.h
net/sctp/socket.c

index 16f949eef52fdfd7c90fa15b44093334d1355aaf..6168e34491318975d74322267ead8364248599b1 100644 (file)
@@ -202,12 +202,17 @@ struct sctp_sock {
        /* Flags controlling Heartbeat, SACK delay, and Path MTU Discovery. */
        __u32 param_flags;
 
-       struct sctp_initmsg initmsg;
        struct sctp_rtoinfo rtoinfo;
        struct sctp_paddrparams paddrparam;
-       struct sctp_event_subscribe subscribe;
        struct sctp_assocparams assocparams;
 
+       /*
+        * These two structures must be grouped together for the usercopy
+        * whitelist region.
+        */
+       struct sctp_event_subscribe subscribe;
+       struct sctp_initmsg initmsg;
+
        int user_frag;
 
        __u32 autoclose;
index 014847e25648182dbf99d8fb095e094af76264bb..efbc8f52c53162019670f66d8d9e0bcfa54959a2 100644 (file)
@@ -8470,6 +8470,10 @@ struct proto sctp_prot = {
        .unhash      =  sctp_unhash,
        .get_port    =  sctp_get_port,
        .obj_size    =  sizeof(struct sctp_sock),
+       .useroffset  =  offsetof(struct sctp_sock, subscribe),
+       .usersize    =  offsetof(struct sctp_sock, initmsg) -
+                               offsetof(struct sctp_sock, subscribe) +
+                               sizeof_field(struct sctp_sock, initmsg),
        .sysctl_mem  =  sysctl_sctp_mem,
        .sysctl_rmem =  sysctl_sctp_rmem,
        .sysctl_wmem =  sysctl_sctp_wmem,
@@ -8509,6 +8513,10 @@ struct proto sctpv6_prot = {
        .unhash         = sctp_unhash,
        .get_port       = sctp_get_port,
        .obj_size       = sizeof(struct sctp6_sock),
+       .useroffset     = offsetof(struct sctp6_sock, sctp.subscribe),
+       .usersize       = offsetof(struct sctp6_sock, sctp.initmsg) -
+                               offsetof(struct sctp6_sock, sctp.subscribe) +
+                               sizeof_field(struct sctp6_sock, sctp.initmsg),
        .sysctl_mem     = sysctl_sctp_mem,
        .sysctl_rmem    = sysctl_sctp_rmem,
        .sysctl_wmem    = sysctl_sctp_wmem,