netnode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. }
  153. /* we need to impose a limit on the growth of the hash table so check
  154. * this bucket to make sure it is within the specified bounds */
  155. list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
  156. if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
  157. struct sel_netnode *tail;
  158. tail = list_entry(
  159. rcu_dereference_protected(sel_netnode_hash[idx].list.prev,
  160. lockdep_is_held(&sel_netnode_lock)),
  161. struct sel_netnode, list);
  162. list_del_rcu(&tail->list);
  163. kfree_rcu(tail, rcu);
  164. } else
  165. sel_netnode_hash[idx].size++;
  166. }
  167. /**
  168. * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
  169. * @addr: the IP address
  170. * @family: the address family
  171. * @sid: node SID
  172. *
  173. * Description:
  174. * This function determines the SID of a network address by quering the
  175. * security policy. The result is added to the network address table to
  176. * speedup future queries. Returns zero on success, negative values on
  177. * failure.
  178. *
  179. */
  180. static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
  181. {
  182. int ret = -ENOMEM;
  183. struct sel_netnode *node;
  184. struct sel_netnode *new = NULL;
  185. spin_lock_bh(&sel_netnode_lock);
  186. node = sel_netnode_find(addr, family);
  187. if (node != NULL) {
  188. *sid = node->nsec.sid;
  189. spin_unlock_bh(&sel_netnode_lock);
  190. return 0;
  191. }
  192. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  193. if (new == NULL)
  194. goto out;
  195. switch (family) {
  196. case PF_INET:
  197. ret = security_node_sid(PF_INET,
  198. addr, sizeof(struct in_addr), sid);
  199. new->nsec.addr.ipv4 = *(__be32 *)addr;
  200. break;
  201. case PF_INET6:
  202. ret = security_node_sid(PF_INET6,
  203. addr, sizeof(struct in6_addr), sid);
  204. new->nsec.addr.ipv6 = *(struct in6_addr *)addr;
  205. break;
  206. default:
  207. BUG();
  208. }
  209. if (ret != 0)
  210. goto out;
  211. new->nsec.family = family;
  212. new->nsec.sid = *sid;
  213. sel_netnode_insert(new);
  214. out:
  215. spin_unlock_bh(&sel_netnode_lock);
  216. if (unlikely(ret)) {
  217. printk(KERN_WARNING
  218. "SELinux: failure in sel_netnode_sid_slow(),"
  219. " unable to determine network node label\n");
  220. kfree(new);
  221. }
  222. return ret;
  223. }
  224. /**
  225. * sel_netnode_sid - Lookup the SID of a network address
  226. * @addr: the IP address
  227. * @family: the address family
  228. * @sid: node SID
  229. *
  230. * Description:
  231. * This function determines the SID of a network address using the fastest
  232. * method possible. First the address table is queried, but if an entry
  233. * can't be found then the policy is queried and the result is added to the
  234. * table to speedup future queries. Returns zero on success, negative values
  235. * on failure.
  236. *
  237. */
  238. int sel_netnode_sid(void *addr, u16 family, u32 *sid)
  239. {
  240. struct sel_netnode *node;
  241. rcu_read_lock();
  242. node = sel_netnode_find(addr, family);
  243. if (node != NULL) {
  244. *sid = node->nsec.sid;
  245. rcu_read_unlock();
  246. return 0;
  247. }
  248. rcu_read_unlock();
  249. return sel_netnode_sid_slow(addr, family, sid);
  250. }
  251. /**
  252. * sel_netnode_flush - Flush the entire network address table
  253. *
  254. * Description:
  255. * Remove all entries from the network address table.
  256. *
  257. */
  258. static void sel_netnode_flush(void)
  259. {
  260. unsigned int idx;
  261. struct sel_netnode *node, *node_tmp;
  262. spin_lock_bh(&sel_netnode_lock);
  263. for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
  264. list_for_each_entry_safe(node, node_tmp,
  265. &sel_netnode_hash[idx].list, list) {
  266. list_del_rcu(&node->list);
  267. kfree_rcu(node, rcu);
  268. }
  269. sel_netnode_hash[idx].size = 0;
  270. }
  271. spin_unlock_bh(&sel_netnode_lock);
  272. }
  273. static int sel_netnode_avc_callback(u32 event, u32 ssid, u32 tsid,
  274. u16 class, u32 perms, u32 *retained)
  275. {
  276. if (event == AVC_CALLBACK_RESET) {
  277. sel_netnode_flush();
  278. synchronize_net();
  279. }
  280. return 0;
  281. }
  282. static __init int sel_netnode_init(void)
  283. {
  284. int iter;
  285. int ret;
  286. #ifdef CONFIG_ALWAYS_ENFORCE
  287. selinux_enabled = 1;
  288. #endif
  289. if (!selinux_enabled)
  290. return 0;
  291. for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
  292. INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
  293. sel_netnode_hash[iter].size = 0;
  294. }
  295. ret = avc_add_callback(sel_netnode_avc_callback, AVC_CALLBACK_RESET,
  296. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  297. if (ret != 0)
  298. panic("avc_add_callback() failed, error %d\n", ret);
  299. return ret;
  300. }
  301. __initcall(sel_netnode_init);