15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2224dfbd8SPaul Moore /*
3224dfbd8SPaul Moore * Network node table
4224dfbd8SPaul Moore *
5224dfbd8SPaul Moore * SELinux must keep a mapping of network nodes to labels/SIDs. This
6224dfbd8SPaul Moore * mapping is maintained as part of the normal policy but a fast cache is
7224dfbd8SPaul Moore * needed to reduce the lookup overhead since most of these queries happen on
8224dfbd8SPaul Moore * a per-packet basis.
9224dfbd8SPaul Moore *
1082c21bfaSPaul Moore * Author: Paul Moore <paul@paul-moore.com>
11224dfbd8SPaul Moore *
12224dfbd8SPaul Moore * This code is heavily based on the "netif" concept originally developed by
13224dfbd8SPaul Moore * James Morris <jmorris@redhat.com>
14224dfbd8SPaul Moore * (see security/selinux/netif.c for more information)
15224dfbd8SPaul Moore */
16224dfbd8SPaul Moore
17224dfbd8SPaul Moore /*
18224dfbd8SPaul Moore * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
19224dfbd8SPaul Moore */
20224dfbd8SPaul Moore
21224dfbd8SPaul Moore #include <linux/types.h>
22224dfbd8SPaul Moore #include <linux/rcupdate.h>
23224dfbd8SPaul Moore #include <linux/list.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25224dfbd8SPaul Moore #include <linux/spinlock.h>
26224dfbd8SPaul Moore #include <linux/in.h>
27224dfbd8SPaul Moore #include <linux/in6.h>
28224dfbd8SPaul Moore #include <linux/ip.h>
29224dfbd8SPaul Moore #include <linux/ipv6.h>
30224dfbd8SPaul Moore #include <net/ip.h>
31224dfbd8SPaul Moore #include <net/ipv6.h>
32224dfbd8SPaul Moore
33a639e7caSPaul Moore #include "netnode.h"
34224dfbd8SPaul Moore #include "objsec.h"
35224dfbd8SPaul Moore
36224dfbd8SPaul Moore #define SEL_NETNODE_HASH_SIZE 256
37224dfbd8SPaul Moore #define SEL_NETNODE_HASH_BKT_LIMIT 16
38224dfbd8SPaul Moore
39a639e7caSPaul Moore struct sel_netnode_bkt {
40a639e7caSPaul Moore unsigned int size;
41a639e7caSPaul Moore struct list_head list;
42a639e7caSPaul Moore };
43a639e7caSPaul Moore
44224dfbd8SPaul Moore struct sel_netnode {
45224dfbd8SPaul Moore struct netnode_security_struct nsec;
46224dfbd8SPaul Moore
47224dfbd8SPaul Moore struct list_head list;
48224dfbd8SPaul Moore struct rcu_head rcu;
49224dfbd8SPaul Moore };
50224dfbd8SPaul Moore
51224dfbd8SPaul Moore /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
52224dfbd8SPaul Moore * for this is that I suspect most users will not make heavy use of both
53224dfbd8SPaul Moore * address families at the same time so one table will usually end up wasted,
54224dfbd8SPaul Moore * if this becomes a problem we can always add a hash table for each address
55224dfbd8SPaul Moore * family later */
56224dfbd8SPaul Moore
57224dfbd8SPaul Moore static DEFINE_SPINLOCK(sel_netnode_lock);
58a639e7caSPaul Moore static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];
59224dfbd8SPaul Moore
60224dfbd8SPaul Moore /**
61224dfbd8SPaul Moore * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table
62224dfbd8SPaul Moore * @addr: IPv4 address
63224dfbd8SPaul Moore *
64224dfbd8SPaul Moore * Description:
65224dfbd8SPaul Moore * This is the IPv4 hashing function for the node interface table, it returns
66224dfbd8SPaul Moore * the bucket number for the given IP address.
67224dfbd8SPaul Moore *
68224dfbd8SPaul Moore */
sel_netnode_hashfn_ipv4(__be32 addr)69a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)
70224dfbd8SPaul Moore {
71224dfbd8SPaul Moore /* at some point we should determine if the mismatch in byte order
72224dfbd8SPaul Moore * affects the hash function dramatically */
73224dfbd8SPaul Moore return (addr & (SEL_NETNODE_HASH_SIZE - 1));
74224dfbd8SPaul Moore }
75224dfbd8SPaul Moore
76224dfbd8SPaul Moore /**
77224dfbd8SPaul Moore * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table
78224dfbd8SPaul Moore * @addr: IPv6 address
79224dfbd8SPaul Moore *
80224dfbd8SPaul Moore * Description:
81224dfbd8SPaul Moore * This is the IPv6 hashing function for the node interface table, it returns
82224dfbd8SPaul Moore * the bucket number for the given IP address.
83224dfbd8SPaul Moore *
84224dfbd8SPaul Moore */
sel_netnode_hashfn_ipv6(const struct in6_addr * addr)85a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
86224dfbd8SPaul Moore {
87224dfbd8SPaul Moore /* just hash the least significant 32 bits to keep things fast (they
88224dfbd8SPaul Moore * are the most likely to be different anyway), we can revisit this
89224dfbd8SPaul Moore * later if needed */
90224dfbd8SPaul Moore return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
91224dfbd8SPaul Moore }
92224dfbd8SPaul Moore
93224dfbd8SPaul Moore /**
94224dfbd8SPaul Moore * sel_netnode_find - Search for a node record
95224dfbd8SPaul Moore * @addr: IP address
96224dfbd8SPaul Moore * @family: address family
97224dfbd8SPaul Moore *
98224dfbd8SPaul Moore * Description:
99224dfbd8SPaul Moore * Search the network node table and return the record matching @addr. If an
100224dfbd8SPaul Moore * entry can not be found in the table return NULL.
101224dfbd8SPaul Moore *
102224dfbd8SPaul Moore */
sel_netnode_find(const void * addr,u16 family)103224dfbd8SPaul Moore static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
104224dfbd8SPaul Moore {
105a639e7caSPaul Moore unsigned int idx;
106224dfbd8SPaul Moore struct sel_netnode *node;
107224dfbd8SPaul Moore
108224dfbd8SPaul Moore switch (family) {
109224dfbd8SPaul Moore case PF_INET:
11073073d95SChristian Göttsche idx = sel_netnode_hashfn_ipv4(*(const __be32 *)addr);
111224dfbd8SPaul Moore break;
112224dfbd8SPaul Moore case PF_INET6:
113224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv6(addr);
114224dfbd8SPaul Moore break;
115224dfbd8SPaul Moore default:
116224dfbd8SPaul Moore BUG();
117a35c6c83SEric Paris return NULL;
118224dfbd8SPaul Moore }
119224dfbd8SPaul Moore
120a639e7caSPaul Moore list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
121224dfbd8SPaul Moore if (node->nsec.family == family)
122224dfbd8SPaul Moore switch (family) {
123224dfbd8SPaul Moore case PF_INET:
12473073d95SChristian Göttsche if (node->nsec.addr.ipv4 == *(const __be32 *)addr)
125224dfbd8SPaul Moore return node;
126224dfbd8SPaul Moore break;
127224dfbd8SPaul Moore case PF_INET6:
128224dfbd8SPaul Moore if (ipv6_addr_equal(&node->nsec.addr.ipv6,
129224dfbd8SPaul Moore addr))
130224dfbd8SPaul Moore return node;
131224dfbd8SPaul Moore break;
132224dfbd8SPaul Moore }
133224dfbd8SPaul Moore
134224dfbd8SPaul Moore return NULL;
135224dfbd8SPaul Moore }
136224dfbd8SPaul Moore
137224dfbd8SPaul Moore /**
138224dfbd8SPaul Moore * sel_netnode_insert - Insert a new node into the table
139224dfbd8SPaul Moore * @node: the new node record
140224dfbd8SPaul Moore *
141224dfbd8SPaul Moore * Description:
142a639e7caSPaul Moore * Add a new node record to the network address hash table.
143224dfbd8SPaul Moore *
144224dfbd8SPaul Moore */
sel_netnode_insert(struct sel_netnode * node)145a639e7caSPaul Moore static void sel_netnode_insert(struct sel_netnode *node)
146224dfbd8SPaul Moore {
147a639e7caSPaul Moore unsigned int idx;
148224dfbd8SPaul Moore
149224dfbd8SPaul Moore switch (node->nsec.family) {
150224dfbd8SPaul Moore case PF_INET:
151224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
152224dfbd8SPaul Moore break;
153224dfbd8SPaul Moore case PF_INET6:
154224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
155224dfbd8SPaul Moore break;
156224dfbd8SPaul Moore default:
157224dfbd8SPaul Moore BUG();
158b04eea88SPaul Moore return;
159224dfbd8SPaul Moore }
160a639e7caSPaul Moore
161224dfbd8SPaul Moore /* we need to impose a limit on the growth of the hash table so check
162224dfbd8SPaul Moore * this bucket to make sure it is within the specified bounds */
163a639e7caSPaul Moore list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
164a639e7caSPaul Moore if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
165a639e7caSPaul Moore struct sel_netnode *tail;
166a639e7caSPaul Moore tail = list_entry(
1670e326df0SPaul Moore rcu_dereference_protected(
1680e326df0SPaul Moore list_tail_rcu(&sel_netnode_hash[idx].list),
16988a693b5SDave Jones lockdep_is_held(&sel_netnode_lock)),
170a639e7caSPaul Moore struct sel_netnode, list);
171a639e7caSPaul Moore list_del_rcu(&tail->list);
1729801c60eSLai Jiangshan kfree_rcu(tail, rcu);
173a639e7caSPaul Moore } else
174a639e7caSPaul Moore sel_netnode_hash[idx].size++;
175224dfbd8SPaul Moore }
176224dfbd8SPaul Moore
177224dfbd8SPaul Moore /**
178224dfbd8SPaul Moore * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
179224dfbd8SPaul Moore * @addr: the IP address
180224dfbd8SPaul Moore * @family: the address family
181224dfbd8SPaul Moore * @sid: node SID
182224dfbd8SPaul Moore *
183224dfbd8SPaul Moore * Description:
1842c3d8dfeSlihao * This function determines the SID of a network address by querying the
185224dfbd8SPaul Moore * security policy. The result is added to the network address table to
186224dfbd8SPaul Moore * speedup future queries. Returns zero on success, negative values on
187224dfbd8SPaul Moore * failure.
188224dfbd8SPaul Moore *
189224dfbd8SPaul Moore */
sel_netnode_sid_slow(void * addr,u16 family,u32 * sid)190224dfbd8SPaul Moore static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
191224dfbd8SPaul Moore {
1929b80c363SPaul Moore int ret;
193224dfbd8SPaul Moore struct sel_netnode *node;
1949b80c363SPaul Moore struct sel_netnode *new;
195224dfbd8SPaul Moore
196224dfbd8SPaul Moore spin_lock_bh(&sel_netnode_lock);
197224dfbd8SPaul Moore node = sel_netnode_find(addr, family);
198224dfbd8SPaul Moore if (node != NULL) {
199224dfbd8SPaul Moore *sid = node->nsec.sid;
200a639e7caSPaul Moore spin_unlock_bh(&sel_netnode_lock);
201a639e7caSPaul Moore return 0;
202224dfbd8SPaul Moore }
2039b80c363SPaul Moore
204224dfbd8SPaul Moore new = kzalloc(sizeof(*new), GFP_ATOMIC);
205224dfbd8SPaul Moore switch (family) {
206224dfbd8SPaul Moore case PF_INET:
207*e67b7985SStephen Smalley ret = security_node_sid(PF_INET,
208a639e7caSPaul Moore addr, sizeof(struct in_addr), sid);
2099b80c363SPaul Moore if (new)
210224dfbd8SPaul Moore new->nsec.addr.ipv4 = *(__be32 *)addr;
211224dfbd8SPaul Moore break;
212224dfbd8SPaul Moore case PF_INET6:
213*e67b7985SStephen Smalley ret = security_node_sid(PF_INET6,
214a639e7caSPaul Moore addr, sizeof(struct in6_addr), sid);
2159b80c363SPaul Moore if (new)
2164e3fd7a0SAlexey Dobriyan new->nsec.addr.ipv6 = *(struct in6_addr *)addr;
217224dfbd8SPaul Moore break;
218224dfbd8SPaul Moore default:
219224dfbd8SPaul Moore BUG();
220b04eea88SPaul Moore ret = -EINVAL;
221224dfbd8SPaul Moore }
2229b80c363SPaul Moore if (ret == 0 && new) {
223224dfbd8SPaul Moore new->nsec.family = family;
224a639e7caSPaul Moore new->nsec.sid = *sid;
225a639e7caSPaul Moore sel_netnode_insert(new);
2269b80c363SPaul Moore } else
2279b80c363SPaul Moore kfree(new);
228224dfbd8SPaul Moore
229224dfbd8SPaul Moore spin_unlock_bh(&sel_netnode_lock);
2309b80c363SPaul Moore if (unlikely(ret))
23167b0b4e4Speter enderborg pr_warn("SELinux: failure in %s(), unable to determine network node label\n",
23267b0b4e4Speter enderborg __func__);
233224dfbd8SPaul Moore return ret;
234224dfbd8SPaul Moore }
235224dfbd8SPaul Moore
236224dfbd8SPaul Moore /**
237224dfbd8SPaul Moore * sel_netnode_sid - Lookup the SID of a network address
238224dfbd8SPaul Moore * @addr: the IP address
239224dfbd8SPaul Moore * @family: the address family
240224dfbd8SPaul Moore * @sid: node SID
241224dfbd8SPaul Moore *
242224dfbd8SPaul Moore * Description:
243224dfbd8SPaul Moore * This function determines the SID of a network address using the fastest
244224dfbd8SPaul Moore * method possible. First the address table is queried, but if an entry
245224dfbd8SPaul Moore * can't be found then the policy is queried and the result is added to the
246224dfbd8SPaul Moore * table to speedup future queries. Returns zero on success, negative values
247224dfbd8SPaul Moore * on failure.
248224dfbd8SPaul Moore *
249224dfbd8SPaul Moore */
sel_netnode_sid(void * addr,u16 family,u32 * sid)250224dfbd8SPaul Moore int sel_netnode_sid(void *addr, u16 family, u32 *sid)
251224dfbd8SPaul Moore {
252224dfbd8SPaul Moore struct sel_netnode *node;
253224dfbd8SPaul Moore
254224dfbd8SPaul Moore rcu_read_lock();
255224dfbd8SPaul Moore node = sel_netnode_find(addr, family);
256224dfbd8SPaul Moore if (node != NULL) {
257224dfbd8SPaul Moore *sid = node->nsec.sid;
258224dfbd8SPaul Moore rcu_read_unlock();
259224dfbd8SPaul Moore return 0;
260224dfbd8SPaul Moore }
261224dfbd8SPaul Moore rcu_read_unlock();
262224dfbd8SPaul Moore
263224dfbd8SPaul Moore return sel_netnode_sid_slow(addr, family, sid);
264224dfbd8SPaul Moore }
265224dfbd8SPaul Moore
266224dfbd8SPaul Moore /**
267224dfbd8SPaul Moore * sel_netnode_flush - Flush the entire network address table
268224dfbd8SPaul Moore *
269224dfbd8SPaul Moore * Description:
270224dfbd8SPaul Moore * Remove all entries from the network address table.
271224dfbd8SPaul Moore *
272224dfbd8SPaul Moore */
sel_netnode_flush(void)273615e51fdSPaul Moore void sel_netnode_flush(void)
274224dfbd8SPaul Moore {
275a639e7caSPaul Moore unsigned int idx;
276a639e7caSPaul Moore struct sel_netnode *node, *node_tmp;
277224dfbd8SPaul Moore
278224dfbd8SPaul Moore spin_lock_bh(&sel_netnode_lock);
279a639e7caSPaul Moore for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
280a639e7caSPaul Moore list_for_each_entry_safe(node, node_tmp,
281a639e7caSPaul Moore &sel_netnode_hash[idx].list, list) {
282a639e7caSPaul Moore list_del_rcu(&node->list);
2839801c60eSLai Jiangshan kfree_rcu(node, rcu);
284a639e7caSPaul Moore }
285a639e7caSPaul Moore sel_netnode_hash[idx].size = 0;
286a639e7caSPaul Moore }
287224dfbd8SPaul Moore spin_unlock_bh(&sel_netnode_lock);
288224dfbd8SPaul Moore }
289224dfbd8SPaul Moore
sel_netnode_init(void)290224dfbd8SPaul Moore static __init int sel_netnode_init(void)
291224dfbd8SPaul Moore {
292224dfbd8SPaul Moore int iter;
293224dfbd8SPaul Moore
2946c5a682eSStephen Smalley if (!selinux_enabled_boot)
295224dfbd8SPaul Moore return 0;
296224dfbd8SPaul Moore
297a639e7caSPaul Moore for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
298a639e7caSPaul Moore INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
299a639e7caSPaul Moore sel_netnode_hash[iter].size = 0;
300a639e7caSPaul Moore }
301224dfbd8SPaul Moore
302942ba364SPaul Moore return 0;
303224dfbd8SPaul Moore }
304224dfbd8SPaul Moore
305224dfbd8SPaul Moore __initcall(sel_netnode_init);
306