blob: 86813b46fad5f54c41d88f016b6f7af2064cc152 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Paul Mooree8bfdb92008-01-29 08:38:08 -050011 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14#include <linux/init.h>
15#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020023#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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
Eric Paris338366c2008-04-18 17:38:22 -040032struct sel_netif {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 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
Paul Mooree8bfdb92008-01-29 08:38:08 -050043/**
44 * sel_netif_hashfn - Hashing function for the interface table
Paul Moorecbe0d6e2014-09-10 17:09:57 -040045 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -050046 * @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 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -040053static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Paul Moorecbe0d6e2014-09-10 17:09:57 -040055 return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Paul Mooree8bfdb92008-01-29 08:38:08 -050058/**
59 * sel_netif_find - Search for an interface record
Paul Moorecbe0d6e2014-09-10 17:09:57 -040060 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -050061 * @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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -040068static inline struct sel_netif *sel_netif_find(const struct net *ns,
69 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Paul Moorecbe0d6e2014-09-10 17:09:57 -040071 int idx = sel_netif_hashfn(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -050072 struct sel_netif *netif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Mooree8bfdb92008-01-29 08:38:08 -050074 list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
Paul Moorecbe0d6e2014-09-10 17:09:57 -040075 if (net_eq(netif->nsec.ns, ns) &&
76 netif->nsec.ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return netif;
Paul Mooree8bfdb92008-01-29 08:38:08 -050078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return NULL;
80}
81
Paul Mooree8bfdb92008-01-29 08:38:08 -050082/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int sel_netif_insert(struct sel_netif *netif)
92{
Paul Mooree8bfdb92008-01-29 08:38:08 -050093 int idx;
Eric Paris338366c2008-04-18 17:38:22 -040094
Paul Mooree8bfdb92008-01-29 08:38:08 -050095 if (sel_netif_total >= SEL_NETIF_HASH_MAX)
96 return -ENOSPC;
Eric Paris338366c2008-04-18 17:38:22 -040097
Paul Moorecbe0d6e2014-09-10 17:09:57 -040098 idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 list_add_rcu(&netif->list, &sel_netif_hash[idx]);
100 sel_netif_total++;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500101
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Paul Mooree8bfdb92008-01-29 08:38:08 -0500105/**
Paul Mooree8bfdb92008-01-29 08:38:08 -0500106 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void sel_netif_destroy(struct sel_netif *netif)
114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 list_del_rcu(&netif->list);
116 sel_netif_total--;
Lai Jiangshan690273f2011-03-18 12:03:19 +0800117 kfree_rcu(netif, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Paul Mooree8bfdb92008-01-29 08:38:08 -0500120/**
121 * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400122 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500123 * @ifindex: the network interface
124 * @sid: interface SID
125 *
126 * Description:
lihao2c3d8df2020-07-07 12:00:04 +0800127 * This function determines the SID of a network interface by querying the
Paul Mooree8bfdb92008-01-29 08:38:08 -0500128 * 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 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400133static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Paul Moore9b80c362019-08-01 17:55:06 -0400135 int ret = 0;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500136 struct sel_netif *netif;
Paul Moore9b80c362019-08-01 17:55:06 -0400137 struct sel_netif *new;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500138 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Paul Mooree8bfdb92008-01-29 08:38:08 -0500140 /* NOTE: we always use init's network namespace since we don't
141 * currently support containers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400143 dev = dev_get_by_index(ns, ifindex);
Paul Moore71f1cb02008-01-29 08:51:16 -0500144 if (unlikely(dev == NULL)) {
peter enderborg0d3a1152018-06-12 10:09:10 +0200145 pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",
146 __func__, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500147 return -ENOENT;
Paul Moore71f1cb02008-01-29 08:51:16 -0500148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_lock_bh(&sel_netif_lock);
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400151 netif = sel_netif_find(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500152 if (netif != NULL) {
153 *sid = netif->nsec.sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
155 }
Paul Moore9b80c362019-08-01 17:55:06 -0400156
157 ret = security_netif_sid(&selinux_state, dev->name, sid);
158 if (ret != 0)
159 goto out;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500160 new = kzalloc(sizeof(*new), GFP_ATOMIC);
Paul Moore9b80c362019-08-01 17:55:06 -0400161 if (new) {
162 new->nsec.ns = ns;
163 new->nsec.ifindex = ifindex;
164 new->nsec.sid = *sid;
165 if (sel_netif_insert(new))
166 kfree(new);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169out:
Paul Mooree8bfdb92008-01-29 08:38:08 -0500170 spin_unlock_bh(&sel_netif_lock);
171 dev_put(dev);
Paul Moore9b80c362019-08-01 17:55:06 -0400172 if (unlikely(ret))
peter enderborg0d3a1152018-06-12 10:09:10 +0200173 pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",
174 __func__, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return ret;
176}
177
Paul Mooree8bfdb92008-01-29 08:38:08 -0500178/**
179 * sel_netif_sid - Lookup the SID of a network interface
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400180 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500181 * @ifindex: the network interface
182 * @sid: interface SID
183 *
184 * Description:
185 * This function determines the SID of a network interface using the fastest
186 * method possible. First the interface table is queried, but if an entry
187 * can't be found then the policy is queried and the result is added to the
188 * table to speedup future queries. Returns zero on success, negative values
189 * on failure.
190 *
191 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400192int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct sel_netif *netif;
195
196 rcu_read_lock();
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400197 netif = sel_netif_find(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500198 if (likely(netif != NULL)) {
199 *sid = netif->nsec.sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 rcu_read_unlock();
Paul Mooree8bfdb92008-01-29 08:38:08 -0500201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 rcu_read_unlock();
Paul Mooree8bfdb92008-01-29 08:38:08 -0500204
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400205 return sel_netif_sid_slow(ns, ifindex, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Paul Mooree8bfdb92008-01-29 08:38:08 -0500208/**
209 * sel_netif_kill - Remove an entry from the network interface table
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400210 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500211 * @ifindex: the network interface
212 *
213 * Description:
214 * This function removes the entry matching @ifindex from the network interface
215 * table if it exists.
216 *
217 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400218static void sel_netif_kill(const struct net *ns, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct sel_netif *netif;
221
Paul E. McKenney61844252008-04-21 18:12:33 -0700222 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_lock_bh(&sel_netif_lock);
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400224 netif = sel_netif_find(ns, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (netif)
226 sel_netif_destroy(netif);
227 spin_unlock_bh(&sel_netif_lock);
Paul E. McKenney61844252008-04-21 18:12:33 -0700228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Paul Mooree8bfdb92008-01-29 08:38:08 -0500231/**
232 * sel_netif_flush - Flush the entire network interface table
233 *
234 * Description:
235 * Remove all entries from the network interface table.
236 *
237 */
Paul Moore615e51f2014-06-26 14:33:56 -0400238void sel_netif_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int idx;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500241 struct sel_netif *netif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 spin_lock_bh(&sel_netif_lock);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500244 for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 list_for_each_entry(netif, &sel_netif_hash[idx], list)
246 sel_netif_destroy(netif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 spin_unlock_bh(&sel_netif_lock);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
Eric Paris338366c2008-04-18 17:38:22 -0400251 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Jiri Pirko351638e2013-05-28 01:30:21 +0000253 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (event == NETDEV_DOWN)
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400256 sel_netif_kill(dev_net(dev), dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return NOTIFY_DONE;
259}
260
261static struct notifier_block sel_netif_netdev_notifier = {
262 .notifier_call = sel_netif_netdev_notifier_handler,
263};
264
265static __init int sel_netif_init(void)
266{
Paul Moore942ba362014-08-07 20:55:30 -0400267 int i;
Eric Paris338366c2008-04-18 17:38:22 -0400268
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500269 if (!selinux_enabled_boot)
Paul Mooree8bfdb92008-01-29 08:38:08 -0500270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
273 INIT_LIST_HEAD(&sel_netif_hash[i]);
274
275 register_netdevice_notifier(&sel_netif_netdev_notifier);
Eric Paris338366c2008-04-18 17:38:22 -0400276
Paul Moore942ba362014-08-07 20:55:30 -0400277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280__initcall(sel_netif_init);
281