capability.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor capability mediation functions
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/capability.h>
  11. #include <linux/errno.h>
  12. #include <linux/gfp.h>
  13. #include <linux/security.h>
  14. #include "include/apparmor.h"
  15. #include "include/capability.h"
  16. #include "include/cred.h"
  17. #include "include/policy.h"
  18. #include "include/audit.h"
  19. /*
  20. * Table of capability names: we generate it from capabilities.h.
  21. */
  22. #include "capability_names.h"
  23. struct aa_sfs_entry aa_sfs_entry_caps[] = {
  24. AA_SFS_FILE_STRING("mask", AA_SFS_CAPS_MASK),
  25. { }
  26. };
  27. struct audit_cache {
  28. struct aa_profile *profile;
  29. kernel_cap_t caps;
  30. };
  31. static DEFINE_PER_CPU(struct audit_cache, audit_cache);
  32. /**
  33. * audit_cb - call back for capability components of audit struct
  34. * @ab: audit buffer (NOT NULL)
  35. * @va: audit struct to audit data from (NOT NULL)
  36. */
  37. static void audit_cb(struct audit_buffer *ab, void *va)
  38. {
  39. struct common_audit_data *sa = va;
  40. audit_log_format(ab, " capname=");
  41. audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
  42. }
  43. /**
  44. * audit_caps - audit a capability
  45. * @ad: audit data
  46. * @profile: profile being tested for confinement (NOT NULL)
  47. * @cap: capability tested
  48. * @error: error code returned by test
  49. *
  50. * Do auditing of capability and handle, audit/complain/kill modes switching
  51. * and duplicate message elimination.
  52. *
  53. * Returns: 0 or ad->error on success, error code on failure
  54. */
  55. static int audit_caps(struct apparmor_audit_data *ad, struct aa_profile *profile,
  56. int cap, int error)
  57. {
  58. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  59. typeof(*rules), list);
  60. struct audit_cache *ent;
  61. int type = AUDIT_APPARMOR_AUTO;
  62. ad->error = error;
  63. if (likely(!error)) {
  64. /* test if auditing is being forced */
  65. if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
  66. !cap_raised(rules->caps.audit, cap)))
  67. return 0;
  68. type = AUDIT_APPARMOR_AUDIT;
  69. } else if (KILL_MODE(profile) ||
  70. cap_raised(rules->caps.kill, cap)) {
  71. type = AUDIT_APPARMOR_KILL;
  72. } else if (cap_raised(rules->caps.quiet, cap) &&
  73. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  74. AUDIT_MODE(profile) != AUDIT_ALL) {
  75. /* quiet auditing */
  76. return error;
  77. }
  78. /* Do simple duplicate message elimination */
  79. ent = &get_cpu_var(audit_cache);
  80. if (profile == ent->profile && cap_raised(ent->caps, cap)) {
  81. put_cpu_var(audit_cache);
  82. if (COMPLAIN_MODE(profile))
  83. return complain_error(error);
  84. return error;
  85. } else {
  86. aa_put_profile(ent->profile);
  87. if (profile != ent->profile)
  88. cap_clear(ent->caps);
  89. ent->profile = aa_get_profile(profile);
  90. cap_raise(ent->caps, cap);
  91. }
  92. put_cpu_var(audit_cache);
  93. return aa_audit(type, profile, ad, audit_cb);
  94. }
  95. /**
  96. * profile_capable - test if profile allows use of capability @cap
  97. * @profile: profile being enforced (NOT NULL, NOT unconfined)
  98. * @cap: capability to test if allowed
  99. * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
  100. * @ad: audit data (MAY BE NULL indicating no auditing)
  101. *
  102. * Returns: 0 if allowed else -EPERM
  103. */
  104. static int profile_capable(struct aa_profile *profile, int cap,
  105. unsigned int opts, struct apparmor_audit_data *ad)
  106. {
  107. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  108. typeof(*rules), list);
  109. int error;
  110. if (cap_raised(rules->caps.allow, cap) &&
  111. !cap_raised(rules->caps.denied, cap))
  112. error = 0;
  113. else
  114. error = -EPERM;
  115. if (opts & CAP_OPT_NOAUDIT) {
  116. if (!COMPLAIN_MODE(profile))
  117. return error;
  118. /* audit the cap request in complain mode but note that it
  119. * should be optional.
  120. */
  121. ad->info = "optional: no audit";
  122. }
  123. return audit_caps(ad, profile, cap, error);
  124. }
  125. /**
  126. * aa_capable - test permission to use capability
  127. * @subj_cred: cred we are testing capability against
  128. * @label: label being tested for capability (NOT NULL)
  129. * @cap: capability to be tested
  130. * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
  131. *
  132. * Look up capability in profile capability set.
  133. *
  134. * Returns: 0 on success, or else an error code.
  135. */
  136. int aa_capable(const struct cred *subj_cred, struct aa_label *label,
  137. int cap, unsigned int opts)
  138. {
  139. struct aa_profile *profile;
  140. int error = 0;
  141. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_CAP, AA_CLASS_CAP, OP_CAPABLE);
  142. ad.subj_cred = subj_cred;
  143. ad.common.u.cap = cap;
  144. error = fn_for_each_confined(label, profile,
  145. profile_capable(profile, cap, opts, &ad));
  146. return error;
  147. }