ibpkey.c 5.6 KB

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