netnode.c 8.7 KB

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