xref: /openbmc/linux/security/selinux/netnode.c (revision a639e7ca8e8282b75be2724a28bfc788aa3bb156)
1224dfbd8SPaul Moore /*
2224dfbd8SPaul Moore  * Network node table
3224dfbd8SPaul Moore  *
4224dfbd8SPaul Moore  * SELinux must keep a mapping of network nodes to labels/SIDs.  This
5224dfbd8SPaul Moore  * mapping is maintained as part of the normal policy but a fast cache is
6224dfbd8SPaul Moore  * needed to reduce the lookup overhead since most of these queries happen on
7224dfbd8SPaul Moore  * a per-packet basis.
8224dfbd8SPaul Moore  *
9224dfbd8SPaul Moore  * Author: Paul Moore <paul.moore@hp.com>
10224dfbd8SPaul Moore  *
11224dfbd8SPaul Moore  * This code is heavily based on the "netif" concept originally developed by
12224dfbd8SPaul Moore  * James Morris <jmorris@redhat.com>
13224dfbd8SPaul Moore  *   (see security/selinux/netif.c for more information)
14224dfbd8SPaul Moore  *
15224dfbd8SPaul Moore  */
16224dfbd8SPaul Moore 
17224dfbd8SPaul Moore /*
18224dfbd8SPaul Moore  * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
19224dfbd8SPaul Moore  *
20224dfbd8SPaul Moore  * This program is free software: you can redistribute it and/or modify
21224dfbd8SPaul Moore  * it under the terms of version 2 of the GNU General Public License as
22224dfbd8SPaul Moore  * published by the Free Software Foundation.
23224dfbd8SPaul Moore  *
24224dfbd8SPaul Moore  * This program is distributed in the hope that it will be useful,
25224dfbd8SPaul Moore  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26224dfbd8SPaul Moore  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27224dfbd8SPaul Moore  * GNU General Public License for more details.
28224dfbd8SPaul Moore  *
29224dfbd8SPaul Moore  */
30224dfbd8SPaul Moore 
31224dfbd8SPaul Moore #include <linux/types.h>
32224dfbd8SPaul Moore #include <linux/rcupdate.h>
33224dfbd8SPaul Moore #include <linux/list.h>
34224dfbd8SPaul Moore #include <linux/spinlock.h>
35224dfbd8SPaul Moore #include <linux/in.h>
36224dfbd8SPaul Moore #include <linux/in6.h>
37224dfbd8SPaul Moore #include <linux/ip.h>
38224dfbd8SPaul Moore #include <linux/ipv6.h>
39224dfbd8SPaul Moore #include <net/ip.h>
40224dfbd8SPaul Moore #include <net/ipv6.h>
41224dfbd8SPaul Moore #include <asm/bug.h>
42224dfbd8SPaul Moore 
43*a639e7caSPaul Moore #include "netnode.h"
44224dfbd8SPaul Moore #include "objsec.h"
45224dfbd8SPaul Moore 
46224dfbd8SPaul Moore #define SEL_NETNODE_HASH_SIZE       256
47224dfbd8SPaul Moore #define SEL_NETNODE_HASH_BKT_LIMIT   16
48224dfbd8SPaul Moore 
49*a639e7caSPaul Moore struct sel_netnode_bkt {
50*a639e7caSPaul Moore 	unsigned int size;
51*a639e7caSPaul Moore 	struct list_head list;
52*a639e7caSPaul Moore };
53*a639e7caSPaul Moore 
54224dfbd8SPaul Moore struct sel_netnode {
55224dfbd8SPaul Moore 	struct netnode_security_struct nsec;
56224dfbd8SPaul Moore 
57224dfbd8SPaul Moore 	struct list_head list;
58224dfbd8SPaul Moore 	struct rcu_head rcu;
59224dfbd8SPaul Moore };
60224dfbd8SPaul Moore 
61224dfbd8SPaul Moore /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
62224dfbd8SPaul Moore  * for this is that I suspect most users will not make heavy use of both
63224dfbd8SPaul Moore  * address families at the same time so one table will usually end up wasted,
64224dfbd8SPaul Moore  * if this becomes a problem we can always add a hash table for each address
65224dfbd8SPaul Moore  * family later */
66224dfbd8SPaul Moore 
67224dfbd8SPaul Moore static LIST_HEAD(sel_netnode_list);
68224dfbd8SPaul Moore static DEFINE_SPINLOCK(sel_netnode_lock);
69*a639e7caSPaul Moore static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];
70224dfbd8SPaul Moore 
71224dfbd8SPaul Moore /**
72224dfbd8SPaul Moore  * sel_netnode_free - Frees a node entry
73224dfbd8SPaul Moore  * @p: the entry's RCU field
74224dfbd8SPaul Moore  *
75224dfbd8SPaul Moore  * Description:
76224dfbd8SPaul Moore  * This function is designed to be used as a callback to the call_rcu()
77224dfbd8SPaul Moore  * function so that memory allocated to a hash table node entry can be
78224dfbd8SPaul Moore  * released safely.
79224dfbd8SPaul Moore  *
80224dfbd8SPaul Moore  */
81224dfbd8SPaul Moore static void sel_netnode_free(struct rcu_head *p)
82224dfbd8SPaul Moore {
83224dfbd8SPaul Moore 	struct sel_netnode *node = container_of(p, struct sel_netnode, rcu);
84224dfbd8SPaul Moore 	kfree(node);
85224dfbd8SPaul Moore }
86224dfbd8SPaul Moore 
87224dfbd8SPaul Moore /**
88224dfbd8SPaul Moore  * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table
89224dfbd8SPaul Moore  * @addr: IPv4 address
90224dfbd8SPaul Moore  *
91224dfbd8SPaul Moore  * Description:
92224dfbd8SPaul Moore  * This is the IPv4 hashing function for the node interface table, it returns
93224dfbd8SPaul Moore  * the bucket number for the given IP address.
94224dfbd8SPaul Moore  *
95224dfbd8SPaul Moore  */
96*a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)
97224dfbd8SPaul Moore {
98224dfbd8SPaul Moore 	/* at some point we should determine if the mismatch in byte order
99224dfbd8SPaul Moore 	 * affects the hash function dramatically */
100224dfbd8SPaul Moore 	return (addr & (SEL_NETNODE_HASH_SIZE - 1));
101224dfbd8SPaul Moore }
102224dfbd8SPaul Moore 
103224dfbd8SPaul Moore /**
104224dfbd8SPaul Moore  * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table
105224dfbd8SPaul Moore  * @addr: IPv6 address
106224dfbd8SPaul Moore  *
107224dfbd8SPaul Moore  * Description:
108224dfbd8SPaul Moore  * This is the IPv6 hashing function for the node interface table, it returns
109224dfbd8SPaul Moore  * the bucket number for the given IP address.
110224dfbd8SPaul Moore  *
111224dfbd8SPaul Moore  */
112*a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
113224dfbd8SPaul Moore {
114224dfbd8SPaul Moore 	/* just hash the least significant 32 bits to keep things fast (they
115224dfbd8SPaul Moore 	 * are the most likely to be different anyway), we can revisit this
116224dfbd8SPaul Moore 	 * later if needed */
117224dfbd8SPaul Moore 	return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
118224dfbd8SPaul Moore }
119224dfbd8SPaul Moore 
120224dfbd8SPaul Moore /**
121224dfbd8SPaul Moore  * sel_netnode_find - Search for a node record
122224dfbd8SPaul Moore  * @addr: IP address
123224dfbd8SPaul Moore  * @family: address family
124224dfbd8SPaul Moore  *
125224dfbd8SPaul Moore  * Description:
126224dfbd8SPaul Moore  * Search the network node table and return the record matching @addr.  If an
127224dfbd8SPaul Moore  * entry can not be found in the table return NULL.
128224dfbd8SPaul Moore  *
129224dfbd8SPaul Moore  */
130224dfbd8SPaul Moore static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
131224dfbd8SPaul Moore {
132*a639e7caSPaul Moore 	unsigned int idx;
133224dfbd8SPaul Moore 	struct sel_netnode *node;
134224dfbd8SPaul Moore 
135224dfbd8SPaul Moore 	switch (family) {
136224dfbd8SPaul Moore 	case PF_INET:
137224dfbd8SPaul Moore 		idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr);
138224dfbd8SPaul Moore 		break;
139224dfbd8SPaul Moore 	case PF_INET6:
140224dfbd8SPaul Moore 		idx = sel_netnode_hashfn_ipv6(addr);
141224dfbd8SPaul Moore 		break;
142224dfbd8SPaul Moore 	default:
143224dfbd8SPaul Moore 		BUG();
144224dfbd8SPaul Moore 	}
145224dfbd8SPaul Moore 
146*a639e7caSPaul Moore 	list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
147224dfbd8SPaul Moore 		if (node->nsec.family == family)
148224dfbd8SPaul Moore 			switch (family) {
149224dfbd8SPaul Moore 			case PF_INET:
150224dfbd8SPaul Moore 				if (node->nsec.addr.ipv4 == *(__be32 *)addr)
151224dfbd8SPaul Moore 					return node;
152224dfbd8SPaul Moore 				break;
153224dfbd8SPaul Moore 			case PF_INET6:
154224dfbd8SPaul Moore 				if (ipv6_addr_equal(&node->nsec.addr.ipv6,
155224dfbd8SPaul Moore 						    addr))
156224dfbd8SPaul Moore 					return node;
157224dfbd8SPaul Moore 				break;
158224dfbd8SPaul Moore 			}
159224dfbd8SPaul Moore 
160224dfbd8SPaul Moore 	return NULL;
161224dfbd8SPaul Moore }
162224dfbd8SPaul Moore 
163224dfbd8SPaul Moore /**
164224dfbd8SPaul Moore  * sel_netnode_insert - Insert a new node into the table
165224dfbd8SPaul Moore  * @node: the new node record
166224dfbd8SPaul Moore  *
167224dfbd8SPaul Moore  * Description:
168*a639e7caSPaul Moore  * Add a new node record to the network address hash table.
169224dfbd8SPaul Moore  *
170224dfbd8SPaul Moore  */
171*a639e7caSPaul Moore static void sel_netnode_insert(struct sel_netnode *node)
172224dfbd8SPaul Moore {
173*a639e7caSPaul Moore 	unsigned int idx;
174224dfbd8SPaul Moore 
175224dfbd8SPaul Moore 	switch (node->nsec.family) {
176224dfbd8SPaul Moore 	case PF_INET:
177224dfbd8SPaul Moore 		idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
178224dfbd8SPaul Moore 		break;
179224dfbd8SPaul Moore 	case PF_INET6:
180224dfbd8SPaul Moore 		idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
181224dfbd8SPaul Moore 		break;
182224dfbd8SPaul Moore 	default:
183224dfbd8SPaul Moore 		BUG();
184224dfbd8SPaul Moore 	}
185*a639e7caSPaul Moore 
186*a639e7caSPaul Moore 	INIT_RCU_HEAD(&node->rcu);
187224dfbd8SPaul Moore 
188224dfbd8SPaul Moore 	/* we need to impose a limit on the growth of the hash table so check
189224dfbd8SPaul Moore 	 * this bucket to make sure it is within the specified bounds */
190*a639e7caSPaul Moore 	list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
191*a639e7caSPaul Moore 	if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
192*a639e7caSPaul Moore 		struct sel_netnode *tail;
193*a639e7caSPaul Moore 		tail = list_entry(
194*a639e7caSPaul Moore 			rcu_dereference(sel_netnode_hash[idx].list.prev),
195*a639e7caSPaul Moore 			struct sel_netnode, list);
196*a639e7caSPaul Moore 		list_del_rcu(&tail->list);
197*a639e7caSPaul Moore 		call_rcu(&tail->rcu, sel_netnode_free);
198*a639e7caSPaul Moore 	} else
199*a639e7caSPaul Moore 		sel_netnode_hash[idx].size++;
200224dfbd8SPaul Moore }
201224dfbd8SPaul Moore 
202224dfbd8SPaul Moore /**
203224dfbd8SPaul Moore  * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
204224dfbd8SPaul Moore  * @addr: the IP address
205224dfbd8SPaul Moore  * @family: the address family
206224dfbd8SPaul Moore  * @sid: node SID
207224dfbd8SPaul Moore  *
208224dfbd8SPaul Moore  * Description:
209224dfbd8SPaul Moore  * This function determines the SID of a network address by quering the
210224dfbd8SPaul Moore  * security policy.  The result is added to the network address table to
211224dfbd8SPaul Moore  * speedup future queries.  Returns zero on success, negative values on
212224dfbd8SPaul Moore  * failure.
213224dfbd8SPaul Moore  *
214224dfbd8SPaul Moore  */
215224dfbd8SPaul Moore static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
216224dfbd8SPaul Moore {
217*a639e7caSPaul Moore 	int ret = -ENOMEM;
218224dfbd8SPaul Moore 	struct sel_netnode *node;
219224dfbd8SPaul Moore 	struct sel_netnode *new = NULL;
220224dfbd8SPaul Moore 
221224dfbd8SPaul Moore 	spin_lock_bh(&sel_netnode_lock);
222224dfbd8SPaul Moore 	node = sel_netnode_find(addr, family);
223224dfbd8SPaul Moore 	if (node != NULL) {
224224dfbd8SPaul Moore 		*sid = node->nsec.sid;
225*a639e7caSPaul Moore 		spin_unlock_bh(&sel_netnode_lock);
226*a639e7caSPaul Moore 		return 0;
227224dfbd8SPaul Moore 	}
228224dfbd8SPaul Moore 	new = kzalloc(sizeof(*new), GFP_ATOMIC);
229*a639e7caSPaul Moore 	if (new == NULL)
230224dfbd8SPaul Moore 		goto out;
231224dfbd8SPaul Moore 	switch (family) {
232224dfbd8SPaul Moore 	case PF_INET:
233224dfbd8SPaul Moore 		ret = security_node_sid(PF_INET,
234*a639e7caSPaul Moore 					addr, sizeof(struct in_addr), sid);
235224dfbd8SPaul Moore 		new->nsec.addr.ipv4 = *(__be32 *)addr;
236224dfbd8SPaul Moore 		break;
237224dfbd8SPaul Moore 	case PF_INET6:
238224dfbd8SPaul Moore 		ret = security_node_sid(PF_INET6,
239*a639e7caSPaul Moore 					addr, sizeof(struct in6_addr), sid);
240224dfbd8SPaul Moore 		ipv6_addr_copy(&new->nsec.addr.ipv6, addr);
241224dfbd8SPaul Moore 		break;
242224dfbd8SPaul Moore 	default:
243224dfbd8SPaul Moore 		BUG();
244224dfbd8SPaul Moore 	}
245224dfbd8SPaul Moore 	if (ret != 0)
246224dfbd8SPaul Moore 		goto out;
247*a639e7caSPaul Moore 
248224dfbd8SPaul Moore 	new->nsec.family = family;
249*a639e7caSPaul Moore 	new->nsec.sid = *sid;
250*a639e7caSPaul Moore 	sel_netnode_insert(new);
251224dfbd8SPaul Moore 
252224dfbd8SPaul Moore out:
253224dfbd8SPaul Moore 	spin_unlock_bh(&sel_netnode_lock);
25471f1cb05SPaul Moore 	if (unlikely(ret)) {
25571f1cb05SPaul Moore 		printk(KERN_WARNING
25671f1cb05SPaul Moore 		       "SELinux: failure in sel_netnode_sid_slow(),"
25771f1cb05SPaul Moore 		       " unable to determine network node label\n");
258224dfbd8SPaul Moore 		kfree(new);
25971f1cb05SPaul Moore 	}
260224dfbd8SPaul Moore 	return ret;
261224dfbd8SPaul Moore }
262224dfbd8SPaul Moore 
263224dfbd8SPaul Moore /**
264224dfbd8SPaul Moore  * sel_netnode_sid - Lookup the SID of a network address
265224dfbd8SPaul Moore  * @addr: the IP address
266224dfbd8SPaul Moore  * @family: the address family
267224dfbd8SPaul Moore  * @sid: node SID
268224dfbd8SPaul Moore  *
269224dfbd8SPaul Moore  * Description:
270224dfbd8SPaul Moore  * This function determines the SID of a network address using the fastest
271224dfbd8SPaul Moore  * method possible.  First the address table is queried, but if an entry
272224dfbd8SPaul Moore  * can't be found then the policy is queried and the result is added to the
273224dfbd8SPaul Moore  * table to speedup future queries.  Returns zero on success, negative values
274224dfbd8SPaul Moore  * on failure.
275224dfbd8SPaul Moore  *
276224dfbd8SPaul Moore  */
277224dfbd8SPaul Moore int sel_netnode_sid(void *addr, u16 family, u32 *sid)
278224dfbd8SPaul Moore {
279224dfbd8SPaul Moore 	struct sel_netnode *node;
280224dfbd8SPaul Moore 
281224dfbd8SPaul Moore 	rcu_read_lock();
282224dfbd8SPaul Moore 	node = sel_netnode_find(addr, family);
283224dfbd8SPaul Moore 	if (node != NULL) {
284224dfbd8SPaul Moore 		*sid = node->nsec.sid;
285224dfbd8SPaul Moore 		rcu_read_unlock();
286224dfbd8SPaul Moore 		return 0;
287224dfbd8SPaul Moore 	}
288224dfbd8SPaul Moore 	rcu_read_unlock();
289224dfbd8SPaul Moore 
290224dfbd8SPaul Moore 	return sel_netnode_sid_slow(addr, family, sid);
291224dfbd8SPaul Moore }
292224dfbd8SPaul Moore 
293224dfbd8SPaul Moore /**
294224dfbd8SPaul Moore  * sel_netnode_flush - Flush the entire network address table
295224dfbd8SPaul Moore  *
296224dfbd8SPaul Moore  * Description:
297224dfbd8SPaul Moore  * Remove all entries from the network address table.
298224dfbd8SPaul Moore  *
299224dfbd8SPaul Moore  */
300224dfbd8SPaul Moore static void sel_netnode_flush(void)
301224dfbd8SPaul Moore {
302*a639e7caSPaul Moore 	unsigned int idx;
303*a639e7caSPaul Moore 	struct sel_netnode *node, *node_tmp;
304224dfbd8SPaul Moore 
305224dfbd8SPaul Moore 	spin_lock_bh(&sel_netnode_lock);
306*a639e7caSPaul Moore 	for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
307*a639e7caSPaul Moore 		list_for_each_entry_safe(node, node_tmp,
308*a639e7caSPaul Moore 					 &sel_netnode_hash[idx].list, list) {
309*a639e7caSPaul Moore 				list_del_rcu(&node->list);
310*a639e7caSPaul Moore 				call_rcu(&node->rcu, sel_netnode_free);
311*a639e7caSPaul Moore 		}
312*a639e7caSPaul Moore 		sel_netnode_hash[idx].size = 0;
313*a639e7caSPaul Moore 	}
314224dfbd8SPaul Moore 	spin_unlock_bh(&sel_netnode_lock);
315224dfbd8SPaul Moore }
316224dfbd8SPaul Moore 
317224dfbd8SPaul Moore static int sel_netnode_avc_callback(u32 event, u32 ssid, u32 tsid,
318224dfbd8SPaul Moore 				    u16 class, u32 perms, u32 *retained)
319224dfbd8SPaul Moore {
320224dfbd8SPaul Moore 	if (event == AVC_CALLBACK_RESET) {
321224dfbd8SPaul Moore 		sel_netnode_flush();
322224dfbd8SPaul Moore 		synchronize_net();
323224dfbd8SPaul Moore 	}
324224dfbd8SPaul Moore 	return 0;
325224dfbd8SPaul Moore }
326224dfbd8SPaul Moore 
327224dfbd8SPaul Moore static __init int sel_netnode_init(void)
328224dfbd8SPaul Moore {
329224dfbd8SPaul Moore 	int iter;
330224dfbd8SPaul Moore 	int ret;
331224dfbd8SPaul Moore 
332224dfbd8SPaul Moore 	if (!selinux_enabled)
333224dfbd8SPaul Moore 		return 0;
334224dfbd8SPaul Moore 
335*a639e7caSPaul Moore 	for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
336*a639e7caSPaul Moore 		INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
337*a639e7caSPaul Moore 		sel_netnode_hash[iter].size = 0;
338*a639e7caSPaul Moore 	}
339224dfbd8SPaul Moore 
340224dfbd8SPaul Moore 	ret = avc_add_callback(sel_netnode_avc_callback, AVC_CALLBACK_RESET,
341224dfbd8SPaul Moore 			       SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
342224dfbd8SPaul Moore 	if (ret != 0)
343224dfbd8SPaul Moore 		panic("avc_add_callback() failed, error %d\n", ret);
344224dfbd8SPaul Moore 
345224dfbd8SPaul Moore 	return ret;
346224dfbd8SPaul Moore }
347224dfbd8SPaul Moore 
348224dfbd8SPaul Moore __initcall(sel_netnode_init);
349