secid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor security identifier (secid) manipulation fns
  5. *
  6. * Copyright 2009-2017 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. *
  13. *
  14. * AppArmor allocates a unique secid for every label used. If a label
  15. * is replaced it receives the secid of the label it is replacing.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/err.h>
  19. #include <linux/gfp.h>
  20. #include <linux/idr.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include "include/cred.h"
  24. #include "include/lib.h"
  25. #include "include/secid.h"
  26. #include "include/label.h"
  27. #include "include/policy_ns.h"
  28. /*
  29. * secids - do not pin labels with a refcount. They rely on the label
  30. * properly updating/freeing them
  31. */
  32. #define AA_FIRST_SECID 1
  33. static DEFINE_IDR(aa_secids);
  34. static DEFINE_SPINLOCK(secid_lock);
  35. /*
  36. * TODO: allow policy to reserve a secid range?
  37. * TODO: add secid pinning
  38. * TODO: use secid_update in label replace
  39. */
  40. /**
  41. * aa_secid_update - update a secid mapping to a new label
  42. * @secid: secid to update
  43. * @label: label the secid will now map to
  44. */
  45. void aa_secid_update(u32 secid, struct aa_label *label)
  46. {
  47. unsigned long flags;
  48. spin_lock_irqsave(&secid_lock, flags);
  49. idr_replace(&aa_secids, label, secid);
  50. spin_unlock_irqrestore(&secid_lock, flags);
  51. }
  52. /**
  53. *
  54. * see label for inverse aa_label_to_secid
  55. */
  56. struct aa_label *aa_secid_to_label(u32 secid)
  57. {
  58. struct aa_label *label;
  59. rcu_read_lock();
  60. label = idr_find(&aa_secids, secid);
  61. rcu_read_unlock();
  62. return label;
  63. }
  64. int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  65. {
  66. /* TODO: cache secctx and ref count so we don't have to recreate */
  67. struct aa_label *label = aa_secid_to_label(secid);
  68. int len;
  69. AA_BUG(!seclen);
  70. if (!label)
  71. return -EINVAL;
  72. if (secdata)
  73. len = aa_label_asxprint(secdata, root_ns, label,
  74. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  75. FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT,
  76. GFP_ATOMIC);
  77. else
  78. len = aa_label_snxprint(NULL, 0, root_ns, label,
  79. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  80. FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT);
  81. if (len < 0)
  82. return -ENOMEM;
  83. *seclen = len;
  84. return 0;
  85. }
  86. int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  87. {
  88. struct aa_label *label;
  89. label = aa_label_strn_parse(&root_ns->unconfined->label, secdata,
  90. seclen, GFP_KERNEL, false, false);
  91. if (IS_ERR(label))
  92. return PTR_ERR(label);
  93. *secid = label->secid;
  94. return 0;
  95. }
  96. void apparmor_release_secctx(char *secdata, u32 seclen)
  97. {
  98. kfree(secdata);
  99. }
  100. /**
  101. * aa_alloc_secid - allocate a new secid for a profile
  102. * @label: the label to allocate a secid for
  103. * @gfp: memory allocation flags
  104. *
  105. * Returns: 0 with @label->secid initialized
  106. * <0 returns error with @label->secid set to AA_SECID_INVALID
  107. */
  108. int aa_alloc_secid(struct aa_label *label, gfp_t gfp)
  109. {
  110. unsigned long flags;
  111. int ret;
  112. idr_preload(gfp);
  113. spin_lock_irqsave(&secid_lock, flags);
  114. ret = idr_alloc(&aa_secids, label, AA_FIRST_SECID, 0, GFP_ATOMIC);
  115. spin_unlock_irqrestore(&secid_lock, flags);
  116. idr_preload_end();
  117. if (ret < 0) {
  118. label->secid = AA_SECID_INVALID;
  119. return ret;
  120. }
  121. AA_BUG(ret == AA_SECID_INVALID);
  122. label->secid = ret;
  123. return 0;
  124. }
  125. /**
  126. * aa_free_secid - free a secid
  127. * @secid: secid to free
  128. */
  129. void aa_free_secid(u32 secid)
  130. {
  131. unsigned long flags;
  132. spin_lock_irqsave(&secid_lock, flags);
  133. idr_remove(&aa_secids, secid);
  134. spin_unlock_irqrestore(&secid_lock, flags);
  135. }
  136. void aa_secids_init(void)
  137. {
  138. idr_init_base(&aa_secids, AA_FIRST_SECID);
  139. }