capability.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor capability mediation functions
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/gfp.h>
  17. #include <linux/security.h>
  18. #include "include/apparmor.h"
  19. #include "include/capability.h"
  20. #include "include/context.h"
  21. #include "include/policy.h"
  22. #include "include/audit.h"
  23. /*
  24. * Table of capability names: we generate it from capabilities.h.
  25. */
  26. #include "capability_names.h"
  27. struct aa_sfs_entry aa_sfs_entry_caps[] = {
  28. AA_SFS_FILE_STRING("mask", AA_SFS_CAPS_MASK),
  29. { }
  30. };
  31. struct audit_cache {
  32. struct aa_profile *profile;
  33. kernel_cap_t caps;
  34. };
  35. static DEFINE_PER_CPU(struct audit_cache, audit_cache);
  36. /**
  37. * audit_cb - call back for capability components of audit struct
  38. * @ab - audit buffer (NOT NULL)
  39. * @va - audit struct to audit data from (NOT NULL)
  40. */
  41. static void audit_cb(struct audit_buffer *ab, void *va)
  42. {
  43. struct common_audit_data *sa = va;
  44. audit_log_format(ab, " capname=");
  45. audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
  46. }
  47. /**
  48. * audit_caps - audit a capability
  49. * @sa: audit data
  50. * @profile: profile being tested for confinement (NOT NULL)
  51. * @cap: capability tested
  52. * @error: error code returned by test
  53. *
  54. * Do auditing of capability and handle, audit/complain/kill modes switching
  55. * and duplicate message elimination.
  56. *
  57. * Returns: 0 or sa->error on success, error code on failure
  58. */
  59. static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  60. int cap, int error)
  61. {
  62. struct audit_cache *ent;
  63. int type = AUDIT_APPARMOR_AUTO;
  64. aad(sa)->error = error;
  65. if (likely(!error)) {
  66. /* test if auditing is being forced */
  67. if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
  68. !cap_raised(profile->caps.audit, cap)))
  69. return 0;
  70. type = AUDIT_APPARMOR_AUDIT;
  71. } else if (KILL_MODE(profile) ||
  72. cap_raised(profile->caps.kill, cap)) {
  73. type = AUDIT_APPARMOR_KILL;
  74. } else if (cap_raised(profile->caps.quiet, cap) &&
  75. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  76. AUDIT_MODE(profile) != AUDIT_ALL) {
  77. /* quiet auditing */
  78. return error;
  79. }
  80. /* Do simple duplicate message elimination */
  81. ent = &get_cpu_var(audit_cache);
  82. if (profile == ent->profile && cap_raised(ent->caps, cap)) {
  83. put_cpu_var(audit_cache);
  84. if (COMPLAIN_MODE(profile))
  85. return complain_error(error);
  86. return error;
  87. } else {
  88. aa_put_profile(ent->profile);
  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, sa, 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. * @audit: whether an audit record should be generated
  100. * @sa: 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, int audit,
  105. struct common_audit_data *sa)
  106. {
  107. int error;
  108. if (cap_raised(profile->caps.allow, cap) &&
  109. !cap_raised(profile->caps.denied, cap))
  110. error = 0;
  111. else
  112. error = -EPERM;
  113. if (audit == SECURITY_CAP_NOAUDIT) {
  114. if (!COMPLAIN_MODE(profile))
  115. return error;
  116. /* audit the cap request in complain mode but note that it
  117. * should be optional.
  118. */
  119. aad(sa)->info = "optional: no audit";
  120. }
  121. return audit_caps(sa, profile, cap, error);
  122. }
  123. /**
  124. * aa_capable - test permission to use capability
  125. * @label: label being tested for capability (NOT NULL)
  126. * @cap: capability to be tested
  127. * @audit: whether an audit record should be generated
  128. *
  129. * Look up capability in profile capability set.
  130. *
  131. * Returns: 0 on success, or else an error code.
  132. */
  133. int aa_capable(struct aa_label *label, int cap, int audit)
  134. {
  135. struct aa_profile *profile;
  136. int error = 0;
  137. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_CAP, OP_CAPABLE);
  138. sa.u.cap = cap;
  139. error = fn_for_each_confined(label, profile,
  140. profile_capable(profile, cap, audit, &sa));
  141. return error;
  142. }