blob: 1ab03efe74947a33d368ab3b21b335c3ca07324d [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static DEFINE_SPINLOCK(sel_netif_lock);
40static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
41
Paul Mooree8bfdb92008-01-29 08:38:08 -050042/**
43 * sel_netif_hashfn - Hashing function for the interface table
Paul Moorecbe0d6e2014-09-10 17:09:57 -040044 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -050045 * @ifindex: the network interface
46 *
47 * Description:
48 * This is the hashing function for the network interface table, it returns the
49 * bucket number for the given interface.
50 *
51 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -040052static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Paul Moorecbe0d6e2014-09-10 17:09:57 -040054 return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Paul Mooree8bfdb92008-01-29 08:38:08 -050057/**
58 * sel_netif_find - Search for an interface record
Paul Moorecbe0d6e2014-09-10 17:09:57 -040059 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -050060 * @ifindex: the network interface
61 *
62 * Description:
63 * Search the network interface table and return the record matching @ifindex.
64 * If an entry can not be found in the table return NULL.
65 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -040067static inline struct sel_netif *sel_netif_find(const struct net *ns,
68 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Paul Moorecbe0d6e2014-09-10 17:09:57 -040070 int idx = sel_netif_hashfn(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -050071 struct sel_netif *netif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Paul Mooree8bfdb92008-01-29 08:38:08 -050073 list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
Paul Moorecbe0d6e2014-09-10 17:09:57 -040074 if (net_eq(netif->nsec.ns, ns) &&
75 netif->nsec.ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return netif;
Paul Mooree8bfdb92008-01-29 08:38:08 -050077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return NULL;
79}
80
Paul Mooree8bfdb92008-01-29 08:38:08 -050081/**
82 * sel_netif_insert - Insert a new interface into the table
83 * @netif: the new interface record
84 *
85 * Description:
86 * Add a new interface record to the network interface hash table. Returns
87 * zero on success, negative values on failure.
88 *
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sel_netif_insert(struct sel_netif *netif)
91{
Paul Mooree8bfdb92008-01-29 08:38:08 -050092 int idx;
Eric Paris338366c2008-04-18 17:38:22 -040093
Paul Mooree8bfdb92008-01-29 08:38:08 -050094 if (sel_netif_total >= SEL_NETIF_HASH_MAX)
95 return -ENOSPC;
Eric Paris338366c2008-04-18 17:38:22 -040096
Paul Moorecbe0d6e2014-09-10 17:09:57 -040097 idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 list_add_rcu(&netif->list, &sel_netif_hash[idx]);
99 sel_netif_total++;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Paul Mooree8bfdb92008-01-29 08:38:08 -0500104/**
Paul Mooree8bfdb92008-01-29 08:38:08 -0500105 * sel_netif_destroy - Remove an interface record from the table
106 * @netif: the existing interface record
107 *
108 * Description:
109 * Remove an existing interface record from the network interface table.
110 *
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void sel_netif_destroy(struct sel_netif *netif)
113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 list_del_rcu(&netif->list);
115 sel_netif_total--;
Lai Jiangshan690273f2011-03-18 12:03:19 +0800116 kfree_rcu(netif, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Paul Mooree8bfdb92008-01-29 08:38:08 -0500119/**
120 * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400121 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500122 * @ifindex: the network interface
123 * @sid: interface SID
124 *
125 * Description:
lihao2c3d8df2020-07-07 12:00:04 +0800126 * This function determines the SID of a network interface by querying the
Paul Mooree8bfdb92008-01-29 08:38:08 -0500127 * security policy. The result is added to the network interface table to
128 * speedup future queries. Returns zero on success, negative values on
129 * failure.
130 *
131 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400132static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Paul Moore9b80c362019-08-01 17:55:06 -0400134 int ret = 0;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500135 struct sel_netif *netif;
Paul Moore9b80c362019-08-01 17:55:06 -0400136 struct sel_netif *new;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500137 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Paul Mooree8bfdb92008-01-29 08:38:08 -0500139 /* NOTE: we always use init's network namespace since we don't
140 * currently support containers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400142 dev = dev_get_by_index(ns, ifindex);
Paul Moore71f1cb02008-01-29 08:51:16 -0500143 if (unlikely(dev == NULL)) {
peter enderborg0d3a1152018-06-12 10:09:10 +0200144 pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",
145 __func__, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500146 return -ENOENT;
Paul Moore71f1cb02008-01-29 08:51:16 -0500147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 spin_lock_bh(&sel_netif_lock);
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400150 netif = sel_netif_find(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500151 if (netif != NULL) {
152 *sid = netif->nsec.sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto out;
154 }
Paul Moore9b80c362019-08-01 17:55:06 -0400155
156 ret = security_netif_sid(&selinux_state, dev->name, sid);
157 if (ret != 0)
158 goto out;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500159 new = kzalloc(sizeof(*new), GFP_ATOMIC);
Paul Moore9b80c362019-08-01 17:55:06 -0400160 if (new) {
161 new->nsec.ns = ns;
162 new->nsec.ifindex = ifindex;
163 new->nsec.sid = *sid;
164 if (sel_netif_insert(new))
165 kfree(new);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168out:
Paul Mooree8bfdb92008-01-29 08:38:08 -0500169 spin_unlock_bh(&sel_netif_lock);
170 dev_put(dev);
Paul Moore9b80c362019-08-01 17:55:06 -0400171 if (unlikely(ret))
peter enderborg0d3a1152018-06-12 10:09:10 +0200172 pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",
173 __func__, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return ret;
175}
176
Paul Mooree8bfdb92008-01-29 08:38:08 -0500177/**
178 * sel_netif_sid - Lookup the SID of a network interface
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400179 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500180 * @ifindex: the network interface
181 * @sid: interface SID
182 *
183 * Description:
184 * This function determines the SID of a network interface using the fastest
185 * method possible. First the interface table is queried, but if an entry
186 * can't be found then the policy is queried and the result is added to the
187 * table to speedup future queries. Returns zero on success, negative values
188 * on failure.
189 *
190 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400191int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct sel_netif *netif;
194
195 rcu_read_lock();
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400196 netif = sel_netif_find(ns, ifindex);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500197 if (likely(netif != NULL)) {
198 *sid = netif->nsec.sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 rcu_read_unlock();
Paul Mooree8bfdb92008-01-29 08:38:08 -0500200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 rcu_read_unlock();
Paul Mooree8bfdb92008-01-29 08:38:08 -0500203
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400204 return sel_netif_sid_slow(ns, ifindex, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Paul Mooree8bfdb92008-01-29 08:38:08 -0500207/**
208 * sel_netif_kill - Remove an entry from the network interface table
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400209 * @ns: the network namespace
Paul Mooree8bfdb92008-01-29 08:38:08 -0500210 * @ifindex: the network interface
211 *
212 * Description:
213 * This function removes the entry matching @ifindex from the network interface
214 * table if it exists.
215 *
216 */
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400217static void sel_netif_kill(const struct net *ns, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct sel_netif *netif;
220
Paul E. McKenney61844252008-04-21 18:12:33 -0700221 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_lock_bh(&sel_netif_lock);
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400223 netif = sel_netif_find(ns, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (netif)
225 sel_netif_destroy(netif);
226 spin_unlock_bh(&sel_netif_lock);
Paul E. McKenney61844252008-04-21 18:12:33 -0700227 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Paul Mooree8bfdb92008-01-29 08:38:08 -0500230/**
231 * sel_netif_flush - Flush the entire network interface table
232 *
233 * Description:
234 * Remove all entries from the network interface table.
235 *
236 */
Paul Moore615e51f2014-06-26 14:33:56 -0400237void sel_netif_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int idx;
Paul Mooree8bfdb92008-01-29 08:38:08 -0500240 struct sel_netif *netif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 spin_lock_bh(&sel_netif_lock);
Paul Mooree8bfdb92008-01-29 08:38:08 -0500243 for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 list_for_each_entry(netif, &sel_netif_hash[idx], list)
245 sel_netif_destroy(netif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock_bh(&sel_netif_lock);
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
Eric Paris338366c2008-04-18 17:38:22 -0400250 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Jiri Pirko351638e2013-05-28 01:30:21 +0000252 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (event == NETDEV_DOWN)
Paul Moorecbe0d6e2014-09-10 17:09:57 -0400255 sel_netif_kill(dev_net(dev), dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return NOTIFY_DONE;
258}
259
260static struct notifier_block sel_netif_netdev_notifier = {
261 .notifier_call = sel_netif_netdev_notifier_handler,
262};
263
264static __init int sel_netif_init(void)
265{
Paul Moore942ba362014-08-07 20:55:30 -0400266 int i;
Eric Paris338366c2008-04-18 17:38:22 -0400267
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500268 if (!selinux_enabled_boot)
Paul Mooree8bfdb92008-01-29 08:38:08 -0500269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
272 INIT_LIST_HEAD(&sel_netif_hash[i]);
273
274 register_netdevice_notifier(&sel_netif_netdev_notifier);
Eric Paris338366c2008-04-18 17:38:22 -0400275
Paul Moore942ba362014-08-07 20:55:30 -0400276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279__initcall(sel_netif_init);
280