| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 1 | /* audit.c -- Auditing support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. |
| 3 | * System-call specific features have moved to auditsc.c |
| 4 | * |
Steve Grubb | 6a01b07f | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 5 | * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 23 | * |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 24 | * Goals: 1) Integrate fully with Security Modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * 2) Minimal run-time overhead: |
| 26 | * a) Minimal when syscall auditing is disabled (audit_enable=0). |
| 27 | * b) Small when syscall auditing is enabled and no audit record |
| 28 | * is generated (defer as much work as possible to record |
| 29 | * generation time): |
| 30 | * i) context is allocated, |
| 31 | * ii) names from getname are stored without a copy, and |
| 32 | * iii) inode information stored from path_lookup. |
| 33 | * 3) Ability to disable syscall auditing at boot time (audit=0). |
| 34 | * 4) Usable by other parts of the kernel (if audit_log* is called, |
| 35 | * then a syscall record will be generated automatically for the |
| 36 | * current syscall). |
| 37 | * 5) Netlink interface to user-space. |
| 38 | * 6) Support low-overhead kernel-based filtering to minimize the |
| 39 | * information that must be passed to user-space. |
| 40 | * |
| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 41 | * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
| 43 | |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 44 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 45 | |
Davidlohr Bueso | 5b28255 | 2015-02-22 18:20:09 -0800 | [diff] [blame] | 46 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/init.h> |
Paul McQuade | 7153e40 | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 48 | #include <linux/types.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 49 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 51 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 53 | #include <linux/err.h> |
| 54 | #include <linux/kthread.h> |
Richard Guy Briggs | 46e959e | 2013-05-03 14:03:50 -0400 | [diff] [blame] | 55 | #include <linux/kernel.h> |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 56 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #include <linux/audit.h> |
| 59 | |
| 60 | #include <net/sock.h> |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 61 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include <linux/skbuff.h> |
Mr Dash Four | 131ad62 | 2011-06-30 13:31:57 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_SECURITY |
| 64 | #include <linux/security.h> |
| 65 | #endif |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 66 | #include <linux/freezer.h> |
Eric W. Biederman | 34e36d8e | 2012-09-10 23:20:20 -0700 | [diff] [blame] | 67 | #include <linux/pid_namespace.h> |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 68 | #include <net/netns/generic.h> |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 69 | |
| 70 | #include "audit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 72 | /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * (Initialization happens after skb_init is called.) */ |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 74 | #define AUDIT_DISABLED -1 |
| 75 | #define AUDIT_UNINITIALIZED 0 |
| 76 | #define AUDIT_INITIALIZED 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static int audit_initialized; |
| 78 | |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 79 | #define AUDIT_OFF 0 |
| 80 | #define AUDIT_ON 1 |
| 81 | #define AUDIT_LOCKED 2 |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 82 | u32 audit_enabled; |
| 83 | u32 audit_ever_enabled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Jan Engelhardt | ae9d67af | 2011-01-18 06:48:12 +0100 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(audit_enabled); |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* Default state when kernel boots without any parameters. */ |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 88 | static u32 audit_default; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* If auditing cannot proceed, audit_failure selects what happens. */ |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 91 | static u32 audit_failure = AUDIT_FAIL_PRINTK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Pavel Emelyanov | 75c0371 | 2008-03-20 15:39:41 -0700 | [diff] [blame] | 93 | /* |
| 94 | * If audit records are to be written to the netlink socket, audit_pid |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 95 | * contains the pid of the auditd process and audit_nlk_portid contains |
| 96 | * the portid to use to send netlink messages to that process. |
Pavel Emelyanov | 75c0371 | 2008-03-20 15:39:41 -0700 | [diff] [blame] | 97 | */ |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 98 | int audit_pid; |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 99 | static __u32 audit_nlk_portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 101 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * to that number per second. This prevents DoS attacks, but results in |
| 103 | * audit records being dropped. */ |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 104 | static u32 audit_rate_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Richard Guy Briggs | 40c0775 | 2013-10-22 13:28:49 -0400 | [diff] [blame] | 106 | /* Number of outstanding audit_buffers allowed. |
| 107 | * When set to zero, this means unlimited. */ |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 108 | static u32 audit_backlog_limit = 64; |
Richard Guy Briggs | e789e56 | 2013-09-12 23:03:51 -0400 | [diff] [blame] | 109 | #define AUDIT_BACKLOG_WAIT_TIME (60 * HZ) |
Richard Guy Briggs | a77ed4e | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 110 | static u32 audit_backlog_wait_time_master = AUDIT_BACKLOG_WAIT_TIME; |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 111 | static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 113 | /* The identity of the user shutting down the audit system. */ |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 114 | kuid_t audit_sig_uid = INVALID_UID; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 115 | pid_t audit_sig_pid = -1; |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 116 | u32 audit_sig_sid = 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* Records can be lost in several ways: |
| 119 | 0) [suppressed in audit_alloc] |
| 120 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] |
| 121 | 2) out of memory in audit_log_move [alloc_skb] |
| 122 | 3) suppressed due to audit_rate_limit |
| 123 | 4) suppressed due to audit_backlog_limit |
| 124 | */ |
| 125 | static atomic_t audit_lost = ATOMIC_INIT(0); |
| 126 | |
| 127 | /* The netlink socket. */ |
| 128 | static struct sock *audit_sock; |
Richard Guy Briggs | c0a8d9b | 2014-05-26 10:59:28 -0400 | [diff] [blame] | 129 | static int audit_net_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 131 | /* Hash for inode-based rules */ |
| 132 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; |
| 133 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 134 | /* The audit_freelist is a list of pre-allocated audit buffers (if more |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
| 136 | * being placed on the freelist). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static DEFINE_SPINLOCK(audit_freelist_lock); |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 138 | static int audit_freelist_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static LIST_HEAD(audit_freelist); |
| 140 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 141 | /* queue msgs to send via kauditd_task */ |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 142 | static struct sk_buff_head audit_queue; |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 143 | /* queue msgs due to temporary unicast send problems */ |
| 144 | static struct sk_buff_head audit_retry_queue; |
| 145 | /* queue msgs waiting for new auditd connection */ |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 146 | static struct sk_buff_head audit_hold_queue; |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 147 | |
| 148 | /* queue servicing thread */ |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 149 | static struct task_struct *kauditd_task; |
| 150 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 151 | |
| 152 | /* waitqueue for callers who are blocked on the audit backlog */ |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 153 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 155 | static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION, |
| 156 | .mask = -1, |
| 157 | .features = 0, |
| 158 | .lock = 0,}; |
| 159 | |
Eric Paris | 21b85c3 | 2013-05-23 14:26:00 -0400 | [diff] [blame] | 160 | static char *audit_feature_names[2] = { |
Eric Paris | d040e5a | 2013-05-24 09:18:04 -0400 | [diff] [blame] | 161 | "only_unset_loginuid", |
Eric Paris | 21b85c3 | 2013-05-23 14:26:00 -0400 | [diff] [blame] | 162 | "loginuid_immutable", |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 166 | /* Serialize requests from userspace. */ |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 167 | DEFINE_MUTEX(audit_cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting |
| 170 | * audit records. Since printk uses a 1024 byte buffer, this buffer |
| 171 | * should be at least that large. */ |
| 172 | #define AUDIT_BUFSIZ 1024 |
| 173 | |
| 174 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the |
| 175 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ |
| 176 | #define AUDIT_MAXFREE (2*NR_CPUS) |
| 177 | |
| 178 | /* The audit_buffer is used when formatting an audit record. The caller |
| 179 | * locks briefly to get the record off the freelist or to allocate the |
| 180 | * buffer, and locks briefly to send the buffer to the netlink layer or |
| 181 | * to place it on a transmit queue. Multiple audit_buffers can be in |
| 182 | * use simultaneously. */ |
| 183 | struct audit_buffer { |
| 184 | struct list_head list; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 185 | struct sk_buff *skb; /* formatted skb ready to send */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | struct audit_context *ctx; /* NULL or associated context */ |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 187 | gfp_t gfp_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 190 | struct audit_reply { |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 191 | __u32 portid; |
Eric W. Biederman | 638a0fd | 2014-02-28 10:49:05 -0800 | [diff] [blame] | 192 | struct net *net; |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 193 | struct sk_buff *skb; |
| 194 | }; |
| 195 | |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 196 | static void audit_set_portid(struct audit_buffer *ab, __u32 portid) |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 197 | { |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 198 | if (ab) { |
| 199 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 200 | nlh->nlmsg_pid = portid; |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 201 | } |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 204 | void audit_panic(const char *message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 206 | switch (audit_failure) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | case AUDIT_FAIL_SILENT: |
| 208 | break; |
| 209 | case AUDIT_FAIL_PRINTK: |
Eric Paris | 320f1b1 | 2008-01-23 22:55:05 -0500 | [diff] [blame] | 210 | if (printk_ratelimit()) |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 211 | pr_err("%s\n", message); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | break; |
| 213 | case AUDIT_FAIL_PANIC: |
Eric Paris | b29ee87 | 2008-02-21 15:53:05 -0500 | [diff] [blame] | 214 | /* test audit_pid since printk is always losey, why bother? */ |
| 215 | if (audit_pid) |
| 216 | panic("audit: %s\n", message); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static inline int audit_rate_check(void) |
| 222 | { |
| 223 | static unsigned long last_check = 0; |
| 224 | static int messages = 0; |
| 225 | static DEFINE_SPINLOCK(lock); |
| 226 | unsigned long flags; |
| 227 | unsigned long now; |
| 228 | unsigned long elapsed; |
| 229 | int retval = 0; |
| 230 | |
| 231 | if (!audit_rate_limit) return 1; |
| 232 | |
| 233 | spin_lock_irqsave(&lock, flags); |
| 234 | if (++messages < audit_rate_limit) { |
| 235 | retval = 1; |
| 236 | } else { |
| 237 | now = jiffies; |
| 238 | elapsed = now - last_check; |
| 239 | if (elapsed > HZ) { |
| 240 | last_check = now; |
| 241 | messages = 0; |
| 242 | retval = 1; |
| 243 | } |
| 244 | } |
| 245 | spin_unlock_irqrestore(&lock, flags); |
| 246 | |
| 247 | return retval; |
| 248 | } |
| 249 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 250 | /** |
| 251 | * audit_log_lost - conditionally log lost audit message event |
| 252 | * @message: the message stating reason for lost audit message |
| 253 | * |
| 254 | * Emit at least 1 message per second, even if audit_rate_check is |
| 255 | * throttling. |
| 256 | * Always increment the lost messages counter. |
| 257 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | void audit_log_lost(const char *message) |
| 259 | { |
| 260 | static unsigned long last_msg = 0; |
| 261 | static DEFINE_SPINLOCK(lock); |
| 262 | unsigned long flags; |
| 263 | unsigned long now; |
| 264 | int print; |
| 265 | |
| 266 | atomic_inc(&audit_lost); |
| 267 | |
| 268 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); |
| 269 | |
| 270 | if (!print) { |
| 271 | spin_lock_irqsave(&lock, flags); |
| 272 | now = jiffies; |
| 273 | if (now - last_msg > HZ) { |
| 274 | print = 1; |
| 275 | last_msg = now; |
| 276 | } |
| 277 | spin_unlock_irqrestore(&lock, flags); |
| 278 | } |
| 279 | |
| 280 | if (print) { |
Eric Paris | 320f1b1 | 2008-01-23 22:55:05 -0500 | [diff] [blame] | 281 | if (printk_ratelimit()) |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 282 | pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n", |
Eric Paris | 320f1b1 | 2008-01-23 22:55:05 -0500 | [diff] [blame] | 283 | atomic_read(&audit_lost), |
| 284 | audit_rate_limit, |
| 285 | audit_backlog_limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | audit_panic(message); |
| 287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 290 | static int audit_log_config_change(char *function_name, u32 new, u32 old, |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 291 | int allow_changes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 293 | struct audit_buffer *ab; |
| 294 | int rc = 0; |
Steve Grubb | 6a01b07f | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 295 | |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 296 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 297 | if (unlikely(!ab)) |
| 298 | return rc; |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 299 | audit_log_format(ab, "%s=%u old=%u", function_name, new, old); |
Eric Paris | 4d3fb70 | 2013-04-30 09:53:34 -0400 | [diff] [blame] | 300 | audit_log_session_info(ab); |
Eric Paris | b122c37 | 2013-04-19 15:00:33 -0400 | [diff] [blame] | 301 | rc = audit_log_task_context(ab); |
| 302 | if (rc) |
| 303 | allow_changes = 0; /* Something weird, deny request */ |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 304 | audit_log_format(ab, " res=%d", allow_changes); |
| 305 | audit_log_end(ab); |
| 306 | return rc; |
| 307 | } |
| 308 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 309 | static int audit_do_config_change(char *function_name, u32 *to_change, u32 new) |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 310 | { |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 311 | int allow_changes, rc = 0; |
| 312 | u32 old = *to_change; |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 313 | |
| 314 | /* check if we are locked */ |
| 315 | if (audit_enabled == AUDIT_LOCKED) |
| 316 | allow_changes = 0; |
| 317 | else |
| 318 | allow_changes = 1; |
| 319 | |
| 320 | if (audit_enabled != AUDIT_OFF) { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 321 | rc = audit_log_config_change(function_name, new, old, allow_changes); |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 322 | if (rc) |
| 323 | allow_changes = 0; |
| 324 | } |
Steve Grubb | 6a01b07f | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 325 | |
| 326 | /* If we are allowed, make the change */ |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 327 | if (allow_changes == 1) |
| 328 | *to_change = new; |
Steve Grubb | 6a01b07f | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 329 | /* Not allowed, update reason */ |
| 330 | else if (rc == 0) |
| 331 | rc = -EPERM; |
| 332 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 335 | static int audit_set_rate_limit(u32 limit) |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 336 | { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 337 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit); |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 338 | } |
| 339 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 340 | static int audit_set_backlog_limit(u32 limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 342 | return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 345 | static int audit_set_backlog_wait_time(u32 timeout) |
Richard Guy Briggs | 51cc83f | 2013-09-18 11:55:12 -0400 | [diff] [blame] | 346 | { |
| 347 | return audit_do_config_change("audit_backlog_wait_time", |
Richard Guy Briggs | a77ed4e | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 348 | &audit_backlog_wait_time_master, timeout); |
Richard Guy Briggs | 51cc83f | 2013-09-18 11:55:12 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 351 | static int audit_set_enabled(u32 state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Eric Paris | b593d38 | 2008-01-08 17:38:31 -0500 | [diff] [blame] | 353 | int rc; |
Pranith Kumar | 724e7bf | 2015-03-11 14:08:19 -0400 | [diff] [blame] | 354 | if (state > AUDIT_LOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return -EINVAL; |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 356 | |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 357 | rc = audit_do_config_change("audit_enabled", &audit_enabled, state); |
Eric Paris | b593d38 | 2008-01-08 17:38:31 -0500 | [diff] [blame] | 358 | if (!rc) |
| 359 | audit_ever_enabled |= !!state; |
| 360 | |
| 361 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 364 | static int audit_set_failure(u32 state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (state != AUDIT_FAIL_SILENT |
| 367 | && state != AUDIT_FAIL_PRINTK |
| 368 | && state != AUDIT_FAIL_PANIC) |
| 369 | return -EINVAL; |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 370 | |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 371 | return audit_do_config_change("audit_failure", &audit_failure, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 374 | /* |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 375 | * For one reason or another this nlh isn't getting delivered to the userspace |
| 376 | * audit daemon, just send it to printk. |
| 377 | */ |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 378 | static void kauditd_printk_skb(struct sk_buff *skb) |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 379 | { |
| 380 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 381 | char *data = nlmsg_data(nlh); |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 382 | |
| 383 | if (nlh->nlmsg_type != AUDIT_EOE) { |
| 384 | if (printk_ratelimit()) |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 385 | pr_notice("type=%d %s\n", nlh->nlmsg_type, data); |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 386 | else |
Josh Boyer | f128352 | 2014-03-05 16:29:55 -0500 | [diff] [blame] | 387 | audit_log_lost("printk limit exceeded"); |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 388 | } |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 391 | /** |
| 392 | * kauditd_hold_skb - Queue an audit record, waiting for auditd |
| 393 | * @skb: audit record |
| 394 | * |
| 395 | * Description: |
| 396 | * Queue the audit record, waiting for an instance of auditd. When this |
| 397 | * function is called we haven't given up yet on sending the record, but things |
| 398 | * are not looking good. The first thing we want to do is try to write the |
| 399 | * record via printk and then see if we want to try and hold on to the record |
| 400 | * and queue it, if we have room. If we want to hold on to the record, but we |
| 401 | * don't have room, record a record lost message. |
| 402 | */ |
| 403 | static void kauditd_hold_skb(struct sk_buff *skb) |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 404 | { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 405 | /* at this point it is uncertain if we will ever send this to auditd so |
| 406 | * try to send the message via printk before we go any further */ |
| 407 | kauditd_printk_skb(skb); |
Richard Guy Briggs | 32a1dba | 2015-11-04 08:23:50 -0500 | [diff] [blame] | 408 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 409 | /* can we just silently drop the message? */ |
| 410 | if (!audit_default) { |
| 411 | kfree_skb(skb); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | /* if we have room, queue the message */ |
| 416 | if (!audit_backlog_limit || |
| 417 | skb_queue_len(&audit_hold_queue) < audit_backlog_limit) { |
| 418 | skb_queue_tail(&audit_hold_queue, skb); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | /* we have no other options - drop the message */ |
| 423 | audit_log_lost("kauditd hold queue overflow"); |
| 424 | kfree_skb(skb); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd |
| 429 | * @skb: audit record |
| 430 | * |
| 431 | * Description: |
| 432 | * Not as serious as kauditd_hold_skb() as we still have a connected auditd, |
| 433 | * but for some reason we are having problems sending it audit records so |
| 434 | * queue the given record and attempt to resend. |
| 435 | */ |
| 436 | static void kauditd_retry_skb(struct sk_buff *skb) |
| 437 | { |
| 438 | /* NOTE: because records should only live in the retry queue for a |
| 439 | * short period of time, before either being sent or moved to the hold |
| 440 | * queue, we don't currently enforce a limit on this queue */ |
| 441 | skb_queue_tail(&audit_retry_queue, skb); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * auditd_reset - Disconnect the auditd connection |
| 446 | * |
| 447 | * Description: |
| 448 | * Break the auditd/kauditd connection and move all the records in the retry |
| 449 | * queue into the hold queue in case auditd reconnects. |
| 450 | */ |
| 451 | static void auditd_reset(void) |
| 452 | { |
| 453 | struct sk_buff *skb; |
| 454 | |
| 455 | /* break the connection */ |
| 456 | audit_pid = 0; |
| 457 | audit_sock = NULL; |
| 458 | |
| 459 | /* flush all of the retry queue to the hold queue */ |
| 460 | while ((skb = skb_dequeue(&audit_retry_queue))) |
| 461 | kauditd_hold_skb(skb); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * kauditd_send_unicast_skb - Send a record via unicast to auditd |
| 466 | * @skb: audit record |
| 467 | */ |
| 468 | static int kauditd_send_unicast_skb(struct sk_buff *skb) |
| 469 | { |
| 470 | int rc; |
| 471 | |
| 472 | /* get an extra skb reference in case we fail to send */ |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 473 | skb_get(skb); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 474 | rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); |
| 475 | if (rc >= 0) { |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 476 | consume_skb(skb); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 477 | rc = 0; |
| 478 | } |
| 479 | |
| 480 | return rc; |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Richard Guy Briggs | b551d1d | 2013-01-24 13:15:10 -0500 | [diff] [blame] | 483 | /* |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 484 | * kauditd_send_multicast_skb - Send a record to any multicast listeners |
| 485 | * @skb: audit record |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 486 | * |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 487 | * Description: |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 488 | * This function doesn't consume an skb as might be expected since it has to |
| 489 | * copy it anyways. |
| 490 | */ |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 491 | static void kauditd_send_multicast_skb(struct sk_buff *skb) |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 492 | { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 493 | struct sk_buff *copy; |
| 494 | struct audit_net *aunet = net_generic(&init_net, audit_net_id); |
| 495 | struct sock *sock = aunet->nlsk; |
| 496 | struct nlmsghdr *nlh; |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 497 | |
Richard Guy Briggs | 7f74ecd | 2014-04-22 21:31:58 -0400 | [diff] [blame] | 498 | if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG)) |
| 499 | return; |
| 500 | |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 501 | /* |
| 502 | * The seemingly wasteful skb_copy() rather than bumping the refcount |
| 503 | * using skb_get() is necessary because non-standard mods are made to |
| 504 | * the skb by the original kaudit unicast socket send routine. The |
| 505 | * existing auditd daemon assumes this breakage. Fixing this would |
| 506 | * require co-ordinating a change in the established protocol between |
| 507 | * the kaudit kernel subsystem and the auditd userspace code. There is |
| 508 | * no reason for new multicast clients to continue with this |
| 509 | * non-compliance. |
| 510 | */ |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 511 | copy = skb_copy(skb, GFP_KERNEL); |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 512 | if (!copy) |
| 513 | return; |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 514 | nlh = nlmsg_hdr(copy); |
| 515 | nlh->nlmsg_len = skb->len; |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 516 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 517 | nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL); |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 520 | /** |
| 521 | * kauditd_wake_condition - Return true when it is time to wake kauditd_thread |
Richard Guy Briggs | b551d1d | 2013-01-24 13:15:10 -0500 | [diff] [blame] | 522 | * |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 523 | * Description: |
| 524 | * This function is for use by the wait_event_freezable() call in |
| 525 | * kauditd_thread(). |
Richard Guy Briggs | b551d1d | 2013-01-24 13:15:10 -0500 | [diff] [blame] | 526 | */ |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 527 | static int kauditd_wake_condition(void) |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 528 | { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 529 | static int pid_last = 0; |
| 530 | int rc; |
| 531 | int pid = audit_pid; |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 532 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 533 | /* wake on new messages or a change in the connected auditd */ |
| 534 | rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last); |
| 535 | if (rc) |
| 536 | pid_last = pid; |
Richard Guy Briggs | b551d1d | 2013-01-24 13:15:10 -0500 | [diff] [blame] | 537 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 538 | return rc; |
Richard Guy Briggs | b551d1d | 2013-01-24 13:15:10 -0500 | [diff] [blame] | 539 | } |
| 540 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 541 | static int kauditd_thread(void *dummy) |
| 542 | { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 543 | int rc; |
| 544 | int auditd = 0; |
| 545 | int reschedule = 0; |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 546 | struct sk_buff *skb; |
| 547 | struct nlmsghdr *nlh; |
| 548 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 549 | #define UNICAST_RETRIES 5 |
| 550 | #define AUDITD_BAD(x,y) \ |
| 551 | ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES) |
| 552 | |
| 553 | /* NOTE: we do invalidate the auditd connection flag on any sending |
| 554 | * errors, but we only "restore" the connection flag at specific places |
| 555 | * in the loop in order to help ensure proper ordering of audit |
| 556 | * records */ |
| 557 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 558 | set_freezable(); |
Andrew Morton | 4899b8b | 2006-10-06 00:43:48 -0700 | [diff] [blame] | 559 | while (!kthread_should_stop()) { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 560 | /* NOTE: possible area for future improvement is to look at |
| 561 | * the hold and retry queues, since only this thread |
| 562 | * has access to these queues we might be able to do |
| 563 | * our own queuing and skip some/all of the locking */ |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 564 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 565 | /* NOTE: it might be a fun experiment to split the hold and |
| 566 | * retry queue handling to another thread, but the |
| 567 | * synchronization issues and other overhead might kill |
| 568 | * any performance gains */ |
| 569 | |
| 570 | /* attempt to flush the hold queue */ |
| 571 | while (auditd && (skb = skb_dequeue(&audit_hold_queue))) { |
| 572 | rc = kauditd_send_unicast_skb(skb); |
| 573 | if (rc) { |
| 574 | /* requeue to the same spot */ |
| 575 | skb_queue_head(&audit_hold_queue, skb); |
| 576 | |
| 577 | auditd = 0; |
| 578 | if (AUDITD_BAD(rc, reschedule)) { |
| 579 | auditd_reset(); |
| 580 | reschedule = 0; |
| 581 | } |
| 582 | } else |
| 583 | /* we were able to send successfully */ |
| 584 | reschedule = 0; |
| 585 | } |
| 586 | |
| 587 | /* attempt to flush the retry queue */ |
| 588 | while (auditd && (skb = skb_dequeue(&audit_retry_queue))) { |
| 589 | rc = kauditd_send_unicast_skb(skb); |
| 590 | if (rc) { |
| 591 | auditd = 0; |
| 592 | if (AUDITD_BAD(rc, reschedule)) { |
| 593 | kauditd_hold_skb(skb); |
| 594 | auditd_reset(); |
| 595 | reschedule = 0; |
| 596 | } else |
| 597 | /* temporary problem (we hope), queue |
| 598 | * to the same spot and retry */ |
| 599 | skb_queue_head(&audit_retry_queue, skb); |
| 600 | } else |
| 601 | /* we were able to send successfully */ |
| 602 | reschedule = 0; |
| 603 | } |
| 604 | |
| 605 | /* standard queue processing, try to be as quick as possible */ |
| 606 | quick_loop: |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 607 | skb = skb_dequeue(&audit_queue); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 608 | if (skb) { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 609 | /* setup the netlink header, see the comments in |
| 610 | * kauditd_send_multicast_skb() for length quirks */ |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 611 | nlh = nlmsg_hdr(skb); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 612 | nlh->nlmsg_len = skb->len - NLMSG_HDRLEN; |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 613 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 614 | /* attempt to send to any multicast listeners */ |
| 615 | kauditd_send_multicast_skb(skb); |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 616 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 617 | /* attempt to send to auditd, queue on failure */ |
| 618 | if (auditd) { |
| 619 | rc = kauditd_send_unicast_skb(skb); |
| 620 | if (rc) { |
| 621 | auditd = 0; |
| 622 | if (AUDITD_BAD(rc, reschedule)) { |
| 623 | auditd_reset(); |
| 624 | reschedule = 0; |
| 625 | } |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 626 | |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 627 | /* move to the retry queue */ |
| 628 | kauditd_retry_skb(skb); |
| 629 | } else |
| 630 | /* everything is working so go fast! */ |
| 631 | goto quick_loop; |
| 632 | } else if (reschedule) |
| 633 | /* we are currently having problems, move to |
| 634 | * the retry queue */ |
| 635 | kauditd_retry_skb(skb); |
Eric Paris | 038cbcf | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 636 | else |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 637 | /* dump the message via printk and hold it */ |
| 638 | kauditd_hold_skb(skb); |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 639 | } else { |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 640 | /* we have flushed the backlog so wake everyone */ |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 641 | wake_up(&audit_backlog_wait); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 642 | |
| 643 | /* if everything is okay with auditd (if present), go |
| 644 | * to sleep until there is something new in the queue |
| 645 | * or we have a change in the connected auditd; |
| 646 | * otherwise simply reschedule to give things a chance |
| 647 | * to recover */ |
| 648 | if (reschedule) { |
| 649 | set_current_state(TASK_INTERRUPTIBLE); |
| 650 | schedule(); |
| 651 | } else |
| 652 | wait_event_freezable(kauditd_wait, |
| 653 | kauditd_wake_condition()); |
| 654 | |
| 655 | /* update the auditd connection status */ |
| 656 | auditd = (audit_pid ? 1 : 0); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 657 | } |
| 658 | } |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 659 | |
Andrew Morton | 4899b8b | 2006-10-06 00:43:48 -0700 | [diff] [blame] | 660 | return 0; |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 663 | int audit_send_list(void *_dest) |
| 664 | { |
| 665 | struct audit_netlink_list *dest = _dest; |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 666 | struct sk_buff *skb; |
Eric W. Biederman | 48095d9 | 2014-02-03 17:25:33 -0800 | [diff] [blame] | 667 | struct net *net = dest->net; |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 668 | struct audit_net *aunet = net_generic(net, audit_net_id); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 669 | |
| 670 | /* wait for parent to finish and send an ACK */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 671 | mutex_lock(&audit_cmd_mutex); |
| 672 | mutex_unlock(&audit_cmd_mutex); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 673 | |
| 674 | while ((skb = __skb_dequeue(&dest->q)) != NULL) |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 675 | netlink_unicast(aunet->nlsk, skb, dest->portid, 0); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 676 | |
Eric W. Biederman | 48095d9 | 2014-02-03 17:25:33 -0800 | [diff] [blame] | 677 | put_net(net); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 678 | kfree(dest); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 683 | struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done, |
Stephen Hemminger | b8800aa | 2010-10-20 17:23:50 -0700 | [diff] [blame] | 684 | int multi, const void *payload, int size) |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 685 | { |
| 686 | struct sk_buff *skb; |
| 687 | struct nlmsghdr *nlh; |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 688 | void *data; |
| 689 | int flags = multi ? NLM_F_MULTI : 0; |
| 690 | int t = done ? NLMSG_DONE : type; |
| 691 | |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 692 | skb = nlmsg_new(size, GFP_KERNEL); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 693 | if (!skb) |
| 694 | return NULL; |
| 695 | |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 696 | nlh = nlmsg_put(skb, portid, seq, t, size, flags); |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 697 | if (!nlh) |
| 698 | goto out_kfree_skb; |
| 699 | data = nlmsg_data(nlh); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 700 | memcpy(data, payload, size); |
| 701 | return skb; |
| 702 | |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 703 | out_kfree_skb: |
| 704 | kfree_skb(skb); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 705 | return NULL; |
| 706 | } |
| 707 | |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 708 | static int audit_send_reply_thread(void *arg) |
| 709 | { |
| 710 | struct audit_reply *reply = (struct audit_reply *)arg; |
Eric W. Biederman | 48095d9 | 2014-02-03 17:25:33 -0800 | [diff] [blame] | 711 | struct net *net = reply->net; |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 712 | struct audit_net *aunet = net_generic(net, audit_net_id); |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 713 | |
| 714 | mutex_lock(&audit_cmd_mutex); |
| 715 | mutex_unlock(&audit_cmd_mutex); |
| 716 | |
| 717 | /* Ignore failure. It'll only happen if the sender goes away, |
| 718 | because our timeout is set to infinite. */ |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 719 | netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0); |
Eric W. Biederman | 48095d9 | 2014-02-03 17:25:33 -0800 | [diff] [blame] | 720 | put_net(net); |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 721 | kfree(reply); |
| 722 | return 0; |
| 723 | } |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 724 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 725 | /** |
| 726 | * audit_send_reply - send an audit reply message via netlink |
Eric W. Biederman | d211f177 | 2014-03-08 15:31:54 -0800 | [diff] [blame] | 727 | * @request_skb: skb of request we are replying to (used to target the reply) |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 728 | * @seq: sequence number |
| 729 | * @type: audit message type |
| 730 | * @done: done (last) flag |
| 731 | * @multi: multi-part message flag |
| 732 | * @payload: payload data |
| 733 | * @size: payload size |
| 734 | * |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 735 | * Allocates an skb, builds the netlink message, and sends it to the port id. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 736 | * No failure notifications. |
| 737 | */ |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 738 | static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done, |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 739 | int multi, const void *payload, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 741 | u32 portid = NETLINK_CB(request_skb).portid; |
| 742 | struct net *net = sock_net(NETLINK_CB(request_skb).sk); |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 743 | struct sk_buff *skb; |
| 744 | struct task_struct *tsk; |
| 745 | struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), |
| 746 | GFP_KERNEL); |
| 747 | |
| 748 | if (!reply) |
| 749 | return; |
| 750 | |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 751 | skb = audit_make_reply(portid, seq, type, done, multi, payload, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (!skb) |
Andrew Morton | fcaf1eb | 2008-05-14 16:11:48 -0700 | [diff] [blame] | 753 | goto out; |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 754 | |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 755 | reply->net = get_net(net); |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 756 | reply->portid = portid; |
Eric Paris | f09ac9d | 2008-04-18 10:11:04 -0400 | [diff] [blame] | 757 | reply->skb = skb; |
| 758 | |
| 759 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); |
Andrew Morton | fcaf1eb | 2008-05-14 16:11:48 -0700 | [diff] [blame] | 760 | if (!IS_ERR(tsk)) |
| 761 | return; |
| 762 | kfree_skb(skb); |
| 763 | out: |
| 764 | kfree(reply); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit |
| 769 | * control messages. |
| 770 | */ |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 771 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
| 773 | int err = 0; |
| 774 | |
Richard Guy Briggs | 5a3cb3b | 2013-08-16 00:04:46 -0400 | [diff] [blame] | 775 | /* Only support initial user namespace for now. */ |
Eric Paris | aa4af83 | 2014-03-30 19:07:54 -0400 | [diff] [blame] | 776 | /* |
| 777 | * We return ECONNREFUSED because it tricks userspace into thinking |
| 778 | * that audit was not configured into the kernel. Lots of users |
| 779 | * configure their PAM stack (because that's what the distro does) |
| 780 | * to reject login if unable to send messages to audit. If we return |
| 781 | * ECONNREFUSED the PAM stack thinks the kernel does not have audit |
| 782 | * configured in and will let login proceed. If we return EPERM |
| 783 | * userspace will reject all logins. This should be removed when we |
| 784 | * support non init namespaces!! |
| 785 | */ |
Linus Torvalds | 0b74717 | 2014-04-12 12:38:53 -0700 | [diff] [blame] | 786 | if (current_user_ns() != &init_user_ns) |
Eric Paris | aa4af83 | 2014-03-30 19:07:54 -0400 | [diff] [blame] | 787 | return -ECONNREFUSED; |
Eric W. Biederman | 34e36d8e | 2012-09-10 23:20:20 -0700 | [diff] [blame] | 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | switch (msg_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | case AUDIT_LIST: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | case AUDIT_ADD: |
| 792 | case AUDIT_DEL: |
Eric Paris | 1890090 | 2013-04-18 19:16:36 -0400 | [diff] [blame] | 793 | return -EOPNOTSUPP; |
| 794 | case AUDIT_GET: |
| 795 | case AUDIT_SET: |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 796 | case AUDIT_GET_FEATURE: |
| 797 | case AUDIT_SET_FEATURE: |
Eric Paris | 1890090 | 2013-04-18 19:16:36 -0400 | [diff] [blame] | 798 | case AUDIT_LIST_RULES: |
| 799 | case AUDIT_ADD_RULE: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 800 | case AUDIT_DEL_RULE: |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 801 | case AUDIT_SIGNAL_INFO: |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 802 | case AUDIT_TTY_GET: |
| 803 | case AUDIT_TTY_SET: |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 804 | case AUDIT_TRIM: |
| 805 | case AUDIT_MAKE_EQUIV: |
Richard Guy Briggs | 5a3cb3b | 2013-08-16 00:04:46 -0400 | [diff] [blame] | 806 | /* Only support auditd and auditctl in initial pid namespace |
| 807 | * for now. */ |
Ameen Ali | 5985de6 | 2015-02-23 15:38:00 -0500 | [diff] [blame] | 808 | if (task_active_pid_ns(current) != &init_pid_ns) |
Richard Guy Briggs | 5a3cb3b | 2013-08-16 00:04:46 -0400 | [diff] [blame] | 809 | return -EPERM; |
| 810 | |
Eric W. Biederman | 90f62cf3 | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 811 | if (!netlink_capable(skb, CAP_AUDIT_CONTROL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | err = -EPERM; |
| 813 | break; |
Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 814 | case AUDIT_USER: |
Robert P. J. Day | 039b6b3 | 2007-05-08 00:29:20 -0700 | [diff] [blame] | 815 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
| 816 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: |
Eric W. Biederman | 90f62cf3 | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 817 | if (!netlink_capable(skb, CAP_AUDIT_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | err = -EPERM; |
| 819 | break; |
| 820 | default: /* bad msg */ |
| 821 | err = -EINVAL; |
| 822 | } |
| 823 | |
| 824 | return err; |
| 825 | } |
| 826 | |
Paul Moore | 233a686 | 2015-11-04 08:23:52 -0500 | [diff] [blame] | 827 | static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type) |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 828 | { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 829 | uid_t uid = from_kuid(&init_user_ns, current_uid()); |
Richard Guy Briggs | f1dc486 | 2013-12-11 13:52:26 -0500 | [diff] [blame] | 830 | pid_t pid = task_tgid_nr(current); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 831 | |
Tyler Hicks | 0868a5e | 2013-07-25 18:02:55 -0700 | [diff] [blame] | 832 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) { |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 833 | *ab = NULL; |
Paul Moore | 233a686 | 2015-11-04 08:23:52 -0500 | [diff] [blame] | 834 | return; |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | *ab = audit_log_start(NULL, GFP_KERNEL, msg_type); |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 838 | if (unlikely(!*ab)) |
Paul Moore | 233a686 | 2015-11-04 08:23:52 -0500 | [diff] [blame] | 839 | return; |
Richard Guy Briggs | f1dc486 | 2013-12-11 13:52:26 -0500 | [diff] [blame] | 840 | audit_log_format(*ab, "pid=%d uid=%u", pid, uid); |
Eric Paris | 4d3fb70 | 2013-04-30 09:53:34 -0400 | [diff] [blame] | 841 | audit_log_session_info(*ab); |
Eric Paris | b122c37 | 2013-04-19 15:00:33 -0400 | [diff] [blame] | 842 | audit_log_task_context(*ab); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 843 | } |
| 844 | |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 845 | int is_audit_feature_set(int i) |
| 846 | { |
| 847 | return af.features & AUDIT_FEATURE_TO_MASK(i); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | static int audit_get_feature(struct sk_buff *skb) |
| 852 | { |
| 853 | u32 seq; |
| 854 | |
| 855 | seq = nlmsg_hdr(skb)->nlmsg_seq; |
| 856 | |
Richard Guy Briggs | 9ef9151 | 2014-08-24 20:37:52 -0400 | [diff] [blame] | 857 | audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af)); |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature, |
| 863 | u32 old_lock, u32 new_lock, int res) |
| 864 | { |
| 865 | struct audit_buffer *ab; |
| 866 | |
Gao feng | b6c50fe | 2013-11-01 19:34:43 +0800 | [diff] [blame] | 867 | if (audit_enabled == AUDIT_OFF) |
| 868 | return; |
| 869 | |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 870 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE); |
Richard Guy Briggs | ad2ac26 | 2014-01-07 13:08:41 -0500 | [diff] [blame] | 871 | audit_log_task_info(ab, current); |
Richard Guy Briggs | 897f1ac | 2014-10-30 11:22:53 -0400 | [diff] [blame] | 872 | audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d", |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 873 | audit_feature_names[which], !!old_feature, !!new_feature, |
| 874 | !!old_lock, !!new_lock, res); |
| 875 | audit_log_end(ab); |
| 876 | } |
| 877 | |
| 878 | static int audit_set_feature(struct sk_buff *skb) |
| 879 | { |
| 880 | struct audit_features *uaf; |
| 881 | int i; |
| 882 | |
Fabian Frederick | 6eed9b2 | 2014-06-03 22:05:10 +0200 | [diff] [blame] | 883 | BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names)); |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 884 | uaf = nlmsg_data(nlmsg_hdr(skb)); |
| 885 | |
| 886 | /* if there is ever a version 2 we should handle that here */ |
| 887 | |
| 888 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { |
| 889 | u32 feature = AUDIT_FEATURE_TO_MASK(i); |
| 890 | u32 old_feature, new_feature, old_lock, new_lock; |
| 891 | |
| 892 | /* if we are not changing this feature, move along */ |
| 893 | if (!(feature & uaf->mask)) |
| 894 | continue; |
| 895 | |
| 896 | old_feature = af.features & feature; |
| 897 | new_feature = uaf->features & feature; |
| 898 | new_lock = (uaf->lock | af.lock) & feature; |
| 899 | old_lock = af.lock & feature; |
| 900 | |
| 901 | /* are we changing a locked feature? */ |
Gao feng | 4547b3b | 2013-11-01 19:34:44 +0800 | [diff] [blame] | 902 | if (old_lock && (new_feature != old_feature)) { |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 903 | audit_log_feature_change(i, old_feature, new_feature, |
| 904 | old_lock, new_lock, 0); |
| 905 | return -EPERM; |
| 906 | } |
| 907 | } |
| 908 | /* nothing invalid, do the changes */ |
| 909 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { |
| 910 | u32 feature = AUDIT_FEATURE_TO_MASK(i); |
| 911 | u32 old_feature, new_feature, old_lock, new_lock; |
| 912 | |
| 913 | /* if we are not changing this feature, move along */ |
| 914 | if (!(feature & uaf->mask)) |
| 915 | continue; |
| 916 | |
| 917 | old_feature = af.features & feature; |
| 918 | new_feature = uaf->features & feature; |
| 919 | old_lock = af.lock & feature; |
| 920 | new_lock = (uaf->lock | af.lock) & feature; |
| 921 | |
| 922 | if (new_feature != old_feature) |
| 923 | audit_log_feature_change(i, old_feature, new_feature, |
| 924 | old_lock, new_lock, 1); |
| 925 | |
| 926 | if (new_feature) |
| 927 | af.features |= feature; |
| 928 | else |
| 929 | af.features &= ~feature; |
| 930 | af.lock |= new_lock; |
| 931 | } |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
Richard Guy Briggs | 133e1e5 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 936 | static int audit_replace(pid_t pid) |
| 937 | { |
| 938 | struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0, |
| 939 | &pid, sizeof(pid)); |
| 940 | |
| 941 | if (!skb) |
| 942 | return -ENOMEM; |
| 943 | return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); |
| 944 | } |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 947 | { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 948 | u32 seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | int err; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 951 | struct audit_buffer *ab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | u16 msg_type = nlh->nlmsg_type; |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 953 | struct audit_sig_info *sig_data; |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 954 | char *ctx = NULL; |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 955 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 957 | err = audit_netlink_ok(skb, msg_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (err) |
| 959 | return err; |
| 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | seq = nlh->nlmsg_seq; |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 962 | data = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
| 964 | switch (msg_type) { |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 965 | case AUDIT_GET: { |
| 966 | struct audit_status s; |
| 967 | memset(&s, 0, sizeof(s)); |
| 968 | s.enabled = audit_enabled; |
| 969 | s.failure = audit_failure; |
| 970 | s.pid = audit_pid; |
| 971 | s.rate_limit = audit_rate_limit; |
| 972 | s.backlog_limit = audit_backlog_limit; |
| 973 | s.lost = atomic_read(&audit_lost); |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 974 | s.backlog = skb_queue_len(&audit_queue); |
Richard Guy Briggs | 0288d71 | 2014-11-17 15:51:01 -0500 | [diff] [blame] | 975 | s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL; |
Richard Guy Briggs | a77ed4e | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 976 | s.backlog_wait_time = audit_backlog_wait_time_master; |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 977 | audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | break; |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 979 | } |
| 980 | case AUDIT_SET: { |
| 981 | struct audit_status s; |
| 982 | memset(&s, 0, sizeof(s)); |
| 983 | /* guard against past and future API changes */ |
| 984 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); |
| 985 | if (s.mask & AUDIT_STATUS_ENABLED) { |
| 986 | err = audit_set_enabled(s.enabled); |
zhangxiliang | 20c6aaa | 2008-07-31 10:11:19 +0800 | [diff] [blame] | 987 | if (err < 0) |
| 988 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 990 | if (s.mask & AUDIT_STATUS_FAILURE) { |
| 991 | err = audit_set_failure(s.failure); |
zhangxiliang | 20c6aaa | 2008-07-31 10:11:19 +0800 | [diff] [blame] | 992 | if (err < 0) |
| 993 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 995 | if (s.mask & AUDIT_STATUS_PID) { |
| 996 | int new_pid = s.pid; |
Richard Guy Briggs | 133e1e5 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 997 | pid_t requesting_pid = task_tgid_vnr(current); |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 998 | |
Richard Guy Briggs | 935c9e7 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 999 | if ((!new_pid) && (requesting_pid != audit_pid)) { |
| 1000 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); |
Richard Guy Briggs | 34eab0a | 2013-06-21 14:47:13 -0400 | [diff] [blame] | 1001 | return -EACCES; |
Richard Guy Briggs | 935c9e7 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 1002 | } |
Richard Guy Briggs | 133e1e5 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 1003 | if (audit_pid && new_pid && |
Richard Guy Briggs | 935c9e7 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 1004 | audit_replace(requesting_pid) != -ECONNREFUSED) { |
| 1005 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); |
Richard Guy Briggs | 133e1e5 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 1006 | return -EEXIST; |
Richard Guy Briggs | 935c9e7 | 2016-01-25 18:04:15 -0500 | [diff] [blame] | 1007 | } |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 1008 | if (audit_enabled != AUDIT_OFF) |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1009 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 1010 | audit_pid = new_pid; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1011 | audit_nlk_portid = NETLINK_CB(skb).portid; |
Gao feng | de92fc9 | 2013-12-17 11:10:42 +0800 | [diff] [blame] | 1012 | audit_sock = skb->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 1014 | if (s.mask & AUDIT_STATUS_RATE_LIMIT) { |
| 1015 | err = audit_set_rate_limit(s.rate_limit); |
zhangxiliang | 20c6aaa | 2008-07-31 10:11:19 +0800 | [diff] [blame] | 1016 | if (err < 0) |
| 1017 | return err; |
| 1018 | } |
Richard Guy Briggs | 51cc83f | 2013-09-18 11:55:12 -0400 | [diff] [blame] | 1019 | if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) { |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 1020 | err = audit_set_backlog_limit(s.backlog_limit); |
Richard Guy Briggs | 51cc83f | 2013-09-18 11:55:12 -0400 | [diff] [blame] | 1021 | if (err < 0) |
| 1022 | return err; |
| 1023 | } |
Eric Paris | 3f0c5fa | 2014-01-13 16:49:28 -0500 | [diff] [blame] | 1024 | if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) { |
| 1025 | if (sizeof(s) > (size_t)nlh->nlmsg_len) |
| 1026 | return -EINVAL; |
Pranith Kumar | 724e7bf | 2015-03-11 14:08:19 -0400 | [diff] [blame] | 1027 | if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME) |
Eric Paris | 3f0c5fa | 2014-01-13 16:49:28 -0500 | [diff] [blame] | 1028 | return -EINVAL; |
| 1029 | err = audit_set_backlog_wait_time(s.backlog_wait_time); |
| 1030 | if (err < 0) |
| 1031 | return err; |
Richard Guy Briggs | 51cc83f | 2013-09-18 11:55:12 -0400 | [diff] [blame] | 1032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | break; |
Richard Guy Briggs | 09f883a | 2013-09-18 09:32:24 -0400 | [diff] [blame] | 1034 | } |
Eric Paris | b0fed40 | 2013-05-22 12:54:49 -0400 | [diff] [blame] | 1035 | case AUDIT_GET_FEATURE: |
| 1036 | err = audit_get_feature(skb); |
| 1037 | if (err) |
| 1038 | return err; |
| 1039 | break; |
| 1040 | case AUDIT_SET_FEATURE: |
| 1041 | err = audit_set_feature(skb); |
| 1042 | if (err) |
| 1043 | return err; |
| 1044 | break; |
Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 1045 | case AUDIT_USER: |
Robert P. J. Day | 039b6b3 | 2007-05-08 00:29:20 -0700 | [diff] [blame] | 1046 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
| 1047 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 1048 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
| 1049 | return 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1050 | |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1051 | err = audit_filter(msg_type, AUDIT_FILTER_USER); |
Richard Guy Briggs | 724e4fc | 2013-11-25 21:57:51 -0500 | [diff] [blame] | 1052 | if (err == 1) { /* match or error */ |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 1053 | err = 0; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1054 | if (msg_type == AUDIT_USER_TTY) { |
Peter Hurley | 37282a7 | 2016-01-09 22:55:31 -0800 | [diff] [blame] | 1055 | err = tty_audit_push(); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1056 | if (err) |
| 1057 | break; |
| 1058 | } |
Richard Guy Briggs | 1b7b533 | 2013-12-02 11:33:01 -0500 | [diff] [blame] | 1059 | mutex_unlock(&audit_cmd_mutex); |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1060 | audit_log_common_recv_msg(&ab, msg_type); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1061 | if (msg_type != AUDIT_USER_TTY) |
Richard Guy Briggs | b50eba7 | 2013-09-16 18:20:42 -0400 | [diff] [blame] | 1062 | audit_log_format(ab, " msg='%.*s'", |
| 1063 | AUDIT_MESSAGE_TEXT_MAX, |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1064 | (char *)data); |
| 1065 | else { |
| 1066 | int size; |
| 1067 | |
Eric Paris | f761610 | 2013-04-11 11:25:00 -0400 | [diff] [blame] | 1068 | audit_log_format(ab, " data="); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1069 | size = nlmsg_len(nlh); |
Miloslav Trmac | 55ad2f8 | 2009-03-19 09:52:47 -0400 | [diff] [blame] | 1070 | if (size > 0 && |
| 1071 | ((unsigned char *)data)[size - 1] == '\0') |
| 1072 | size--; |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1073 | audit_log_n_untrustedstring(ab, data, size); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 1074 | } |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 1075 | audit_set_portid(ab, NETLINK_CB(skb).portid); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1076 | audit_log_end(ab); |
Richard Guy Briggs | 1b7b533 | 2013-12-02 11:33:01 -0500 | [diff] [blame] | 1077 | mutex_lock(&audit_cmd_mutex); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | break; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1080 | case AUDIT_ADD_RULE: |
| 1081 | case AUDIT_DEL_RULE: |
| 1082 | if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) |
| 1083 | return -EINVAL; |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 1084 | if (audit_enabled == AUDIT_LOCKED) { |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1085 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
| 1086 | audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1087 | audit_log_end(ab); |
Steve Grubb | 6a01b07f | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 1088 | return -EPERM; |
| 1089 | } |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1090 | err = audit_rule_change(msg_type, NETLINK_CB(skb).portid, |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1091 | seq, data, nlmsg_len(nlh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | break; |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1093 | case AUDIT_LIST_RULES: |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1094 | err = audit_list_rules_send(skb, seq); |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1095 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1096 | case AUDIT_TRIM: |
| 1097 | audit_trim_trees(); |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1098 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1099 | audit_log_format(ab, " op=trim res=1"); |
| 1100 | audit_log_end(ab); |
| 1101 | break; |
| 1102 | case AUDIT_MAKE_EQUIV: { |
| 1103 | void *bufp = data; |
| 1104 | u32 sizes[2]; |
Harvey Harrison | 7719e43 | 2008-04-27 02:39:56 -0700 | [diff] [blame] | 1105 | size_t msglen = nlmsg_len(nlh); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1106 | char *old, *new; |
| 1107 | |
| 1108 | err = -EINVAL; |
Harvey Harrison | 7719e43 | 2008-04-27 02:39:56 -0700 | [diff] [blame] | 1109 | if (msglen < 2 * sizeof(u32)) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1110 | break; |
| 1111 | memcpy(sizes, bufp, 2 * sizeof(u32)); |
| 1112 | bufp += 2 * sizeof(u32); |
Harvey Harrison | 7719e43 | 2008-04-27 02:39:56 -0700 | [diff] [blame] | 1113 | msglen -= 2 * sizeof(u32); |
| 1114 | old = audit_unpack_string(&bufp, &msglen, sizes[0]); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1115 | if (IS_ERR(old)) { |
| 1116 | err = PTR_ERR(old); |
| 1117 | break; |
| 1118 | } |
Harvey Harrison | 7719e43 | 2008-04-27 02:39:56 -0700 | [diff] [blame] | 1119 | new = audit_unpack_string(&bufp, &msglen, sizes[1]); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1120 | if (IS_ERR(new)) { |
| 1121 | err = PTR_ERR(new); |
| 1122 | kfree(old); |
| 1123 | break; |
| 1124 | } |
| 1125 | /* OK, here comes... */ |
| 1126 | err = audit_tag_tree(old, new); |
| 1127 | |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1128 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
Eric Paris | 50397bd | 2008-01-07 18:14:19 -0500 | [diff] [blame] | 1129 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1130 | audit_log_format(ab, " op=make_equiv old="); |
| 1131 | audit_log_untrustedstring(ab, old); |
| 1132 | audit_log_format(ab, " new="); |
| 1133 | audit_log_untrustedstring(ab, new); |
| 1134 | audit_log_format(ab, " res=%d", !err); |
| 1135 | audit_log_end(ab); |
| 1136 | kfree(old); |
| 1137 | kfree(new); |
| 1138 | break; |
| 1139 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1140 | case AUDIT_SIGNAL_INFO: |
Eric Paris | 939cbf2 | 2009-09-23 13:46:00 -0400 | [diff] [blame] | 1141 | len = 0; |
| 1142 | if (audit_sig_sid) { |
| 1143 | err = security_secid_to_secctx(audit_sig_sid, &ctx, &len); |
| 1144 | if (err) |
| 1145 | return err; |
| 1146 | } |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 1147 | sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL); |
| 1148 | if (!sig_data) { |
Eric Paris | 939cbf2 | 2009-09-23 13:46:00 -0400 | [diff] [blame] | 1149 | if (audit_sig_sid) |
| 1150 | security_release_secctx(ctx, len); |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 1151 | return -ENOMEM; |
| 1152 | } |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 1153 | sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid); |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 1154 | sig_data->pid = audit_sig_pid; |
Eric Paris | 939cbf2 | 2009-09-23 13:46:00 -0400 | [diff] [blame] | 1155 | if (audit_sig_sid) { |
| 1156 | memcpy(sig_data->ctx, ctx, len); |
| 1157 | security_release_secctx(ctx, len); |
| 1158 | } |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1159 | audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0, |
| 1160 | sig_data, sizeof(*sig_data) + len); |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 1161 | kfree(sig_data); |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1162 | break; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1163 | case AUDIT_TTY_GET: { |
| 1164 | struct audit_tty_status s; |
Peter Hurley | 2e28d38 | 2016-01-09 22:55:33 -0800 | [diff] [blame] | 1165 | unsigned int t; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1166 | |
Peter Hurley | 2e28d38 | 2016-01-09 22:55:33 -0800 | [diff] [blame] | 1167 | t = READ_ONCE(current->signal->audit_tty); |
| 1168 | s.enabled = t & AUDIT_TTY_ENABLE; |
| 1169 | s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); |
Thomas Gleixner | 2070320 | 2009-12-09 14:19:35 +0000 | [diff] [blame] | 1170 | |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1171 | audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s)); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1172 | break; |
| 1173 | } |
| 1174 | case AUDIT_TTY_SET: { |
Richard Guy Briggs | a06e56b | 2013-11-15 11:29:02 -0500 | [diff] [blame] | 1175 | struct audit_tty_status s, old; |
Richard Guy Briggs | a06e56b | 2013-11-15 11:29:02 -0500 | [diff] [blame] | 1176 | struct audit_buffer *ab; |
Peter Hurley | 2e28d38 | 2016-01-09 22:55:33 -0800 | [diff] [blame] | 1177 | unsigned int t; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1178 | |
Richard Guy Briggs | 46e959e | 2013-05-03 14:03:50 -0400 | [diff] [blame] | 1179 | memset(&s, 0, sizeof(s)); |
| 1180 | /* guard against past and future API changes */ |
Mathias Krause | 4d8fe73 | 2013-09-30 22:04:25 +0200 | [diff] [blame] | 1181 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); |
Eric Paris | 0e23bac | 2014-01-13 21:12:34 -0500 | [diff] [blame] | 1182 | /* check if new data is valid */ |
| 1183 | if ((s.enabled != 0 && s.enabled != 1) || |
| 1184 | (s.log_passwd != 0 && s.log_passwd != 1)) |
| 1185 | err = -EINVAL; |
| 1186 | |
Peter Hurley | 2e28d38 | 2016-01-09 22:55:33 -0800 | [diff] [blame] | 1187 | if (err) |
| 1188 | t = READ_ONCE(current->signal->audit_tty); |
| 1189 | else { |
| 1190 | t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD); |
| 1191 | t = xchg(¤t->signal->audit_tty, t); |
Eric Paris | 0e23bac | 2014-01-13 21:12:34 -0500 | [diff] [blame] | 1192 | } |
Peter Hurley | 2e28d38 | 2016-01-09 22:55:33 -0800 | [diff] [blame] | 1193 | old.enabled = t & AUDIT_TTY_ENABLE; |
| 1194 | old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); |
Eric Paris | 0e23bac | 2014-01-13 21:12:34 -0500 | [diff] [blame] | 1195 | |
Richard Guy Briggs | a06e56b | 2013-11-15 11:29:02 -0500 | [diff] [blame] | 1196 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
Eric Paris | 1ce319f | 2014-01-13 21:16:59 -0500 | [diff] [blame] | 1197 | audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d" |
| 1198 | " old-log_passwd=%d new-log_passwd=%d res=%d", |
| 1199 | old.enabled, s.enabled, old.log_passwd, |
| 1200 | s.log_passwd, !err); |
Richard Guy Briggs | a06e56b | 2013-11-15 11:29:02 -0500 | [diff] [blame] | 1201 | audit_log_end(ab); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1202 | break; |
| 1203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | default: |
| 1205 | err = -EINVAL; |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | return err < 0 ? err : 0; |
| 1210 | } |
| 1211 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1212 | /* |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1213 | * Get message from skb. Each message is processed by audit_receive_msg. |
| 1214 | * Malformed skbs with wrong length are discarded silently. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1215 | */ |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 1216 | static void audit_receive_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | { |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1218 | struct nlmsghdr *nlh; |
| 1219 | /* |
Hong zhi guo | 9419121 | 2013-03-27 06:49:06 +0000 | [diff] [blame] | 1220 | * len MUST be signed for nlmsg_next to be able to dec it below 0 |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1221 | * if the nlmsg_len was not aligned |
| 1222 | */ |
| 1223 | int len; |
| 1224 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1226 | nlh = nlmsg_hdr(skb); |
| 1227 | len = skb->len; |
| 1228 | |
Hong zhi guo | 9419121 | 2013-03-27 06:49:06 +0000 | [diff] [blame] | 1229 | while (nlmsg_ok(nlh, len)) { |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1230 | err = audit_receive_msg(skb, nlh); |
| 1231 | /* if err or if this message says it wants a response */ |
| 1232 | if (err || (nlh->nlmsg_flags & NLM_F_ACK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | netlink_ack(skb, nlh, err); |
Eric Paris | ea7ae60 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1234 | |
Alexandru Copot | 2851da5 | 2013-03-28 23:31:29 +0200 | [diff] [blame] | 1235 | nlh = nlmsg_next(nlh, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /* Receive messages from netlink socket. */ |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1240 | static void audit_receive(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1242 | mutex_lock(&audit_cmd_mutex); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1243 | audit_receive_skb(skb); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1244 | mutex_unlock(&audit_cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Richard Guy Briggs | 3a101b8 | 2014-04-22 21:31:56 -0400 | [diff] [blame] | 1247 | /* Run custom bind function on netlink socket group connect or bind requests. */ |
Johannes Berg | 023e2cf | 2014-12-23 21:00:06 +0100 | [diff] [blame] | 1248 | static int audit_bind(struct net *net, int group) |
Richard Guy Briggs | 3a101b8 | 2014-04-22 21:31:56 -0400 | [diff] [blame] | 1249 | { |
| 1250 | if (!capable(CAP_AUDIT_READ)) |
| 1251 | return -EPERM; |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1256 | static int __net_init audit_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1258 | struct netlink_kernel_cfg cfg = { |
| 1259 | .input = audit_receive, |
Richard Guy Briggs | 3a101b8 | 2014-04-22 21:31:56 -0400 | [diff] [blame] | 1260 | .bind = audit_bind, |
Richard Guy Briggs | 451f921 | 2014-04-22 21:31:57 -0400 | [diff] [blame] | 1261 | .flags = NL_CFG_F_NONROOT_RECV, |
| 1262 | .groups = AUDIT_NLGRP_MAX, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1263 | }; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1264 | |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1265 | struct audit_net *aunet = net_generic(net, audit_net_id); |
| 1266 | |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1267 | aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg); |
Gao feng | 11ee39e | 2013-12-17 11:10:41 +0800 | [diff] [blame] | 1268 | if (aunet->nlsk == NULL) { |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1269 | audit_panic("cannot initialize netlink socket in namespace"); |
Gao feng | 11ee39e | 2013-12-17 11:10:41 +0800 | [diff] [blame] | 1270 | return -ENOMEM; |
| 1271 | } |
| 1272 | aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | static void __net_exit audit_net_exit(struct net *net) |
| 1277 | { |
| 1278 | struct audit_net *aunet = net_generic(net, audit_net_id); |
| 1279 | struct sock *sock = aunet->nlsk; |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 1280 | if (sock == audit_sock) |
| 1281 | auditd_reset(); |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1282 | |
Monam Agarwal | e231d54 | 2014-03-24 00:16:19 +0530 | [diff] [blame] | 1283 | RCU_INIT_POINTER(aunet->nlsk, NULL); |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1284 | synchronize_net(); |
| 1285 | netlink_kernel_release(sock); |
| 1286 | } |
| 1287 | |
Richard Guy Briggs | 8626877 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1288 | static struct pernet_operations audit_net_ops __net_initdata = { |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1289 | .init = audit_net_init, |
| 1290 | .exit = audit_net_exit, |
| 1291 | .id = &audit_net_id, |
| 1292 | .size = sizeof(struct audit_net), |
| 1293 | }; |
| 1294 | |
| 1295 | /* Initialize audit support at boot time. */ |
| 1296 | static int __init audit_init(void) |
| 1297 | { |
| 1298 | int i; |
| 1299 | |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 1300 | if (audit_initialized == AUDIT_DISABLED) |
| 1301 | return 0; |
| 1302 | |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1303 | pr_info("initializing netlink subsys (%s)\n", |
| 1304 | audit_default ? "enabled" : "disabled"); |
Richard Guy Briggs | 33faba7 | 2013-07-16 13:18:45 -0400 | [diff] [blame] | 1305 | register_pernet_subsys(&audit_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 1307 | skb_queue_head_init(&audit_queue); |
Paul Moore | c648020 | 2016-11-29 16:53:25 -0500 | [diff] [blame^] | 1308 | skb_queue_head_init(&audit_retry_queue); |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 1309 | skb_queue_head_init(&audit_hold_queue); |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 1310 | audit_initialized = AUDIT_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | audit_enabled = audit_default; |
Eric Paris | b593d38 | 2008-01-08 17:38:31 -0500 | [diff] [blame] | 1312 | audit_ever_enabled |= !!audit_default; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1313 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1314 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) |
| 1315 | INIT_LIST_HEAD(&audit_inode_hash[i]); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1316 | |
Paul Moore | 6c925564 | 2016-11-29 16:53:23 -0500 | [diff] [blame] | 1317 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); |
| 1318 | if (IS_ERR(kauditd_task)) { |
| 1319 | int err = PTR_ERR(kauditd_task); |
| 1320 | panic("audit: failed to start the kauditd thread (%d)\n", err); |
| 1321 | } |
| 1322 | |
| 1323 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); |
| 1324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | __initcall(audit_init); |
| 1328 | |
| 1329 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ |
| 1330 | static int __init audit_enable(char *str) |
| 1331 | { |
| 1332 | audit_default = !!simple_strtol(str, NULL, 0); |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 1333 | if (!audit_default) |
| 1334 | audit_initialized = AUDIT_DISABLED; |
| 1335 | |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1336 | pr_info("%s\n", audit_default ? |
Gao feng | d3ca034 | 2013-10-31 14:31:01 +0800 | [diff] [blame] | 1337 | "enabled (after initialization)" : "disabled (until reboot)"); |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 1338 | |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 1339 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | __setup("audit=", audit_enable); |
| 1342 | |
Richard Guy Briggs | f910fde | 2013-09-17 12:34:52 -0400 | [diff] [blame] | 1343 | /* Process kernel command-line parameter at boot time. |
| 1344 | * audit_backlog_limit=<n> */ |
| 1345 | static int __init audit_backlog_limit_set(char *str) |
| 1346 | { |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 1347 | u32 audit_backlog_limit_arg; |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1348 | |
Richard Guy Briggs | f910fde | 2013-09-17 12:34:52 -0400 | [diff] [blame] | 1349 | pr_info("audit_backlog_limit: "); |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 1350 | if (kstrtouint(str, 0, &audit_backlog_limit_arg)) { |
| 1351 | pr_cont("using default of %u, unable to parse %s\n", |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1352 | audit_backlog_limit, str); |
Richard Guy Briggs | f910fde | 2013-09-17 12:34:52 -0400 | [diff] [blame] | 1353 | return 1; |
| 1354 | } |
Joe Perches | 3e1d0bb | 2014-01-14 10:33:13 -0800 | [diff] [blame] | 1355 | |
| 1356 | audit_backlog_limit = audit_backlog_limit_arg; |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1357 | pr_cont("%d\n", audit_backlog_limit); |
Richard Guy Briggs | f910fde | 2013-09-17 12:34:52 -0400 | [diff] [blame] | 1358 | |
| 1359 | return 1; |
| 1360 | } |
| 1361 | __setup("audit_backlog_limit=", audit_backlog_limit_set); |
| 1362 | |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1363 | static void audit_buffer_free(struct audit_buffer *ab) |
| 1364 | { |
| 1365 | unsigned long flags; |
| 1366 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1367 | if (!ab) |
| 1368 | return; |
| 1369 | |
Markus Elfring | d865e57 | 2016-01-13 09:18:55 -0500 | [diff] [blame] | 1370 | kfree_skb(ab->skb); |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1371 | spin_lock_irqsave(&audit_freelist_lock, flags); |
Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 1372 | if (audit_freelist_count > AUDIT_MAXFREE) |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1373 | kfree(ab); |
Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 1374 | else { |
| 1375 | audit_freelist_count++; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1376 | list_add(&ab->list, &audit_freelist); |
Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 1377 | } |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1378 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 1379 | } |
| 1380 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1381 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1382 | gfp_t gfp_mask, int type) |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1383 | { |
| 1384 | unsigned long flags; |
| 1385 | struct audit_buffer *ab = NULL; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1386 | struct nlmsghdr *nlh; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1387 | |
| 1388 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 1389 | if (!list_empty(&audit_freelist)) { |
| 1390 | ab = list_entry(audit_freelist.next, |
| 1391 | struct audit_buffer, list); |
| 1392 | list_del(&ab->list); |
| 1393 | --audit_freelist_count; |
| 1394 | } |
| 1395 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 1396 | |
| 1397 | if (!ab) { |
David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 1398 | ab = kmalloc(sizeof(*ab), gfp_mask); |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1399 | if (!ab) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1400 | goto err; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1401 | } |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1402 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1403 | ab->ctx = ctx; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1404 | ab->gfp_mask = gfp_mask; |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1405 | |
| 1406 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask); |
| 1407 | if (!ab->skb) |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 1408 | goto err; |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1409 | |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 1410 | nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0); |
| 1411 | if (!nlh) |
| 1412 | goto out_kfree_skb; |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1413 | |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1414 | return ab; |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1415 | |
David S. Miller | c64e66c | 2012-06-26 21:45:21 -0700 | [diff] [blame] | 1416 | out_kfree_skb: |
Eric Paris | ee080e6 | 2009-06-11 14:31:35 -0400 | [diff] [blame] | 1417 | kfree_skb(ab->skb); |
| 1418 | ab->skb = NULL; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1419 | err: |
| 1420 | audit_buffer_free(ab); |
| 1421 | return NULL; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1424 | /** |
| 1425 | * audit_serial - compute a serial number for the audit record |
| 1426 | * |
| 1427 | * Compute a serial number for the audit record. Audit records are |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1428 | * written to user-space as soon as they are generated, so a complete |
| 1429 | * audit record may be written in several pieces. The timestamp of the |
| 1430 | * record and this serial number are used by the user-space tools to |
| 1431 | * determine which pieces belong to the same audit record. The |
| 1432 | * (timestamp,serial) tuple is unique for each syscall and is live from |
| 1433 | * syscall entry to syscall exit. |
| 1434 | * |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1435 | * NOTE: Another possibility is to store the formatted records off the |
| 1436 | * audit context (for those records that have a context), and emit them |
| 1437 | * all at syscall exit. However, this could delay the reporting of |
| 1438 | * significant errors until syscall exit (or never, if the system |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1439 | * halts). |
| 1440 | */ |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1441 | unsigned int audit_serial(void) |
| 1442 | { |
Richard Guy Briggs | 01478d7 | 2014-06-13 18:22:00 -0400 | [diff] [blame] | 1443 | static atomic_t serial = ATOMIC_INIT(0); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1444 | |
Richard Guy Briggs | 01478d7 | 2014-06-13 18:22:00 -0400 | [diff] [blame] | 1445 | return atomic_add_return(1, &serial); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1446 | } |
| 1447 | |
Daniel Walker | 5600b89 | 2007-10-18 03:06:10 -0700 | [diff] [blame] | 1448 | static inline void audit_get_stamp(struct audit_context *ctx, |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1449 | struct timespec *t, unsigned int *serial) |
| 1450 | { |
Al Viro | 48887e6 | 2008-12-06 01:05:50 -0500 | [diff] [blame] | 1451 | if (!ctx || !auditsc_get_stamp(ctx, t, serial)) { |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1452 | *t = CURRENT_TIME; |
| 1453 | *serial = audit_serial(); |
| 1454 | } |
| 1455 | } |
| 1456 | |
Andrew Morton | 8291991 | 2013-01-11 14:32:11 -0800 | [diff] [blame] | 1457 | /* |
| 1458 | * Wait for auditd to drain the queue a little |
| 1459 | */ |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1460 | static long wait_for_auditd(long sleep_time) |
Andrew Morton | 8291991 | 2013-01-11 14:32:11 -0800 | [diff] [blame] | 1461 | { |
| 1462 | DECLARE_WAITQUEUE(wait, current); |
Andrew Morton | 8291991 | 2013-01-11 14:32:11 -0800 | [diff] [blame] | 1463 | |
| 1464 | if (audit_backlog_limit && |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 1465 | skb_queue_len(&audit_queue) > audit_backlog_limit) { |
Paul Moore | 7ffb8e3 | 2016-04-04 16:44:02 -0400 | [diff] [blame] | 1466 | add_wait_queue_exclusive(&audit_backlog_wait, &wait); |
| 1467 | set_current_state(TASK_UNINTERRUPTIBLE); |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1468 | sleep_time = schedule_timeout(sleep_time); |
Paul Moore | 7ffb8e3 | 2016-04-04 16:44:02 -0400 | [diff] [blame] | 1469 | remove_wait_queue(&audit_backlog_wait, &wait); |
| 1470 | } |
Richard Guy Briggs | ae887e0 | 2013-09-16 10:45:59 -0400 | [diff] [blame] | 1471 | |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1472 | return sleep_time; |
Andrew Morton | 8291991 | 2013-01-11 14:32:11 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1475 | /** |
| 1476 | * audit_log_start - obtain an audit buffer |
| 1477 | * @ctx: audit_context (may be NULL) |
| 1478 | * @gfp_mask: type of allocation |
| 1479 | * @type: audit message type |
| 1480 | * |
| 1481 | * Returns audit_buffer pointer on success or NULL on error. |
| 1482 | * |
| 1483 | * Obtain an audit buffer. This routine does locking to obtain the |
| 1484 | * audit buffer, but then no locking is required for calls to |
| 1485 | * audit_log_*format. If the task (ctx) is a task that is currently in a |
| 1486 | * syscall, then the syscall is marked as auditable and an audit record |
| 1487 | * will be written at syscall exit. If there is no associated task, then |
| 1488 | * task context (ctx) should be NULL. |
| 1489 | */ |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 1490 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1491 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
| 1493 | struct audit_buffer *ab = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | struct timespec t; |
Andrew Morton | ef00be0 | 2008-01-10 11:02:39 -0800 | [diff] [blame] | 1495 | unsigned int uninitialized_var(serial); |
Toshiyuki Okajima | 6dd80ab | 2013-12-05 16:15:23 +0900 | [diff] [blame] | 1496 | int reserve = 5; /* Allow atomic callers to go up to five |
| 1497 | entries over the normal backlog limit */ |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1498 | unsigned long timeout_start = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
Eric Paris | a3f0711 | 2008-11-05 12:47:09 -0500 | [diff] [blame] | 1500 | if (audit_initialized != AUDIT_INITIALIZED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | return NULL; |
| 1502 | |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1503 | if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE))) |
Dustin Kirkland | c8edc80 | 2005-11-03 16:12:36 +0000 | [diff] [blame] | 1504 | return NULL; |
| 1505 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1506 | if (gfp_mask & __GFP_DIRECT_RECLAIM) { |
Richard Guy Briggs | f48a942 | 2016-01-13 09:15:19 -0500 | [diff] [blame] | 1507 | if (audit_pid && audit_pid == current->tgid) |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1508 | gfp_mask &= ~__GFP_DIRECT_RECLAIM; |
Toshiyuki Okajima | 6dd80ab | 2013-12-05 16:15:23 +0900 | [diff] [blame] | 1509 | else |
| 1510 | reserve = 0; |
| 1511 | } |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1512 | |
| 1513 | while (audit_backlog_limit |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 1514 | && skb_queue_len(&audit_queue) > audit_backlog_limit + reserve) { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1515 | if (gfp_mask & __GFP_DIRECT_RECLAIM && audit_backlog_wait_time) { |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1516 | long sleep_time; |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1517 | |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1518 | sleep_time = timeout_start + audit_backlog_wait_time - jiffies; |
| 1519 | if (sleep_time > 0) { |
Richard Guy Briggs | ae887e0 | 2013-09-16 10:45:59 -0400 | [diff] [blame] | 1520 | sleep_time = wait_for_auditd(sleep_time); |
Eric Paris | c81825d | 2014-01-13 15:42:16 -0500 | [diff] [blame] | 1521 | if (sleep_time > 0) |
Richard Guy Briggs | ae887e0 | 2013-09-16 10:45:59 -0400 | [diff] [blame] | 1522 | continue; |
Konstantin Khlebnikov | 8ac1c8d | 2013-09-24 15:27:42 -0700 | [diff] [blame] | 1523 | } |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1524 | } |
Eric Paris | 320f1b1 | 2008-01-23 22:55:05 -0500 | [diff] [blame] | 1525 | if (audit_rate_check() && printk_ratelimit()) |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1526 | pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n", |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 1527 | skb_queue_len(&audit_queue), |
Joe Perches | d957f7b | 2014-01-14 10:33:12 -0800 | [diff] [blame] | 1528 | audit_backlog_limit); |
David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 1529 | audit_log_lost("backlog limit exceeded"); |
Paul Moore | eb8baf6 | 2016-01-13 09:15:18 -0500 | [diff] [blame] | 1530 | audit_backlog_wait_time = 0; |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1531 | wake_up(&audit_backlog_wait); |
David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 1532 | return NULL; |
| 1533 | } |
| 1534 | |
Richard Guy Briggs | c4b7a77 | 2016-01-13 09:15:18 -0500 | [diff] [blame] | 1535 | if (!reserve && !audit_backlog_wait_time) |
Richard Guy Briggs | efef73a | 2015-02-23 15:38:00 -0500 | [diff] [blame] | 1536 | audit_backlog_wait_time = audit_backlog_wait_time_master; |
Richard Guy Briggs | e789e56 | 2013-09-12 23:03:51 -0400 | [diff] [blame] | 1537 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1538 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | if (!ab) { |
| 1540 | audit_log_lost("out of memory in audit_log_start"); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1544 | audit_get_stamp(ab->ctx, &t, &serial); |
Chris Wright | 197c69c | 2005-05-11 10:54:05 +0100 | [diff] [blame] | 1545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
| 1547 | t.tv_sec, t.tv_nsec/1000000, serial); |
| 1548 | return ab; |
| 1549 | } |
| 1550 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1551 | /** |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1552 | * audit_expand - expand skb in the audit buffer |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1553 | * @ab: audit_buffer |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1554 | * @extra: space to add at tail of the skb |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1555 | * |
| 1556 | * Returns 0 (no space) on failed expansion, or available space if |
| 1557 | * successful. |
| 1558 | */ |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 1559 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1560 | { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1561 | struct sk_buff *skb = ab->skb; |
Herbert Xu | 406a1d8 | 2008-01-28 20:47:09 -0800 | [diff] [blame] | 1562 | int oldtail = skb_tailroom(skb); |
| 1563 | int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask); |
| 1564 | int newtail = skb_tailroom(skb); |
| 1565 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1566 | if (ret < 0) { |
| 1567 | audit_log_lost("out of memory in audit_expand"); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1568 | return 0; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1569 | } |
Herbert Xu | 406a1d8 | 2008-01-28 20:47:09 -0800 | [diff] [blame] | 1570 | |
| 1571 | skb->truesize += newtail - oldtail; |
| 1572 | return newtail; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1575 | /* |
| 1576 | * Format an audit message into the audit buffer. If there isn't enough |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | * room in the audit buffer, more room will be allocated and vsnprint |
| 1578 | * will be called a second time. Currently, we assume that a printk |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1579 | * can't format message larger than 1024 bytes, so we don't either. |
| 1580 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
| 1582 | va_list args) |
| 1583 | { |
| 1584 | int len, avail; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1585 | struct sk_buff *skb; |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 1586 | va_list args2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | if (!ab) |
| 1589 | return; |
| 1590 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1591 | BUG_ON(!ab->skb); |
| 1592 | skb = ab->skb; |
| 1593 | avail = skb_tailroom(skb); |
| 1594 | if (avail == 0) { |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 1595 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1596 | if (!avail) |
| 1597 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | } |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 1599 | va_copy(args2, args); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1600 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | if (len >= avail) { |
| 1602 | /* The printk buffer is 1024 bytes long, so if we get |
| 1603 | * here and AUDIT_BUFSIZ is at least 1024, then we can |
| 1604 | * log everything that printk could have logged. */ |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1605 | avail = audit_expand(ab, |
| 1606 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1607 | if (!avail) |
Jesper Juhl | a0e86bd | 2012-01-08 22:44:29 +0100 | [diff] [blame] | 1608 | goto out_va_end; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1609 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | } |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1611 | if (len > 0) |
| 1612 | skb_put(skb, len); |
Jesper Juhl | a0e86bd | 2012-01-08 22:44:29 +0100 | [diff] [blame] | 1613 | out_va_end: |
| 1614 | va_end(args2); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1615 | out: |
| 1616 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1619 | /** |
| 1620 | * audit_log_format - format a message into the audit buffer. |
| 1621 | * @ab: audit_buffer |
| 1622 | * @fmt: format string |
| 1623 | * @...: optional parameters matching @fmt string |
| 1624 | * |
| 1625 | * All the work is done in audit_log_vformat. |
| 1626 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
| 1628 | { |
| 1629 | va_list args; |
| 1630 | |
| 1631 | if (!ab) |
| 1632 | return; |
| 1633 | va_start(args, fmt); |
| 1634 | audit_log_vformat(ab, fmt, args); |
| 1635 | va_end(args); |
| 1636 | } |
| 1637 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1638 | /** |
| 1639 | * audit_log_hex - convert a buffer to hex and append it to the audit skb |
| 1640 | * @ab: the audit_buffer |
| 1641 | * @buf: buffer to convert to hex |
| 1642 | * @len: length of @buf to be converted |
| 1643 | * |
| 1644 | * No return value; failure to expand is silently ignored. |
| 1645 | * |
| 1646 | * This function will take the passed buf and convert it into a string of |
| 1647 | * ascii hex digits. The new string is placed onto the skb. |
| 1648 | */ |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1649 | void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1650 | size_t len) |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1651 | { |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1652 | int i, avail, new_len; |
| 1653 | unsigned char *ptr; |
| 1654 | struct sk_buff *skb; |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1655 | |
Amy Griffis | 8ef2d30 | 2006-09-07 17:03:02 -0400 | [diff] [blame] | 1656 | if (!ab) |
| 1657 | return; |
| 1658 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1659 | BUG_ON(!ab->skb); |
| 1660 | skb = ab->skb; |
| 1661 | avail = skb_tailroom(skb); |
| 1662 | new_len = len<<1; |
| 1663 | if (new_len >= avail) { |
| 1664 | /* Round the buffer request up to the next multiple */ |
| 1665 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); |
| 1666 | avail = audit_expand(ab, new_len); |
| 1667 | if (!avail) |
| 1668 | return; |
| 1669 | } |
| 1670 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1671 | ptr = skb_tail_pointer(skb); |
Joe Perches | b8dbc32 | 2014-01-13 23:31:27 -0800 | [diff] [blame] | 1672 | for (i = 0; i < len; i++) |
| 1673 | ptr = hex_byte_pack_upper(ptr, buf[i]); |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1674 | *ptr = 0; |
| 1675 | skb_put(skb, len << 1); /* new string is twice the old string */ |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1676 | } |
| 1677 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1678 | /* |
| 1679 | * Format a string of no more than slen characters into the audit buffer, |
| 1680 | * enclosed in quote marks. |
| 1681 | */ |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1682 | void audit_log_n_string(struct audit_buffer *ab, const char *string, |
| 1683 | size_t slen) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1684 | { |
| 1685 | int avail, new_len; |
| 1686 | unsigned char *ptr; |
| 1687 | struct sk_buff *skb; |
| 1688 | |
Amy Griffis | 8ef2d30 | 2006-09-07 17:03:02 -0400 | [diff] [blame] | 1689 | if (!ab) |
| 1690 | return; |
| 1691 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1692 | BUG_ON(!ab->skb); |
| 1693 | skb = ab->skb; |
| 1694 | avail = skb_tailroom(skb); |
| 1695 | new_len = slen + 3; /* enclosing quotes + null terminator */ |
| 1696 | if (new_len > avail) { |
| 1697 | avail = audit_expand(ab, new_len); |
| 1698 | if (!avail) |
| 1699 | return; |
| 1700 | } |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1701 | ptr = skb_tail_pointer(skb); |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1702 | *ptr++ = '"'; |
| 1703 | memcpy(ptr, string, slen); |
| 1704 | ptr += slen; |
| 1705 | *ptr++ = '"'; |
| 1706 | *ptr = 0; |
| 1707 | skb_put(skb, slen + 2); /* don't include null terminator */ |
| 1708 | } |
| 1709 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1710 | /** |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1711 | * audit_string_contains_control - does a string need to be logged in hex |
Dave Jones | f706d5d | 2008-03-28 14:15:56 -0700 | [diff] [blame] | 1712 | * @string: string to be checked |
| 1713 | * @len: max length of the string to check |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1714 | */ |
Yaowei Bai | 9fcf836 | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 1715 | bool audit_string_contains_control(const char *string, size_t len) |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1716 | { |
| 1717 | const unsigned char *p; |
Miloslav Trmac | b3897f5 | 2009-03-19 09:48:27 -0400 | [diff] [blame] | 1718 | for (p = string; p < (const unsigned char *)string + len; p++) { |
Vesa-Matti J Kari | 1d6c964 | 2008-07-23 00:06:13 +0300 | [diff] [blame] | 1719 | if (*p == '"' || *p < 0x21 || *p > 0x7e) |
Yaowei Bai | 9fcf836 | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 1720 | return true; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1721 | } |
Yaowei Bai | 9fcf836 | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 1722 | return false; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /** |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1726 | * audit_log_n_untrustedstring - log a string that may contain random characters |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1727 | * @ab: audit_buffer |
Dave Jones | f706d5d | 2008-03-28 14:15:56 -0700 | [diff] [blame] | 1728 | * @len: length of string (not including trailing null) |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1729 | * @string: string to be logged |
| 1730 | * |
| 1731 | * This code will escape a string that is passed to it if the string |
| 1732 | * contains a control character, unprintable character, double quote mark, |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1733 | * or a space. Unescaped strings will start and end with a double quote mark. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1734 | * Strings that are escaped are printed in hex (2 digits per char). |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1735 | * |
| 1736 | * The caller specifies the number of characters in the string to log, which may |
| 1737 | * or may not be the entire string. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1738 | */ |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1739 | void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string, |
| 1740 | size_t len) |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1741 | { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1742 | if (audit_string_contains_control(string, len)) |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1743 | audit_log_n_hex(ab, string, len); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1744 | else |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1745 | audit_log_n_string(ab, string, len); |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1748 | /** |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1749 | * audit_log_untrustedstring - log a string that may contain random characters |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1750 | * @ab: audit_buffer |
| 1751 | * @string: string to be logged |
| 1752 | * |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1753 | * Same as audit_log_n_untrustedstring(), except that strlen is used to |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1754 | * determine string length. |
| 1755 | */ |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1756 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1757 | { |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1758 | audit_log_n_untrustedstring(ab, string, strlen(string)); |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1761 | /* This is a helper-function to print the escaped d_path */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
Al Viro | 66b3fad | 2012-03-14 21:48:20 -0400 | [diff] [blame] | 1763 | const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | { |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1765 | char *p, *pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1767 | if (prefix) |
Kees Cook | c158a35 | 2012-01-06 14:07:10 -0800 | [diff] [blame] | 1768 | audit_log_format(ab, "%s", prefix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1770 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1771 | pathname = kmalloc(PATH_MAX+11, ab->gfp_mask); |
| 1772 | if (!pathname) { |
Eric Paris | def5754 | 2009-03-10 18:00:14 -0400 | [diff] [blame] | 1773 | audit_log_string(ab, "<no_memory>"); |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1774 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | } |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 1776 | p = d_path(path, pathname, PATH_MAX+11); |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1777 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ |
| 1778 | /* FIXME: can we save some information here? */ |
Eric Paris | def5754 | 2009-03-10 18:00:14 -0400 | [diff] [blame] | 1779 | audit_log_string(ab, "<too_long>"); |
Daniel Walker | 5600b89 | 2007-10-18 03:06:10 -0700 | [diff] [blame] | 1780 | } else |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1781 | audit_log_untrustedstring(ab, p); |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1782 | kfree(pathname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Eric Paris | 4d3fb70 | 2013-04-30 09:53:34 -0400 | [diff] [blame] | 1785 | void audit_log_session_info(struct audit_buffer *ab) |
| 1786 | { |
Eric Paris | 4440e85 | 2013-11-27 17:35:17 -0500 | [diff] [blame] | 1787 | unsigned int sessionid = audit_get_sessionid(current); |
Eric Paris | 4d3fb70 | 2013-04-30 09:53:34 -0400 | [diff] [blame] | 1788 | uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); |
| 1789 | |
Richard Guy Briggs | b8f89ca | 2013-09-18 11:17:43 -0400 | [diff] [blame] | 1790 | audit_log_format(ab, " auid=%u ses=%u", auid, sessionid); |
Eric Paris | 4d3fb70 | 2013-04-30 09:53:34 -0400 | [diff] [blame] | 1791 | } |
| 1792 | |
Eric Paris | 9d96098 | 2009-06-11 14:31:37 -0400 | [diff] [blame] | 1793 | void audit_log_key(struct audit_buffer *ab, char *key) |
| 1794 | { |
| 1795 | audit_log_format(ab, " key="); |
| 1796 | if (key) |
| 1797 | audit_log_untrustedstring(ab, key); |
| 1798 | else |
| 1799 | audit_log_format(ab, "(null)"); |
| 1800 | } |
| 1801 | |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1802 | void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap) |
| 1803 | { |
| 1804 | int i; |
| 1805 | |
| 1806 | audit_log_format(ab, " %s=", prefix); |
| 1807 | CAP_FOR_EACH_U32(i) { |
| 1808 | audit_log_format(ab, "%08x", |
Eric Paris | 7d8b6c6 | 2014-07-23 15:36:26 -0400 | [diff] [blame] | 1809 | cap->cap[CAP_LAST_U32 - i]); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
Richard Guy Briggs | 691e6d5 | 2014-05-26 11:02:48 -0400 | [diff] [blame] | 1813 | static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name) |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1814 | { |
| 1815 | kernel_cap_t *perm = &name->fcap.permitted; |
| 1816 | kernel_cap_t *inh = &name->fcap.inheritable; |
| 1817 | int log = 0; |
| 1818 | |
| 1819 | if (!cap_isclear(*perm)) { |
| 1820 | audit_log_cap(ab, "cap_fp", perm); |
| 1821 | log = 1; |
| 1822 | } |
| 1823 | if (!cap_isclear(*inh)) { |
| 1824 | audit_log_cap(ab, "cap_fi", inh); |
| 1825 | log = 1; |
| 1826 | } |
| 1827 | |
| 1828 | if (log) |
| 1829 | audit_log_format(ab, " cap_fe=%d cap_fver=%x", |
| 1830 | name->fcap.fE, name->fcap_ver); |
| 1831 | } |
| 1832 | |
| 1833 | static inline int audit_copy_fcaps(struct audit_names *name, |
| 1834 | const struct dentry *dentry) |
| 1835 | { |
| 1836 | struct cpu_vfs_cap_data caps; |
| 1837 | int rc; |
| 1838 | |
| 1839 | if (!dentry) |
| 1840 | return 0; |
| 1841 | |
| 1842 | rc = get_vfs_caps_from_disk(dentry, &caps); |
| 1843 | if (rc) |
| 1844 | return rc; |
| 1845 | |
| 1846 | name->fcap.permitted = caps.permitted; |
| 1847 | name->fcap.inheritable = caps.inheritable; |
| 1848 | name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); |
| 1849 | name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> |
| 1850 | VFS_CAP_REVISION_SHIFT; |
| 1851 | |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
| 1855 | /* Copy inode data into an audit_names. */ |
| 1856 | void audit_copy_inode(struct audit_names *name, const struct dentry *dentry, |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1857 | struct inode *inode) |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1858 | { |
| 1859 | name->ino = inode->i_ino; |
| 1860 | name->dev = inode->i_sb->s_dev; |
| 1861 | name->mode = inode->i_mode; |
| 1862 | name->uid = inode->i_uid; |
| 1863 | name->gid = inode->i_gid; |
| 1864 | name->rdev = inode->i_rdev; |
| 1865 | security_inode_getsecid(inode, &name->osid); |
| 1866 | audit_copy_fcaps(name, dentry); |
| 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * audit_log_name - produce AUDIT_PATH record from struct audit_names |
| 1871 | * @context: audit_context for the task |
| 1872 | * @n: audit_names structure with reportable details |
| 1873 | * @path: optional path to report instead of audit_names->name |
| 1874 | * @record_num: record number to report when handling a list of names |
| 1875 | * @call_panic: optional pointer to int that will be updated if secid fails |
| 1876 | */ |
| 1877 | void audit_log_name(struct audit_context *context, struct audit_names *n, |
| 1878 | struct path *path, int record_num, int *call_panic) |
| 1879 | { |
| 1880 | struct audit_buffer *ab; |
| 1881 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
| 1882 | if (!ab) |
| 1883 | return; |
| 1884 | |
| 1885 | audit_log_format(ab, "item=%d", record_num); |
| 1886 | |
| 1887 | if (path) |
| 1888 | audit_log_d_path(ab, " name=", path); |
| 1889 | else if (n->name) { |
| 1890 | switch (n->name_len) { |
| 1891 | case AUDIT_NAME_FULL: |
| 1892 | /* log the full path */ |
| 1893 | audit_log_format(ab, " name="); |
| 1894 | audit_log_untrustedstring(ab, n->name->name); |
| 1895 | break; |
| 1896 | case 0: |
| 1897 | /* name was specified as a relative path and the |
| 1898 | * directory component is the cwd */ |
| 1899 | audit_log_d_path(ab, " name=", &context->pwd); |
| 1900 | break; |
| 1901 | default: |
| 1902 | /* log the name's directory component */ |
| 1903 | audit_log_format(ab, " name="); |
| 1904 | audit_log_n_untrustedstring(ab, n->name->name, |
| 1905 | n->name_len); |
| 1906 | } |
| 1907 | } else |
| 1908 | audit_log_format(ab, " name=(null)"); |
| 1909 | |
Linus Torvalds | 425afcf | 2015-09-08 13:34:59 -0700 | [diff] [blame] | 1910 | if (n->ino != AUDIT_INO_UNSET) |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1911 | audit_log_format(ab, " inode=%lu" |
| 1912 | " dev=%02x:%02x mode=%#ho" |
| 1913 | " ouid=%u ogid=%u rdev=%02x:%02x", |
| 1914 | n->ino, |
| 1915 | MAJOR(n->dev), |
| 1916 | MINOR(n->dev), |
| 1917 | n->mode, |
| 1918 | from_kuid(&init_user_ns, n->uid), |
| 1919 | from_kgid(&init_user_ns, n->gid), |
| 1920 | MAJOR(n->rdev), |
| 1921 | MINOR(n->rdev)); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1922 | if (n->osid != 0) { |
| 1923 | char *ctx = NULL; |
| 1924 | u32 len; |
| 1925 | if (security_secid_to_secctx( |
| 1926 | n->osid, &ctx, &len)) { |
| 1927 | audit_log_format(ab, " osid=%u", n->osid); |
| 1928 | if (call_panic) |
| 1929 | *call_panic = 2; |
| 1930 | } else { |
| 1931 | audit_log_format(ab, " obj=%s", ctx); |
| 1932 | security_release_secctx(ctx, len); |
| 1933 | } |
| 1934 | } |
| 1935 | |
Jeff Layton | d3aea84 | 2013-05-08 10:32:23 -0400 | [diff] [blame] | 1936 | /* log the audit_names record type */ |
| 1937 | audit_log_format(ab, " nametype="); |
| 1938 | switch(n->type) { |
| 1939 | case AUDIT_TYPE_NORMAL: |
| 1940 | audit_log_format(ab, "NORMAL"); |
| 1941 | break; |
| 1942 | case AUDIT_TYPE_PARENT: |
| 1943 | audit_log_format(ab, "PARENT"); |
| 1944 | break; |
| 1945 | case AUDIT_TYPE_CHILD_DELETE: |
| 1946 | audit_log_format(ab, "DELETE"); |
| 1947 | break; |
| 1948 | case AUDIT_TYPE_CHILD_CREATE: |
| 1949 | audit_log_format(ab, "CREATE"); |
| 1950 | break; |
| 1951 | default: |
| 1952 | audit_log_format(ab, "UNKNOWN"); |
| 1953 | break; |
| 1954 | } |
| 1955 | |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1956 | audit_log_fcaps(ab, n); |
| 1957 | audit_log_end(ab); |
| 1958 | } |
| 1959 | |
| 1960 | int audit_log_task_context(struct audit_buffer *ab) |
| 1961 | { |
| 1962 | char *ctx = NULL; |
| 1963 | unsigned len; |
| 1964 | int error; |
| 1965 | u32 sid; |
| 1966 | |
| 1967 | security_task_getsecid(current, &sid); |
| 1968 | if (!sid) |
| 1969 | return 0; |
| 1970 | |
| 1971 | error = security_secid_to_secctx(sid, &ctx, &len); |
| 1972 | if (error) { |
| 1973 | if (error != -EINVAL) |
| 1974 | goto error_path; |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | audit_log_format(ab, " subj=%s", ctx); |
| 1979 | security_release_secctx(ctx, len); |
| 1980 | return 0; |
| 1981 | |
| 1982 | error_path: |
| 1983 | audit_panic("error in audit_log_task_context"); |
| 1984 | return error; |
| 1985 | } |
| 1986 | EXPORT_SYMBOL(audit_log_task_context); |
| 1987 | |
Davidlohr Bueso | 4766b19 | 2015-02-22 18:20:00 -0800 | [diff] [blame] | 1988 | void audit_log_d_path_exe(struct audit_buffer *ab, |
| 1989 | struct mm_struct *mm) |
| 1990 | { |
Davidlohr Bueso | 5b28255 | 2015-02-22 18:20:09 -0800 | [diff] [blame] | 1991 | struct file *exe_file; |
Davidlohr Bueso | 4766b19 | 2015-02-22 18:20:00 -0800 | [diff] [blame] | 1992 | |
Davidlohr Bueso | 5b28255 | 2015-02-22 18:20:09 -0800 | [diff] [blame] | 1993 | if (!mm) |
| 1994 | goto out_null; |
| 1995 | |
| 1996 | exe_file = get_mm_exe_file(mm); |
| 1997 | if (!exe_file) |
| 1998 | goto out_null; |
| 1999 | |
| 2000 | audit_log_d_path(ab, " exe=", &exe_file->f_path); |
| 2001 | fput(exe_file); |
| 2002 | return; |
| 2003 | out_null: |
| 2004 | audit_log_format(ab, " exe=(null)"); |
Davidlohr Bueso | 4766b19 | 2015-02-22 18:20:00 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
Richard Guy Briggs | 3f5be2d | 2016-06-28 12:07:50 -0400 | [diff] [blame] | 2007 | struct tty_struct *audit_get_tty(struct task_struct *tsk) |
| 2008 | { |
| 2009 | struct tty_struct *tty = NULL; |
| 2010 | unsigned long flags; |
| 2011 | |
| 2012 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
| 2013 | if (tsk->signal) |
| 2014 | tty = tty_kref_get(tsk->signal->tty); |
| 2015 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
| 2016 | return tty; |
| 2017 | } |
| 2018 | |
| 2019 | void audit_put_tty(struct tty_struct *tty) |
| 2020 | { |
| 2021 | tty_kref_put(tty); |
| 2022 | } |
| 2023 | |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2024 | void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk) |
| 2025 | { |
| 2026 | const struct cred *cred; |
Richard Guy Briggs | 9eab339 | 2014-03-15 18:42:34 -0400 | [diff] [blame] | 2027 | char comm[sizeof(tsk->comm)]; |
Richard Guy Briggs | db0a6fb | 2016-04-21 14:14:01 -0400 | [diff] [blame] | 2028 | struct tty_struct *tty; |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2029 | |
| 2030 | if (!ab) |
| 2031 | return; |
| 2032 | |
| 2033 | /* tsk == current */ |
| 2034 | cred = current_cred(); |
Richard Guy Briggs | db0a6fb | 2016-04-21 14:14:01 -0400 | [diff] [blame] | 2035 | tty = audit_get_tty(tsk); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2036 | audit_log_format(ab, |
Richard Guy Briggs | c92cdeb | 2013-12-10 22:10:41 -0500 | [diff] [blame] | 2037 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2038 | " euid=%u suid=%u fsuid=%u" |
Richard Guy Briggs | 2f2ad10 | 2013-07-15 10:23:11 -0400 | [diff] [blame] | 2039 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", |
Richard Guy Briggs | c92cdeb | 2013-12-10 22:10:41 -0500 | [diff] [blame] | 2040 | task_ppid_nr(tsk), |
Richard Guy Briggs | f1dc486 | 2013-12-11 13:52:26 -0500 | [diff] [blame] | 2041 | task_pid_nr(tsk), |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2042 | from_kuid(&init_user_ns, audit_get_loginuid(tsk)), |
| 2043 | from_kuid(&init_user_ns, cred->uid), |
| 2044 | from_kgid(&init_user_ns, cred->gid), |
| 2045 | from_kuid(&init_user_ns, cred->euid), |
| 2046 | from_kuid(&init_user_ns, cred->suid), |
| 2047 | from_kuid(&init_user_ns, cred->fsuid), |
| 2048 | from_kgid(&init_user_ns, cred->egid), |
| 2049 | from_kgid(&init_user_ns, cred->sgid), |
| 2050 | from_kgid(&init_user_ns, cred->fsgid), |
Richard Guy Briggs | db0a6fb | 2016-04-21 14:14:01 -0400 | [diff] [blame] | 2051 | tty ? tty_name(tty) : "(none)", |
| 2052 | audit_get_sessionid(tsk)); |
| 2053 | audit_put_tty(tty); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2054 | audit_log_format(ab, " comm="); |
Richard Guy Briggs | 9eab339 | 2014-03-15 18:42:34 -0400 | [diff] [blame] | 2055 | audit_log_untrustedstring(ab, get_task_comm(comm, tsk)); |
Davidlohr Bueso | 4766b19 | 2015-02-22 18:20:00 -0800 | [diff] [blame] | 2056 | audit_log_d_path_exe(ab, tsk->mm); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2057 | audit_log_task_context(ab); |
| 2058 | } |
| 2059 | EXPORT_SYMBOL(audit_log_task_info); |
| 2060 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2061 | /** |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2062 | * audit_log_link_denied - report a link restriction denial |
Shailendra Verma | 2201196 | 2015-05-23 10:40:27 +0530 | [diff] [blame] | 2063 | * @operation: specific link operation |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2064 | * @link: the path that triggered the restriction |
| 2065 | */ |
| 2066 | void audit_log_link_denied(const char *operation, struct path *link) |
| 2067 | { |
| 2068 | struct audit_buffer *ab; |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2069 | struct audit_names *name; |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2070 | |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2071 | name = kzalloc(sizeof(*name), GFP_NOFS); |
| 2072 | if (!name) |
| 2073 | return; |
| 2074 | |
| 2075 | /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */ |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2076 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
| 2077 | AUDIT_ANOM_LINK); |
Sasha Levin | d1c7d97 | 2012-10-04 19:57:31 -0400 | [diff] [blame] | 2078 | if (!ab) |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2079 | goto out; |
| 2080 | audit_log_format(ab, "op=%s", operation); |
| 2081 | audit_log_task_info(ab, current); |
| 2082 | audit_log_format(ab, " res=0"); |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2083 | audit_log_end(ab); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2084 | |
| 2085 | /* Generate AUDIT_PATH record with object. */ |
| 2086 | name->type = AUDIT_TYPE_NORMAL; |
David Howells | 3b36215 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 2087 | audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry)); |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 2088 | audit_log_name(current->audit_context, name, link, 0, NULL); |
| 2089 | out: |
| 2090 | kfree(name); |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2094 | * audit_log_end - end one audit record |
| 2095 | * @ab: the audit_buffer |
| 2096 | * |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 2097 | * We can not do a netlink send inside an irq context because it blocks (last |
| 2098 | * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a |
| 2099 | * queue and a tasklet is scheduled to remove them from the queue outside the |
| 2100 | * irq context. May be called in any context. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2101 | */ |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 2102 | void audit_log_end(struct audit_buffer *ab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | if (!ab) |
| 2105 | return; |
| 2106 | if (!audit_rate_check()) { |
| 2107 | audit_log_lost("rate limit exceeded"); |
| 2108 | } else { |
Paul Moore | af8b824 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 2109 | skb_queue_tail(&audit_queue, ab->skb); |
Paul Moore | 4aa83872 | 2016-11-29 16:53:24 -0500 | [diff] [blame] | 2110 | wake_up_interruptible(&kauditd_wait); |
Eric Paris | f3d357b | 2008-04-18 10:02:28 -0400 | [diff] [blame] | 2111 | ab->skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 2113 | audit_buffer_free(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2116 | /** |
| 2117 | * audit_log - Log an audit record |
| 2118 | * @ctx: audit context |
| 2119 | * @gfp_mask: type of allocation |
| 2120 | * @type: audit message type |
| 2121 | * @fmt: format string to use |
| 2122 | * @...: variable parameters matching the format string |
| 2123 | * |
| 2124 | * This is a convenience function that calls audit_log_start, |
| 2125 | * audit_log_vformat, and audit_log_end. It may be called |
| 2126 | * in any context. |
| 2127 | */ |
Daniel Walker | 5600b89 | 2007-10-18 03:06:10 -0700 | [diff] [blame] | 2128 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 2129 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | { |
| 2131 | struct audit_buffer *ab; |
| 2132 | va_list args; |
| 2133 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 2134 | ab = audit_log_start(ctx, gfp_mask, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | if (ab) { |
| 2136 | va_start(args, fmt); |
| 2137 | audit_log_vformat(ab, fmt, args); |
| 2138 | va_end(args); |
| 2139 | audit_log_end(ab); |
| 2140 | } |
| 2141 | } |
lorenzo@gnu.org | bf45da9 | 2006-03-09 00:33:47 +0100 | [diff] [blame] | 2142 | |
Mr Dash Four | 131ad62 | 2011-06-30 13:31:57 +0200 | [diff] [blame] | 2143 | #ifdef CONFIG_SECURITY |
| 2144 | /** |
| 2145 | * audit_log_secctx - Converts and logs SELinux context |
| 2146 | * @ab: audit_buffer |
| 2147 | * @secid: security number |
| 2148 | * |
| 2149 | * This is a helper function that calls security_secid_to_secctx to convert |
| 2150 | * secid to secctx and then adds the (converted) SELinux context to the audit |
| 2151 | * log by calling audit_log_format, thus also preventing leak of internal secid |
| 2152 | * to userspace. If secid cannot be converted audit_panic is called. |
| 2153 | */ |
| 2154 | void audit_log_secctx(struct audit_buffer *ab, u32 secid) |
| 2155 | { |
| 2156 | u32 len; |
| 2157 | char *secctx; |
| 2158 | |
| 2159 | if (security_secid_to_secctx(secid, &secctx, &len)) { |
| 2160 | audit_panic("Cannot convert secid to context"); |
| 2161 | } else { |
| 2162 | audit_log_format(ab, " obj=%s", secctx); |
| 2163 | security_release_secctx(secctx, len); |
| 2164 | } |
| 2165 | } |
| 2166 | EXPORT_SYMBOL(audit_log_secctx); |
| 2167 | #endif |
| 2168 | |
lorenzo@gnu.org | bf45da9 | 2006-03-09 00:33:47 +0100 | [diff] [blame] | 2169 | EXPORT_SYMBOL(audit_log_start); |
| 2170 | EXPORT_SYMBOL(audit_log_end); |
| 2171 | EXPORT_SYMBOL(audit_log_format); |
| 2172 | EXPORT_SYMBOL(audit_log); |