1 /*
2  * Network interface table.
3  *
4  * Network interfaces (devices) do not have a security field, so we
5  * maintain a table associating each interface with a SID.
6  *
7  * Author: James Morris <jmorris@redhat.com>
8  *
9  * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
10  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
11  *		      Paul Moore <paul.moore@hp.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2,
15  * as published by the Free Software Foundation.
16  */
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/stddef.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/notifier.h>
24 #include <linux/netdevice.h>
25 #include <linux/rcupdate.h>
26 #include <net/net_namespace.h>
27 
28 #include "security.h"
29 #include "objsec.h"
30 #include "netif.h"
31 
32 #define SEL_NETIF_HASH_SIZE	64
33 #define SEL_NETIF_HASH_MAX	1024
34 
35 struct sel_netif {
36 	struct list_head list;
37 	struct netif_security_struct nsec;
38 	struct rcu_head rcu_head;
39 };
40 
41 static u32 sel_netif_total;
42 static LIST_HEAD(sel_netif_list);
43 static DEFINE_SPINLOCK(sel_netif_lock);
44 static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
45 
46 /**
47  * sel_netif_hashfn - Hashing function for the interface table
48  * @ifindex: the network interface
49  *
50  * Description:
51  * This is the hashing function for the network interface table, it returns the
52  * bucket number for the given interface.
53  *
54  */
sel_netif_hashfn(int ifindex)55 static inline u32 sel_netif_hashfn(int ifindex)
56 {
57 	return (ifindex & (SEL_NETIF_HASH_SIZE - 1));
58 }
59 
60 /**
61  * sel_netif_find - Search for an interface record
62  * @ifindex: the network interface
63  *
64  * Description:
65  * Search the network interface table and return the record matching @ifindex.
66  * If an entry can not be found in the table return NULL.
67  *
68  */
sel_netif_find(int ifindex)69 static inline struct sel_netif *sel_netif_find(int ifindex)
70 {
71 	int idx = sel_netif_hashfn(ifindex);
72 	struct sel_netif *netif;
73 
74 	list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
75 		/* all of the devices should normally fit in the hash, so we
76 		 * optimize for that case */
77 		if (likely(netif->nsec.ifindex == ifindex))
78 			return netif;
79 
80 	return NULL;
81 }
82 
83 /**
84  * sel_netif_insert - Insert a new interface into the table
85  * @netif: the new interface record
86  *
87  * Description:
88  * Add a new interface record to the network interface hash table.  Returns
89  * zero on success, negative values on failure.
90  *
91  */
sel_netif_insert(struct sel_netif * netif)92 static int sel_netif_insert(struct sel_netif *netif)
93 {
94 	int idx;
95 
96 	if (sel_netif_total >= SEL_NETIF_HASH_MAX)
97 		return -ENOSPC;
98 
99 	idx = sel_netif_hashfn(netif->nsec.ifindex);
100 	list_add_rcu(&netif->list, &sel_netif_hash[idx]);
101 	sel_netif_total++;
102 
103 	return 0;
104 }
105 
106 /**
107  * sel_netif_free - Frees an interface entry
108  * @p: the entry's RCU field
109  *
110  * Description:
111  * This function is designed to be used as a callback to the call_rcu()
112  * function so that memory allocated to a hash table interface entry can be
113  * released safely.
114  *
115  */
sel_netif_free(struct rcu_head * p)116 static void sel_netif_free(struct rcu_head *p)
117 {
118 	struct sel_netif *netif = container_of(p, struct sel_netif, rcu_head);
119 	kfree(netif);
120 }
121 
122 /**
123  * sel_netif_destroy - Remove an interface record from the table
124  * @netif: the existing interface record
125  *
126  * Description:
127  * Remove an existing interface record from the network interface table.
128  *
129  */
sel_netif_destroy(struct sel_netif * netif)130 static void sel_netif_destroy(struct sel_netif *netif)
131 {
132 	list_del_rcu(&netif->list);
133 	sel_netif_total--;
134 	call_rcu(&netif->rcu_head, sel_netif_free);
135 }
136 
137 /**
138  * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
139  * @ifindex: the network interface
140  * @sid: interface SID
141  *
142  * Description:
143  * This function determines the SID of a network interface by quering the
144  * security policy.  The result is added to the network interface table to
145  * speedup future queries.  Returns zero on success, negative values on
146  * failure.
147  *
148  */
sel_netif_sid_slow(int ifindex,u32 * sid)149 static int sel_netif_sid_slow(int ifindex, u32 *sid)
150 {
151 	int ret;
152 	struct sel_netif *netif;
153 	struct sel_netif *new = NULL;
154 	struct net_device *dev;
155 
156 	/* NOTE: we always use init's network namespace since we don't
157 	 * currently support containers */
158 
159 	dev = dev_get_by_index(&init_net, ifindex);
160 	if (unlikely(dev == NULL)) {
161 		printk(KERN_WARNING
162 		       "SELinux: failure in sel_netif_sid_slow(),"
163 		       " invalid network interface (%d)\n", ifindex);
164 		return -ENOENT;
165 	}
166 
167 	spin_lock_bh(&sel_netif_lock);
168 	netif = sel_netif_find(ifindex);
169 	if (netif != NULL) {
170 		*sid = netif->nsec.sid;
171 		ret = 0;
172 		goto out;
173 	}
174 	new = kzalloc(sizeof(*new), GFP_ATOMIC);
175 	if (new == NULL) {
176 		ret = -ENOMEM;
177 		goto out;
178 	}
179 	ret = security_netif_sid(dev->name, &new->nsec.sid);
180 	if (ret != 0)
181 		goto out;
182 	new->nsec.ifindex = ifindex;
183 	ret = sel_netif_insert(new);
184 	if (ret != 0)
185 		goto out;
186 	*sid = new->nsec.sid;
187 
188 out:
189 	spin_unlock_bh(&sel_netif_lock);
190 	dev_put(dev);
191 	if (unlikely(ret)) {
192 		printk(KERN_WARNING
193 		       "SELinux: failure in sel_netif_sid_slow(),"
194 		       " unable to determine network interface label (%d)\n",
195 		       ifindex);
196 		kfree(new);
197 	}
198 	return ret;
199 }
200 
201 /**
202  * sel_netif_sid - Lookup the SID of a network interface
203  * @ifindex: the network interface
204  * @sid: interface SID
205  *
206  * Description:
207  * This function determines the SID of a network interface using the fastest
208  * method possible.  First the interface table is queried, but if an entry
209  * can't be found then the policy is queried and the result is added to the
210  * table to speedup future queries.  Returns zero on success, negative values
211  * on failure.
212  *
213  */
sel_netif_sid(int ifindex,u32 * sid)214 int sel_netif_sid(int ifindex, u32 *sid)
215 {
216 	struct sel_netif *netif;
217 
218 	rcu_read_lock();
219 	netif = sel_netif_find(ifindex);
220 	if (likely(netif != NULL)) {
221 		*sid = netif->nsec.sid;
222 		rcu_read_unlock();
223 		return 0;
224 	}
225 	rcu_read_unlock();
226 
227 	return sel_netif_sid_slow(ifindex, sid);
228 }
229 
230 /**
231  * sel_netif_kill - Remove an entry from the network interface table
232  * @ifindex: the network interface
233  *
234  * Description:
235  * This function removes the entry matching @ifindex from the network interface
236  * table if it exists.
237  *
238  */
sel_netif_kill(int ifindex)239 static void sel_netif_kill(int ifindex)
240 {
241 	struct sel_netif *netif;
242 
243 	rcu_read_lock();
244 	spin_lock_bh(&sel_netif_lock);
245 	netif = sel_netif_find(ifindex);
246 	if (netif)
247 		sel_netif_destroy(netif);
248 	spin_unlock_bh(&sel_netif_lock);
249 	rcu_read_unlock();
250 }
251 
252 /**
253  * sel_netif_flush - Flush the entire network interface table
254  *
255  * Description:
256  * Remove all entries from the network interface table.
257  *
258  */
sel_netif_flush(void)259 static void sel_netif_flush(void)
260 {
261 	int idx;
262 	struct sel_netif *netif;
263 
264 	spin_lock_bh(&sel_netif_lock);
265 	for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
266 		list_for_each_entry(netif, &sel_netif_hash[idx], list)
267 			sel_netif_destroy(netif);
268 	spin_unlock_bh(&sel_netif_lock);
269 }
270 
sel_netif_avc_callback(u32 event,u32 ssid,u32 tsid,u16 class,u32 perms,u32 * retained)271 static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
272 				  u16 class, u32 perms, u32 *retained)
273 {
274 	if (event == AVC_CALLBACK_RESET) {
275 		sel_netif_flush();
276 		synchronize_net();
277 	}
278 	return 0;
279 }
280 
sel_netif_netdev_notifier_handler(struct notifier_block * this,unsigned long event,void * ptr)281 static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
282 					     unsigned long event, void *ptr)
283 {
284 	struct net_device *dev = ptr;
285 
286 	if (dev_net(dev) != &init_net)
287 		return NOTIFY_DONE;
288 
289 	if (event == NETDEV_DOWN)
290 		sel_netif_kill(dev->ifindex);
291 
292 	return NOTIFY_DONE;
293 }
294 
295 static struct notifier_block sel_netif_netdev_notifier = {
296 	.notifier_call = sel_netif_netdev_notifier_handler,
297 };
298 
sel_netif_init(void)299 static __init int sel_netif_init(void)
300 {
301 	int i, err;
302 
303 	if (!selinux_enabled)
304 		return 0;
305 
306 	for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
307 		INIT_LIST_HEAD(&sel_netif_hash[i]);
308 
309 	register_netdevice_notifier(&sel_netif_netdev_notifier);
310 
311 	err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
312 			       SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
313 	if (err)
314 		panic("avc_add_callback() failed, error %d\n", err);
315 
316 	return err;
317 }
318 
319 __initcall(sel_netif_init);
320 
321