netif.c 7.0 KB

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