netnode.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.moore@hp.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_free - Frees a node entry
  63. * @p: the entry's RCU field
  64. *
  65. * Description:
  66. * This function is designed to be used as a callback to the call_rcu()
  67. * function so that memory allocated to a hash table node entry can be
  68. * released safely.
  69. *
  70. */
  71. static void sel_netnode_free(struct rcu_head *p)
  72. {
  73. struct sel_netnode *node = container_of(p, struct sel_netnode, rcu);
  74. kfree(node);
  75. }
  76. /**
  77. * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table
  78. * @addr: IPv4 address
  79. *
  80. * Description:
  81. * This is the IPv4 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_ipv4(__be32 addr)
  86. {
  87. /* at some point we should determine if the mismatch in byte order
  88. * affects the hash function dramatically */
  89. return (addr & (SEL_NETNODE_HASH_SIZE - 1));
  90. }
  91. /**
  92. * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table
  93. * @addr: IPv6 address
  94. *
  95. * Description:
  96. * This is the IPv6 hashing function for the node interface table, it returns
  97. * the bucket number for the given IP address.
  98. *
  99. */
  100. static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
  101. {
  102. /* just hash the least significant 32 bits to keep things fast (they
  103. * are the most likely to be different anyway), we can revisit this
  104. * later if needed */
  105. return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
  106. }
  107. /**
  108. * sel_netnode_find - Search for a node record
  109. * @addr: IP address
  110. * @family: address family
  111. *
  112. * Description:
  113. * Search the network node table and return the record matching @addr. If an
  114. * entry can not be found in the table return NULL.
  115. *
  116. */
  117. static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
  118. {
  119. unsigned int idx;
  120. struct sel_netnode *node;
  121. switch (family) {
  122. case PF_INET:
  123. idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr);
  124. break;
  125. case PF_INET6:
  126. idx = sel_netnode_hashfn_ipv6(addr);
  127. break;
  128. default:
  129. BUG();
  130. return NULL;
  131. }
  132. list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
  133. if (node->nsec.family == family)
  134. switch (family) {
  135. case PF_INET:
  136. if (node->nsec.addr.ipv4 == *(__be32 *)addr)
  137. return node;
  138. break;
  139. case PF_INET6:
  140. if (ipv6_addr_equal(&node->nsec.addr.ipv6,
  141. addr))
  142. return node;
  143. break;
  144. }
  145. return NULL;
  146. }
  147. /**
  148. * sel_netnode_insert - Insert a new node into the table
  149. * @node: the new node record
  150. *
  151. * Description:
  152. * Add a new node record to the network address hash table.
  153. *
  154. */
  155. static void sel_netnode_insert(struct sel_netnode *node)
  156. {
  157. unsigned int idx;
  158. switch (node->nsec.family) {
  159. case PF_INET:
  160. idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
  161. break;
  162. case PF_INET6:
  163. idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
  164. break;
  165. default:
  166. BUG();
  167. }
  168. /* we need to impose a limit on the growth of the hash table so check
  169. * this bucket to make sure it is within the specified bounds */
  170. list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
  171. if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
  172. struct sel_netnode *tail;
  173. tail = list_entry(
  174. rcu_dereference(sel_netnode_hash[idx].list.prev),
  175. struct sel_netnode, list);
  176. list_del_rcu(&tail->list);
  177. call_rcu(&tail->rcu, sel_netnode_free);
  178. } else
  179. sel_netnode_hash[idx].size++;
  180. }
  181. /**
  182. * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
  183. * @addr: the IP address
  184. * @family: the address family
  185. * @sid: node SID
  186. *
  187. * Description:
  188. * This function determines the SID of a network address by quering the
  189. * security policy. The result is added to the network address table to
  190. * speedup future queries. Returns zero on success, negative values on
  191. * failure.
  192. *
  193. */
  194. static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
  195. {
  196. int ret = -ENOMEM;
  197. struct sel_netnode *node;
  198. struct sel_netnode *new = NULL;
  199. spin_lock_bh(&sel_netnode_lock);
  200. node = sel_netnode_find(addr, family);
  201. if (node != NULL) {
  202. *sid = node->nsec.sid;
  203. spin_unlock_bh(&sel_netnode_lock);
  204. return 0;
  205. }
  206. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  207. if (new == NULL)
  208. goto out;
  209. switch (family) {
  210. case PF_INET:
  211. ret = security_node_sid(PF_INET,
  212. addr, sizeof(struct in_addr), sid);
  213. new->nsec.addr.ipv4 = *(__be32 *)addr;
  214. break;
  215. case PF_INET6:
  216. ret = security_node_sid(PF_INET6,
  217. addr, sizeof(struct in6_addr), sid);
  218. ipv6_addr_copy(&new->nsec.addr.ipv6, addr);
  219. break;
  220. default:
  221. BUG();
  222. }
  223. if (ret != 0)
  224. goto out;
  225. new->nsec.family = family;
  226. new->nsec.sid = *sid;
  227. sel_netnode_insert(new);
  228. out:
  229. spin_unlock_bh(&sel_netnode_lock);
  230. if (unlikely(ret)) {
  231. printk(KERN_WARNING
  232. "SELinux: failure in sel_netnode_sid_slow(),"
  233. " unable to determine network node label\n");
  234. kfree(new);
  235. }
  236. return ret;
  237. }
  238. /**
  239. * sel_netnode_sid - Lookup the SID of a network address
  240. * @addr: the IP address
  241. * @family: the address family
  242. * @sid: node SID
  243. *
  244. * Description:
  245. * This function determines the SID of a network address using the fastest
  246. * method possible. First the address table is queried, but if an entry
  247. * can't be found then the policy is queried and the result is added to the
  248. * table to speedup future queries. Returns zero on success, negative values
  249. * on failure.
  250. *
  251. */
  252. int sel_netnode_sid(void *addr, u16 family, u32 *sid)
  253. {
  254. struct sel_netnode *node;
  255. rcu_read_lock();
  256. node = sel_netnode_find(addr, family);
  257. if (node != NULL) {
  258. *sid = node->nsec.sid;
  259. rcu_read_unlock();
  260. return 0;
  261. }
  262. rcu_read_unlock();
  263. return sel_netnode_sid_slow(addr, family, sid);
  264. }
  265. /**
  266. * sel_netnode_flush - Flush the entire network address table
  267. *
  268. * Description:
  269. * Remove all entries from the network address table.
  270. *
  271. */
  272. static void sel_netnode_flush(void)
  273. {
  274. unsigned int idx;
  275. struct sel_netnode *node, *node_tmp;
  276. spin_lock_bh(&sel_netnode_lock);
  277. for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
  278. list_for_each_entry_safe(node, node_tmp,
  279. &sel_netnode_hash[idx].list, list) {
  280. list_del_rcu(&node->list);
  281. call_rcu(&node->rcu, sel_netnode_free);
  282. }
  283. sel_netnode_hash[idx].size = 0;
  284. }
  285. spin_unlock_bh(&sel_netnode_lock);
  286. }
  287. static int sel_netnode_avc_callback(u32 event, u32 ssid, u32 tsid,
  288. u16 class, u32 perms, u32 *retained)
  289. {
  290. if (event == AVC_CALLBACK_RESET) {
  291. sel_netnode_flush();
  292. synchronize_net();
  293. }
  294. return 0;
  295. }
  296. static __init int sel_netnode_init(void)
  297. {
  298. int iter;
  299. int ret;
  300. if (!selinux_enabled)
  301. return 0;
  302. for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
  303. INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
  304. sel_netnode_hash[iter].size = 0;
  305. }
  306. ret = avc_add_callback(sel_netnode_avc_callback, AVC_CALLBACK_RESET,
  307. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  308. if (ret != 0)
  309. panic("avc_add_callback() failed, error %d\n", ret);
  310. return ret;
  311. }
  312. __initcall(sel_netnode_init);