audit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. const char *const audit_mode_names[] = {
  21. "normal",
  22. "quiet_denied",
  23. "quiet",
  24. "noquiet",
  25. "all"
  26. };
  27. static const char *const aa_audit_type[] = {
  28. "AUDIT",
  29. "ALLOWED",
  30. "DENIED",
  31. "HINT",
  32. "STATUS",
  33. "ERROR",
  34. "KILLED",
  35. "AUTO"
  36. };
  37. /*
  38. * Currently AppArmor auditing is fed straight into the audit framework.
  39. *
  40. * TODO:
  41. * netlink interface for complain mode
  42. * user auditing, - send user auditing to netlink interface
  43. * system control of whether user audit messages go to system log
  44. */
  45. /**
  46. * audit_base - core AppArmor function.
  47. * @ab: audit buffer to fill (NOT NULL)
  48. * @ca: audit structure containing data to audit (NOT NULL)
  49. *
  50. * Record common AppArmor audit data from @sa
  51. */
  52. static void audit_pre(struct audit_buffer *ab, void *ca)
  53. {
  54. struct common_audit_data *sa = ca;
  55. if (aa_g_audit_header) {
  56. audit_log_format(ab, "apparmor=");
  57. audit_log_string(ab, aa_audit_type[aad(sa)->type]);
  58. }
  59. if (aad(sa)->op) {
  60. audit_log_format(ab, " operation=");
  61. audit_log_string(ab, aad(sa)->op);
  62. }
  63. if (aad(sa)->info) {
  64. audit_log_format(ab, " info=");
  65. audit_log_string(ab, aad(sa)->info);
  66. if (aad(sa)->error)
  67. audit_log_format(ab, " error=%d", aad(sa)->error);
  68. }
  69. if (aad(sa)->label) {
  70. struct aa_label *label = aad(sa)->label;
  71. if (label_isprofile(label)) {
  72. struct aa_profile *profile = labels_profile(label);
  73. if (profile->ns != root_ns) {
  74. audit_log_format(ab, " namespace=");
  75. audit_log_untrustedstring(ab,
  76. profile->ns->base.hname);
  77. }
  78. audit_log_format(ab, " profile=");
  79. audit_log_untrustedstring(ab, profile->base.hname);
  80. } else {
  81. audit_log_format(ab, " label=");
  82. aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
  83. GFP_ATOMIC);
  84. }
  85. }
  86. if (aad(sa)->name) {
  87. audit_log_format(ab, " name=");
  88. audit_log_untrustedstring(ab, aad(sa)->name);
  89. }
  90. }
  91. /**
  92. * aa_audit_msg - Log a message to the audit subsystem
  93. * @sa: audit event structure (NOT NULL)
  94. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  95. */
  96. void aa_audit_msg(int type, struct common_audit_data *sa,
  97. void (*cb) (struct audit_buffer *, void *))
  98. {
  99. aad(sa)->type = type;
  100. common_lsm_audit(sa, audit_pre, cb);
  101. }
  102. /**
  103. * aa_audit - Log a profile based audit event to the audit subsystem
  104. * @type: audit type for the message
  105. * @profile: profile to check against (NOT NULL)
  106. * @sa: audit event (NOT NULL)
  107. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  108. *
  109. * Handle default message switching based off of audit mode flags
  110. *
  111. * Returns: error on failure
  112. */
  113. int aa_audit(int type, struct aa_profile *profile, struct common_audit_data *sa,
  114. void (*cb) (struct audit_buffer *, void *))
  115. {
  116. AA_BUG(!profile);
  117. if (type == AUDIT_APPARMOR_AUTO) {
  118. if (likely(!aad(sa)->error)) {
  119. if (AUDIT_MODE(profile) != AUDIT_ALL)
  120. return 0;
  121. type = AUDIT_APPARMOR_AUDIT;
  122. } else if (COMPLAIN_MODE(profile))
  123. type = AUDIT_APPARMOR_ALLOWED;
  124. else
  125. type = AUDIT_APPARMOR_DENIED;
  126. }
  127. if (AUDIT_MODE(profile) == AUDIT_QUIET ||
  128. (type == AUDIT_APPARMOR_DENIED &&
  129. AUDIT_MODE(profile) == AUDIT_QUIET))
  130. return aad(sa)->error;
  131. if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
  132. type = AUDIT_APPARMOR_KILL;
  133. aad(sa)->label = &profile->label;
  134. aa_audit_msg(type, sa, cb);
  135. if (aad(sa)->type == AUDIT_APPARMOR_KILL)
  136. (void)send_sig_info(SIGKILL, NULL,
  137. sa->type == LSM_AUDIT_DATA_TASK && sa->u.tsk ?
  138. sa->u.tsk : current);
  139. if (aad(sa)->type == AUDIT_APPARMOR_ALLOWED)
  140. return complain_error(aad(sa)->error);
  141. return aad(sa)->error;
  142. }