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