netif.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. printk(KERN_WARNING
  133. "SELinux: failure in sel_netif_sid_slow(),"
  134. " invalid network interface (%d)\n", ifindex);
  135. return -ENOENT;
  136. }
  137. spin_lock_bh(&sel_netif_lock);
  138. netif = sel_netif_find(ns, ifindex);
  139. if (netif != NULL) {
  140. *sid = netif->nsec.sid;
  141. ret = 0;
  142. goto out;
  143. }
  144. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  145. if (new == NULL) {
  146. ret = -ENOMEM;
  147. goto out;
  148. }
  149. ret = security_netif_sid(dev->name, &new->nsec.sid);
  150. if (ret != 0)
  151. goto out;
  152. new->nsec.ns = ns;
  153. new->nsec.ifindex = ifindex;
  154. ret = sel_netif_insert(new);
  155. if (ret != 0)
  156. goto out;
  157. *sid = new->nsec.sid;
  158. out:
  159. spin_unlock_bh(&sel_netif_lock);
  160. dev_put(dev);
  161. if (unlikely(ret)) {
  162. printk(KERN_WARNING
  163. "SELinux: failure in sel_netif_sid_slow(),"
  164. " unable to determine network interface label (%d)\n",
  165. ifindex);
  166. kfree(new);
  167. }
  168. return ret;
  169. }
  170. /**
  171. * sel_netif_sid - Lookup the SID of a network interface
  172. * @ns: the network namespace
  173. * @ifindex: the network interface
  174. * @sid: interface SID
  175. *
  176. * Description:
  177. * This function determines the SID of a network interface using the fastest
  178. * method possible. First the interface table is queried, but if an entry
  179. * can't be found then the policy is queried and the result is added to the
  180. * table to speedup future queries. Returns zero on success, negative values
  181. * on failure.
  182. *
  183. */
  184. int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
  185. {
  186. struct sel_netif *netif;
  187. rcu_read_lock();
  188. netif = sel_netif_find(ns, ifindex);
  189. if (likely(netif != NULL)) {
  190. *sid = netif->nsec.sid;
  191. rcu_read_unlock();
  192. return 0;
  193. }
  194. rcu_read_unlock();
  195. return sel_netif_sid_slow(ns, ifindex, sid);
  196. }
  197. /**
  198. * sel_netif_kill - Remove an entry from the network interface table
  199. * @ns: the network namespace
  200. * @ifindex: the network interface
  201. *
  202. * Description:
  203. * This function removes the entry matching @ifindex from the network interface
  204. * table if it exists.
  205. *
  206. */
  207. static void sel_netif_kill(const struct net *ns, int ifindex)
  208. {
  209. struct sel_netif *netif;
  210. rcu_read_lock();
  211. spin_lock_bh(&sel_netif_lock);
  212. netif = sel_netif_find(ns, ifindex);
  213. if (netif)
  214. sel_netif_destroy(netif);
  215. spin_unlock_bh(&sel_netif_lock);
  216. rcu_read_unlock();
  217. }
  218. /**
  219. * sel_netif_flush - Flush the entire network interface table
  220. *
  221. * Description:
  222. * Remove all entries from the network interface table.
  223. *
  224. */
  225. void sel_netif_flush(void)
  226. {
  227. int idx;
  228. struct sel_netif *netif;
  229. spin_lock_bh(&sel_netif_lock);
  230. for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
  231. list_for_each_entry(netif, &sel_netif_hash[idx], list)
  232. sel_netif_destroy(netif);
  233. spin_unlock_bh(&sel_netif_lock);
  234. }
  235. static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
  236. unsigned long event, void *ptr)
  237. {
  238. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  239. if (event == NETDEV_DOWN)
  240. sel_netif_kill(dev_net(dev), dev->ifindex);
  241. return NOTIFY_DONE;
  242. }
  243. static struct notifier_block sel_netif_netdev_notifier = {
  244. .notifier_call = sel_netif_netdev_notifier_handler,
  245. };
  246. static __init int sel_netif_init(void)
  247. {
  248. int i;
  249. if (!selinux_enabled)
  250. return 0;
  251. for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
  252. INIT_LIST_HEAD(&sel_netif_hash[i]);
  253. register_netdevice_notifier(&sel_netif_netdev_notifier);
  254. return 0;
  255. }
  256. __initcall(sel_netif_init);