Skip to content

Commit 3610cda

Browse files
committed
af_unix: Avoid socket->sk NULL OOPS in stream connect security hooks.
unix_release() can asynchornously set socket->sk to NULL, and it does so without holding the unix_state_lock() on "other" during stream connects. However, the reverse mapping, sk->sk_socket, is only transitioned to NULL under the unix_state_lock(). Therefore make the security hooks follow the reverse mapping instead of the forward mapping. Reported-by: Jeremy Fitzhardinge <jeremy@goop.org> Reported-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 44b8288 commit 3610cda

6 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎include/linux/security.h‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,9 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
796796
* @unix_stream_connect:
797797
* Check permissions before establishing a Unix domain stream connection
798798
* between @sock and @other.
799-
* @sock contains the socket structure.
800-
* @other contains the peer socket structure.
799+
* @sock contains the sock structure.
800+
* @other contains the peer sock structure.
801+
* @newsk contains the new sock structure.
801802
* Return 0 if permission is granted.
802803
* @unix_may_send:
803804
* Check permissions before connecting or sending datagrams from @sock to
@@ -1568,8 +1569,7 @@ struct security_operations {
15681569
int (*inode_getsecctx)(struct inode *inode, void **ctx, u32 *ctxlen);
15691570

15701571
#ifdef CONFIG_SECURITY_NETWORK
1571-
int (*unix_stream_connect) (struct socket *sock,
1572-
struct socket *other, struct sock *newsk);
1572+
int (*unix_stream_connect) (struct sock *sock, struct sock *other, struct sock *newsk);
15731573
int (*unix_may_send) (struct socket *sock, struct socket *other);
15741574

15751575
int (*socket_create) (int family, int type, int protocol, int kern);
@@ -2525,8 +2525,7 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32
25252525

25262526
#ifdef CONFIG_SECURITY_NETWORK
25272527

2528-
int security_unix_stream_connect(struct socket *sock, struct socket *other,
2529-
struct sock *newsk);
2528+
int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk);
25302529
int security_unix_may_send(struct socket *sock, struct socket *other);
25312530
int security_socket_create(int family, int type, int protocol, int kern);
25322531
int security_socket_post_create(struct socket *sock, int family,
@@ -2567,8 +2566,8 @@ void security_tun_dev_post_create(struct sock *sk);
25672566
int security_tun_dev_attach(struct sock *sk);
25682567

25692568
#else /* CONFIG_SECURITY_NETWORK */
2570-
static inline int security_unix_stream_connect(struct socket *sock,
2571-
struct socket *other,
2569+
static inline int security_unix_stream_connect(struct sock *sock,
2570+
struct sock *other,
25722571
struct sock *newsk)
25732572
{
25742573
return 0;

‎net/unix/af_unix.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
11571157
goto restart;
11581158
}
11591159

1160-
err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1160+
err = security_unix_stream_connect(sk, other, newsk);
11611161
if (err) {
11621162
unix_state_unlock(sk);
11631163
goto out_unlock;

‎security/capability.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ static int cap_sem_semop(struct sem_array *sma, struct sembuf *sops,
548548
}
549549

550550
#ifdef CONFIG_SECURITY_NETWORK
551-
static int cap_unix_stream_connect(struct socket *sock, struct socket *other,
551+
static int cap_unix_stream_connect(struct sock *sock, struct sock *other,
552552
struct sock *newsk)
553553
{
554554
return 0;

‎security/security.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,7 @@ EXPORT_SYMBOL(security_inode_getsecctx);
977977

978978
#ifdef CONFIG_SECURITY_NETWORK
979979

980-
int security_unix_stream_connect(struct socket *sock, struct socket *other,
981-
struct sock *newsk)
980+
int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
982981
{
983982
return security_ops->unix_stream_connect(sock, other, newsk);
984983
}

‎security/selinux/hooks.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,18 +3921,18 @@ static int selinux_socket_shutdown(struct socket *sock, int how)
39213921
return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
39223922
}
39233923

3924-
static int selinux_socket_unix_stream_connect(struct socket *sock,
3925-
struct socket *other,
3924+
static int selinux_socket_unix_stream_connect(struct sock *sock,
3925+
struct sock *other,
39263926
struct sock *newsk)
39273927
{
3928-
struct sk_security_struct *sksec_sock = sock->sk->sk_security;
3929-
struct sk_security_struct *sksec_other = other->sk->sk_security;
3928+
struct sk_security_struct *sksec_sock = sock->sk_security;
3929+
struct sk_security_struct *sksec_other = other->sk_security;
39303930
struct sk_security_struct *sksec_new = newsk->sk_security;
39313931
struct common_audit_data ad;
39323932
int err;
39333933

39343934
COMMON_AUDIT_DATA_INIT(&ad, NET);
3935-
ad.u.net.sk = other->sk;
3935+
ad.u.net.sk = other;
39363936

39373937
err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
39383938
sksec_other->sclass,

‎security/smack/smack_lsm.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,22 +2408,22 @@ static int smack_setprocattr(struct task_struct *p, char *name,
24082408

24092409
/**
24102410
* smack_unix_stream_connect - Smack access on UDS
2411-
* @sock: one socket
2412-
* @other: the other socket
2411+
* @sock: one sock
2412+
* @other: the other sock
24132413
* @newsk: unused
24142414
*
24152415
* Return 0 if a subject with the smack of sock could access
24162416
* an object with the smack of other, otherwise an error code
24172417
*/
2418-
static int smack_unix_stream_connect(struct socket *sock,
2419-
struct socket *other, struct sock *newsk)
2418+
static int smack_unix_stream_connect(struct sock *sock,
2419+
struct sock *other, struct sock *newsk)
24202420
{
2421-
struct inode *sp = SOCK_INODE(sock);
2422-
struct inode *op = SOCK_INODE(other);
2421+
struct inode *sp = SOCK_INODE(sock->sk_socket);
2422+
struct inode *op = SOCK_INODE(other->sk_socket);
24232423
struct smk_audit_info ad;
24242424

24252425
smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2426-
smk_ad_setfield_u_net_sk(&ad, other->sk);
2426+
smk_ad_setfield_u_net_sk(&ad, other);
24272427
return smk_access(smk_of_inode(sp), smk_of_inode(op),
24282428
MAY_READWRITE, &ad);
24292429
}

0 commit comments

Comments
 (0)