audit.c 4.6 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 *const 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 *const audit_mode_names[] = {
  65. "normal",
  66. "quiet_denied",
  67. "quiet",
  68. "noquiet",
  69. "all"
  70. };
  71. static const char *const aa_audit_type[] = {
  72. "AUDIT",
  73. "ALLOWED",
  74. "DENIED",
  75. "HINT",
  76. "STATUS",
  77. "ERROR",
  78. "KILLED"
  79. "AUTO"
  80. };
  81. /*
  82. * Currently AppArmor auditing is fed straight into the audit framework.
  83. *
  84. * TODO:
  85. * netlink interface for complain mode
  86. * user auditing, - send user auditing to netlink interface
  87. * system control of whether user audit messages go to system log
  88. */
  89. /**
  90. * audit_base - core AppArmor function.
  91. * @ab: audit buffer to fill (NOT NULL)
  92. * @ca: audit structure containing data to audit (NOT NULL)
  93. *
  94. * Record common AppArmor audit data from @sa
  95. */
  96. static void audit_pre(struct audit_buffer *ab, void *ca)
  97. {
  98. struct common_audit_data *sa = ca;
  99. struct task_struct *tsk = sa->tsk ? sa->tsk : current;
  100. if (aa_g_audit_header) {
  101. audit_log_format(ab, "apparmor=");
  102. audit_log_string(ab, aa_audit_type[sa->aad->type]);
  103. }
  104. if (sa->aad->op) {
  105. audit_log_format(ab, " operation=");
  106. audit_log_string(ab, op_table[sa->aad->op]);
  107. }
  108. if (sa->aad->info) {
  109. audit_log_format(ab, " info=");
  110. audit_log_string(ab, sa->aad->info);
  111. if (sa->aad->error)
  112. audit_log_format(ab, " error=%d", sa->aad->error);
  113. }
  114. if (sa->aad->profile) {
  115. struct aa_profile *profile = sa->aad->profile;
  116. pid_t pid;
  117. rcu_read_lock();
  118. pid = rcu_dereference(tsk->real_parent)->pid;
  119. rcu_read_unlock();
  120. audit_log_format(ab, " parent=%d", pid);
  121. if (profile->ns != root_ns) {
  122. audit_log_format(ab, " namespace=");
  123. audit_log_untrustedstring(ab, profile->ns->base.hname);
  124. }
  125. audit_log_format(ab, " profile=");
  126. audit_log_untrustedstring(ab, profile->base.hname);
  127. }
  128. if (sa->aad->name) {
  129. audit_log_format(ab, " name=");
  130. audit_log_untrustedstring(ab, sa->aad->name);
  131. }
  132. }
  133. /**
  134. * aa_audit_msg - Log a message to the audit subsystem
  135. * @sa: audit event structure (NOT NULL)
  136. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  137. */
  138. void aa_audit_msg(int type, struct common_audit_data *sa,
  139. void (*cb) (struct audit_buffer *, void *))
  140. {
  141. sa->aad->type = type;
  142. common_lsm_audit(sa, audit_pre, cb);
  143. }
  144. /**
  145. * aa_audit - Log a profile based audit event to the audit subsystem
  146. * @type: audit type for the message
  147. * @profile: profile to check against (NOT NULL)
  148. * @gfp: allocation flags to use
  149. * @sa: audit event (NOT NULL)
  150. * @cb: optional callback fn for type specific fields (MAYBE NULL)
  151. *
  152. * Handle default message switching based off of audit mode flags
  153. *
  154. * Returns: error on failure
  155. */
  156. int aa_audit(int type, struct aa_profile *profile, gfp_t gfp,
  157. struct common_audit_data *sa,
  158. void (*cb) (struct audit_buffer *, void *))
  159. {
  160. BUG_ON(!profile);
  161. if (type == AUDIT_APPARMOR_AUTO) {
  162. if (likely(!sa->aad->error)) {
  163. if (AUDIT_MODE(profile) != AUDIT_ALL)
  164. return 0;
  165. type = AUDIT_APPARMOR_AUDIT;
  166. } else if (COMPLAIN_MODE(profile))
  167. type = AUDIT_APPARMOR_ALLOWED;
  168. else
  169. type = AUDIT_APPARMOR_DENIED;
  170. }
  171. if (AUDIT_MODE(profile) == AUDIT_QUIET ||
  172. (type == AUDIT_APPARMOR_DENIED &&
  173. AUDIT_MODE(profile) == AUDIT_QUIET))
  174. return sa->aad->error;
  175. if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
  176. type = AUDIT_APPARMOR_KILL;
  177. if (!unconfined(profile))
  178. sa->aad->profile = profile;
  179. aa_audit_msg(type, sa, cb);
  180. if (sa->aad->type == AUDIT_APPARMOR_KILL)
  181. (void)send_sig_info(SIGKILL, NULL, sa->tsk ? sa->tsk : current);
  182. if (sa->aad->type == AUDIT_APPARMOR_ALLOWED)
  183. return complain_error(sa->aad->error);
  184. return sa->aad->error;
  185. }