audit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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)->profile) {
  70. struct aa_profile *profile = aad(sa)->profile;
  71. if (profile->ns != root_ns) {
  72. audit_log_format(ab, " namespace=");
  73. audit_log_untrustedstring(ab, profile->ns->base.hname);
  74. }
  75. audit_log_format(ab, " profile=");
  76. audit_log_untrustedstring(ab, profile->base.hname);
  77. }
  78. if (aad(sa)->name) {
  79. audit_log_format(ab, " name=");
  80. audit_log_untrustedstring(ab, aad(sa)->name);
  81. }
  82. }
  83. /**
  84. * aa_audit_msg - Log a message to the audit subsystem
  85. * @sa: audit event structure (NOT NULL)
  86. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  87. */
  88. void aa_audit_msg(int type, struct common_audit_data *sa,
  89. void (*cb) (struct audit_buffer *, void *))
  90. {
  91. aad(sa)->type = type;
  92. common_lsm_audit(sa, audit_pre, cb);
  93. }
  94. /**
  95. * aa_audit - Log a profile based audit event to the audit subsystem
  96. * @type: audit type for the message
  97. * @profile: profile to check against (NOT NULL)
  98. * @sa: audit event (NOT NULL)
  99. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  100. *
  101. * Handle default message switching based off of audit mode flags
  102. *
  103. * Returns: error on failure
  104. */
  105. int aa_audit(int type, struct aa_profile *profile, struct common_audit_data *sa,
  106. void (*cb) (struct audit_buffer *, void *))
  107. {
  108. AA_BUG(!profile);
  109. if (type == AUDIT_APPARMOR_AUTO) {
  110. if (likely(!aad(sa)->error)) {
  111. if (AUDIT_MODE(profile) != AUDIT_ALL)
  112. return 0;
  113. type = AUDIT_APPARMOR_AUDIT;
  114. } else if (COMPLAIN_MODE(profile))
  115. type = AUDIT_APPARMOR_ALLOWED;
  116. else
  117. type = AUDIT_APPARMOR_DENIED;
  118. }
  119. if (AUDIT_MODE(profile) == AUDIT_QUIET ||
  120. (type == AUDIT_APPARMOR_DENIED &&
  121. AUDIT_MODE(profile) == AUDIT_QUIET))
  122. return aad(sa)->error;
  123. if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
  124. type = AUDIT_APPARMOR_KILL;
  125. if (!unconfined(profile))
  126. aad(sa)->profile = profile;
  127. aa_audit_msg(type, sa, cb);
  128. if (aad(sa)->type == AUDIT_APPARMOR_KILL)
  129. (void)send_sig_info(SIGKILL, NULL,
  130. sa->type == LSM_AUDIT_DATA_TASK && sa->u.tsk ?
  131. sa->u.tsk : current);
  132. if (aad(sa)->type == AUDIT_APPARMOR_ALLOWED)
  133. return complain_error(aad(sa)->error);
  134. return aad(sa)->error;
  135. }