capability.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "include/apparmor.h"
  18. #include "include/capability.h"
  19. #include "include/context.h"
  20. #include "include/policy.h"
  21. #include "include/audit.h"
  22. /*
  23. * Table of capability names: we generate it from capabilities.h.
  24. */
  25. #include "capability_names.h"
  26. struct audit_cache {
  27. struct aa_profile *profile;
  28. kernel_cap_t caps;
  29. };
  30. static DEFINE_PER_CPU(struct audit_cache, audit_cache);
  31. /**
  32. * audit_cb - call back for capability components of audit struct
  33. * @ab - audit buffer (NOT NULL)
  34. * @va - audit struct to audit data from (NOT NULL)
  35. */
  36. static void audit_cb(struct audit_buffer *ab, void *va)
  37. {
  38. struct common_audit_data *sa = va;
  39. audit_log_format(ab, " capname=");
  40. audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
  41. }
  42. /**
  43. * audit_caps - audit a capability
  44. * @profile: profile confining task (NOT NULL)
  45. * @task: task capability test was performed against (NOT NULL)
  46. * @cap: capability tested
  47. * @error: error code returned by test
  48. *
  49. * Do auditing of capability and handle, audit/complain/kill modes switching
  50. * and duplicate message elimination.
  51. *
  52. * Returns: 0 or sa->error on success, error code on failure
  53. */
  54. static int audit_caps(struct aa_profile *profile, struct task_struct *task,
  55. int cap, int error)
  56. {
  57. struct audit_cache *ent;
  58. int type = AUDIT_APPARMOR_AUTO;
  59. struct common_audit_data sa;
  60. struct apparmor_audit_data aad = {0,};
  61. COMMON_AUDIT_DATA_INIT(&sa, CAP);
  62. sa.aad = &aad;
  63. sa.tsk = task;
  64. sa.u.cap = cap;
  65. sa.aad->op = OP_CAPABLE;
  66. sa.aad->error = error;
  67. if (likely(!error)) {
  68. /* test if auditing is being forced */
  69. if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
  70. !cap_raised(profile->caps.audit, cap)))
  71. return 0;
  72. type = AUDIT_APPARMOR_AUDIT;
  73. } else if (KILL_MODE(profile) ||
  74. cap_raised(profile->caps.kill, cap)) {
  75. type = AUDIT_APPARMOR_KILL;
  76. } else if (cap_raised(profile->caps.quiet, cap) &&
  77. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  78. AUDIT_MODE(profile) != AUDIT_ALL) {
  79. /* quiet auditing */
  80. return error;
  81. }
  82. /* Do simple duplicate message elimination */
  83. ent = &get_cpu_var(audit_cache);
  84. if (profile == ent->profile && cap_raised(ent->caps, cap)) {
  85. put_cpu_var(audit_cache);
  86. if (COMPLAIN_MODE(profile))
  87. return complain_error(error);
  88. return error;
  89. } else {
  90. aa_put_profile(ent->profile);
  91. ent->profile = aa_get_profile(profile);
  92. cap_raise(ent->caps, cap);
  93. }
  94. put_cpu_var(audit_cache);
  95. return aa_audit(type, profile, GFP_ATOMIC, &sa, audit_cb);
  96. }
  97. /**
  98. * profile_capable - test if profile allows use of capability @cap
  99. * @profile: profile being enforced (NOT NULL, NOT unconfined)
  100. * @cap: capability to test if allowed
  101. *
  102. * Returns: 0 if allowed else -EPERM
  103. */
  104. static int profile_capable(struct aa_profile *profile, int cap)
  105. {
  106. return cap_raised(profile->caps.allow, cap) ? 0 : -EPERM;
  107. }
  108. /**
  109. * aa_capable - test permission to use capability
  110. * @task: task doing capability test against (NOT NULL)
  111. * @profile: profile confining @task (NOT NULL)
  112. * @cap: capability to be tested
  113. * @audit: whether an audit record should be generated
  114. *
  115. * Look up capability in profile capability set.
  116. *
  117. * Returns: 0 on success, or else an error code.
  118. */
  119. int aa_capable(struct task_struct *task, struct aa_profile *profile, int cap,
  120. int audit)
  121. {
  122. int error = profile_capable(profile, cap);
  123. if (!audit) {
  124. if (COMPLAIN_MODE(profile))
  125. return complain_error(error);
  126. return error;
  127. }
  128. return audit_caps(profile, task, cap, error);
  129. }