netport.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Network port table
  3. *
  4. * SELinux must keep a mapping of network ports 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.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. * This code is heavily based on the "netif" concept originally developed by
  11. * James Morris <jmorris@redhat.com>
  12. * (see security/selinux/netif.c for more information)
  13. *
  14. */
  15. /*
  16. * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of version 2 of the GNU General Public License as
  20. * published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. */
  28. #include <linux/types.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/list.h>
  31. #include <linux/slab.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 "netport.h"
  40. #include "objsec.h"
  41. #define SEL_NETPORT_HASH_SIZE 256
  42. #define SEL_NETPORT_HASH_BKT_LIMIT 16
  43. struct sel_netport_bkt {
  44. int size;
  45. struct list_head list;
  46. };
  47. struct sel_netport {
  48. struct netport_security_struct psec;
  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_netport_list);
  58. static DEFINE_SPINLOCK(sel_netport_lock);
  59. static struct sel_netport_bkt sel_netport_hash[SEL_NETPORT_HASH_SIZE];
  60. /**
  61. * sel_netport_free - Frees a port 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 port entry can be
  67. * released safely.
  68. *
  69. */
  70. static void sel_netport_free(struct rcu_head *p)
  71. {
  72. struct sel_netport *port = container_of(p, struct sel_netport, rcu);
  73. kfree(port);
  74. }
  75. /**
  76. * sel_netport_hashfn - Hashing function for the port table
  77. * @pnum: port number
  78. *
  79. * Description:
  80. * This is the hashing function for the port table, it returns the bucket
  81. * number for the given port.
  82. *
  83. */
  84. static unsigned int sel_netport_hashfn(u16 pnum)
  85. {
  86. return (pnum & (SEL_NETPORT_HASH_SIZE - 1));
  87. }
  88. /**
  89. * sel_netport_find - Search for a port record
  90. * @protocol: protocol
  91. * @port: pnum
  92. *
  93. * Description:
  94. * Search the network port table and return the matching record. If an entry
  95. * can not be found in the table return NULL.
  96. *
  97. */
  98. static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)
  99. {
  100. unsigned int idx;
  101. struct sel_netport *port;
  102. idx = sel_netport_hashfn(pnum);
  103. list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)
  104. if (port->psec.port == pnum && port->psec.protocol == protocol)
  105. return port;
  106. return NULL;
  107. }
  108. /**
  109. * sel_netport_insert - Insert a new port into the table
  110. * @port: the new port record
  111. *
  112. * Description:
  113. * Add a new port record to the network address hash table.
  114. *
  115. */
  116. static void sel_netport_insert(struct sel_netport *port)
  117. {
  118. unsigned int idx;
  119. /* we need to impose a limit on the growth of the hash table so check
  120. * this bucket to make sure it is within the specified bounds */
  121. idx = sel_netport_hashfn(port->psec.port);
  122. list_add_rcu(&port->list, &sel_netport_hash[idx].list);
  123. if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {
  124. struct sel_netport *tail;
  125. tail = list_entry(
  126. rcu_dereference_protected(
  127. sel_netport_hash[idx].list.prev,
  128. lockdep_is_held(&sel_netport_lock)),
  129. struct sel_netport, list);
  130. list_del_rcu(&tail->list);
  131. call_rcu(&tail->rcu, sel_netport_free);
  132. } else
  133. sel_netport_hash[idx].size++;
  134. }
  135. /**
  136. * sel_netport_sid_slow - Lookup the SID of a network address using the policy
  137. * @protocol: protocol
  138. * @pnum: port
  139. * @sid: port SID
  140. *
  141. * Description:
  142. * This function determines the SID of a network port by quering the security
  143. * policy. The result is added to the network port table to speedup future
  144. * queries. Returns zero on success, negative values on failure.
  145. *
  146. */
  147. static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)
  148. {
  149. int ret = -ENOMEM;
  150. struct sel_netport *port;
  151. struct sel_netport *new = NULL;
  152. spin_lock_bh(&sel_netport_lock);
  153. port = sel_netport_find(protocol, pnum);
  154. if (port != NULL) {
  155. *sid = port->psec.sid;
  156. spin_unlock_bh(&sel_netport_lock);
  157. return 0;
  158. }
  159. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  160. if (new == NULL)
  161. goto out;
  162. ret = security_port_sid(protocol, pnum, sid);
  163. if (ret != 0)
  164. goto out;
  165. new->psec.port = pnum;
  166. new->psec.protocol = protocol;
  167. new->psec.sid = *sid;
  168. sel_netport_insert(new);
  169. out:
  170. spin_unlock_bh(&sel_netport_lock);
  171. if (unlikely(ret)) {
  172. printk(KERN_WARNING
  173. "SELinux: failure in sel_netport_sid_slow(),"
  174. " unable to determine network port label\n");
  175. kfree(new);
  176. }
  177. return ret;
  178. }
  179. /**
  180. * sel_netport_sid - Lookup the SID of a network port
  181. * @protocol: protocol
  182. * @pnum: port
  183. * @sid: port SID
  184. *
  185. * Description:
  186. * This function determines the SID of a network port using the fastest method
  187. * possible. First the port table is queried, but if an entry can't be found
  188. * then the policy is queried and the result is added to the table to speedup
  189. * future queries. Returns zero on success, negative values on failure.
  190. *
  191. */
  192. int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)
  193. {
  194. struct sel_netport *port;
  195. rcu_read_lock();
  196. port = sel_netport_find(protocol, pnum);
  197. if (port != NULL) {
  198. *sid = port->psec.sid;
  199. rcu_read_unlock();
  200. return 0;
  201. }
  202. rcu_read_unlock();
  203. return sel_netport_sid_slow(protocol, pnum, sid);
  204. }
  205. /**
  206. * sel_netport_flush - Flush the entire network port table
  207. *
  208. * Description:
  209. * Remove all entries from the network address table.
  210. *
  211. */
  212. static void sel_netport_flush(void)
  213. {
  214. unsigned int idx;
  215. struct sel_netport *port, *port_tmp;
  216. spin_lock_bh(&sel_netport_lock);
  217. for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {
  218. list_for_each_entry_safe(port, port_tmp,
  219. &sel_netport_hash[idx].list, list) {
  220. list_del_rcu(&port->list);
  221. call_rcu(&port->rcu, sel_netport_free);
  222. }
  223. sel_netport_hash[idx].size = 0;
  224. }
  225. spin_unlock_bh(&sel_netport_lock);
  226. }
  227. static int sel_netport_avc_callback(u32 event, u32 ssid, u32 tsid,
  228. u16 class, u32 perms, u32 *retained)
  229. {
  230. if (event == AVC_CALLBACK_RESET) {
  231. sel_netport_flush();
  232. synchronize_net();
  233. }
  234. return 0;
  235. }
  236. static __init int sel_netport_init(void)
  237. {
  238. int iter;
  239. int ret;
  240. if (!selinux_enabled)
  241. return 0;
  242. for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {
  243. INIT_LIST_HEAD(&sel_netport_hash[iter].list);
  244. sel_netport_hash[iter].size = 0;
  245. }
  246. ret = avc_add_callback(sel_netport_avc_callback, AVC_CALLBACK_RESET,
  247. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  248. if (ret != 0)
  249. panic("avc_add_callback() failed, error %d\n", ret);
  250. return ret;
  251. }
  252. __initcall(sel_netport_init);