capability.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. COMMON_AUDIT_DATA_INIT(&sa, CAP);
  61. sa.tsk = task;
  62. sa.u.cap = cap;
  63. sa.aad.op = OP_CAPABLE;
  64. sa.aad.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, GFP_ATOMIC, &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. *
  100. * Returns: 0 if allowed else -EPERM
  101. */
  102. static int profile_capable(struct aa_profile *profile, int cap)
  103. {
  104. return cap_raised(profile->caps.allow, cap) ? 0 : -EPERM;
  105. }
  106. /**
  107. * aa_capable - test permission to use capability
  108. * @task: task doing capability test against (NOT NULL)
  109. * @profile: profile confining @task (NOT NULL)
  110. * @cap: capability to be tested
  111. * @audit: whether an audit record should be generated
  112. *
  113. * Look up capability in profile capability set.
  114. *
  115. * Returns: 0 on success, or else an error code.
  116. */
  117. int aa_capable(struct task_struct *task, struct aa_profile *profile, int cap,
  118. int audit)
  119. {
  120. int error = profile_capable(profile, cap);
  121. if (!audit) {
  122. if (COMPLAIN_MODE(profile))
  123. return complain_error(error);
  124. return error;
  125. }
  126. return audit_caps(profile, task, cap, error);
  127. }