xref: /OK3568_Linux_fs/kernel/security/selinux/netport.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Network port table
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SELinux must keep a mapping of network ports to labels/SIDs.  This
6*4882a593Smuzhiyun  * mapping is maintained as part of the normal policy but a fast cache is
7*4882a593Smuzhiyun  * needed to reduce the lookup overhead.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Author: Paul Moore <paul@paul-moore.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This code is heavily based on the "netif" concept originally developed by
12*4882a593Smuzhiyun  * James Morris <jmorris@redhat.com>
13*4882a593Smuzhiyun  *   (see security/selinux/netif.c for more information)
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun  * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/rcupdate.h>
22*4882a593Smuzhiyun #include <linux/list.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/spinlock.h>
25*4882a593Smuzhiyun #include <linux/in.h>
26*4882a593Smuzhiyun #include <linux/in6.h>
27*4882a593Smuzhiyun #include <linux/ip.h>
28*4882a593Smuzhiyun #include <linux/ipv6.h>
29*4882a593Smuzhiyun #include <net/ip.h>
30*4882a593Smuzhiyun #include <net/ipv6.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include "netport.h"
33*4882a593Smuzhiyun #include "objsec.h"
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define SEL_NETPORT_HASH_SIZE       256
36*4882a593Smuzhiyun #define SEL_NETPORT_HASH_BKT_LIMIT   16
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun struct sel_netport_bkt {
39*4882a593Smuzhiyun 	int size;
40*4882a593Smuzhiyun 	struct list_head list;
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun struct sel_netport {
44*4882a593Smuzhiyun 	struct netport_security_struct psec;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	struct list_head list;
47*4882a593Smuzhiyun 	struct rcu_head rcu;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
51*4882a593Smuzhiyun  * for this is that I suspect most users will not make heavy use of both
52*4882a593Smuzhiyun  * address families at the same time so one table will usually end up wasted,
53*4882a593Smuzhiyun  * if this becomes a problem we can always add a hash table for each address
54*4882a593Smuzhiyun  * family later */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static LIST_HEAD(sel_netport_list);
57*4882a593Smuzhiyun static DEFINE_SPINLOCK(sel_netport_lock);
58*4882a593Smuzhiyun static struct sel_netport_bkt sel_netport_hash[SEL_NETPORT_HASH_SIZE];
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun  * sel_netport_hashfn - Hashing function for the port table
62*4882a593Smuzhiyun  * @pnum: port number
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Description:
65*4882a593Smuzhiyun  * This is the hashing function for the port table, it returns the bucket
66*4882a593Smuzhiyun  * number for the given port.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  */
sel_netport_hashfn(u16 pnum)69*4882a593Smuzhiyun static unsigned int sel_netport_hashfn(u16 pnum)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	return (pnum & (SEL_NETPORT_HASH_SIZE - 1));
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * sel_netport_find - Search for a port record
76*4882a593Smuzhiyun  * @protocol: protocol
77*4882a593Smuzhiyun  * @port: pnum
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Description:
80*4882a593Smuzhiyun  * Search the network port table and return the matching record.  If an entry
81*4882a593Smuzhiyun  * can not be found in the table return NULL.
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  */
sel_netport_find(u8 protocol,u16 pnum)84*4882a593Smuzhiyun static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	unsigned int idx;
87*4882a593Smuzhiyun 	struct sel_netport *port;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	idx = sel_netport_hashfn(pnum);
90*4882a593Smuzhiyun 	list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)
91*4882a593Smuzhiyun 		if (port->psec.port == pnum && port->psec.protocol == protocol)
92*4882a593Smuzhiyun 			return port;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return NULL;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun  * sel_netport_insert - Insert a new port into the table
99*4882a593Smuzhiyun  * @port: the new port record
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * Description:
102*4882a593Smuzhiyun  * Add a new port record to the network address hash table.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  */
sel_netport_insert(struct sel_netport * port)105*4882a593Smuzhiyun static void sel_netport_insert(struct sel_netport *port)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	unsigned int idx;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* we need to impose a limit on the growth of the hash table so check
110*4882a593Smuzhiyun 	 * this bucket to make sure it is within the specified bounds */
111*4882a593Smuzhiyun 	idx = sel_netport_hashfn(port->psec.port);
112*4882a593Smuzhiyun 	list_add_rcu(&port->list, &sel_netport_hash[idx].list);
113*4882a593Smuzhiyun 	if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {
114*4882a593Smuzhiyun 		struct sel_netport *tail;
115*4882a593Smuzhiyun 		tail = list_entry(
116*4882a593Smuzhiyun 			rcu_dereference_protected(
117*4882a593Smuzhiyun 				sel_netport_hash[idx].list.prev,
118*4882a593Smuzhiyun 				lockdep_is_held(&sel_netport_lock)),
119*4882a593Smuzhiyun 			struct sel_netport, list);
120*4882a593Smuzhiyun 		list_del_rcu(&tail->list);
121*4882a593Smuzhiyun 		kfree_rcu(tail, rcu);
122*4882a593Smuzhiyun 	} else
123*4882a593Smuzhiyun 		sel_netport_hash[idx].size++;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * sel_netport_sid_slow - Lookup the SID of a network address using the policy
128*4882a593Smuzhiyun  * @protocol: protocol
129*4882a593Smuzhiyun  * @pnum: port
130*4882a593Smuzhiyun  * @sid: port SID
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  * Description:
133*4882a593Smuzhiyun  * This function determines the SID of a network port by querying the security
134*4882a593Smuzhiyun  * policy.  The result is added to the network port table to speedup future
135*4882a593Smuzhiyun  * queries.  Returns zero on success, negative values on failure.
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  */
sel_netport_sid_slow(u8 protocol,u16 pnum,u32 * sid)138*4882a593Smuzhiyun static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	int ret;
141*4882a593Smuzhiyun 	struct sel_netport *port;
142*4882a593Smuzhiyun 	struct sel_netport *new;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	spin_lock_bh(&sel_netport_lock);
145*4882a593Smuzhiyun 	port = sel_netport_find(protocol, pnum);
146*4882a593Smuzhiyun 	if (port != NULL) {
147*4882a593Smuzhiyun 		*sid = port->psec.sid;
148*4882a593Smuzhiyun 		spin_unlock_bh(&sel_netport_lock);
149*4882a593Smuzhiyun 		return 0;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	ret = security_port_sid(&selinux_state, protocol, pnum, sid);
153*4882a593Smuzhiyun 	if (ret != 0)
154*4882a593Smuzhiyun 		goto out;
155*4882a593Smuzhiyun 	new = kzalloc(sizeof(*new), GFP_ATOMIC);
156*4882a593Smuzhiyun 	if (new) {
157*4882a593Smuzhiyun 		new->psec.port = pnum;
158*4882a593Smuzhiyun 		new->psec.protocol = protocol;
159*4882a593Smuzhiyun 		new->psec.sid = *sid;
160*4882a593Smuzhiyun 		sel_netport_insert(new);
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun out:
164*4882a593Smuzhiyun 	spin_unlock_bh(&sel_netport_lock);
165*4882a593Smuzhiyun 	if (unlikely(ret))
166*4882a593Smuzhiyun 		pr_warn("SELinux: failure in %s(), unable to determine network port label\n",
167*4882a593Smuzhiyun 			__func__);
168*4882a593Smuzhiyun 	return ret;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /**
172*4882a593Smuzhiyun  * sel_netport_sid - Lookup the SID of a network port
173*4882a593Smuzhiyun  * @protocol: protocol
174*4882a593Smuzhiyun  * @pnum: port
175*4882a593Smuzhiyun  * @sid: port SID
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  * Description:
178*4882a593Smuzhiyun  * This function determines the SID of a network port using the fastest method
179*4882a593Smuzhiyun  * possible.  First the port table is queried, but if an entry can't be found
180*4882a593Smuzhiyun  * then the policy is queried and the result is added to the table to speedup
181*4882a593Smuzhiyun  * future queries.  Returns zero on success, negative values on failure.
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  */
sel_netport_sid(u8 protocol,u16 pnum,u32 * sid)184*4882a593Smuzhiyun int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct sel_netport *port;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	rcu_read_lock();
189*4882a593Smuzhiyun 	port = sel_netport_find(protocol, pnum);
190*4882a593Smuzhiyun 	if (port != NULL) {
191*4882a593Smuzhiyun 		*sid = port->psec.sid;
192*4882a593Smuzhiyun 		rcu_read_unlock();
193*4882a593Smuzhiyun 		return 0;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 	rcu_read_unlock();
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return sel_netport_sid_slow(protocol, pnum, sid);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun  * sel_netport_flush - Flush the entire network port table
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * Description:
204*4882a593Smuzhiyun  * Remove all entries from the network address table.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  */
sel_netport_flush(void)207*4882a593Smuzhiyun void sel_netport_flush(void)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	unsigned int idx;
210*4882a593Smuzhiyun 	struct sel_netport *port, *port_tmp;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	spin_lock_bh(&sel_netport_lock);
213*4882a593Smuzhiyun 	for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {
214*4882a593Smuzhiyun 		list_for_each_entry_safe(port, port_tmp,
215*4882a593Smuzhiyun 					 &sel_netport_hash[idx].list, list) {
216*4882a593Smuzhiyun 			list_del_rcu(&port->list);
217*4882a593Smuzhiyun 			kfree_rcu(port, rcu);
218*4882a593Smuzhiyun 		}
219*4882a593Smuzhiyun 		sel_netport_hash[idx].size = 0;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 	spin_unlock_bh(&sel_netport_lock);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
sel_netport_init(void)224*4882a593Smuzhiyun static __init int sel_netport_init(void)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	int iter;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	if (!selinux_enabled_boot)
229*4882a593Smuzhiyun 		return 0;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {
232*4882a593Smuzhiyun 		INIT_LIST_HEAD(&sel_netport_hash[iter].list);
233*4882a593Smuzhiyun 		sel_netport_hash[iter].size = 0;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	return 0;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun __initcall(sel_netport_init);
240