Skip to content

Commit 446b802

Browse files
committed
selinux: handle TCP SYN-ACK packets correctly in selinux_ip_postroute()
In selinux_ip_postroute() we perform access checks based on the packet's security label. For locally generated traffic we get the packet's security label from the associated socket; this works in all cases except for TCP SYN-ACK packets. In the case of SYN-ACK packet's the correct security label is stored in the connection's request_sock, not the server's socket. Unfortunately, at the point in time when selinux_ip_postroute() is called we can't query the request_sock directly, we need to recreate the label using the same logic that originally labeled the associated request_sock. See the inline comments for more explanation. Reported-by: Janak Desai <Janak.Desai@gtri.gatech.edu> Tested-by: Janak Desai <Janak.Desai@gtri.gatech.edu> Cc: stable@vger.kernel.org Signed-off-by: Paul Moore <pmoore@redhat.com>
1 parent 4718006 commit 446b802

1 file changed

Lines changed: 53 additions & 15 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,30 @@ static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
38473847
return 0;
38483848
}
38493849

3850+
/**
3851+
* selinux_conn_sid - Determine the child socket label for a connection
3852+
* @sk_sid: the parent socket's SID
3853+
* @skb_sid: the packet's SID
3854+
* @conn_sid: the resulting connection SID
3855+
*
3856+
* If @skb_sid is valid then the user:role:type information from @sk_sid is
3857+
* combined with the MLS information from @skb_sid in order to create
3858+
* @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3859+
* of @sk_sid. Returns zero on success, negative values on failure.
3860+
*
3861+
*/
3862+
static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3863+
{
3864+
int err = 0;
3865+
3866+
if (skb_sid != SECSID_NULL)
3867+
err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3868+
else
3869+
*conn_sid = sk_sid;
3870+
3871+
return err;
3872+
}
3873+
38503874
/* socket security operations */
38513875

38523876
static int socket_sockcreate_sid(const struct task_security_struct *tsec,
@@ -4453,7 +4477,7 @@ static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
44534477
struct sk_security_struct *sksec = sk->sk_security;
44544478
int err;
44554479
u16 family = sk->sk_family;
4456-
u32 newsid;
4480+
u32 connsid;
44574481
u32 peersid;
44584482

44594483
/* handle mapped IPv4 packets arriving via IPv6 sockets */
@@ -4463,16 +4487,11 @@ static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
44634487
err = selinux_skb_peerlbl_sid(skb, family, &peersid);
44644488
if (err)
44654489
return err;
4466-
if (peersid == SECSID_NULL) {
4467-
req->secid = sksec->sid;
4468-
req->peer_secid = SECSID_NULL;
4469-
} else {
4470-
err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4471-
if (err)
4472-
return err;
4473-
req->secid = newsid;
4474-
req->peer_secid = peersid;
4475-
}
4490+
err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4491+
if (err)
4492+
return err;
4493+
req->secid = connsid;
4494+
req->peer_secid = peersid;
44764495

44774496
return selinux_netlbl_inet_conn_request(req, family);
44784497
}
@@ -4846,12 +4865,12 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
48464865
if (!secmark_active && !peerlbl_active)
48474866
return NF_ACCEPT;
48484867

4849-
/* if the packet is being forwarded then get the peer label from the
4850-
* packet itself; otherwise check to see if it is from a local
4851-
* application or the kernel, if from an application get the peer label
4852-
* from the sending socket, otherwise use the kernel's sid */
48534868
sk = skb->sk;
48544869
if (sk == NULL) {
4870+
/* Without an associated socket the packet is either coming
4871+
* from the kernel or it is being forwarded; check the packet
4872+
* to determine which and if the packet is being forwarded
4873+
* query the packet directly to determine the security label. */
48554874
if (skb->skb_iif) {
48564875
secmark_perm = PACKET__FORWARD_OUT;
48574876
if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
@@ -4860,7 +4879,26 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
48604879
secmark_perm = PACKET__SEND;
48614880
peer_sid = SECINITSID_KERNEL;
48624881
}
4882+
} else if (sk->sk_state == TCP_LISTEN) {
4883+
/* Locally generated packet but the associated socket is in the
4884+
* listening state which means this is a SYN-ACK packet. In
4885+
* this particular case the correct security label is assigned
4886+
* to the connection/request_sock but unfortunately we can't
4887+
* query the request_sock as it isn't queued on the parent
4888+
* socket until after the SYN-ACK packet is sent; the only
4889+
* viable choice is to regenerate the label like we do in
4890+
* selinux_inet_conn_request(). See also selinux_ip_output()
4891+
* for similar problems. */
4892+
u32 skb_sid;
4893+
struct sk_security_struct *sksec = sk->sk_security;
4894+
if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4895+
return NF_DROP;
4896+
if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4897+
return NF_DROP;
4898+
secmark_perm = PACKET__SEND;
48634899
} else {
4900+
/* Locally generated packet, fetch the security label from the
4901+
* associated socket. */
48644902
struct sk_security_struct *sksec = sk->sk_security;
48654903
peer_sid = sksec->sid;
48664904
secmark_perm = PACKET__SEND;

0 commit comments

Comments
 (0)