netif.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * @ifindex: the network interface
  44. *
  45. * Description:
  46. * This is the hashing function for the network interface table, it returns the
  47. * bucket number for the given interface.
  48. *
  49. */
  50. static inline u32 sel_netif_hashfn(int ifindex)
  51. {
  52. return (ifindex & (SEL_NETIF_HASH_SIZE - 1));
  53. }
  54. /**
  55. * sel_netif_find - Search for an interface record
  56. * @ifindex: the network interface
  57. *
  58. * Description:
  59. * Search the network interface table and return the record matching @ifindex.
  60. * If an entry can not be found in the table return NULL.
  61. *
  62. */
  63. static inline struct sel_netif *sel_netif_find(int ifindex)
  64. {
  65. int idx = sel_netif_hashfn(ifindex);
  66. struct sel_netif *netif;
  67. list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
  68. /* all of the devices should normally fit in the hash, so we
  69. * optimize for that case */
  70. if (likely(netif->nsec.ifindex == ifindex))
  71. return netif;
  72. return NULL;
  73. }
  74. /**
  75. * sel_netif_insert - Insert a new interface into the table
  76. * @netif: the new interface record
  77. *
  78. * Description:
  79. * Add a new interface record to the network interface hash table. Returns
  80. * zero on success, negative values on failure.
  81. *
  82. */
  83. static int sel_netif_insert(struct sel_netif *netif)
  84. {
  85. int idx;
  86. if (sel_netif_total >= SEL_NETIF_HASH_MAX)
  87. return -ENOSPC;
  88. idx = sel_netif_hashfn(netif->nsec.ifindex);
  89. list_add_rcu(&netif->list, &sel_netif_hash[idx]);
  90. sel_netif_total++;
  91. return 0;
  92. }
  93. /**
  94. * sel_netif_destroy - Remove an interface record from the table
  95. * @netif: the existing interface record
  96. *
  97. * Description:
  98. * Remove an existing interface record from the network interface table.
  99. *
  100. */
  101. static void sel_netif_destroy(struct sel_netif *netif)
  102. {
  103. list_del_rcu(&netif->list);
  104. sel_netif_total--;
  105. kfree_rcu(netif, rcu_head);
  106. }
  107. /**
  108. * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
  109. * @ifindex: the network interface
  110. * @sid: interface SID
  111. *
  112. * Description:
  113. * This function determines the SID of a network interface by quering 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(int ifindex, u32 *sid)
  120. {
  121. int ret;
  122. struct sel_netif *netif;
  123. struct sel_netif *new = NULL;
  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(&init_net, ifindex);
  128. if (unlikely(dev == NULL)) {
  129. printk(KERN_WARNING
  130. "SELinux: failure in sel_netif_sid_slow(),"
  131. " invalid network interface (%d)\n", ifindex);
  132. return -ENOENT;
  133. }
  134. spin_lock_bh(&sel_netif_lock);
  135. netif = sel_netif_find(ifindex);
  136. if (netif != NULL) {
  137. *sid = netif->nsec.sid;
  138. ret = 0;
  139. goto out;
  140. }
  141. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  142. if (new == NULL) {
  143. ret = -ENOMEM;
  144. goto out;
  145. }
  146. ret = security_netif_sid(dev->name, &new->nsec.sid);
  147. if (ret != 0)
  148. goto out;
  149. new->nsec.ifindex = ifindex;
  150. ret = sel_netif_insert(new);
  151. if (ret != 0)
  152. goto out;
  153. *sid = new->nsec.sid;
  154. out:
  155. spin_unlock_bh(&sel_netif_lock);
  156. dev_put(dev);
  157. if (unlikely(ret)) {
  158. printk(KERN_WARNING
  159. "SELinux: failure in sel_netif_sid_slow(),"
  160. " unable to determine network interface label (%d)\n",
  161. ifindex);
  162. kfree(new);
  163. }
  164. return ret;
  165. }
  166. /**
  167. * sel_netif_sid - Lookup the SID of a network interface
  168. * @ifindex: the network interface
  169. * @sid: interface SID
  170. *
  171. * Description:
  172. * This function determines the SID of a network interface using the fastest
  173. * method possible. First the interface table is queried, but if an entry
  174. * can't be found then the policy is queried and the result is added to the
  175. * table to speedup future queries. Returns zero on success, negative values
  176. * on failure.
  177. *
  178. */
  179. int sel_netif_sid(int ifindex, u32 *sid)
  180. {
  181. struct sel_netif *netif;
  182. rcu_read_lock();
  183. netif = sel_netif_find(ifindex);
  184. if (likely(netif != NULL)) {
  185. *sid = netif->nsec.sid;
  186. rcu_read_unlock();
  187. return 0;
  188. }
  189. rcu_read_unlock();
  190. return sel_netif_sid_slow(ifindex, sid);
  191. }
  192. /**
  193. * sel_netif_kill - Remove an entry from the network interface table
  194. * @ifindex: the network interface
  195. *
  196. * Description:
  197. * This function removes the entry matching @ifindex from the network interface
  198. * table if it exists.
  199. *
  200. */
  201. static void sel_netif_kill(int ifindex)
  202. {
  203. struct sel_netif *netif;
  204. rcu_read_lock();
  205. spin_lock_bh(&sel_netif_lock);
  206. netif = sel_netif_find(ifindex);
  207. if (netif)
  208. sel_netif_destroy(netif);
  209. spin_unlock_bh(&sel_netif_lock);
  210. rcu_read_unlock();
  211. }
  212. /**
  213. * sel_netif_flush - Flush the entire network interface table
  214. *
  215. * Description:
  216. * Remove all entries from the network interface table.
  217. *
  218. */
  219. static void sel_netif_flush(void)
  220. {
  221. int idx;
  222. struct sel_netif *netif;
  223. spin_lock_bh(&sel_netif_lock);
  224. for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
  225. list_for_each_entry(netif, &sel_netif_hash[idx], list)
  226. sel_netif_destroy(netif);
  227. spin_unlock_bh(&sel_netif_lock);
  228. }
  229. static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
  230. u16 class, u32 perms, u32 *retained)
  231. {
  232. if (event == AVC_CALLBACK_RESET) {
  233. sel_netif_flush();
  234. synchronize_net();
  235. }
  236. return 0;
  237. }
  238. static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
  239. unsigned long event, void *ptr)
  240. {
  241. struct net_device *dev = ptr;
  242. if (dev_net(dev) != &init_net)
  243. return NOTIFY_DONE;
  244. if (event == NETDEV_DOWN)
  245. sel_netif_kill(dev->ifindex);
  246. return NOTIFY_DONE;
  247. }
  248. static struct notifier_block sel_netif_netdev_notifier = {
  249. .notifier_call = sel_netif_netdev_notifier_handler,
  250. };
  251. static __init int sel_netif_init(void)
  252. {
  253. int i, err;
  254. #ifdef CONFIG_ALWAYS_ENFORCE
  255. selinux_enabled = 1;
  256. #endif
  257. if (!selinux_enabled)
  258. return 0;
  259. for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
  260. INIT_LIST_HEAD(&sel_netif_hash[i]);
  261. register_netdevice_notifier(&sel_netif_netdev_notifier);
  262. err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
  263. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  264. if (err)
  265. panic("avc_add_callback() failed, error %d\n", err);
  266. return err;
  267. }
  268. __initcall(sel_netif_init);