netport.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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@paul-moore.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_hashfn - Hashing function for the port table
  62. * @pnum: port number
  63. *
  64. * Description:
  65. * This is the hashing function for the port table, it returns the bucket
  66. * number for the given port.
  67. *
  68. */
  69. static unsigned int sel_netport_hashfn(u16 pnum)
  70. {
  71. return (pnum & (SEL_NETPORT_HASH_SIZE - 1));
  72. }
  73. /**
  74. * sel_netport_find - Search for a port record
  75. * @protocol: protocol
  76. * @port: pnum
  77. *
  78. * Description:
  79. * Search the network port table and return the matching record. If an entry
  80. * can not be found in the table return NULL.
  81. *
  82. */
  83. static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)
  84. {
  85. unsigned int idx;
  86. struct sel_netport *port;
  87. idx = sel_netport_hashfn(pnum);
  88. list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)
  89. if (port->psec.port == pnum && port->psec.protocol == protocol)
  90. return port;
  91. return NULL;
  92. }
  93. /**
  94. * sel_netport_insert - Insert a new port into the table
  95. * @port: the new port record
  96. *
  97. * Description:
  98. * Add a new port record to the network address hash table.
  99. *
  100. */
  101. static void sel_netport_insert(struct sel_netport *port)
  102. {
  103. unsigned int idx;
  104. /* we need to impose a limit on the growth of the hash table so check
  105. * this bucket to make sure it is within the specified bounds */
  106. idx = sel_netport_hashfn(port->psec.port);
  107. list_add_rcu(&port->list, &sel_netport_hash[idx].list);
  108. if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {
  109. struct sel_netport *tail;
  110. tail = list_entry(
  111. rcu_dereference_protected(
  112. sel_netport_hash[idx].list.prev,
  113. lockdep_is_held(&sel_netport_lock)),
  114. struct sel_netport, list);
  115. list_del_rcu(&tail->list);
  116. kfree_rcu(tail, rcu);
  117. } else
  118. sel_netport_hash[idx].size++;
  119. }
  120. /**
  121. * sel_netport_sid_slow - Lookup the SID of a network address using the policy
  122. * @protocol: protocol
  123. * @pnum: port
  124. * @sid: port SID
  125. *
  126. * Description:
  127. * This function determines the SID of a network port by quering the security
  128. * policy. The result is added to the network port table to speedup future
  129. * queries. Returns zero on success, negative values on failure.
  130. *
  131. */
  132. static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)
  133. {
  134. int ret = -ENOMEM;
  135. struct sel_netport *port;
  136. struct sel_netport *new = NULL;
  137. spin_lock_bh(&sel_netport_lock);
  138. port = sel_netport_find(protocol, pnum);
  139. if (port != NULL) {
  140. *sid = port->psec.sid;
  141. spin_unlock_bh(&sel_netport_lock);
  142. return 0;
  143. }
  144. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  145. if (new == NULL)
  146. goto out;
  147. ret = security_port_sid(protocol, pnum, sid);
  148. if (ret != 0)
  149. goto out;
  150. new->psec.port = pnum;
  151. new->psec.protocol = protocol;
  152. new->psec.sid = *sid;
  153. sel_netport_insert(new);
  154. out:
  155. spin_unlock_bh(&sel_netport_lock);
  156. if (unlikely(ret)) {
  157. printk(KERN_WARNING
  158. "SELinux: failure in sel_netport_sid_slow(),"
  159. " unable to determine network port label\n");
  160. kfree(new);
  161. }
  162. return ret;
  163. }
  164. /**
  165. * sel_netport_sid - Lookup the SID of a network port
  166. * @protocol: protocol
  167. * @pnum: port
  168. * @sid: port SID
  169. *
  170. * Description:
  171. * This function determines the SID of a network port using the fastest method
  172. * possible. First the port table is queried, but if an entry can't be found
  173. * then the policy is queried and the result is added to the table to speedup
  174. * future queries. Returns zero on success, negative values on failure.
  175. *
  176. */
  177. int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)
  178. {
  179. struct sel_netport *port;
  180. rcu_read_lock();
  181. port = sel_netport_find(protocol, pnum);
  182. if (port != NULL) {
  183. *sid = port->psec.sid;
  184. rcu_read_unlock();
  185. return 0;
  186. }
  187. rcu_read_unlock();
  188. return sel_netport_sid_slow(protocol, pnum, sid);
  189. }
  190. /**
  191. * sel_netport_flush - Flush the entire network port table
  192. *
  193. * Description:
  194. * Remove all entries from the network address table.
  195. *
  196. */
  197. void sel_netport_flush(void)
  198. {
  199. unsigned int idx;
  200. struct sel_netport *port, *port_tmp;
  201. spin_lock_bh(&sel_netport_lock);
  202. for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {
  203. list_for_each_entry_safe(port, port_tmp,
  204. &sel_netport_hash[idx].list, list) {
  205. list_del_rcu(&port->list);
  206. kfree_rcu(port, rcu);
  207. }
  208. sel_netport_hash[idx].size = 0;
  209. }
  210. spin_unlock_bh(&sel_netport_lock);
  211. }
  212. static __init int sel_netport_init(void)
  213. {
  214. int iter;
  215. if (!selinux_enabled)
  216. return 0;
  217. for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {
  218. INIT_LIST_HEAD(&sel_netport_hash[iter].list);
  219. sel_netport_hash[iter].size = 0;
  220. }
  221. return 0;
  222. }
  223. __initcall(sel_netport_init);