netif.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. {
  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_free - Frees an interface entry
  95. * @p: the entry's RCU field
  96. *
  97. * Description:
  98. * This function is designed to be used as a callback to the call_rcu()
  99. * function so that memory allocated to a hash table interface entry can be
  100. * released safely.
  101. *
  102. */
  103. static void sel_netif_free(struct rcu_head *p)
  104. {
  105. struct sel_netif *netif = container_of(p, struct sel_netif, rcu_head);
  106. kfree(netif);
  107. }
  108. /**
  109. * sel_netif_destroy - Remove an interface record from the table
  110. * @netif: the existing interface record
  111. *
  112. * Description:
  113. * Remove an existing interface record from the network interface table.
  114. *
  115. */
  116. static void sel_netif_destroy(struct sel_netif *netif)
  117. {
  118. list_del_rcu(&netif->list);
  119. sel_netif_total--;
  120. call_rcu(&netif->rcu_head, sel_netif_free);
  121. }
  122. /**
  123. * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
  124. * @ifindex: the network interface
  125. * @sid: interface SID
  126. *
  127. * Description:
  128. * This function determines the SID of a network interface by quering the
  129. * security policy. The result is added to the network interface table to
  130. * speedup future queries. Returns zero on success, negative values on
  131. * failure.
  132. *
  133. */
  134. static int sel_netif_sid_slow(int ifindex, u32 *sid)
  135. {
  136. int ret;
  137. struct sel_netif *netif;
  138. struct sel_netif *new = NULL;
  139. struct net_device *dev;
  140. /* NOTE: we always use init's network namespace since we don't
  141. * currently support containers */
  142. dev = dev_get_by_index(&init_net, ifindex);
  143. if (unlikely(dev == NULL)) {
  144. printk(KERN_WARNING
  145. "SELinux: failure in sel_netif_sid_slow(),"
  146. " invalid network interface (%d)\n", ifindex);
  147. return -ENOENT;
  148. }
  149. spin_lock_bh(&sel_netif_lock);
  150. netif = sel_netif_find(ifindex);
  151. if (netif != NULL) {
  152. *sid = netif->nsec.sid;
  153. ret = 0;
  154. goto out;
  155. }
  156. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  157. if (new == NULL) {
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. ret = security_netif_sid(dev->name, &new->nsec.sid);
  162. if (ret != 0)
  163. goto out;
  164. new->nsec.ifindex = ifindex;
  165. ret = sel_netif_insert(new);
  166. if (ret != 0)
  167. goto out;
  168. *sid = new->nsec.sid;
  169. out:
  170. spin_unlock_bh(&sel_netif_lock);
  171. dev_put(dev);
  172. if (unlikely(ret)) {
  173. printk(KERN_WARNING
  174. "SELinux: failure in sel_netif_sid_slow(),"
  175. " unable to determine network interface label (%d)\n",
  176. ifindex);
  177. kfree(new);
  178. }
  179. return ret;
  180. }
  181. /**
  182. * sel_netif_sid - Lookup the SID of a network interface
  183. * @ifindex: the network interface
  184. * @sid: interface SID
  185. *
  186. * Description:
  187. * This function determines the SID of a network interface using the fastest
  188. * method possible. First the interface table is queried, but if an entry
  189. * can't be found then the policy is queried and the result is added to the
  190. * table to speedup future queries. Returns zero on success, negative values
  191. * on failure.
  192. *
  193. */
  194. int sel_netif_sid(int ifindex, u32 *sid)
  195. {
  196. struct sel_netif *netif;
  197. rcu_read_lock();
  198. netif = sel_netif_find(ifindex);
  199. if (likely(netif != NULL)) {
  200. *sid = netif->nsec.sid;
  201. rcu_read_unlock();
  202. return 0;
  203. }
  204. rcu_read_unlock();
  205. return sel_netif_sid_slow(ifindex, sid);
  206. }
  207. /**
  208. * sel_netif_kill - Remove an entry from the network interface table
  209. * @ifindex: the network interface
  210. *
  211. * Description:
  212. * This function removes the entry matching @ifindex from the network interface
  213. * table if it exists.
  214. *
  215. */
  216. static void sel_netif_kill(int ifindex)
  217. {
  218. struct sel_netif *netif;
  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. }
  225. /**
  226. * sel_netif_flush - Flush the entire network interface table
  227. *
  228. * Description:
  229. * Remove all entries from the network interface table.
  230. *
  231. */
  232. static void sel_netif_flush(void)
  233. {
  234. int idx;
  235. struct sel_netif *netif;
  236. spin_lock_bh(&sel_netif_lock);
  237. for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
  238. list_for_each_entry(netif, &sel_netif_hash[idx], list)
  239. sel_netif_destroy(netif);
  240. spin_unlock_bh(&sel_netif_lock);
  241. }
  242. static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
  243. u16 class, u32 perms, u32 *retained)
  244. {
  245. if (event == AVC_CALLBACK_RESET) {
  246. sel_netif_flush();
  247. synchronize_net();
  248. }
  249. return 0;
  250. }
  251. static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
  252. unsigned long event, void *ptr)
  253. {
  254. struct net_device *dev = ptr;
  255. if (dev->nd_net != &init_net)
  256. return NOTIFY_DONE;
  257. if (event == NETDEV_DOWN)
  258. sel_netif_kill(dev->ifindex);
  259. return NOTIFY_DONE;
  260. }
  261. static struct notifier_block sel_netif_netdev_notifier = {
  262. .notifier_call = sel_netif_netdev_notifier_handler,
  263. };
  264. static __init int sel_netif_init(void)
  265. {
  266. int i, err;
  267. if (!selinux_enabled)
  268. return 0;
  269. for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
  270. INIT_LIST_HEAD(&sel_netif_hash[i]);
  271. register_netdevice_notifier(&sel_netif_netdev_notifier);
  272. err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
  273. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  274. if (err)
  275. panic("avc_add_callback() failed, error %d\n", err);
  276. return err;
  277. }
  278. __initcall(sel_netif_init);