blob: 2de74be7cef5aa505b7e3c7698eda00d1cc0b450 [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 *
Richard Guy Briggsd590dca2018-02-03 00:33:11 -050041 * Audit userspace, documentation, tests, and bug/issue trackers:
42 * https://github.com/linux-audit
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
Joe Perchesd957f7b2014-01-14 10:33:12 -080045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Davidlohr Bueso5b282552015-02-22 18:20:09 -080047#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/init.h>
Paul McQuade7153e402014-06-06 14:37:37 -070049#include <linux/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070050#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040052#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
David Woodhouseb7d11252005-05-19 10:56:58 +010054#include <linux/err.h>
55#include <linux/kthread.h>
Richard Guy Briggs46e959e2013-05-03 14:03:50 -040056#include <linux/kernel.h>
Eric Parisb24a30a2013-04-30 15:30:32 -040057#include <linux/syscalls.h>
Paul Moore5b523302017-03-21 11:26:35 -040058#include <linux/spinlock.h>
59#include <linux/rcupdate.h>
60#include <linux/mutex.h>
61#include <linux/gfp.h>
Paul Mooreb6c7c112017-05-02 10:16:05 -040062#include <linux/pid.h>
Paul Moore8cc96382017-05-02 10:16:05 -040063#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <linux/audit.h>
66
67#include <net/sock.h>
Amy Griffis93315ed2006-02-07 12:05:27 -050068#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/skbuff.h>
Mr Dash Four131ad622011-06-30 13:31:57 +020070#ifdef CONFIG_SECURITY
71#include <linux/security.h>
72#endif
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080073#include <linux/freezer.h>
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -070074#include <linux/pid_namespace.h>
Richard Guy Briggs33faba72013-07-16 13:18:45 -040075#include <net/netns/generic.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060076
77#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Eric Parisa3f07112008-11-05 12:47:09 -050079/* No auditing will take place until audit_initialized == AUDIT_INITIALIZED.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * (Initialization happens after skb_init is called.) */
Eric Parisa3f07112008-11-05 12:47:09 -050081#define AUDIT_DISABLED -1
82#define AUDIT_UNINITIALIZED 0
83#define AUDIT_INITIALIZED 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int audit_initialized;
85
Eric Paris1a6b9f22008-01-07 17:09:31 -050086#define AUDIT_OFF 0
87#define AUDIT_ON 1
88#define AUDIT_LOCKED 2
Paul Moore173743d2017-09-01 09:44:34 -040089u32 audit_enabled = AUDIT_OFF;
Paul Mooreb3b4fdf2017-09-01 09:44:57 -040090bool audit_ever_enabled = !!AUDIT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jan Engelhardtae9d67af2011-01-18 06:48:12 +010092EXPORT_SYMBOL_GPL(audit_enabled);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* Default state when kernel boots without any parameters. */
Paul Moore173743d2017-09-01 09:44:34 -040095static u32 audit_default = AUDIT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* If auditing cannot proceed, audit_failure selects what happens. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080098static u32 audit_failure = AUDIT_FAIL_PRINTK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Moore5b523302017-03-21 11:26:35 -0400100/* private audit network namespace index */
101static unsigned int audit_net_id;
102
103/**
104 * struct audit_net - audit private network namespace data
105 * @sk: communication socket
Pavel Emelyanov75c03712008-03-20 15:39:41 -0700106 */
Paul Moore5b523302017-03-21 11:26:35 -0400107struct audit_net {
108 struct sock *sk;
109};
110
111/**
112 * struct auditd_connection - kernel/auditd connection state
113 * @pid: auditd PID
114 * @portid: netlink portid
115 * @net: the associated network namespace
Paul Moore48d0e022017-05-02 10:16:05 -0400116 * @rcu: RCU head
Paul Moore5b523302017-03-21 11:26:35 -0400117 *
118 * Description:
119 * This struct is RCU protected; you must either hold the RCU lock for reading
Paul Moore48d0e022017-05-02 10:16:05 -0400120 * or the associated spinlock for writing.
Paul Moore5b523302017-03-21 11:26:35 -0400121 */
122static struct auditd_connection {
Paul Mooreb6c7c112017-05-02 10:16:05 -0400123 struct pid *pid;
Paul Moore5b523302017-03-21 11:26:35 -0400124 u32 portid;
125 struct net *net;
Paul Moore48d0e022017-05-02 10:16:05 -0400126 struct rcu_head rcu;
127} *auditd_conn = NULL;
128static DEFINE_SPINLOCK(auditd_conn_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700130/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * to that number per second. This prevents DoS attacks, but results in
132 * audit records being dropped. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800133static u32 audit_rate_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Richard Guy Briggs40c07752013-10-22 13:28:49 -0400135/* Number of outstanding audit_buffers allowed.
136 * When set to zero, this means unlimited. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800137static u32 audit_backlog_limit = 64;
Richard Guy Briggse789e562013-09-12 23:03:51 -0400138#define AUDIT_BACKLOG_WAIT_TIME (60 * HZ)
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800139static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100141/* The identity of the user shutting down the audit system. */
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800142kuid_t audit_sig_uid = INVALID_UID;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100143pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -0400144u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* Records can be lost in several ways:
147 0) [suppressed in audit_alloc]
148 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
149 2) out of memory in audit_log_move [alloc_skb]
150 3) suppressed due to audit_rate_limit
151 4) suppressed due to audit_backlog_limit
152*/
Richard Guy Briggs92c82e82017-01-13 03:26:29 -0500153static atomic_t audit_lost = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Amy Griffisf368c07d2006-04-07 16:55:56 -0400155/* Hash for inode-based rules */
156struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
157
Paul Moore8cc96382017-05-02 10:16:05 -0400158static struct kmem_cache *audit_buffer_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Paul Moorec6480202016-11-29 16:53:25 -0500160/* queue msgs to send via kauditd_task */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500161static struct sk_buff_head audit_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500162/* queue msgs due to temporary unicast send problems */
163static struct sk_buff_head audit_retry_queue;
164/* queue msgs waiting for new auditd connection */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500165static struct sk_buff_head audit_hold_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500166
167/* queue servicing thread */
David Woodhouseb7d11252005-05-19 10:56:58 +0100168static struct task_struct *kauditd_task;
169static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500170
171/* waitqueue for callers who are blocked on the audit backlog */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100172static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Eric Parisb0fed402013-05-22 12:54:49 -0400174static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION,
175 .mask = -1,
176 .features = 0,
177 .lock = 0,};
178
Eric Paris21b85c32013-05-23 14:26:00 -0400179static char *audit_feature_names[2] = {
Eric Parisd040e5a2013-05-24 09:18:04 -0400180 "only_unset_loginuid",
Eric Paris21b85c32013-05-23 14:26:00 -0400181 "loginuid_immutable",
Eric Parisb0fed402013-05-22 12:54:49 -0400182};
183
184
Amy Griffisf368c07d2006-04-07 16:55:56 -0400185/* Serialize requests from userspace. */
Al Viro916d7572009-06-24 00:02:38 -0400186DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
189 * audit records. Since printk uses a 1024 byte buffer, this buffer
190 * should be at least that large. */
191#define AUDIT_BUFSIZ 1024
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* The audit_buffer is used when formatting an audit record. The caller
194 * locks briefly to get the record off the freelist or to allocate the
195 * buffer, and locks briefly to send the buffer to the netlink layer or
196 * to place it on a transmit queue. Multiple audit_buffers can be in
197 * use simultaneously. */
198struct audit_buffer {
Chris Wright8fc61152005-05-06 15:54:17 +0100199 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400201 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Eric Parisf09ac9d2008-04-18 10:11:04 -0400204struct audit_reply {
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400205 __u32 portid;
Eric W. Biederman638a0fd2014-02-28 10:49:05 -0800206 struct net *net;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400207 struct sk_buff *skb;
208};
209
Paul Moore5b523302017-03-21 11:26:35 -0400210/**
211 * auditd_test_task - Check to see if a given task is an audit daemon
212 * @task: the task to check
213 *
214 * Description:
215 * Return 1 if the task is a registered audit daemon, 0 otherwise.
216 */
Paul Mooreb6c7c112017-05-02 10:16:05 -0400217int auditd_test_task(struct task_struct *task)
Paul Moore5b523302017-03-21 11:26:35 -0400218{
219 int rc;
Paul Moore48d0e022017-05-02 10:16:05 -0400220 struct auditd_connection *ac;
Paul Moore5b523302017-03-21 11:26:35 -0400221
222 rcu_read_lock();
Paul Moore48d0e022017-05-02 10:16:05 -0400223 ac = rcu_dereference(auditd_conn);
224 rc = (ac && ac->pid == task_tgid(task) ? 1 : 0);
Paul Moore5b523302017-03-21 11:26:35 -0400225 rcu_read_unlock();
226
227 return rc;
228}
229
230/**
Paul Mooreb6c7c112017-05-02 10:16:05 -0400231 * auditd_pid_vnr - Return the auditd PID relative to the namespace
Paul Mooreb6c7c112017-05-02 10:16:05 -0400232 *
233 * Description:
Paul Moore48d0e022017-05-02 10:16:05 -0400234 * Returns the PID in relation to the namespace, 0 on failure.
Paul Mooreb6c7c112017-05-02 10:16:05 -0400235 */
Paul Moore48d0e022017-05-02 10:16:05 -0400236static pid_t auditd_pid_vnr(void)
Paul Mooreb6c7c112017-05-02 10:16:05 -0400237{
238 pid_t pid;
Paul Moore48d0e022017-05-02 10:16:05 -0400239 const struct auditd_connection *ac;
Paul Mooreb6c7c112017-05-02 10:16:05 -0400240
241 rcu_read_lock();
Paul Moore48d0e022017-05-02 10:16:05 -0400242 ac = rcu_dereference(auditd_conn);
243 if (!ac || !ac->pid)
Paul Mooreb6c7c112017-05-02 10:16:05 -0400244 pid = 0;
245 else
Paul Moore48d0e022017-05-02 10:16:05 -0400246 pid = pid_vnr(ac->pid);
Paul Mooreb6c7c112017-05-02 10:16:05 -0400247 rcu_read_unlock();
248
249 return pid;
250}
251
252/**
Paul Moore5b523302017-03-21 11:26:35 -0400253 * audit_get_sk - Return the audit socket for the given network namespace
254 * @net: the destination network namespace
255 *
256 * Description:
257 * Returns the sock pointer if valid, NULL otherwise. The caller must ensure
258 * that a reference is held for the network namespace while the sock is in use.
259 */
260static struct sock *audit_get_sk(const struct net *net)
261{
262 struct audit_net *aunet;
263
264 if (!net)
265 return NULL;
266
267 aunet = net_generic(net, audit_net_id);
268 return aunet->sk;
269}
270
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000271void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Joe Perchesd957f7b2014-01-14 10:33:12 -0800273 switch (audit_failure) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 case AUDIT_FAIL_SILENT:
275 break;
276 case AUDIT_FAIL_PRINTK:
Eric Paris320f1b12008-01-23 22:55:05 -0500277 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800278 pr_err("%s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
280 case AUDIT_FAIL_PANIC:
Paul Moore5b523302017-03-21 11:26:35 -0400281 panic("audit: %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 }
284}
285
286static inline int audit_rate_check(void)
287{
288 static unsigned long last_check = 0;
289 static int messages = 0;
290 static DEFINE_SPINLOCK(lock);
291 unsigned long flags;
292 unsigned long now;
293 unsigned long elapsed;
294 int retval = 0;
295
296 if (!audit_rate_limit) return 1;
297
298 spin_lock_irqsave(&lock, flags);
299 if (++messages < audit_rate_limit) {
300 retval = 1;
301 } else {
302 now = jiffies;
303 elapsed = now - last_check;
304 if (elapsed > HZ) {
305 last_check = now;
306 messages = 0;
307 retval = 1;
308 }
309 }
310 spin_unlock_irqrestore(&lock, flags);
311
312 return retval;
313}
314
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700315/**
316 * audit_log_lost - conditionally log lost audit message event
317 * @message: the message stating reason for lost audit message
318 *
319 * Emit at least 1 message per second, even if audit_rate_check is
320 * throttling.
321 * Always increment the lost messages counter.
322*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323void audit_log_lost(const char *message)
324{
325 static unsigned long last_msg = 0;
326 static DEFINE_SPINLOCK(lock);
327 unsigned long flags;
328 unsigned long now;
329 int print;
330
331 atomic_inc(&audit_lost);
332
333 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
334
335 if (!print) {
336 spin_lock_irqsave(&lock, flags);
337 now = jiffies;
338 if (now - last_msg > HZ) {
339 print = 1;
340 last_msg = now;
341 }
342 spin_unlock_irqrestore(&lock, flags);
343 }
344
345 if (print) {
Eric Paris320f1b12008-01-23 22:55:05 -0500346 if (printk_ratelimit())
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800347 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
Eric Paris320f1b12008-01-23 22:55:05 -0500348 atomic_read(&audit_lost),
349 audit_rate_limit,
350 audit_backlog_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 audit_panic(message);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800355static int audit_log_config_change(char *function_name, u32 new, u32 old,
Eric Paris25323862008-04-18 10:09:25 -0400356 int allow_changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Eric Paris1a6b9f22008-01-07 17:09:31 -0500358 struct audit_buffer *ab;
359 int rc = 0;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500360
Eric Paris1a6b9f22008-01-07 17:09:31 -0500361 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
Kees Cook0644ec02013-01-11 14:32:07 -0800362 if (unlikely(!ab))
363 return rc;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800364 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
Eric Paris4d3fb702013-04-30 09:53:34 -0400365 audit_log_session_info(ab);
Eric Parisb122c372013-04-19 15:00:33 -0400366 rc = audit_log_task_context(ab);
367 if (rc)
368 allow_changes = 0; /* Something weird, deny request */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500369 audit_log_format(ab, " res=%d", allow_changes);
370 audit_log_end(ab);
371 return rc;
372}
373
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800374static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500375{
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800376 int allow_changes, rc = 0;
377 u32 old = *to_change;
Eric Paris1a6b9f22008-01-07 17:09:31 -0500378
379 /* check if we are locked */
380 if (audit_enabled == AUDIT_LOCKED)
381 allow_changes = 0;
382 else
383 allow_changes = 1;
384
385 if (audit_enabled != AUDIT_OFF) {
Eric Parisdc9eb692013-04-19 13:23:09 -0400386 rc = audit_log_config_change(function_name, new, old, allow_changes);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500387 if (rc)
388 allow_changes = 0;
389 }
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500390
391 /* If we are allowed, make the change */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500392 if (allow_changes == 1)
393 *to_change = new;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500394 /* Not allowed, update reason */
395 else if (rc == 0)
396 rc = -EPERM;
397 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800400static int audit_set_rate_limit(u32 limit)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500401{
Eric Parisdc9eb692013-04-19 13:23:09 -0400402 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500403}
404
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800405static int audit_set_backlog_limit(u32 limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Eric Parisdc9eb692013-04-19 13:23:09 -0400407 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800410static int audit_set_backlog_wait_time(u32 timeout)
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400411{
412 return audit_do_config_change("audit_backlog_wait_time",
Paul Moore31975422016-11-29 16:53:25 -0500413 &audit_backlog_wait_time, timeout);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400414}
415
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800416static int audit_set_enabled(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Eric Parisb593d382008-01-08 17:38:31 -0500418 int rc;
Pranith Kumar724e7bf2015-03-11 14:08:19 -0400419 if (state > AUDIT_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500421
Eric Parisdc9eb692013-04-19 13:23:09 -0400422 rc = audit_do_config_change("audit_enabled", &audit_enabled, state);
Eric Parisb593d382008-01-08 17:38:31 -0500423 if (!rc)
424 audit_ever_enabled |= !!state;
425
426 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800429static int audit_set_failure(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (state != AUDIT_FAIL_SILENT
432 && state != AUDIT_FAIL_PRINTK
433 && state != AUDIT_FAIL_PANIC)
434 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500435
Eric Parisdc9eb692013-04-19 13:23:09 -0400436 return audit_do_config_change("audit_failure", &audit_failure, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Paul Moore5b523302017-03-21 11:26:35 -0400439/**
Paul Moore48d0e022017-05-02 10:16:05 -0400440 * auditd_conn_free - RCU helper to release an auditd connection struct
441 * @rcu: RCU head
442 *
443 * Description:
444 * Drop any references inside the auditd connection tracking struct and free
445 * the memory.
446 */
447 static void auditd_conn_free(struct rcu_head *rcu)
448 {
449 struct auditd_connection *ac;
450
451 ac = container_of(rcu, struct auditd_connection, rcu);
452 put_pid(ac->pid);
453 put_net(ac->net);
454 kfree(ac);
455 }
456
457/**
Paul Moore5b523302017-03-21 11:26:35 -0400458 * auditd_set - Set/Reset the auditd connection state
459 * @pid: auditd PID
460 * @portid: auditd netlink portid
461 * @net: auditd network namespace pointer
462 *
463 * Description:
464 * This function will obtain and drop network namespace references as
Paul Moore48d0e022017-05-02 10:16:05 -0400465 * necessary. Returns zero on success, negative values on failure.
Paul Moore5b523302017-03-21 11:26:35 -0400466 */
Paul Moore48d0e022017-05-02 10:16:05 -0400467static int auditd_set(struct pid *pid, u32 portid, struct net *net)
Paul Moore5b523302017-03-21 11:26:35 -0400468{
469 unsigned long flags;
Paul Moore48d0e022017-05-02 10:16:05 -0400470 struct auditd_connection *ac_old, *ac_new;
Paul Moore5b523302017-03-21 11:26:35 -0400471
Paul Moore48d0e022017-05-02 10:16:05 -0400472 if (!pid || !net)
473 return -EINVAL;
474
475 ac_new = kzalloc(sizeof(*ac_new), GFP_KERNEL);
476 if (!ac_new)
477 return -ENOMEM;
478 ac_new->pid = get_pid(pid);
479 ac_new->portid = portid;
480 ac_new->net = get_net(net);
481
482 spin_lock_irqsave(&auditd_conn_lock, flags);
483 ac_old = rcu_dereference_protected(auditd_conn,
484 lockdep_is_held(&auditd_conn_lock));
485 rcu_assign_pointer(auditd_conn, ac_new);
486 spin_unlock_irqrestore(&auditd_conn_lock, flags);
487
488 if (ac_old)
489 call_rcu(&ac_old->rcu, auditd_conn_free);
490
491 return 0;
Paul Moore5b523302017-03-21 11:26:35 -0400492}
493
494/**
Paul Moore5b523302017-03-21 11:26:35 -0400495 * kauditd_print_skb - Print the audit record to the ring buffer
496 * @skb: audit record
497 *
498 * Whatever the reason, this packet may not make it to the auditd connection
499 * so write it via printk so the information isn't completely lost.
Eric Paris038cbcf2009-06-11 14:31:35 -0400500 */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500501static void kauditd_printk_skb(struct sk_buff *skb)
Eric Paris038cbcf2009-06-11 14:31:35 -0400502{
503 struct nlmsghdr *nlh = nlmsg_hdr(skb);
David S. Millerc64e66c2012-06-26 21:45:21 -0700504 char *data = nlmsg_data(nlh);
Eric Paris038cbcf2009-06-11 14:31:35 -0400505
Paul Moore5b523302017-03-21 11:26:35 -0400506 if (nlh->nlmsg_type != AUDIT_EOE && printk_ratelimit())
507 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
508}
509
510/**
511 * kauditd_rehold_skb - Handle a audit record send failure in the hold queue
512 * @skb: audit record
513 *
514 * Description:
515 * This should only be used by the kauditd_thread when it fails to flush the
516 * hold queue.
517 */
518static void kauditd_rehold_skb(struct sk_buff *skb)
519{
520 /* put the record back in the queue at the same place */
521 skb_queue_head(&audit_hold_queue, skb);
Eric Paris038cbcf2009-06-11 14:31:35 -0400522}
523
Paul Moorec6480202016-11-29 16:53:25 -0500524/**
525 * kauditd_hold_skb - Queue an audit record, waiting for auditd
526 * @skb: audit record
527 *
528 * Description:
529 * Queue the audit record, waiting for an instance of auditd. When this
530 * function is called we haven't given up yet on sending the record, but things
531 * are not looking good. The first thing we want to do is try to write the
532 * record via printk and then see if we want to try and hold on to the record
533 * and queue it, if we have room. If we want to hold on to the record, but we
534 * don't have room, record a record lost message.
535 */
536static void kauditd_hold_skb(struct sk_buff *skb)
Eric Parisf3d357b2008-04-18 10:02:28 -0400537{
Paul Moorec6480202016-11-29 16:53:25 -0500538 /* at this point it is uncertain if we will ever send this to auditd so
539 * try to send the message via printk before we go any further */
540 kauditd_printk_skb(skb);
Richard Guy Briggs32a1dba2015-11-04 08:23:50 -0500541
Paul Moorec6480202016-11-29 16:53:25 -0500542 /* can we just silently drop the message? */
543 if (!audit_default) {
544 kfree_skb(skb);
545 return;
546 }
547
548 /* if we have room, queue the message */
549 if (!audit_backlog_limit ||
550 skb_queue_len(&audit_hold_queue) < audit_backlog_limit) {
551 skb_queue_tail(&audit_hold_queue, skb);
552 return;
553 }
554
555 /* we have no other options - drop the message */
556 audit_log_lost("kauditd hold queue overflow");
557 kfree_skb(skb);
558}
559
560/**
561 * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd
562 * @skb: audit record
563 *
564 * Description:
565 * Not as serious as kauditd_hold_skb() as we still have a connected auditd,
566 * but for some reason we are having problems sending it audit records so
567 * queue the given record and attempt to resend.
568 */
569static void kauditd_retry_skb(struct sk_buff *skb)
570{
571 /* NOTE: because records should only live in the retry queue for a
572 * short period of time, before either being sent or moved to the hold
573 * queue, we don't currently enforce a limit on this queue */
574 skb_queue_tail(&audit_retry_queue, skb);
575}
576
577/**
Paul Moore264d5092017-04-10 11:16:59 -0400578 * auditd_reset - Disconnect the auditd connection
Paul Moorec81be522017-06-12 09:35:24 -0400579 * @ac: auditd connection state
Paul Moore264d5092017-04-10 11:16:59 -0400580 *
581 * Description:
582 * Break the auditd/kauditd connection and move all the queued records into the
Paul Moorec81be522017-06-12 09:35:24 -0400583 * hold queue in case auditd reconnects. It is important to note that the @ac
584 * pointer should never be dereferenced inside this function as it may be NULL
585 * or invalid, you can only compare the memory address! If @ac is NULL then
586 * the connection will always be reset.
Paul Moore264d5092017-04-10 11:16:59 -0400587 */
Paul Moorec81be522017-06-12 09:35:24 -0400588static void auditd_reset(const struct auditd_connection *ac)
Paul Moore264d5092017-04-10 11:16:59 -0400589{
Paul Moore48d0e022017-05-02 10:16:05 -0400590 unsigned long flags;
Paul Moore264d5092017-04-10 11:16:59 -0400591 struct sk_buff *skb;
Paul Moore48d0e022017-05-02 10:16:05 -0400592 struct auditd_connection *ac_old;
Paul Moore264d5092017-04-10 11:16:59 -0400593
594 /* if it isn't already broken, break the connection */
Paul Moore48d0e022017-05-02 10:16:05 -0400595 spin_lock_irqsave(&auditd_conn_lock, flags);
596 ac_old = rcu_dereference_protected(auditd_conn,
597 lockdep_is_held(&auditd_conn_lock));
Paul Moorec81be522017-06-12 09:35:24 -0400598 if (ac && ac != ac_old) {
599 /* someone already registered a new auditd connection */
600 spin_unlock_irqrestore(&auditd_conn_lock, flags);
601 return;
602 }
Paul Moore48d0e022017-05-02 10:16:05 -0400603 rcu_assign_pointer(auditd_conn, NULL);
604 spin_unlock_irqrestore(&auditd_conn_lock, flags);
605
606 if (ac_old)
607 call_rcu(&ac_old->rcu, auditd_conn_free);
Paul Moore264d5092017-04-10 11:16:59 -0400608
Paul Moorecd33f5f2017-06-12 11:53:09 -0400609 /* flush the retry queue to the hold queue, but don't touch the main
610 * queue since we need to process that normally for multicast */
Paul Moore264d5092017-04-10 11:16:59 -0400611 while ((skb = skb_dequeue(&audit_retry_queue)))
612 kauditd_hold_skb(skb);
Paul Moore264d5092017-04-10 11:16:59 -0400613}
614
615/**
Paul Moore5b523302017-03-21 11:26:35 -0400616 * auditd_send_unicast_skb - Send a record via unicast to auditd
617 * @skb: audit record
Paul Moorec6480202016-11-29 16:53:25 -0500618 *
619 * Description:
Paul Moore5b523302017-03-21 11:26:35 -0400620 * Send a skb to the audit daemon, returns positive/zero values on success and
621 * negative values on failure; in all cases the skb will be consumed by this
622 * function. If the send results in -ECONNREFUSED the connection with auditd
623 * will be reset. This function may sleep so callers should not hold any locks
624 * where this would cause a problem.
Paul Moorec6480202016-11-29 16:53:25 -0500625 */
Paul Moore5b523302017-03-21 11:26:35 -0400626static int auditd_send_unicast_skb(struct sk_buff *skb)
Paul Moorec6480202016-11-29 16:53:25 -0500627{
Paul Moore5b523302017-03-21 11:26:35 -0400628 int rc;
629 u32 portid;
630 struct net *net;
631 struct sock *sk;
Paul Moore48d0e022017-05-02 10:16:05 -0400632 struct auditd_connection *ac;
Paul Moorec6480202016-11-29 16:53:25 -0500633
Paul Moore5b523302017-03-21 11:26:35 -0400634 /* NOTE: we can't call netlink_unicast while in the RCU section so
635 * take a reference to the network namespace and grab local
636 * copies of the namespace, the sock, and the portid; the
637 * namespace and sock aren't going to go away while we hold a
638 * reference and if the portid does become invalid after the RCU
639 * section netlink_unicast() should safely return an error */
640
641 rcu_read_lock();
Paul Moore48d0e022017-05-02 10:16:05 -0400642 ac = rcu_dereference(auditd_conn);
643 if (!ac) {
Paul Moore5b523302017-03-21 11:26:35 -0400644 rcu_read_unlock();
Shu Wangb0659ae2017-07-18 14:37:24 +0800645 kfree_skb(skb);
Paul Moore5b523302017-03-21 11:26:35 -0400646 rc = -ECONNREFUSED;
647 goto err;
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500648 }
Paul Moore48d0e022017-05-02 10:16:05 -0400649 net = get_net(ac->net);
Paul Moore5b523302017-03-21 11:26:35 -0400650 sk = audit_get_sk(net);
Paul Moore48d0e022017-05-02 10:16:05 -0400651 portid = ac->portid;
Paul Moore5b523302017-03-21 11:26:35 -0400652 rcu_read_unlock();
Paul Moorec6480202016-11-29 16:53:25 -0500653
Paul Moore5b523302017-03-21 11:26:35 -0400654 rc = netlink_unicast(sk, skb, portid, 0);
655 put_net(net);
656 if (rc < 0)
657 goto err;
658
659 return rc;
660
661err:
Paul Moorec81be522017-06-12 09:35:24 -0400662 if (ac && rc == -ECONNREFUSED)
663 auditd_reset(ac);
Paul Moore5b523302017-03-21 11:26:35 -0400664 return rc;
Paul Moorec6480202016-11-29 16:53:25 -0500665}
666
667/**
Paul Moore5b523302017-03-21 11:26:35 -0400668 * kauditd_send_queue - Helper for kauditd_thread to flush skb queues
669 * @sk: the sending sock
670 * @portid: the netlink destination
671 * @queue: the skb queue to process
672 * @retry_limit: limit on number of netlink unicast failures
673 * @skb_hook: per-skb hook for additional processing
674 * @err_hook: hook called if the skb fails the netlink unicast send
675 *
676 * Description:
677 * Run through the given queue and attempt to send the audit records to auditd,
678 * returns zero on success, negative values on failure. It is up to the caller
679 * to ensure that the @sk is valid for the duration of this function.
680 *
Paul Moorec6480202016-11-29 16:53:25 -0500681 */
Paul Moore5b523302017-03-21 11:26:35 -0400682static int kauditd_send_queue(struct sock *sk, u32 portid,
683 struct sk_buff_head *queue,
684 unsigned int retry_limit,
685 void (*skb_hook)(struct sk_buff *skb),
686 void (*err_hook)(struct sk_buff *skb))
Paul Moorec6480202016-11-29 16:53:25 -0500687{
Paul Moore5b523302017-03-21 11:26:35 -0400688 int rc = 0;
689 struct sk_buff *skb;
690 static unsigned int failed = 0;
Paul Moorec6480202016-11-29 16:53:25 -0500691
Paul Moore5b523302017-03-21 11:26:35 -0400692 /* NOTE: kauditd_thread takes care of all our locking, we just use
693 * the netlink info passed to us (e.g. sk and portid) */
Paul Moore6c54e782016-11-29 16:53:26 -0500694
Paul Moore5b523302017-03-21 11:26:35 -0400695 while ((skb = skb_dequeue(queue))) {
696 /* call the skb_hook for each skb we touch */
697 if (skb_hook)
698 (*skb_hook)(skb);
699
700 /* can we send to anyone via unicast? */
701 if (!sk) {
702 if (err_hook)
703 (*err_hook)(skb);
704 continue;
705 }
706
707 /* grab an extra skb reference in case of error */
708 skb_get(skb);
709 rc = netlink_unicast(sk, skb, portid, 0);
710 if (rc < 0) {
711 /* fatal failure for our queue flush attempt? */
712 if (++failed >= retry_limit ||
713 rc == -ECONNREFUSED || rc == -EPERM) {
714 /* yes - error processing for the queue */
715 sk = NULL;
716 if (err_hook)
717 (*err_hook)(skb);
718 if (!skb_hook)
719 goto out;
720 /* keep processing with the skb_hook */
721 continue;
722 } else
723 /* no - requeue to preserve ordering */
724 skb_queue_head(queue, skb);
725 } else {
726 /* it worked - drop the extra reference and continue */
727 consume_skb(skb);
728 failed = 0;
729 }
Paul Moorec6480202016-11-29 16:53:25 -0500730 }
731
Paul Moore5b523302017-03-21 11:26:35 -0400732out:
733 return (rc >= 0 ? 0 : rc);
Eric Parisf3d357b2008-04-18 10:02:28 -0400734}
735
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500736/*
Paul Moorec6480202016-11-29 16:53:25 -0500737 * kauditd_send_multicast_skb - Send a record to any multicast listeners
738 * @skb: audit record
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400739 *
Paul Moorec6480202016-11-29 16:53:25 -0500740 * Description:
Paul Moore5b523302017-03-21 11:26:35 -0400741 * Write a multicast message to anyone listening in the initial network
742 * namespace. This function doesn't consume an skb as might be expected since
743 * it has to copy it anyways.
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400744 */
Paul Moorec6480202016-11-29 16:53:25 -0500745static void kauditd_send_multicast_skb(struct sk_buff *skb)
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400746{
Paul Moorec6480202016-11-29 16:53:25 -0500747 struct sk_buff *copy;
Paul Moore5b523302017-03-21 11:26:35 -0400748 struct sock *sock = audit_get_sk(&init_net);
Paul Moorec6480202016-11-29 16:53:25 -0500749 struct nlmsghdr *nlh;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400750
Paul Moore5b523302017-03-21 11:26:35 -0400751 /* NOTE: we are not taking an additional reference for init_net since
752 * we don't have to worry about it going away */
753
Richard Guy Briggs7f74ecd2014-04-22 21:31:58 -0400754 if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG))
755 return;
756
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400757 /*
758 * The seemingly wasteful skb_copy() rather than bumping the refcount
759 * using skb_get() is necessary because non-standard mods are made to
760 * the skb by the original kaudit unicast socket send routine. The
761 * existing auditd daemon assumes this breakage. Fixing this would
762 * require co-ordinating a change in the established protocol between
763 * the kaudit kernel subsystem and the auditd userspace code. There is
764 * no reason for new multicast clients to continue with this
765 * non-compliance.
766 */
Paul Moorec6480202016-11-29 16:53:25 -0500767 copy = skb_copy(skb, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400768 if (!copy)
769 return;
Paul Moorec6480202016-11-29 16:53:25 -0500770 nlh = nlmsg_hdr(copy);
771 nlh->nlmsg_len = skb->len;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400772
Paul Moorec6480202016-11-29 16:53:25 -0500773 nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400774}
775
Paul Moorec6480202016-11-29 16:53:25 -0500776/**
Paul Moore5b523302017-03-21 11:26:35 -0400777 * kauditd_thread - Worker thread to send audit records to userspace
778 * @dummy: unused
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500779 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800780static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100781{
Paul Moorec6480202016-11-29 16:53:25 -0500782 int rc;
Paul Moore5b523302017-03-21 11:26:35 -0400783 u32 portid = 0;
784 struct net *net = NULL;
785 struct sock *sk = NULL;
Paul Moore48d0e022017-05-02 10:16:05 -0400786 struct auditd_connection *ac;
Paul Moore4aa838722016-11-29 16:53:24 -0500787
Paul Moorec6480202016-11-29 16:53:25 -0500788#define UNICAST_RETRIES 5
Paul Moorec6480202016-11-29 16:53:25 -0500789
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700790 set_freezable();
Andrew Morton4899b8b2006-10-06 00:43:48 -0700791 while (!kthread_should_stop()) {
Paul Moore5b523302017-03-21 11:26:35 -0400792 /* NOTE: see the lock comments in auditd_send_unicast_skb() */
793 rcu_read_lock();
Paul Moore48d0e022017-05-02 10:16:05 -0400794 ac = rcu_dereference(auditd_conn);
795 if (!ac) {
Paul Moore5b523302017-03-21 11:26:35 -0400796 rcu_read_unlock();
797 goto main_queue;
798 }
Paul Moore48d0e022017-05-02 10:16:05 -0400799 net = get_net(ac->net);
Paul Moore5b523302017-03-21 11:26:35 -0400800 sk = audit_get_sk(net);
Paul Moore48d0e022017-05-02 10:16:05 -0400801 portid = ac->portid;
Paul Moore5b523302017-03-21 11:26:35 -0400802 rcu_read_unlock();
Eric Parisf3d357b2008-04-18 10:02:28 -0400803
Paul Moorec6480202016-11-29 16:53:25 -0500804 /* attempt to flush the hold queue */
Paul Moore5b523302017-03-21 11:26:35 -0400805 rc = kauditd_send_queue(sk, portid,
806 &audit_hold_queue, UNICAST_RETRIES,
807 NULL, kauditd_rehold_skb);
Paul Moorec81be522017-06-12 09:35:24 -0400808 if (ac && rc < 0) {
Paul Moore5b523302017-03-21 11:26:35 -0400809 sk = NULL;
Paul Moorec81be522017-06-12 09:35:24 -0400810 auditd_reset(ac);
Paul Moore5b523302017-03-21 11:26:35 -0400811 goto main_queue;
David Woodhouseb7d11252005-05-19 10:56:58 +0100812 }
Richard Guy Briggs3320c512013-01-24 13:15:11 -0500813
Paul Moorec6480202016-11-29 16:53:25 -0500814 /* attempt to flush the retry queue */
Paul Moore5b523302017-03-21 11:26:35 -0400815 rc = kauditd_send_queue(sk, portid,
816 &audit_retry_queue, UNICAST_RETRIES,
817 NULL, kauditd_hold_skb);
Paul Moorec81be522017-06-12 09:35:24 -0400818 if (ac && rc < 0) {
Paul Moore5b523302017-03-21 11:26:35 -0400819 sk = NULL;
Paul Moorec81be522017-06-12 09:35:24 -0400820 auditd_reset(ac);
Paul Moore5b523302017-03-21 11:26:35 -0400821 goto main_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500822 }
823
Paul Moore5b523302017-03-21 11:26:35 -0400824main_queue:
825 /* process the main queue - do the multicast send and attempt
826 * unicast, dump failed record sends to the retry queue; if
827 * sk == NULL due to previous failures we will just do the
Paul Moorec81be522017-06-12 09:35:24 -0400828 * multicast send and move the record to the hold queue */
Paul Moore264d5092017-04-10 11:16:59 -0400829 rc = kauditd_send_queue(sk, portid, &audit_queue, 1,
830 kauditd_send_multicast_skb,
Paul Moorec81be522017-06-12 09:35:24 -0400831 (sk ?
832 kauditd_retry_skb : kauditd_hold_skb));
833 if (ac && rc < 0)
834 auditd_reset(ac);
Paul Moore264d5092017-04-10 11:16:59 -0400835 sk = NULL;
Paul Moore4aa838722016-11-29 16:53:24 -0500836
Paul Moore5b523302017-03-21 11:26:35 -0400837 /* drop our netns reference, no auditd sends past this line */
838 if (net) {
839 put_net(net);
840 net = NULL;
Richard Guy Briggs3320c512013-01-24 13:15:11 -0500841 }
Paul Moore5b523302017-03-21 11:26:35 -0400842
843 /* we have processed all the queues so wake everyone */
844 wake_up(&audit_backlog_wait);
845
846 /* NOTE: we want to wake up if there is anything on the queue,
847 * regardless of if an auditd is connected, as we need to
848 * do the multicast send and rotate records from the
849 * main queue to the retry/hold queues */
850 wait_event_freezable(kauditd_wait,
851 (skb_queue_len(&audit_queue) ? 1 : 0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100852 }
Paul Moorec6480202016-11-29 16:53:25 -0500853
Andrew Morton4899b8b2006-10-06 00:43:48 -0700854 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100855}
856
Al Viro9044e6b2006-05-22 01:09:24 -0400857int audit_send_list(void *_dest)
858{
859 struct audit_netlink_list *dest = _dest;
Al Viro9044e6b2006-05-22 01:09:24 -0400860 struct sk_buff *skb;
Paul Moore5b523302017-03-21 11:26:35 -0400861 struct sock *sk = audit_get_sk(dest->net);
Al Viro9044e6b2006-05-22 01:09:24 -0400862
863 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400864 mutex_lock(&audit_cmd_mutex);
865 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400866
867 while ((skb = __skb_dequeue(&dest->q)) != NULL)
Paul Moore5b523302017-03-21 11:26:35 -0400868 netlink_unicast(sk, skb, dest->portid, 0);
Al Viro9044e6b2006-05-22 01:09:24 -0400869
Paul Moore5b523302017-03-21 11:26:35 -0400870 put_net(dest->net);
Al Viro9044e6b2006-05-22 01:09:24 -0400871 kfree(dest);
872
873 return 0;
874}
875
Paul Moore45a06422017-05-02 10:16:05 -0400876struct sk_buff *audit_make_reply(int seq, int type, int done,
Stephen Hemmingerb8800aa2010-10-20 17:23:50 -0700877 int multi, const void *payload, int size)
Al Viro9044e6b2006-05-22 01:09:24 -0400878{
879 struct sk_buff *skb;
880 struct nlmsghdr *nlh;
Al Viro9044e6b2006-05-22 01:09:24 -0400881 void *data;
882 int flags = multi ? NLM_F_MULTI : 0;
883 int t = done ? NLMSG_DONE : type;
884
Eric Parisee080e62009-06-11 14:31:35 -0400885 skb = nlmsg_new(size, GFP_KERNEL);
Al Viro9044e6b2006-05-22 01:09:24 -0400886 if (!skb)
887 return NULL;
888
Paul Moore45a06422017-05-02 10:16:05 -0400889 nlh = nlmsg_put(skb, 0, seq, t, size, flags);
David S. Millerc64e66c2012-06-26 21:45:21 -0700890 if (!nlh)
891 goto out_kfree_skb;
892 data = nlmsg_data(nlh);
Al Viro9044e6b2006-05-22 01:09:24 -0400893 memcpy(data, payload, size);
894 return skb;
895
David S. Millerc64e66c2012-06-26 21:45:21 -0700896out_kfree_skb:
897 kfree_skb(skb);
Al Viro9044e6b2006-05-22 01:09:24 -0400898 return NULL;
899}
900
Eric Parisf09ac9d2008-04-18 10:11:04 -0400901static int audit_send_reply_thread(void *arg)
902{
903 struct audit_reply *reply = (struct audit_reply *)arg;
Paul Moore5b523302017-03-21 11:26:35 -0400904 struct sock *sk = audit_get_sk(reply->net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400905
906 mutex_lock(&audit_cmd_mutex);
907 mutex_unlock(&audit_cmd_mutex);
908
909 /* Ignore failure. It'll only happen if the sender goes away,
910 because our timeout is set to infinite. */
Paul Moore5b523302017-03-21 11:26:35 -0400911 netlink_unicast(sk, reply->skb, reply->portid, 0);
912 put_net(reply->net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400913 kfree(reply);
914 return 0;
915}
Paul Moorec6480202016-11-29 16:53:25 -0500916
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700917/**
918 * audit_send_reply - send an audit reply message via netlink
Eric W. Biedermand211f1772014-03-08 15:31:54 -0800919 * @request_skb: skb of request we are replying to (used to target the reply)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700920 * @seq: sequence number
921 * @type: audit message type
922 * @done: done (last) flag
923 * @multi: multi-part message flag
924 * @payload: payload data
925 * @size: payload size
926 *
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400927 * Allocates an skb, builds the netlink message, and sends it to the port id.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700928 * No failure notifications.
929 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800930static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400931 int multi, const void *payload, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800933 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400934 struct sk_buff *skb;
935 struct task_struct *tsk;
936 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
937 GFP_KERNEL);
938
939 if (!reply)
940 return;
941
Paul Moore45a06422017-05-02 10:16:05 -0400942 skb = audit_make_reply(seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!skb)
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700944 goto out;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400945
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800946 reply->net = get_net(net);
Paul Moore45a06422017-05-02 10:16:05 -0400947 reply->portid = NETLINK_CB(request_skb).portid;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400948 reply->skb = skb;
949
950 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700951 if (!IS_ERR(tsk))
952 return;
953 kfree_skb(skb);
954out:
955 kfree(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
959 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
960 * control messages.
961 */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700962static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 int err = 0;
965
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400966 /* Only support initial user namespace for now. */
Eric Parisaa4af832014-03-30 19:07:54 -0400967 /*
968 * We return ECONNREFUSED because it tricks userspace into thinking
969 * that audit was not configured into the kernel. Lots of users
970 * configure their PAM stack (because that's what the distro does)
971 * to reject login if unable to send messages to audit. If we return
972 * ECONNREFUSED the PAM stack thinks the kernel does not have audit
973 * configured in and will let login proceed. If we return EPERM
974 * userspace will reject all logins. This should be removed when we
975 * support non init namespaces!!
976 */
Linus Torvalds0b747172014-04-12 12:38:53 -0700977 if (current_user_ns() != &init_user_ns)
Eric Parisaa4af832014-03-30 19:07:54 -0400978 return -ECONNREFUSED;
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -0700979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 switch (msg_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case AUDIT_ADD:
983 case AUDIT_DEL:
Eric Paris18900902013-04-18 19:16:36 -0400984 return -EOPNOTSUPP;
985 case AUDIT_GET:
986 case AUDIT_SET:
Eric Parisb0fed402013-05-22 12:54:49 -0400987 case AUDIT_GET_FEATURE:
988 case AUDIT_SET_FEATURE:
Eric Paris18900902013-04-18 19:16:36 -0400989 case AUDIT_LIST_RULES:
990 case AUDIT_ADD_RULE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500991 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100992 case AUDIT_SIGNAL_INFO:
Miloslav Trmac522ed772007-07-15 23:40:56 -0700993 case AUDIT_TTY_GET:
994 case AUDIT_TTY_SET:
Al Viro74c3cbe2007-07-22 08:04:18 -0400995 case AUDIT_TRIM:
996 case AUDIT_MAKE_EQUIV:
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400997 /* Only support auditd and auditctl in initial pid namespace
998 * for now. */
Ameen Ali5985de62015-02-23 15:38:00 -0500999 if (task_active_pid_ns(current) != &init_pid_ns)
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -04001000 return -EPERM;
1001
Eric W. Biederman90f62cf32014-04-23 14:29:27 -07001002 if (!netlink_capable(skb, CAP_AUDIT_CONTROL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = -EPERM;
1004 break;
Steve Grubb05474102005-05-21 00:18:37 +01001005 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001006 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1007 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
Eric W. Biederman90f62cf32014-04-23 14:29:27 -07001008 if (!netlink_capable(skb, CAP_AUDIT_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 err = -EPERM;
1010 break;
1011 default: /* bad msg */
1012 err = -EINVAL;
1013 }
1014
1015 return err;
1016}
1017
Paul Moore233a6862015-11-04 08:23:52 -05001018static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type)
Eric Paris50397bd2008-01-07 18:14:19 -05001019{
Eric Parisdc9eb692013-04-19 13:23:09 -04001020 uid_t uid = from_kuid(&init_user_ns, current_uid());
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05001021 pid_t pid = task_tgid_nr(current);
Eric Paris50397bd2008-01-07 18:14:19 -05001022
Tyler Hicks0868a5e2013-07-25 18:02:55 -07001023 if (!audit_enabled && msg_type != AUDIT_USER_AVC) {
Eric Paris50397bd2008-01-07 18:14:19 -05001024 *ab = NULL;
Paul Moore233a6862015-11-04 08:23:52 -05001025 return;
Eric Paris50397bd2008-01-07 18:14:19 -05001026 }
1027
1028 *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
Kees Cook0644ec02013-01-11 14:32:07 -08001029 if (unlikely(!*ab))
Paul Moore233a6862015-11-04 08:23:52 -05001030 return;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05001031 audit_log_format(*ab, "pid=%d uid=%u", pid, uid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001032 audit_log_session_info(*ab);
Eric Parisb122c372013-04-19 15:00:33 -04001033 audit_log_task_context(*ab);
Eric Paris50397bd2008-01-07 18:14:19 -05001034}
1035
Eric Parisb0fed402013-05-22 12:54:49 -04001036int is_audit_feature_set(int i)
1037{
1038 return af.features & AUDIT_FEATURE_TO_MASK(i);
1039}
1040
1041
1042static int audit_get_feature(struct sk_buff *skb)
1043{
1044 u32 seq;
1045
1046 seq = nlmsg_hdr(skb)->nlmsg_seq;
1047
Richard Guy Briggs9ef91512014-08-24 20:37:52 -04001048 audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af));
Eric Parisb0fed402013-05-22 12:54:49 -04001049
1050 return 0;
1051}
1052
1053static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature,
1054 u32 old_lock, u32 new_lock, int res)
1055{
1056 struct audit_buffer *ab;
1057
Gao fengb6c50fe2013-11-01 19:34:43 +08001058 if (audit_enabled == AUDIT_OFF)
1059 return;
1060
Eric Parisb0fed402013-05-22 12:54:49 -04001061 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
Richard Guy Briggs23138ea2018-02-21 04:30:07 -05001062 if (!ab)
1063 return;
Richard Guy Briggsad2ac262014-01-07 13:08:41 -05001064 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -04001065 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 -04001066 audit_feature_names[which], !!old_feature, !!new_feature,
1067 !!old_lock, !!new_lock, res);
1068 audit_log_end(ab);
1069}
1070
1071static int audit_set_feature(struct sk_buff *skb)
1072{
1073 struct audit_features *uaf;
1074 int i;
1075
Fabian Frederick6eed9b22014-06-03 22:05:10 +02001076 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -04001077 uaf = nlmsg_data(nlmsg_hdr(skb));
1078
1079 /* if there is ever a version 2 we should handle that here */
1080
1081 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1082 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1083 u32 old_feature, new_feature, old_lock, new_lock;
1084
1085 /* if we are not changing this feature, move along */
1086 if (!(feature & uaf->mask))
1087 continue;
1088
1089 old_feature = af.features & feature;
1090 new_feature = uaf->features & feature;
1091 new_lock = (uaf->lock | af.lock) & feature;
1092 old_lock = af.lock & feature;
1093
1094 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +08001095 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -04001096 audit_log_feature_change(i, old_feature, new_feature,
1097 old_lock, new_lock, 0);
1098 return -EPERM;
1099 }
1100 }
1101 /* nothing invalid, do the changes */
1102 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1103 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1104 u32 old_feature, new_feature, old_lock, new_lock;
1105
1106 /* if we are not changing this feature, move along */
1107 if (!(feature & uaf->mask))
1108 continue;
1109
1110 old_feature = af.features & feature;
1111 new_feature = uaf->features & feature;
1112 old_lock = af.lock & feature;
1113 new_lock = (uaf->lock | af.lock) & feature;
1114
1115 if (new_feature != old_feature)
1116 audit_log_feature_change(i, old_feature, new_feature,
1117 old_lock, new_lock, 1);
1118
1119 if (new_feature)
1120 af.features |= feature;
1121 else
1122 af.features &= ~feature;
1123 af.lock |= new_lock;
1124 }
1125
1126 return 0;
1127}
1128
Paul Mooreb6c7c112017-05-02 10:16:05 -04001129static int audit_replace(struct pid *pid)
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001130{
Paul Mooreb6c7c112017-05-02 10:16:05 -04001131 pid_t pvnr;
Paul Moore5b523302017-03-21 11:26:35 -04001132 struct sk_buff *skb;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001133
Paul Mooreb6c7c112017-05-02 10:16:05 -04001134 pvnr = pid_vnr(pid);
1135 skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pvnr, sizeof(pvnr));
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001136 if (!skb)
1137 return -ENOMEM;
Paul Moore5b523302017-03-21 11:26:35 -04001138 return auditd_send_unicast_skb(skb);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1142{
Eric Parisdc9eb692013-04-19 13:23:09 -04001143 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int err;
Steve Grubbc0404992005-05-13 18:17:42 +01001146 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -04001148 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -05001149 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -04001150 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001152 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (err)
1154 return err;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -07001157 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001160 case AUDIT_GET: {
1161 struct audit_status s;
1162 memset(&s, 0, sizeof(s));
1163 s.enabled = audit_enabled;
1164 s.failure = audit_failure;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001165 /* NOTE: use pid_vnr() so the PID is relative to the current
1166 * namespace */
Paul Moore48d0e022017-05-02 10:16:05 -04001167 s.pid = auditd_pid_vnr();
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001168 s.rate_limit = audit_rate_limit;
1169 s.backlog_limit = audit_backlog_limit;
1170 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001171 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -05001172 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Paul Moore31975422016-11-29 16:53:25 -05001173 s.backlog_wait_time = audit_backlog_wait_time;
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001174 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001176 }
1177 case AUDIT_SET: {
1178 struct audit_status s;
1179 memset(&s, 0, sizeof(s));
1180 /* guard against past and future API changes */
1181 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
1182 if (s.mask & AUDIT_STATUS_ENABLED) {
1183 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001184 if (err < 0)
1185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001187 if (s.mask & AUDIT_STATUS_FAILURE) {
1188 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001189 if (err < 0)
1190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001192 if (s.mask & AUDIT_STATUS_PID) {
Paul Mooreb6c7c112017-05-02 10:16:05 -04001193 /* NOTE: we are using the vnr PID functions below
1194 * because the s.pid value is relative to the
1195 * namespace of the caller; at present this
1196 * doesn't matter much since you can really only
1197 * run auditd from the initial pid namespace, but
1198 * something to keep in mind if this changes */
1199 pid_t new_pid = s.pid;
Paul Moore5b523302017-03-21 11:26:35 -04001200 pid_t auditd_pid;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001201 struct pid *req_pid = task_tgid(current);
1202
Steve Grubb33e8a902017-10-17 18:29:22 -04001203 /* Sanity check - PID values must match. Setting
1204 * pid to 0 is how auditd ends auditing. */
1205 if (new_pid && (new_pid != pid_vnr(req_pid)))
Paul Mooreb6c7c112017-05-02 10:16:05 -04001206 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001207
Paul Moore5b523302017-03-21 11:26:35 -04001208 /* test the auditd connection */
Paul Mooreb6c7c112017-05-02 10:16:05 -04001209 audit_replace(req_pid);
Paul Moore5b523302017-03-21 11:26:35 -04001210
Paul Moore48d0e022017-05-02 10:16:05 -04001211 auditd_pid = auditd_pid_vnr();
Steve Grubb33e8a902017-10-17 18:29:22 -04001212 if (auditd_pid) {
1213 /* replacing a healthy auditd is not allowed */
1214 if (new_pid) {
1215 audit_log_config_change("audit_pid",
1216 new_pid, auditd_pid, 0);
1217 return -EEXIST;
1218 }
1219 /* only current auditd can unregister itself */
1220 if (pid_vnr(req_pid) != auditd_pid) {
1221 audit_log_config_change("audit_pid",
1222 new_pid, auditd_pid, 0);
1223 return -EACCES;
1224 }
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001225 }
Paul Moore5b523302017-03-21 11:26:35 -04001226
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001227 if (new_pid) {
Paul Moore5b523302017-03-21 11:26:35 -04001228 /* register a new auditd connection */
Paul Moore48d0e022017-05-02 10:16:05 -04001229 err = auditd_set(req_pid,
1230 NETLINK_CB(skb).portid,
1231 sock_net(NETLINK_CB(skb).sk));
1232 if (audit_enabled != AUDIT_OFF)
1233 audit_log_config_change("audit_pid",
1234 new_pid,
1235 auditd_pid,
1236 err ? 0 : 1);
1237 if (err)
1238 return err;
1239
Paul Moore5b523302017-03-21 11:26:35 -04001240 /* try to process any backlog */
1241 wake_up_interruptible(&kauditd_wait);
Paul Moore48d0e022017-05-02 10:16:05 -04001242 } else {
1243 if (audit_enabled != AUDIT_OFF)
1244 audit_log_config_change("audit_pid",
1245 new_pid,
1246 auditd_pid, 1);
1247
Paul Moore5b523302017-03-21 11:26:35 -04001248 /* unregister the auditd connection */
Paul Moorec81be522017-06-12 09:35:24 -04001249 auditd_reset(NULL);
Paul Moore48d0e022017-05-02 10:16:05 -04001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001252 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1253 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001254 if (err < 0)
1255 return err;
1256 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001257 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001258 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001259 if (err < 0)
1260 return err;
1261 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001262 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1263 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1264 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001265 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001266 return -EINVAL;
1267 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1268 if (err < 0)
1269 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001270 }
Richard Guy Briggs92c82e82017-01-13 03:26:29 -05001271 if (s.mask == AUDIT_STATUS_LOST) {
1272 u32 lost = atomic_xchg(&audit_lost, 0);
1273
1274 audit_log_config_change("lost", 0, lost, 1);
1275 return lost;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001278 }
Eric Parisb0fed402013-05-22 12:54:49 -04001279 case AUDIT_GET_FEATURE:
1280 err = audit_get_feature(skb);
1281 if (err)
1282 return err;
1283 break;
1284 case AUDIT_SET_FEATURE:
1285 err = audit_set_feature(skb);
1286 if (err)
1287 return err;
1288 break;
Steve Grubb05474102005-05-21 00:18:37 +01001289 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001290 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1291 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001292 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1293 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001294
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001295 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001296 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001297 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001298 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001299 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001300 if (err)
1301 break;
1302 }
Eric Parisdc9eb692013-04-19 13:23:09 -04001303 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001304 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001305 audit_log_format(ab, " msg='%.*s'",
1306 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001307 (char *)data);
1308 else {
1309 int size;
1310
Eric Parisf7616102013-04-11 11:25:00 -04001311 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001312 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001313 if (size > 0 &&
1314 ((unsigned char *)data)[size - 1] == '\0')
1315 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001316 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001317 }
Eric Paris50397bd2008-01-07 18:14:19 -05001318 audit_log_end(ab);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001321 case AUDIT_ADD_RULE:
1322 case AUDIT_DEL_RULE:
1323 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1324 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001325 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001326 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1327 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001328 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001329 return -EPERM;
1330 }
Paul Moore45a06422017-05-02 10:16:05 -04001331 err = audit_rule_change(msg_type, seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001333 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001334 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001335 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001336 case AUDIT_TRIM:
1337 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001338 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001339 audit_log_format(ab, " op=trim res=1");
1340 audit_log_end(ab);
1341 break;
1342 case AUDIT_MAKE_EQUIV: {
1343 void *bufp = data;
1344 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001345 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001346 char *old, *new;
1347
1348 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001349 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001350 break;
1351 memcpy(sizes, bufp, 2 * sizeof(u32));
1352 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001353 msglen -= 2 * sizeof(u32);
1354 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001355 if (IS_ERR(old)) {
1356 err = PTR_ERR(old);
1357 break;
1358 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001359 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001360 if (IS_ERR(new)) {
1361 err = PTR_ERR(new);
1362 kfree(old);
1363 break;
1364 }
1365 /* OK, here comes... */
1366 err = audit_tag_tree(old, new);
1367
Eric Parisdc9eb692013-04-19 13:23:09 -04001368 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001369
Al Viro74c3cbe2007-07-22 08:04:18 -04001370 audit_log_format(ab, " op=make_equiv old=");
1371 audit_log_untrustedstring(ab, old);
1372 audit_log_format(ab, " new=");
1373 audit_log_untrustedstring(ab, new);
1374 audit_log_format(ab, " res=%d", !err);
1375 audit_log_end(ab);
1376 kfree(old);
1377 kfree(new);
1378 break;
1379 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001380 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001381 len = 0;
1382 if (audit_sig_sid) {
1383 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1384 if (err)
1385 return err;
1386 }
Al Viroe1396062006-05-25 10:19:47 -04001387 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1388 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001389 if (audit_sig_sid)
1390 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001391 return -ENOMEM;
1392 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001393 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001394 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001395 if (audit_sig_sid) {
1396 memcpy(sig_data->ctx, ctx, len);
1397 security_release_secctx(ctx, len);
1398 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001399 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1400 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001401 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001402 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001403 case AUDIT_TTY_GET: {
1404 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001405 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001406
Peter Hurley2e28d382016-01-09 22:55:33 -08001407 t = READ_ONCE(current->signal->audit_tty);
1408 s.enabled = t & AUDIT_TTY_ENABLE;
1409 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001410
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001411 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001412 break;
1413 }
1414 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001415 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001416 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001417 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001418
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001419 memset(&s, 0, sizeof(s));
1420 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001421 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001422 /* check if new data is valid */
1423 if ((s.enabled != 0 && s.enabled != 1) ||
1424 (s.log_passwd != 0 && s.log_passwd != 1))
1425 err = -EINVAL;
1426
Peter Hurley2e28d382016-01-09 22:55:33 -08001427 if (err)
1428 t = READ_ONCE(current->signal->audit_tty);
1429 else {
1430 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1431 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001432 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001433 old.enabled = t & AUDIT_TTY_ENABLE;
1434 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001435
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001436 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001437 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1438 " old-log_passwd=%d new-log_passwd=%d res=%d",
1439 old.enabled, s.enabled, old.log_passwd,
1440 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001441 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001442 break;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 default:
1445 err = -EINVAL;
1446 break;
1447 }
1448
1449 return err < 0 ? err : 0;
1450}
1451
Paul Moorea9d16202017-05-02 10:16:05 -04001452/**
1453 * audit_receive - receive messages from a netlink control socket
1454 * @skb: the message buffer
1455 *
1456 * Parse the provided skb and deal with any messages that may be present,
1457 * malformed skbs are discarded.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001458 */
Paul Moorea9d16202017-05-02 10:16:05 -04001459static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Eric Parisea7ae602009-06-11 14:31:35 -04001461 struct nlmsghdr *nlh;
1462 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001463 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001464 * if the nlmsg_len was not aligned
1465 */
1466 int len;
1467 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Eric Parisea7ae602009-06-11 14:31:35 -04001469 nlh = nlmsg_hdr(skb);
1470 len = skb->len;
1471
Paul Moorea9d16202017-05-02 10:16:05 -04001472 mutex_lock(&audit_cmd_mutex);
Hong zhi guo94191212013-03-27 06:49:06 +00001473 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001474 err = audit_receive_msg(skb, nlh);
1475 /* if err or if this message says it wants a response */
1476 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Johannes Berg2d4bc9332017-04-12 14:34:04 +02001477 netlink_ack(skb, nlh, err, NULL);
Eric Parisea7ae602009-06-11 14:31:35 -04001478
Alexandru Copot2851da52013-03-28 23:31:29 +02001479 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001481 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001484/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001485static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001486{
1487 if (!capable(CAP_AUDIT_READ))
1488 return -EPERM;
1489
1490 return 0;
1491}
1492
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001493static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001495 struct netlink_kernel_cfg cfg = {
1496 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001497 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001498 .flags = NL_CFG_F_NONROOT_RECV,
1499 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001500 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001501
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001502 struct audit_net *aunet = net_generic(net, audit_net_id);
1503
Paul Moore5b523302017-03-21 11:26:35 -04001504 aunet->sk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
1505 if (aunet->sk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001506 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001507 return -ENOMEM;
1508 }
Paul Moore5b523302017-03-21 11:26:35 -04001509 aunet->sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1510
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001511 return 0;
1512}
1513
1514static void __net_exit audit_net_exit(struct net *net)
1515{
1516 struct audit_net *aunet = net_generic(net, audit_net_id);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001517
Paul Moore48d0e022017-05-02 10:16:05 -04001518 /* NOTE: you would think that we would want to check the auditd
1519 * connection and potentially reset it here if it lives in this
1520 * namespace, but since the auditd connection tracking struct holds a
1521 * reference to this namespace (see auditd_set()) we are only ever
1522 * going to get here after that connection has been released */
Paul Moore5b523302017-03-21 11:26:35 -04001523
1524 netlink_kernel_release(aunet->sk);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001525}
1526
Richard Guy Briggs86268772013-07-16 13:18:45 -04001527static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001528 .init = audit_net_init,
1529 .exit = audit_net_exit,
1530 .id = &audit_net_id,
1531 .size = sizeof(struct audit_net),
1532};
1533
1534/* Initialize audit support at boot time. */
1535static int __init audit_init(void)
1536{
1537 int i;
1538
Eric Parisa3f07112008-11-05 12:47:09 -05001539 if (audit_initialized == AUDIT_DISABLED)
1540 return 0;
1541
Paul Moore8cc96382017-05-02 10:16:05 -04001542 audit_buffer_cache = kmem_cache_create("audit_buffer",
1543 sizeof(struct audit_buffer),
1544 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Paul Mooreaf8b8242016-11-29 16:53:24 -05001546 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001547 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001548 skb_queue_head_init(&audit_hold_queue);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001549
Amy Griffisf368c07d2006-04-07 16:55:56 -04001550 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1551 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001552
Paul Moore5b523302017-03-21 11:26:35 -04001553 pr_info("initializing netlink subsys (%s)\n",
1554 audit_default ? "enabled" : "disabled");
1555 register_pernet_subsys(&audit_net_ops);
1556
1557 audit_initialized = AUDIT_INITIALIZED;
Paul Moore5b523302017-03-21 11:26:35 -04001558
Paul Moore6c9255642016-11-29 16:53:23 -05001559 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1560 if (IS_ERR(kauditd_task)) {
1561 int err = PTR_ERR(kauditd_task);
1562 panic("audit: failed to start the kauditd thread (%d)\n", err);
1563 }
1564
Steve Grubb7c397d02016-12-14 15:59:46 -05001565 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL,
1566 "state=initialized audit_enabled=%u res=1",
1567 audit_enabled);
Paul Moore6c9255642016-11-29 16:53:23 -05001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570}
Paul Moorebe4104a2017-09-01 09:44:44 -04001571postcore_initcall(audit_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1574static int __init audit_enable(char *str)
1575{
Paul Moore80ab4df2017-09-01 09:44:51 -04001576 long val;
1577
1578 if (kstrtol(str, 0, &val))
1579 panic("audit: invalid 'audit' parameter value (%s)\n", str);
1580 audit_default = (val ? AUDIT_ON : AUDIT_OFF);
1581
1582 if (audit_default == AUDIT_OFF)
Eric Parisa3f07112008-11-05 12:47:09 -05001583 audit_initialized = AUDIT_DISABLED;
Paul Moore5d842a52017-09-01 09:45:05 -04001584 if (audit_set_enabled(audit_default))
1585 panic("audit: error setting audit state (%d)\n", audit_default);
Eric Parisa3f07112008-11-05 12:47:09 -05001586
Joe Perchesd957f7b2014-01-14 10:33:12 -08001587 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001588 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001589
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001590 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592__setup("audit=", audit_enable);
1593
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001594/* Process kernel command-line parameter at boot time.
1595 * audit_backlog_limit=<n> */
1596static int __init audit_backlog_limit_set(char *str)
1597{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001598 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001599
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001600 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001601 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1602 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001603 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001604 return 1;
1605 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001606
1607 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001608 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001609
1610 return 1;
1611}
1612__setup("audit_backlog_limit=", audit_backlog_limit_set);
1613
Chris Wright16e19042005-05-06 15:53:34 +01001614static void audit_buffer_free(struct audit_buffer *ab)
1615{
Chris Wright8fc61152005-05-06 15:54:17 +01001616 if (!ab)
1617 return;
1618
Markus Elfringd865e572016-01-13 09:18:55 -05001619 kfree_skb(ab->skb);
Paul Moore8cc96382017-05-02 10:16:05 -04001620 kmem_cache_free(audit_buffer_cache, ab);
Chris Wright16e19042005-05-06 15:53:34 +01001621}
1622
Paul Moore8cc96382017-05-02 10:16:05 -04001623static struct audit_buffer *audit_buffer_alloc(struct audit_context *ctx,
1624 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001625{
Paul Moore8cc96382017-05-02 10:16:05 -04001626 struct audit_buffer *ab;
Chris Wright16e19042005-05-06 15:53:34 +01001627
Paul Moore8cc96382017-05-02 10:16:05 -04001628 ab = kmem_cache_alloc(audit_buffer_cache, gfp_mask);
1629 if (!ab)
1630 return NULL;
Eric Parisee080e62009-06-11 14:31:35 -04001631
1632 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1633 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001634 goto err;
Paul Moore8cc96382017-05-02 10:16:05 -04001635 if (!nlmsg_put(ab->skb, 0, 0, type, 0, 0))
1636 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001637
Paul Moore8cc96382017-05-02 10:16:05 -04001638 ab->ctx = ctx;
1639 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001640
Chris Wright16e19042005-05-06 15:53:34 +01001641 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001642
Chris Wright8fc61152005-05-06 15:54:17 +01001643err:
1644 audit_buffer_free(ab);
1645 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001646}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001648/**
1649 * audit_serial - compute a serial number for the audit record
1650 *
1651 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001652 * written to user-space as soon as they are generated, so a complete
1653 * audit record may be written in several pieces. The timestamp of the
1654 * record and this serial number are used by the user-space tools to
1655 * determine which pieces belong to the same audit record. The
1656 * (timestamp,serial) tuple is unique for each syscall and is live from
1657 * syscall entry to syscall exit.
1658 *
David Woodhousebfb44962005-05-21 21:08:09 +01001659 * NOTE: Another possibility is to store the formatted records off the
1660 * audit context (for those records that have a context), and emit them
1661 * all at syscall exit. However, this could delay the reporting of
1662 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001663 * halts).
1664 */
David Woodhousebfb44962005-05-21 21:08:09 +01001665unsigned int audit_serial(void)
1666{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001667 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001668
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001669 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001670}
1671
Daniel Walker5600b892007-10-18 03:06:10 -07001672static inline void audit_get_stamp(struct audit_context *ctx,
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001673 struct timespec64 *t, unsigned int *serial)
David Woodhousebfb44962005-05-21 21:08:09 +01001674{
Al Viro48887e62008-12-06 01:05:50 -05001675 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
Mel Gormane832bf42017-07-04 13:11:43 +01001676 *t = current_kernel_time64();
David Woodhousebfb44962005-05-21 21:08:09 +01001677 *serial = audit_serial();
1678 }
1679}
1680
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001681/**
1682 * audit_log_start - obtain an audit buffer
1683 * @ctx: audit_context (may be NULL)
1684 * @gfp_mask: type of allocation
1685 * @type: audit message type
1686 *
1687 * Returns audit_buffer pointer on success or NULL on error.
1688 *
1689 * Obtain an audit buffer. This routine does locking to obtain the
1690 * audit buffer, but then no locking is required for calls to
1691 * audit_log_*format. If the task (ctx) is a task that is currently in a
1692 * syscall, then the syscall is marked as auditable and an audit record
1693 * will be written at syscall exit. If there is no associated task, then
1694 * task context (ctx) should be NULL.
1695 */
Al Viro9796fdd2005-10-21 03:22:03 -04001696struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001697 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Paul Moore31975422016-11-29 16:53:25 -05001699 struct audit_buffer *ab;
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001700 struct timespec64 t;
Paul Moore31975422016-11-29 16:53:25 -05001701 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Eric Parisa3f07112008-11-05 12:47:09 -05001703 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return NULL;
1705
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001706 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001707 return NULL;
1708
Paul Moore5b523302017-03-21 11:26:35 -04001709 /* NOTE: don't ever fail/sleep on these two conditions:
Paul Moorea09cfa42016-11-29 16:53:26 -05001710 * 1. auditd generated record - since we need auditd to drain the
1711 * queue; also, when we are checking for auditd, compare PIDs using
1712 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg()
1713 * using a PID anchored in the caller's namespace
Paul Moore5b523302017-03-21 11:26:35 -04001714 * 2. generator holding the audit_cmd_mutex - we don't want to block
1715 * while holding the mutex */
1716 if (!(auditd_test_task(current) ||
1717 (current == __mutex_owner(&audit_cmd_mutex)))) {
1718 long stime = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001719
Paul Moore31975422016-11-29 16:53:25 -05001720 while (audit_backlog_limit &&
1721 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1722 /* wake kauditd to try and flush the queue */
1723 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001724
Paul Moore31975422016-11-29 16:53:25 -05001725 /* sleep if we are allowed and we haven't exhausted our
1726 * backlog wait limit */
Paul Moore5b523302017-03-21 11:26:35 -04001727 if (gfpflags_allow_blocking(gfp_mask) && (stime > 0)) {
Paul Moore31975422016-11-29 16:53:25 -05001728 DECLARE_WAITQUEUE(wait, current);
1729
1730 add_wait_queue_exclusive(&audit_backlog_wait,
1731 &wait);
1732 set_current_state(TASK_UNINTERRUPTIBLE);
Paul Moore5b523302017-03-21 11:26:35 -04001733 stime = schedule_timeout(stime);
Paul Moore31975422016-11-29 16:53:25 -05001734 remove_wait_queue(&audit_backlog_wait, &wait);
1735 } else {
1736 if (audit_rate_check() && printk_ratelimit())
1737 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1738 skb_queue_len(&audit_queue),
1739 audit_backlog_limit);
1740 audit_log_lost("backlog limit exceeded");
1741 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001742 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001743 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001744 }
1745
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001746 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!ab) {
1748 audit_log_lost("out of memory in audit_log_start");
1749 return NULL;
1750 }
1751
David Woodhousebfb44962005-05-21 21:08:09 +01001752 audit_get_stamp(ab->ctx, &t, &serial);
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001753 audit_log_format(ab, "audit(%llu.%03lu:%u): ",
1754 (unsigned long long)t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return ab;
1757}
1758
Chris Wright8fc61152005-05-06 15:54:17 +01001759/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001760 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001761 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001762 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001763 *
1764 * Returns 0 (no space) on failed expansion, or available space if
1765 * successful.
1766 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001767static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001768{
Chris Wright5ac52f32005-05-06 15:54:53 +01001769 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001770 int oldtail = skb_tailroom(skb);
1771 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1772 int newtail = skb_tailroom(skb);
1773
Chris Wright5ac52f32005-05-06 15:54:53 +01001774 if (ret < 0) {
1775 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001776 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001777 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001778
1779 skb->truesize += newtail - oldtail;
1780 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001781}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001783/*
1784 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 * room in the audit buffer, more room will be allocated and vsnprint
1786 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001787 * can't format message larger than 1024 bytes, so we don't either.
1788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1790 va_list args)
1791{
1792 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001793 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001794 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (!ab)
1797 return;
1798
Chris Wright5ac52f32005-05-06 15:54:53 +01001799 BUG_ON(!ab->skb);
1800 skb = ab->skb;
1801 avail = skb_tailroom(skb);
1802 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001803 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001804 if (!avail)
1805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001807 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001808 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (len >= avail) {
1810 /* The printk buffer is 1024 bytes long, so if we get
1811 * here and AUDIT_BUFSIZ is at least 1024, then we can
1812 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001813 avail = audit_expand(ab,
1814 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001815 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001816 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001817 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Steve Grubb168b7172005-05-19 10:24:22 +01001819 if (len > 0)
1820 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001821out_va_end:
1822 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001823out:
1824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001827/**
1828 * audit_log_format - format a message into the audit buffer.
1829 * @ab: audit_buffer
1830 * @fmt: format string
1831 * @...: optional parameters matching @fmt string
1832 *
1833 * All the work is done in audit_log_vformat.
1834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1836{
1837 va_list args;
1838
1839 if (!ab)
1840 return;
1841 va_start(args, fmt);
1842 audit_log_vformat(ab, fmt, args);
1843 va_end(args);
1844}
1845
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001846/**
Geliang Tang196a5082017-08-07 21:44:24 +08001847 * audit_log_n_hex - convert a buffer to hex and append it to the audit skb
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001848 * @ab: the audit_buffer
1849 * @buf: buffer to convert to hex
1850 * @len: length of @buf to be converted
1851 *
1852 * No return value; failure to expand is silently ignored.
1853 *
1854 * This function will take the passed buf and convert it into a string of
1855 * ascii hex digits. The new string is placed onto the skb.
1856 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001857void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001858 size_t len)
83c7d092005-04-29 15:54:44 +01001859{
Steve Grubb168b7172005-05-19 10:24:22 +01001860 int i, avail, new_len;
1861 unsigned char *ptr;
1862 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001863
Amy Griffis8ef2d302006-09-07 17:03:02 -04001864 if (!ab)
1865 return;
1866
Steve Grubb168b7172005-05-19 10:24:22 +01001867 BUG_ON(!ab->skb);
1868 skb = ab->skb;
1869 avail = skb_tailroom(skb);
1870 new_len = len<<1;
1871 if (new_len >= avail) {
1872 /* Round the buffer request up to the next multiple */
1873 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1874 avail = audit_expand(ab, new_len);
1875 if (!avail)
1876 return;
1877 }
1878
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001879 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001880 for (i = 0; i < len; i++)
1881 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001882 *ptr = 0;
1883 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001884}
1885
Amy Griffis9c937dc2006-06-08 23:19:31 -04001886/*
1887 * Format a string of no more than slen characters into the audit buffer,
1888 * enclosed in quote marks.
1889 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001890void audit_log_n_string(struct audit_buffer *ab, const char *string,
1891 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001892{
1893 int avail, new_len;
1894 unsigned char *ptr;
1895 struct sk_buff *skb;
1896
Amy Griffis8ef2d302006-09-07 17:03:02 -04001897 if (!ab)
1898 return;
1899
Amy Griffis9c937dc2006-06-08 23:19:31 -04001900 BUG_ON(!ab->skb);
1901 skb = ab->skb;
1902 avail = skb_tailroom(skb);
1903 new_len = slen + 3; /* enclosing quotes + null terminator */
1904 if (new_len > avail) {
1905 avail = audit_expand(ab, new_len);
1906 if (!avail)
1907 return;
1908 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001909 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001910 *ptr++ = '"';
1911 memcpy(ptr, string, slen);
1912 ptr += slen;
1913 *ptr++ = '"';
1914 *ptr = 0;
1915 skb_put(skb, slen + 2); /* don't include null terminator */
1916}
1917
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001918/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001919 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001920 * @string: string to be checked
1921 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001922 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001923bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001924{
1925 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001926 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001927 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001928 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001929 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001930 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001931}
1932
1933/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001934 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001935 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001936 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001937 * @string: string to be logged
1938 *
1939 * This code will escape a string that is passed to it if the string
1940 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001941 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001942 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001943 *
1944 * The caller specifies the number of characters in the string to log, which may
1945 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001946 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001947void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1948 size_t len)
83c7d092005-04-29 15:54:44 +01001949{
Eric Parisde6bbd12008-01-07 14:31:58 -05001950 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001951 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001952 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001953 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001954}
1955
Amy Griffis9c937dc2006-06-08 23:19:31 -04001956/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001957 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001958 * @ab: audit_buffer
1959 * @string: string to be logged
1960 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001961 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001962 * determine string length.
1963 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001964void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001965{
Eric Parisb556f8a2008-04-18 10:12:59 -04001966 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001967}
1968
Steve Grubb168b7172005-05-19 10:24:22 +01001969/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001971 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Jan Blunck44707fd2008-02-14 19:38:33 -08001973 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Chris Wright8fc61152005-05-06 15:54:17 +01001975 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001976 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Steve Grubb168b7172005-05-19 10:24:22 +01001978 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001979 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1980 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001981 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001984 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001985 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1986 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001987 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001988 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001989 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001990 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Eric Paris4d3fb702013-04-30 09:53:34 -04001993void audit_log_session_info(struct audit_buffer *ab)
1994{
Eric Paris4440e852013-11-27 17:35:17 -05001995 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001996 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1997
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001998 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001999}
2000
Eric Paris9d960982009-06-11 14:31:37 -04002001void audit_log_key(struct audit_buffer *ab, char *key)
2002{
2003 audit_log_format(ab, " key=");
2004 if (key)
2005 audit_log_untrustedstring(ab, key);
2006 else
2007 audit_log_format(ab, "(null)");
2008}
2009
Eric Parisb24a30a2013-04-30 15:30:32 -04002010void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
2011{
2012 int i;
2013
2014 audit_log_format(ab, " %s=", prefix);
2015 CAP_FOR_EACH_U32(i) {
2016 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04002017 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04002018 }
2019}
2020
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04002021static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04002022{
Richard Guy Briggs4b3e4ed2017-04-20 13:07:30 -04002023 audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
2024 audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
2025 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
2026 name->fcap.fE, name->fcap_ver);
Eric Parisb24a30a2013-04-30 15:30:32 -04002027}
2028
2029static inline int audit_copy_fcaps(struct audit_names *name,
2030 const struct dentry *dentry)
2031{
2032 struct cpu_vfs_cap_data caps;
2033 int rc;
2034
2035 if (!dentry)
2036 return 0;
2037
2038 rc = get_vfs_caps_from_disk(dentry, &caps);
2039 if (rc)
2040 return rc;
2041
2042 name->fcap.permitted = caps.permitted;
2043 name->fcap.inheritable = caps.inheritable;
2044 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2045 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
2046 VFS_CAP_REVISION_SHIFT;
2047
2048 return 0;
2049}
2050
2051/* Copy inode data into an audit_names. */
2052void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05002053 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04002054{
2055 name->ino = inode->i_ino;
2056 name->dev = inode->i_sb->s_dev;
2057 name->mode = inode->i_mode;
2058 name->uid = inode->i_uid;
2059 name->gid = inode->i_gid;
2060 name->rdev = inode->i_rdev;
2061 security_inode_getsecid(inode, &name->osid);
2062 audit_copy_fcaps(name, dentry);
2063}
2064
2065/**
2066 * audit_log_name - produce AUDIT_PATH record from struct audit_names
2067 * @context: audit_context for the task
2068 * @n: audit_names structure with reportable details
2069 * @path: optional path to report instead of audit_names->name
2070 * @record_num: record number to report when handling a list of names
2071 * @call_panic: optional pointer to int that will be updated if secid fails
2072 */
2073void audit_log_name(struct audit_context *context, struct audit_names *n,
Al Viro8bd10762016-11-20 20:36:51 -05002074 const struct path *path, int record_num, int *call_panic)
Eric Parisb24a30a2013-04-30 15:30:32 -04002075{
2076 struct audit_buffer *ab;
2077 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
2078 if (!ab)
2079 return;
2080
2081 audit_log_format(ab, "item=%d", record_num);
2082
2083 if (path)
2084 audit_log_d_path(ab, " name=", path);
2085 else if (n->name) {
2086 switch (n->name_len) {
2087 case AUDIT_NAME_FULL:
2088 /* log the full path */
2089 audit_log_format(ab, " name=");
2090 audit_log_untrustedstring(ab, n->name->name);
2091 break;
2092 case 0:
2093 /* name was specified as a relative path and the
2094 * directory component is the cwd */
2095 audit_log_d_path(ab, " name=", &context->pwd);
2096 break;
2097 default:
2098 /* log the name's directory component */
2099 audit_log_format(ab, " name=");
2100 audit_log_n_untrustedstring(ab, n->name->name,
2101 n->name_len);
2102 }
2103 } else
2104 audit_log_format(ab, " name=(null)");
2105
Linus Torvalds425afcf2015-09-08 13:34:59 -07002106 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04002107 audit_log_format(ab, " inode=%lu"
2108 " dev=%02x:%02x mode=%#ho"
2109 " ouid=%u ogid=%u rdev=%02x:%02x",
2110 n->ino,
2111 MAJOR(n->dev),
2112 MINOR(n->dev),
2113 n->mode,
2114 from_kuid(&init_user_ns, n->uid),
2115 from_kgid(&init_user_ns, n->gid),
2116 MAJOR(n->rdev),
2117 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04002118 if (n->osid != 0) {
2119 char *ctx = NULL;
2120 u32 len;
2121 if (security_secid_to_secctx(
2122 n->osid, &ctx, &len)) {
2123 audit_log_format(ab, " osid=%u", n->osid);
2124 if (call_panic)
2125 *call_panic = 2;
2126 } else {
2127 audit_log_format(ab, " obj=%s", ctx);
2128 security_release_secctx(ctx, len);
2129 }
2130 }
2131
Jeff Laytond3aea842013-05-08 10:32:23 -04002132 /* log the audit_names record type */
2133 audit_log_format(ab, " nametype=");
2134 switch(n->type) {
2135 case AUDIT_TYPE_NORMAL:
2136 audit_log_format(ab, "NORMAL");
2137 break;
2138 case AUDIT_TYPE_PARENT:
2139 audit_log_format(ab, "PARENT");
2140 break;
2141 case AUDIT_TYPE_CHILD_DELETE:
2142 audit_log_format(ab, "DELETE");
2143 break;
2144 case AUDIT_TYPE_CHILD_CREATE:
2145 audit_log_format(ab, "CREATE");
2146 break;
2147 default:
2148 audit_log_format(ab, "UNKNOWN");
2149 break;
2150 }
2151
Eric Parisb24a30a2013-04-30 15:30:32 -04002152 audit_log_fcaps(ab, n);
2153 audit_log_end(ab);
2154}
2155
2156int audit_log_task_context(struct audit_buffer *ab)
2157{
2158 char *ctx = NULL;
2159 unsigned len;
2160 int error;
2161 u32 sid;
2162
2163 security_task_getsecid(current, &sid);
2164 if (!sid)
2165 return 0;
2166
2167 error = security_secid_to_secctx(sid, &ctx, &len);
2168 if (error) {
2169 if (error != -EINVAL)
2170 goto error_path;
2171 return 0;
2172 }
2173
2174 audit_log_format(ab, " subj=%s", ctx);
2175 security_release_secctx(ctx, len);
2176 return 0;
2177
2178error_path:
2179 audit_panic("error in audit_log_task_context");
2180 return error;
2181}
2182EXPORT_SYMBOL(audit_log_task_context);
2183
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002184void audit_log_d_path_exe(struct audit_buffer *ab,
2185 struct mm_struct *mm)
2186{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002187 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002188
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002189 if (!mm)
2190 goto out_null;
2191
2192 exe_file = get_mm_exe_file(mm);
2193 if (!exe_file)
2194 goto out_null;
2195
2196 audit_log_d_path(ab, " exe=", &exe_file->f_path);
2197 fput(exe_file);
2198 return;
2199out_null:
2200 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002201}
2202
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04002203struct tty_struct *audit_get_tty(struct task_struct *tsk)
2204{
2205 struct tty_struct *tty = NULL;
2206 unsigned long flags;
2207
2208 spin_lock_irqsave(&tsk->sighand->siglock, flags);
2209 if (tsk->signal)
2210 tty = tty_kref_get(tsk->signal->tty);
2211 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2212 return tty;
2213}
2214
2215void audit_put_tty(struct tty_struct *tty)
2216{
2217 tty_kref_put(tty);
2218}
2219
Eric Parisb24a30a2013-04-30 15:30:32 -04002220void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2221{
2222 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002223 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002224 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002225
2226 if (!ab)
2227 return;
2228
2229 /* tsk == current */
2230 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002231 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002232 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002233 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002234 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002235 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002236 task_ppid_nr(tsk),
Paul Moorefa2bea22016-08-30 17:19:13 -04002237 task_tgid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002238 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2239 from_kuid(&init_user_ns, cred->uid),
2240 from_kgid(&init_user_ns, cred->gid),
2241 from_kuid(&init_user_ns, cred->euid),
2242 from_kuid(&init_user_ns, cred->suid),
2243 from_kuid(&init_user_ns, cred->fsuid),
2244 from_kgid(&init_user_ns, cred->egid),
2245 from_kgid(&init_user_ns, cred->sgid),
2246 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002247 tty ? tty_name(tty) : "(none)",
2248 audit_get_sessionid(tsk));
2249 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002250 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002251 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002252 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002253 audit_log_task_context(ab);
2254}
2255EXPORT_SYMBOL(audit_log_task_info);
2256
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002257/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002258 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302259 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002260 * @link: the path that triggered the restriction
2261 */
Al Viro8bd10762016-11-20 20:36:51 -05002262void audit_log_link_denied(const char *operation, const struct path *link)
Kees Cooka51d9ea2012-07-25 17:29:08 -07002263{
2264 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002265 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002266
Eric Parisb24a30a2013-04-30 15:30:32 -04002267 name = kzalloc(sizeof(*name), GFP_NOFS);
2268 if (!name)
2269 return;
2270
2271 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002272 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2273 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002274 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002275 goto out;
2276 audit_log_format(ab, "op=%s", operation);
2277 audit_log_task_info(ab, current);
2278 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002279 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002280
2281 /* Generate AUDIT_PATH record with object. */
2282 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002283 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002284 audit_log_name(current->audit_context, name, link, 0, NULL);
2285out:
2286 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002287}
2288
2289/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002290 * audit_log_end - end one audit record
2291 * @ab: the audit_buffer
2292 *
Paul Moore4aa838722016-11-29 16:53:24 -05002293 * We can not do a netlink send inside an irq context because it blocks (last
2294 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2295 * queue and a tasklet is scheduled to remove them from the queue outside the
2296 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002297 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002298void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Paul Moore5b523302017-03-21 11:26:35 -04002300 struct sk_buff *skb;
2301 struct nlmsghdr *nlh;
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (!ab)
2304 return;
Paul Moore5b523302017-03-21 11:26:35 -04002305
2306 if (audit_rate_check()) {
2307 skb = ab->skb;
Eric Parisf3d357b2008-04-18 10:02:28 -04002308 ab->skb = NULL;
Paul Moore5b523302017-03-21 11:26:35 -04002309
2310 /* setup the netlink header, see the comments in
2311 * kauditd_send_multicast_skb() for length quirks */
2312 nlh = nlmsg_hdr(skb);
2313 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
2314
2315 /* queue the netlink packet and poke the kauditd thread */
2316 skb_queue_tail(&audit_queue, skb);
2317 wake_up_interruptible(&kauditd_wait);
2318 } else
2319 audit_log_lost("rate limit exceeded");
2320
Chris Wright16e19042005-05-06 15:53:34 +01002321 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002324/**
2325 * audit_log - Log an audit record
2326 * @ctx: audit context
2327 * @gfp_mask: type of allocation
2328 * @type: audit message type
2329 * @fmt: format string to use
2330 * @...: variable parameters matching the format string
2331 *
2332 * This is a convenience function that calls audit_log_start,
2333 * audit_log_vformat, and audit_log_end. It may be called
2334 * in any context.
2335 */
Daniel Walker5600b892007-10-18 03:06:10 -07002336void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002337 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct audit_buffer *ab;
2340 va_list args;
2341
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002342 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (ab) {
2344 va_start(args, fmt);
2345 audit_log_vformat(ab, fmt, args);
2346 va_end(args);
2347 audit_log_end(ab);
2348 }
2349}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002350
2351EXPORT_SYMBOL(audit_log_start);
2352EXPORT_SYMBOL(audit_log_end);
2353EXPORT_SYMBOL(audit_log_format);
2354EXPORT_SYMBOL(audit_log);