netif.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.moore@hp.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/stddef.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/notifier.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/rcupdate.h>
  25. #include <net/net_namespace.h>
  26. #include "security.h"
  27. #include "objsec.h"
  28. #include "netif.h"
  29. #define SEL_NETIF_HASH_SIZE 64
  30. #define SEL_NETIF_HASH_MAX 1024
  31. struct sel_netif {
  32. struct list_head list;
  33. struct netif_security_struct nsec;
  34. struct rcu_head rcu_head;
  35. };
  36. static u32 sel_netif_total;
  37. static LIST_HEAD(sel_netif_list);
  38. static DEFINE_SPINLOCK(sel_netif_lock);
  39. static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
  40. /**
  41. * sel_netif_hashfn - Hashing function for the interface table
  42. * @ifindex: the network interface
  43. *
  44. * Description:
  45. * This is the hashing function for the network interface table, it returns the
  46. * bucket number for the given interface.
  47. *
  48. */
  49. static inline u32 sel_netif_hashfn(int ifindex)
  50. {
  51. return (ifindex & (SEL_NETIF_HASH_SIZE - 1));
  52. }
  53. /**
  54. * sel_netif_find - Search for an interface record
  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(int ifindex)
  63. {
  64. int idx = sel_netif_hashfn(ifindex);
  65. struct sel_netif *netif;
  66. list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
  67. /* all of the devices should normally fit in the hash, so we
  68. * optimize for that case */
  69. if (likely(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.ifindex);
  88. list_add_rcu(&netif->list, &sel_netif_hash[idx]);
  89. sel_netif_total++;
  90. return 0;
  91. }
  92. /**
  93. * sel_netif_free - Frees an interface entry
  94. * @p: the entry's RCU field
  95. *
  96. * Description:
  97. * This function is designed to be used as a callback to the call_rcu()
  98. * function so that memory allocated to a hash table interface entry can be
  99. * released safely.
  100. *
  101. */
  102. static void sel_netif_free(struct rcu_head *p)
  103. {
  104. struct sel_netif *netif = container_of(p, struct sel_netif, rcu_head);
  105. kfree(netif);
  106. }
  107. /**
  108. * sel_netif_destroy - Remove an interface record from the table
  109. * @netif: the existing interface record
  110. *
  111. * Description:
  112. * Remove an existing interface record from the network interface table.
  113. *
  114. */
  115. static void sel_netif_destroy(struct sel_netif *netif)
  116. {
  117. list_del_rcu(&netif->list);
  118. sel_netif_total--;
  119. call_rcu(&netif->rcu_head, sel_netif_free);
  120. }
  121. /**
  122. * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
  123. * @ifindex: the network interface
  124. * @sid: interface SID
  125. *
  126. * Description:
  127. * This function determines the SID of a network interface by quering the
  128. * security policy. The result is added to the network interface table to
  129. * speedup future queries. Returns zero on success, negative values on
  130. * failure.
  131. *
  132. */
  133. static int sel_netif_sid_slow(int ifindex, u32 *sid)
  134. {
  135. int ret;
  136. struct sel_netif *netif;
  137. struct sel_netif *new = NULL;
  138. struct net_device *dev;
  139. /* NOTE: we always use init's network namespace since we don't
  140. * currently support containers */
  141. dev = dev_get_by_index(&init_net, ifindex);
  142. if (unlikely(dev == NULL)) {
  143. printk(KERN_WARNING
  144. "SELinux: failure in sel_netif_sid_slow(),"
  145. " invalid network interface (%d)\n", ifindex);
  146. return -ENOENT;
  147. }
  148. spin_lock_bh(&sel_netif_lock);
  149. netif = sel_netif_find(ifindex);
  150. if (netif != NULL) {
  151. *sid = netif->nsec.sid;
  152. ret = 0;
  153. goto out;
  154. }
  155. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  156. if (new == NULL) {
  157. ret = -ENOMEM;
  158. goto out;
  159. }
  160. ret = security_netif_sid(dev->name, &new->nsec.sid);
  161. if (ret != 0)
  162. goto out;
  163. new->nsec.ifindex = ifindex;
  164. ret = sel_netif_insert(new);
  165. if (ret != 0)
  166. goto out;
  167. *sid = new->nsec.sid;
  168. out:
  169. spin_unlock_bh(&sel_netif_lock);
  170. dev_put(dev);
  171. if (unlikely(ret)) {
  172. printk(KERN_WARNING
  173. "SELinux: failure in sel_netif_sid_slow(),"
  174. " unable to determine network interface label (%d)\n",
  175. ifindex);
  176. kfree(new);
  177. }
  178. return ret;
  179. }
  180. /**
  181. * sel_netif_sid - Lookup the SID of a network interface
  182. * @ifindex: the network interface
  183. * @sid: interface SID
  184. *
  185. * Description:
  186. * This function determines the SID of a network interface using the fastest
  187. * method possible. First the interface table is queried, but if an entry
  188. * can't be found then the policy is queried and the result is added to the
  189. * table to speedup future queries. Returns zero on success, negative values
  190. * on failure.
  191. *
  192. */
  193. int sel_netif_sid(int ifindex, u32 *sid)
  194. {
  195. struct sel_netif *netif;
  196. rcu_read_lock();
  197. netif = sel_netif_find(ifindex);
  198. if (likely(netif != NULL)) {
  199. *sid = netif->nsec.sid;
  200. rcu_read_unlock();
  201. return 0;
  202. }
  203. rcu_read_unlock();
  204. return sel_netif_sid_slow(ifindex, sid);
  205. }
  206. /**
  207. * sel_netif_kill - Remove an entry from the network interface table
  208. * @ifindex: the network interface
  209. *
  210. * Description:
  211. * This function removes the entry matching @ifindex from the network interface
  212. * table if it exists.
  213. *
  214. */
  215. static void sel_netif_kill(int ifindex)
  216. {
  217. struct sel_netif *netif;
  218. rcu_read_lock();
  219. spin_lock_bh(&sel_netif_lock);
  220. netif = sel_netif_find(ifindex);
  221. if (netif)
  222. sel_netif_destroy(netif);
  223. spin_unlock_bh(&sel_netif_lock);
  224. rcu_read_unlock();
  225. }
  226. /**
  227. * sel_netif_flush - Flush the entire network interface table
  228. *
  229. * Description:
  230. * Remove all entries from the network interface table.
  231. *
  232. */
  233. static void sel_netif_flush(void)
  234. {
  235. int idx;
  236. struct sel_netif *netif;
  237. spin_lock_bh(&sel_netif_lock);
  238. for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
  239. list_for_each_entry(netif, &sel_netif_hash[idx], list)
  240. sel_netif_destroy(netif);
  241. spin_unlock_bh(&sel_netif_lock);
  242. }
  243. static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
  244. u16 class, u32 perms, u32 *retained)
  245. {
  246. if (event == AVC_CALLBACK_RESET) {
  247. sel_netif_flush();
  248. synchronize_net();
  249. }
  250. return 0;
  251. }
  252. static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
  253. unsigned long event, void *ptr)
  254. {
  255. struct net_device *dev = ptr;
  256. if (dev_net(dev) != &init_net)
  257. return NOTIFY_DONE;
  258. if (event == NETDEV_DOWN)
  259. sel_netif_kill(dev->ifindex);
  260. return NOTIFY_DONE;
  261. }
  262. static struct notifier_block sel_netif_netdev_notifier = {
  263. .notifier_call = sel_netif_netdev_notifier_handler,
  264. };
  265. static __init int sel_netif_init(void)
  266. {
  267. int i, err;
  268. if (!selinux_enabled)
  269. return 0;
  270. for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
  271. INIT_LIST_HEAD(&sel_netif_hash[i]);
  272. register_netdevice_notifier(&sel_netif_netdev_notifier);
  273. err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
  274. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  275. if (err)
  276. panic("avc_add_callback() failed, error %d\n", err);
  277. return err;
  278. }
  279. __initcall(sel_netif_init);