Skip to content

Commit 220deb9

Browse files
pcmoorexjamesmorris
authored andcommitted
SELinux: Better integration between peer labeling subsystems
Rework the handling of network peer labels so that the different peer labeling subsystems work better together. This includes moving both subsystems to a single "peer" object class which involves not only changes to the permission checks but an improved method of consolidating multiple packet peer labels. As part of this work the inbound packet permission check code has been heavily modified to handle both the old and new behavior in as sane a fashion as possible. Signed-off-by: Paul Moore <paul.moore@hp.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent f67f4f3 commit 220deb9

6 files changed

Lines changed: 208 additions & 100 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 112 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <net/icmp.h>
5151
#include <net/ip.h> /* for local_port_range[] */
5252
#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
53+
#include <net/net_namespace.h>
5354
#include <asm/uaccess.h>
5455
#include <asm/ioctls.h>
5556
#include <linux/bitops.h>
@@ -3426,36 +3427,39 @@ static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
34263427
}
34273428

34283429
/**
3429-
* selinux_skb_extlbl_sid - Determine the external label of a packet
3430+
* selinux_skb_peerlbl_sid - Determine the peer label of a packet
34303431
* @skb: the packet
34313432
* @family: protocol family
3432-
* @sid: the packet's SID
3433+
* @sid: the packet's peer label SID
34333434
*
34343435
* Description:
3435-
* Check the various different forms of external packet labeling and determine
3436-
* the external SID for the packet. If only one form of external labeling is
3437-
* present then it is used, if both labeled IPsec and NetLabel labels are
3438-
* present then the SELinux type information is taken from the labeled IPsec
3439-
* SA and the MLS sensitivity label information is taken from the NetLabel
3440-
* security attributes. This bit of "magic" is done in the call to
3441-
* selinux_netlbl_skbuff_getsid().
3436+
* Check the various different forms of network peer labeling and determine
3437+
* the peer label/SID for the packet; most of the magic actually occurs in
3438+
* the security server function security_net_peersid_cmp(). The function
3439+
* returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3440+
* or -EACCES if @sid is invalid due to inconsistencies with the different
3441+
* peer labels.
34423442
*
34433443
*/
3444-
static void selinux_skb_extlbl_sid(struct sk_buff *skb,
3445-
u16 family,
3446-
u32 *sid)
3444+
static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
34473445
{
34483446
u32 xfrm_sid;
34493447
u32 nlbl_sid;
3448+
u32 nlbl_type;
34503449

34513450
selinux_skb_xfrm_sid(skb, &xfrm_sid);
3452-
if (selinux_netlbl_skbuff_getsid(skb,
3453-
family,
3454-
(xfrm_sid == SECSID_NULL ?
3455-
SECINITSID_NETMSG : xfrm_sid),
3456-
&nlbl_sid) != 0)
3457-
nlbl_sid = SECSID_NULL;
3458-
*sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
3451+
selinux_netlbl_skbuff_getsid(skb,
3452+
family,
3453+
SECINITSID_NETMSG,
3454+
&nlbl_type,
3455+
&nlbl_sid);
3456+
3457+
if (security_net_peersid_resolve(nlbl_sid, nlbl_type,
3458+
xfrm_sid,
3459+
sid) != 0)
3460+
return -EACCES;
3461+
3462+
return 0;
34593463
}
34603464

34613465
/* socket security operations */
@@ -3521,6 +3525,7 @@ static int selinux_socket_post_create(struct socket *sock, int family,
35213525
if (sock->sk) {
35223526
sksec = sock->sk->sk_security;
35233527
sksec->sid = isec->sid;
3528+
sksec->sclass = isec->sclass;
35243529
err = selinux_netlbl_socket_post_create(sock);
35253530
}
35263531

@@ -3824,104 +3829,114 @@ static int selinux_socket_unix_may_send(struct socket *sock,
38243829
return 0;
38253830
}
38263831

3827-
static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
3828-
struct avc_audit_data *ad,
3829-
u16 family, char *addrp)
3832+
static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
3833+
struct sk_buff *skb,
3834+
struct avc_audit_data *ad,
3835+
u16 family,
3836+
char *addrp)
38303837
{
3831-
int err = 0;
3832-
u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0;
3833-
struct socket *sock;
3834-
u16 sock_class = 0;
3835-
u32 sock_sid = 0;
3836-
3837-
read_lock_bh(&sk->sk_callback_lock);
3838-
sock = sk->sk_socket;
3839-
if (sock) {
3840-
struct inode *inode;
3841-
inode = SOCK_INODE(sock);
3842-
if (inode) {
3843-
struct inode_security_struct *isec;
3844-
isec = inode->i_security;
3845-
sock_sid = isec->sid;
3846-
sock_class = isec->sclass;
3847-
}
3848-
}
3849-
read_unlock_bh(&sk->sk_callback_lock);
3850-
if (!sock_sid)
3851-
goto out;
3852-
3853-
if (!skb->dev)
3854-
goto out;
3838+
int err;
3839+
struct sk_security_struct *sksec = sk->sk_security;
3840+
u16 sk_class;
3841+
u32 netif_perm, node_perm, recv_perm;
3842+
u32 port_sid, node_sid, if_sid, sk_sid;
38553843

3856-
err = sel_netif_sid(skb->iif, &if_sid);
3857-
if (err)
3858-
goto out;
3844+
sk_sid = sksec->sid;
3845+
sk_class = sksec->sclass;
38593846

3860-
switch (sock_class) {
3847+
switch (sk_class) {
38613848
case SECCLASS_UDP_SOCKET:
38623849
netif_perm = NETIF__UDP_RECV;
38633850
node_perm = NODE__UDP_RECV;
38643851
recv_perm = UDP_SOCKET__RECV_MSG;
38653852
break;
3866-
38673853
case SECCLASS_TCP_SOCKET:
38683854
netif_perm = NETIF__TCP_RECV;
38693855
node_perm = NODE__TCP_RECV;
38703856
recv_perm = TCP_SOCKET__RECV_MSG;
38713857
break;
3872-
38733858
case SECCLASS_DCCP_SOCKET:
38743859
netif_perm = NETIF__DCCP_RECV;
38753860
node_perm = NODE__DCCP_RECV;
38763861
recv_perm = DCCP_SOCKET__RECV_MSG;
38773862
break;
3878-
38793863
default:
38803864
netif_perm = NETIF__RAWIP_RECV;
38813865
node_perm = NODE__RAWIP_RECV;
3866+
recv_perm = 0;
38823867
break;
38833868
}
38843869

3885-
err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
3870+
err = sel_netif_sid(skb->iif, &if_sid);
38863871
if (err)
3887-
goto out;
3872+
return err;
3873+
err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
3874+
if (err)
3875+
return err;
38883876

38893877
err = sel_netnode_sid(addrp, family, &node_sid);
38903878
if (err)
3891-
goto out;
3892-
3893-
err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, ad);
3879+
return err;
3880+
err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
38943881
if (err)
3895-
goto out;
3882+
return err;
38963883

3897-
if (recv_perm) {
3898-
u32 port_sid;
3884+
if (!recv_perm)
3885+
return 0;
3886+
err = security_port_sid(sk->sk_family, sk->sk_type,
3887+
sk->sk_protocol, ntohs(ad->u.net.sport),
3888+
&port_sid);
3889+
if (err)
3890+
return err;
3891+
return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad);
3892+
}
38993893

3900-
err = security_port_sid(sk->sk_family, sk->sk_type,
3901-
sk->sk_protocol, ntohs(ad->u.net.sport),
3902-
&port_sid);
3903-
if (err)
3904-
goto out;
3894+
static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
3895+
struct avc_audit_data *ad,
3896+
u16 family, char *addrp)
3897+
{
3898+
int err;
3899+
struct sk_security_struct *sksec = sk->sk_security;
3900+
u32 peer_sid;
3901+
u32 sk_sid = sksec->sid;
3902+
3903+
if (selinux_compat_net)
3904+
err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad,
3905+
family, addrp);
3906+
else
3907+
err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
3908+
PACKET__RECV, ad);
3909+
if (err)
3910+
return err;
39053911

3906-
err = avc_has_perm(sock_sid, port_sid,
3907-
sock_class, recv_perm, ad);
3912+
if (selinux_policycap_netpeer) {
3913+
err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
3914+
if (err)
3915+
return err;
3916+
err = avc_has_perm(sk_sid, peer_sid,
3917+
SECCLASS_PEER, PEER__RECV, ad);
3918+
} else {
3919+
err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad);
3920+
if (err)
3921+
return err;
3922+
err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad);
39083923
}
39093924

3910-
out:
39113925
return err;
39123926
}
39133927

39143928
static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
39153929
{
3916-
u16 family;
3917-
char *addrp;
3918-
int err = 0;
3919-
struct avc_audit_data ad;
3930+
int err;
39203931
struct sk_security_struct *sksec = sk->sk_security;
3932+
u16 family = sk->sk_family;
3933+
u32 sk_sid = sksec->sid;
3934+
u32 peer_sid;
3935+
struct avc_audit_data ad;
3936+
char *addrp;
39213937

3922-
family = sk->sk_family;
39233938
if (family != PF_INET && family != PF_INET6)
3924-
goto out;
3939+
return 0;
39253940

39263941
/* Handle mapped IPv4 packets arriving via IPv6 sockets */
39273942
if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
@@ -3930,26 +3945,27 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
39303945
AVC_AUDIT_DATA_INIT(&ad, NET);
39313946
ad.u.net.netif = skb->iif;
39323947
ad.u.net.family = family;
3933-
39343948
err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
39353949
if (err)
3936-
goto out;
3950+
return err;
39373951

3938-
if (selinux_compat_net)
3939-
err = selinux_sock_rcv_skb_compat(sk, skb, &ad, family, addrp);
3940-
else
3941-
err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET,
3942-
PACKET__RECV, &ad);
3943-
if (err)
3944-
goto out;
3952+
/* If any sort of compatibility mode is enabled then handoff processing
3953+
* to the selinux_sock_rcv_skb_compat() function to deal with the
3954+
* special handling. We do this in an attempt to keep this function
3955+
* as fast and as clean as possible. */
3956+
if (selinux_compat_net || !selinux_policycap_netpeer)
3957+
return selinux_sock_rcv_skb_compat(sk, skb, &ad,
3958+
family, addrp);
39453959

3946-
err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
3960+
err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
3961+
PACKET__RECV, &ad);
39473962
if (err)
3948-
goto out;
3963+
return err;
39493964

3950-
err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
3951-
out:
3952-
return err;
3965+
err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
3966+
if (err)
3967+
return err;
3968+
return avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, PEER__RECV, &ad);
39533969
}
39543970

39553971
static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
@@ -4011,7 +4027,7 @@ static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *
40114027
if (sock && family == PF_UNIX)
40124028
selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid);
40134029
else if (skb)
4014-
selinux_skb_extlbl_sid(skb, family, &peer_secid);
4030+
selinux_skb_peerlbl_sid(skb, family, &peer_secid);
40154031

40164032
out:
40174033
*secid = peer_secid;
@@ -4037,6 +4053,7 @@ static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
40374053

40384054
newssec->sid = ssec->sid;
40394055
newssec->peer_sid = ssec->peer_sid;
4056+
newssec->sclass = ssec->sclass;
40404057

40414058
selinux_netlbl_sk_security_clone(ssec, newssec);
40424059
}
@@ -4060,6 +4077,7 @@ static void selinux_sock_graft(struct sock* sk, struct socket *parent)
40604077
if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
40614078
sk->sk_family == PF_UNIX)
40624079
isec->sid = sksec->sid;
4080+
sksec->sclass = isec->sclass;
40634081

40644082
selinux_netlbl_sock_graft(sk, parent);
40654083
}
@@ -4072,7 +4090,9 @@ static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
40724090
u32 newsid;
40734091
u32 peersid;
40744092

4075-
selinux_skb_extlbl_sid(skb, sk->sk_family, &peersid);
4093+
err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid);
4094+
if (err)
4095+
return err;
40764096
if (peersid == SECSID_NULL) {
40774097
req->secid = sksec->sid;
40784098
req->peer_secid = SECSID_NULL;
@@ -4110,7 +4130,7 @@ static void selinux_inet_conn_established(struct sock *sk,
41104130
{
41114131
struct sk_security_struct *sksec = sk->sk_security;
41124132

4113-
selinux_skb_extlbl_sid(skb, sk->sk_family, &sksec->peer_sid);
4133+
selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid);
41144134
}
41154135

41164136
static void selinux_req_classify_flow(const struct request_sock *req,

‎security/selinux/include/netlabel.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
4949
int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
5050
u16 family,
5151
u32 base_sid,
52+
u32 *type,
5253
u32 *sid);
5354

5455
void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock);
@@ -89,8 +90,10 @@ static inline void selinux_netlbl_sk_security_clone(
8990
static inline int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
9091
u16 family,
9192
u32 base_sid,
93+
u32 *type,
9294
u32 *sid)
9395
{
96+
*type = NETLBL_NLTYPE_NONE;
9497
*sid = SECSID_NULL;
9598
return 0;
9699
}

‎security/selinux/include/objsec.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ struct sk_security_struct {
113113
struct sock *sk; /* back pointer to sk object */
114114
u32 sid; /* SID of this object */
115115
u32 peer_sid; /* SID of peer */
116-
#ifdef CONFIG_NETLABEL
117116
u16 sclass; /* sock security class */
117+
#ifdef CONFIG_NETLABEL
118118
enum { /* NetLabel state */
119119
NLBL_UNSET = 0,
120120
NLBL_REQUIRE,

‎security/selinux/include/security.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
9999

100100
int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid);
101101

102+
int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
103+
u32 xfrm_sid,
104+
u32 *peer_sid);
105+
102106
int security_get_classes(char ***classes, int *nclasses);
103107
int security_get_permissions(char *class, char ***perms, int *nperms);
104108
int security_get_reject_unknown(void);

0 commit comments

Comments
 (0)