netnode.c 7.5 KB

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