netif.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Network interface table.
  4. *
  5. * Network interfaces (devices) do not have a security field, so we
  6. * maintain a table associating each interface with a SID.
  7. *
  8. * Author: James Morris <[email protected]>
  9. *
  10. * Copyright (C) 2003 Red Hat, Inc., James Morris <[email protected]>
  11. * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  12. * Paul Moore <[email protected]>
  13. */
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/stddef.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/notifier.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/rcupdate.h>
  23. #include <net/net_namespace.h>
  24. #include "security.h"
  25. #include "objsec.h"
  26. #include "netif.h"
  27. #define SEL_NETIF_HASH_SIZE 64
  28. #define SEL_NETIF_HASH_MAX 1024
  29. struct sel_netif {
  30. struct list_head list;
  31. struct netif_security_struct nsec;
  32. struct rcu_head rcu_head;
  33. };
  34. static u32 sel_netif_total;
  35. static DEFINE_SPINLOCK(sel_netif_lock);
  36. static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
  37. /**
  38. * sel_netif_hashfn - Hashing function for the interface table
  39. * @ns: the network namespace
  40. * @ifindex: the network interface
  41. *
  42. * Description:
  43. * This is the hashing function for the network interface table, it returns the
  44. * bucket number for the given interface.
  45. *
  46. */
  47. static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)
  48. {
  49. return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));
  50. }
  51. /**
  52. * sel_netif_find - Search for an interface record
  53. * @ns: the network namespace
  54. * @ifindex: the network interface
  55. *
  56. * Description:
  57. * Search the network interface table and return the record matching @ifindex.
  58. * If an entry can not be found in the table return NULL.
  59. *
  60. */
  61. static inline struct sel_netif *sel_netif_find(const struct net *ns,
  62. int ifindex)
  63. {
  64. int idx = sel_netif_hashfn(ns, ifindex);
  65. struct sel_netif *netif;
  66. list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
  67. if (net_eq(netif->nsec.ns, ns) &&
  68. netif->nsec.ifindex == ifindex)
  69. return netif;
  70. return NULL;
  71. }
  72. /**
  73. * sel_netif_insert - Insert a new interface into the table
  74. * @netif: the new interface record
  75. *
  76. * Description:
  77. * Add a new interface record to the network interface hash table. Returns
  78. * zero on success, negative values on failure.
  79. *
  80. */
  81. static int sel_netif_insert(struct sel_netif *netif)
  82. {
  83. int idx;
  84. if (sel_netif_total >= SEL_NETIF_HASH_MAX)
  85. return -ENOSPC;
  86. idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);
  87. list_add_rcu(&netif->list, &sel_netif_hash[idx]);
  88. sel_netif_total++;
  89. return 0;
  90. }
  91. /**
  92. * sel_netif_destroy - Remove an interface record from the table
  93. * @netif: the existing interface record
  94. *
  95. * Description:
  96. * Remove an existing interface record from the network interface table.
  97. *
  98. */
  99. static void sel_netif_destroy(struct sel_netif *netif)
  100. {
  101. list_del_rcu(&netif->list);
  102. sel_netif_total--;
  103. kfree_rcu(netif, rcu_head);
  104. }
  105. /**
  106. * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
  107. * @ns: the network namespace
  108. * @ifindex: the network interface
  109. * @sid: interface SID
  110. *
  111. * Description:
  112. * This function determines the SID of a network interface by querying the
  113. * security policy. The result is added to the network interface table to
  114. * speedup future queries. Returns zero on success, negative values on
  115. * failure.
  116. *
  117. */
  118. static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)
  119. {
  120. int ret = 0;
  121. struct sel_netif *netif;
  122. struct sel_netif *new;
  123. struct net_device *dev;
  124. /* NOTE: we always use init's network namespace since we don't
  125. * currently support containers */
  126. dev = dev_get_by_index(ns, ifindex);
  127. if (unlikely(dev == NULL)) {
  128. pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",
  129. __func__, ifindex);
  130. return -ENOENT;
  131. }
  132. spin_lock_bh(&sel_netif_lock);
  133. netif = sel_netif_find(ns, ifindex);
  134. if (netif != NULL) {
  135. *sid = netif->nsec.sid;
  136. goto out;
  137. }
  138. ret = security_netif_sid(&selinux_state, dev->name, sid);
  139. if (ret != 0)
  140. goto out;
  141. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  142. if (new) {
  143. new->nsec.ns = ns;
  144. new->nsec.ifindex = ifindex;
  145. new->nsec.sid = *sid;
  146. if (sel_netif_insert(new))
  147. kfree(new);
  148. }
  149. out:
  150. spin_unlock_bh(&sel_netif_lock);
  151. dev_put(dev);
  152. if (unlikely(ret))
  153. pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",
  154. __func__, ifindex);
  155. return ret;
  156. }
  157. /**
  158. * sel_netif_sid - Lookup the SID of a network interface
  159. * @ns: the network namespace
  160. * @ifindex: the network interface
  161. * @sid: interface SID
  162. *
  163. * Description:
  164. * This function determines the SID of a network interface using the fastest
  165. * method possible. First the interface table is queried, but if an entry
  166. * can't be found then the policy is queried and the result is added to the
  167. * table to speedup future queries. Returns zero on success, negative values
  168. * on failure.
  169. *
  170. */
  171. int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
  172. {
  173. struct sel_netif *netif;
  174. rcu_read_lock();
  175. netif = sel_netif_find(ns, ifindex);
  176. if (likely(netif != NULL)) {
  177. *sid = netif->nsec.sid;
  178. rcu_read_unlock();
  179. return 0;
  180. }
  181. rcu_read_unlock();
  182. return sel_netif_sid_slow(ns, ifindex, sid);
  183. }
  184. /**
  185. * sel_netif_kill - Remove an entry from the network interface table
  186. * @ns: the network namespace
  187. * @ifindex: the network interface
  188. *
  189. * Description:
  190. * This function removes the entry matching @ifindex from the network interface
  191. * table if it exists.
  192. *
  193. */
  194. static void sel_netif_kill(const struct net *ns, int ifindex)
  195. {
  196. struct sel_netif *netif;
  197. rcu_read_lock();
  198. spin_lock_bh(&sel_netif_lock);
  199. netif = sel_netif_find(ns, ifindex);
  200. if (netif)
  201. sel_netif_destroy(netif);
  202. spin_unlock_bh(&sel_netif_lock);
  203. rcu_read_unlock();
  204. }
  205. /**
  206. * sel_netif_flush - Flush the entire network interface table
  207. *
  208. * Description:
  209. * Remove all entries from the network interface table.
  210. *
  211. */
  212. void sel_netif_flush(void)
  213. {
  214. int idx;
  215. struct sel_netif *netif;
  216. spin_lock_bh(&sel_netif_lock);
  217. for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
  218. list_for_each_entry(netif, &sel_netif_hash[idx], list)
  219. sel_netif_destroy(netif);
  220. spin_unlock_bh(&sel_netif_lock);
  221. }
  222. static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
  223. unsigned long event, void *ptr)
  224. {
  225. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  226. if (event == NETDEV_DOWN)
  227. sel_netif_kill(dev_net(dev), dev->ifindex);
  228. return NOTIFY_DONE;
  229. }
  230. static struct notifier_block sel_netif_netdev_notifier = {
  231. .notifier_call = sel_netif_netdev_notifier_handler,
  232. };
  233. static __init int sel_netif_init(void)
  234. {
  235. int i;
  236. if (!selinux_enabled_boot)
  237. return 0;
  238. for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
  239. INIT_LIST_HEAD(&sel_netif_hash[i]);
  240. register_netdevice_notifier(&sel_netif_netdev_notifier);
  241. return 0;
  242. }
  243. __initcall(sel_netif_init);