blob: e23ce6ce101f1bc6964ae1417fb55e6069695bbb [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001013 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1014 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001015 if (err < 0)
1016 return err;
1017 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001018 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001019 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001020 if (err < 0)
1021 return err;
1022 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001023 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1024 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1025 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001026 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001027 return -EINVAL;
1028 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1029 if (err < 0)
1030 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001033 }
Eric Parisb0fed402013-05-22 12:54:49 -04001034 case AUDIT_GET_FEATURE:
1035 err = audit_get_feature(skb);
1036 if (err)
1037 return err;
1038 break;
1039 case AUDIT_SET_FEATURE:
1040 err = audit_set_feature(skb);
1041 if (err)
1042 return err;
1043 break;
Steve Grubb05474102005-05-21 00:18:37 +01001044 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001045 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1046 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001047 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1048 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001049
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001050 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001051 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001052 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001053 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001054 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001055 if (err)
1056 break;
1057 }
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001058 mutex_unlock(&audit_cmd_mutex);
Eric Parisdc9eb692013-04-19 13:23:09 -04001059 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001060 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001061 audit_log_format(ab, " msg='%.*s'",
1062 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001063 (char *)data);
1064 else {
1065 int size;
1066
Eric Parisf7616102013-04-11 11:25:00 -04001067 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001068 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001069 if (size > 0 &&
1070 ((unsigned char *)data)[size - 1] == '\0')
1071 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001072 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001073 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001074 audit_set_portid(ab, NETLINK_CB(skb).portid);
Eric Paris50397bd2008-01-07 18:14:19 -05001075 audit_log_end(ab);
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001076 mutex_lock(&audit_cmd_mutex);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001079 case AUDIT_ADD_RULE:
1080 case AUDIT_DEL_RULE:
1081 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1082 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001083 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001084 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1085 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001086 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001087 return -EPERM;
1088 }
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001089 err = audit_rule_change(msg_type, NETLINK_CB(skb).portid,
Eric Parisdc9eb692013-04-19 13:23:09 -04001090 seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001092 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001093 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001094 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001095 case AUDIT_TRIM:
1096 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001097 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001098 audit_log_format(ab, " op=trim res=1");
1099 audit_log_end(ab);
1100 break;
1101 case AUDIT_MAKE_EQUIV: {
1102 void *bufp = data;
1103 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001104 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001105 char *old, *new;
1106
1107 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001108 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001109 break;
1110 memcpy(sizes, bufp, 2 * sizeof(u32));
1111 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001112 msglen -= 2 * sizeof(u32);
1113 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001114 if (IS_ERR(old)) {
1115 err = PTR_ERR(old);
1116 break;
1117 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001118 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001119 if (IS_ERR(new)) {
1120 err = PTR_ERR(new);
1121 kfree(old);
1122 break;
1123 }
1124 /* OK, here comes... */
1125 err = audit_tag_tree(old, new);
1126
Eric Parisdc9eb692013-04-19 13:23:09 -04001127 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001128
Al Viro74c3cbe2007-07-22 08:04:18 -04001129 audit_log_format(ab, " op=make_equiv old=");
1130 audit_log_untrustedstring(ab, old);
1131 audit_log_format(ab, " new=");
1132 audit_log_untrustedstring(ab, new);
1133 audit_log_format(ab, " res=%d", !err);
1134 audit_log_end(ab);
1135 kfree(old);
1136 kfree(new);
1137 break;
1138 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001139 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001140 len = 0;
1141 if (audit_sig_sid) {
1142 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1143 if (err)
1144 return err;
1145 }
Al Viroe1396062006-05-25 10:19:47 -04001146 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1147 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001148 if (audit_sig_sid)
1149 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001150 return -ENOMEM;
1151 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001152 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001153 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001154 if (audit_sig_sid) {
1155 memcpy(sig_data->ctx, ctx, len);
1156 security_release_secctx(ctx, len);
1157 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001158 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1159 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001160 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001161 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001162 case AUDIT_TTY_GET: {
1163 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001164 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001165
Peter Hurley2e28d382016-01-09 22:55:33 -08001166 t = READ_ONCE(current->signal->audit_tty);
1167 s.enabled = t & AUDIT_TTY_ENABLE;
1168 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001169
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001170 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001171 break;
1172 }
1173 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001174 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001175 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001176 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001177
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001178 memset(&s, 0, sizeof(s));
1179 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001180 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001181 /* check if new data is valid */
1182 if ((s.enabled != 0 && s.enabled != 1) ||
1183 (s.log_passwd != 0 && s.log_passwd != 1))
1184 err = -EINVAL;
1185
Peter Hurley2e28d382016-01-09 22:55:33 -08001186 if (err)
1187 t = READ_ONCE(current->signal->audit_tty);
1188 else {
1189 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1190 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001191 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001192 old.enabled = t & AUDIT_TTY_ENABLE;
1193 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001194
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001195 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001196 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1197 " old-log_passwd=%d new-log_passwd=%d res=%d",
1198 old.enabled, s.enabled, old.log_passwd,
1199 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001200 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001201 break;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 default:
1204 err = -EINVAL;
1205 break;
1206 }
1207
1208 return err < 0 ? err : 0;
1209}
1210
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001211/*
Eric Parisea7ae602009-06-11 14:31:35 -04001212 * Get message from skb. Each message is processed by audit_receive_msg.
1213 * Malformed skbs with wrong length are discarded silently.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001214 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001215static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Eric Parisea7ae602009-06-11 14:31:35 -04001217 struct nlmsghdr *nlh;
1218 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001219 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001220 * if the nlmsg_len was not aligned
1221 */
1222 int len;
1223 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Eric Parisea7ae602009-06-11 14:31:35 -04001225 nlh = nlmsg_hdr(skb);
1226 len = skb->len;
1227
Hong zhi guo94191212013-03-27 06:49:06 +00001228 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001229 err = audit_receive_msg(skb, nlh);
1230 /* if err or if this message says it wants a response */
1231 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 netlink_ack(skb, nlh, err);
Eric Parisea7ae602009-06-11 14:31:35 -04001233
Alexandru Copot2851da52013-03-28 23:31:29 +02001234 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/* Receive messages from netlink socket. */
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001239static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Amy Griffisf368c07d2006-04-07 16:55:56 -04001241 mutex_lock(&audit_cmd_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001242 audit_receive_skb(skb);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001243 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001246/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001247static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001248{
1249 if (!capable(CAP_AUDIT_READ))
1250 return -EPERM;
1251
1252 return 0;
1253}
1254
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001255static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001257 struct netlink_kernel_cfg cfg = {
1258 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001259 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001260 .flags = NL_CFG_F_NONROOT_RECV,
1261 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001262 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001263
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001264 struct audit_net *aunet = net_generic(net, audit_net_id);
1265
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001266 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
Gao feng11ee39e2013-12-17 11:10:41 +08001267 if (aunet->nlsk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001268 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001269 return -ENOMEM;
1270 }
1271 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001272 return 0;
1273}
1274
1275static void __net_exit audit_net_exit(struct net *net)
1276{
1277 struct audit_net *aunet = net_generic(net, audit_net_id);
1278 struct sock *sock = aunet->nlsk;
Paul Moorec6480202016-11-29 16:53:25 -05001279 if (sock == audit_sock)
1280 auditd_reset();
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001281
Monam Agarwale231d542014-03-24 00:16:19 +05301282 RCU_INIT_POINTER(aunet->nlsk, NULL);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001283 synchronize_net();
1284 netlink_kernel_release(sock);
1285}
1286
Richard Guy Briggs86268772013-07-16 13:18:45 -04001287static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001288 .init = audit_net_init,
1289 .exit = audit_net_exit,
1290 .id = &audit_net_id,
1291 .size = sizeof(struct audit_net),
1292};
1293
1294/* Initialize audit support at boot time. */
1295static int __init audit_init(void)
1296{
1297 int i;
1298
Eric Parisa3f07112008-11-05 12:47:09 -05001299 if (audit_initialized == AUDIT_DISABLED)
1300 return 0;
1301
Joe Perchesd957f7b2014-01-14 10:33:12 -08001302 pr_info("initializing netlink subsys (%s)\n",
1303 audit_default ? "enabled" : "disabled");
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001304 register_pernet_subsys(&audit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Paul Mooreaf8b8242016-11-29 16:53:24 -05001306 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001307 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001308 skb_queue_head_init(&audit_hold_queue);
Eric Parisa3f07112008-11-05 12:47:09 -05001309 audit_initialized = AUDIT_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 audit_enabled = audit_default;
Eric Parisb593d382008-01-08 17:38:31 -05001311 audit_ever_enabled |= !!audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001312
Amy Griffisf368c07d2006-04-07 16:55:56 -04001313 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1314 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001315
Paul Moore6c9255642016-11-29 16:53:23 -05001316 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1317 if (IS_ERR(kauditd_task)) {
1318 int err = PTR_ERR(kauditd_task);
1319 panic("audit: failed to start the kauditd thread (%d)\n", err);
1320 }
1321
1322 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326__initcall(audit_init);
1327
1328/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1329static int __init audit_enable(char *str)
1330{
1331 audit_default = !!simple_strtol(str, NULL, 0);
Eric Parisa3f07112008-11-05 12:47:09 -05001332 if (!audit_default)
1333 audit_initialized = AUDIT_DISABLED;
1334
Joe Perchesd957f7b2014-01-14 10:33:12 -08001335 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001336 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001337
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001338 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340__setup("audit=", audit_enable);
1341
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001342/* Process kernel command-line parameter at boot time.
1343 * audit_backlog_limit=<n> */
1344static int __init audit_backlog_limit_set(char *str)
1345{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001346 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001347
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001348 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001349 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1350 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001351 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001352 return 1;
1353 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001354
1355 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001356 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001357
1358 return 1;
1359}
1360__setup("audit_backlog_limit=", audit_backlog_limit_set);
1361
Chris Wright16e19042005-05-06 15:53:34 +01001362static void audit_buffer_free(struct audit_buffer *ab)
1363{
1364 unsigned long flags;
1365
Chris Wright8fc61152005-05-06 15:54:17 +01001366 if (!ab)
1367 return;
1368
Markus Elfringd865e572016-01-13 09:18:55 -05001369 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +01001370 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001371 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +01001372 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001373 else {
1374 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +01001375 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001376 }
Chris Wright16e19042005-05-06 15:53:34 +01001377 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1378}
1379
Steve Grubbc0404992005-05-13 18:17:42 +01001380static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +01001381 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001382{
1383 unsigned long flags;
1384 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +01001385 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +01001386
1387 spin_lock_irqsave(&audit_freelist_lock, flags);
1388 if (!list_empty(&audit_freelist)) {
1389 ab = list_entry(audit_freelist.next,
1390 struct audit_buffer, list);
1391 list_del(&ab->list);
1392 --audit_freelist_count;
1393 }
1394 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1395
1396 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +01001397 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +01001398 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +01001399 goto err;
Chris Wright16e19042005-05-06 15:53:34 +01001400 }
Chris Wright8fc61152005-05-06 15:54:17 +01001401
David Woodhouseb7d11252005-05-19 10:56:58 +01001402 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001403 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001404
1405 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1406 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001407 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001408
David S. Millerc64e66c2012-06-26 21:45:21 -07001409 nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0);
1410 if (!nlh)
1411 goto out_kfree_skb;
Eric Parisee080e62009-06-11 14:31:35 -04001412
Chris Wright16e19042005-05-06 15:53:34 +01001413 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001414
David S. Millerc64e66c2012-06-26 21:45:21 -07001415out_kfree_skb:
Eric Parisee080e62009-06-11 14:31:35 -04001416 kfree_skb(ab->skb);
1417 ab->skb = NULL;
Chris Wright8fc61152005-05-06 15:54:17 +01001418err:
1419 audit_buffer_free(ab);
1420 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001423/**
1424 * audit_serial - compute a serial number for the audit record
1425 *
1426 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001427 * written to user-space as soon as they are generated, so a complete
1428 * audit record may be written in several pieces. The timestamp of the
1429 * record and this serial number are used by the user-space tools to
1430 * determine which pieces belong to the same audit record. The
1431 * (timestamp,serial) tuple is unique for each syscall and is live from
1432 * syscall entry to syscall exit.
1433 *
David Woodhousebfb44962005-05-21 21:08:09 +01001434 * NOTE: Another possibility is to store the formatted records off the
1435 * audit context (for those records that have a context), and emit them
1436 * all at syscall exit. However, this could delay the reporting of
1437 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001438 * halts).
1439 */
David Woodhousebfb44962005-05-21 21:08:09 +01001440unsigned int audit_serial(void)
1441{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001442 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001443
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001444 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001445}
1446
Daniel Walker5600b892007-10-18 03:06:10 -07001447static inline void audit_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001448 struct timespec *t, unsigned int *serial)
1449{
Al Viro48887e62008-12-06 01:05:50 -05001450 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
David Woodhousebfb44962005-05-21 21:08:09 +01001451 *t = CURRENT_TIME;
1452 *serial = audit_serial();
1453 }
1454}
1455
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001456/**
1457 * audit_log_start - obtain an audit buffer
1458 * @ctx: audit_context (may be NULL)
1459 * @gfp_mask: type of allocation
1460 * @type: audit message type
1461 *
1462 * Returns audit_buffer pointer on success or NULL on error.
1463 *
1464 * Obtain an audit buffer. This routine does locking to obtain the
1465 * audit buffer, but then no locking is required for calls to
1466 * audit_log_*format. If the task (ctx) is a task that is currently in a
1467 * syscall, then the syscall is marked as auditable and an audit record
1468 * will be written at syscall exit. If there is no associated task, then
1469 * task context (ctx) should be NULL.
1470 */
Al Viro9796fdd2005-10-21 03:22:03 -04001471struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001472 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Paul Moore31975422016-11-29 16:53:25 -05001474 struct audit_buffer *ab;
1475 struct timespec t;
1476 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Eric Parisa3f07112008-11-05 12:47:09 -05001478 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return NULL;
1480
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001481 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001482 return NULL;
1483
Paul Moore31975422016-11-29 16:53:25 -05001484 /* don't ever fail/sleep on auditd since we need auditd to drain the
1485 * queue; also, when we are checking for auditd, compare PIDs using
1486 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg() using
1487 * a PID anchored in the caller's namespace */
1488 if (!(audit_pid && audit_pid == task_tgid_vnr(current))) {
1489 long sleep_time = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001490
Paul Moore31975422016-11-29 16:53:25 -05001491 while (audit_backlog_limit &&
1492 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1493 /* wake kauditd to try and flush the queue */
1494 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001495
Paul Moore31975422016-11-29 16:53:25 -05001496 /* sleep if we are allowed and we haven't exhausted our
1497 * backlog wait limit */
1498 if ((gfp_mask & __GFP_DIRECT_RECLAIM) &&
1499 (sleep_time > 0)) {
1500 DECLARE_WAITQUEUE(wait, current);
1501
1502 add_wait_queue_exclusive(&audit_backlog_wait,
1503 &wait);
1504 set_current_state(TASK_UNINTERRUPTIBLE);
1505 sleep_time = schedule_timeout(sleep_time);
1506 remove_wait_queue(&audit_backlog_wait, &wait);
1507 } else {
1508 if (audit_rate_check() && printk_ratelimit())
1509 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1510 skb_queue_len(&audit_queue),
1511 audit_backlog_limit);
1512 audit_log_lost("backlog limit exceeded");
1513 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001514 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001515 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001516 }
1517
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001518 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (!ab) {
1520 audit_log_lost("out of memory in audit_log_start");
1521 return NULL;
1522 }
1523
David Woodhousebfb44962005-05-21 21:08:09 +01001524 audit_get_stamp(ab->ctx, &t, &serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
1526 t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return ab;
1529}
1530
Chris Wright8fc61152005-05-06 15:54:17 +01001531/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001532 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001533 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001534 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001535 *
1536 * Returns 0 (no space) on failed expansion, or available space if
1537 * successful.
1538 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001539static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001540{
Chris Wright5ac52f32005-05-06 15:54:53 +01001541 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001542 int oldtail = skb_tailroom(skb);
1543 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1544 int newtail = skb_tailroom(skb);
1545
Chris Wright5ac52f32005-05-06 15:54:53 +01001546 if (ret < 0) {
1547 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001548 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001549 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001550
1551 skb->truesize += newtail - oldtail;
1552 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001555/*
1556 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * room in the audit buffer, more room will be allocated and vsnprint
1558 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001559 * can't format message larger than 1024 bytes, so we don't either.
1560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1562 va_list args)
1563{
1564 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001565 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001566 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (!ab)
1569 return;
1570
Chris Wright5ac52f32005-05-06 15:54:53 +01001571 BUG_ON(!ab->skb);
1572 skb = ab->skb;
1573 avail = skb_tailroom(skb);
1574 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001575 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001576 if (!avail)
1577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001579 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001580 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (len >= avail) {
1582 /* The printk buffer is 1024 bytes long, so if we get
1583 * here and AUDIT_BUFSIZ is at least 1024, then we can
1584 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001585 avail = audit_expand(ab,
1586 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001587 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001588 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001589 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Steve Grubb168b7172005-05-19 10:24:22 +01001591 if (len > 0)
1592 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001593out_va_end:
1594 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001595out:
1596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001599/**
1600 * audit_log_format - format a message into the audit buffer.
1601 * @ab: audit_buffer
1602 * @fmt: format string
1603 * @...: optional parameters matching @fmt string
1604 *
1605 * All the work is done in audit_log_vformat.
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1608{
1609 va_list args;
1610
1611 if (!ab)
1612 return;
1613 va_start(args, fmt);
1614 audit_log_vformat(ab, fmt, args);
1615 va_end(args);
1616}
1617
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001618/**
1619 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1620 * @ab: the audit_buffer
1621 * @buf: buffer to convert to hex
1622 * @len: length of @buf to be converted
1623 *
1624 * No return value; failure to expand is silently ignored.
1625 *
1626 * This function will take the passed buf and convert it into a string of
1627 * ascii hex digits. The new string is placed onto the skb.
1628 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001629void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001630 size_t len)
83c7d092005-04-29 15:54:44 +01001631{
Steve Grubb168b7172005-05-19 10:24:22 +01001632 int i, avail, new_len;
1633 unsigned char *ptr;
1634 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001635
Amy Griffis8ef2d302006-09-07 17:03:02 -04001636 if (!ab)
1637 return;
1638
Steve Grubb168b7172005-05-19 10:24:22 +01001639 BUG_ON(!ab->skb);
1640 skb = ab->skb;
1641 avail = skb_tailroom(skb);
1642 new_len = len<<1;
1643 if (new_len >= avail) {
1644 /* Round the buffer request up to the next multiple */
1645 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1646 avail = audit_expand(ab, new_len);
1647 if (!avail)
1648 return;
1649 }
1650
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001651 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001652 for (i = 0; i < len; i++)
1653 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001654 *ptr = 0;
1655 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001656}
1657
Amy Griffis9c937dc2006-06-08 23:19:31 -04001658/*
1659 * Format a string of no more than slen characters into the audit buffer,
1660 * enclosed in quote marks.
1661 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001662void audit_log_n_string(struct audit_buffer *ab, const char *string,
1663 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001664{
1665 int avail, new_len;
1666 unsigned char *ptr;
1667 struct sk_buff *skb;
1668
Amy Griffis8ef2d302006-09-07 17:03:02 -04001669 if (!ab)
1670 return;
1671
Amy Griffis9c937dc2006-06-08 23:19:31 -04001672 BUG_ON(!ab->skb);
1673 skb = ab->skb;
1674 avail = skb_tailroom(skb);
1675 new_len = slen + 3; /* enclosing quotes + null terminator */
1676 if (new_len > avail) {
1677 avail = audit_expand(ab, new_len);
1678 if (!avail)
1679 return;
1680 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001681 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001682 *ptr++ = '"';
1683 memcpy(ptr, string, slen);
1684 ptr += slen;
1685 *ptr++ = '"';
1686 *ptr = 0;
1687 skb_put(skb, slen + 2); /* don't include null terminator */
1688}
1689
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001690/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001691 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001692 * @string: string to be checked
1693 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001694 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001695bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001696{
1697 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001698 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001699 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001700 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001701 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001702 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001703}
1704
1705/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001706 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001707 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001708 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001709 * @string: string to be logged
1710 *
1711 * This code will escape a string that is passed to it if the string
1712 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001713 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001714 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001715 *
1716 * The caller specifies the number of characters in the string to log, which may
1717 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001718 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001719void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1720 size_t len)
83c7d092005-04-29 15:54:44 +01001721{
Eric Parisde6bbd12008-01-07 14:31:58 -05001722 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001723 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001724 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001725 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001726}
1727
Amy Griffis9c937dc2006-06-08 23:19:31 -04001728/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001729 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001730 * @ab: audit_buffer
1731 * @string: string to be logged
1732 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001733 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001734 * determine string length.
1735 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001736void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001737{
Eric Parisb556f8a2008-04-18 10:12:59 -04001738 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001739}
1740
Steve Grubb168b7172005-05-19 10:24:22 +01001741/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001743 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Jan Blunck44707fd2008-02-14 19:38:33 -08001745 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Chris Wright8fc61152005-05-06 15:54:17 +01001747 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001748 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Steve Grubb168b7172005-05-19 10:24:22 +01001750 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001751 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1752 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001753 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001754 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001756 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001757 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1758 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001759 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001760 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001761 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001762 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Eric Paris4d3fb702013-04-30 09:53:34 -04001765void audit_log_session_info(struct audit_buffer *ab)
1766{
Eric Paris4440e852013-11-27 17:35:17 -05001767 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001768 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1769
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001770 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001771}
1772
Eric Paris9d960982009-06-11 14:31:37 -04001773void audit_log_key(struct audit_buffer *ab, char *key)
1774{
1775 audit_log_format(ab, " key=");
1776 if (key)
1777 audit_log_untrustedstring(ab, key);
1778 else
1779 audit_log_format(ab, "(null)");
1780}
1781
Eric Parisb24a30a2013-04-30 15:30:32 -04001782void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1783{
1784 int i;
1785
1786 audit_log_format(ab, " %s=", prefix);
1787 CAP_FOR_EACH_U32(i) {
1788 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04001789 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04001790 }
1791}
1792
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04001793static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04001794{
1795 kernel_cap_t *perm = &name->fcap.permitted;
1796 kernel_cap_t *inh = &name->fcap.inheritable;
1797 int log = 0;
1798
1799 if (!cap_isclear(*perm)) {
1800 audit_log_cap(ab, "cap_fp", perm);
1801 log = 1;
1802 }
1803 if (!cap_isclear(*inh)) {
1804 audit_log_cap(ab, "cap_fi", inh);
1805 log = 1;
1806 }
1807
1808 if (log)
1809 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1810 name->fcap.fE, name->fcap_ver);
1811}
1812
1813static inline int audit_copy_fcaps(struct audit_names *name,
1814 const struct dentry *dentry)
1815{
1816 struct cpu_vfs_cap_data caps;
1817 int rc;
1818
1819 if (!dentry)
1820 return 0;
1821
1822 rc = get_vfs_caps_from_disk(dentry, &caps);
1823 if (rc)
1824 return rc;
1825
1826 name->fcap.permitted = caps.permitted;
1827 name->fcap.inheritable = caps.inheritable;
1828 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1829 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1830 VFS_CAP_REVISION_SHIFT;
1831
1832 return 0;
1833}
1834
1835/* Copy inode data into an audit_names. */
1836void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001837 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04001838{
1839 name->ino = inode->i_ino;
1840 name->dev = inode->i_sb->s_dev;
1841 name->mode = inode->i_mode;
1842 name->uid = inode->i_uid;
1843 name->gid = inode->i_gid;
1844 name->rdev = inode->i_rdev;
1845 security_inode_getsecid(inode, &name->osid);
1846 audit_copy_fcaps(name, dentry);
1847}
1848
1849/**
1850 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1851 * @context: audit_context for the task
1852 * @n: audit_names structure with reportable details
1853 * @path: optional path to report instead of audit_names->name
1854 * @record_num: record number to report when handling a list of names
1855 * @call_panic: optional pointer to int that will be updated if secid fails
1856 */
1857void audit_log_name(struct audit_context *context, struct audit_names *n,
1858 struct path *path, int record_num, int *call_panic)
1859{
1860 struct audit_buffer *ab;
1861 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1862 if (!ab)
1863 return;
1864
1865 audit_log_format(ab, "item=%d", record_num);
1866
1867 if (path)
1868 audit_log_d_path(ab, " name=", path);
1869 else if (n->name) {
1870 switch (n->name_len) {
1871 case AUDIT_NAME_FULL:
1872 /* log the full path */
1873 audit_log_format(ab, " name=");
1874 audit_log_untrustedstring(ab, n->name->name);
1875 break;
1876 case 0:
1877 /* name was specified as a relative path and the
1878 * directory component is the cwd */
1879 audit_log_d_path(ab, " name=", &context->pwd);
1880 break;
1881 default:
1882 /* log the name's directory component */
1883 audit_log_format(ab, " name=");
1884 audit_log_n_untrustedstring(ab, n->name->name,
1885 n->name_len);
1886 }
1887 } else
1888 audit_log_format(ab, " name=(null)");
1889
Linus Torvalds425afcf2015-09-08 13:34:59 -07001890 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04001891 audit_log_format(ab, " inode=%lu"
1892 " dev=%02x:%02x mode=%#ho"
1893 " ouid=%u ogid=%u rdev=%02x:%02x",
1894 n->ino,
1895 MAJOR(n->dev),
1896 MINOR(n->dev),
1897 n->mode,
1898 from_kuid(&init_user_ns, n->uid),
1899 from_kgid(&init_user_ns, n->gid),
1900 MAJOR(n->rdev),
1901 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04001902 if (n->osid != 0) {
1903 char *ctx = NULL;
1904 u32 len;
1905 if (security_secid_to_secctx(
1906 n->osid, &ctx, &len)) {
1907 audit_log_format(ab, " osid=%u", n->osid);
1908 if (call_panic)
1909 *call_panic = 2;
1910 } else {
1911 audit_log_format(ab, " obj=%s", ctx);
1912 security_release_secctx(ctx, len);
1913 }
1914 }
1915
Jeff Laytond3aea842013-05-08 10:32:23 -04001916 /* log the audit_names record type */
1917 audit_log_format(ab, " nametype=");
1918 switch(n->type) {
1919 case AUDIT_TYPE_NORMAL:
1920 audit_log_format(ab, "NORMAL");
1921 break;
1922 case AUDIT_TYPE_PARENT:
1923 audit_log_format(ab, "PARENT");
1924 break;
1925 case AUDIT_TYPE_CHILD_DELETE:
1926 audit_log_format(ab, "DELETE");
1927 break;
1928 case AUDIT_TYPE_CHILD_CREATE:
1929 audit_log_format(ab, "CREATE");
1930 break;
1931 default:
1932 audit_log_format(ab, "UNKNOWN");
1933 break;
1934 }
1935
Eric Parisb24a30a2013-04-30 15:30:32 -04001936 audit_log_fcaps(ab, n);
1937 audit_log_end(ab);
1938}
1939
1940int audit_log_task_context(struct audit_buffer *ab)
1941{
1942 char *ctx = NULL;
1943 unsigned len;
1944 int error;
1945 u32 sid;
1946
1947 security_task_getsecid(current, &sid);
1948 if (!sid)
1949 return 0;
1950
1951 error = security_secid_to_secctx(sid, &ctx, &len);
1952 if (error) {
1953 if (error != -EINVAL)
1954 goto error_path;
1955 return 0;
1956 }
1957
1958 audit_log_format(ab, " subj=%s", ctx);
1959 security_release_secctx(ctx, len);
1960 return 0;
1961
1962error_path:
1963 audit_panic("error in audit_log_task_context");
1964 return error;
1965}
1966EXPORT_SYMBOL(audit_log_task_context);
1967
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001968void audit_log_d_path_exe(struct audit_buffer *ab,
1969 struct mm_struct *mm)
1970{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001971 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001972
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001973 if (!mm)
1974 goto out_null;
1975
1976 exe_file = get_mm_exe_file(mm);
1977 if (!exe_file)
1978 goto out_null;
1979
1980 audit_log_d_path(ab, " exe=", &exe_file->f_path);
1981 fput(exe_file);
1982 return;
1983out_null:
1984 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001985}
1986
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04001987struct tty_struct *audit_get_tty(struct task_struct *tsk)
1988{
1989 struct tty_struct *tty = NULL;
1990 unsigned long flags;
1991
1992 spin_lock_irqsave(&tsk->sighand->siglock, flags);
1993 if (tsk->signal)
1994 tty = tty_kref_get(tsk->signal->tty);
1995 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
1996 return tty;
1997}
1998
1999void audit_put_tty(struct tty_struct *tty)
2000{
2001 tty_kref_put(tty);
2002}
2003
Eric Parisb24a30a2013-04-30 15:30:32 -04002004void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2005{
2006 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002007 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002008 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002009
2010 if (!ab)
2011 return;
2012
2013 /* tsk == current */
2014 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002015 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002016 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002017 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002018 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002019 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002020 task_ppid_nr(tsk),
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002021 task_pid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002022 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2023 from_kuid(&init_user_ns, cred->uid),
2024 from_kgid(&init_user_ns, cred->gid),
2025 from_kuid(&init_user_ns, cred->euid),
2026 from_kuid(&init_user_ns, cred->suid),
2027 from_kuid(&init_user_ns, cred->fsuid),
2028 from_kgid(&init_user_ns, cred->egid),
2029 from_kgid(&init_user_ns, cred->sgid),
2030 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002031 tty ? tty_name(tty) : "(none)",
2032 audit_get_sessionid(tsk));
2033 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002034 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002035 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002036 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002037 audit_log_task_context(ab);
2038}
2039EXPORT_SYMBOL(audit_log_task_info);
2040
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002041/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002042 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302043 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002044 * @link: the path that triggered the restriction
2045 */
2046void audit_log_link_denied(const char *operation, struct path *link)
2047{
2048 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002049 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002050
Eric Parisb24a30a2013-04-30 15:30:32 -04002051 name = kzalloc(sizeof(*name), GFP_NOFS);
2052 if (!name)
2053 return;
2054
2055 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002056 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2057 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002058 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002059 goto out;
2060 audit_log_format(ab, "op=%s", operation);
2061 audit_log_task_info(ab, current);
2062 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002063 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002064
2065 /* Generate AUDIT_PATH record with object. */
2066 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002067 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002068 audit_log_name(current->audit_context, name, link, 0, NULL);
2069out:
2070 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002071}
2072
2073/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002074 * audit_log_end - end one audit record
2075 * @ab: the audit_buffer
2076 *
Paul Moore4aa838722016-11-29 16:53:24 -05002077 * We can not do a netlink send inside an irq context because it blocks (last
2078 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2079 * queue and a tasklet is scheduled to remove them from the queue outside the
2080 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002081 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002082void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (!ab)
2085 return;
2086 if (!audit_rate_check()) {
2087 audit_log_lost("rate limit exceeded");
2088 } else {
Paul Mooreaf8b8242016-11-29 16:53:24 -05002089 skb_queue_tail(&audit_queue, ab->skb);
Paul Moore4aa838722016-11-29 16:53:24 -05002090 wake_up_interruptible(&kauditd_wait);
Eric Parisf3d357b2008-04-18 10:02:28 -04002091 ab->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Chris Wright16e19042005-05-06 15:53:34 +01002093 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002096/**
2097 * audit_log - Log an audit record
2098 * @ctx: audit context
2099 * @gfp_mask: type of allocation
2100 * @type: audit message type
2101 * @fmt: format string to use
2102 * @...: variable parameters matching the format string
2103 *
2104 * This is a convenience function that calls audit_log_start,
2105 * audit_log_vformat, and audit_log_end. It may be called
2106 * in any context.
2107 */
Daniel Walker5600b892007-10-18 03:06:10 -07002108void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002109 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 struct audit_buffer *ab;
2112 va_list args;
2113
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002114 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (ab) {
2116 va_start(args, fmt);
2117 audit_log_vformat(ab, fmt, args);
2118 va_end(args);
2119 audit_log_end(ab);
2120 }
2121}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002122
Mr Dash Four131ad622011-06-30 13:31:57 +02002123#ifdef CONFIG_SECURITY
2124/**
2125 * audit_log_secctx - Converts and logs SELinux context
2126 * @ab: audit_buffer
2127 * @secid: security number
2128 *
2129 * This is a helper function that calls security_secid_to_secctx to convert
2130 * secid to secctx and then adds the (converted) SELinux context to the audit
2131 * log by calling audit_log_format, thus also preventing leak of internal secid
2132 * to userspace. If secid cannot be converted audit_panic is called.
2133 */
2134void audit_log_secctx(struct audit_buffer *ab, u32 secid)
2135{
2136 u32 len;
2137 char *secctx;
2138
2139 if (security_secid_to_secctx(secid, &secctx, &len)) {
2140 audit_panic("Cannot convert secid to context");
2141 } else {
2142 audit_log_format(ab, " obj=%s", secctx);
2143 security_release_secctx(secctx, len);
2144 }
2145}
2146EXPORT_SYMBOL(audit_log_secctx);
2147#endif
2148
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002149EXPORT_SYMBOL(audit_log_start);
2150EXPORT_SYMBOL(audit_log_end);
2151EXPORT_SYMBOL(audit_log_format);
2152EXPORT_SYMBOL(audit_log);