netnode.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Network node table
  4. *
  5. * SELinux must keep a mapping of network nodes to labels/SIDs. This
  6. * mapping is maintained as part of the normal policy but a fast cache is
  7. * needed to reduce the lookup overhead since most of these queries happen on
  8. * a per-packet basis.
  9. *
  10. * Author: Paul Moore <paul@paul-moore.com>
  11. *
  12. * This code is heavily based on the "netif" concept originally developed by
  13. * James Morris <jmorris@redhat.com>
  14. * (see security/selinux/netif.c for more information)
  15. */
  16. /*
  17. * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
  18. */
  19. #include <linux/types.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/list.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/in.h>
  25. #include <linux/in6.h>
  26. #include <linux/ip.h>
  27. #include <linux/ipv6.h>
  28. #include <net/ip.h>
  29. #include <net/ipv6.h>
  30. #include "initcalls.h"
  31. #include "netnode.h"
  32. #include "objsec.h"
  33. #define SEL_NETNODE_HASH_SIZE 256
  34. #define SEL_NETNODE_HASH_BKT_LIMIT 16
  35. struct sel_netnode_bkt {
  36. unsigned int size;
  37. struct list_head list;
  38. };
  39. struct sel_netnode {
  40. struct netnode_security_struct nsec;
  41. struct list_head list;
  42. struct rcu_head rcu;
  43. };
  44. /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
  45. * for this is that I suspect most users will not make heavy use of both
  46. * address families at the same time so one table will usually end up wasted,
  47. * if this becomes a problem we can always add a hash table for each address
  48. * family later */
  49. static DEFINE_SPINLOCK(sel_netnode_lock);
  50. static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];
  51. /**
  52. * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table
  53. * @addr: IPv4 address
  54. *
  55. * Description:
  56. * This is the IPv4 hashing function for the node interface table, it returns
  57. * the bucket number for the given IP address.
  58. *
  59. */
  60. static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)
  61. {
  62. /* at some point we should determine if the mismatch in byte order
  63. * affects the hash function dramatically */
  64. return (addr & (SEL_NETNODE_HASH_SIZE - 1));
  65. }
  66. /**
  67. * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table
  68. * @addr: IPv6 address
  69. *
  70. * Description:
  71. * This is the IPv6 hashing function for the node interface table, it returns
  72. * the bucket number for the given IP address.
  73. *
  74. */
  75. static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
  76. {
  77. /* just hash the least significant 32 bits to keep things fast (they
  78. * are the most likely to be different anyway), we can revisit this
  79. * later if needed */
  80. return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
  81. }
  82. /**
  83. * sel_netnode_find - Search for a node record
  84. * @addr: IP address
  85. * @family: address family
  86. *
  87. * Description:
  88. * Search the network node table and return the record matching @addr. If an
  89. * entry can not be found in the table return NULL.
  90. *
  91. */
  92. static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
  93. {
  94. unsigned int idx;
  95. struct sel_netnode *node;
  96. switch (family) {
  97. case PF_INET:
  98. idx = sel_netnode_hashfn_ipv4(*(const __be32 *)addr);
  99. break;
  100. case PF_INET6:
  101. idx = sel_netnode_hashfn_ipv6(addr);
  102. break;
  103. default:
  104. BUG();
  105. return NULL;
  106. }
  107. list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
  108. if (node->nsec.family == family)
  109. switch (family) {
  110. case PF_INET:
  111. if (node->nsec.addr.ipv4 == *(const __be32 *)addr)
  112. return node;
  113. break;
  114. case PF_INET6:
  115. if (ipv6_addr_equal(&node->nsec.addr.ipv6,
  116. addr))
  117. return node;
  118. break;
  119. }
  120. return NULL;
  121. }
  122. /**
  123. * sel_netnode_insert - Insert a new node into the table
  124. * @node: the new node record
  125. *
  126. * Description:
  127. * Add a new node record to the network address hash table.
  128. *
  129. */
  130. static void sel_netnode_insert(struct sel_netnode *node)
  131. {
  132. unsigned int idx;
  133. switch (node->nsec.family) {
  134. case PF_INET:
  135. idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
  136. break;
  137. case PF_INET6:
  138. idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
  139. break;
  140. default:
  141. BUG();
  142. return;
  143. }
  144. /* we need to impose a limit on the growth of the hash table so check
  145. * this bucket to make sure it is within the specified bounds */
  146. list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
  147. if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
  148. struct sel_netnode *tail;
  149. tail = list_entry(
  150. rcu_dereference_protected(
  151. list_tail_rcu(&sel_netnode_hash[idx].list),
  152. lockdep_is_held(&sel_netnode_lock)),
  153. struct sel_netnode, list);
  154. list_del_rcu(&tail->list);
  155. kfree_rcu(tail, rcu);
  156. } else
  157. sel_netnode_hash[idx].size++;
  158. }
  159. /**
  160. * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
  161. * @addr: the IP address
  162. * @family: the address family
  163. * @sid: node SID
  164. *
  165. * Description:
  166. * This function determines the SID of a network address by querying the
  167. * security policy. The result is added to the network address table to
  168. * speedup future queries. Returns zero on success, negative values on
  169. * failure.
  170. *
  171. */
  172. static int sel_netnode_sid_slow(const void *addr, u16 family, u32 *sid)
  173. {
  174. int ret;
  175. struct sel_netnode *node;
  176. struct sel_netnode *new;
  177. spin_lock_bh(&sel_netnode_lock);
  178. node = sel_netnode_find(addr, family);
  179. if (node != NULL) {
  180. *sid = node->nsec.sid;
  181. spin_unlock_bh(&sel_netnode_lock);
  182. return 0;
  183. }
  184. /* If this memory allocation fails still return 0. The SID
  185. * is valid, it just won't be added to the cache.
  186. */
  187. new = kmalloc_obj(*new, GFP_ATOMIC);
  188. switch (family) {
  189. case PF_INET:
  190. ret = security_node_sid(PF_INET,
  191. addr, sizeof(struct in_addr), sid);
  192. if (new)
  193. new->nsec.addr.ipv4 = *(const __be32 *)addr;
  194. break;
  195. case PF_INET6:
  196. ret = security_node_sid(PF_INET6,
  197. addr, sizeof(struct in6_addr), sid);
  198. if (new)
  199. new->nsec.addr.ipv6 = *(const struct in6_addr *)addr;
  200. break;
  201. default:
  202. BUG();
  203. ret = -EINVAL;
  204. }
  205. if (ret == 0 && new) {
  206. new->nsec.family = family;
  207. new->nsec.sid = *sid;
  208. sel_netnode_insert(new);
  209. } else
  210. kfree(new);
  211. spin_unlock_bh(&sel_netnode_lock);
  212. if (unlikely(ret))
  213. pr_warn("SELinux: failure in %s(), unable to determine network node label\n",
  214. __func__);
  215. return ret;
  216. }
  217. /**
  218. * sel_netnode_sid - Lookup the SID of a network address
  219. * @addr: the IP address
  220. * @family: the address family
  221. * @sid: node SID
  222. *
  223. * Description:
  224. * This function determines the SID of a network address using the fastest
  225. * method possible. First the address table is queried, but if an entry
  226. * can't be found then the policy is queried and the result is added to the
  227. * table to speedup future queries. Returns zero on success, negative values
  228. * on failure.
  229. *
  230. */
  231. int sel_netnode_sid(const void *addr, u16 family, u32 *sid)
  232. {
  233. struct sel_netnode *node;
  234. rcu_read_lock();
  235. node = sel_netnode_find(addr, family);
  236. if (likely(node != NULL)) {
  237. *sid = node->nsec.sid;
  238. rcu_read_unlock();
  239. return 0;
  240. }
  241. rcu_read_unlock();
  242. return sel_netnode_sid_slow(addr, family, sid);
  243. }
  244. /**
  245. * sel_netnode_flush - Flush the entire network address table
  246. *
  247. * Description:
  248. * Remove all entries from the network address table.
  249. *
  250. */
  251. void sel_netnode_flush(void)
  252. {
  253. unsigned int idx;
  254. struct sel_netnode *node, *node_tmp;
  255. spin_lock_bh(&sel_netnode_lock);
  256. for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
  257. list_for_each_entry_safe(node, node_tmp,
  258. &sel_netnode_hash[idx].list, list) {
  259. list_del_rcu(&node->list);
  260. kfree_rcu(node, rcu);
  261. }
  262. sel_netnode_hash[idx].size = 0;
  263. }
  264. spin_unlock_bh(&sel_netnode_lock);
  265. }
  266. int __init sel_netnode_init(void)
  267. {
  268. int iter;
  269. if (!selinux_enabled_boot)
  270. return 0;
  271. for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
  272. INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
  273. sel_netnode_hash[iter].size = 0;
  274. }
  275. return 0;
  276. }