Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-block.git] / security / selinux / netif.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * Network interface table.
4 *
5 * Network interfaces (devices) do not have a security field, so we
6 * maintain a table associating each interface with a SID.
7 *
8 * Author: James Morris <jmorris@redhat.com>
9 *
10 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
e8bfdb9d 11 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
82c21bfa 12 * Paul Moore <paul@paul-moore.com>
1da177e4
LT
13 */
14#include <linux/init.h>
15#include <linux/types.h>
5a0e3ad6 16#include <linux/slab.h>
1da177e4
LT
17#include <linux/stddef.h>
18#include <linux/kernel.h>
19#include <linux/list.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/rcupdate.h>
e9dc8653 23#include <net/net_namespace.h>
1da177e4
LT
24
25#include "security.h"
26#include "objsec.h"
27#include "netif.h"
28
29#define SEL_NETIF_HASH_SIZE 64
30#define SEL_NETIF_HASH_MAX 1024
31
338366cb 32struct sel_netif {
1da177e4
LT
33 struct list_head list;
34 struct netif_security_struct nsec;
35 struct rcu_head rcu_head;
36};
37
38static u32 sel_netif_total;
39static LIST_HEAD(sel_netif_list);
40static DEFINE_SPINLOCK(sel_netif_lock);
41static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
42
e8bfdb9d
PM
43/**
44 * sel_netif_hashfn - Hashing function for the interface table
cbe0d6e8 45 * @ns: the network namespace
e8bfdb9d
PM
46 * @ifindex: the network interface
47 *
48 * Description:
49 * This is the hashing function for the network interface table, it returns the
50 * bucket number for the given interface.
51 *
52 */
cbe0d6e8 53static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)
1da177e4 54{
cbe0d6e8 55 return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));
1da177e4
LT
56}
57
e8bfdb9d
PM
58/**
59 * sel_netif_find - Search for an interface record
cbe0d6e8 60 * @ns: the network namespace
e8bfdb9d
PM
61 * @ifindex: the network interface
62 *
63 * Description:
64 * Search the network interface table and return the record matching @ifindex.
65 * If an entry can not be found in the table return NULL.
66 *
1da177e4 67 */
cbe0d6e8
PM
68static inline struct sel_netif *sel_netif_find(const struct net *ns,
69 int ifindex)
1da177e4 70{
cbe0d6e8 71 int idx = sel_netif_hashfn(ns, ifindex);
e8bfdb9d 72 struct sel_netif *netif;
1da177e4 73
e8bfdb9d 74 list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
cbe0d6e8
PM
75 if (net_eq(netif->nsec.ns, ns) &&
76 netif->nsec.ifindex == ifindex)
1da177e4 77 return netif;
e8bfdb9d 78
1da177e4
LT
79 return NULL;
80}
81
e8bfdb9d
PM
82/**
83 * sel_netif_insert - Insert a new interface into the table
84 * @netif: the new interface record
85 *
86 * Description:
87 * Add a new interface record to the network interface hash table. Returns
88 * zero on success, negative values on failure.
89 *
90 */
1da177e4
LT
91static int sel_netif_insert(struct sel_netif *netif)
92{
e8bfdb9d 93 int idx;
338366cb 94
e8bfdb9d
PM
95 if (sel_netif_total >= SEL_NETIF_HASH_MAX)
96 return -ENOSPC;
338366cb 97
cbe0d6e8 98 idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);
1da177e4
LT
99 list_add_rcu(&netif->list, &sel_netif_hash[idx]);
100 sel_netif_total++;
e8bfdb9d
PM
101
102 return 0;
1da177e4
LT
103}
104
e8bfdb9d
PM
105/**
106 * sel_netif_destroy - Remove an interface record from the table
107 * @netif: the existing interface record
108 *
109 * Description:
110 * Remove an existing interface record from the network interface table.
111 *
112 */
1da177e4
LT
113static void sel_netif_destroy(struct sel_netif *netif)
114{
1da177e4
LT
115 list_del_rcu(&netif->list);
116 sel_netif_total--;
690273fc 117 kfree_rcu(netif, rcu_head);
1da177e4
LT
118}
119
e8bfdb9d
PM
120/**
121 * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
cbe0d6e8 122 * @ns: the network namespace
e8bfdb9d
PM
123 * @ifindex: the network interface
124 * @sid: interface SID
125 *
126 * Description:
127 * This function determines the SID of a network interface by quering the
128 * security policy. The result is added to the network interface table to
129 * speedup future queries. Returns zero on success, negative values on
130 * failure.
131 *
132 */
cbe0d6e8 133static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)
1da177e4
LT
134{
135 int ret;
e8bfdb9d
PM
136 struct sel_netif *netif;
137 struct sel_netif *new = NULL;
138 struct net_device *dev;
1da177e4 139
e8bfdb9d
PM
140 /* NOTE: we always use init's network namespace since we don't
141 * currently support containers */
1da177e4 142
cbe0d6e8 143 dev = dev_get_by_index(ns, ifindex);
71f1cb05 144 if (unlikely(dev == NULL)) {
0d3a1154 145 pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",
146 __func__, ifindex);
e8bfdb9d 147 return -ENOENT;
71f1cb05 148 }
1da177e4 149
1da177e4 150 spin_lock_bh(&sel_netif_lock);
cbe0d6e8 151 netif = sel_netif_find(ns, ifindex);
e8bfdb9d
PM
152 if (netif != NULL) {
153 *sid = netif->nsec.sid;
154 ret = 0;
1da177e4
LT
155 goto out;
156 }
e8bfdb9d
PM
157 new = kzalloc(sizeof(*new), GFP_ATOMIC);
158 if (new == NULL) {
159 ret = -ENOMEM;
1da177e4
LT
160 goto out;
161 }
aa8e712c 162 ret = security_netif_sid(&selinux_state, dev->name, &new->nsec.sid);
e8bfdb9d
PM
163 if (ret != 0)
164 goto out;
cbe0d6e8 165 new->nsec.ns = ns;
e8bfdb9d
PM
166 new->nsec.ifindex = ifindex;
167 ret = sel_netif_insert(new);
168 if (ret != 0)
169 goto out;
170 *sid = new->nsec.sid;
1da177e4 171
1da177e4 172out:
e8bfdb9d
PM
173 spin_unlock_bh(&sel_netif_lock);
174 dev_put(dev);
71f1cb05 175 if (unlikely(ret)) {
0d3a1154 176 pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",
177 __func__, ifindex);
e8bfdb9d 178 kfree(new);
71f1cb05 179 }
1da177e4
LT
180 return ret;
181}
182
e8bfdb9d
PM
183/**
184 * sel_netif_sid - Lookup the SID of a network interface
cbe0d6e8 185 * @ns: the network namespace
e8bfdb9d
PM
186 * @ifindex: the network interface
187 * @sid: interface SID
188 *
189 * Description:
190 * This function determines the SID of a network interface using the fastest
191 * method possible. First the interface table is queried, but if an entry
192 * can't be found then the policy is queried and the result is added to the
193 * table to speedup future queries. Returns zero on success, negative values
194 * on failure.
195 *
196 */
cbe0d6e8 197int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
1da177e4 198{
1da177e4
LT
199 struct sel_netif *netif;
200
201 rcu_read_lock();
cbe0d6e8 202 netif = sel_netif_find(ns, ifindex);
e8bfdb9d
PM
203 if (likely(netif != NULL)) {
204 *sid = netif->nsec.sid;
1da177e4 205 rcu_read_unlock();
e8bfdb9d 206 return 0;
1da177e4 207 }
1da177e4 208 rcu_read_unlock();
e8bfdb9d 209
cbe0d6e8 210 return sel_netif_sid_slow(ns, ifindex, sid);
1da177e4
LT
211}
212
e8bfdb9d
PM
213/**
214 * sel_netif_kill - Remove an entry from the network interface table
cbe0d6e8 215 * @ns: the network namespace
e8bfdb9d
PM
216 * @ifindex: the network interface
217 *
218 * Description:
219 * This function removes the entry matching @ifindex from the network interface
220 * table if it exists.
221 *
222 */
cbe0d6e8 223static void sel_netif_kill(const struct net *ns, int ifindex)
1da177e4
LT
224{
225 struct sel_netif *netif;
226
61844250 227 rcu_read_lock();
1da177e4 228 spin_lock_bh(&sel_netif_lock);
cbe0d6e8 229 netif = sel_netif_find(ns, ifindex);
1da177e4
LT
230 if (netif)
231 sel_netif_destroy(netif);
232 spin_unlock_bh(&sel_netif_lock);
61844250 233 rcu_read_unlock();
1da177e4
LT
234}
235
e8bfdb9d
PM
236/**
237 * sel_netif_flush - Flush the entire network interface table
238 *
239 * Description:
240 * Remove all entries from the network interface table.
241 *
242 */
615e51fd 243void sel_netif_flush(void)
1da177e4
LT
244{
245 int idx;
e8bfdb9d 246 struct sel_netif *netif;
1da177e4
LT
247
248 spin_lock_bh(&sel_netif_lock);
e8bfdb9d 249 for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
1da177e4
LT
250 list_for_each_entry(netif, &sel_netif_hash[idx], list)
251 sel_netif_destroy(netif);
1da177e4
LT
252 spin_unlock_bh(&sel_netif_lock);
253}
254
1da177e4 255static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
338366cb 256 unsigned long event, void *ptr)
1da177e4 257{
351638e7 258 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1da177e4
LT
259
260 if (event == NETDEV_DOWN)
cbe0d6e8 261 sel_netif_kill(dev_net(dev), dev->ifindex);
1da177e4
LT
262
263 return NOTIFY_DONE;
264}
265
266static struct notifier_block sel_netif_netdev_notifier = {
267 .notifier_call = sel_netif_netdev_notifier_handler,
268};
269
270static __init int sel_netif_init(void)
271{
942ba364 272 int i;
338366cb 273
1da177e4 274 if (!selinux_enabled)
e8bfdb9d 275 return 0;
1da177e4
LT
276
277 for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
278 INIT_LIST_HEAD(&sel_netif_hash[i]);
279
280 register_netdevice_notifier(&sel_netif_netdev_notifier);
338366cb 281
942ba364 282 return 0;
1da177e4
LT
283}
284
285__initcall(sel_netif_init);
286