Skip to content

Commit 3e11217

Browse files
pcmoorexjamesmorris
authored andcommitted
SELinux: Add network port SID cache
Much like we added a network node cache, this patch adds a network port cache. The design is taken almost completely from the network node cache which in turn was taken from the network interface cache. The basic idea is to cache entries in a hash table based on protocol/port information. The hash function only takes the port number into account since the number of different protocols in use at any one time is expected to be relatively small. Signed-off-by: Paul Moore <paul.moore@hp.com> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 832cbd9 commit 3e11217

5 files changed

Lines changed: 17 additions & 21 deletions

File tree

‎security/selinux/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ selinux-y := avc.o \
1111
nlmsgtab.o \
1212
netif.o \
1313
netnode.o \
14+
netport.o \
1415
exports.o
1516

1617
selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o

‎security/selinux/hooks.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "objsec.h"
8181
#include "netif.h"
8282
#include "netnode.h"
83+
#include "netport.h"
8384
#include "xfrm.h"
8485
#include "netlabel.h"
8586

@@ -3670,10 +3671,8 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in
36703671
inet_get_local_port_range(&low, &high);
36713672

36723673
if (snum < max(PROT_SOCK, low) || snum > high) {
3673-
err = security_port_sid(sk->sk_family,
3674-
sk->sk_type,
3675-
sk->sk_protocol, snum,
3676-
&sid);
3674+
err = sel_netport_sid(sk->sk_protocol,
3675+
snum, &sid);
36773676
if (err)
36783677
goto out;
36793678
AVC_AUDIT_DATA_INIT(&ad,NET);
@@ -3761,8 +3760,7 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
37613760
snum = ntohs(addr6->sin6_port);
37623761
}
37633762

3764-
err = security_port_sid(sk->sk_family, sk->sk_type,
3765-
sk->sk_protocol, snum, &sid);
3763+
err = sel_netport_sid(sk->sk_protocol, snum, &sid);
37663764
if (err)
37673765
goto out;
37683766

@@ -3993,9 +3991,8 @@ static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
39933991

39943992
if (!recv_perm)
39953993
return 0;
3996-
err = security_port_sid(sk->sk_family, sk->sk_type,
3997-
sk->sk_protocol, ntohs(ad->u.net.sport),
3998-
&port_sid);
3994+
err = sel_netport_sid(sk->sk_protocol,
3995+
ntohs(ad->u.net.sport), &port_sid);
39993996
if (unlikely(err)) {
40003997
printk(KERN_WARNING
40013998
"SELinux: failure in"
@@ -4416,9 +4413,8 @@ static int selinux_ip_postroute_iptables_compat(struct sock *sk,
44164413
if (send_perm != 0)
44174414
return 0;
44184415

4419-
err = security_port_sid(sk->sk_family, sk->sk_type,
4420-
sk->sk_protocol, ntohs(ad->u.net.dport),
4421-
&port_sid);
4416+
err = sel_netport_sid(sk->sk_protocol,
4417+
ntohs(ad->u.net.dport), &port_sid);
44224418
if (unlikely(err)) {
44234419
printk(KERN_WARNING
44244420
"SELinux: failure in"

‎security/selinux/include/objsec.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ struct netnode_security_struct {
103103
u16 family; /* address family */
104104
};
105105

106+
struct netport_security_struct {
107+
u32 sid; /* SID for this node */
108+
u16 port; /* port number */
109+
u8 protocol; /* transport protocol */
110+
};
111+
106112
struct sk_security_struct {
107113
u32 sid; /* SID of this object */
108114
u32 peer_sid; /* SID of peer */

‎security/selinux/include/security.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ int security_context_to_sid_default(char *scontext, u32 scontext_len,
102102
int security_get_user_sids(u32 callsid, char *username,
103103
u32 **sids, u32 *nel);
104104

105-
int security_port_sid(u16 domain, u16 type, u8 protocol, u16 port,
106-
u32 *out_sid);
105+
int security_port_sid(u8 protocol, u16 port, u32 *out_sid);
107106

108107
int security_netif_sid(char *name, u32 *if_sid);
109108

‎security/selinux/ss/services.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,17 +1472,11 @@ int security_load_policy(void *data, size_t len)
14721472

14731473
/**
14741474
* security_port_sid - Obtain the SID for a port.
1475-
* @domain: communication domain aka address family
1476-
* @type: socket type
14771475
* @protocol: protocol number
14781476
* @port: port number
14791477
* @out_sid: security identifier
14801478
*/
1481-
int security_port_sid(u16 domain,
1482-
u16 type,
1483-
u8 protocol,
1484-
u16 port,
1485-
u32 *out_sid)
1479+
int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
14861480
{
14871481
struct ocontext *c;
14881482
int rc = 0;

0 commit comments

Comments
 (0)