netif.c 6.8 KB

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