audit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. const char *op_table[] = {
  20. "null",
  21. "sysctl",
  22. "capable",
  23. "unlink",
  24. "mkdir",
  25. "rmdir",
  26. "mknod",
  27. "truncate",
  28. "link",
  29. "symlink",
  30. "rename_src",
  31. "rename_dest",
  32. "chmod",
  33. "chown",
  34. "getattr",
  35. "open",
  36. "file_perm",
  37. "file_lock",
  38. "file_mmap",
  39. "file_mprotect",
  40. "create",
  41. "post_create",
  42. "bind",
  43. "connect",
  44. "listen",
  45. "accept",
  46. "sendmsg",
  47. "recvmsg",
  48. "getsockname",
  49. "getpeername",
  50. "getsockopt",
  51. "setsockopt",
  52. "socket_shutdown",
  53. "ptrace",
  54. "exec",
  55. "change_hat",
  56. "change_profile",
  57. "change_onexec",
  58. "setprocattr",
  59. "setrlimit",
  60. "profile_replace",
  61. "profile_load",
  62. "profile_remove"
  63. };
  64. const char *audit_mode_names[] = {
  65. "normal",
  66. "quiet_denied",
  67. "quiet",
  68. "noquiet",
  69. "all"
  70. };
  71. static char *aa_audit_type[] = {
  72. "AUDIT",
  73. "ALLOWED",
  74. "DENIED",
  75. "HINT",
  76. "STATUS",
  77. "ERROR",
  78. "KILLED"
  79. };
  80. /*
  81. * Currently AppArmor auditing is fed straight into the audit framework.
  82. *
  83. * TODO:
  84. * netlink interface for complain mode
  85. * user auditing, - send user auditing to netlink interface
  86. * system control of whether user audit messages go to system log
  87. */
  88. /**
  89. * audit_base - core AppArmor function.
  90. * @ab: audit buffer to fill (NOT NULL)
  91. * @ca: audit structure containing data to audit (NOT NULL)
  92. *
  93. * Record common AppArmor audit data from @sa
  94. */
  95. static void audit_pre(struct audit_buffer *ab, void *ca)
  96. {
  97. struct common_audit_data *sa = ca;
  98. struct task_struct *tsk = sa->tsk ? sa->tsk : current;
  99. if (aa_g_audit_header) {
  100. audit_log_format(ab, "apparmor=");
  101. audit_log_string(ab, aa_audit_type[sa->aad.type]);
  102. }
  103. if (sa->aad.op) {
  104. audit_log_format(ab, " operation=");
  105. audit_log_string(ab, op_table[sa->aad.op]);
  106. }
  107. if (sa->aad.info) {
  108. audit_log_format(ab, " info=");
  109. audit_log_string(ab, sa->aad.info);
  110. if (sa->aad.error)
  111. audit_log_format(ab, " error=%d", sa->aad.error);
  112. }
  113. if (sa->aad.profile) {
  114. struct aa_profile *profile = sa->aad.profile;
  115. pid_t pid;
  116. rcu_read_lock();
  117. pid = tsk->real_parent->pid;
  118. rcu_read_unlock();
  119. audit_log_format(ab, " parent=%d", pid);
  120. if (profile->ns != root_ns) {
  121. audit_log_format(ab, " namespace=");
  122. audit_log_untrustedstring(ab, profile->ns->base.hname);
  123. }
  124. audit_log_format(ab, " profile=");
  125. audit_log_untrustedstring(ab, profile->base.hname);
  126. }
  127. if (sa->aad.name) {
  128. audit_log_format(ab, " name=");
  129. audit_log_untrustedstring(ab, sa->aad.name);
  130. }
  131. }
  132. /**
  133. * aa_audit_msg - Log a message to the audit subsystem
  134. * @sa: audit event structure (NOT NULL)
  135. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  136. */
  137. void aa_audit_msg(int type, struct common_audit_data *sa,
  138. void (*cb) (struct audit_buffer *, void *))
  139. {
  140. sa->aad.type = type;
  141. sa->lsm_pre_audit = audit_pre;
  142. sa->lsm_post_audit = cb;
  143. common_lsm_audit(sa);
  144. }
  145. /**
  146. * aa_audit - Log a profile based audit event to the audit subsystem
  147. * @type: audit type for the message
  148. * @profile: profile to check against (NOT NULL)
  149. * @gfp: allocation flags to use
  150. * @sa: audit event (NOT NULL)
  151. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  152. *
  153. * Handle default message switching based off of audit mode flags
  154. *
  155. * Returns: error on failure
  156. */
  157. int aa_audit(int type, struct aa_profile *profile, gfp_t gfp,
  158. struct common_audit_data *sa,
  159. void (*cb) (struct audit_buffer *, void *))
  160. {
  161. BUG_ON(!profile);
  162. if (type == AUDIT_APPARMOR_AUTO) {
  163. if (likely(!sa->aad.error)) {
  164. if (AUDIT_MODE(profile) != AUDIT_ALL)
  165. return 0;
  166. type = AUDIT_APPARMOR_AUDIT;
  167. } else if (COMPLAIN_MODE(profile))
  168. type = AUDIT_APPARMOR_ALLOWED;
  169. else
  170. type = AUDIT_APPARMOR_DENIED;
  171. }
  172. if (AUDIT_MODE(profile) == AUDIT_QUIET ||
  173. (type == AUDIT_APPARMOR_DENIED &&
  174. AUDIT_MODE(profile) == AUDIT_QUIET))
  175. return sa->aad.error;
  176. if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
  177. type = AUDIT_APPARMOR_KILL;
  178. if (!unconfined(profile))
  179. sa->aad.profile = profile;
  180. aa_audit_msg(type, sa, cb);
  181. if (sa->aad.type == AUDIT_APPARMOR_KILL)
  182. (void)send_sig_info(SIGKILL, NULL, sa->tsk ? sa->tsk : current);
  183. if (sa->aad.type == AUDIT_APPARMOR_ALLOWED)
  184. return complain_error(sa->aad.error);
  185. return sa->aad.error;
  186. }