blob: f4056bc331fc34a34a2d8899e4982ab56e4d4ece [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)
Richard Guy Briggsa77ed4e2015-02-23 15:37:59 -0500110static u32 audit_backlog_wait_time_master = AUDIT_BACKLOG_WAIT_TIME;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800111static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100113/* The identity of the user shutting down the audit system. */
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800114kuid_t audit_sig_uid = INVALID_UID;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100115pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -0400116u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Records can be lost in several ways:
119 0) [suppressed in audit_alloc]
120 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
121 2) out of memory in audit_log_move [alloc_skb]
122 3) suppressed due to audit_rate_limit
123 4) suppressed due to audit_backlog_limit
124*/
125static atomic_t audit_lost = ATOMIC_INIT(0);
126
127/* The netlink socket. */
128static struct sock *audit_sock;
Richard Guy Briggsc0a8d9b2014-05-26 10:59:28 -0400129static int audit_net_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Amy Griffisf368c07d2006-04-07 16:55:56 -0400131/* Hash for inode-based rules */
132struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
133
David Woodhouseb7d11252005-05-19 10:56:58 +0100134/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
136 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700138static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static LIST_HEAD(audit_freelist);
140
Paul Moorec6480202016-11-29 16:53:25 -0500141/* queue msgs to send via kauditd_task */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500142static struct sk_buff_head audit_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500143/* queue msgs due to temporary unicast send problems */
144static struct sk_buff_head audit_retry_queue;
145/* queue msgs waiting for new auditd connection */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500146static struct sk_buff_head audit_hold_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500147
148/* queue servicing thread */
David Woodhouseb7d11252005-05-19 10:56:58 +0100149static struct task_struct *kauditd_task;
150static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500151
152/* waitqueue for callers who are blocked on the audit backlog */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100153static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric Parisb0fed402013-05-22 12:54:49 -0400155static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION,
156 .mask = -1,
157 .features = 0,
158 .lock = 0,};
159
Eric Paris21b85c32013-05-23 14:26:00 -0400160static char *audit_feature_names[2] = {
Eric Parisd040e5a2013-05-24 09:18:04 -0400161 "only_unset_loginuid",
Eric Paris21b85c32013-05-23 14:26:00 -0400162 "loginuid_immutable",
Eric Parisb0fed402013-05-22 12:54:49 -0400163};
164
165
Amy Griffisf368c07d2006-04-07 16:55:56 -0400166/* Serialize requests from userspace. */
Al Viro916d7572009-06-24 00:02:38 -0400167DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
170 * audit records. Since printk uses a 1024 byte buffer, this buffer
171 * should be at least that large. */
172#define AUDIT_BUFSIZ 1024
173
174/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
175 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
176#define AUDIT_MAXFREE (2*NR_CPUS)
177
178/* The audit_buffer is used when formatting an audit record. The caller
179 * locks briefly to get the record off the freelist or to allocate the
180 * buffer, and locks briefly to send the buffer to the netlink layer or
181 * to place it on a transmit queue. Multiple audit_buffers can be in
182 * use simultaneously. */
183struct audit_buffer {
184 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100185 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400187 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Eric Parisf09ac9d2008-04-18 10:11:04 -0400190struct audit_reply {
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400191 __u32 portid;
Eric W. Biederman638a0fd2014-02-28 10:49:05 -0800192 struct net *net;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400193 struct sk_buff *skb;
194};
195
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400196static void audit_set_portid(struct audit_buffer *ab, __u32 portid)
Steve Grubbc0404992005-05-13 18:17:42 +0100197{
Eric Paris50397bd2008-01-07 18:14:19 -0500198 if (ab) {
199 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400200 nlh->nlmsg_pid = portid;
Eric Paris50397bd2008-01-07 18:14:19 -0500201 }
Steve Grubbc0404992005-05-13 18:17:42 +0100202}
203
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000204void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Joe Perchesd957f7b2014-01-14 10:33:12 -0800206 switch (audit_failure) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case AUDIT_FAIL_SILENT:
208 break;
209 case AUDIT_FAIL_PRINTK:
Eric Paris320f1b12008-01-23 22:55:05 -0500210 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800211 pr_err("%s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 case AUDIT_FAIL_PANIC:
Eric Parisb29ee872008-02-21 15:53:05 -0500214 /* test audit_pid since printk is always losey, why bother? */
215 if (audit_pid)
216 panic("audit: %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
218 }
219}
220
221static inline int audit_rate_check(void)
222{
223 static unsigned long last_check = 0;
224 static int messages = 0;
225 static DEFINE_SPINLOCK(lock);
226 unsigned long flags;
227 unsigned long now;
228 unsigned long elapsed;
229 int retval = 0;
230
231 if (!audit_rate_limit) return 1;
232
233 spin_lock_irqsave(&lock, flags);
234 if (++messages < audit_rate_limit) {
235 retval = 1;
236 } else {
237 now = jiffies;
238 elapsed = now - last_check;
239 if (elapsed > HZ) {
240 last_check = now;
241 messages = 0;
242 retval = 1;
243 }
244 }
245 spin_unlock_irqrestore(&lock, flags);
246
247 return retval;
248}
249
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700250/**
251 * audit_log_lost - conditionally log lost audit message event
252 * @message: the message stating reason for lost audit message
253 *
254 * Emit at least 1 message per second, even if audit_rate_check is
255 * throttling.
256 * Always increment the lost messages counter.
257*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258void audit_log_lost(const char *message)
259{
260 static unsigned long last_msg = 0;
261 static DEFINE_SPINLOCK(lock);
262 unsigned long flags;
263 unsigned long now;
264 int print;
265
266 atomic_inc(&audit_lost);
267
268 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
269
270 if (!print) {
271 spin_lock_irqsave(&lock, flags);
272 now = jiffies;
273 if (now - last_msg > HZ) {
274 print = 1;
275 last_msg = now;
276 }
277 spin_unlock_irqrestore(&lock, flags);
278 }
279
280 if (print) {
Eric Paris320f1b12008-01-23 22:55:05 -0500281 if (printk_ratelimit())
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800282 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
Eric Paris320f1b12008-01-23 22:55:05 -0500283 atomic_read(&audit_lost),
284 audit_rate_limit,
285 audit_backlog_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 audit_panic(message);
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800290static int audit_log_config_change(char *function_name, u32 new, u32 old,
Eric Paris25323862008-04-18 10:09:25 -0400291 int allow_changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Eric Paris1a6b9f22008-01-07 17:09:31 -0500293 struct audit_buffer *ab;
294 int rc = 0;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500295
Eric Paris1a6b9f22008-01-07 17:09:31 -0500296 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
Kees Cook0644ec02013-01-11 14:32:07 -0800297 if (unlikely(!ab))
298 return rc;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800299 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
Eric Paris4d3fb702013-04-30 09:53:34 -0400300 audit_log_session_info(ab);
Eric Parisb122c372013-04-19 15:00:33 -0400301 rc = audit_log_task_context(ab);
302 if (rc)
303 allow_changes = 0; /* Something weird, deny request */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500304 audit_log_format(ab, " res=%d", allow_changes);
305 audit_log_end(ab);
306 return rc;
307}
308
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800309static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500310{
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800311 int allow_changes, rc = 0;
312 u32 old = *to_change;
Eric Paris1a6b9f22008-01-07 17:09:31 -0500313
314 /* check if we are locked */
315 if (audit_enabled == AUDIT_LOCKED)
316 allow_changes = 0;
317 else
318 allow_changes = 1;
319
320 if (audit_enabled != AUDIT_OFF) {
Eric Parisdc9eb692013-04-19 13:23:09 -0400321 rc = audit_log_config_change(function_name, new, old, allow_changes);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500322 if (rc)
323 allow_changes = 0;
324 }
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500325
326 /* If we are allowed, make the change */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500327 if (allow_changes == 1)
328 *to_change = new;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500329 /* Not allowed, update reason */
330 else if (rc == 0)
331 rc = -EPERM;
332 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800335static int audit_set_rate_limit(u32 limit)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500336{
Eric Parisdc9eb692013-04-19 13:23:09 -0400337 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500338}
339
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800340static int audit_set_backlog_limit(u32 limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Eric Parisdc9eb692013-04-19 13:23:09 -0400342 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800345static int audit_set_backlog_wait_time(u32 timeout)
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400346{
347 return audit_do_config_change("audit_backlog_wait_time",
Richard Guy Briggsa77ed4e2015-02-23 15:37:59 -0500348 &audit_backlog_wait_time_master, timeout);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400349}
350
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800351static int audit_set_enabled(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Eric Parisb593d382008-01-08 17:38:31 -0500353 int rc;
Pranith Kumar724e7bf2015-03-11 14:08:19 -0400354 if (state > AUDIT_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500356
Eric Parisdc9eb692013-04-19 13:23:09 -0400357 rc = audit_do_config_change("audit_enabled", &audit_enabled, state);
Eric Parisb593d382008-01-08 17:38:31 -0500358 if (!rc)
359 audit_ever_enabled |= !!state;
360
361 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800364static int audit_set_failure(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (state != AUDIT_FAIL_SILENT
367 && state != AUDIT_FAIL_PRINTK
368 && state != AUDIT_FAIL_PANIC)
369 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500370
Eric Parisdc9eb692013-04-19 13:23:09 -0400371 return audit_do_config_change("audit_failure", &audit_failure, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Eric Parisf3d357b2008-04-18 10:02:28 -0400374/*
Eric Paris038cbcf2009-06-11 14:31:35 -0400375 * For one reason or another this nlh isn't getting delivered to the userspace
376 * audit daemon, just send it to printk.
377 */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500378static void kauditd_printk_skb(struct sk_buff *skb)
Eric Paris038cbcf2009-06-11 14:31:35 -0400379{
380 struct nlmsghdr *nlh = nlmsg_hdr(skb);
David S. Millerc64e66c2012-06-26 21:45:21 -0700381 char *data = nlmsg_data(nlh);
Eric Paris038cbcf2009-06-11 14:31:35 -0400382
383 if (nlh->nlmsg_type != AUDIT_EOE) {
384 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800385 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
Eric Paris038cbcf2009-06-11 14:31:35 -0400386 else
Josh Boyerf1283522014-03-05 16:29:55 -0500387 audit_log_lost("printk limit exceeded");
Eric Paris038cbcf2009-06-11 14:31:35 -0400388 }
Eric Paris038cbcf2009-06-11 14:31:35 -0400389}
390
Paul Moorec6480202016-11-29 16:53:25 -0500391/**
392 * kauditd_hold_skb - Queue an audit record, waiting for auditd
393 * @skb: audit record
394 *
395 * Description:
396 * Queue the audit record, waiting for an instance of auditd. When this
397 * function is called we haven't given up yet on sending the record, but things
398 * are not looking good. The first thing we want to do is try to write the
399 * record via printk and then see if we want to try and hold on to the record
400 * and queue it, if we have room. If we want to hold on to the record, but we
401 * don't have room, record a record lost message.
402 */
403static void kauditd_hold_skb(struct sk_buff *skb)
Eric Parisf3d357b2008-04-18 10:02:28 -0400404{
Paul Moorec6480202016-11-29 16:53:25 -0500405 /* at this point it is uncertain if we will ever send this to auditd so
406 * try to send the message via printk before we go any further */
407 kauditd_printk_skb(skb);
Richard Guy Briggs32a1dba2015-11-04 08:23:50 -0500408
Paul Moorec6480202016-11-29 16:53:25 -0500409 /* can we just silently drop the message? */
410 if (!audit_default) {
411 kfree_skb(skb);
412 return;
413 }
414
415 /* if we have room, queue the message */
416 if (!audit_backlog_limit ||
417 skb_queue_len(&audit_hold_queue) < audit_backlog_limit) {
418 skb_queue_tail(&audit_hold_queue, skb);
419 return;
420 }
421
422 /* we have no other options - drop the message */
423 audit_log_lost("kauditd hold queue overflow");
424 kfree_skb(skb);
425}
426
427/**
428 * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd
429 * @skb: audit record
430 *
431 * Description:
432 * Not as serious as kauditd_hold_skb() as we still have a connected auditd,
433 * but for some reason we are having problems sending it audit records so
434 * queue the given record and attempt to resend.
435 */
436static void kauditd_retry_skb(struct sk_buff *skb)
437{
438 /* NOTE: because records should only live in the retry queue for a
439 * short period of time, before either being sent or moved to the hold
440 * queue, we don't currently enforce a limit on this queue */
441 skb_queue_tail(&audit_retry_queue, skb);
442}
443
444/**
445 * auditd_reset - Disconnect the auditd connection
446 *
447 * Description:
448 * Break the auditd/kauditd connection and move all the records in the retry
449 * queue into the hold queue in case auditd reconnects.
450 */
451static void auditd_reset(void)
452{
453 struct sk_buff *skb;
454
455 /* break the connection */
456 audit_pid = 0;
457 audit_sock = NULL;
458
459 /* flush all of the retry queue to the hold queue */
460 while ((skb = skb_dequeue(&audit_retry_queue)))
461 kauditd_hold_skb(skb);
462}
463
464/**
465 * kauditd_send_unicast_skb - Send a record via unicast to auditd
466 * @skb: audit record
467 */
468static int kauditd_send_unicast_skb(struct sk_buff *skb)
469{
470 int rc;
471
472 /* get an extra skb reference in case we fail to send */
Eric Parisf3d357b2008-04-18 10:02:28 -0400473 skb_get(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500474 rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
475 if (rc >= 0) {
Neil Horman70d4bf62010-07-20 06:45:56 +0000476 consume_skb(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500477 rc = 0;
478 }
479
480 return rc;
Eric Parisf3d357b2008-04-18 10:02:28 -0400481}
482
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500483/*
Paul Moorec6480202016-11-29 16:53:25 -0500484 * kauditd_send_multicast_skb - Send a record to any multicast listeners
485 * @skb: audit record
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400486 *
Paul Moorec6480202016-11-29 16:53:25 -0500487 * Description:
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400488 * This function doesn't consume an skb as might be expected since it has to
489 * copy it anyways.
490 */
Paul Moorec6480202016-11-29 16:53:25 -0500491static void kauditd_send_multicast_skb(struct sk_buff *skb)
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400492{
Paul Moorec6480202016-11-29 16:53:25 -0500493 struct sk_buff *copy;
494 struct audit_net *aunet = net_generic(&init_net, audit_net_id);
495 struct sock *sock = aunet->nlsk;
496 struct nlmsghdr *nlh;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400497
Richard Guy Briggs7f74ecd2014-04-22 21:31:58 -0400498 if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG))
499 return;
500
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400501 /*
502 * The seemingly wasteful skb_copy() rather than bumping the refcount
503 * using skb_get() is necessary because non-standard mods are made to
504 * the skb by the original kaudit unicast socket send routine. The
505 * existing auditd daemon assumes this breakage. Fixing this would
506 * require co-ordinating a change in the established protocol between
507 * the kaudit kernel subsystem and the auditd userspace code. There is
508 * no reason for new multicast clients to continue with this
509 * non-compliance.
510 */
Paul Moorec6480202016-11-29 16:53:25 -0500511 copy = skb_copy(skb, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400512 if (!copy)
513 return;
Paul Moorec6480202016-11-29 16:53:25 -0500514 nlh = nlmsg_hdr(copy);
515 nlh->nlmsg_len = skb->len;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400516
Paul Moorec6480202016-11-29 16:53:25 -0500517 nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400518}
519
Paul Moorec6480202016-11-29 16:53:25 -0500520/**
521 * kauditd_wake_condition - Return true when it is time to wake kauditd_thread
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500522 *
Paul Moorec6480202016-11-29 16:53:25 -0500523 * Description:
524 * This function is for use by the wait_event_freezable() call in
525 * kauditd_thread().
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500526 */
Paul Moorec6480202016-11-29 16:53:25 -0500527static int kauditd_wake_condition(void)
David Woodhouseb7d11252005-05-19 10:56:58 +0100528{
Paul Moorec6480202016-11-29 16:53:25 -0500529 static int pid_last = 0;
530 int rc;
531 int pid = audit_pid;
David Woodhouseb7d11252005-05-19 10:56:58 +0100532
Paul Moorec6480202016-11-29 16:53:25 -0500533 /* wake on new messages or a change in the connected auditd */
534 rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last);
535 if (rc)
536 pid_last = pid;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500537
Paul Moorec6480202016-11-29 16:53:25 -0500538 return rc;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500539}
540
David Woodhouseb7d11252005-05-19 10:56:58 +0100541static int kauditd_thread(void *dummy)
542{
Paul Moorec6480202016-11-29 16:53:25 -0500543 int rc;
544 int auditd = 0;
545 int reschedule = 0;
Paul Moore4aa838722016-11-29 16:53:24 -0500546 struct sk_buff *skb;
547 struct nlmsghdr *nlh;
548
Paul Moorec6480202016-11-29 16:53:25 -0500549#define UNICAST_RETRIES 5
550#define AUDITD_BAD(x,y) \
551 ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES)
552
553 /* NOTE: we do invalidate the auditd connection flag on any sending
554 * errors, but we only "restore" the connection flag at specific places
555 * in the loop in order to help ensure proper ordering of audit
556 * records */
557
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700558 set_freezable();
Andrew Morton4899b8b2006-10-06 00:43:48 -0700559 while (!kthread_should_stop()) {
Paul Moorec6480202016-11-29 16:53:25 -0500560 /* NOTE: possible area for future improvement is to look at
561 * the hold and retry queues, since only this thread
562 * has access to these queues we might be able to do
563 * our own queuing and skip some/all of the locking */
Eric Parisf3d357b2008-04-18 10:02:28 -0400564
Paul Moorec6480202016-11-29 16:53:25 -0500565 /* NOTE: it might be a fun experiment to split the hold and
566 * retry queue handling to another thread, but the
567 * synchronization issues and other overhead might kill
568 * any performance gains */
569
570 /* attempt to flush the hold queue */
571 while (auditd && (skb = skb_dequeue(&audit_hold_queue))) {
572 rc = kauditd_send_unicast_skb(skb);
573 if (rc) {
574 /* requeue to the same spot */
575 skb_queue_head(&audit_hold_queue, skb);
576
577 auditd = 0;
578 if (AUDITD_BAD(rc, reschedule)) {
579 auditd_reset();
580 reschedule = 0;
581 }
582 } else
583 /* we were able to send successfully */
584 reschedule = 0;
585 }
586
587 /* attempt to flush the retry queue */
588 while (auditd && (skb = skb_dequeue(&audit_retry_queue))) {
589 rc = kauditd_send_unicast_skb(skb);
590 if (rc) {
591 auditd = 0;
592 if (AUDITD_BAD(rc, reschedule)) {
593 kauditd_hold_skb(skb);
594 auditd_reset();
595 reschedule = 0;
596 } else
597 /* temporary problem (we hope), queue
598 * to the same spot and retry */
599 skb_queue_head(&audit_retry_queue, skb);
600 } else
601 /* we were able to send successfully */
602 reschedule = 0;
603 }
604
605 /* standard queue processing, try to be as quick as possible */
606quick_loop:
Paul Mooreaf8b8242016-11-29 16:53:24 -0500607 skb = skb_dequeue(&audit_queue);
David Woodhouseb7d11252005-05-19 10:56:58 +0100608 if (skb) {
Paul Moorec6480202016-11-29 16:53:25 -0500609 /* setup the netlink header, see the comments in
610 * kauditd_send_multicast_skb() for length quirks */
Paul Moore4aa838722016-11-29 16:53:24 -0500611 nlh = nlmsg_hdr(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500612 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
Paul Moore4aa838722016-11-29 16:53:24 -0500613
Paul Moorec6480202016-11-29 16:53:25 -0500614 /* attempt to send to any multicast listeners */
615 kauditd_send_multicast_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500616
Paul Moorec6480202016-11-29 16:53:25 -0500617 /* attempt to send to auditd, queue on failure */
618 if (auditd) {
619 rc = kauditd_send_unicast_skb(skb);
620 if (rc) {
621 auditd = 0;
622 if (AUDITD_BAD(rc, reschedule)) {
623 auditd_reset();
624 reschedule = 0;
625 }
Paul Moore4aa838722016-11-29 16:53:24 -0500626
Paul Moorec6480202016-11-29 16:53:25 -0500627 /* move to the retry queue */
628 kauditd_retry_skb(skb);
629 } else
630 /* everything is working so go fast! */
631 goto quick_loop;
632 } else if (reschedule)
633 /* we are currently having problems, move to
634 * the retry queue */
635 kauditd_retry_skb(skb);
Eric Paris038cbcf2009-06-11 14:31:35 -0400636 else
Paul Moorec6480202016-11-29 16:53:25 -0500637 /* dump the message via printk and hold it */
638 kauditd_hold_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500639 } else {
Paul Moorec6480202016-11-29 16:53:25 -0500640 /* we have flushed the backlog so wake everyone */
Paul Moore4aa838722016-11-29 16:53:24 -0500641 wake_up(&audit_backlog_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500642
643 /* if everything is okay with auditd (if present), go
644 * to sleep until there is something new in the queue
645 * or we have a change in the connected auditd;
646 * otherwise simply reschedule to give things a chance
647 * to recover */
648 if (reschedule) {
649 set_current_state(TASK_INTERRUPTIBLE);
650 schedule();
651 } else
652 wait_event_freezable(kauditd_wait,
653 kauditd_wake_condition());
654
655 /* update the auditd connection status */
656 auditd = (audit_pid ? 1 : 0);
David Woodhouseb7d11252005-05-19 10:56:58 +0100657 }
658 }
Paul Moorec6480202016-11-29 16:53:25 -0500659
Andrew Morton4899b8b2006-10-06 00:43:48 -0700660 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100661}
662
Al Viro9044e6b2006-05-22 01:09:24 -0400663int audit_send_list(void *_dest)
664{
665 struct audit_netlink_list *dest = _dest;
Al Viro9044e6b2006-05-22 01:09:24 -0400666 struct sk_buff *skb;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800667 struct net *net = dest->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400668 struct audit_net *aunet = net_generic(net, audit_net_id);
Al Viro9044e6b2006-05-22 01:09:24 -0400669
670 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400671 mutex_lock(&audit_cmd_mutex);
672 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400673
674 while ((skb = __skb_dequeue(&dest->q)) != NULL)
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400675 netlink_unicast(aunet->nlsk, skb, dest->portid, 0);
Al Viro9044e6b2006-05-22 01:09:24 -0400676
Eric W. Biederman48095d92014-02-03 17:25:33 -0800677 put_net(net);
Al Viro9044e6b2006-05-22 01:09:24 -0400678 kfree(dest);
679
680 return 0;
681}
682
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400683struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
Stephen Hemmingerb8800aa2010-10-20 17:23:50 -0700684 int multi, const void *payload, int size)
Al Viro9044e6b2006-05-22 01:09:24 -0400685{
686 struct sk_buff *skb;
687 struct nlmsghdr *nlh;
Al Viro9044e6b2006-05-22 01:09:24 -0400688 void *data;
689 int flags = multi ? NLM_F_MULTI : 0;
690 int t = done ? NLMSG_DONE : type;
691
Eric Parisee080e62009-06-11 14:31:35 -0400692 skb = nlmsg_new(size, GFP_KERNEL);
Al Viro9044e6b2006-05-22 01:09:24 -0400693 if (!skb)
694 return NULL;
695
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400696 nlh = nlmsg_put(skb, portid, seq, t, size, flags);
David S. Millerc64e66c2012-06-26 21:45:21 -0700697 if (!nlh)
698 goto out_kfree_skb;
699 data = nlmsg_data(nlh);
Al Viro9044e6b2006-05-22 01:09:24 -0400700 memcpy(data, payload, size);
701 return skb;
702
David S. Millerc64e66c2012-06-26 21:45:21 -0700703out_kfree_skb:
704 kfree_skb(skb);
Al Viro9044e6b2006-05-22 01:09:24 -0400705 return NULL;
706}
707
Eric Parisf09ac9d2008-04-18 10:11:04 -0400708static int audit_send_reply_thread(void *arg)
709{
710 struct audit_reply *reply = (struct audit_reply *)arg;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800711 struct net *net = reply->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400712 struct audit_net *aunet = net_generic(net, audit_net_id);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400713
714 mutex_lock(&audit_cmd_mutex);
715 mutex_unlock(&audit_cmd_mutex);
716
717 /* Ignore failure. It'll only happen if the sender goes away,
718 because our timeout is set to infinite. */
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400719 netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0);
Eric W. Biederman48095d92014-02-03 17:25:33 -0800720 put_net(net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400721 kfree(reply);
722 return 0;
723}
Paul Moorec6480202016-11-29 16:53:25 -0500724
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700725/**
726 * audit_send_reply - send an audit reply message via netlink
Eric W. Biedermand211f1772014-03-08 15:31:54 -0800727 * @request_skb: skb of request we are replying to (used to target the reply)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700728 * @seq: sequence number
729 * @type: audit message type
730 * @done: done (last) flag
731 * @multi: multi-part message flag
732 * @payload: payload data
733 * @size: payload size
734 *
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400735 * Allocates an skb, builds the netlink message, and sends it to the port id.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700736 * No failure notifications.
737 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800738static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400739 int multi, const void *payload, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800741 u32 portid = NETLINK_CB(request_skb).portid;
742 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400743 struct sk_buff *skb;
744 struct task_struct *tsk;
745 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
746 GFP_KERNEL);
747
748 if (!reply)
749 return;
750
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400751 skb = audit_make_reply(portid, seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!skb)
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700753 goto out;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400754
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800755 reply->net = get_net(net);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400756 reply->portid = portid;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400757 reply->skb = skb;
758
759 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700760 if (!IS_ERR(tsk))
761 return;
762 kfree_skb(skb);
763out:
764 kfree(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
768 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
769 * control messages.
770 */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700771static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int err = 0;
774
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400775 /* Only support initial user namespace for now. */
Eric Parisaa4af832014-03-30 19:07:54 -0400776 /*
777 * We return ECONNREFUSED because it tricks userspace into thinking
778 * that audit was not configured into the kernel. Lots of users
779 * configure their PAM stack (because that's what the distro does)
780 * to reject login if unable to send messages to audit. If we return
781 * ECONNREFUSED the PAM stack thinks the kernel does not have audit
782 * configured in and will let login proceed. If we return EPERM
783 * userspace will reject all logins. This should be removed when we
784 * support non init namespaces!!
785 */
Linus Torvalds0b747172014-04-12 12:38:53 -0700786 if (current_user_ns() != &init_user_ns)
Eric Parisaa4af832014-03-30 19:07:54 -0400787 return -ECONNREFUSED;
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 switch (msg_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case AUDIT_ADD:
792 case AUDIT_DEL:
Eric Paris18900902013-04-18 19:16:36 -0400793 return -EOPNOTSUPP;
794 case AUDIT_GET:
795 case AUDIT_SET:
Eric Parisb0fed402013-05-22 12:54:49 -0400796 case AUDIT_GET_FEATURE:
797 case AUDIT_SET_FEATURE:
Eric Paris18900902013-04-18 19:16:36 -0400798 case AUDIT_LIST_RULES:
799 case AUDIT_ADD_RULE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500800 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100801 case AUDIT_SIGNAL_INFO:
Miloslav Trmac522ed772007-07-15 23:40:56 -0700802 case AUDIT_TTY_GET:
803 case AUDIT_TTY_SET:
Al Viro74c3cbe2007-07-22 08:04:18 -0400804 case AUDIT_TRIM:
805 case AUDIT_MAKE_EQUIV:
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400806 /* Only support auditd and auditctl in initial pid namespace
807 * for now. */
Ameen Ali5985de62015-02-23 15:38:00 -0500808 if (task_active_pid_ns(current) != &init_pid_ns)
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400809 return -EPERM;
810
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700811 if (!netlink_capable(skb, CAP_AUDIT_CONTROL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 err = -EPERM;
813 break;
Steve Grubb05474102005-05-21 00:18:37 +0100814 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -0700815 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
816 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700817 if (!netlink_capable(skb, CAP_AUDIT_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 err = -EPERM;
819 break;
820 default: /* bad msg */
821 err = -EINVAL;
822 }
823
824 return err;
825}
826
Paul Moore233a6862015-11-04 08:23:52 -0500827static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type)
Eric Paris50397bd2008-01-07 18:14:19 -0500828{
Eric Parisdc9eb692013-04-19 13:23:09 -0400829 uid_t uid = from_kuid(&init_user_ns, current_uid());
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500830 pid_t pid = task_tgid_nr(current);
Eric Paris50397bd2008-01-07 18:14:19 -0500831
Tyler Hicks0868a5e2013-07-25 18:02:55 -0700832 if (!audit_enabled && msg_type != AUDIT_USER_AVC) {
Eric Paris50397bd2008-01-07 18:14:19 -0500833 *ab = NULL;
Paul Moore233a6862015-11-04 08:23:52 -0500834 return;
Eric Paris50397bd2008-01-07 18:14:19 -0500835 }
836
837 *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
Kees Cook0644ec02013-01-11 14:32:07 -0800838 if (unlikely(!*ab))
Paul Moore233a6862015-11-04 08:23:52 -0500839 return;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500840 audit_log_format(*ab, "pid=%d uid=%u", pid, uid);
Eric Paris4d3fb702013-04-30 09:53:34 -0400841 audit_log_session_info(*ab);
Eric Parisb122c372013-04-19 15:00:33 -0400842 audit_log_task_context(*ab);
Eric Paris50397bd2008-01-07 18:14:19 -0500843}
844
Eric Parisb0fed402013-05-22 12:54:49 -0400845int is_audit_feature_set(int i)
846{
847 return af.features & AUDIT_FEATURE_TO_MASK(i);
848}
849
850
851static int audit_get_feature(struct sk_buff *skb)
852{
853 u32 seq;
854
855 seq = nlmsg_hdr(skb)->nlmsg_seq;
856
Richard Guy Briggs9ef91512014-08-24 20:37:52 -0400857 audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af));
Eric Parisb0fed402013-05-22 12:54:49 -0400858
859 return 0;
860}
861
862static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature,
863 u32 old_lock, u32 new_lock, int res)
864{
865 struct audit_buffer *ab;
866
Gao fengb6c50fe2013-11-01 19:34:43 +0800867 if (audit_enabled == AUDIT_OFF)
868 return;
869
Eric Parisb0fed402013-05-22 12:54:49 -0400870 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
Richard Guy Briggsad2ac262014-01-07 13:08:41 -0500871 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -0400872 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 -0400873 audit_feature_names[which], !!old_feature, !!new_feature,
874 !!old_lock, !!new_lock, res);
875 audit_log_end(ab);
876}
877
878static int audit_set_feature(struct sk_buff *skb)
879{
880 struct audit_features *uaf;
881 int i;
882
Fabian Frederick6eed9b22014-06-03 22:05:10 +0200883 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -0400884 uaf = nlmsg_data(nlmsg_hdr(skb));
885
886 /* if there is ever a version 2 we should handle that here */
887
888 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
889 u32 feature = AUDIT_FEATURE_TO_MASK(i);
890 u32 old_feature, new_feature, old_lock, new_lock;
891
892 /* if we are not changing this feature, move along */
893 if (!(feature & uaf->mask))
894 continue;
895
896 old_feature = af.features & feature;
897 new_feature = uaf->features & feature;
898 new_lock = (uaf->lock | af.lock) & feature;
899 old_lock = af.lock & feature;
900
901 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +0800902 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -0400903 audit_log_feature_change(i, old_feature, new_feature,
904 old_lock, new_lock, 0);
905 return -EPERM;
906 }
907 }
908 /* nothing invalid, do the changes */
909 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
910 u32 feature = AUDIT_FEATURE_TO_MASK(i);
911 u32 old_feature, new_feature, old_lock, new_lock;
912
913 /* if we are not changing this feature, move along */
914 if (!(feature & uaf->mask))
915 continue;
916
917 old_feature = af.features & feature;
918 new_feature = uaf->features & feature;
919 old_lock = af.lock & feature;
920 new_lock = (uaf->lock | af.lock) & feature;
921
922 if (new_feature != old_feature)
923 audit_log_feature_change(i, old_feature, new_feature,
924 old_lock, new_lock, 1);
925
926 if (new_feature)
927 af.features |= feature;
928 else
929 af.features &= ~feature;
930 af.lock |= new_lock;
931 }
932
933 return 0;
934}
935
Richard Guy Briggs133e1e52016-01-25 18:04:15 -0500936static int audit_replace(pid_t pid)
937{
938 struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0,
939 &pid, sizeof(pid));
940
941 if (!skb)
942 return -ENOMEM;
943 return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
947{
Eric Parisdc9eb692013-04-19 13:23:09 -0400948 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100951 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -0400953 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -0500954 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -0400955 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700957 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (err)
959 return err;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -0700962 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400965 case AUDIT_GET: {
966 struct audit_status s;
967 memset(&s, 0, sizeof(s));
968 s.enabled = audit_enabled;
969 s.failure = audit_failure;
970 s.pid = audit_pid;
971 s.rate_limit = audit_rate_limit;
972 s.backlog_limit = audit_backlog_limit;
973 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -0500974 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -0500975 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Richard Guy Briggsa77ed4e2015-02-23 15:37:59 -0500976 s.backlog_wait_time = audit_backlog_wait_time_master;
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800977 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400979 }
980 case AUDIT_SET: {
981 struct audit_status s;
982 memset(&s, 0, sizeof(s));
983 /* guard against past and future API changes */
984 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
985 if (s.mask & AUDIT_STATUS_ENABLED) {
986 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +0800987 if (err < 0)
988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400990 if (s.mask & AUDIT_STATUS_FAILURE) {
991 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +0800992 if (err < 0)
993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400995 if (s.mask & AUDIT_STATUS_PID) {
996 int new_pid = s.pid;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -0500997 pid_t requesting_pid = task_tgid_vnr(current);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500998
Richard Guy Briggs935c9e72016-01-25 18:04:15 -0500999 if ((!new_pid) && (requesting_pid != audit_pid)) {
1000 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs34eab0a2013-06-21 14:47:13 -04001001 return -EACCES;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001002 }
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001003 if (audit_pid && new_pid &&
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001004 audit_replace(requesting_pid) != -ECONNREFUSED) {
1005 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001006 return -EEXIST;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001007 }
Eric Paris1a6b9f22008-01-07 17:09:31 -05001008 if (audit_enabled != AUDIT_OFF)
Eric Parisdc9eb692013-04-19 13:23:09 -04001009 audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
Eric Paris1a6b9f22008-01-07 17:09:31 -05001010 audit_pid = new_pid;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001011 audit_nlk_portid = NETLINK_CB(skb).portid;
Gao fengde92fc92013-12-17 11:10:42 +08001012 audit_sock = skb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001014 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1015 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001016 if (err < 0)
1017 return err;
1018 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001019 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001020 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001021 if (err < 0)
1022 return err;
1023 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001024 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1025 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1026 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001027 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001028 return -EINVAL;
1029 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1030 if (err < 0)
1031 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001034 }
Eric Parisb0fed402013-05-22 12:54:49 -04001035 case AUDIT_GET_FEATURE:
1036 err = audit_get_feature(skb);
1037 if (err)
1038 return err;
1039 break;
1040 case AUDIT_SET_FEATURE:
1041 err = audit_set_feature(skb);
1042 if (err)
1043 return err;
1044 break;
Steve Grubb05474102005-05-21 00:18:37 +01001045 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001046 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1047 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001048 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1049 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001050
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001051 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001052 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001053 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001054 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001055 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001056 if (err)
1057 break;
1058 }
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001059 mutex_unlock(&audit_cmd_mutex);
Eric Parisdc9eb692013-04-19 13:23:09 -04001060 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001061 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001062 audit_log_format(ab, " msg='%.*s'",
1063 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001064 (char *)data);
1065 else {
1066 int size;
1067
Eric Parisf7616102013-04-11 11:25:00 -04001068 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001069 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001070 if (size > 0 &&
1071 ((unsigned char *)data)[size - 1] == '\0')
1072 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001073 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001074 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001075 audit_set_portid(ab, NETLINK_CB(skb).portid);
Eric Paris50397bd2008-01-07 18:14:19 -05001076 audit_log_end(ab);
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001077 mutex_lock(&audit_cmd_mutex);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001080 case AUDIT_ADD_RULE:
1081 case AUDIT_DEL_RULE:
1082 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1083 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001084 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001085 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1086 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001087 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001088 return -EPERM;
1089 }
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001090 err = audit_rule_change(msg_type, NETLINK_CB(skb).portid,
Eric Parisdc9eb692013-04-19 13:23:09 -04001091 seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001093 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001094 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001095 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001096 case AUDIT_TRIM:
1097 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001098 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001099 audit_log_format(ab, " op=trim res=1");
1100 audit_log_end(ab);
1101 break;
1102 case AUDIT_MAKE_EQUIV: {
1103 void *bufp = data;
1104 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001105 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001106 char *old, *new;
1107
1108 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001109 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001110 break;
1111 memcpy(sizes, bufp, 2 * sizeof(u32));
1112 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001113 msglen -= 2 * sizeof(u32);
1114 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001115 if (IS_ERR(old)) {
1116 err = PTR_ERR(old);
1117 break;
1118 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001119 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001120 if (IS_ERR(new)) {
1121 err = PTR_ERR(new);
1122 kfree(old);
1123 break;
1124 }
1125 /* OK, here comes... */
1126 err = audit_tag_tree(old, new);
1127
Eric Parisdc9eb692013-04-19 13:23:09 -04001128 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001129
Al Viro74c3cbe2007-07-22 08:04:18 -04001130 audit_log_format(ab, " op=make_equiv old=");
1131 audit_log_untrustedstring(ab, old);
1132 audit_log_format(ab, " new=");
1133 audit_log_untrustedstring(ab, new);
1134 audit_log_format(ab, " res=%d", !err);
1135 audit_log_end(ab);
1136 kfree(old);
1137 kfree(new);
1138 break;
1139 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001140 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001141 len = 0;
1142 if (audit_sig_sid) {
1143 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1144 if (err)
1145 return err;
1146 }
Al Viroe1396062006-05-25 10:19:47 -04001147 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1148 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001149 if (audit_sig_sid)
1150 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001151 return -ENOMEM;
1152 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001153 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001154 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001155 if (audit_sig_sid) {
1156 memcpy(sig_data->ctx, ctx, len);
1157 security_release_secctx(ctx, len);
1158 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001159 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1160 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001161 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001162 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001163 case AUDIT_TTY_GET: {
1164 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001165 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001166
Peter Hurley2e28d382016-01-09 22:55:33 -08001167 t = READ_ONCE(current->signal->audit_tty);
1168 s.enabled = t & AUDIT_TTY_ENABLE;
1169 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001170
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001171 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001172 break;
1173 }
1174 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001175 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001176 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001177 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001178
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001179 memset(&s, 0, sizeof(s));
1180 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001181 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001182 /* check if new data is valid */
1183 if ((s.enabled != 0 && s.enabled != 1) ||
1184 (s.log_passwd != 0 && s.log_passwd != 1))
1185 err = -EINVAL;
1186
Peter Hurley2e28d382016-01-09 22:55:33 -08001187 if (err)
1188 t = READ_ONCE(current->signal->audit_tty);
1189 else {
1190 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1191 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001192 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001193 old.enabled = t & AUDIT_TTY_ENABLE;
1194 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001195
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001196 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001197 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1198 " old-log_passwd=%d new-log_passwd=%d res=%d",
1199 old.enabled, s.enabled, old.log_passwd,
1200 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001201 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001202 break;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 default:
1205 err = -EINVAL;
1206 break;
1207 }
1208
1209 return err < 0 ? err : 0;
1210}
1211
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001212/*
Eric Parisea7ae602009-06-11 14:31:35 -04001213 * Get message from skb. Each message is processed by audit_receive_msg.
1214 * Malformed skbs with wrong length are discarded silently.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001215 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001216static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Eric Parisea7ae602009-06-11 14:31:35 -04001218 struct nlmsghdr *nlh;
1219 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001220 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001221 * if the nlmsg_len was not aligned
1222 */
1223 int len;
1224 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric Parisea7ae602009-06-11 14:31:35 -04001226 nlh = nlmsg_hdr(skb);
1227 len = skb->len;
1228
Hong zhi guo94191212013-03-27 06:49:06 +00001229 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001230 err = audit_receive_msg(skb, nlh);
1231 /* if err or if this message says it wants a response */
1232 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 netlink_ack(skb, nlh, err);
Eric Parisea7ae602009-06-11 14:31:35 -04001234
Alexandru Copot2851da52013-03-28 23:31:29 +02001235 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/* Receive messages from netlink socket. */
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001240static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Amy Griffisf368c07d2006-04-07 16:55:56 -04001242 mutex_lock(&audit_cmd_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001243 audit_receive_skb(skb);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001244 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001247/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001248static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001249{
1250 if (!capable(CAP_AUDIT_READ))
1251 return -EPERM;
1252
1253 return 0;
1254}
1255
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001256static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001258 struct netlink_kernel_cfg cfg = {
1259 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001260 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001261 .flags = NL_CFG_F_NONROOT_RECV,
1262 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001263 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001264
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001265 struct audit_net *aunet = net_generic(net, audit_net_id);
1266
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001267 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
Gao feng11ee39e2013-12-17 11:10:41 +08001268 if (aunet->nlsk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001269 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001270 return -ENOMEM;
1271 }
1272 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001273 return 0;
1274}
1275
1276static void __net_exit audit_net_exit(struct net *net)
1277{
1278 struct audit_net *aunet = net_generic(net, audit_net_id);
1279 struct sock *sock = aunet->nlsk;
Paul Moorec6480202016-11-29 16:53:25 -05001280 if (sock == audit_sock)
1281 auditd_reset();
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001282
Monam Agarwale231d542014-03-24 00:16:19 +05301283 RCU_INIT_POINTER(aunet->nlsk, NULL);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001284 synchronize_net();
1285 netlink_kernel_release(sock);
1286}
1287
Richard Guy Briggs86268772013-07-16 13:18:45 -04001288static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001289 .init = audit_net_init,
1290 .exit = audit_net_exit,
1291 .id = &audit_net_id,
1292 .size = sizeof(struct audit_net),
1293};
1294
1295/* Initialize audit support at boot time. */
1296static int __init audit_init(void)
1297{
1298 int i;
1299
Eric Parisa3f07112008-11-05 12:47:09 -05001300 if (audit_initialized == AUDIT_DISABLED)
1301 return 0;
1302
Joe Perchesd957f7b2014-01-14 10:33:12 -08001303 pr_info("initializing netlink subsys (%s)\n",
1304 audit_default ? "enabled" : "disabled");
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001305 register_pernet_subsys(&audit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Paul Mooreaf8b8242016-11-29 16:53:24 -05001307 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001308 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001309 skb_queue_head_init(&audit_hold_queue);
Eric Parisa3f07112008-11-05 12:47:09 -05001310 audit_initialized = AUDIT_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 audit_enabled = audit_default;
Eric Parisb593d382008-01-08 17:38:31 -05001312 audit_ever_enabled |= !!audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001313
Amy Griffisf368c07d2006-04-07 16:55:56 -04001314 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1315 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001316
Paul Moore6c9255642016-11-29 16:53:23 -05001317 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1318 if (IS_ERR(kauditd_task)) {
1319 int err = PTR_ERR(kauditd_task);
1320 panic("audit: failed to start the kauditd thread (%d)\n", err);
1321 }
1322
1323 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327__initcall(audit_init);
1328
1329/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1330static int __init audit_enable(char *str)
1331{
1332 audit_default = !!simple_strtol(str, NULL, 0);
Eric Parisa3f07112008-11-05 12:47:09 -05001333 if (!audit_default)
1334 audit_initialized = AUDIT_DISABLED;
1335
Joe Perchesd957f7b2014-01-14 10:33:12 -08001336 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001337 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001338
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001339 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341__setup("audit=", audit_enable);
1342
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001343/* Process kernel command-line parameter at boot time.
1344 * audit_backlog_limit=<n> */
1345static int __init audit_backlog_limit_set(char *str)
1346{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001347 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001348
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001349 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001350 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1351 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001352 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001353 return 1;
1354 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001355
1356 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001357 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001358
1359 return 1;
1360}
1361__setup("audit_backlog_limit=", audit_backlog_limit_set);
1362
Chris Wright16e19042005-05-06 15:53:34 +01001363static void audit_buffer_free(struct audit_buffer *ab)
1364{
1365 unsigned long flags;
1366
Chris Wright8fc61152005-05-06 15:54:17 +01001367 if (!ab)
1368 return;
1369
Markus Elfringd865e572016-01-13 09:18:55 -05001370 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +01001371 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001372 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +01001373 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001374 else {
1375 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +01001376 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001377 }
Chris Wright16e19042005-05-06 15:53:34 +01001378 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1379}
1380
Steve Grubbc0404992005-05-13 18:17:42 +01001381static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +01001382 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001383{
1384 unsigned long flags;
1385 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +01001386 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +01001387
1388 spin_lock_irqsave(&audit_freelist_lock, flags);
1389 if (!list_empty(&audit_freelist)) {
1390 ab = list_entry(audit_freelist.next,
1391 struct audit_buffer, list);
1392 list_del(&ab->list);
1393 --audit_freelist_count;
1394 }
1395 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1396
1397 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +01001398 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +01001399 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +01001400 goto err;
Chris Wright16e19042005-05-06 15:53:34 +01001401 }
Chris Wright8fc61152005-05-06 15:54:17 +01001402
David Woodhouseb7d11252005-05-19 10:56:58 +01001403 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001404 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001405
1406 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1407 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001408 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001409
David S. Millerc64e66c2012-06-26 21:45:21 -07001410 nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0);
1411 if (!nlh)
1412 goto out_kfree_skb;
Eric Parisee080e62009-06-11 14:31:35 -04001413
Chris Wright16e19042005-05-06 15:53:34 +01001414 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001415
David S. Millerc64e66c2012-06-26 21:45:21 -07001416out_kfree_skb:
Eric Parisee080e62009-06-11 14:31:35 -04001417 kfree_skb(ab->skb);
1418 ab->skb = NULL;
Chris Wright8fc61152005-05-06 15:54:17 +01001419err:
1420 audit_buffer_free(ab);
1421 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001424/**
1425 * audit_serial - compute a serial number for the audit record
1426 *
1427 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001428 * written to user-space as soon as they are generated, so a complete
1429 * audit record may be written in several pieces. The timestamp of the
1430 * record and this serial number are used by the user-space tools to
1431 * determine which pieces belong to the same audit record. The
1432 * (timestamp,serial) tuple is unique for each syscall and is live from
1433 * syscall entry to syscall exit.
1434 *
David Woodhousebfb44962005-05-21 21:08:09 +01001435 * NOTE: Another possibility is to store the formatted records off the
1436 * audit context (for those records that have a context), and emit them
1437 * all at syscall exit. However, this could delay the reporting of
1438 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001439 * halts).
1440 */
David Woodhousebfb44962005-05-21 21:08:09 +01001441unsigned int audit_serial(void)
1442{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001443 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001444
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001445 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001446}
1447
Daniel Walker5600b892007-10-18 03:06:10 -07001448static inline void audit_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001449 struct timespec *t, unsigned int *serial)
1450{
Al Viro48887e62008-12-06 01:05:50 -05001451 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
David Woodhousebfb44962005-05-21 21:08:09 +01001452 *t = CURRENT_TIME;
1453 *serial = audit_serial();
1454 }
1455}
1456
Andrew Morton82919912013-01-11 14:32:11 -08001457/*
1458 * Wait for auditd to drain the queue a little
1459 */
Eric Parisc81825d2014-01-13 15:42:16 -05001460static long wait_for_auditd(long sleep_time)
Andrew Morton82919912013-01-11 14:32:11 -08001461{
1462 DECLARE_WAITQUEUE(wait, current);
Andrew Morton82919912013-01-11 14:32:11 -08001463
1464 if (audit_backlog_limit &&
Paul Mooreaf8b8242016-11-29 16:53:24 -05001465 skb_queue_len(&audit_queue) > audit_backlog_limit) {
Paul Moore7ffb8e32016-04-04 16:44:02 -04001466 add_wait_queue_exclusive(&audit_backlog_wait, &wait);
1467 set_current_state(TASK_UNINTERRUPTIBLE);
Eric Parisc81825d2014-01-13 15:42:16 -05001468 sleep_time = schedule_timeout(sleep_time);
Paul Moore7ffb8e32016-04-04 16:44:02 -04001469 remove_wait_queue(&audit_backlog_wait, &wait);
1470 }
Richard Guy Briggsae887e02013-09-16 10:45:59 -04001471
Eric Parisc81825d2014-01-13 15:42:16 -05001472 return sleep_time;
Andrew Morton82919912013-01-11 14:32:11 -08001473}
1474
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001475/**
1476 * audit_log_start - obtain an audit buffer
1477 * @ctx: audit_context (may be NULL)
1478 * @gfp_mask: type of allocation
1479 * @type: audit message type
1480 *
1481 * Returns audit_buffer pointer on success or NULL on error.
1482 *
1483 * Obtain an audit buffer. This routine does locking to obtain the
1484 * audit buffer, but then no locking is required for calls to
1485 * audit_log_*format. If the task (ctx) is a task that is currently in a
1486 * syscall, then the syscall is marked as auditable and an audit record
1487 * will be written at syscall exit. If there is no associated task, then
1488 * task context (ctx) should be NULL.
1489 */
Al Viro9796fdd2005-10-21 03:22:03 -04001490struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001491 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct timespec t;
Andrew Mortonef00be02008-01-10 11:02:39 -08001495 unsigned int uninitialized_var(serial);
Toshiyuki Okajima6dd80ab2013-12-05 16:15:23 +09001496 int reserve = 5; /* Allow atomic callers to go up to five
1497 entries over the normal backlog limit */
David Woodhouseac4cec42005-07-02 14:08:48 +01001498 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Eric Parisa3f07112008-11-05 12:47:09 -05001500 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return NULL;
1502
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001503 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001504 return NULL;
1505
Mel Gormand0164ad2015-11-06 16:28:21 -08001506 if (gfp_mask & __GFP_DIRECT_RECLAIM) {
Richard Guy Briggsf48a9422016-01-13 09:15:19 -05001507 if (audit_pid && audit_pid == current->tgid)
Mel Gormand0164ad2015-11-06 16:28:21 -08001508 gfp_mask &= ~__GFP_DIRECT_RECLAIM;
Toshiyuki Okajima6dd80ab2013-12-05 16:15:23 +09001509 else
1510 reserve = 0;
1511 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001512
1513 while (audit_backlog_limit
Paul Mooreaf8b8242016-11-29 16:53:24 -05001514 && skb_queue_len(&audit_queue) > audit_backlog_limit + reserve) {
Mel Gormand0164ad2015-11-06 16:28:21 -08001515 if (gfp_mask & __GFP_DIRECT_RECLAIM && audit_backlog_wait_time) {
Eric Parisc81825d2014-01-13 15:42:16 -05001516 long sleep_time;
David Woodhouseac4cec42005-07-02 14:08:48 +01001517
Eric Parisc81825d2014-01-13 15:42:16 -05001518 sleep_time = timeout_start + audit_backlog_wait_time - jiffies;
1519 if (sleep_time > 0) {
Richard Guy Briggsae887e02013-09-16 10:45:59 -04001520 sleep_time = wait_for_auditd(sleep_time);
Eric Parisc81825d2014-01-13 15:42:16 -05001521 if (sleep_time > 0)
Richard Guy Briggsae887e02013-09-16 10:45:59 -04001522 continue;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001523 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001524 }
Eric Paris320f1b12008-01-23 22:55:05 -05001525 if (audit_rate_check() && printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -08001526 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
Paul Mooreaf8b8242016-11-29 16:53:24 -05001527 skb_queue_len(&audit_queue),
Joe Perchesd957f7b2014-01-14 10:33:12 -08001528 audit_backlog_limit);
David Woodhousefb19b4c2005-05-19 14:55:56 +01001529 audit_log_lost("backlog limit exceeded");
Paul Mooreeb8baf62016-01-13 09:15:18 -05001530 audit_backlog_wait_time = 0;
David Woodhouseac4cec42005-07-02 14:08:48 +01001531 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +01001532 return NULL;
1533 }
1534
Richard Guy Briggsc4b7a772016-01-13 09:15:18 -05001535 if (!reserve && !audit_backlog_wait_time)
Richard Guy Briggsefef73a2015-02-23 15:38:00 -05001536 audit_backlog_wait_time = audit_backlog_wait_time_master;
Richard Guy Briggse789e562013-09-12 23:03:51 -04001537
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001538 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!ab) {
1540 audit_log_lost("out of memory in audit_log_start");
1541 return NULL;
1542 }
1543
David Woodhousebfb44962005-05-21 21:08:09 +01001544 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +01001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
1547 t.tv_sec, t.tv_nsec/1000000, serial);
1548 return ab;
1549}
1550
Chris Wright8fc61152005-05-06 15:54:17 +01001551/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001552 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001553 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001554 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001555 *
1556 * Returns 0 (no space) on failed expansion, or available space if
1557 * successful.
1558 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001559static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001560{
Chris Wright5ac52f32005-05-06 15:54:53 +01001561 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001562 int oldtail = skb_tailroom(skb);
1563 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1564 int newtail = skb_tailroom(skb);
1565
Chris Wright5ac52f32005-05-06 15:54:53 +01001566 if (ret < 0) {
1567 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001568 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001569 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001570
1571 skb->truesize += newtail - oldtail;
1572 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001575/*
1576 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * room in the audit buffer, more room will be allocated and vsnprint
1578 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001579 * can't format message larger than 1024 bytes, so we don't either.
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1582 va_list args)
1583{
1584 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001585 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001586 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 if (!ab)
1589 return;
1590
Chris Wright5ac52f32005-05-06 15:54:53 +01001591 BUG_ON(!ab->skb);
1592 skb = ab->skb;
1593 avail = skb_tailroom(skb);
1594 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001595 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001596 if (!avail)
1597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001599 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001600 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (len >= avail) {
1602 /* The printk buffer is 1024 bytes long, so if we get
1603 * here and AUDIT_BUFSIZ is at least 1024, then we can
1604 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001605 avail = audit_expand(ab,
1606 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001607 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001608 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001609 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Steve Grubb168b7172005-05-19 10:24:22 +01001611 if (len > 0)
1612 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001613out_va_end:
1614 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001615out:
1616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001619/**
1620 * audit_log_format - format a message into the audit buffer.
1621 * @ab: audit_buffer
1622 * @fmt: format string
1623 * @...: optional parameters matching @fmt string
1624 *
1625 * All the work is done in audit_log_vformat.
1626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1628{
1629 va_list args;
1630
1631 if (!ab)
1632 return;
1633 va_start(args, fmt);
1634 audit_log_vformat(ab, fmt, args);
1635 va_end(args);
1636}
1637
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001638/**
1639 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1640 * @ab: the audit_buffer
1641 * @buf: buffer to convert to hex
1642 * @len: length of @buf to be converted
1643 *
1644 * No return value; failure to expand is silently ignored.
1645 *
1646 * This function will take the passed buf and convert it into a string of
1647 * ascii hex digits. The new string is placed onto the skb.
1648 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001649void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001650 size_t len)
83c7d092005-04-29 15:54:44 +01001651{
Steve Grubb168b7172005-05-19 10:24:22 +01001652 int i, avail, new_len;
1653 unsigned char *ptr;
1654 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001655
Amy Griffis8ef2d302006-09-07 17:03:02 -04001656 if (!ab)
1657 return;
1658
Steve Grubb168b7172005-05-19 10:24:22 +01001659 BUG_ON(!ab->skb);
1660 skb = ab->skb;
1661 avail = skb_tailroom(skb);
1662 new_len = len<<1;
1663 if (new_len >= avail) {
1664 /* Round the buffer request up to the next multiple */
1665 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1666 avail = audit_expand(ab, new_len);
1667 if (!avail)
1668 return;
1669 }
1670
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001671 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001672 for (i = 0; i < len; i++)
1673 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001674 *ptr = 0;
1675 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001676}
1677
Amy Griffis9c937dc2006-06-08 23:19:31 -04001678/*
1679 * Format a string of no more than slen characters into the audit buffer,
1680 * enclosed in quote marks.
1681 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001682void audit_log_n_string(struct audit_buffer *ab, const char *string,
1683 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001684{
1685 int avail, new_len;
1686 unsigned char *ptr;
1687 struct sk_buff *skb;
1688
Amy Griffis8ef2d302006-09-07 17:03:02 -04001689 if (!ab)
1690 return;
1691
Amy Griffis9c937dc2006-06-08 23:19:31 -04001692 BUG_ON(!ab->skb);
1693 skb = ab->skb;
1694 avail = skb_tailroom(skb);
1695 new_len = slen + 3; /* enclosing quotes + null terminator */
1696 if (new_len > avail) {
1697 avail = audit_expand(ab, new_len);
1698 if (!avail)
1699 return;
1700 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001701 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001702 *ptr++ = '"';
1703 memcpy(ptr, string, slen);
1704 ptr += slen;
1705 *ptr++ = '"';
1706 *ptr = 0;
1707 skb_put(skb, slen + 2); /* don't include null terminator */
1708}
1709
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001710/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001711 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001712 * @string: string to be checked
1713 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001714 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001715bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001716{
1717 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001718 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001719 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001720 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001721 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001722 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001723}
1724
1725/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001726 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001727 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001728 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001729 * @string: string to be logged
1730 *
1731 * This code will escape a string that is passed to it if the string
1732 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001733 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001734 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001735 *
1736 * The caller specifies the number of characters in the string to log, which may
1737 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001738 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001739void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1740 size_t len)
83c7d092005-04-29 15:54:44 +01001741{
Eric Parisde6bbd12008-01-07 14:31:58 -05001742 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001743 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001744 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001745 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001746}
1747
Amy Griffis9c937dc2006-06-08 23:19:31 -04001748/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001749 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001750 * @ab: audit_buffer
1751 * @string: string to be logged
1752 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001753 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001754 * determine string length.
1755 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001756void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001757{
Eric Parisb556f8a2008-04-18 10:12:59 -04001758 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001759}
1760
Steve Grubb168b7172005-05-19 10:24:22 +01001761/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001763 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Jan Blunck44707fd2008-02-14 19:38:33 -08001765 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Chris Wright8fc61152005-05-06 15:54:17 +01001767 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001768 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Steve Grubb168b7172005-05-19 10:24:22 +01001770 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001771 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1772 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001773 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001774 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001776 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001777 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1778 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001779 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001780 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001781 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001782 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Eric Paris4d3fb702013-04-30 09:53:34 -04001785void audit_log_session_info(struct audit_buffer *ab)
1786{
Eric Paris4440e852013-11-27 17:35:17 -05001787 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001788 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1789
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001790 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001791}
1792
Eric Paris9d960982009-06-11 14:31:37 -04001793void audit_log_key(struct audit_buffer *ab, char *key)
1794{
1795 audit_log_format(ab, " key=");
1796 if (key)
1797 audit_log_untrustedstring(ab, key);
1798 else
1799 audit_log_format(ab, "(null)");
1800}
1801
Eric Parisb24a30a2013-04-30 15:30:32 -04001802void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1803{
1804 int i;
1805
1806 audit_log_format(ab, " %s=", prefix);
1807 CAP_FOR_EACH_U32(i) {
1808 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04001809 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04001810 }
1811}
1812
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04001813static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04001814{
1815 kernel_cap_t *perm = &name->fcap.permitted;
1816 kernel_cap_t *inh = &name->fcap.inheritable;
1817 int log = 0;
1818
1819 if (!cap_isclear(*perm)) {
1820 audit_log_cap(ab, "cap_fp", perm);
1821 log = 1;
1822 }
1823 if (!cap_isclear(*inh)) {
1824 audit_log_cap(ab, "cap_fi", inh);
1825 log = 1;
1826 }
1827
1828 if (log)
1829 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1830 name->fcap.fE, name->fcap_ver);
1831}
1832
1833static inline int audit_copy_fcaps(struct audit_names *name,
1834 const struct dentry *dentry)
1835{
1836 struct cpu_vfs_cap_data caps;
1837 int rc;
1838
1839 if (!dentry)
1840 return 0;
1841
1842 rc = get_vfs_caps_from_disk(dentry, &caps);
1843 if (rc)
1844 return rc;
1845
1846 name->fcap.permitted = caps.permitted;
1847 name->fcap.inheritable = caps.inheritable;
1848 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1849 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1850 VFS_CAP_REVISION_SHIFT;
1851
1852 return 0;
1853}
1854
1855/* Copy inode data into an audit_names. */
1856void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001857 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04001858{
1859 name->ino = inode->i_ino;
1860 name->dev = inode->i_sb->s_dev;
1861 name->mode = inode->i_mode;
1862 name->uid = inode->i_uid;
1863 name->gid = inode->i_gid;
1864 name->rdev = inode->i_rdev;
1865 security_inode_getsecid(inode, &name->osid);
1866 audit_copy_fcaps(name, dentry);
1867}
1868
1869/**
1870 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1871 * @context: audit_context for the task
1872 * @n: audit_names structure with reportable details
1873 * @path: optional path to report instead of audit_names->name
1874 * @record_num: record number to report when handling a list of names
1875 * @call_panic: optional pointer to int that will be updated if secid fails
1876 */
1877void audit_log_name(struct audit_context *context, struct audit_names *n,
1878 struct path *path, int record_num, int *call_panic)
1879{
1880 struct audit_buffer *ab;
1881 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1882 if (!ab)
1883 return;
1884
1885 audit_log_format(ab, "item=%d", record_num);
1886
1887 if (path)
1888 audit_log_d_path(ab, " name=", path);
1889 else if (n->name) {
1890 switch (n->name_len) {
1891 case AUDIT_NAME_FULL:
1892 /* log the full path */
1893 audit_log_format(ab, " name=");
1894 audit_log_untrustedstring(ab, n->name->name);
1895 break;
1896 case 0:
1897 /* name was specified as a relative path and the
1898 * directory component is the cwd */
1899 audit_log_d_path(ab, " name=", &context->pwd);
1900 break;
1901 default:
1902 /* log the name's directory component */
1903 audit_log_format(ab, " name=");
1904 audit_log_n_untrustedstring(ab, n->name->name,
1905 n->name_len);
1906 }
1907 } else
1908 audit_log_format(ab, " name=(null)");
1909
Linus Torvalds425afcf2015-09-08 13:34:59 -07001910 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04001911 audit_log_format(ab, " inode=%lu"
1912 " dev=%02x:%02x mode=%#ho"
1913 " ouid=%u ogid=%u rdev=%02x:%02x",
1914 n->ino,
1915 MAJOR(n->dev),
1916 MINOR(n->dev),
1917 n->mode,
1918 from_kuid(&init_user_ns, n->uid),
1919 from_kgid(&init_user_ns, n->gid),
1920 MAJOR(n->rdev),
1921 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04001922 if (n->osid != 0) {
1923 char *ctx = NULL;
1924 u32 len;
1925 if (security_secid_to_secctx(
1926 n->osid, &ctx, &len)) {
1927 audit_log_format(ab, " osid=%u", n->osid);
1928 if (call_panic)
1929 *call_panic = 2;
1930 } else {
1931 audit_log_format(ab, " obj=%s", ctx);
1932 security_release_secctx(ctx, len);
1933 }
1934 }
1935
Jeff Laytond3aea842013-05-08 10:32:23 -04001936 /* log the audit_names record type */
1937 audit_log_format(ab, " nametype=");
1938 switch(n->type) {
1939 case AUDIT_TYPE_NORMAL:
1940 audit_log_format(ab, "NORMAL");
1941 break;
1942 case AUDIT_TYPE_PARENT:
1943 audit_log_format(ab, "PARENT");
1944 break;
1945 case AUDIT_TYPE_CHILD_DELETE:
1946 audit_log_format(ab, "DELETE");
1947 break;
1948 case AUDIT_TYPE_CHILD_CREATE:
1949 audit_log_format(ab, "CREATE");
1950 break;
1951 default:
1952 audit_log_format(ab, "UNKNOWN");
1953 break;
1954 }
1955
Eric Parisb24a30a2013-04-30 15:30:32 -04001956 audit_log_fcaps(ab, n);
1957 audit_log_end(ab);
1958}
1959
1960int audit_log_task_context(struct audit_buffer *ab)
1961{
1962 char *ctx = NULL;
1963 unsigned len;
1964 int error;
1965 u32 sid;
1966
1967 security_task_getsecid(current, &sid);
1968 if (!sid)
1969 return 0;
1970
1971 error = security_secid_to_secctx(sid, &ctx, &len);
1972 if (error) {
1973 if (error != -EINVAL)
1974 goto error_path;
1975 return 0;
1976 }
1977
1978 audit_log_format(ab, " subj=%s", ctx);
1979 security_release_secctx(ctx, len);
1980 return 0;
1981
1982error_path:
1983 audit_panic("error in audit_log_task_context");
1984 return error;
1985}
1986EXPORT_SYMBOL(audit_log_task_context);
1987
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001988void audit_log_d_path_exe(struct audit_buffer *ab,
1989 struct mm_struct *mm)
1990{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001991 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08001992
Davidlohr Bueso5b282552015-02-22 18:20:09 -08001993 if (!mm)
1994 goto out_null;
1995
1996 exe_file = get_mm_exe_file(mm);
1997 if (!exe_file)
1998 goto out_null;
1999
2000 audit_log_d_path(ab, " exe=", &exe_file->f_path);
2001 fput(exe_file);
2002 return;
2003out_null:
2004 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002005}
2006
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04002007struct tty_struct *audit_get_tty(struct task_struct *tsk)
2008{
2009 struct tty_struct *tty = NULL;
2010 unsigned long flags;
2011
2012 spin_lock_irqsave(&tsk->sighand->siglock, flags);
2013 if (tsk->signal)
2014 tty = tty_kref_get(tsk->signal->tty);
2015 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2016 return tty;
2017}
2018
2019void audit_put_tty(struct tty_struct *tty)
2020{
2021 tty_kref_put(tty);
2022}
2023
Eric Parisb24a30a2013-04-30 15:30:32 -04002024void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2025{
2026 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002027 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002028 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002029
2030 if (!ab)
2031 return;
2032
2033 /* tsk == current */
2034 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002035 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002036 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002037 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002038 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002039 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002040 task_ppid_nr(tsk),
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002041 task_pid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002042 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2043 from_kuid(&init_user_ns, cred->uid),
2044 from_kgid(&init_user_ns, cred->gid),
2045 from_kuid(&init_user_ns, cred->euid),
2046 from_kuid(&init_user_ns, cred->suid),
2047 from_kuid(&init_user_ns, cred->fsuid),
2048 from_kgid(&init_user_ns, cred->egid),
2049 from_kgid(&init_user_ns, cred->sgid),
2050 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002051 tty ? tty_name(tty) : "(none)",
2052 audit_get_sessionid(tsk));
2053 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002054 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002055 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002056 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002057 audit_log_task_context(ab);
2058}
2059EXPORT_SYMBOL(audit_log_task_info);
2060
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002061/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002062 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302063 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002064 * @link: the path that triggered the restriction
2065 */
2066void audit_log_link_denied(const char *operation, struct path *link)
2067{
2068 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002069 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002070
Eric Parisb24a30a2013-04-30 15:30:32 -04002071 name = kzalloc(sizeof(*name), GFP_NOFS);
2072 if (!name)
2073 return;
2074
2075 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002076 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2077 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002078 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002079 goto out;
2080 audit_log_format(ab, "op=%s", operation);
2081 audit_log_task_info(ab, current);
2082 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002083 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002084
2085 /* Generate AUDIT_PATH record with object. */
2086 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002087 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002088 audit_log_name(current->audit_context, name, link, 0, NULL);
2089out:
2090 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002091}
2092
2093/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002094 * audit_log_end - end one audit record
2095 * @ab: the audit_buffer
2096 *
Paul Moore4aa838722016-11-29 16:53:24 -05002097 * We can not do a netlink send inside an irq context because it blocks (last
2098 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2099 * queue and a tasklet is scheduled to remove them from the queue outside the
2100 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002101 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002102void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (!ab)
2105 return;
2106 if (!audit_rate_check()) {
2107 audit_log_lost("rate limit exceeded");
2108 } else {
Paul Mooreaf8b8242016-11-29 16:53:24 -05002109 skb_queue_tail(&audit_queue, ab->skb);
Paul Moore4aa838722016-11-29 16:53:24 -05002110 wake_up_interruptible(&kauditd_wait);
Eric Parisf3d357b2008-04-18 10:02:28 -04002111 ab->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Chris Wright16e19042005-05-06 15:53:34 +01002113 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002116/**
2117 * audit_log - Log an audit record
2118 * @ctx: audit context
2119 * @gfp_mask: type of allocation
2120 * @type: audit message type
2121 * @fmt: format string to use
2122 * @...: variable parameters matching the format string
2123 *
2124 * This is a convenience function that calls audit_log_start,
2125 * audit_log_vformat, and audit_log_end. It may be called
2126 * in any context.
2127 */
Daniel Walker5600b892007-10-18 03:06:10 -07002128void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002129 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 struct audit_buffer *ab;
2132 va_list args;
2133
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002134 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (ab) {
2136 va_start(args, fmt);
2137 audit_log_vformat(ab, fmt, args);
2138 va_end(args);
2139 audit_log_end(ab);
2140 }
2141}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002142
Mr Dash Four131ad622011-06-30 13:31:57 +02002143#ifdef CONFIG_SECURITY
2144/**
2145 * audit_log_secctx - Converts and logs SELinux context
2146 * @ab: audit_buffer
2147 * @secid: security number
2148 *
2149 * This is a helper function that calls security_secid_to_secctx to convert
2150 * secid to secctx and then adds the (converted) SELinux context to the audit
2151 * log by calling audit_log_format, thus also preventing leak of internal secid
2152 * to userspace. If secid cannot be converted audit_panic is called.
2153 */
2154void audit_log_secctx(struct audit_buffer *ab, u32 secid)
2155{
2156 u32 len;
2157 char *secctx;
2158
2159 if (security_secid_to_secctx(secid, &secctx, &len)) {
2160 audit_panic("Cannot convert secid to context");
2161 } else {
2162 audit_log_format(ab, " obj=%s", secctx);
2163 security_release_secctx(secctx, len);
2164 }
2165}
2166EXPORT_SYMBOL(audit_log_secctx);
2167#endif
2168
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002169EXPORT_SYMBOL(audit_log_start);
2170EXPORT_SYMBOL(audit_log_end);
2171EXPORT_SYMBOL(audit_log_format);
2172EXPORT_SYMBOL(audit_log);