blob: 0572e5dcfda78db3b01a97a439f1111e4b76727f [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* audit.c -- Auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Grubb6a01b07f2007-01-19 14:39:55 -05005 * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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. Darwishd7a96f32008-03-01 22:01:11 +020024 * Goals: 1) Integrate fully with Security Modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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 *
85c87212005-04-29 16:23:29 +010041 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
43
Joe Perchesd957f7b2014-01-14 10:33:12 -080044#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
Davidlohr Bueso5b282552015-02-22 18:20:09 -080046#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/init.h>
Paul McQuade7153e402014-06-06 14:37:37 -070048#include <linux/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040051#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
David Woodhouseb7d11252005-05-19 10:56:58 +010053#include <linux/err.h>
54#include <linux/kthread.h>
Richard Guy Briggs46e959e2013-05-03 14:03:50 -040055#include <linux/kernel.h>
Eric Parisb24a30a2013-04-30 15:30:32 -040056#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <linux/audit.h>
59
60#include <net/sock.h>
Amy Griffis93315ed2006-02-07 12:05:27 -050061#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/skbuff.h>
Mr Dash Four131ad622011-06-30 13:31:57 +020063#ifdef CONFIG_SECURITY
64#include <linux/security.h>
65#endif
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080066#include <linux/freezer.h>
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -070067#include <linux/pid_namespace.h>
Richard Guy Briggs33faba72013-07-16 13:18:45 -040068#include <net/netns/generic.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060069
70#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Parisa3f07112008-11-05 12:47:09 -050072/* No auditing will take place until audit_initialized == AUDIT_INITIALIZED.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * (Initialization happens after skb_init is called.) */
Eric Parisa3f07112008-11-05 12:47:09 -050074#define AUDIT_DISABLED -1
75#define AUDIT_UNINITIALIZED 0
76#define AUDIT_INITIALIZED 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int audit_initialized;
78
Eric Paris1a6b9f22008-01-07 17:09:31 -050079#define AUDIT_OFF 0
80#define AUDIT_ON 1
81#define AUDIT_LOCKED 2
Joe Perches3e1d0bb2014-01-14 10:33:13 -080082u32 audit_enabled;
83u32 audit_ever_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jan Engelhardtae9d67af2011-01-18 06:48:12 +010085EXPORT_SYMBOL_GPL(audit_enabled);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Default state when kernel boots without any parameters. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080088static u32 audit_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* If auditing cannot proceed, audit_failure selects what happens. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080091static u32 audit_failure = AUDIT_FAIL_PRINTK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Pavel Emelyanov75c03712008-03-20 15:39:41 -070093/*
94 * If audit records are to be written to the netlink socket, audit_pid
Eric W. Biederman15e47302012-09-07 20:12:54 +000095 * 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 Emelyanov75c03712008-03-20 15:39:41 -070097 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010098int audit_pid;
Richard Guy Briggsf9441632013-08-14 11:32:45 -040099static __u32 audit_nlk_portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700101/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * to that number per second. This prevents DoS attacks, but results in
103 * audit records being dropped. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800104static u32 audit_rate_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Richard Guy Briggs40c07752013-10-22 13:28:49 -0400106/* Number of outstanding audit_buffers allowed.
107 * When set to zero, this means unlimited. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800108static u32 audit_backlog_limit = 64;
Richard Guy Briggse789e562013-09-12 23:03:51 -0400109#define AUDIT_BACKLOG_WAIT_TIME (60 * HZ)
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800110static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100112/* The identity of the user shutting down the audit system. */
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800113kuid_t audit_sig_uid = INVALID_UID;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100114pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -0400115u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* 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*/
124static atomic_t audit_lost = ATOMIC_INIT(0);
125
126/* The netlink socket. */
127static struct sock *audit_sock;
Richard Guy Briggsc0a8d9b2014-05-26 10:59:28 -0400128static int audit_net_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Amy Griffisf368c07d2006-04-07 16:55:56 -0400130/* Hash for inode-based rules */
131struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
132
David Woodhouseb7d11252005-05-19 10:56:58 +0100133/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
135 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700137static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static LIST_HEAD(audit_freelist);
139
Paul Moorec6480202016-11-29 16:53:25 -0500140/* queue msgs to send via kauditd_task */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500141static struct sk_buff_head audit_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500142/* queue msgs due to temporary unicast send problems */
143static struct sk_buff_head audit_retry_queue;
144/* queue msgs waiting for new auditd connection */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500145static struct sk_buff_head audit_hold_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500146
147/* queue servicing thread */
David Woodhouseb7d11252005-05-19 10:56:58 +0100148static struct task_struct *kauditd_task;
149static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500150
151/* waitqueue for callers who are blocked on the audit backlog */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100152static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Parisb0fed402013-05-22 12:54:49 -0400154static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION,
155 .mask = -1,
156 .features = 0,
157 .lock = 0,};
158
Eric Paris21b85c32013-05-23 14:26:00 -0400159static char *audit_feature_names[2] = {
Eric Parisd040e5a2013-05-24 09:18:04 -0400160 "only_unset_loginuid",
Eric Paris21b85c32013-05-23 14:26:00 -0400161 "loginuid_immutable",
Eric Parisb0fed402013-05-22 12:54:49 -0400162};
163
164
Amy Griffisf368c07d2006-04-07 16:55:56 -0400165/* Serialize requests from userspace. */
Al Viro916d7572009-06-24 00:02:38 -0400166DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
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. */
182struct audit_buffer {
183 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100184 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400186 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Eric Parisf09ac9d2008-04-18 10:11:04 -0400189struct audit_reply {
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400190 __u32 portid;
Eric W. Biederman638a0fd2014-02-28 10:49:05 -0800191 struct net *net;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400192 struct sk_buff *skb;
193};
194
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400195static void audit_set_portid(struct audit_buffer *ab, __u32 portid)
Steve Grubbc0404992005-05-13 18:17:42 +0100196{
Eric Paris50397bd2008-01-07 18:14:19 -0500197 if (ab) {
198 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400199 nlh->nlmsg_pid = portid;
Eric Paris50397bd2008-01-07 18:14:19 -0500200 }
Steve Grubbc0404992005-05-13 18:17:42 +0100201}
202
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000203void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Joe Perchesd957f7b2014-01-14 10:33:12 -0800205 switch (audit_failure) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case AUDIT_FAIL_SILENT:
207 break;
208 case AUDIT_FAIL_PRINTK:
Eric Paris320f1b12008-01-23 22:55:05 -0500209 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800210 pr_err("%s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 case AUDIT_FAIL_PANIC:
Eric Parisb29ee872008-02-21 15:53:05 -0500213 /* test audit_pid since printk is always losey, why bother? */
214 if (audit_pid)
215 panic("audit: %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
218}
219
220static 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 Dunlapb0dd25a2005-09-13 12:47:11 -0700249/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700257void 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 Paris320f1b12008-01-23 22:55:05 -0500280 if (printk_ratelimit())
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800281 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
Eric Paris320f1b12008-01-23 22:55:05 -0500282 atomic_read(&audit_lost),
283 audit_rate_limit,
284 audit_backlog_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 audit_panic(message);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800289static int audit_log_config_change(char *function_name, u32 new, u32 old,
Eric Paris25323862008-04-18 10:09:25 -0400290 int allow_changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Eric Paris1a6b9f22008-01-07 17:09:31 -0500292 struct audit_buffer *ab;
293 int rc = 0;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500294
Eric Paris1a6b9f22008-01-07 17:09:31 -0500295 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
Kees Cook0644ec02013-01-11 14:32:07 -0800296 if (unlikely(!ab))
297 return rc;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800298 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
Eric Paris4d3fb702013-04-30 09:53:34 -0400299 audit_log_session_info(ab);
Eric Parisb122c372013-04-19 15:00:33 -0400300 rc = audit_log_task_context(ab);
301 if (rc)
302 allow_changes = 0; /* Something weird, deny request */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500303 audit_log_format(ab, " res=%d", allow_changes);
304 audit_log_end(ab);
305 return rc;
306}
307
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800308static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500309{
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800310 int allow_changes, rc = 0;
311 u32 old = *to_change;
Eric Paris1a6b9f22008-01-07 17:09:31 -0500312
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 Parisdc9eb692013-04-19 13:23:09 -0400320 rc = audit_log_config_change(function_name, new, old, allow_changes);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500321 if (rc)
322 allow_changes = 0;
323 }
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500324
325 /* If we are allowed, make the change */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500326 if (allow_changes == 1)
327 *to_change = new;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500328 /* Not allowed, update reason */
329 else if (rc == 0)
330 rc = -EPERM;
331 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800334static int audit_set_rate_limit(u32 limit)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500335{
Eric Parisdc9eb692013-04-19 13:23:09 -0400336 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500337}
338
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800339static int audit_set_backlog_limit(u32 limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Eric Parisdc9eb692013-04-19 13:23:09 -0400341 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800344static int audit_set_backlog_wait_time(u32 timeout)
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400345{
346 return audit_do_config_change("audit_backlog_wait_time",
Paul Moore31975422016-11-29 16:53:25 -0500347 &audit_backlog_wait_time, timeout);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400348}
349
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800350static int audit_set_enabled(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Eric Parisb593d382008-01-08 17:38:31 -0500352 int rc;
Pranith Kumar724e7bf2015-03-11 14:08:19 -0400353 if (state > AUDIT_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500355
Eric Parisdc9eb692013-04-19 13:23:09 -0400356 rc = audit_do_config_change("audit_enabled", &audit_enabled, state);
Eric Parisb593d382008-01-08 17:38:31 -0500357 if (!rc)
358 audit_ever_enabled |= !!state;
359
360 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800363static int audit_set_failure(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (state != AUDIT_FAIL_SILENT
366 && state != AUDIT_FAIL_PRINTK
367 && state != AUDIT_FAIL_PANIC)
368 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500369
Eric Parisdc9eb692013-04-19 13:23:09 -0400370 return audit_do_config_change("audit_failure", &audit_failure, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Eric Parisf3d357b2008-04-18 10:02:28 -0400373/*
Eric Paris038cbcf2009-06-11 14:31:35 -0400374 * For one reason or another this nlh isn't getting delivered to the userspace
375 * audit daemon, just send it to printk.
376 */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500377static void kauditd_printk_skb(struct sk_buff *skb)
Eric Paris038cbcf2009-06-11 14:31:35 -0400378{
379 struct nlmsghdr *nlh = nlmsg_hdr(skb);
David S. Millerc64e66c2012-06-26 21:45:21 -0700380 char *data = nlmsg_data(nlh);
Eric Paris038cbcf2009-06-11 14:31:35 -0400381
382 if (nlh->nlmsg_type != AUDIT_EOE) {
383 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800384 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
Eric Paris038cbcf2009-06-11 14:31:35 -0400385 else
Josh Boyerf1283522014-03-05 16:29:55 -0500386 audit_log_lost("printk limit exceeded");
Eric Paris038cbcf2009-06-11 14:31:35 -0400387 }
Eric Paris038cbcf2009-06-11 14:31:35 -0400388}
389
Paul Moorec6480202016-11-29 16:53:25 -0500390/**
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 */
402static void kauditd_hold_skb(struct sk_buff *skb)
Eric Parisf3d357b2008-04-18 10:02:28 -0400403{
Paul Moorec6480202016-11-29 16:53:25 -0500404 /* 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 Briggs32a1dba2015-11-04 08:23:50 -0500407
Paul Moorec6480202016-11-29 16:53:25 -0500408 /* 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 */
435static 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
448 * queue into the hold queue in case auditd reconnects.
449 */
450static void auditd_reset(void)
451{
452 struct sk_buff *skb;
453
454 /* break the connection */
455 audit_pid = 0;
456 audit_sock = NULL;
457
458 /* flush all of the retry queue to the hold queue */
459 while ((skb = skb_dequeue(&audit_retry_queue)))
460 kauditd_hold_skb(skb);
461}
462
463/**
464 * kauditd_send_unicast_skb - Send a record via unicast to auditd
465 * @skb: audit record
466 */
467static int kauditd_send_unicast_skb(struct sk_buff *skb)
468{
469 int rc;
470
471 /* get an extra skb reference in case we fail to send */
Eric Parisf3d357b2008-04-18 10:02:28 -0400472 skb_get(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500473 rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
474 if (rc >= 0) {
Neil Horman70d4bf62010-07-20 06:45:56 +0000475 consume_skb(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500476 rc = 0;
477 }
478
479 return rc;
Eric Parisf3d357b2008-04-18 10:02:28 -0400480}
481
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500482/*
Paul Moorec6480202016-11-29 16:53:25 -0500483 * kauditd_send_multicast_skb - Send a record to any multicast listeners
484 * @skb: audit record
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400485 *
Paul Moorec6480202016-11-29 16:53:25 -0500486 * Description:
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400487 * This function doesn't consume an skb as might be expected since it has to
488 * copy it anyways.
489 */
Paul Moorec6480202016-11-29 16:53:25 -0500490static void kauditd_send_multicast_skb(struct sk_buff *skb)
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400491{
Paul Moorec6480202016-11-29 16:53:25 -0500492 struct sk_buff *copy;
493 struct audit_net *aunet = net_generic(&init_net, audit_net_id);
494 struct sock *sock = aunet->nlsk;
495 struct nlmsghdr *nlh;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400496
Richard Guy Briggs7f74ecd2014-04-22 21:31:58 -0400497 if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG))
498 return;
499
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400500 /*
501 * The seemingly wasteful skb_copy() rather than bumping the refcount
502 * using skb_get() is necessary because non-standard mods are made to
503 * the skb by the original kaudit unicast socket send routine. The
504 * existing auditd daemon assumes this breakage. Fixing this would
505 * require co-ordinating a change in the established protocol between
506 * the kaudit kernel subsystem and the auditd userspace code. There is
507 * no reason for new multicast clients to continue with this
508 * non-compliance.
509 */
Paul Moorec6480202016-11-29 16:53:25 -0500510 copy = skb_copy(skb, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400511 if (!copy)
512 return;
Paul Moorec6480202016-11-29 16:53:25 -0500513 nlh = nlmsg_hdr(copy);
514 nlh->nlmsg_len = skb->len;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400515
Paul Moorec6480202016-11-29 16:53:25 -0500516 nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400517}
518
Paul Moorec6480202016-11-29 16:53:25 -0500519/**
520 * kauditd_wake_condition - Return true when it is time to wake kauditd_thread
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500521 *
Paul Moorec6480202016-11-29 16:53:25 -0500522 * Description:
523 * This function is for use by the wait_event_freezable() call in
524 * kauditd_thread().
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500525 */
Paul Moorec6480202016-11-29 16:53:25 -0500526static int kauditd_wake_condition(void)
David Woodhouseb7d11252005-05-19 10:56:58 +0100527{
Paul Moorec6480202016-11-29 16:53:25 -0500528 static int pid_last = 0;
529 int rc;
530 int pid = audit_pid;
David Woodhouseb7d11252005-05-19 10:56:58 +0100531
Paul Moorec6480202016-11-29 16:53:25 -0500532 /* wake on new messages or a change in the connected auditd */
533 rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last);
534 if (rc)
535 pid_last = pid;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500536
Paul Moorec6480202016-11-29 16:53:25 -0500537 return rc;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500538}
539
David Woodhouseb7d11252005-05-19 10:56:58 +0100540static int kauditd_thread(void *dummy)
541{
Paul Moorec6480202016-11-29 16:53:25 -0500542 int rc;
543 int auditd = 0;
544 int reschedule = 0;
Paul Moore4aa838722016-11-29 16:53:24 -0500545 struct sk_buff *skb;
546 struct nlmsghdr *nlh;
547
Paul Moorec6480202016-11-29 16:53:25 -0500548#define UNICAST_RETRIES 5
549#define AUDITD_BAD(x,y) \
550 ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES)
551
552 /* NOTE: we do invalidate the auditd connection flag on any sending
553 * errors, but we only "restore" the connection flag at specific places
554 * in the loop in order to help ensure proper ordering of audit
555 * records */
556
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700557 set_freezable();
Andrew Morton4899b8b2006-10-06 00:43:48 -0700558 while (!kthread_should_stop()) {
Paul Moorec6480202016-11-29 16:53:25 -0500559 /* NOTE: possible area for future improvement is to look at
560 * the hold and retry queues, since only this thread
561 * has access to these queues we might be able to do
562 * our own queuing and skip some/all of the locking */
Eric Parisf3d357b2008-04-18 10:02:28 -0400563
Paul Moorec6480202016-11-29 16:53:25 -0500564 /* NOTE: it might be a fun experiment to split the hold and
565 * retry queue handling to another thread, but the
566 * synchronization issues and other overhead might kill
567 * any performance gains */
568
569 /* attempt to flush the hold queue */
570 while (auditd && (skb = skb_dequeue(&audit_hold_queue))) {
571 rc = kauditd_send_unicast_skb(skb);
572 if (rc) {
573 /* requeue to the same spot */
574 skb_queue_head(&audit_hold_queue, skb);
575
576 auditd = 0;
577 if (AUDITD_BAD(rc, reschedule)) {
578 auditd_reset();
579 reschedule = 0;
580 }
581 } else
582 /* we were able to send successfully */
583 reschedule = 0;
584 }
585
586 /* attempt to flush the retry queue */
587 while (auditd && (skb = skb_dequeue(&audit_retry_queue))) {
588 rc = kauditd_send_unicast_skb(skb);
589 if (rc) {
590 auditd = 0;
591 if (AUDITD_BAD(rc, reschedule)) {
592 kauditd_hold_skb(skb);
593 auditd_reset();
594 reschedule = 0;
595 } else
596 /* temporary problem (we hope), queue
597 * to the same spot and retry */
598 skb_queue_head(&audit_retry_queue, skb);
599 } else
600 /* we were able to send successfully */
601 reschedule = 0;
602 }
603
604 /* standard queue processing, try to be as quick as possible */
605quick_loop:
Paul Mooreaf8b8242016-11-29 16:53:24 -0500606 skb = skb_dequeue(&audit_queue);
David Woodhouseb7d11252005-05-19 10:56:58 +0100607 if (skb) {
Paul Moorec6480202016-11-29 16:53:25 -0500608 /* setup the netlink header, see the comments in
609 * kauditd_send_multicast_skb() for length quirks */
Paul Moore4aa838722016-11-29 16:53:24 -0500610 nlh = nlmsg_hdr(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500611 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
Paul Moore4aa838722016-11-29 16:53:24 -0500612
Paul Moorec6480202016-11-29 16:53:25 -0500613 /* attempt to send to any multicast listeners */
614 kauditd_send_multicast_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500615
Paul Moorec6480202016-11-29 16:53:25 -0500616 /* attempt to send to auditd, queue on failure */
617 if (auditd) {
618 rc = kauditd_send_unicast_skb(skb);
619 if (rc) {
620 auditd = 0;
621 if (AUDITD_BAD(rc, reschedule)) {
622 auditd_reset();
623 reschedule = 0;
624 }
Paul Moore4aa838722016-11-29 16:53:24 -0500625
Paul Moorec6480202016-11-29 16:53:25 -0500626 /* move to the retry queue */
627 kauditd_retry_skb(skb);
628 } else
629 /* everything is working so go fast! */
630 goto quick_loop;
631 } else if (reschedule)
632 /* we are currently having problems, move to
633 * the retry queue */
634 kauditd_retry_skb(skb);
Eric Paris038cbcf2009-06-11 14:31:35 -0400635 else
Paul Moorec6480202016-11-29 16:53:25 -0500636 /* dump the message via printk and hold it */
637 kauditd_hold_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500638 } else {
Paul Moorec6480202016-11-29 16:53:25 -0500639 /* we have flushed the backlog so wake everyone */
Paul Moore4aa838722016-11-29 16:53:24 -0500640 wake_up(&audit_backlog_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500641
642 /* if everything is okay with auditd (if present), go
643 * to sleep until there is something new in the queue
644 * or we have a change in the connected auditd;
645 * otherwise simply reschedule to give things a chance
646 * to recover */
647 if (reschedule) {
648 set_current_state(TASK_INTERRUPTIBLE);
649 schedule();
650 } else
651 wait_event_freezable(kauditd_wait,
652 kauditd_wake_condition());
653
654 /* update the auditd connection status */
655 auditd = (audit_pid ? 1 : 0);
David Woodhouseb7d11252005-05-19 10:56:58 +0100656 }
657 }
Paul Moorec6480202016-11-29 16:53:25 -0500658
Andrew Morton4899b8b2006-10-06 00:43:48 -0700659 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100660}
661
Al Viro9044e6b2006-05-22 01:09:24 -0400662int audit_send_list(void *_dest)
663{
664 struct audit_netlink_list *dest = _dest;
Al Viro9044e6b2006-05-22 01:09:24 -0400665 struct sk_buff *skb;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800666 struct net *net = dest->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400667 struct audit_net *aunet = net_generic(net, audit_net_id);
Al Viro9044e6b2006-05-22 01:09:24 -0400668
669 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400670 mutex_lock(&audit_cmd_mutex);
671 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400672
673 while ((skb = __skb_dequeue(&dest->q)) != NULL)
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400674 netlink_unicast(aunet->nlsk, skb, dest->portid, 0);
Al Viro9044e6b2006-05-22 01:09:24 -0400675
Eric W. Biederman48095d92014-02-03 17:25:33 -0800676 put_net(net);
Al Viro9044e6b2006-05-22 01:09:24 -0400677 kfree(dest);
678
679 return 0;
680}
681
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400682struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
Stephen Hemmingerb8800aa2010-10-20 17:23:50 -0700683 int multi, const void *payload, int size)
Al Viro9044e6b2006-05-22 01:09:24 -0400684{
685 struct sk_buff *skb;
686 struct nlmsghdr *nlh;
Al Viro9044e6b2006-05-22 01:09:24 -0400687 void *data;
688 int flags = multi ? NLM_F_MULTI : 0;
689 int t = done ? NLMSG_DONE : type;
690
Eric Parisee080e62009-06-11 14:31:35 -0400691 skb = nlmsg_new(size, GFP_KERNEL);
Al Viro9044e6b2006-05-22 01:09:24 -0400692 if (!skb)
693 return NULL;
694
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400695 nlh = nlmsg_put(skb, portid, seq, t, size, flags);
David S. Millerc64e66c2012-06-26 21:45:21 -0700696 if (!nlh)
697 goto out_kfree_skb;
698 data = nlmsg_data(nlh);
Al Viro9044e6b2006-05-22 01:09:24 -0400699 memcpy(data, payload, size);
700 return skb;
701
David S. Millerc64e66c2012-06-26 21:45:21 -0700702out_kfree_skb:
703 kfree_skb(skb);
Al Viro9044e6b2006-05-22 01:09:24 -0400704 return NULL;
705}
706
Eric Parisf09ac9d2008-04-18 10:11:04 -0400707static int audit_send_reply_thread(void *arg)
708{
709 struct audit_reply *reply = (struct audit_reply *)arg;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800710 struct net *net = reply->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400711 struct audit_net *aunet = net_generic(net, audit_net_id);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400712
713 mutex_lock(&audit_cmd_mutex);
714 mutex_unlock(&audit_cmd_mutex);
715
716 /* Ignore failure. It'll only happen if the sender goes away,
717 because our timeout is set to infinite. */
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400718 netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0);
Eric W. Biederman48095d92014-02-03 17:25:33 -0800719 put_net(net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400720 kfree(reply);
721 return 0;
722}
Paul Moorec6480202016-11-29 16:53:25 -0500723
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700724/**
725 * audit_send_reply - send an audit reply message via netlink
Eric W. Biedermand211f1772014-03-08 15:31:54 -0800726 * @request_skb: skb of request we are replying to (used to target the reply)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700727 * @seq: sequence number
728 * @type: audit message type
729 * @done: done (last) flag
730 * @multi: multi-part message flag
731 * @payload: payload data
732 * @size: payload size
733 *
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400734 * Allocates an skb, builds the netlink message, and sends it to the port id.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700735 * No failure notifications.
736 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800737static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400738 int multi, const void *payload, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800740 u32 portid = NETLINK_CB(request_skb).portid;
741 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400742 struct sk_buff *skb;
743 struct task_struct *tsk;
744 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
745 GFP_KERNEL);
746
747 if (!reply)
748 return;
749
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400750 skb = audit_make_reply(portid, seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!skb)
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700752 goto out;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400753
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800754 reply->net = get_net(net);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400755 reply->portid = portid;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400756 reply->skb = skb;
757
758 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700759 if (!IS_ERR(tsk))
760 return;
761 kfree_skb(skb);
762out:
763 kfree(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
768 * control messages.
769 */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700770static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 int err = 0;
773
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400774 /* Only support initial user namespace for now. */
Eric Parisaa4af832014-03-30 19:07:54 -0400775 /*
776 * We return ECONNREFUSED because it tricks userspace into thinking
777 * that audit was not configured into the kernel. Lots of users
778 * configure their PAM stack (because that's what the distro does)
779 * to reject login if unable to send messages to audit. If we return
780 * ECONNREFUSED the PAM stack thinks the kernel does not have audit
781 * configured in and will let login proceed. If we return EPERM
782 * userspace will reject all logins. This should be removed when we
783 * support non init namespaces!!
784 */
Linus Torvalds0b747172014-04-12 12:38:53 -0700785 if (current_user_ns() != &init_user_ns)
Eric Parisaa4af832014-03-30 19:07:54 -0400786 return -ECONNREFUSED;
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 switch (msg_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case AUDIT_ADD:
791 case AUDIT_DEL:
Eric Paris18900902013-04-18 19:16:36 -0400792 return -EOPNOTSUPP;
793 case AUDIT_GET:
794 case AUDIT_SET:
Eric Parisb0fed402013-05-22 12:54:49 -0400795 case AUDIT_GET_FEATURE:
796 case AUDIT_SET_FEATURE:
Eric Paris18900902013-04-18 19:16:36 -0400797 case AUDIT_LIST_RULES:
798 case AUDIT_ADD_RULE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500799 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100800 case AUDIT_SIGNAL_INFO:
Miloslav Trmac522ed772007-07-15 23:40:56 -0700801 case AUDIT_TTY_GET:
802 case AUDIT_TTY_SET:
Al Viro74c3cbe2007-07-22 08:04:18 -0400803 case AUDIT_TRIM:
804 case AUDIT_MAKE_EQUIV:
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400805 /* Only support auditd and auditctl in initial pid namespace
806 * for now. */
Ameen Ali5985de62015-02-23 15:38:00 -0500807 if (task_active_pid_ns(current) != &init_pid_ns)
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400808 return -EPERM;
809
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700810 if (!netlink_capable(skb, CAP_AUDIT_CONTROL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 err = -EPERM;
812 break;
Steve Grubb05474102005-05-21 00:18:37 +0100813 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -0700814 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
815 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700816 if (!netlink_capable(skb, CAP_AUDIT_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 err = -EPERM;
818 break;
819 default: /* bad msg */
820 err = -EINVAL;
821 }
822
823 return err;
824}
825
Paul Moore233a6862015-11-04 08:23:52 -0500826static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type)
Eric Paris50397bd2008-01-07 18:14:19 -0500827{
Eric Parisdc9eb692013-04-19 13:23:09 -0400828 uid_t uid = from_kuid(&init_user_ns, current_uid());
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500829 pid_t pid = task_tgid_nr(current);
Eric Paris50397bd2008-01-07 18:14:19 -0500830
Tyler Hicks0868a5e2013-07-25 18:02:55 -0700831 if (!audit_enabled && msg_type != AUDIT_USER_AVC) {
Eric Paris50397bd2008-01-07 18:14:19 -0500832 *ab = NULL;
Paul Moore233a6862015-11-04 08:23:52 -0500833 return;
Eric Paris50397bd2008-01-07 18:14:19 -0500834 }
835
836 *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
Kees Cook0644ec02013-01-11 14:32:07 -0800837 if (unlikely(!*ab))
Paul Moore233a6862015-11-04 08:23:52 -0500838 return;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500839 audit_log_format(*ab, "pid=%d uid=%u", pid, uid);
Eric Paris4d3fb702013-04-30 09:53:34 -0400840 audit_log_session_info(*ab);
Eric Parisb122c372013-04-19 15:00:33 -0400841 audit_log_task_context(*ab);
Eric Paris50397bd2008-01-07 18:14:19 -0500842}
843
Eric Parisb0fed402013-05-22 12:54:49 -0400844int is_audit_feature_set(int i)
845{
846 return af.features & AUDIT_FEATURE_TO_MASK(i);
847}
848
849
850static int audit_get_feature(struct sk_buff *skb)
851{
852 u32 seq;
853
854 seq = nlmsg_hdr(skb)->nlmsg_seq;
855
Richard Guy Briggs9ef91512014-08-24 20:37:52 -0400856 audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af));
Eric Parisb0fed402013-05-22 12:54:49 -0400857
858 return 0;
859}
860
861static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature,
862 u32 old_lock, u32 new_lock, int res)
863{
864 struct audit_buffer *ab;
865
Gao fengb6c50fe2013-11-01 19:34:43 +0800866 if (audit_enabled == AUDIT_OFF)
867 return;
868
Eric Parisb0fed402013-05-22 12:54:49 -0400869 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
Richard Guy Briggsad2ac262014-01-07 13:08:41 -0500870 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -0400871 audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d",
Eric Parisb0fed402013-05-22 12:54:49 -0400872 audit_feature_names[which], !!old_feature, !!new_feature,
873 !!old_lock, !!new_lock, res);
874 audit_log_end(ab);
875}
876
877static int audit_set_feature(struct sk_buff *skb)
878{
879 struct audit_features *uaf;
880 int i;
881
Fabian Frederick6eed9b22014-06-03 22:05:10 +0200882 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -0400883 uaf = nlmsg_data(nlmsg_hdr(skb));
884
885 /* if there is ever a version 2 we should handle that here */
886
887 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
888 u32 feature = AUDIT_FEATURE_TO_MASK(i);
889 u32 old_feature, new_feature, old_lock, new_lock;
890
891 /* if we are not changing this feature, move along */
892 if (!(feature & uaf->mask))
893 continue;
894
895 old_feature = af.features & feature;
896 new_feature = uaf->features & feature;
897 new_lock = (uaf->lock | af.lock) & feature;
898 old_lock = af.lock & feature;
899
900 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +0800901 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -0400902 audit_log_feature_change(i, old_feature, new_feature,
903 old_lock, new_lock, 0);
904 return -EPERM;
905 }
906 }
907 /* nothing invalid, do the changes */
908 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
909 u32 feature = AUDIT_FEATURE_TO_MASK(i);
910 u32 old_feature, new_feature, old_lock, new_lock;
911
912 /* if we are not changing this feature, move along */
913 if (!(feature & uaf->mask))
914 continue;
915
916 old_feature = af.features & feature;
917 new_feature = uaf->features & feature;
918 old_lock = af.lock & feature;
919 new_lock = (uaf->lock | af.lock) & feature;
920
921 if (new_feature != old_feature)
922 audit_log_feature_change(i, old_feature, new_feature,
923 old_lock, new_lock, 1);
924
925 if (new_feature)
926 af.features |= feature;
927 else
928 af.features &= ~feature;
929 af.lock |= new_lock;
930 }
931
932 return 0;
933}
934
Richard Guy Briggs133e1e52016-01-25 18:04:15 -0500935static int audit_replace(pid_t pid)
936{
937 struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0,
938 &pid, sizeof(pid));
939
940 if (!skb)
941 return -ENOMEM;
942 return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
946{
Eric Parisdc9eb692013-04-19 13:23:09 -0400947 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100950 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -0400952 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -0500953 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -0400954 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700956 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (err)
958 return err;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -0700961 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400964 case AUDIT_GET: {
965 struct audit_status s;
966 memset(&s, 0, sizeof(s));
967 s.enabled = audit_enabled;
968 s.failure = audit_failure;
969 s.pid = audit_pid;
970 s.rate_limit = audit_rate_limit;
971 s.backlog_limit = audit_backlog_limit;
972 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -0500973 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -0500974 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Paul Moore31975422016-11-29 16:53:25 -0500975 s.backlog_wait_time = audit_backlog_wait_time;
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800976 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400978 }
979 case AUDIT_SET: {
980 struct audit_status s;
981 memset(&s, 0, sizeof(s));
982 /* guard against past and future API changes */
983 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
984 if (s.mask & AUDIT_STATUS_ENABLED) {
985 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +0800986 if (err < 0)
987 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400989 if (s.mask & AUDIT_STATUS_FAILURE) {
990 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +0800991 if (err < 0)
992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400994 if (s.mask & AUDIT_STATUS_PID) {
995 int new_pid = s.pid;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -0500996 pid_t requesting_pid = task_tgid_vnr(current);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500997
Richard Guy Briggs935c9e72016-01-25 18:04:15 -0500998 if ((!new_pid) && (requesting_pid != audit_pid)) {
999 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs34eab0a2013-06-21 14:47:13 -04001000 return -EACCES;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001001 }
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001002 if (audit_pid && new_pid &&
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001003 audit_replace(requesting_pid) != -ECONNREFUSED) {
1004 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001005 return -EEXIST;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001006 }
Eric Paris1a6b9f22008-01-07 17:09:31 -05001007 if (audit_enabled != AUDIT_OFF)
Eric Parisdc9eb692013-04-19 13:23:09 -04001008 audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
Eric Paris1a6b9f22008-01-07 17:09:31 -05001009 audit_pid = new_pid;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001010 audit_nlk_portid = NETLINK_CB(skb).portid;
Gao fengde92fc92013-12-17 11:10:42 +08001011 audit_sock = skb->sk;
Paul Mooree1d16622016-11-29 16:53:25 -05001012 wake_up_interruptible(&kauditd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001014 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1015 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001016 if (err < 0)
1017 return err;
1018 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001019 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001020 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001021 if (err < 0)
1022 return err;
1023 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001024 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1025 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1026 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001027 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001028 return -EINVAL;
1029 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1030 if (err < 0)
1031 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001034 }
Eric Parisb0fed402013-05-22 12:54:49 -04001035 case AUDIT_GET_FEATURE:
1036 err = audit_get_feature(skb);
1037 if (err)
1038 return err;
1039 break;
1040 case AUDIT_SET_FEATURE:
1041 err = audit_set_feature(skb);
1042 if (err)
1043 return err;
1044 break;
Steve Grubb05474102005-05-21 00:18:37 +01001045 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001046 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1047 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001048 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1049 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001050
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001051 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001052 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001053 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001054 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001055 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001056 if (err)
1057 break;
1058 }
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001059 mutex_unlock(&audit_cmd_mutex);
Eric Parisdc9eb692013-04-19 13:23:09 -04001060 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001061 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001062 audit_log_format(ab, " msg='%.*s'",
1063 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001064 (char *)data);
1065 else {
1066 int size;
1067
Eric Parisf7616102013-04-11 11:25:00 -04001068 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001069 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001070 if (size > 0 &&
1071 ((unsigned char *)data)[size - 1] == '\0')
1072 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001073 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001074 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001075 audit_set_portid(ab, NETLINK_CB(skb).portid);
Eric Paris50397bd2008-01-07 18:14:19 -05001076 audit_log_end(ab);
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001077 mutex_lock(&audit_cmd_mutex);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001080 case AUDIT_ADD_RULE:
1081 case AUDIT_DEL_RULE:
1082 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1083 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001084 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001085 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1086 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001087 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001088 return -EPERM;
1089 }
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001090 err = audit_rule_change(msg_type, NETLINK_CB(skb).portid,
Eric Parisdc9eb692013-04-19 13:23:09 -04001091 seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001093 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001094 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001095 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001096 case AUDIT_TRIM:
1097 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001098 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001099 audit_log_format(ab, " op=trim res=1");
1100 audit_log_end(ab);
1101 break;
1102 case AUDIT_MAKE_EQUIV: {
1103 void *bufp = data;
1104 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001105 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001106 char *old, *new;
1107
1108 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001109 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001110 break;
1111 memcpy(sizes, bufp, 2 * sizeof(u32));
1112 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001113 msglen -= 2 * sizeof(u32);
1114 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001115 if (IS_ERR(old)) {
1116 err = PTR_ERR(old);
1117 break;
1118 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001119 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001120 if (IS_ERR(new)) {
1121 err = PTR_ERR(new);
1122 kfree(old);
1123 break;
1124 }
1125 /* OK, here comes... */
1126 err = audit_tag_tree(old, new);
1127
Eric Parisdc9eb692013-04-19 13:23:09 -04001128 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001129
Al Viro74c3cbe2007-07-22 08:04:18 -04001130 audit_log_format(ab, " op=make_equiv old=");
1131 audit_log_untrustedstring(ab, old);
1132 audit_log_format(ab, " new=");
1133 audit_log_untrustedstring(ab, new);
1134 audit_log_format(ab, " res=%d", !err);
1135 audit_log_end(ab);
1136 kfree(old);
1137 kfree(new);
1138 break;
1139 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001140 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001141 len = 0;
1142 if (audit_sig_sid) {
1143 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1144 if (err)
1145 return err;
1146 }
Al Viroe1396062006-05-25 10:19:47 -04001147 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1148 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001149 if (audit_sig_sid)
1150 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001151 return -ENOMEM;
1152 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001153 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001154 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001155 if (audit_sig_sid) {
1156 memcpy(sig_data->ctx, ctx, len);
1157 security_release_secctx(ctx, len);
1158 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001159 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1160 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001161 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001162 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001163 case AUDIT_TTY_GET: {
1164 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001165 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001166
Peter Hurley2e28d382016-01-09 22:55:33 -08001167 t = READ_ONCE(current->signal->audit_tty);
1168 s.enabled = t & AUDIT_TTY_ENABLE;
1169 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001170
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001171 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001172 break;
1173 }
1174 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001175 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001176 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001177 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001178
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001179 memset(&s, 0, sizeof(s));
1180 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001181 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001182 /* check if new data is valid */
1183 if ((s.enabled != 0 && s.enabled != 1) ||
1184 (s.log_passwd != 0 && s.log_passwd != 1))
1185 err = -EINVAL;
1186
Peter Hurley2e28d382016-01-09 22:55:33 -08001187 if (err)
1188 t = READ_ONCE(current->signal->audit_tty);
1189 else {
1190 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1191 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001192 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001193 old.enabled = t & AUDIT_TTY_ENABLE;
1194 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001195
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001196 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001197 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1198 " old-log_passwd=%d new-log_passwd=%d res=%d",
1199 old.enabled, s.enabled, old.log_passwd,
1200 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001201 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001202 break;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 default:
1205 err = -EINVAL;
1206 break;
1207 }
1208
1209 return err < 0 ? err : 0;
1210}
1211
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001212/*
Eric Parisea7ae602009-06-11 14:31:35 -04001213 * Get message from skb. Each message is processed by audit_receive_msg.
1214 * Malformed skbs with wrong length are discarded silently.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001215 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001216static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Eric Parisea7ae602009-06-11 14:31:35 -04001218 struct nlmsghdr *nlh;
1219 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001220 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001221 * if the nlmsg_len was not aligned
1222 */
1223 int len;
1224 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric Parisea7ae602009-06-11 14:31:35 -04001226 nlh = nlmsg_hdr(skb);
1227 len = skb->len;
1228
Hong zhi guo94191212013-03-27 06:49:06 +00001229 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001230 err = audit_receive_msg(skb, nlh);
1231 /* if err or if this message says it wants a response */
1232 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 netlink_ack(skb, nlh, err);
Eric Parisea7ae602009-06-11 14:31:35 -04001234
Alexandru Copot2851da52013-03-28 23:31:29 +02001235 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/* Receive messages from netlink socket. */
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001240static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Amy Griffisf368c07d2006-04-07 16:55:56 -04001242 mutex_lock(&audit_cmd_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001243 audit_receive_skb(skb);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001244 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001247/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001248static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001249{
1250 if (!capable(CAP_AUDIT_READ))
1251 return -EPERM;
1252
1253 return 0;
1254}
1255
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001256static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001258 struct netlink_kernel_cfg cfg = {
1259 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001260 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001261 .flags = NL_CFG_F_NONROOT_RECV,
1262 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001263 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001264
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001265 struct audit_net *aunet = net_generic(net, audit_net_id);
1266
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001267 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
Gao feng11ee39e2013-12-17 11:10:41 +08001268 if (aunet->nlsk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001269 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001270 return -ENOMEM;
1271 }
1272 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001273 return 0;
1274}
1275
1276static void __net_exit audit_net_exit(struct net *net)
1277{
1278 struct audit_net *aunet = net_generic(net, audit_net_id);
1279 struct sock *sock = aunet->nlsk;
Paul Moorec6480202016-11-29 16:53:25 -05001280 if (sock == audit_sock)
1281 auditd_reset();
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001282
Monam Agarwale231d542014-03-24 00:16:19 +05301283 RCU_INIT_POINTER(aunet->nlsk, NULL);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001284 synchronize_net();
1285 netlink_kernel_release(sock);
1286}
1287
Richard Guy Briggs86268772013-07-16 13:18:45 -04001288static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001289 .init = audit_net_init,
1290 .exit = audit_net_exit,
1291 .id = &audit_net_id,
1292 .size = sizeof(struct audit_net),
1293};
1294
1295/* Initialize audit support at boot time. */
1296static int __init audit_init(void)
1297{
1298 int i;
1299
Eric Parisa3f07112008-11-05 12:47:09 -05001300 if (audit_initialized == AUDIT_DISABLED)
1301 return 0;
1302
Joe Perchesd957f7b2014-01-14 10:33:12 -08001303 pr_info("initializing netlink subsys (%s)\n",
1304 audit_default ? "enabled" : "disabled");
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001305 register_pernet_subsys(&audit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Paul Mooreaf8b8242016-11-29 16:53:24 -05001307 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001308 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001309 skb_queue_head_init(&audit_hold_queue);
Eric Parisa3f07112008-11-05 12:47:09 -05001310 audit_initialized = AUDIT_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 audit_enabled = audit_default;
Eric Parisb593d382008-01-08 17:38:31 -05001312 audit_ever_enabled |= !!audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001313
Amy Griffisf368c07d2006-04-07 16:55:56 -04001314 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1315 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001316
Paul Moore6c9255642016-11-29 16:53:23 -05001317 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1318 if (IS_ERR(kauditd_task)) {
1319 int err = PTR_ERR(kauditd_task);
1320 panic("audit: failed to start the kauditd thread (%d)\n", err);
1321 }
1322
1323 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327__initcall(audit_init);
1328
1329/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1330static int __init audit_enable(char *str)
1331{
1332 audit_default = !!simple_strtol(str, NULL, 0);
Eric Parisa3f07112008-11-05 12:47:09 -05001333 if (!audit_default)
1334 audit_initialized = AUDIT_DISABLED;
1335
Joe Perchesd957f7b2014-01-14 10:33:12 -08001336 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001337 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001338
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001339 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341__setup("audit=", audit_enable);
1342
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001343/* Process kernel command-line parameter at boot time.
1344 * audit_backlog_limit=<n> */
1345static int __init audit_backlog_limit_set(char *str)
1346{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001347 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001348
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001349 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001350 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1351 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001352 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001353 return 1;
1354 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001355
1356 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001357 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001358
1359 return 1;
1360}
1361__setup("audit_backlog_limit=", audit_backlog_limit_set);
1362
Chris Wright16e19042005-05-06 15:53:34 +01001363static void audit_buffer_free(struct audit_buffer *ab)
1364{
1365 unsigned long flags;
1366
Chris Wright8fc61152005-05-06 15:54:17 +01001367 if (!ab)
1368 return;
1369
Markus Elfringd865e572016-01-13 09:18:55 -05001370 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +01001371 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001372 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +01001373 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001374 else {
1375 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +01001376 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001377 }
Chris Wright16e19042005-05-06 15:53:34 +01001378 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1379}
1380
Steve Grubbc0404992005-05-13 18:17:42 +01001381static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +01001382 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001383{
1384 unsigned long flags;
1385 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +01001386 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +01001387
1388 spin_lock_irqsave(&audit_freelist_lock, flags);
1389 if (!list_empty(&audit_freelist)) {
1390 ab = list_entry(audit_freelist.next,
1391 struct audit_buffer, list);
1392 list_del(&ab->list);
1393 --audit_freelist_count;
1394 }
1395 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1396
1397 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +01001398 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +01001399 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +01001400 goto err;
Chris Wright16e19042005-05-06 15:53:34 +01001401 }
Chris Wright8fc61152005-05-06 15:54:17 +01001402
David Woodhouseb7d11252005-05-19 10:56:58 +01001403 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001404 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001405
1406 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1407 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001408 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001409
David S. Millerc64e66c2012-06-26 21:45:21 -07001410 nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0);
1411 if (!nlh)
1412 goto out_kfree_skb;
Eric Parisee080e62009-06-11 14:31:35 -04001413
Chris Wright16e19042005-05-06 15:53:34 +01001414 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001415
David S. Millerc64e66c2012-06-26 21:45:21 -07001416out_kfree_skb:
Eric Parisee080e62009-06-11 14:31:35 -04001417 kfree_skb(ab->skb);
1418 ab->skb = NULL;
Chris Wright8fc61152005-05-06 15:54:17 +01001419err:
1420 audit_buffer_free(ab);
1421 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001424/**
1425 * audit_serial - compute a serial number for the audit record
1426 *
1427 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001428 * written to user-space as soon as they are generated, so a complete
1429 * audit record may be written in several pieces. The timestamp of the
1430 * record and this serial number are used by the user-space tools to
1431 * determine which pieces belong to the same audit record. The
1432 * (timestamp,serial) tuple is unique for each syscall and is live from
1433 * syscall entry to syscall exit.
1434 *
David Woodhousebfb44962005-05-21 21:08:09 +01001435 * NOTE: Another possibility is to store the formatted records off the
1436 * audit context (for those records that have a context), and emit them
1437 * all at syscall exit. However, this could delay the reporting of
1438 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001439 * halts).
1440 */
David Woodhousebfb44962005-05-21 21:08:09 +01001441unsigned int audit_serial(void)
1442{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001443 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001444
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001445 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001446}
1447
Daniel Walker5600b892007-10-18 03:06:10 -07001448static inline void audit_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001449 struct timespec *t, unsigned int *serial)
1450{
Al Viro48887e62008-12-06 01:05:50 -05001451 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
David Woodhousebfb44962005-05-21 21:08:09 +01001452 *t = CURRENT_TIME;
1453 *serial = audit_serial();
1454 }
1455}
1456
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001457/**
1458 * audit_log_start - obtain an audit buffer
1459 * @ctx: audit_context (may be NULL)
1460 * @gfp_mask: type of allocation
1461 * @type: audit message type
1462 *
1463 * Returns audit_buffer pointer on success or NULL on error.
1464 *
1465 * Obtain an audit buffer. This routine does locking to obtain the
1466 * audit buffer, but then no locking is required for calls to
1467 * audit_log_*format. If the task (ctx) is a task that is currently in a
1468 * syscall, then the syscall is marked as auditable and an audit record
1469 * will be written at syscall exit. If there is no associated task, then
1470 * task context (ctx) should be NULL.
1471 */
Al Viro9796fdd2005-10-21 03:22:03 -04001472struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001473 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Paul Moore31975422016-11-29 16:53:25 -05001475 struct audit_buffer *ab;
1476 struct timespec t;
1477 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric Parisa3f07112008-11-05 12:47:09 -05001479 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return NULL;
1481
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001482 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001483 return NULL;
1484
Paul Moore31975422016-11-29 16:53:25 -05001485 /* don't ever fail/sleep on auditd since we need auditd to drain the
1486 * queue; also, when we are checking for auditd, compare PIDs using
1487 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg() using
1488 * a PID anchored in the caller's namespace */
1489 if (!(audit_pid && audit_pid == task_tgid_vnr(current))) {
1490 long sleep_time = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001491
Paul Moore31975422016-11-29 16:53:25 -05001492 while (audit_backlog_limit &&
1493 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1494 /* wake kauditd to try and flush the queue */
1495 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001496
Paul Moore31975422016-11-29 16:53:25 -05001497 /* sleep if we are allowed and we haven't exhausted our
1498 * backlog wait limit */
1499 if ((gfp_mask & __GFP_DIRECT_RECLAIM) &&
1500 (sleep_time > 0)) {
1501 DECLARE_WAITQUEUE(wait, current);
1502
1503 add_wait_queue_exclusive(&audit_backlog_wait,
1504 &wait);
1505 set_current_state(TASK_UNINTERRUPTIBLE);
1506 sleep_time = schedule_timeout(sleep_time);
1507 remove_wait_queue(&audit_backlog_wait, &wait);
1508 } else {
1509 if (audit_rate_check() && printk_ratelimit())
1510 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1511 skb_queue_len(&audit_queue),
1512 audit_backlog_limit);
1513 audit_log_lost("backlog limit exceeded");
1514 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001515 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001516 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001517 }
1518
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001519 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!ab) {
1521 audit_log_lost("out of memory in audit_log_start");
1522 return NULL;
1523 }
1524
David Woodhousebfb44962005-05-21 21:08:09 +01001525 audit_get_stamp(ab->ctx, &t, &serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
1527 t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return ab;
1530}
1531
Chris Wright8fc61152005-05-06 15:54:17 +01001532/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001533 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001534 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001535 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001536 *
1537 * Returns 0 (no space) on failed expansion, or available space if
1538 * successful.
1539 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001540static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001541{
Chris Wright5ac52f32005-05-06 15:54:53 +01001542 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001543 int oldtail = skb_tailroom(skb);
1544 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1545 int newtail = skb_tailroom(skb);
1546
Chris Wright5ac52f32005-05-06 15:54:53 +01001547 if (ret < 0) {
1548 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001549 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001550 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001551
1552 skb->truesize += newtail - oldtail;
1553 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001556/*
1557 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * room in the audit buffer, more room will be allocated and vsnprint
1559 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001560 * can't format message larger than 1024 bytes, so we don't either.
1561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1563 va_list args)
1564{
1565 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001566 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001567 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 if (!ab)
1570 return;
1571
Chris Wright5ac52f32005-05-06 15:54:53 +01001572 BUG_ON(!ab->skb);
1573 skb = ab->skb;
1574 avail = skb_tailroom(skb);
1575 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001576 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001577 if (!avail)
1578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001580 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001581 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (len >= avail) {
1583 /* The printk buffer is 1024 bytes long, so if we get
1584 * here and AUDIT_BUFSIZ is at least 1024, then we can
1585 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001586 avail = audit_expand(ab,
1587 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001588 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001589 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001590 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Steve Grubb168b7172005-05-19 10:24:22 +01001592 if (len > 0)
1593 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001594out_va_end:
1595 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001596out:
1597 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001600/**
1601 * audit_log_format - format a message into the audit buffer.
1602 * @ab: audit_buffer
1603 * @fmt: format string
1604 * @...: optional parameters matching @fmt string
1605 *
1606 * All the work is done in audit_log_vformat.
1607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1609{
1610 va_list args;
1611
1612 if (!ab)
1613 return;
1614 va_start(args, fmt);
1615 audit_log_vformat(ab, fmt, args);
1616 va_end(args);
1617}
1618
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001619/**
1620 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1621 * @ab: the audit_buffer
1622 * @buf: buffer to convert to hex
1623 * @len: length of @buf to be converted
1624 *
1625 * No return value; failure to expand is silently ignored.
1626 *
1627 * This function will take the passed buf and convert it into a string of
1628 * ascii hex digits. The new string is placed onto the skb.
1629 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001630void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001631 size_t len)
83c7d092005-04-29 15:54:44 +01001632{
Steve Grubb168b7172005-05-19 10:24:22 +01001633 int i, avail, new_len;
1634 unsigned char *ptr;
1635 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001636
Amy Griffis8ef2d302006-09-07 17:03:02 -04001637 if (!ab)
1638 return;
1639
Steve Grubb168b7172005-05-19 10:24:22 +01001640 BUG_ON(!ab->skb);
1641 skb = ab->skb;
1642 avail = skb_tailroom(skb);
1643 new_len = len<<1;
1644 if (new_len >= avail) {
1645 /* Round the buffer request up to the next multiple */
1646 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1647 avail = audit_expand(ab, new_len);
1648 if (!avail)
1649 return;
1650 }
1651
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001652 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001653 for (i = 0; i < len; i++)
1654 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001655 *ptr = 0;
1656 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001657}
1658
Amy Griffis9c937dc2006-06-08 23:19:31 -04001659/*
1660 * Format a string of no more than slen characters into the audit buffer,
1661 * enclosed in quote marks.
1662 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001663void audit_log_n_string(struct audit_buffer *ab, const char *string,
1664 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001665{
1666 int avail, new_len;
1667 unsigned char *ptr;
1668 struct sk_buff *skb;
1669
Amy Griffis8ef2d302006-09-07 17:03:02 -04001670 if (!ab)
1671 return;
1672
Amy Griffis9c937dc2006-06-08 23:19:31 -04001673 BUG_ON(!ab->skb);
1674 skb = ab->skb;
1675 avail = skb_tailroom(skb);
1676 new_len = slen + 3; /* enclosing quotes + null terminator */
1677 if (new_len > avail) {
1678 avail = audit_expand(ab, new_len);
1679 if (!avail)
1680 return;
1681 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001682 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001683 *ptr++ = '"';
1684 memcpy(ptr, string, slen);
1685 ptr += slen;
1686 *ptr++ = '"';
1687 *ptr = 0;
1688 skb_put(skb, slen + 2); /* don't include null terminator */
1689}
1690
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001691/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001692 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001693 * @string: string to be checked
1694 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001695 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001696bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001697{
1698 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001699 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001700 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001701 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001702 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001703 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001704}
1705
1706/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001707 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001708 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001709 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001710 * @string: string to be logged
1711 *
1712 * This code will escape a string that is passed to it if the string
1713 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001714 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001715 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001716 *
1717 * The caller specifies the number of characters in the string to log, which may
1718 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001719 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001720void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1721 size_t len)
83c7d092005-04-29 15:54:44 +01001722{
Eric Parisde6bbd12008-01-07 14:31:58 -05001723 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001724 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001725 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001726 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001727}
1728
Amy Griffis9c937dc2006-06-08 23:19:31 -04001729/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001730 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001731 * @ab: audit_buffer
1732 * @string: string to be logged
1733 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001734 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001735 * determine string length.
1736 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001737void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001738{
Eric Parisb556f8a2008-04-18 10:12:59 -04001739 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001740}
1741
Steve Grubb168b7172005-05-19 10:24:22 +01001742/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001744 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Jan Blunck44707fd2008-02-14 19:38:33 -08001746 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Chris Wright8fc61152005-05-06 15:54:17 +01001748 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001749 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Steve Grubb168b7172005-05-19 10:24:22 +01001751 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001752 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1753 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001754 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001755 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001757 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001758 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1759 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001760 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001761 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001762 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001763 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Eric Paris4d3fb702013-04-30 09:53:34 -04001766void audit_log_session_info(struct audit_buffer *ab)
1767{
Eric Paris4440e852013-11-27 17:35:17 -05001768 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001769 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1770
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001771 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001772}
1773
Eric Paris9d960982009-06-11 14:31:37 -04001774void audit_log_key(struct audit_buffer *ab, char *key)
1775{
1776 audit_log_format(ab, " key=");
1777 if (key)
1778 audit_log_untrustedstring(ab, key);
1779 else
1780 audit_log_format(ab, "(null)");
1781}
1782
Eric Parisb24a30a2013-04-30 15:30:32 -04001783void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1784{
1785 int i;
1786
1787 audit_log_format(ab, " %s=", prefix);
1788 CAP_FOR_EACH_U32(i) {
1789 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04001790 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04001791 }
1792}
1793
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04001794static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04001795{
1796 kernel_cap_t *perm = &name->fcap.permitted;
1797 kernel_cap_t *inh = &name->fcap.inheritable;
1798 int log = 0;
1799
1800 if (!cap_isclear(*perm)) {
1801 audit_log_cap(ab, "cap_fp", perm);
1802 log = 1;
1803 }
1804 if (!cap_isclear(*inh)) {
1805 audit_log_cap(ab, "cap_fi", inh);
1806 log = 1;
1807 }
1808
1809 if (log)
1810 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1811 name->fcap.fE, name->fcap_ver);
1812}
1813
1814static inline int audit_copy_fcaps(struct audit_names *name,
1815 const struct dentry *dentry)
1816{
1817 struct cpu_vfs_cap_data caps;
1818 int rc;
1819
1820 if (!dentry)
1821 return 0;
1822
1823 rc = get_vfs_caps_from_disk(dentry, &caps);
1824 if (rc)
1825 return rc;
1826
1827 name->fcap.permitted = caps.permitted;
1828 name->fcap.inheritable = caps.inheritable;
1829 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1830 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1831 VFS_CAP_REVISION_SHIFT;
1832
1833 return 0;
1834}
1835
1836/* Copy inode data into an audit_names. */
1837void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001838 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04001839{
1840 name->ino = inode->i_ino;
1841 name->dev = inode->i_sb->s_dev;
1842 name->mode = inode->i_mode;
1843 name->uid = inode->i_uid;
1844 name->gid = inode->i_gid;
1845 name->rdev = inode->i_rdev;
1846 security_inode_getsecid(inode, &name->osid);
1847 audit_copy_fcaps(name, dentry);
1848}
1849
1850/**
1851 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1852 * @context: audit_context for the task
1853 * @n: audit_names structure with reportable details
1854 * @path: optional path to report instead of audit_names->name
1855 * @record_num: record number to report when handling a list of names
1856 * @call_panic: optional pointer to int that will be updated if secid fails
1857 */
1858void audit_log_name(struct audit_context *context, struct audit_names *n,
1859 struct path *path, int record_num, int *call_panic)
1860{
1861 struct audit_buffer *ab;
1862 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1863 if (!ab)
1864 return;
1865
1866 audit_log_format(ab, "item=%d", record_num);
1867
1868 if (path)
1869 audit_log_d_path(ab, " name=", path);
1870 else if (n->name) {
1871 switch (n->name_len) {
1872 case AUDIT_NAME_FULL:
1873 /* log the full path */
1874 audit_log_format(ab, " name=");
1875 audit_log_untrustedstring(ab, n->name->name);
1876 break;
1877 case 0:
1878 /* name was specified as a relative path and the
1879 * directory component is the cwd */
1880 audit_log_d_path(ab, " name=", &context->pwd);
1881 break;
1882 default:
1883 /* log the name's directory component */
1884 audit_log_format(ab, " name=");
1885 audit_log_n_untrustedstring(ab, n->name->name,
1886 n->name_len);
1887 }
1888 } else
1889 audit_log_format(ab, " name=(null)");
1890
Linus Torvalds425afcf2015-09-08 13:34:59 -07001891 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04001892 audit_log_format(ab, " inode=%lu"
1893 " dev=%02x:%02x mode=%#ho"
1894 " ouid=%u ogid=%u rdev=%02x:%02x",
1895 n->ino,
1896 MAJOR(n->dev),
1897 MINOR(n->dev),
1898 n->mode,
1899 from_kuid(&init_user_ns, n->uid),
1900 from_kgid(&init_user_ns, n->gid),
1901 MAJOR(n->rdev),
1902 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04001903 if (n->osid != 0) {
1904 char *ctx = NULL;
1905 u32 len;
1906 if (security_secid_to_secctx(
1907 n->osid, &ctx, &len)) {
1908 audit_log_format(ab, " osid=%u", n->osid);
1909 if (call_panic)
1910 *call_panic = 2;
1911 } else {
1912 audit_log_format(ab, " obj=%s", ctx);
1913 security_release_secctx(ctx, len);
1914 }
1915 }
1916
Jeff Laytond3aea842013-05-08 10:32:23 -04001917 /* log the audit_names record type */
1918 audit_log_format(ab, " nametype=");
1919 switch(n->type) {
1920 case AUDIT_TYPE_NORMAL:
1921 audit_log_format(ab, "NORMAL");
1922 break;
1923 case AUDIT_TYPE_PARENT:
1924 audit_log_format(ab, "PARENT");
1925 break;
1926 case AUDIT_TYPE_CHILD_DELETE:
1927 audit_log_format(ab, "DELETE");
1928 break;
1929 case AUDIT_TYPE_CHILD_CREATE:
1930 audit_log_format(ab, "CREATE");
1931 break;
1932 default:
1933 audit_log_format(ab, "UNKNOWN");
1934 break;
1935 }
1936
Eric Parisb24a30a2013-04-30 15:30:32 -04001937 audit_log_fcaps(ab, n);
1938 audit_log_end(ab);
1939}
1940
1941int audit_log_task_context(struct audit_buffer *ab)
1942{
1943 char *ctx = NULL;
1944 unsigned len;
1945 int error;
1946 u32 sid;
1947
1948 security_task_getsecid(current, &sid);
1949 if (!sid)
1950 return 0;
1951
1952 error = security_secid_to_secctx(sid, &ctx, &len);
1953 if (error) {
1954 if (error != -EINVAL)
1955 goto error_path;
1956 return 0;
1957 }
1958
1959 audit_log_format(ab, " subj=%s", ctx);
1960 security_release_secctx(ctx, len);
1961 return 0;
1962
1963error_path:
1964 audit_panic("error in audit_log_task_context");
1965 return error;
1966}
1967EXPORT_SYMBOL(audit_log_task_context);
1968
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001969void audit_log_d_path_exe(struct audit_buffer *ab,
1970 struct mm_struct *mm)
1971{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001972 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001973
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001974 if (!mm)
1975 goto out_null;
1976
1977 exe_file = get_mm_exe_file(mm);
1978 if (!exe_file)
1979 goto out_null;
1980
1981 audit_log_d_path(ab, " exe=", &exe_file->f_path);
1982 fput(exe_file);
1983 return;
1984out_null:
1985 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001986}
1987
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04001988struct tty_struct *audit_get_tty(struct task_struct *tsk)
1989{
1990 struct tty_struct *tty = NULL;
1991 unsigned long flags;
1992
1993 spin_lock_irqsave(&tsk->sighand->siglock, flags);
1994 if (tsk->signal)
1995 tty = tty_kref_get(tsk->signal->tty);
1996 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
1997 return tty;
1998}
1999
2000void audit_put_tty(struct tty_struct *tty)
2001{
2002 tty_kref_put(tty);
2003}
2004
Eric Parisb24a30a2013-04-30 15:30:32 -04002005void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2006{
2007 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002008 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002009 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002010
2011 if (!ab)
2012 return;
2013
2014 /* tsk == current */
2015 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002016 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002017 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002018 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002019 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002020 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002021 task_ppid_nr(tsk),
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002022 task_pid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002023 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2024 from_kuid(&init_user_ns, cred->uid),
2025 from_kgid(&init_user_ns, cred->gid),
2026 from_kuid(&init_user_ns, cred->euid),
2027 from_kuid(&init_user_ns, cred->suid),
2028 from_kuid(&init_user_ns, cred->fsuid),
2029 from_kgid(&init_user_ns, cred->egid),
2030 from_kgid(&init_user_ns, cred->sgid),
2031 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002032 tty ? tty_name(tty) : "(none)",
2033 audit_get_sessionid(tsk));
2034 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002035 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002036 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002037 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002038 audit_log_task_context(ab);
2039}
2040EXPORT_SYMBOL(audit_log_task_info);
2041
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002042/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002043 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302044 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002045 * @link: the path that triggered the restriction
2046 */
2047void audit_log_link_denied(const char *operation, struct path *link)
2048{
2049 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002050 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002051
Eric Parisb24a30a2013-04-30 15:30:32 -04002052 name = kzalloc(sizeof(*name), GFP_NOFS);
2053 if (!name)
2054 return;
2055
2056 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002057 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2058 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002059 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002060 goto out;
2061 audit_log_format(ab, "op=%s", operation);
2062 audit_log_task_info(ab, current);
2063 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002064 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002065
2066 /* Generate AUDIT_PATH record with object. */
2067 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002068 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002069 audit_log_name(current->audit_context, name, link, 0, NULL);
2070out:
2071 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002072}
2073
2074/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002075 * audit_log_end - end one audit record
2076 * @ab: the audit_buffer
2077 *
Paul Moore4aa838722016-11-29 16:53:24 -05002078 * We can not do a netlink send inside an irq context because it blocks (last
2079 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2080 * queue and a tasklet is scheduled to remove them from the queue outside the
2081 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002082 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002083void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (!ab)
2086 return;
2087 if (!audit_rate_check()) {
2088 audit_log_lost("rate limit exceeded");
2089 } else {
Paul Mooreaf8b8242016-11-29 16:53:24 -05002090 skb_queue_tail(&audit_queue, ab->skb);
Paul Moore4aa838722016-11-29 16:53:24 -05002091 wake_up_interruptible(&kauditd_wait);
Eric Parisf3d357b2008-04-18 10:02:28 -04002092 ab->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Chris Wright16e19042005-05-06 15:53:34 +01002094 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002097/**
2098 * audit_log - Log an audit record
2099 * @ctx: audit context
2100 * @gfp_mask: type of allocation
2101 * @type: audit message type
2102 * @fmt: format string to use
2103 * @...: variable parameters matching the format string
2104 *
2105 * This is a convenience function that calls audit_log_start,
2106 * audit_log_vformat, and audit_log_end. It may be called
2107 * in any context.
2108 */
Daniel Walker5600b892007-10-18 03:06:10 -07002109void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002110 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct audit_buffer *ab;
2113 va_list args;
2114
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002115 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (ab) {
2117 va_start(args, fmt);
2118 audit_log_vformat(ab, fmt, args);
2119 va_end(args);
2120 audit_log_end(ab);
2121 }
2122}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002123
Mr Dash Four131ad622011-06-30 13:31:57 +02002124#ifdef CONFIG_SECURITY
2125/**
2126 * audit_log_secctx - Converts and logs SELinux context
2127 * @ab: audit_buffer
2128 * @secid: security number
2129 *
2130 * This is a helper function that calls security_secid_to_secctx to convert
2131 * secid to secctx and then adds the (converted) SELinux context to the audit
2132 * log by calling audit_log_format, thus also preventing leak of internal secid
2133 * to userspace. If secid cannot be converted audit_panic is called.
2134 */
2135void audit_log_secctx(struct audit_buffer *ab, u32 secid)
2136{
2137 u32 len;
2138 char *secctx;
2139
2140 if (security_secid_to_secctx(secid, &secctx, &len)) {
2141 audit_panic("Cannot convert secid to context");
2142 } else {
2143 audit_log_format(ab, " obj=%s", secctx);
2144 security_release_secctx(secctx, len);
2145 }
2146}
2147EXPORT_SYMBOL(audit_log_secctx);
2148#endif
2149
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002150EXPORT_SYMBOL(audit_log_start);
2151EXPORT_SYMBOL(audit_log_end);
2152EXPORT_SYMBOL(audit_log_format);
2153EXPORT_SYMBOL(audit_log);