netif.c 7.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 * Network interface table.
 *
 * Network interfaces (devices) do not have a security field, so we
 * maintain a table associating each interface with a SID.
 *
 * Author: James Morris <jmorris@redhat.com>
 *
 * 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@paul-moore.com>
L
Linus Torvalds 已提交
12 13 14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 */
#include <linux/init.h>
#include <linux/types.h>
19
#include <linux/slab.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25
#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/notifier.h>
#include <linux/netdevice.h>
#include <linux/rcupdate.h>
26
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34

#include "security.h"
#include "objsec.h"
#include "netif.h"

#define SEL_NETIF_HASH_SIZE	64
#define SEL_NETIF_HASH_MAX	1024

35
struct sel_netif {
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45
	struct list_head list;
	struct netif_security_struct nsec;
	struct rcu_head rcu_head;
};

static u32 sel_netif_total;
static LIST_HEAD(sel_netif_list);
static DEFINE_SPINLOCK(sel_netif_lock);
static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];

46 47 48 49 50 51 52 53 54 55
/**
 * sel_netif_hashfn - Hashing function for the interface table
 * @ifindex: the network interface
 *
 * Description:
 * This is the hashing function for the network interface table, it returns the
 * bucket number for the given interface.
 *
 */
static inline u32 sel_netif_hashfn(int ifindex)
L
Linus Torvalds 已提交
56
{
57
	return (ifindex & (SEL_NETIF_HASH_SIZE - 1));
L
Linus Torvalds 已提交
58 59
}

60 61 62 63 64 65 66 67
/**
 * sel_netif_find - Search for an interface record
 * @ifindex: the network interface
 *
 * Description:
 * Search the network interface table and return the record matching @ifindex.
 * If an entry can not be found in the table return NULL.
 *
L
Linus Torvalds 已提交
68
 */
69
static inline struct sel_netif *sel_netif_find(int ifindex)
L
Linus Torvalds 已提交
70
{
71 72
	int idx = sel_netif_hashfn(ifindex);
	struct sel_netif *netif;
L
Linus Torvalds 已提交
73

74 75 76 77
	list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
		/* all of the devices should normally fit in the hash, so we
		 * optimize for that case */
		if (likely(netif->nsec.ifindex == ifindex))
L
Linus Torvalds 已提交
78
			return netif;
79

L
Linus Torvalds 已提交
80 81 82
	return NULL;
}

83 84 85 86 87 88 89 90 91
/**
 * sel_netif_insert - Insert a new interface into the table
 * @netif: the new interface record
 *
 * Description:
 * Add a new interface record to the network interface hash table.  Returns
 * zero on success, negative values on failure.
 *
 */
L
Linus Torvalds 已提交
92 93
static int sel_netif_insert(struct sel_netif *netif)
{
94
	int idx;
95

96 97
	if (sel_netif_total >= SEL_NETIF_HASH_MAX)
		return -ENOSPC;
98

99
	idx = sel_netif_hashfn(netif->nsec.ifindex);
L
Linus Torvalds 已提交
100 101
	list_add_rcu(&netif->list, &sel_netif_hash[idx]);
	sel_netif_total++;
102 103

	return 0;
L
Linus Torvalds 已提交
104 105
}

106 107 108 109 110 111 112 113
/**
 * sel_netif_destroy - Remove an interface record from the table
 * @netif: the existing interface record
 *
 * Description:
 * Remove an existing interface record from the network interface table.
 *
 */
L
Linus Torvalds 已提交
114 115 116 117
static void sel_netif_destroy(struct sel_netif *netif)
{
	list_del_rcu(&netif->list);
	sel_netif_total--;
118
	kfree_rcu(netif, rcu_head);
L
Linus Torvalds 已提交
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133
/**
 * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
 * @ifindex: the network interface
 * @sid: interface SID
 *
 * Description:
 * This function determines the SID of a network interface by quering the
 * security policy.  The result is added to the network interface table to
 * speedup future queries.  Returns zero on success, negative values on
 * failure.
 *
 */
static int sel_netif_sid_slow(int ifindex, u32 *sid)
L
Linus Torvalds 已提交
134 135
{
	int ret;
136 137 138
	struct sel_netif *netif;
	struct sel_netif *new = NULL;
	struct net_device *dev;
L
Linus Torvalds 已提交
139

140 141
	/* NOTE: we always use init's network namespace since we don't
	 * currently support containers */
L
Linus Torvalds 已提交
142

143
	dev = dev_get_by_index(&init_net, ifindex);
144 145 146 147
	if (unlikely(dev == NULL)) {
		printk(KERN_WARNING
		       "SELinux: failure in sel_netif_sid_slow(),"
		       " invalid network interface (%d)\n", ifindex);
148
		return -ENOENT;
149
	}
L
Linus Torvalds 已提交
150 151

	spin_lock_bh(&sel_netif_lock);
152 153 154 155
	netif = sel_netif_find(ifindex);
	if (netif != NULL) {
		*sid = netif->nsec.sid;
		ret = 0;
L
Linus Torvalds 已提交
156 157
		goto out;
	}
158 159 160
	new = kzalloc(sizeof(*new), GFP_ATOMIC);
	if (new == NULL) {
		ret = -ENOMEM;
L
Linus Torvalds 已提交
161 162
		goto out;
	}
163 164 165 166 167 168 169 170
	ret = security_netif_sid(dev->name, &new->nsec.sid);
	if (ret != 0)
		goto out;
	new->nsec.ifindex = ifindex;
	ret = sel_netif_insert(new);
	if (ret != 0)
		goto out;
	*sid = new->nsec.sid;
L
Linus Torvalds 已提交
171 172

out:
173 174
	spin_unlock_bh(&sel_netif_lock);
	dev_put(dev);
175 176 177 178 179
	if (unlikely(ret)) {
		printk(KERN_WARNING
		       "SELinux: failure in sel_netif_sid_slow(),"
		       " unable to determine network interface label (%d)\n",
		       ifindex);
180
		kfree(new);
181
	}
L
Linus Torvalds 已提交
182 183 184
	return ret;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198
/**
 * sel_netif_sid - Lookup the SID of a network interface
 * @ifindex: the network interface
 * @sid: interface SID
 *
 * Description:
 * This function determines the SID of a network interface using the fastest
 * method possible.  First the interface table is queried, but if an entry
 * can't be found then the policy is queried and the result is added to the
 * table to speedup future queries.  Returns zero on success, negative values
 * on failure.
 *
 */
int sel_netif_sid(int ifindex, u32 *sid)
L
Linus Torvalds 已提交
199 200 201 202
{
	struct sel_netif *netif;

	rcu_read_lock();
203 204 205
	netif = sel_netif_find(ifindex);
	if (likely(netif != NULL)) {
		*sid = netif->nsec.sid;
L
Linus Torvalds 已提交
206
		rcu_read_unlock();
207
		return 0;
L
Linus Torvalds 已提交
208 209
	}
	rcu_read_unlock();
210 211

	return sel_netif_sid_slow(ifindex, sid);
L
Linus Torvalds 已提交
212 213
}

214 215 216 217 218 219 220 221 222 223
/**
 * sel_netif_kill - Remove an entry from the network interface table
 * @ifindex: the network interface
 *
 * Description:
 * This function removes the entry matching @ifindex from the network interface
 * table if it exists.
 *
 */
static void sel_netif_kill(int ifindex)
L
Linus Torvalds 已提交
224 225 226
{
	struct sel_netif *netif;

227
	rcu_read_lock();
L
Linus Torvalds 已提交
228
	spin_lock_bh(&sel_netif_lock);
229
	netif = sel_netif_find(ifindex);
L
Linus Torvalds 已提交
230 231 232
	if (netif)
		sel_netif_destroy(netif);
	spin_unlock_bh(&sel_netif_lock);
233
	rcu_read_unlock();
L
Linus Torvalds 已提交
234 235
}

236 237 238 239 240 241 242
/**
 * sel_netif_flush - Flush the entire network interface table
 *
 * Description:
 * Remove all entries from the network interface table.
 *
 */
L
Linus Torvalds 已提交
243 244 245
static void sel_netif_flush(void)
{
	int idx;
246
	struct sel_netif *netif;
L
Linus Torvalds 已提交
247 248

	spin_lock_bh(&sel_netif_lock);
249
	for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
L
Linus Torvalds 已提交
250 251 252 253 254 255
		list_for_each_entry(netif, &sel_netif_hash[idx], list)
			sel_netif_destroy(netif);
	spin_unlock_bh(&sel_netif_lock);
}

static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
256
				  u16 class, u32 perms, u32 *retained)
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265
{
	if (event == AVC_CALLBACK_RESET) {
		sel_netif_flush();
		synchronize_net();
	}
	return 0;
}

static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
266
					     unsigned long event, void *ptr)
L
Linus Torvalds 已提交
267 268 269
{
	struct net_device *dev = ptr;

270
	if (dev_net(dev) != &init_net)
271 272
		return NOTIFY_DONE;

L
Linus Torvalds 已提交
273
	if (event == NETDEV_DOWN)
274
		sel_netif_kill(dev->ifindex);
L
Linus Torvalds 已提交
275 276 277 278 279 280 281 282 283 284

	return NOTIFY_DONE;
}

static struct notifier_block sel_netif_netdev_notifier = {
	.notifier_call = sel_netif_netdev_notifier_handler,
};

static __init int sel_netif_init(void)
{
285
	int i, err;
286

L
Linus Torvalds 已提交
287
	if (!selinux_enabled)
288
		return 0;
L
Linus Torvalds 已提交
289 290 291 292 293

	for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
		INIT_LIST_HEAD(&sel_netif_hash[i]);

	register_netdevice_notifier(&sel_netif_netdev_notifier);
294

L
Linus Torvalds 已提交
295
	err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
296
			       SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304
	if (err)
		panic("avc_add_callback() failed, error %d\n", err);

	return err;
}

__initcall(sel_netif_init);