selinux: fix NULL pointer dereference in selinux_sctp_bind_connect()
Subsystems (2)
PATCH 1/1 selinux: fix NULL pointer dereference in selinux_sctp_bind_connect()
From: Tristan Madani <[email protected]>
selinux_sctp_bind_connect() reads sk->sk_socket and passes it to selinux_socket_bind() or selinux_socket_connect_helper() without checking for NULL. When an SCTP ASCONF chunk is processed in softirq context on a socket that has been concurrently closed, sock_orphan() will have already set sk->sk_socket to NULL. The subsequent dereference of sock->sk at offset 0x18 triggers a kernel panic.
Add a NULL check on sk->sk_socket before use.
--- security/selinux/hooks.c | 3 +++ 1 file changed, 3 insertions(+)
security/selinux/hooks.c +3 -0
@@ -5717,6 +5717,9 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,/* Process one or more addresses that may be IPv4 or IPv6 */ sock = sk->sk_socket; +if (!sock) +return -ECONNRESET ; + addr_buf = address;while (walk_size < addrlen) {
Signature · 1 line
2.47.3
Stephen Smalley
maintainerQuoted 11 lines · Tristan Madani
From: Tristan Madani <[email protected]>
selinux_sctp_bind_connect()readssk->sk_socketand passes it toselinux_socket_bind()orselinux_socket_connect_helper()without checking for NULL. When an SCTP ASCONF chunk is processed in softirq context on a socket that has been concurrently closed,sock_orphan()will have already setsk->sk_socketto NULL. The subsequent dereference ofsock->skat offset0x18triggers a kernel panic.Add a NULL check on
sk->sk_socketbefore use.
Is this sufficient, or can the sk_socket be freed under us after the assignment? Do different callers of this hook provide different guarantees regarding sk_socket or are they all the same?
Quoted 25 lines
Fixes d452930fd3b9 ("selinux: Add SCTP support")Signed-off-by Tristan Madani <[email protected]>--- security/selinux/hooks.c | 3 +++ 1 file changed, 3 insertions(+)security/selinux/hooks.c +3
-1@@ -5717,6 +5717,9 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,/* Process one or more addresses that may be IPv4 or IPv6 */ sock = sk->sk_socket; +if (!sock) +return -ECONNRESET ; + addr_buf = address;while (walk_size < addrlen) { --2.47.3
Quoted 2 lines · Stephen Smalley
Is this sufficient, or can the
sk_socketbe freed under us after the assignment?
The assignment is safe. sock_orphan() only NULLs sk->sk_socket -- the struct socket is freed later in __sock_release(), after inet_release() returns. That path goes through sctp_close() -> lock_sock(), which serializes with the ASCONF softirq path (bh_lock_sock). So once we read a non-NULL pointer into the local variable, the socket is guaranteed to remain alive for the duration of the function.
Quoted 2 lines
Do different callers of this hook provide different guarantees regarding
sk_socketor are they all the same?
They differ. The setsockopt callers (bindx, connectx, set_primary, sendmsg connect) run in process context with a file reference, so sk_socket is guaranteed non-NULL. The ASCONF softirq path (sctp_process_asconf) has no file reference and can race with socket close -- that is the only caller that can hit the NULL.
Tristan
Stephen Smalley
maintainerQuoted 12 lines · Tristan Madani
Quoted 2 lines · Stephen Smalley
Is this sufficient, or can the
sk_socketbe freed under us after the assignment?The assignment is safe.
sock_orphan()only NULLssk->sk_socket-- the struct socket is freed later in__sock_release(), afterinet_release()returns. That path goes throughsctp_close()->lock_sock(), which serializes with the ASCONF softirq path (bh_lock_sock). So once we read a non-NULL pointer into the local variable, the socket is guaranteed to remain alive for the duration of the function.
Quoted 8 lines
Quoted 2 lines
Do different callers of this hook provide different guarantees regarding
sk_socketor are they all the same?They differ. The setsockopt callers (bindx, connectx,
set_primary, sendmsg connect) run in process context with a file reference, sosk_socketis guaranteed non-NULL. The ASCONF softirq path (sctp_process_asconf) has no file reference and can race with socket close -- that is the only caller that can hit the NULL.
Thank you for clarifying. It might be good to add some or all of the above to the patch description and/or
a comment in the code to make it clear going forward.
Stephen Smalley
maintainerQuoted 23 lines · Stephen Smalley
Quoted 12 lines · Tristan Madani
Quoted 2 lines · Stephen Smalley
Is this sufficient, or can the
sk_socketbe freed under us after the assignment?The assignment is safe.
sock_orphan()only NULLssk->sk_socket-- the struct socket is freed later in__sock_release(), afterinet_release()returns. That path goes throughsctp_close()->lock_sock(), which serializes with the ASCONF softirq path (bh_lock_sock). So once we read a non-NULL pointer into the local variable, the socket is guaranteed to remain alive for the duration of the function.Quoted 8 lines
Quoted 2 lines
Do different callers of this hook provide different guarantees regarding
sk_socketor are they all the same?They differ. The setsockopt callers (bindx, connectx,
set_primary, sendmsg connect) run in process context with a file reference, sosk_socketis guaranteed non-NULL. The ASCONF softirq path (sctp_process_asconf) has no file reference and can race with socket close -- that is the only caller that can hit the NULL.
Quoted 3 lines
Thank you for clarifying. It might be good to add some or all of the above to the patch description and/or
a comment in the code to make it clear going forward.
Actually, given that selinux_sctp_bind_connect() just passes the socket to selinux_socket_bind() or selinux_socket_connect_helper() and the first thing those functions do is to grab the sock from it, it seems like we could just refactor helpers that directly take the struct sock * and never have to deal with this issue at all.
Otherwise, we likely want a READ_ONCE() here as well.
PATCH v2 1/1 selinux: avoid sk_socket dereference in selinux_sctp_bind_connect()
In reply to Tristan MadaniFrom: Tristan Madani <[email protected]>
selinux_sctp_bind_connect() dereferences sk->sk_socket to pass a struct socket * to selinux_socket_bind() and selinux_socket_connect_helper(). However, when the hook is invoked from the ASCONF softirq path (sctp_process_asconf), there is no file reference guaranteeing that sk->sk_socket is non-NULL. The setsockopt callers (bindx, connectx, set_primary, sendmsg connect) hold a file reference and are not affected.
Both selinux_socket_bind() and selinux_socket_connect_helper() immediately resolve sock->sk, never using the struct socket * for anything else. Refactor the inner logic into helpers that take a struct sock * directly so that selinux_sctp_bind_connect() never needs to touch sk->sk_socket at all.
---
Changes in v2:
- Refactor selinux_socket_bind() and selinux_socket_connect_helper() into sk-based inner helpers instead of adding a NULL check on sk->sk_socket (Stephen Smalley)
security/selinux/hooks.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
security/selinux/hooks.c +10 -10
@@ -4994,9 +4994,8 @@ static int selinux_socket_socketpair(struct socket *socka,Need to determine whether we should perform a name_bind permission check between the socket and the port number. */ -static int selinux_socket_bind (struct socket *sock,struct sockaddr *address,int addrlen) +static int __selinux_socket_bind (struct sock *sk,struct sockaddr *address,int addrlen) { -struct sock *sk = sock->sk;struct sk_security_struct *sksec =selinux_sock (sk);u16 family;int err;
@@ -5126,13 +5125,17 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, inreturn -EAFNOSUPPORT ; } +static int selinux_socket_bind (struct socket *sock,struct sockaddr *address,int addrlen) +{ +return __selinux_socket_bind (sock->sk, address, addrlen); +} +/* This supports connect(2) and SCTP connect services such as sctp_connectx(3) * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst */ -static int selinux_socket_connect_helper (struct socket *sock, +static int selinux_socket_connect_helper (struct sock *sk,struct sockaddr *address,int addrlen) { -struct sock *sk = sock->sk;struct sk_security_struct *sksec =selinux_sock (sk);int err;
@@ -5221,7 +5224,7 @@ static int selinux_socket_connect(struct socket *sock,int err;struct sock *sk = sock->sk; - err =selinux_socket_connect_helper (sock, address, addrlen); + err =selinux_socket_connect_helper (sk, address, addrlen);if (err)return err;
@@ -5706,13 +5709,10 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,int len, err =0 , walk_size =0 ;void *addr_buf;struct sockaddr *addr; -struct socket *sock;if (!selinux_policycap_extsockclass ())return 0 ; -/* Process one or more addresses that may be IPv4 or IPv6 */ - sock = sk->sk_socket; addr_buf = address;while (walk_size < addrlen) {
@@ -5741,14 +5741,14 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,case SCTP_PRIMARY_ADDR :case SCTP_SET_PEER_PRIMARY_ADDR :case SCTP_SOCKOPT_BINDX_ADD : - err =selinux_socket_bind (sock, addr, len); + err =__selinux_socket_bind (sk, addr, len);break ;/* Connect checks */ case SCTP_SOCKOPT_CONNECTX :case SCTP_PARAM_SET_PRIMARY :case SCTP_PARAM_ADD_IP :case SCTP_SENDMSG_CONNECT : - err =selinux_socket_connect_helper (sock, addr, len); + err =selinux_socket_connect_helper (sk, addr, len);if (err)return err;
Signature · 1 line
2.47.3
Stephen Smalley
maintainerRe: [PATCH v2] selinux: avoid sk_socket dereference in selinux_sctp_bind_connect()
Quoted 75 lines · Tristan Madani
From: Tristan Madani <[email protected]>
selinux_sctp_bind_connect()dereferencessk->sk_socketto pass a struct socket * toselinux_socket_bind()andselinux_socket_connect_helper(). However, when the hook is invoked from the ASCONF softirq path (sctp_process_asconf), there is no file reference guaranteeing thatsk->sk_socketis non-NULL. The setsockopt callers (bindx, connectx,set_primary, sendmsg connect) hold a file reference and are not affected.Both
selinux_socket_bind()andselinux_socket_connect_helper()immediately resolvesock->sk, never using the struct socket * for anything else. Refactor the inner logic into helpers that take a struct sock * directly so thatselinux_sctp_bind_connect()never needs to touchsk->sk_socketat all.Suggested-by Stephen Smalley <[email protected]>Fixes d452930fd3b9 ("selinux: Add SCTP support")Signed-off-by Tristan Madani <[email protected]>---
Changes in v2:
- Refactorselinux_socket_bind()andselinux_socket_connect_helper()into sk-based inner helpers instead of adding a NULL check onsk->sk_socket(Stephen Smalley)security/selinux/hooks.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)security/selinux/hooks.c +8
-5@@ -4994,9 +4994,8 @@ static int selinux_socket_socketpair(struct socket *socka,Need to determine whether we should perform a name_bind permission check between the socket and the port number. */ -static int selinux_socket_bind (struct socket *sock,struct sockaddr *address,int addrlen) +static int __selinux_socket_bind (struct sock *sk,struct sockaddr *address,int addrlen) { -struct sock *sk = sock->sk;struct sk_security_struct *sksec =selinux_sock (sk);u16 family;int err;@@ -5126,13 +5125,17 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, inreturn -EAFNOSUPPORT ; } +static int selinux_socket_bind (struct socket *sock,struct sockaddr *address,int addrlen) +{ +return __selinux_socket_bind (sock->sk, address, addrlen); +} +/* This supports connect(2) and SCTP connect services such as sctp_connectx(3) * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst */ -static int selinux_socket_connect_helper (struct socket *sock, +static int selinux_socket_connect_helper (struct sock *sk,struct sockaddr *address,int addrlen) { -struct sock *sk = sock->sk;struct sk_security_struct *sksec =selinux_sock (sk);int err;@@ -5221,7 +5224,7 @@ static int selinux_socket_connect(struct socket *sock,int err;struct sock *sk = sock->sk; - err =selinux_socket_connect_helper (sock, address, addrlen); + err =selinux_socket_connect_helper (sk, address, addrlen);if (err)return err;
security/selinux/hooks.c static int selinux_sctp_bind_connect(struct sock *sk, int optname, · line 5715
Quoted 10 lines
security/selinux/hooks.c +0
-3@@ -5706,13 +5709,10 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,int len, err =0 , walk_size =0 ;void *addr_buf;struct sockaddr *addr; -struct socket *sock;if (!selinux_policycap_extsockclass ())return 0 ; -/* Process one or more addresses that may be IPv4 or IPv6 */ - sock = sk->sk_socket;
The comment should remain since it wasn't specific to the line of code you are removing.
Quoted 23 lines
security/selinux/hooks.c +2
-3addr_buf = address;while (walk_size < addrlen) {@@ -5741,14 +5741,14 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,case SCTP_PRIMARY_ADDR :case SCTP_SET_PEER_PRIMARY_ADDR :case SCTP_SOCKOPT_BINDX_ADD : - err =selinux_socket_bind (sock, addr, len); + err =__selinux_socket_bind (sk, addr, len);break ;/* Connect checks */ case SCTP_SOCKOPT_CONNECTX :case SCTP_PARAM_SET_PRIMARY :case SCTP_PARAM_ADD_IP :case SCTP_SENDMSG_CONNECT : - err =selinux_socket_connect_helper (sock, addr, len); + err =selinux_socket_connect_helper (sk, addr, len);if (err)return err; --2.47.3