netport.c 5.9 KB

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