netport.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Network port table
  4. *
  5. * SELinux must keep a mapping of network ports to labels/SIDs. This
  6. * mapping is maintained as part of the normal policy but a fast cache is
  7. * needed to reduce the lookup overhead.
  8. *
  9. * Author: Paul Moore <[email protected]>
  10. *
  11. * This code is heavily based on the "netif" concept originally developed by
  12. * James Morris <[email protected]>
  13. * (see security/selinux/netif.c for more information)
  14. */
  15. /*
  16. * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
  17. */
  18. #include <linux/types.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/list.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/in.h>
  24. #include <linux/in6.h>
  25. #include <linux/ip.h>
  26. #include <linux/ipv6.h>
  27. #include <net/ip.h>
  28. #include <net/ipv6.h>
  29. #include "netport.h"
  30. #include "objsec.h"
  31. #define SEL_NETPORT_HASH_SIZE 256
  32. #define SEL_NETPORT_HASH_BKT_LIMIT 16
  33. struct sel_netport_bkt {
  34. int size;
  35. struct list_head list;
  36. };
  37. struct sel_netport {
  38. struct netport_security_struct psec;
  39. struct list_head list;
  40. struct rcu_head rcu;
  41. };
  42. /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
  43. * for this is that I suspect most users will not make heavy use of both
  44. * address families at the same time so one table will usually end up wasted,
  45. * if this becomes a problem we can always add a hash table for each address
  46. * family later */
  47. static DEFINE_SPINLOCK(sel_netport_lock);
  48. static struct sel_netport_bkt sel_netport_hash[SEL_NETPORT_HASH_SIZE];
  49. /**
  50. * sel_netport_hashfn - Hashing function for the port table
  51. * @pnum: port number
  52. *
  53. * Description:
  54. * This is the hashing function for the port table, it returns the bucket
  55. * number for the given port.
  56. *
  57. */
  58. static unsigned int sel_netport_hashfn(u16 pnum)
  59. {
  60. return (pnum & (SEL_NETPORT_HASH_SIZE - 1));
  61. }
  62. /**
  63. * sel_netport_find - Search for a port record
  64. * @protocol: protocol
  65. * @pnum: port
  66. *
  67. * Description:
  68. * Search the network port table and return the matching record. If an entry
  69. * can not be found in the table return NULL.
  70. *
  71. */
  72. static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)
  73. {
  74. unsigned int idx;
  75. struct sel_netport *port;
  76. idx = sel_netport_hashfn(pnum);
  77. list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)
  78. if (port->psec.port == pnum && port->psec.protocol == protocol)
  79. return port;
  80. return NULL;
  81. }
  82. /**
  83. * sel_netport_insert - Insert a new port into the table
  84. * @port: the new port record
  85. *
  86. * Description:
  87. * Add a new port record to the network address hash table.
  88. *
  89. */
  90. static void sel_netport_insert(struct sel_netport *port)
  91. {
  92. unsigned int idx;
  93. /* we need to impose a limit on the growth of the hash table so check
  94. * this bucket to make sure it is within the specified bounds */
  95. idx = sel_netport_hashfn(port->psec.port);
  96. list_add_rcu(&port->list, &sel_netport_hash[idx].list);
  97. if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {
  98. struct sel_netport *tail;
  99. tail = list_entry(
  100. rcu_dereference_protected(
  101. list_tail_rcu(&sel_netport_hash[idx].list),
  102. lockdep_is_held(&sel_netport_lock)),
  103. struct sel_netport, list);
  104. list_del_rcu(&tail->list);
  105. kfree_rcu(tail, rcu);
  106. } else
  107. sel_netport_hash[idx].size++;
  108. }
  109. /**
  110. * sel_netport_sid_slow - Lookup the SID of a network address using the policy
  111. * @protocol: protocol
  112. * @pnum: port
  113. * @sid: port SID
  114. *
  115. * Description:
  116. * This function determines the SID of a network port by querying the security
  117. * policy. The result is added to the network port table to speedup future
  118. * queries. Returns zero on success, negative values on failure.
  119. *
  120. */
  121. static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)
  122. {
  123. int ret;
  124. struct sel_netport *port;
  125. struct sel_netport *new;
  126. spin_lock_bh(&sel_netport_lock);
  127. port = sel_netport_find(protocol, pnum);
  128. if (port != NULL) {
  129. *sid = port->psec.sid;
  130. spin_unlock_bh(&sel_netport_lock);
  131. return 0;
  132. }
  133. ret = security_port_sid(&selinux_state, protocol, pnum, sid);
  134. if (ret != 0)
  135. goto out;
  136. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  137. if (new) {
  138. new->psec.port = pnum;
  139. new->psec.protocol = protocol;
  140. new->psec.sid = *sid;
  141. sel_netport_insert(new);
  142. }
  143. out:
  144. spin_unlock_bh(&sel_netport_lock);
  145. if (unlikely(ret))
  146. pr_warn("SELinux: failure in %s(), unable to determine network port label\n",
  147. __func__);
  148. return ret;
  149. }
  150. /**
  151. * sel_netport_sid - Lookup the SID of a network port
  152. * @protocol: protocol
  153. * @pnum: port
  154. * @sid: port SID
  155. *
  156. * Description:
  157. * This function determines the SID of a network port using the fastest method
  158. * possible. First the port table is queried, but if an entry can't be found
  159. * then the policy is queried and the result is added to the table to speedup
  160. * future queries. Returns zero on success, negative values on failure.
  161. *
  162. */
  163. int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)
  164. {
  165. struct sel_netport *port;
  166. rcu_read_lock();
  167. port = sel_netport_find(protocol, pnum);
  168. if (port != NULL) {
  169. *sid = port->psec.sid;
  170. rcu_read_unlock();
  171. return 0;
  172. }
  173. rcu_read_unlock();
  174. return sel_netport_sid_slow(protocol, pnum, sid);
  175. }
  176. /**
  177. * sel_netport_flush - Flush the entire network port table
  178. *
  179. * Description:
  180. * Remove all entries from the network address table.
  181. *
  182. */
  183. void sel_netport_flush(void)
  184. {
  185. unsigned int idx;
  186. struct sel_netport *port, *port_tmp;
  187. spin_lock_bh(&sel_netport_lock);
  188. for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {
  189. list_for_each_entry_safe(port, port_tmp,
  190. &sel_netport_hash[idx].list, list) {
  191. list_del_rcu(&port->list);
  192. kfree_rcu(port, rcu);
  193. }
  194. sel_netport_hash[idx].size = 0;
  195. }
  196. spin_unlock_bh(&sel_netport_lock);
  197. }
  198. static __init int sel_netport_init(void)
  199. {
  200. int iter;
  201. if (!selinux_enabled_boot)
  202. return 0;
  203. for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {
  204. INIT_LIST_HEAD(&sel_netport_hash[iter].list);
  205. sel_netport_hash[iter].size = 0;
  206. }
  207. return 0;
  208. }
  209. __initcall(sel_netport_init);