netnode.c 7.9 KB

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