John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 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 | |
| 15 | #include <linux/audit.h> |
| 16 | #include <linux/socket.h> |
| 17 | |
| 18 | #include "include/apparmor.h" |
| 19 | #include "include/audit.h" |
| 20 | #include "include/policy.h" |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 21 | #include "include/policy_ns.h" |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 22 | |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 23 | |
Jan Engelhardt | 2d4cee7 | 2012-03-14 13:30:36 +0100 | [diff] [blame] | 24 | const char *const audit_mode_names[] = { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 25 | "normal", |
| 26 | "quiet_denied", |
| 27 | "quiet", |
| 28 | "noquiet", |
| 29 | "all" |
| 30 | }; |
| 31 | |
Jan Engelhardt | 2d4cee7 | 2012-03-14 13:30:36 +0100 | [diff] [blame] | 32 | static const char *const aa_audit_type[] = { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 33 | "AUDIT", |
| 34 | "ALLOWED", |
| 35 | "DENIED", |
| 36 | "HINT", |
| 37 | "STATUS", |
| 38 | "ERROR", |
John Johansen | b492d50 | 2013-02-18 16:13:34 -0800 | [diff] [blame] | 39 | "KILLED", |
John Johansen | ade3ddc | 2012-02-22 00:20:26 -0800 | [diff] [blame] | 40 | "AUTO" |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Currently AppArmor auditing is fed straight into the audit framework. |
| 45 | * |
| 46 | * TODO: |
| 47 | * netlink interface for complain mode |
| 48 | * user auditing, - send user auditing to netlink interface |
| 49 | * system control of whether user audit messages go to system log |
| 50 | */ |
| 51 | |
| 52 | /** |
| 53 | * audit_base - core AppArmor function. |
| 54 | * @ab: audit buffer to fill (NOT NULL) |
| 55 | * @ca: audit structure containing data to audit (NOT NULL) |
| 56 | * |
| 57 | * Record common AppArmor audit data from @sa |
| 58 | */ |
| 59 | static void audit_pre(struct audit_buffer *ab, void *ca) |
| 60 | { |
| 61 | struct common_audit_data *sa = ca; |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 62 | |
| 63 | if (aa_g_audit_header) { |
| 64 | audit_log_format(ab, "apparmor="); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 65 | audit_log_string(ab, aa_audit_type[sa->aad->type]); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 68 | if (sa->aad->op) { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 69 | audit_log_format(ab, " operation="); |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame^] | 70 | audit_log_string(ab, sa->aad->op); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 73 | if (sa->aad->info) { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 74 | audit_log_format(ab, " info="); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 75 | audit_log_string(ab, sa->aad->info); |
| 76 | if (sa->aad->error) |
| 77 | audit_log_format(ab, " error=%d", sa->aad->error); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 80 | if (sa->aad->profile) { |
| 81 | struct aa_profile *profile = sa->aad->profile; |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 82 | if (profile->ns != root_ns) { |
| 83 | audit_log_format(ab, " namespace="); |
| 84 | audit_log_untrustedstring(ab, profile->ns->base.hname); |
| 85 | } |
| 86 | audit_log_format(ab, " profile="); |
| 87 | audit_log_untrustedstring(ab, profile->base.hname); |
| 88 | } |
| 89 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 90 | if (sa->aad->name) { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 91 | audit_log_format(ab, " name="); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 92 | audit_log_untrustedstring(ab, sa->aad->name); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * aa_audit_msg - Log a message to the audit subsystem |
| 98 | * @sa: audit event structure (NOT NULL) |
| 99 | * @cb: optional callback fn for type specific fields (MAYBE NULL) |
| 100 | */ |
| 101 | void aa_audit_msg(int type, struct common_audit_data *sa, |
| 102 | void (*cb) (struct audit_buffer *, void *)) |
| 103 | { |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 104 | sa->aad->type = type; |
Linus Torvalds | b61c37f | 2012-04-02 15:48:12 -0700 | [diff] [blame] | 105 | common_lsm_audit(sa, audit_pre, cb); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * aa_audit - Log a profile based audit event to the audit subsystem |
| 110 | * @type: audit type for the message |
| 111 | * @profile: profile to check against (NOT NULL) |
| 112 | * @gfp: allocation flags to use |
| 113 | * @sa: audit event (NOT NULL) |
| 114 | * @cb: optional callback fn for type specific fields (MAYBE NULL) |
| 115 | * |
| 116 | * Handle default message switching based off of audit mode flags |
| 117 | * |
| 118 | * Returns: error on failure |
| 119 | */ |
| 120 | int aa_audit(int type, struct aa_profile *profile, gfp_t gfp, |
| 121 | struct common_audit_data *sa, |
| 122 | void (*cb) (struct audit_buffer *, void *)) |
| 123 | { |
| 124 | BUG_ON(!profile); |
| 125 | |
| 126 | if (type == AUDIT_APPARMOR_AUTO) { |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 127 | if (likely(!sa->aad->error)) { |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 128 | if (AUDIT_MODE(profile) != AUDIT_ALL) |
| 129 | return 0; |
| 130 | type = AUDIT_APPARMOR_AUDIT; |
| 131 | } else if (COMPLAIN_MODE(profile)) |
| 132 | type = AUDIT_APPARMOR_ALLOWED; |
| 133 | else |
| 134 | type = AUDIT_APPARMOR_DENIED; |
| 135 | } |
| 136 | if (AUDIT_MODE(profile) == AUDIT_QUIET || |
| 137 | (type == AUDIT_APPARMOR_DENIED && |
| 138 | AUDIT_MODE(profile) == AUDIT_QUIET)) |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 139 | return sa->aad->error; |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 140 | |
| 141 | if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED) |
| 142 | type = AUDIT_APPARMOR_KILL; |
| 143 | |
| 144 | if (!unconfined(profile)) |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 145 | sa->aad->profile = profile; |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 146 | |
| 147 | aa_audit_msg(type, sa, cb); |
| 148 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 149 | if (sa->aad->type == AUDIT_APPARMOR_KILL) |
Eric Paris | 0972c74 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 150 | (void)send_sig_info(SIGKILL, NULL, |
John Johansen | b6b1b81 | 2014-06-08 11:20:54 -0700 | [diff] [blame] | 151 | sa->type == LSM_AUDIT_DATA_TASK && sa->u.tsk ? |
| 152 | sa->u.tsk : current); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 153 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 154 | if (sa->aad->type == AUDIT_APPARMOR_ALLOWED) |
| 155 | return complain_error(sa->aad->error); |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 156 | |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 157 | return sa->aad->error; |
John Johansen | 67012e8 | 2010-07-29 14:47:58 -0700 | [diff] [blame] | 158 | } |