audit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor auditing 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/audit.h>
  15. #include <linux/socket.h>
  16. #include "include/apparmor.h"
  17. #include "include/audit.h"
  18. #include "include/policy.h"
  19. #include "include/policy_ns.h"
  20. #include "include/secid.h"
  21. const char *const audit_mode_names[] = {
  22. "normal",
  23. "quiet_denied",
  24. "quiet",
  25. "noquiet",
  26. "all"
  27. };
  28. static const char *const aa_audit_type[] = {
  29. "AUDIT",
  30. "ALLOWED",
  31. "DENIED",
  32. "HINT",
  33. "STATUS",
  34. "ERROR",
  35. "KILLED",
  36. "AUTO"
  37. };
  38. /*
  39. * Currently AppArmor auditing is fed straight into the audit framework.
  40. *
  41. * TODO:
  42. * netlink interface for complain mode
  43. * user auditing, - send user auditing to netlink interface
  44. * system control of whether user audit messages go to system log
  45. */
  46. /**
  47. * audit_base - core AppArmor function.
  48. * @ab: audit buffer to fill (NOT NULL)
  49. * @ca: audit structure containing data to audit (NOT NULL)
  50. *
  51. * Record common AppArmor audit data from @sa
  52. */
  53. static void audit_pre(struct audit_buffer *ab, void *ca)
  54. {
  55. struct common_audit_data *sa = ca;
  56. if (aa_g_audit_header) {
  57. audit_log_format(ab, "apparmor=");
  58. audit_log_string(ab, aa_audit_type[aad(sa)->type]);
  59. }
  60. if (aad(sa)->op) {
  61. audit_log_format(ab, " operation=");
  62. audit_log_string(ab, aad(sa)->op);
  63. }
  64. if (aad(sa)->info) {
  65. audit_log_format(ab, " info=");
  66. audit_log_string(ab, aad(sa)->info);
  67. if (aad(sa)->error)
  68. audit_log_format(ab, " error=%d", aad(sa)->error);
  69. }
  70. if (aad(sa)->label) {
  71. struct aa_label *label = aad(sa)->label;
  72. if (label_isprofile(label)) {
  73. struct aa_profile *profile = labels_profile(label);
  74. if (profile->ns != root_ns) {
  75. audit_log_format(ab, " namespace=");
  76. audit_log_untrustedstring(ab,
  77. profile->ns->base.hname);
  78. }
  79. audit_log_format(ab, " profile=");
  80. audit_log_untrustedstring(ab, profile->base.hname);
  81. } else {
  82. audit_log_format(ab, " label=");
  83. aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
  84. GFP_ATOMIC);
  85. }
  86. }
  87. if (aad(sa)->name) {
  88. audit_log_format(ab, " name=");
  89. audit_log_untrustedstring(ab, aad(sa)->name);
  90. }
  91. }
  92. /**
  93. * aa_audit_msg - Log a message to the audit subsystem
  94. * @sa: audit event structure (NOT NULL)
  95. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  96. */
  97. void aa_audit_msg(int type, struct common_audit_data *sa,
  98. void (*cb) (struct audit_buffer *, void *))
  99. {
  100. aad(sa)->type = type;
  101. common_lsm_audit(sa, audit_pre, cb);
  102. }
  103. /**
  104. * aa_audit - Log a profile based audit event to the audit subsystem
  105. * @type: audit type for the message
  106. * @profile: profile to check against (NOT NULL)
  107. * @sa: audit event (NOT NULL)
  108. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  109. *
  110. * Handle default message switching based off of audit mode flags
  111. *
  112. * Returns: error on failure
  113. */
  114. int aa_audit(int type, struct aa_profile *profile, struct common_audit_data *sa,
  115. void (*cb) (struct audit_buffer *, void *))
  116. {
  117. AA_BUG(!profile);
  118. if (type == AUDIT_APPARMOR_AUTO) {
  119. if (likely(!aad(sa)->error)) {
  120. if (AUDIT_MODE(profile) != AUDIT_ALL)
  121. return 0;
  122. type = AUDIT_APPARMOR_AUDIT;
  123. } else if (COMPLAIN_MODE(profile))
  124. type = AUDIT_APPARMOR_ALLOWED;
  125. else
  126. type = AUDIT_APPARMOR_DENIED;
  127. }
  128. if (AUDIT_MODE(profile) == AUDIT_QUIET ||
  129. (type == AUDIT_APPARMOR_DENIED &&
  130. AUDIT_MODE(profile) == AUDIT_QUIET))
  131. return aad(sa)->error;
  132. if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
  133. type = AUDIT_APPARMOR_KILL;
  134. aad(sa)->label = &profile->label;
  135. aa_audit_msg(type, sa, cb);
  136. if (aad(sa)->type == AUDIT_APPARMOR_KILL)
  137. (void)send_sig_info(SIGKILL, NULL,
  138. sa->type == LSM_AUDIT_DATA_TASK && sa->u.tsk ?
  139. sa->u.tsk : current);
  140. if (aad(sa)->type == AUDIT_APPARMOR_ALLOWED)
  141. return complain_error(aad(sa)->error);
  142. return aad(sa)->error;
  143. }
  144. struct aa_audit_rule {
  145. struct aa_label *label;
  146. };
  147. void aa_audit_rule_free(void *vrule)
  148. {
  149. struct aa_audit_rule *rule = vrule;
  150. if (rule) {
  151. if (!IS_ERR(rule->label))
  152. aa_put_label(rule->label);
  153. kfree(rule);
  154. }
  155. }
  156. int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  157. {
  158. struct aa_audit_rule *rule;
  159. switch (field) {
  160. case AUDIT_SUBJ_ROLE:
  161. if (op != Audit_equal && op != Audit_not_equal)
  162. return -EINVAL;
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. rule = kzalloc(sizeof(struct aa_audit_rule), GFP_KERNEL);
  168. if (!rule)
  169. return -ENOMEM;
  170. /* Currently rules are treated as coming from the root ns */
  171. rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
  172. GFP_KERNEL, true, false);
  173. if (IS_ERR(rule->label)) {
  174. aa_audit_rule_free(rule);
  175. return PTR_ERR(rule->label);
  176. }
  177. *vrule = rule;
  178. return 0;
  179. }
  180. int aa_audit_rule_known(struct audit_krule *rule)
  181. {
  182. int i;
  183. for (i = 0; i < rule->field_count; i++) {
  184. struct audit_field *f = &rule->fields[i];
  185. switch (f->type) {
  186. case AUDIT_SUBJ_ROLE:
  187. return 1;
  188. }
  189. }
  190. return 0;
  191. }
  192. int aa_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  193. struct audit_context *actx)
  194. {
  195. struct aa_audit_rule *rule = vrule;
  196. struct aa_label *label;
  197. int found = 0;
  198. label = aa_secid_to_label(sid);
  199. if (!label)
  200. return -ENOENT;
  201. if (aa_label_is_subset(label, rule->label))
  202. found = 1;
  203. switch (field) {
  204. case AUDIT_SUBJ_ROLE:
  205. switch (op) {
  206. case Audit_equal:
  207. return found;
  208. case Audit_not_equal:
  209. return !found;
  210. }
  211. }
  212. return 0;
  213. }