selinux: introduce and use lsm_ad_net_init*() helpers

Perf traces of network-related workload shows a measurable overhead
inside the network-related selinux hooks while zeroing the
lsm_network_audit struct.

In most cases we can delay the initialization of such structure to the
usage point, avoiding such overhead in a few cases.

Additionally, the audit code accesses the IP address information only
for AF_INET* families, and selinux_parse_skb() will fill-out the
relevant fields in such cases. When the family field is zeroed or the
initialization is followed by the mentioned parsing, the zeroing can be
limited to the sk, family and netif fields.

By factoring out the audit-data initialization to new helpers, this
patch removes some duplicate code and gives small but measurable
performance gain under UDP flood.

Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Paolo Abeni 2023-07-19 13:37:49 +02:00 committed by Paul Moore
parent 0fe53224bf
commit dd51fcd42f

View File

@ -224,6 +224,31 @@ static inline u32 cred_sid(const struct cred *cred)
return tsec->sid; return tsec->sid;
} }
static void __ad_net_init(struct common_audit_data *ad,
struct lsm_network_audit *net,
int ifindex, struct sock *sk, u16 family)
{
ad->type = LSM_AUDIT_DATA_NET;
ad->u.net = net;
net->netif = ifindex;
net->sk = sk;
net->family = family;
}
static void ad_net_init_from_sk(struct common_audit_data *ad,
struct lsm_network_audit *net,
struct sock *sk)
{
__ad_net_init(ad, net, 0, sk, 0);
}
static void ad_net_init_from_iif(struct common_audit_data *ad,
struct lsm_network_audit *net,
int ifindex, u16 family)
{
__ad_net_init(ad, net, ifindex, 0, family);
}
/* /*
* get the objective security ID of a task * get the objective security ID of a task
*/ */
@ -4512,7 +4537,7 @@ static int sock_has_perm(struct sock *sk, u32 perms)
{ {
struct sk_security_struct *sksec = sk->sk_security; struct sk_security_struct *sksec = sk->sk_security;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
if (sksec->sid == SECINITSID_KERNEL) if (sksec->sid == SECINITSID_KERNEL)
return 0; return 0;
@ -4532,9 +4557,7 @@ static int sock_has_perm(struct sock *sk, u32 perms)
sksec->sid == SECINITSID_INIT) sksec->sid == SECINITSID_INIT)
return 0; return 0;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_sk(&ad, &net, sk);
ad.u.net = &net;
ad.u.net->sk = sk;
return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms, return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms,
&ad); &ad);
@ -4927,12 +4950,10 @@ static int selinux_socket_unix_stream_connect(struct sock *sock,
struct sk_security_struct *sksec_other = other->sk_security; struct sk_security_struct *sksec_other = other->sk_security;
struct sk_security_struct *sksec_new = newsk->sk_security; struct sk_security_struct *sksec_new = newsk->sk_security;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
int err; int err;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_sk(&ad, &net, other);
ad.u.net = &net;
ad.u.net->sk = other;
err = avc_has_perm(sksec_sock->sid, sksec_other->sid, err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
sksec_other->sclass, sksec_other->sclass,
@ -4959,11 +4980,9 @@ static int selinux_socket_unix_may_send(struct socket *sock,
struct sk_security_struct *ssec = sock->sk->sk_security; struct sk_security_struct *ssec = sock->sk->sk_security;
struct sk_security_struct *osec = other->sk->sk_security; struct sk_security_struct *osec = other->sk->sk_security;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_sk(&ad, &net, other->sk);
ad.u.net = &net;
ad.u.net->sk = other->sk;
return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
&ad); &ad);
@ -4999,13 +5018,10 @@ static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
struct sk_security_struct *sksec = sk->sk_security; struct sk_security_struct *sksec = sk->sk_security;
u32 sk_sid = sksec->sid; u32 sk_sid = sksec->sid;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
char *addrp; char *addrp;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
ad.u.net = &net;
ad.u.net->netif = skb->skb_iif;
ad.u.net->family = family;
err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
if (err) if (err)
return err; return err;
@ -5032,7 +5048,7 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
u16 family = sk->sk_family; u16 family = sk->sk_family;
u32 sk_sid = sksec->sid; u32 sk_sid = sksec->sid;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
char *addrp; char *addrp;
if (family != PF_INET && family != PF_INET6) if (family != PF_INET && family != PF_INET6)
@ -5054,10 +5070,7 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
if (!secmark_active && !peerlbl_active) if (!secmark_active && !peerlbl_active)
return 0; return 0;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
ad.u.net = &net;
ad.u.net->netif = skb->skb_iif;
ad.u.net->family = family;
err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
if (err) if (err)
return err; return err;
@ -5227,7 +5240,7 @@ static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
u16 family = sk->sk_family; u16 family = sk->sk_family;
struct sk_security_struct *sksec = sk->sk_security; struct sk_security_struct *sksec = sk->sk_security;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
int err; int err;
/* handle mapped IPv4 packets arriving via IPv6 sockets */ /* handle mapped IPv4 packets arriving via IPv6 sockets */
@ -5263,9 +5276,7 @@ static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
/* Other association peer SIDs are checked to enforce /* Other association peer SIDs are checked to enforce
* consistency among the peer SIDs. * consistency among the peer SIDs.
*/ */
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_sk(&ad, &net, asoc->base.sk);
ad.u.net = &net;
ad.u.net->sk = asoc->base.sk;
err = avc_has_perm(sksec->peer_sid, asoc->peer_secid, err = avc_has_perm(sksec->peer_sid, asoc->peer_secid,
sksec->sclass, SCTP_SOCKET__ASSOCIATION, sksec->sclass, SCTP_SOCKET__ASSOCIATION,
&ad); &ad);
@ -5610,7 +5621,7 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
char *addrp; char *addrp;
u32 peer_sid; u32 peer_sid;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
int secmark_active, peerlbl_active; int secmark_active, peerlbl_active;
if (!selinux_policycap_netpeer()) if (!selinux_policycap_netpeer())
@ -5626,10 +5637,7 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
return NF_DROP; return NF_DROP;
ifindex = state->in->ifindex; ifindex = state->in->ifindex;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_iif(&ad, &net, ifindex, family);
ad.u.net = &net;
ad.u.net->netif = ifindex;
ad.u.net->family = family;
if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
return NF_DROP; return NF_DROP;
@ -5709,7 +5717,7 @@ static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
struct sock *sk; struct sock *sk;
struct sk_security_struct *sksec; struct sk_security_struct *sksec;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
u8 proto = 0; u8 proto = 0;
sk = skb_to_full_sk(skb); sk = skb_to_full_sk(skb);
@ -5717,10 +5725,7 @@ static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
return NF_ACCEPT; return NF_ACCEPT;
sksec = sk->sk_security; sksec = sk->sk_security;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_iif(&ad, &net, state->out->ifindex, state->pf);
ad.u.net = &net;
ad.u.net->netif = state->out->ifindex;
ad.u.net->family = state->pf;
if (selinux_parse_skb(skb, &ad, NULL, 0, &proto)) if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
return NF_DROP; return NF_DROP;
@ -5745,7 +5750,7 @@ static unsigned int selinux_ip_postroute(void *priv,
int ifindex; int ifindex;
struct sock *sk; struct sock *sk;
struct common_audit_data ad; struct common_audit_data ad;
struct lsm_network_audit net = {0,}; struct lsm_network_audit net;
char *addrp; char *addrp;
int secmark_active, peerlbl_active; int secmark_active, peerlbl_active;
@ -5842,10 +5847,7 @@ static unsigned int selinux_ip_postroute(void *priv,
} }
ifindex = state->out->ifindex; ifindex = state->out->ifindex;
ad.type = LSM_AUDIT_DATA_NET; ad_net_init_from_iif(&ad, &net, ifindex, family);
ad.u.net = &net;
ad.u.net->netif = ifindex;
ad.u.net->family = family;
if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
return NF_DROP; return NF_DROP;