blob: 5c25449843758a887bd0f04ce274726f2d0db59d [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 Briggsad2ac262014-01-07 13:08:41 -05001062 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -04001063 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 -04001064 audit_feature_names[which], !!old_feature, !!new_feature,
1065 !!old_lock, !!new_lock, res);
1066 audit_log_end(ab);
1067}
1068
1069static int audit_set_feature(struct sk_buff *skb)
1070{
1071 struct audit_features *uaf;
1072 int i;
1073
Fabian Frederick6eed9b22014-06-03 22:05:10 +02001074 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -04001075 uaf = nlmsg_data(nlmsg_hdr(skb));
1076
1077 /* if there is ever a version 2 we should handle that here */
1078
1079 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1080 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1081 u32 old_feature, new_feature, old_lock, new_lock;
1082
1083 /* if we are not changing this feature, move along */
1084 if (!(feature & uaf->mask))
1085 continue;
1086
1087 old_feature = af.features & feature;
1088 new_feature = uaf->features & feature;
1089 new_lock = (uaf->lock | af.lock) & feature;
1090 old_lock = af.lock & feature;
1091
1092 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +08001093 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -04001094 audit_log_feature_change(i, old_feature, new_feature,
1095 old_lock, new_lock, 0);
1096 return -EPERM;
1097 }
1098 }
1099 /* nothing invalid, do the changes */
1100 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1101 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1102 u32 old_feature, new_feature, old_lock, new_lock;
1103
1104 /* if we are not changing this feature, move along */
1105 if (!(feature & uaf->mask))
1106 continue;
1107
1108 old_feature = af.features & feature;
1109 new_feature = uaf->features & feature;
1110 old_lock = af.lock & feature;
1111 new_lock = (uaf->lock | af.lock) & feature;
1112
1113 if (new_feature != old_feature)
1114 audit_log_feature_change(i, old_feature, new_feature,
1115 old_lock, new_lock, 1);
1116
1117 if (new_feature)
1118 af.features |= feature;
1119 else
1120 af.features &= ~feature;
1121 af.lock |= new_lock;
1122 }
1123
1124 return 0;
1125}
1126
Paul Mooreb6c7c112017-05-02 10:16:05 -04001127static int audit_replace(struct pid *pid)
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001128{
Paul Mooreb6c7c112017-05-02 10:16:05 -04001129 pid_t pvnr;
Paul Moore5b523302017-03-21 11:26:35 -04001130 struct sk_buff *skb;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001131
Paul Mooreb6c7c112017-05-02 10:16:05 -04001132 pvnr = pid_vnr(pid);
1133 skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pvnr, sizeof(pvnr));
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001134 if (!skb)
1135 return -ENOMEM;
Paul Moore5b523302017-03-21 11:26:35 -04001136 return auditd_send_unicast_skb(skb);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1140{
Eric Parisdc9eb692013-04-19 13:23:09 -04001141 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int err;
Steve Grubbc0404992005-05-13 18:17:42 +01001144 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -04001146 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -05001147 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -04001148 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001150 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (err)
1152 return err;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -07001155 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001158 case AUDIT_GET: {
1159 struct audit_status s;
1160 memset(&s, 0, sizeof(s));
1161 s.enabled = audit_enabled;
1162 s.failure = audit_failure;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001163 /* NOTE: use pid_vnr() so the PID is relative to the current
1164 * namespace */
Paul Moore48d0e022017-05-02 10:16:05 -04001165 s.pid = auditd_pid_vnr();
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001166 s.rate_limit = audit_rate_limit;
1167 s.backlog_limit = audit_backlog_limit;
1168 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001169 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -05001170 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Paul Moore31975422016-11-29 16:53:25 -05001171 s.backlog_wait_time = audit_backlog_wait_time;
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001172 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001174 }
1175 case AUDIT_SET: {
1176 struct audit_status s;
1177 memset(&s, 0, sizeof(s));
1178 /* guard against past and future API changes */
1179 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
1180 if (s.mask & AUDIT_STATUS_ENABLED) {
1181 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001182 if (err < 0)
1183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001185 if (s.mask & AUDIT_STATUS_FAILURE) {
1186 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001187 if (err < 0)
1188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001190 if (s.mask & AUDIT_STATUS_PID) {
Paul Mooreb6c7c112017-05-02 10:16:05 -04001191 /* NOTE: we are using the vnr PID functions below
1192 * because the s.pid value is relative to the
1193 * namespace of the caller; at present this
1194 * doesn't matter much since you can really only
1195 * run auditd from the initial pid namespace, but
1196 * something to keep in mind if this changes */
1197 pid_t new_pid = s.pid;
Paul Moore5b523302017-03-21 11:26:35 -04001198 pid_t auditd_pid;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001199 struct pid *req_pid = task_tgid(current);
1200
Steve Grubb33e8a902017-10-17 18:29:22 -04001201 /* Sanity check - PID values must match. Setting
1202 * pid to 0 is how auditd ends auditing. */
1203 if (new_pid && (new_pid != pid_vnr(req_pid)))
Paul Mooreb6c7c112017-05-02 10:16:05 -04001204 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001205
Paul Moore5b523302017-03-21 11:26:35 -04001206 /* test the auditd connection */
Paul Mooreb6c7c112017-05-02 10:16:05 -04001207 audit_replace(req_pid);
Paul Moore5b523302017-03-21 11:26:35 -04001208
Paul Moore48d0e022017-05-02 10:16:05 -04001209 auditd_pid = auditd_pid_vnr();
Steve Grubb33e8a902017-10-17 18:29:22 -04001210 if (auditd_pid) {
1211 /* replacing a healthy auditd is not allowed */
1212 if (new_pid) {
1213 audit_log_config_change("audit_pid",
1214 new_pid, auditd_pid, 0);
1215 return -EEXIST;
1216 }
1217 /* only current auditd can unregister itself */
1218 if (pid_vnr(req_pid) != auditd_pid) {
1219 audit_log_config_change("audit_pid",
1220 new_pid, auditd_pid, 0);
1221 return -EACCES;
1222 }
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001223 }
Paul Moore5b523302017-03-21 11:26:35 -04001224
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001225 if (new_pid) {
Paul Moore5b523302017-03-21 11:26:35 -04001226 /* register a new auditd connection */
Paul Moore48d0e022017-05-02 10:16:05 -04001227 err = auditd_set(req_pid,
1228 NETLINK_CB(skb).portid,
1229 sock_net(NETLINK_CB(skb).sk));
1230 if (audit_enabled != AUDIT_OFF)
1231 audit_log_config_change("audit_pid",
1232 new_pid,
1233 auditd_pid,
1234 err ? 0 : 1);
1235 if (err)
1236 return err;
1237
Paul Moore5b523302017-03-21 11:26:35 -04001238 /* try to process any backlog */
1239 wake_up_interruptible(&kauditd_wait);
Paul Moore48d0e022017-05-02 10:16:05 -04001240 } else {
1241 if (audit_enabled != AUDIT_OFF)
1242 audit_log_config_change("audit_pid",
1243 new_pid,
1244 auditd_pid, 1);
1245
Paul Moore5b523302017-03-21 11:26:35 -04001246 /* unregister the auditd connection */
Paul Moorec81be522017-06-12 09:35:24 -04001247 auditd_reset(NULL);
Paul Moore48d0e022017-05-02 10:16:05 -04001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001250 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1251 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001252 if (err < 0)
1253 return err;
1254 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001255 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001256 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001257 if (err < 0)
1258 return err;
1259 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001260 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1261 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1262 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001263 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001264 return -EINVAL;
1265 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1266 if (err < 0)
1267 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001268 }
Richard Guy Briggs92c82e82017-01-13 03:26:29 -05001269 if (s.mask == AUDIT_STATUS_LOST) {
1270 u32 lost = atomic_xchg(&audit_lost, 0);
1271
1272 audit_log_config_change("lost", 0, lost, 1);
1273 return lost;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001276 }
Eric Parisb0fed402013-05-22 12:54:49 -04001277 case AUDIT_GET_FEATURE:
1278 err = audit_get_feature(skb);
1279 if (err)
1280 return err;
1281 break;
1282 case AUDIT_SET_FEATURE:
1283 err = audit_set_feature(skb);
1284 if (err)
1285 return err;
1286 break;
Steve Grubb05474102005-05-21 00:18:37 +01001287 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001288 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1289 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001290 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1291 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001292
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001293 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001294 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001295 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001296 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001297 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001298 if (err)
1299 break;
1300 }
Eric Parisdc9eb692013-04-19 13:23:09 -04001301 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001302 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001303 audit_log_format(ab, " msg='%.*s'",
1304 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001305 (char *)data);
1306 else {
1307 int size;
1308
Eric Parisf7616102013-04-11 11:25:00 -04001309 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001310 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001311 if (size > 0 &&
1312 ((unsigned char *)data)[size - 1] == '\0')
1313 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001314 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001315 }
Eric Paris50397bd2008-01-07 18:14:19 -05001316 audit_log_end(ab);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001319 case AUDIT_ADD_RULE:
1320 case AUDIT_DEL_RULE:
1321 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1322 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001323 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001324 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1325 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001326 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001327 return -EPERM;
1328 }
Paul Moore45a06422017-05-02 10:16:05 -04001329 err = audit_rule_change(msg_type, seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001331 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001332 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001333 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001334 case AUDIT_TRIM:
1335 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001336 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001337 audit_log_format(ab, " op=trim res=1");
1338 audit_log_end(ab);
1339 break;
1340 case AUDIT_MAKE_EQUIV: {
1341 void *bufp = data;
1342 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001343 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001344 char *old, *new;
1345
1346 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001347 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001348 break;
1349 memcpy(sizes, bufp, 2 * sizeof(u32));
1350 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001351 msglen -= 2 * sizeof(u32);
1352 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001353 if (IS_ERR(old)) {
1354 err = PTR_ERR(old);
1355 break;
1356 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001357 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001358 if (IS_ERR(new)) {
1359 err = PTR_ERR(new);
1360 kfree(old);
1361 break;
1362 }
1363 /* OK, here comes... */
1364 err = audit_tag_tree(old, new);
1365
Eric Parisdc9eb692013-04-19 13:23:09 -04001366 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001367
Al Viro74c3cbe2007-07-22 08:04:18 -04001368 audit_log_format(ab, " op=make_equiv old=");
1369 audit_log_untrustedstring(ab, old);
1370 audit_log_format(ab, " new=");
1371 audit_log_untrustedstring(ab, new);
1372 audit_log_format(ab, " res=%d", !err);
1373 audit_log_end(ab);
1374 kfree(old);
1375 kfree(new);
1376 break;
1377 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001378 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001379 len = 0;
1380 if (audit_sig_sid) {
1381 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1382 if (err)
1383 return err;
1384 }
Al Viroe1396062006-05-25 10:19:47 -04001385 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1386 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001387 if (audit_sig_sid)
1388 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001389 return -ENOMEM;
1390 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001391 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001392 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001393 if (audit_sig_sid) {
1394 memcpy(sig_data->ctx, ctx, len);
1395 security_release_secctx(ctx, len);
1396 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001397 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1398 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001399 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001400 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001401 case AUDIT_TTY_GET: {
1402 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001403 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001404
Peter Hurley2e28d382016-01-09 22:55:33 -08001405 t = READ_ONCE(current->signal->audit_tty);
1406 s.enabled = t & AUDIT_TTY_ENABLE;
1407 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001408
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001409 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001410 break;
1411 }
1412 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001413 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001414 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001415 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001416
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001417 memset(&s, 0, sizeof(s));
1418 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001419 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001420 /* check if new data is valid */
1421 if ((s.enabled != 0 && s.enabled != 1) ||
1422 (s.log_passwd != 0 && s.log_passwd != 1))
1423 err = -EINVAL;
1424
Peter Hurley2e28d382016-01-09 22:55:33 -08001425 if (err)
1426 t = READ_ONCE(current->signal->audit_tty);
1427 else {
1428 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1429 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001430 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001431 old.enabled = t & AUDIT_TTY_ENABLE;
1432 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001433
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001434 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001435 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1436 " old-log_passwd=%d new-log_passwd=%d res=%d",
1437 old.enabled, s.enabled, old.log_passwd,
1438 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001439 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001440 break;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 default:
1443 err = -EINVAL;
1444 break;
1445 }
1446
1447 return err < 0 ? err : 0;
1448}
1449
Paul Moorea9d16202017-05-02 10:16:05 -04001450/**
1451 * audit_receive - receive messages from a netlink control socket
1452 * @skb: the message buffer
1453 *
1454 * Parse the provided skb and deal with any messages that may be present,
1455 * malformed skbs are discarded.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001456 */
Paul Moorea9d16202017-05-02 10:16:05 -04001457static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Eric Parisea7ae602009-06-11 14:31:35 -04001459 struct nlmsghdr *nlh;
1460 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001461 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001462 * if the nlmsg_len was not aligned
1463 */
1464 int len;
1465 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Eric Parisea7ae602009-06-11 14:31:35 -04001467 nlh = nlmsg_hdr(skb);
1468 len = skb->len;
1469
Paul Moorea9d16202017-05-02 10:16:05 -04001470 mutex_lock(&audit_cmd_mutex);
Hong zhi guo94191212013-03-27 06:49:06 +00001471 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001472 err = audit_receive_msg(skb, nlh);
1473 /* if err or if this message says it wants a response */
1474 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Johannes Berg2d4bc9332017-04-12 14:34:04 +02001475 netlink_ack(skb, nlh, err, NULL);
Eric Parisea7ae602009-06-11 14:31:35 -04001476
Alexandru Copot2851da52013-03-28 23:31:29 +02001477 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001479 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001482/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001483static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001484{
1485 if (!capable(CAP_AUDIT_READ))
1486 return -EPERM;
1487
1488 return 0;
1489}
1490
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001491static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001493 struct netlink_kernel_cfg cfg = {
1494 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001495 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001496 .flags = NL_CFG_F_NONROOT_RECV,
1497 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001498 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001499
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001500 struct audit_net *aunet = net_generic(net, audit_net_id);
1501
Paul Moore5b523302017-03-21 11:26:35 -04001502 aunet->sk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
1503 if (aunet->sk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001504 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001505 return -ENOMEM;
1506 }
Paul Moore5b523302017-03-21 11:26:35 -04001507 aunet->sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1508
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001509 return 0;
1510}
1511
1512static void __net_exit audit_net_exit(struct net *net)
1513{
1514 struct audit_net *aunet = net_generic(net, audit_net_id);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001515
Paul Moore48d0e022017-05-02 10:16:05 -04001516 /* NOTE: you would think that we would want to check the auditd
1517 * connection and potentially reset it here if it lives in this
1518 * namespace, but since the auditd connection tracking struct holds a
1519 * reference to this namespace (see auditd_set()) we are only ever
1520 * going to get here after that connection has been released */
Paul Moore5b523302017-03-21 11:26:35 -04001521
1522 netlink_kernel_release(aunet->sk);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001523}
1524
Richard Guy Briggs86268772013-07-16 13:18:45 -04001525static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001526 .init = audit_net_init,
1527 .exit = audit_net_exit,
1528 .id = &audit_net_id,
1529 .size = sizeof(struct audit_net),
1530};
1531
1532/* Initialize audit support at boot time. */
1533static int __init audit_init(void)
1534{
1535 int i;
1536
Eric Parisa3f07112008-11-05 12:47:09 -05001537 if (audit_initialized == AUDIT_DISABLED)
1538 return 0;
1539
Paul Moore8cc96382017-05-02 10:16:05 -04001540 audit_buffer_cache = kmem_cache_create("audit_buffer",
1541 sizeof(struct audit_buffer),
1542 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Paul Mooreaf8b8242016-11-29 16:53:24 -05001544 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001545 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001546 skb_queue_head_init(&audit_hold_queue);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001547
Amy Griffisf368c07d2006-04-07 16:55:56 -04001548 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1549 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001550
Paul Moore5b523302017-03-21 11:26:35 -04001551 pr_info("initializing netlink subsys (%s)\n",
1552 audit_default ? "enabled" : "disabled");
1553 register_pernet_subsys(&audit_net_ops);
1554
1555 audit_initialized = AUDIT_INITIALIZED;
Paul Moore5b523302017-03-21 11:26:35 -04001556
Paul Moore6c9255642016-11-29 16:53:23 -05001557 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1558 if (IS_ERR(kauditd_task)) {
1559 int err = PTR_ERR(kauditd_task);
1560 panic("audit: failed to start the kauditd thread (%d)\n", err);
1561 }
1562
Steve Grubb7c397d02016-12-14 15:59:46 -05001563 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL,
1564 "state=initialized audit_enabled=%u res=1",
1565 audit_enabled);
Paul Moore6c9255642016-11-29 16:53:23 -05001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return 0;
1568}
Paul Moorebe4104a2017-09-01 09:44:44 -04001569postcore_initcall(audit_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1572static int __init audit_enable(char *str)
1573{
Paul Moore80ab4df2017-09-01 09:44:51 -04001574 long val;
1575
1576 if (kstrtol(str, 0, &val))
1577 panic("audit: invalid 'audit' parameter value (%s)\n", str);
1578 audit_default = (val ? AUDIT_ON : AUDIT_OFF);
1579
1580 if (audit_default == AUDIT_OFF)
Eric Parisa3f07112008-11-05 12:47:09 -05001581 audit_initialized = AUDIT_DISABLED;
Paul Moore5d842a52017-09-01 09:45:05 -04001582 if (audit_set_enabled(audit_default))
1583 panic("audit: error setting audit state (%d)\n", audit_default);
Eric Parisa3f07112008-11-05 12:47:09 -05001584
Joe Perchesd957f7b2014-01-14 10:33:12 -08001585 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001586 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001587
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001588 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590__setup("audit=", audit_enable);
1591
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001592/* Process kernel command-line parameter at boot time.
1593 * audit_backlog_limit=<n> */
1594static int __init audit_backlog_limit_set(char *str)
1595{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001596 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001597
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001598 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001599 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1600 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001601 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001602 return 1;
1603 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001604
1605 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001606 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001607
1608 return 1;
1609}
1610__setup("audit_backlog_limit=", audit_backlog_limit_set);
1611
Chris Wright16e19042005-05-06 15:53:34 +01001612static void audit_buffer_free(struct audit_buffer *ab)
1613{
Chris Wright8fc61152005-05-06 15:54:17 +01001614 if (!ab)
1615 return;
1616
Markus Elfringd865e572016-01-13 09:18:55 -05001617 kfree_skb(ab->skb);
Paul Moore8cc96382017-05-02 10:16:05 -04001618 kmem_cache_free(audit_buffer_cache, ab);
Chris Wright16e19042005-05-06 15:53:34 +01001619}
1620
Paul Moore8cc96382017-05-02 10:16:05 -04001621static struct audit_buffer *audit_buffer_alloc(struct audit_context *ctx,
1622 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001623{
Paul Moore8cc96382017-05-02 10:16:05 -04001624 struct audit_buffer *ab;
Chris Wright16e19042005-05-06 15:53:34 +01001625
Paul Moore8cc96382017-05-02 10:16:05 -04001626 ab = kmem_cache_alloc(audit_buffer_cache, gfp_mask);
1627 if (!ab)
1628 return NULL;
Eric Parisee080e62009-06-11 14:31:35 -04001629
1630 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1631 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001632 goto err;
Paul Moore8cc96382017-05-02 10:16:05 -04001633 if (!nlmsg_put(ab->skb, 0, 0, type, 0, 0))
1634 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001635
Paul Moore8cc96382017-05-02 10:16:05 -04001636 ab->ctx = ctx;
1637 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001638
Chris Wright16e19042005-05-06 15:53:34 +01001639 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001640
Chris Wright8fc61152005-05-06 15:54:17 +01001641err:
1642 audit_buffer_free(ab);
1643 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001646/**
1647 * audit_serial - compute a serial number for the audit record
1648 *
1649 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001650 * written to user-space as soon as they are generated, so a complete
1651 * audit record may be written in several pieces. The timestamp of the
1652 * record and this serial number are used by the user-space tools to
1653 * determine which pieces belong to the same audit record. The
1654 * (timestamp,serial) tuple is unique for each syscall and is live from
1655 * syscall entry to syscall exit.
1656 *
David Woodhousebfb44962005-05-21 21:08:09 +01001657 * NOTE: Another possibility is to store the formatted records off the
1658 * audit context (for those records that have a context), and emit them
1659 * all at syscall exit. However, this could delay the reporting of
1660 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001661 * halts).
1662 */
David Woodhousebfb44962005-05-21 21:08:09 +01001663unsigned int audit_serial(void)
1664{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001665 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001666
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001667 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001668}
1669
Daniel Walker5600b892007-10-18 03:06:10 -07001670static inline void audit_get_stamp(struct audit_context *ctx,
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001671 struct timespec64 *t, unsigned int *serial)
David Woodhousebfb44962005-05-21 21:08:09 +01001672{
Al Viro48887e62008-12-06 01:05:50 -05001673 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
Mel Gormane832bf42017-07-04 13:11:43 +01001674 *t = current_kernel_time64();
David Woodhousebfb44962005-05-21 21:08:09 +01001675 *serial = audit_serial();
1676 }
1677}
1678
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001679/**
1680 * audit_log_start - obtain an audit buffer
1681 * @ctx: audit_context (may be NULL)
1682 * @gfp_mask: type of allocation
1683 * @type: audit message type
1684 *
1685 * Returns audit_buffer pointer on success or NULL on error.
1686 *
1687 * Obtain an audit buffer. This routine does locking to obtain the
1688 * audit buffer, but then no locking is required for calls to
1689 * audit_log_*format. If the task (ctx) is a task that is currently in a
1690 * syscall, then the syscall is marked as auditable and an audit record
1691 * will be written at syscall exit. If there is no associated task, then
1692 * task context (ctx) should be NULL.
1693 */
Al Viro9796fdd2005-10-21 03:22:03 -04001694struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001695 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Paul Moore31975422016-11-29 16:53:25 -05001697 struct audit_buffer *ab;
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001698 struct timespec64 t;
Paul Moore31975422016-11-29 16:53:25 -05001699 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Parisa3f07112008-11-05 12:47:09 -05001701 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return NULL;
1703
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001704 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001705 return NULL;
1706
Paul Moore5b523302017-03-21 11:26:35 -04001707 /* NOTE: don't ever fail/sleep on these two conditions:
Paul Moorea09cfa42016-11-29 16:53:26 -05001708 * 1. auditd generated record - since we need auditd to drain the
1709 * queue; also, when we are checking for auditd, compare PIDs using
1710 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg()
1711 * using a PID anchored in the caller's namespace
Paul Moore5b523302017-03-21 11:26:35 -04001712 * 2. generator holding the audit_cmd_mutex - we don't want to block
1713 * while holding the mutex */
1714 if (!(auditd_test_task(current) ||
1715 (current == __mutex_owner(&audit_cmd_mutex)))) {
1716 long stime = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001717
Paul Moore31975422016-11-29 16:53:25 -05001718 while (audit_backlog_limit &&
1719 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1720 /* wake kauditd to try and flush the queue */
1721 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001722
Paul Moore31975422016-11-29 16:53:25 -05001723 /* sleep if we are allowed and we haven't exhausted our
1724 * backlog wait limit */
Paul Moore5b523302017-03-21 11:26:35 -04001725 if (gfpflags_allow_blocking(gfp_mask) && (stime > 0)) {
Paul Moore31975422016-11-29 16:53:25 -05001726 DECLARE_WAITQUEUE(wait, current);
1727
1728 add_wait_queue_exclusive(&audit_backlog_wait,
1729 &wait);
1730 set_current_state(TASK_UNINTERRUPTIBLE);
Paul Moore5b523302017-03-21 11:26:35 -04001731 stime = schedule_timeout(stime);
Paul Moore31975422016-11-29 16:53:25 -05001732 remove_wait_queue(&audit_backlog_wait, &wait);
1733 } else {
1734 if (audit_rate_check() && printk_ratelimit())
1735 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1736 skb_queue_len(&audit_queue),
1737 audit_backlog_limit);
1738 audit_log_lost("backlog limit exceeded");
1739 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001740 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001741 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001742 }
1743
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001744 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (!ab) {
1746 audit_log_lost("out of memory in audit_log_start");
1747 return NULL;
1748 }
1749
David Woodhousebfb44962005-05-21 21:08:09 +01001750 audit_get_stamp(ab->ctx, &t, &serial);
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001751 audit_log_format(ab, "audit(%llu.%03lu:%u): ",
1752 (unsigned long long)t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return ab;
1755}
1756
Chris Wright8fc61152005-05-06 15:54:17 +01001757/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001758 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001759 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001760 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001761 *
1762 * Returns 0 (no space) on failed expansion, or available space if
1763 * successful.
1764 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001765static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001766{
Chris Wright5ac52f32005-05-06 15:54:53 +01001767 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001768 int oldtail = skb_tailroom(skb);
1769 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1770 int newtail = skb_tailroom(skb);
1771
Chris Wright5ac52f32005-05-06 15:54:53 +01001772 if (ret < 0) {
1773 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001774 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001775 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001776
1777 skb->truesize += newtail - oldtail;
1778 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001781/*
1782 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * room in the audit buffer, more room will be allocated and vsnprint
1784 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001785 * can't format message larger than 1024 bytes, so we don't either.
1786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1788 va_list args)
1789{
1790 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001791 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001792 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 if (!ab)
1795 return;
1796
Chris Wright5ac52f32005-05-06 15:54:53 +01001797 BUG_ON(!ab->skb);
1798 skb = ab->skb;
1799 avail = skb_tailroom(skb);
1800 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001801 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001802 if (!avail)
1803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001805 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001806 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (len >= avail) {
1808 /* The printk buffer is 1024 bytes long, so if we get
1809 * here and AUDIT_BUFSIZ is at least 1024, then we can
1810 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001811 avail = audit_expand(ab,
1812 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001813 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001814 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001815 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Steve Grubb168b7172005-05-19 10:24:22 +01001817 if (len > 0)
1818 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001819out_va_end:
1820 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001821out:
1822 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001825/**
1826 * audit_log_format - format a message into the audit buffer.
1827 * @ab: audit_buffer
1828 * @fmt: format string
1829 * @...: optional parameters matching @fmt string
1830 *
1831 * All the work is done in audit_log_vformat.
1832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1834{
1835 va_list args;
1836
1837 if (!ab)
1838 return;
1839 va_start(args, fmt);
1840 audit_log_vformat(ab, fmt, args);
1841 va_end(args);
1842}
1843
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001844/**
Geliang Tang196a5082017-08-07 21:44:24 +08001845 * audit_log_n_hex - convert a buffer to hex and append it to the audit skb
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001846 * @ab: the audit_buffer
1847 * @buf: buffer to convert to hex
1848 * @len: length of @buf to be converted
1849 *
1850 * No return value; failure to expand is silently ignored.
1851 *
1852 * This function will take the passed buf and convert it into a string of
1853 * ascii hex digits. The new string is placed onto the skb.
1854 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001855void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001856 size_t len)
83c7d092005-04-29 15:54:44 +01001857{
Steve Grubb168b7172005-05-19 10:24:22 +01001858 int i, avail, new_len;
1859 unsigned char *ptr;
1860 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001861
Amy Griffis8ef2d302006-09-07 17:03:02 -04001862 if (!ab)
1863 return;
1864
Steve Grubb168b7172005-05-19 10:24:22 +01001865 BUG_ON(!ab->skb);
1866 skb = ab->skb;
1867 avail = skb_tailroom(skb);
1868 new_len = len<<1;
1869 if (new_len >= avail) {
1870 /* Round the buffer request up to the next multiple */
1871 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1872 avail = audit_expand(ab, new_len);
1873 if (!avail)
1874 return;
1875 }
1876
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001877 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001878 for (i = 0; i < len; i++)
1879 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001880 *ptr = 0;
1881 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001882}
1883
Amy Griffis9c937dc2006-06-08 23:19:31 -04001884/*
1885 * Format a string of no more than slen characters into the audit buffer,
1886 * enclosed in quote marks.
1887 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001888void audit_log_n_string(struct audit_buffer *ab, const char *string,
1889 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001890{
1891 int avail, new_len;
1892 unsigned char *ptr;
1893 struct sk_buff *skb;
1894
Amy Griffis8ef2d302006-09-07 17:03:02 -04001895 if (!ab)
1896 return;
1897
Amy Griffis9c937dc2006-06-08 23:19:31 -04001898 BUG_ON(!ab->skb);
1899 skb = ab->skb;
1900 avail = skb_tailroom(skb);
1901 new_len = slen + 3; /* enclosing quotes + null terminator */
1902 if (new_len > avail) {
1903 avail = audit_expand(ab, new_len);
1904 if (!avail)
1905 return;
1906 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001907 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001908 *ptr++ = '"';
1909 memcpy(ptr, string, slen);
1910 ptr += slen;
1911 *ptr++ = '"';
1912 *ptr = 0;
1913 skb_put(skb, slen + 2); /* don't include null terminator */
1914}
1915
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001916/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001917 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001918 * @string: string to be checked
1919 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001920 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001921bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001922{
1923 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001924 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001925 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001926 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001927 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001928 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001929}
1930
1931/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001932 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001933 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001934 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001935 * @string: string to be logged
1936 *
1937 * This code will escape a string that is passed to it if the string
1938 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001939 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001940 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001941 *
1942 * The caller specifies the number of characters in the string to log, which may
1943 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001944 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001945void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1946 size_t len)
83c7d092005-04-29 15:54:44 +01001947{
Eric Parisde6bbd12008-01-07 14:31:58 -05001948 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001949 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001950 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001951 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001952}
1953
Amy Griffis9c937dc2006-06-08 23:19:31 -04001954/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001955 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001956 * @ab: audit_buffer
1957 * @string: string to be logged
1958 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001959 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001960 * determine string length.
1961 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001962void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001963{
Eric Parisb556f8a2008-04-18 10:12:59 -04001964 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001965}
1966
Steve Grubb168b7172005-05-19 10:24:22 +01001967/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001969 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Jan Blunck44707fd2008-02-14 19:38:33 -08001971 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Chris Wright8fc61152005-05-06 15:54:17 +01001973 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001974 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Steve Grubb168b7172005-05-19 10:24:22 +01001976 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001977 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1978 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001979 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001982 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001983 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1984 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001985 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001986 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001987 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001988 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Eric Paris4d3fb702013-04-30 09:53:34 -04001991void audit_log_session_info(struct audit_buffer *ab)
1992{
Eric Paris4440e852013-11-27 17:35:17 -05001993 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001994 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1995
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001996 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001997}
1998
Eric Paris9d960982009-06-11 14:31:37 -04001999void audit_log_key(struct audit_buffer *ab, char *key)
2000{
2001 audit_log_format(ab, " key=");
2002 if (key)
2003 audit_log_untrustedstring(ab, key);
2004 else
2005 audit_log_format(ab, "(null)");
2006}
2007
Eric Parisb24a30a2013-04-30 15:30:32 -04002008void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
2009{
2010 int i;
2011
2012 audit_log_format(ab, " %s=", prefix);
2013 CAP_FOR_EACH_U32(i) {
2014 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04002015 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04002016 }
2017}
2018
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04002019static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04002020{
Richard Guy Briggs4b3e4ed2017-04-20 13:07:30 -04002021 audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
2022 audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
2023 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
2024 name->fcap.fE, name->fcap_ver);
Eric Parisb24a30a2013-04-30 15:30:32 -04002025}
2026
2027static inline int audit_copy_fcaps(struct audit_names *name,
2028 const struct dentry *dentry)
2029{
2030 struct cpu_vfs_cap_data caps;
2031 int rc;
2032
2033 if (!dentry)
2034 return 0;
2035
2036 rc = get_vfs_caps_from_disk(dentry, &caps);
2037 if (rc)
2038 return rc;
2039
2040 name->fcap.permitted = caps.permitted;
2041 name->fcap.inheritable = caps.inheritable;
2042 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2043 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
2044 VFS_CAP_REVISION_SHIFT;
2045
2046 return 0;
2047}
2048
2049/* Copy inode data into an audit_names. */
2050void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05002051 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04002052{
2053 name->ino = inode->i_ino;
2054 name->dev = inode->i_sb->s_dev;
2055 name->mode = inode->i_mode;
2056 name->uid = inode->i_uid;
2057 name->gid = inode->i_gid;
2058 name->rdev = inode->i_rdev;
2059 security_inode_getsecid(inode, &name->osid);
2060 audit_copy_fcaps(name, dentry);
2061}
2062
2063/**
2064 * audit_log_name - produce AUDIT_PATH record from struct audit_names
2065 * @context: audit_context for the task
2066 * @n: audit_names structure with reportable details
2067 * @path: optional path to report instead of audit_names->name
2068 * @record_num: record number to report when handling a list of names
2069 * @call_panic: optional pointer to int that will be updated if secid fails
2070 */
2071void audit_log_name(struct audit_context *context, struct audit_names *n,
Al Viro8bd10762016-11-20 20:36:51 -05002072 const struct path *path, int record_num, int *call_panic)
Eric Parisb24a30a2013-04-30 15:30:32 -04002073{
2074 struct audit_buffer *ab;
2075 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
2076 if (!ab)
2077 return;
2078
2079 audit_log_format(ab, "item=%d", record_num);
2080
2081 if (path)
2082 audit_log_d_path(ab, " name=", path);
2083 else if (n->name) {
2084 switch (n->name_len) {
2085 case AUDIT_NAME_FULL:
2086 /* log the full path */
2087 audit_log_format(ab, " name=");
2088 audit_log_untrustedstring(ab, n->name->name);
2089 break;
2090 case 0:
2091 /* name was specified as a relative path and the
2092 * directory component is the cwd */
2093 audit_log_d_path(ab, " name=", &context->pwd);
2094 break;
2095 default:
2096 /* log the name's directory component */
2097 audit_log_format(ab, " name=");
2098 audit_log_n_untrustedstring(ab, n->name->name,
2099 n->name_len);
2100 }
2101 } else
2102 audit_log_format(ab, " name=(null)");
2103
Linus Torvalds425afcf2015-09-08 13:34:59 -07002104 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04002105 audit_log_format(ab, " inode=%lu"
2106 " dev=%02x:%02x mode=%#ho"
2107 " ouid=%u ogid=%u rdev=%02x:%02x",
2108 n->ino,
2109 MAJOR(n->dev),
2110 MINOR(n->dev),
2111 n->mode,
2112 from_kuid(&init_user_ns, n->uid),
2113 from_kgid(&init_user_ns, n->gid),
2114 MAJOR(n->rdev),
2115 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04002116 if (n->osid != 0) {
2117 char *ctx = NULL;
2118 u32 len;
2119 if (security_secid_to_secctx(
2120 n->osid, &ctx, &len)) {
2121 audit_log_format(ab, " osid=%u", n->osid);
2122 if (call_panic)
2123 *call_panic = 2;
2124 } else {
2125 audit_log_format(ab, " obj=%s", ctx);
2126 security_release_secctx(ctx, len);
2127 }
2128 }
2129
Jeff Laytond3aea842013-05-08 10:32:23 -04002130 /* log the audit_names record type */
2131 audit_log_format(ab, " nametype=");
2132 switch(n->type) {
2133 case AUDIT_TYPE_NORMAL:
2134 audit_log_format(ab, "NORMAL");
2135 break;
2136 case AUDIT_TYPE_PARENT:
2137 audit_log_format(ab, "PARENT");
2138 break;
2139 case AUDIT_TYPE_CHILD_DELETE:
2140 audit_log_format(ab, "DELETE");
2141 break;
2142 case AUDIT_TYPE_CHILD_CREATE:
2143 audit_log_format(ab, "CREATE");
2144 break;
2145 default:
2146 audit_log_format(ab, "UNKNOWN");
2147 break;
2148 }
2149
Eric Parisb24a30a2013-04-30 15:30:32 -04002150 audit_log_fcaps(ab, n);
2151 audit_log_end(ab);
2152}
2153
2154int audit_log_task_context(struct audit_buffer *ab)
2155{
2156 char *ctx = NULL;
2157 unsigned len;
2158 int error;
2159 u32 sid;
2160
2161 security_task_getsecid(current, &sid);
2162 if (!sid)
2163 return 0;
2164
2165 error = security_secid_to_secctx(sid, &ctx, &len);
2166 if (error) {
2167 if (error != -EINVAL)
2168 goto error_path;
2169 return 0;
2170 }
2171
2172 audit_log_format(ab, " subj=%s", ctx);
2173 security_release_secctx(ctx, len);
2174 return 0;
2175
2176error_path:
2177 audit_panic("error in audit_log_task_context");
2178 return error;
2179}
2180EXPORT_SYMBOL(audit_log_task_context);
2181
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002182void audit_log_d_path_exe(struct audit_buffer *ab,
2183 struct mm_struct *mm)
2184{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002185 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002186
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002187 if (!mm)
2188 goto out_null;
2189
2190 exe_file = get_mm_exe_file(mm);
2191 if (!exe_file)
2192 goto out_null;
2193
2194 audit_log_d_path(ab, " exe=", &exe_file->f_path);
2195 fput(exe_file);
2196 return;
2197out_null:
2198 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002199}
2200
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04002201struct tty_struct *audit_get_tty(struct task_struct *tsk)
2202{
2203 struct tty_struct *tty = NULL;
2204 unsigned long flags;
2205
2206 spin_lock_irqsave(&tsk->sighand->siglock, flags);
2207 if (tsk->signal)
2208 tty = tty_kref_get(tsk->signal->tty);
2209 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2210 return tty;
2211}
2212
2213void audit_put_tty(struct tty_struct *tty)
2214{
2215 tty_kref_put(tty);
2216}
2217
Eric Parisb24a30a2013-04-30 15:30:32 -04002218void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2219{
2220 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002221 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002222 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002223
2224 if (!ab)
2225 return;
2226
2227 /* tsk == current */
2228 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002229 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002230 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002231 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002232 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002233 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002234 task_ppid_nr(tsk),
Paul Moorefa2bea22016-08-30 17:19:13 -04002235 task_tgid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002236 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2237 from_kuid(&init_user_ns, cred->uid),
2238 from_kgid(&init_user_ns, cred->gid),
2239 from_kuid(&init_user_ns, cred->euid),
2240 from_kuid(&init_user_ns, cred->suid),
2241 from_kuid(&init_user_ns, cred->fsuid),
2242 from_kgid(&init_user_ns, cred->egid),
2243 from_kgid(&init_user_ns, cred->sgid),
2244 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002245 tty ? tty_name(tty) : "(none)",
2246 audit_get_sessionid(tsk));
2247 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002248 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002249 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002250 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002251 audit_log_task_context(ab);
2252}
2253EXPORT_SYMBOL(audit_log_task_info);
2254
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002255/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002256 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302257 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002258 * @link: the path that triggered the restriction
2259 */
Al Viro8bd10762016-11-20 20:36:51 -05002260void audit_log_link_denied(const char *operation, const struct path *link)
Kees Cooka51d9ea2012-07-25 17:29:08 -07002261{
2262 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002263 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002264
Eric Parisb24a30a2013-04-30 15:30:32 -04002265 name = kzalloc(sizeof(*name), GFP_NOFS);
2266 if (!name)
2267 return;
2268
2269 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002270 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2271 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002272 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002273 goto out;
2274 audit_log_format(ab, "op=%s", operation);
2275 audit_log_task_info(ab, current);
2276 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002277 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002278
2279 /* Generate AUDIT_PATH record with object. */
2280 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002281 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002282 audit_log_name(current->audit_context, name, link, 0, NULL);
2283out:
2284 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002285}
2286
2287/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002288 * audit_log_end - end one audit record
2289 * @ab: the audit_buffer
2290 *
Paul Moore4aa838722016-11-29 16:53:24 -05002291 * We can not do a netlink send inside an irq context because it blocks (last
2292 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2293 * queue and a tasklet is scheduled to remove them from the queue outside the
2294 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002295 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002296void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Paul Moore5b523302017-03-21 11:26:35 -04002298 struct sk_buff *skb;
2299 struct nlmsghdr *nlh;
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (!ab)
2302 return;
Paul Moore5b523302017-03-21 11:26:35 -04002303
2304 if (audit_rate_check()) {
2305 skb = ab->skb;
Eric Parisf3d357b2008-04-18 10:02:28 -04002306 ab->skb = NULL;
Paul Moore5b523302017-03-21 11:26:35 -04002307
2308 /* setup the netlink header, see the comments in
2309 * kauditd_send_multicast_skb() for length quirks */
2310 nlh = nlmsg_hdr(skb);
2311 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
2312
2313 /* queue the netlink packet and poke the kauditd thread */
2314 skb_queue_tail(&audit_queue, skb);
2315 wake_up_interruptible(&kauditd_wait);
2316 } else
2317 audit_log_lost("rate limit exceeded");
2318
Chris Wright16e19042005-05-06 15:53:34 +01002319 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002322/**
2323 * audit_log - Log an audit record
2324 * @ctx: audit context
2325 * @gfp_mask: type of allocation
2326 * @type: audit message type
2327 * @fmt: format string to use
2328 * @...: variable parameters matching the format string
2329 *
2330 * This is a convenience function that calls audit_log_start,
2331 * audit_log_vformat, and audit_log_end. It may be called
2332 * in any context.
2333 */
Daniel Walker5600b892007-10-18 03:06:10 -07002334void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002335 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 struct audit_buffer *ab;
2338 va_list args;
2339
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002340 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (ab) {
2342 va_start(args, fmt);
2343 audit_log_vformat(ab, fmt, args);
2344 va_end(args);
2345 audit_log_end(ab);
2346 }
2347}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002348
2349EXPORT_SYMBOL(audit_log_start);
2350EXPORT_SYMBOL(audit_log_end);
2351EXPORT_SYMBOL(audit_log_format);
2352EXPORT_SYMBOL(audit_log);