Skip to content

Commit 212cd08

Browse files
edumazetdavem330
authored andcommitted
selinux: fix random read in selinux_ip_postroute_compat()
In commit e446f9d ("net: synack packets can be attached to request sockets"), I missed one remaining case of invalid skb->sk->sk_security access. Dmitry Vyukov got a KASan report pointing to it. Add selinux_skb_sk() helper that is responsible to get back to the listener if skb is attached to a request socket, instead of duplicating the logic. Fixes: ca6fb06 ("tcp: attach SYNACK messages to request sockets instead of listener") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Dmitry Vyukov <dvyukov@google.com> Cc: Paul Moore <paul@paul-moore.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 432599d commit 212cd08

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

security/selinux/hooks.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,11 +4931,23 @@ static unsigned int selinux_ipv4_output(void *priv,
49314931
return selinux_ip_output(skb, PF_INET);
49324932
}
49334933

4934+
/* SYNACK messages might be attached to request sockets.
4935+
* To get back to sk_security, we need to look at the listener.
4936+
*/
4937+
static struct sock *selinux_skb_sk(const struct sk_buff *skb)
4938+
{
4939+
struct sock *sk = skb->sk;
4940+
4941+
if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
4942+
sk = inet_reqsk(sk)->rsk_listener;
4943+
return sk;
4944+
}
4945+
49344946
static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
49354947
int ifindex,
49364948
u16 family)
49374949
{
4938-
struct sock *sk = skb->sk;
4950+
struct sock *sk = selinux_skb_sk(skb);
49394951
struct sk_security_struct *sksec;
49404952
struct common_audit_data ad;
49414953
struct lsm_network_audit net = {0,};
@@ -4990,7 +5002,7 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb,
49905002
if (!secmark_active && !peerlbl_active)
49915003
return NF_ACCEPT;
49925004

4993-
sk = skb->sk;
5005+
sk = selinux_skb_sk(skb);
49945006

49955007
#ifdef CONFIG_XFRM
49965008
/* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
@@ -5035,8 +5047,6 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb,
50355047
u32 skb_sid;
50365048
struct sk_security_struct *sksec;
50375049

5038-
if (sk->sk_state == TCP_NEW_SYN_RECV)
5039-
sk = inet_reqsk(sk)->rsk_listener;
50405050
sksec = sk->sk_security;
50415051
if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
50425052
return NF_DROP;

0 commit comments

Comments
 (0)