secid.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor security identifier (secid) manipulation fns
  6. *
  7. * Copyright 2009-2017 Canonical Ltd.
  8. *
  9. * AppArmor allocates a unique secid for every label used. If a label
  10. * is replaced it receives the secid of the label it is replacing.
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/err.h>
  14. #include <linux/gfp.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/xarray.h>
  18. #include "include/cred.h"
  19. #include "include/lib.h"
  20. #include "include/secid.h"
  21. #include "include/label.h"
  22. #include "include/policy_ns.h"
  23. /*
  24. * secids - do not pin labels with a refcount. They rely on the label
  25. * properly updating/freeing them
  26. */
  27. #define AA_FIRST_SECID 2
  28. static DEFINE_XARRAY_FLAGS(aa_secids, XA_FLAGS_LOCK_IRQ | XA_FLAGS_TRACK_FREE);
  29. int apparmor_display_secid_mode;
  30. /*
  31. * TODO: allow policy to reserve a secid range?
  32. * TODO: add secid pinning
  33. * TODO: use secid_update in label replace
  34. */
  35. /*
  36. * see label for inverse aa_label_to_secid
  37. */
  38. struct aa_label *aa_secid_to_label(u32 secid)
  39. {
  40. return xa_load(&aa_secids, secid);
  41. }
  42. static int apparmor_label_to_secctx(struct aa_label *label,
  43. struct lsm_context *cp)
  44. {
  45. /* TODO: cache secctx and ref count so we don't have to recreate */
  46. int flags = FLAG_VIEW_SUBNS | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT;
  47. int len;
  48. if (!label)
  49. return -EINVAL;
  50. if (apparmor_display_secid_mode)
  51. flags |= FLAG_SHOW_MODE;
  52. if (cp)
  53. len = aa_label_asxprint(&cp->context, root_ns, label,
  54. flags, GFP_ATOMIC);
  55. else
  56. len = aa_label_snxprint(NULL, 0, root_ns, label, flags);
  57. if (len < 0)
  58. return -ENOMEM;
  59. if (cp) {
  60. cp->len = len;
  61. cp->id = LSM_ID_APPARMOR;
  62. }
  63. return len;
  64. }
  65. int apparmor_secid_to_secctx(u32 secid, struct lsm_context *cp)
  66. {
  67. struct aa_label *label = aa_secid_to_label(secid);
  68. return apparmor_label_to_secctx(label, cp);
  69. }
  70. int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp)
  71. {
  72. struct aa_label *label;
  73. label = prop->apparmor.label;
  74. return apparmor_label_to_secctx(label, cp);
  75. }
  76. int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  77. {
  78. struct aa_label *label;
  79. label = aa_label_strn_parse(&root_ns->unconfined->label, secdata,
  80. seclen, GFP_KERNEL, false, false);
  81. if (IS_ERR(label))
  82. return PTR_ERR(label);
  83. *secid = label->secid;
  84. return 0;
  85. }
  86. void apparmor_release_secctx(struct lsm_context *cp)
  87. {
  88. if (cp->id == LSM_ID_APPARMOR) {
  89. kfree(cp->context);
  90. cp->context = NULL;
  91. cp->id = LSM_ID_UNDEF;
  92. }
  93. }
  94. /**
  95. * aa_alloc_secid - allocate a new secid for a profile
  96. * @label: the label to allocate a secid for
  97. * @gfp: memory allocation flags
  98. *
  99. * Returns: 0 with @label->secid initialized
  100. * <0 returns error with @label->secid set to AA_SECID_INVALID
  101. */
  102. int aa_alloc_secid(struct aa_label *label, gfp_t gfp)
  103. {
  104. unsigned long flags;
  105. int ret;
  106. xa_lock_irqsave(&aa_secids, flags);
  107. ret = __xa_alloc(&aa_secids, &label->secid, label,
  108. XA_LIMIT(AA_FIRST_SECID, INT_MAX), gfp);
  109. xa_unlock_irqrestore(&aa_secids, flags);
  110. if (ret < 0) {
  111. label->secid = AA_SECID_INVALID;
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * aa_free_secid - free a secid
  118. * @secid: secid to free
  119. */
  120. void aa_free_secid(u32 secid)
  121. {
  122. unsigned long flags;
  123. xa_lock_irqsave(&aa_secids, flags);
  124. __xa_erase(&aa_secids, secid);
  125. xa_unlock_irqrestore(&aa_secids, flags);
  126. }