ibpkey.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Pkey table
  3. *
  4. * SELinux must keep a mapping of Infinband PKEYs to labels/SIDs. This
  5. * mapping is maintained as part of the normal policy but a fast cache is
  6. * needed to reduce the lookup overhead.
  7. *
  8. * This code is heavily based on the "netif" and "netport" concept originally
  9. * developed by
  10. * James Morris <jmorris@redhat.com> and
  11. * Paul Moore <paul@paul-moore.com>
  12. * (see security/selinux/netif.c and security/selinux/netport.c for more
  13. * information)
  14. *
  15. */
  16. /*
  17. * (c) Mellanox Technologies, 2016
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of version 2 of the GNU General Public License as
  21. * published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include "ibpkey.h"
  34. #include "objsec.h"
  35. #define SEL_PKEY_HASH_SIZE 256
  36. #define SEL_PKEY_HASH_BKT_LIMIT 16
  37. struct sel_ib_pkey_bkt {
  38. int size;
  39. struct list_head list;
  40. };
  41. struct sel_ib_pkey {
  42. struct pkey_security_struct psec;
  43. struct list_head list;
  44. struct rcu_head rcu;
  45. };
  46. static LIST_HEAD(sel_ib_pkey_list);
  47. static DEFINE_SPINLOCK(sel_ib_pkey_lock);
  48. static struct sel_ib_pkey_bkt sel_ib_pkey_hash[SEL_PKEY_HASH_SIZE];
  49. /**
  50. * sel_ib_pkey_hashfn - Hashing function for the pkey table
  51. * @pkey: pkey number
  52. *
  53. * Description:
  54. * This is the hashing function for the pkey table, it returns the bucket
  55. * number for the given pkey.
  56. *
  57. */
  58. static unsigned int sel_ib_pkey_hashfn(u16 pkey)
  59. {
  60. return (pkey & (SEL_PKEY_HASH_SIZE - 1));
  61. }
  62. /**
  63. * sel_ib_pkey_find - Search for a pkey record
  64. * @subnet_prefix: subnet_prefix
  65. * @pkey_num: pkey_num
  66. *
  67. * Description:
  68. * Search the pkey table and return the matching record. If an entry
  69. * can not be found in the table return NULL.
  70. *
  71. */
  72. static struct sel_ib_pkey *sel_ib_pkey_find(u64 subnet_prefix, u16 pkey_num)
  73. {
  74. unsigned int idx;
  75. struct sel_ib_pkey *pkey;
  76. idx = sel_ib_pkey_hashfn(pkey_num);
  77. list_for_each_entry_rcu(pkey, &sel_ib_pkey_hash[idx].list, list) {
  78. if (pkey->psec.pkey == pkey_num &&
  79. pkey->psec.subnet_prefix == subnet_prefix)
  80. return pkey;
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * sel_ib_pkey_insert - Insert a new pkey into the table
  86. * @pkey: the new pkey record
  87. *
  88. * Description:
  89. * Add a new pkey record to the hash table.
  90. *
  91. */
  92. static void sel_ib_pkey_insert(struct sel_ib_pkey *pkey)
  93. {
  94. unsigned int idx;
  95. /* we need to impose a limit on the growth of the hash table so check
  96. * this bucket to make sure it is within the specified bounds
  97. */
  98. idx = sel_ib_pkey_hashfn(pkey->psec.pkey);
  99. list_add_rcu(&pkey->list, &sel_ib_pkey_hash[idx].list);
  100. if (sel_ib_pkey_hash[idx].size == SEL_PKEY_HASH_BKT_LIMIT) {
  101. struct sel_ib_pkey *tail;
  102. tail = list_entry(
  103. rcu_dereference_protected(
  104. sel_ib_pkey_hash[idx].list.prev,
  105. lockdep_is_held(&sel_ib_pkey_lock)),
  106. struct sel_ib_pkey, list);
  107. list_del_rcu(&tail->list);
  108. kfree_rcu(tail, rcu);
  109. } else {
  110. sel_ib_pkey_hash[idx].size++;
  111. }
  112. }
  113. /**
  114. * sel_ib_pkey_sid_slow - Lookup the SID of a pkey using the policy
  115. * @subnet_prefix: subnet prefix
  116. * @pkey_num: pkey number
  117. * @sid: pkey SID
  118. *
  119. * Description:
  120. * This function determines the SID of a pkey by querying the security
  121. * policy. The result is added to the pkey table to speedup future
  122. * queries. Returns zero on success, negative values on failure.
  123. *
  124. */
  125. static int sel_ib_pkey_sid_slow(u64 subnet_prefix, u16 pkey_num, u32 *sid)
  126. {
  127. int ret;
  128. struct sel_ib_pkey *pkey;
  129. struct sel_ib_pkey *new = NULL;
  130. unsigned long flags;
  131. spin_lock_irqsave(&sel_ib_pkey_lock, flags);
  132. pkey = sel_ib_pkey_find(subnet_prefix, pkey_num);
  133. if (pkey) {
  134. *sid = pkey->psec.sid;
  135. spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
  136. return 0;
  137. }
  138. ret = security_ib_pkey_sid(&selinux_state, subnet_prefix, pkey_num,
  139. sid);
  140. if (ret)
  141. goto out;
  142. /* If this memory allocation fails still return 0. The SID
  143. * is valid, it just won't be added to the cache.
  144. */
  145. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  146. if (!new) {
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. new->psec.subnet_prefix = subnet_prefix;
  151. new->psec.pkey = pkey_num;
  152. new->psec.sid = *sid;
  153. sel_ib_pkey_insert(new);
  154. out:
  155. spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
  156. return ret;
  157. }
  158. /**
  159. * sel_ib_pkey_sid - Lookup the SID of a PKEY
  160. * @subnet_prefix: subnet_prefix
  161. * @pkey_num: pkey number
  162. * @sid: pkey SID
  163. *
  164. * Description:
  165. * This function determines the SID of a PKEY using the fastest method
  166. * possible. First the pkey table is queried, but if an entry can't be found
  167. * then the policy is queried and the result is added to the table to speedup
  168. * future queries. Returns zero on success, negative values on failure.
  169. *
  170. */
  171. int sel_ib_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *sid)
  172. {
  173. struct sel_ib_pkey *pkey;
  174. rcu_read_lock();
  175. pkey = sel_ib_pkey_find(subnet_prefix, pkey_num);
  176. if (pkey) {
  177. *sid = pkey->psec.sid;
  178. rcu_read_unlock();
  179. return 0;
  180. }
  181. rcu_read_unlock();
  182. return sel_ib_pkey_sid_slow(subnet_prefix, pkey_num, sid);
  183. }
  184. /**
  185. * sel_ib_pkey_flush - Flush the entire pkey table
  186. *
  187. * Description:
  188. * Remove all entries from the pkey table
  189. *
  190. */
  191. void sel_ib_pkey_flush(void)
  192. {
  193. unsigned int idx;
  194. struct sel_ib_pkey *pkey, *pkey_tmp;
  195. unsigned long flags;
  196. spin_lock_irqsave(&sel_ib_pkey_lock, flags);
  197. for (idx = 0; idx < SEL_PKEY_HASH_SIZE; idx++) {
  198. list_for_each_entry_safe(pkey, pkey_tmp,
  199. &sel_ib_pkey_hash[idx].list, list) {
  200. list_del_rcu(&pkey->list);
  201. kfree_rcu(pkey, rcu);
  202. }
  203. sel_ib_pkey_hash[idx].size = 0;
  204. }
  205. spin_unlock_irqrestore(&sel_ib_pkey_lock, flags);
  206. }
  207. static __init int sel_ib_pkey_init(void)
  208. {
  209. int iter;
  210. if (!selinux_enabled)
  211. return 0;
  212. for (iter = 0; iter < SEL_PKEY_HASH_SIZE; iter++) {
  213. INIT_LIST_HEAD(&sel_ib_pkey_hash[iter].list);
  214. sel_ib_pkey_hash[iter].size = 0;
  215. }
  216. return 0;
  217. }
  218. subsys_initcall(sel_ib_pkey_init);