Skip to content

Commit 1d1e1de

Browse files
committed
selinux: make better use of the nf_hook_state passed to the NF hooks
This patch builds on a previous SELinux/netfilter patch by Florian Westphal and makes better use of the nf_hook_state variable passed into the SELinux/netfilter hooks as well as a number of other small cleanups in the related code. Signed-off-by: Paul Moore <[email protected]>
1 parent cbfcd13 commit 1d1e1de

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

security/selinux/hooks.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,38 +5692,38 @@ static int selinux_tun_dev_open(void *security)
56925692
static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
56935693
const struct nf_hook_state *state)
56945694
{
5695-
const struct net_device *indev = state->in;
5696-
u16 family = state->pf;
5697-
int err;
5695+
int ifindex;
5696+
u16 family;
56985697
char *addrp;
56995698
u32 peer_sid;
57005699
struct common_audit_data ad;
57015700
struct lsm_network_audit net = {0,};
5702-
u8 secmark_active;
5703-
u8 netlbl_active;
5704-
u8 peerlbl_active;
5701+
int secmark_active, peerlbl_active;
57055702

57065703
if (!selinux_policycap_netpeer())
57075704
return NF_ACCEPT;
57085705

57095706
secmark_active = selinux_secmark_enabled();
5710-
netlbl_active = netlbl_enabled();
57115707
peerlbl_active = selinux_peerlbl_enabled();
57125708
if (!secmark_active && !peerlbl_active)
57135709
return NF_ACCEPT;
57145710

5711+
family = state->pf;
57155712
if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
57165713
return NF_DROP;
57175714

5715+
ifindex = state->in->ifindex;
57185716
ad.type = LSM_AUDIT_DATA_NET;
57195717
ad.u.net = &net;
5720-
ad.u.net->netif = indev->ifindex;
5718+
ad.u.net->netif = ifindex;
57215719
ad.u.net->family = family;
57225720
if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
57235721
return NF_DROP;
57245722

57255723
if (peerlbl_active) {
5726-
err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5724+
int err;
5725+
5726+
err = selinux_inet_sys_rcv_skb(state->net, ifindex,
57275727
addrp, family, peer_sid, &ad);
57285728
if (err) {
57295729
selinux_netlbl_err(skb, family, err, 1);
@@ -5737,7 +5737,7 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
57375737
SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
57385738
return NF_DROP;
57395739

5740-
if (netlbl_active)
5740+
if (netlbl_enabled())
57415741
/* we do this in the FORWARD path and not the POST_ROUTING
57425742
* path because we want to make sure we apply the necessary
57435743
* labeling before IPsec is applied so we can leverage AH
@@ -5751,7 +5751,6 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
57515751
static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
57525752
const struct nf_hook_state *state)
57535753
{
5754-
u16 family = state->pf;
57555754
struct sock *sk;
57565755
u32 sid;
57575756

@@ -5785,33 +5784,32 @@ static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
57855784
sid = sksec->sid;
57865785
} else
57875786
sid = SECINITSID_KERNEL;
5788-
if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5787+
if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
57895788
return NF_DROP;
57905789

57915790
return NF_ACCEPT;
57925791
}
57935792

57945793

57955794
static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5796-
int ifindex,
5797-
u16 family)
5795+
const struct nf_hook_state *state)
57985796
{
5799-
struct sock *sk = skb_to_full_sk(skb);
5797+
struct sock *sk;
58005798
struct sk_security_struct *sksec;
58015799
struct common_audit_data ad;
58025800
struct lsm_network_audit net = {0,};
5803-
char *addrp;
58045801
u8 proto;
58055802

5806-
if (sk == NULL)
5803+
if (state->sk == NULL)
58075804
return NF_ACCEPT;
5805+
sk = skb_to_full_sk(skb);
58085806
sksec = sk->sk_security;
58095807

58105808
ad.type = LSM_AUDIT_DATA_NET;
58115809
ad.u.net = &net;
5812-
ad.u.net->netif = ifindex;
5813-
ad.u.net->family = family;
5814-
if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5810+
ad.u.net->netif = state->out->ifindex;
5811+
ad.u.net->family = state->pf;
5812+
if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
58155813
return NF_DROP;
58165814

58175815
if (selinux_secmark_enabled())
@@ -5830,24 +5828,22 @@ static unsigned int selinux_ip_postroute(void *priv,
58305828
struct sk_buff *skb,
58315829
const struct nf_hook_state *state)
58325830
{
5833-
const struct net_device *outdev = state->out;
5834-
u16 family = state->pf;
5831+
u16 family;
58355832
u32 secmark_perm;
58365833
u32 peer_sid;
5837-
int ifindex = outdev->ifindex;
5834+
int ifindex;
58385835
struct sock *sk;
58395836
struct common_audit_data ad;
58405837
struct lsm_network_audit net = {0,};
58415838
char *addrp;
5842-
u8 secmark_active;
5843-
u8 peerlbl_active;
5839+
int secmark_active, peerlbl_active;
58445840

58455841
/* If any sort of compatibility mode is enabled then handoff processing
58465842
* to the selinux_ip_postroute_compat() function to deal with the
58475843
* special handling. We do this in an attempt to keep this function
58485844
* as fast and as clean as possible. */
58495845
if (!selinux_policycap_netpeer())
5850-
return selinux_ip_postroute_compat(skb, ifindex, family);
5846+
return selinux_ip_postroute_compat(skb, state);
58515847

58525848
secmark_active = selinux_secmark_enabled();
58535849
peerlbl_active = selinux_peerlbl_enabled();
@@ -5873,6 +5869,7 @@ static unsigned int selinux_ip_postroute(void *priv,
58735869
return NF_ACCEPT;
58745870
#endif
58755871

5872+
family = state->pf;
58765873
if (sk == NULL) {
58775874
/* Without an associated socket the packet is either coming
58785875
* from the kernel or it is being forwarded; check the packet
@@ -5933,6 +5930,7 @@ static unsigned int selinux_ip_postroute(void *priv,
59335930
secmark_perm = PACKET__SEND;
59345931
}
59355932

5933+
ifindex = state->out->ifindex;
59365934
ad.type = LSM_AUDIT_DATA_NET;
59375935
ad.u.net = &net;
59385936
ad.u.net->netif = ifindex;
@@ -5950,7 +5948,7 @@ static unsigned int selinux_ip_postroute(void *priv,
59505948
u32 if_sid;
59515949
u32 node_sid;
59525950

5953-
if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
5951+
if (sel_netif_sid(state->net, ifindex, &if_sid))
59545952
return NF_DROP;
59555953
if (avc_has_perm(&selinux_state,
59565954
peer_sid, if_sid,

0 commit comments

Comments
 (0)