blob: 10bc2bad2adf0107385a974b2fce437fd91c89c7 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* audit.c -- Auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Gateway between the kernel (e.g., selinux) and the user-space audit daemon.
3 * System-call specific features have moved to auditsc.c
4 *
Steve Grubb6a01b07f2007-01-19 14:39:55 -05005 * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 *
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +020024 * Goals: 1) Integrate fully with Security Modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 2) Minimal run-time overhead:
26 * a) Minimal when syscall auditing is disabled (audit_enable=0).
27 * b) Small when syscall auditing is enabled and no audit record
28 * is generated (defer as much work as possible to record
29 * generation time):
30 * i) context is allocated,
31 * ii) names from getname are stored without a copy, and
32 * iii) inode information stored from path_lookup.
33 * 3) Ability to disable syscall auditing at boot time (audit=0).
34 * 4) Usable by other parts of the kernel (if audit_log* is called,
35 * then a syscall record will be generated automatically for the
36 * current syscall).
37 * 5) Netlink interface to user-space.
38 * 6) Support low-overhead kernel-based filtering to minimize the
39 * information that must be passed to user-space.
40 *
85c87212005-04-29 16:23:29 +010041 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
43
Joe Perchesd957f7b2014-01-14 10:33:12 -080044#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
Davidlohr Bueso5b282552015-02-22 18:20:09 -080046#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/init.h>
Paul McQuade7153e402014-06-06 14:37:37 -070048#include <linux/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040051#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
David Woodhouseb7d11252005-05-19 10:56:58 +010053#include <linux/err.h>
54#include <linux/kthread.h>
Richard Guy Briggs46e959e2013-05-03 14:03:50 -040055#include <linux/kernel.h>
Eric Parisb24a30a2013-04-30 15:30:32 -040056#include <linux/syscalls.h>
Paul Moore5b523302017-03-21 11:26:35 -040057#include <linux/spinlock.h>
58#include <linux/rcupdate.h>
59#include <linux/mutex.h>
60#include <linux/gfp.h>
Paul Mooreb6c7c112017-05-02 10:16:05 -040061#include <linux/pid.h>
Paul Moore8cc96382017-05-02 10:16:05 -040062#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <linux/audit.h>
65
66#include <net/sock.h>
Amy Griffis93315ed2006-02-07 12:05:27 -050067#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/skbuff.h>
Mr Dash Four131ad622011-06-30 13:31:57 +020069#ifdef CONFIG_SECURITY
70#include <linux/security.h>
71#endif
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080072#include <linux/freezer.h>
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -070073#include <linux/pid_namespace.h>
Richard Guy Briggs33faba72013-07-16 13:18:45 -040074#include <net/netns/generic.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060075
76#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Eric Parisa3f07112008-11-05 12:47:09 -050078/* No auditing will take place until audit_initialized == AUDIT_INITIALIZED.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * (Initialization happens after skb_init is called.) */
Eric Parisa3f07112008-11-05 12:47:09 -050080#define AUDIT_DISABLED -1
81#define AUDIT_UNINITIALIZED 0
82#define AUDIT_INITIALIZED 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int audit_initialized;
84
Eric Paris1a6b9f22008-01-07 17:09:31 -050085#define AUDIT_OFF 0
86#define AUDIT_ON 1
87#define AUDIT_LOCKED 2
Joe Perches3e1d0bb2014-01-14 10:33:13 -080088u32 audit_enabled;
89u32 audit_ever_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jan Engelhardtae9d67af2011-01-18 06:48:12 +010091EXPORT_SYMBOL_GPL(audit_enabled);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Default state when kernel boots without any parameters. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080094static u32 audit_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* If auditing cannot proceed, audit_failure selects what happens. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080097static u32 audit_failure = AUDIT_FAIL_PRINTK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Paul Moore5b523302017-03-21 11:26:35 -040099/* private audit network namespace index */
100static unsigned int audit_net_id;
101
102/**
103 * struct audit_net - audit private network namespace data
104 * @sk: communication socket
Pavel Emelyanov75c03712008-03-20 15:39:41 -0700105 */
Paul Moore5b523302017-03-21 11:26:35 -0400106struct audit_net {
107 struct sock *sk;
108};
109
110/**
111 * struct auditd_connection - kernel/auditd connection state
112 * @pid: auditd PID
113 * @portid: netlink portid
114 * @net: the associated network namespace
115 * @lock: spinlock to protect write access
116 *
117 * Description:
118 * This struct is RCU protected; you must either hold the RCU lock for reading
119 * or the included spinlock for writing.
120 */
121static struct auditd_connection {
Paul Mooreb6c7c112017-05-02 10:16:05 -0400122 struct pid *pid;
Paul Moore5b523302017-03-21 11:26:35 -0400123 u32 portid;
124 struct net *net;
125 spinlock_t lock;
126} auditd_conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700128/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * to that number per second. This prevents DoS attacks, but results in
130 * audit records being dropped. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800131static u32 audit_rate_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Richard Guy Briggs40c07752013-10-22 13:28:49 -0400133/* Number of outstanding audit_buffers allowed.
134 * When set to zero, this means unlimited. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800135static u32 audit_backlog_limit = 64;
Richard Guy Briggse789e562013-09-12 23:03:51 -0400136#define AUDIT_BACKLOG_WAIT_TIME (60 * HZ)
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800137static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100139/* The identity of the user shutting down the audit system. */
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800140kuid_t audit_sig_uid = INVALID_UID;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100141pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -0400142u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Records can be lost in several ways:
145 0) [suppressed in audit_alloc]
146 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
147 2) out of memory in audit_log_move [alloc_skb]
148 3) suppressed due to audit_rate_limit
149 4) suppressed due to audit_backlog_limit
150*/
Richard Guy Briggs92c82e82017-01-13 03:26:29 -0500151static atomic_t audit_lost = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Amy Griffisf368c07d2006-04-07 16:55:56 -0400153/* Hash for inode-based rules */
154struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
155
Paul Moore8cc96382017-05-02 10:16:05 -0400156static struct kmem_cache *audit_buffer_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Paul Moorec6480202016-11-29 16:53:25 -0500158/* queue msgs to send via kauditd_task */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500159static struct sk_buff_head audit_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500160/* queue msgs due to temporary unicast send problems */
161static struct sk_buff_head audit_retry_queue;
162/* queue msgs waiting for new auditd connection */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500163static struct sk_buff_head audit_hold_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500164
165/* queue servicing thread */
David Woodhouseb7d11252005-05-19 10:56:58 +0100166static struct task_struct *kauditd_task;
167static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500168
169/* waitqueue for callers who are blocked on the audit backlog */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100170static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Eric Parisb0fed402013-05-22 12:54:49 -0400172static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION,
173 .mask = -1,
174 .features = 0,
175 .lock = 0,};
176
Eric Paris21b85c32013-05-23 14:26:00 -0400177static char *audit_feature_names[2] = {
Eric Parisd040e5a2013-05-24 09:18:04 -0400178 "only_unset_loginuid",
Eric Paris21b85c32013-05-23 14:26:00 -0400179 "loginuid_immutable",
Eric Parisb0fed402013-05-22 12:54:49 -0400180};
181
182
Amy Griffisf368c07d2006-04-07 16:55:56 -0400183/* Serialize requests from userspace. */
Al Viro916d7572009-06-24 00:02:38 -0400184DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
187 * audit records. Since printk uses a 1024 byte buffer, this buffer
188 * should be at least that large. */
189#define AUDIT_BUFSIZ 1024
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* The audit_buffer is used when formatting an audit record. The caller
192 * locks briefly to get the record off the freelist or to allocate the
193 * buffer, and locks briefly to send the buffer to the netlink layer or
194 * to place it on a transmit queue. Multiple audit_buffers can be in
195 * use simultaneously. */
196struct audit_buffer {
Chris Wright8fc61152005-05-06 15:54:17 +0100197 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400199 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Eric Parisf09ac9d2008-04-18 10:11:04 -0400202struct audit_reply {
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400203 __u32 portid;
Eric W. Biederman638a0fd2014-02-28 10:49:05 -0800204 struct net *net;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400205 struct sk_buff *skb;
206};
207
Paul Moore5b523302017-03-21 11:26:35 -0400208/**
209 * auditd_test_task - Check to see if a given task is an audit daemon
210 * @task: the task to check
211 *
212 * Description:
213 * Return 1 if the task is a registered audit daemon, 0 otherwise.
214 */
Paul Mooreb6c7c112017-05-02 10:16:05 -0400215int auditd_test_task(struct task_struct *task)
Paul Moore5b523302017-03-21 11:26:35 -0400216{
217 int rc;
218
219 rcu_read_lock();
Paul Mooreb6c7c112017-05-02 10:16:05 -0400220 rc = (auditd_conn.pid && auditd_conn.pid == task_tgid(task) ? 1 : 0);
Paul Moore5b523302017-03-21 11:26:35 -0400221 rcu_read_unlock();
222
223 return rc;
224}
225
226/**
Paul Mooreb6c7c112017-05-02 10:16:05 -0400227 * auditd_pid_vnr - Return the auditd PID relative to the namespace
228 * @auditd: the auditd connection
229 *
230 * Description:
231 * Returns the PID in relation to the namespace, 0 on failure. This function
232 * takes the RCU read lock internally, but if the caller needs to protect the
233 * auditd_connection pointer it should take the RCU read lock as well.
234 */
235static pid_t auditd_pid_vnr(const struct auditd_connection *auditd)
236{
237 pid_t pid;
238
239 rcu_read_lock();
240 if (!auditd || !auditd->pid)
241 pid = 0;
242 else
243 pid = pid_vnr(auditd->pid);
244 rcu_read_unlock();
245
246 return pid;
247}
248
249/**
Paul Moore5b523302017-03-21 11:26:35 -0400250 * audit_get_sk - Return the audit socket for the given network namespace
251 * @net: the destination network namespace
252 *
253 * Description:
254 * Returns the sock pointer if valid, NULL otherwise. The caller must ensure
255 * that a reference is held for the network namespace while the sock is in use.
256 */
257static struct sock *audit_get_sk(const struct net *net)
258{
259 struct audit_net *aunet;
260
261 if (!net)
262 return NULL;
263
264 aunet = net_generic(net, audit_net_id);
265 return aunet->sk;
266}
267
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000268void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Joe Perchesd957f7b2014-01-14 10:33:12 -0800270 switch (audit_failure) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 case AUDIT_FAIL_SILENT:
272 break;
273 case AUDIT_FAIL_PRINTK:
Eric Paris320f1b12008-01-23 22:55:05 -0500274 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800275 pr_err("%s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 case AUDIT_FAIL_PANIC:
Paul Moore5b523302017-03-21 11:26:35 -0400278 panic("audit: %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
280 }
281}
282
283static inline int audit_rate_check(void)
284{
285 static unsigned long last_check = 0;
286 static int messages = 0;
287 static DEFINE_SPINLOCK(lock);
288 unsigned long flags;
289 unsigned long now;
290 unsigned long elapsed;
291 int retval = 0;
292
293 if (!audit_rate_limit) return 1;
294
295 spin_lock_irqsave(&lock, flags);
296 if (++messages < audit_rate_limit) {
297 retval = 1;
298 } else {
299 now = jiffies;
300 elapsed = now - last_check;
301 if (elapsed > HZ) {
302 last_check = now;
303 messages = 0;
304 retval = 1;
305 }
306 }
307 spin_unlock_irqrestore(&lock, flags);
308
309 return retval;
310}
311
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700312/**
313 * audit_log_lost - conditionally log lost audit message event
314 * @message: the message stating reason for lost audit message
315 *
316 * Emit at least 1 message per second, even if audit_rate_check is
317 * throttling.
318 * Always increment the lost messages counter.
319*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320void audit_log_lost(const char *message)
321{
322 static unsigned long last_msg = 0;
323 static DEFINE_SPINLOCK(lock);
324 unsigned long flags;
325 unsigned long now;
326 int print;
327
328 atomic_inc(&audit_lost);
329
330 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
331
332 if (!print) {
333 spin_lock_irqsave(&lock, flags);
334 now = jiffies;
335 if (now - last_msg > HZ) {
336 print = 1;
337 last_msg = now;
338 }
339 spin_unlock_irqrestore(&lock, flags);
340 }
341
342 if (print) {
Eric Paris320f1b12008-01-23 22:55:05 -0500343 if (printk_ratelimit())
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800344 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
Eric Paris320f1b12008-01-23 22:55:05 -0500345 atomic_read(&audit_lost),
346 audit_rate_limit,
347 audit_backlog_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 audit_panic(message);
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800352static int audit_log_config_change(char *function_name, u32 new, u32 old,
Eric Paris25323862008-04-18 10:09:25 -0400353 int allow_changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Eric Paris1a6b9f22008-01-07 17:09:31 -0500355 struct audit_buffer *ab;
356 int rc = 0;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500357
Eric Paris1a6b9f22008-01-07 17:09:31 -0500358 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
Kees Cook0644ec02013-01-11 14:32:07 -0800359 if (unlikely(!ab))
360 return rc;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800361 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
Eric Paris4d3fb702013-04-30 09:53:34 -0400362 audit_log_session_info(ab);
Eric Parisb122c372013-04-19 15:00:33 -0400363 rc = audit_log_task_context(ab);
364 if (rc)
365 allow_changes = 0; /* Something weird, deny request */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500366 audit_log_format(ab, " res=%d", allow_changes);
367 audit_log_end(ab);
368 return rc;
369}
370
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800371static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500372{
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800373 int allow_changes, rc = 0;
374 u32 old = *to_change;
Eric Paris1a6b9f22008-01-07 17:09:31 -0500375
376 /* check if we are locked */
377 if (audit_enabled == AUDIT_LOCKED)
378 allow_changes = 0;
379 else
380 allow_changes = 1;
381
382 if (audit_enabled != AUDIT_OFF) {
Eric Parisdc9eb692013-04-19 13:23:09 -0400383 rc = audit_log_config_change(function_name, new, old, allow_changes);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500384 if (rc)
385 allow_changes = 0;
386 }
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500387
388 /* If we are allowed, make the change */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500389 if (allow_changes == 1)
390 *to_change = new;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500391 /* Not allowed, update reason */
392 else if (rc == 0)
393 rc = -EPERM;
394 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800397static int audit_set_rate_limit(u32 limit)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500398{
Eric Parisdc9eb692013-04-19 13:23:09 -0400399 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500400}
401
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800402static int audit_set_backlog_limit(u32 limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Eric Parisdc9eb692013-04-19 13:23:09 -0400404 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800407static int audit_set_backlog_wait_time(u32 timeout)
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400408{
409 return audit_do_config_change("audit_backlog_wait_time",
Paul Moore31975422016-11-29 16:53:25 -0500410 &audit_backlog_wait_time, timeout);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400411}
412
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800413static int audit_set_enabled(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Eric Parisb593d382008-01-08 17:38:31 -0500415 int rc;
Pranith Kumar724e7bf2015-03-11 14:08:19 -0400416 if (state > AUDIT_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500418
Eric Parisdc9eb692013-04-19 13:23:09 -0400419 rc = audit_do_config_change("audit_enabled", &audit_enabled, state);
Eric Parisb593d382008-01-08 17:38:31 -0500420 if (!rc)
421 audit_ever_enabled |= !!state;
422
423 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800426static int audit_set_failure(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (state != AUDIT_FAIL_SILENT
429 && state != AUDIT_FAIL_PRINTK
430 && state != AUDIT_FAIL_PANIC)
431 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500432
Eric Parisdc9eb692013-04-19 13:23:09 -0400433 return audit_do_config_change("audit_failure", &audit_failure, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Paul Moore5b523302017-03-21 11:26:35 -0400436/**
437 * auditd_set - Set/Reset the auditd connection state
438 * @pid: auditd PID
439 * @portid: auditd netlink portid
440 * @net: auditd network namespace pointer
441 *
442 * Description:
443 * This function will obtain and drop network namespace references as
444 * necessary.
445 */
Paul Mooreb6c7c112017-05-02 10:16:05 -0400446static void auditd_set(struct pid *pid, u32 portid, struct net *net)
Paul Moore5b523302017-03-21 11:26:35 -0400447{
448 unsigned long flags;
449
450 spin_lock_irqsave(&auditd_conn.lock, flags);
Paul Mooreb6c7c112017-05-02 10:16:05 -0400451 if (auditd_conn.pid)
452 put_pid(auditd_conn.pid);
453 if (pid)
454 auditd_conn.pid = get_pid(pid);
455 else
456 auditd_conn.pid = NULL;
Paul Moore5b523302017-03-21 11:26:35 -0400457 auditd_conn.portid = portid;
458 if (auditd_conn.net)
459 put_net(auditd_conn.net);
460 if (net)
461 auditd_conn.net = get_net(net);
462 else
463 auditd_conn.net = NULL;
464 spin_unlock_irqrestore(&auditd_conn.lock, flags);
465}
466
467/**
Paul Moore5b523302017-03-21 11:26:35 -0400468 * kauditd_print_skb - Print the audit record to the ring buffer
469 * @skb: audit record
470 *
471 * Whatever the reason, this packet may not make it to the auditd connection
472 * so write it via printk so the information isn't completely lost.
Eric Paris038cbcf2009-06-11 14:31:35 -0400473 */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500474static void kauditd_printk_skb(struct sk_buff *skb)
Eric Paris038cbcf2009-06-11 14:31:35 -0400475{
476 struct nlmsghdr *nlh = nlmsg_hdr(skb);
David S. Millerc64e66c2012-06-26 21:45:21 -0700477 char *data = nlmsg_data(nlh);
Eric Paris038cbcf2009-06-11 14:31:35 -0400478
Paul Moore5b523302017-03-21 11:26:35 -0400479 if (nlh->nlmsg_type != AUDIT_EOE && printk_ratelimit())
480 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
481}
482
483/**
484 * kauditd_rehold_skb - Handle a audit record send failure in the hold queue
485 * @skb: audit record
486 *
487 * Description:
488 * This should only be used by the kauditd_thread when it fails to flush the
489 * hold queue.
490 */
491static void kauditd_rehold_skb(struct sk_buff *skb)
492{
493 /* put the record back in the queue at the same place */
494 skb_queue_head(&audit_hold_queue, skb);
Eric Paris038cbcf2009-06-11 14:31:35 -0400495}
496
Paul Moorec6480202016-11-29 16:53:25 -0500497/**
498 * kauditd_hold_skb - Queue an audit record, waiting for auditd
499 * @skb: audit record
500 *
501 * Description:
502 * Queue the audit record, waiting for an instance of auditd. When this
503 * function is called we haven't given up yet on sending the record, but things
504 * are not looking good. The first thing we want to do is try to write the
505 * record via printk and then see if we want to try and hold on to the record
506 * and queue it, if we have room. If we want to hold on to the record, but we
507 * don't have room, record a record lost message.
508 */
509static void kauditd_hold_skb(struct sk_buff *skb)
Eric Parisf3d357b2008-04-18 10:02:28 -0400510{
Paul Moorec6480202016-11-29 16:53:25 -0500511 /* at this point it is uncertain if we will ever send this to auditd so
512 * try to send the message via printk before we go any further */
513 kauditd_printk_skb(skb);
Richard Guy Briggs32a1dba2015-11-04 08:23:50 -0500514
Paul Moorec6480202016-11-29 16:53:25 -0500515 /* can we just silently drop the message? */
516 if (!audit_default) {
517 kfree_skb(skb);
518 return;
519 }
520
521 /* if we have room, queue the message */
522 if (!audit_backlog_limit ||
523 skb_queue_len(&audit_hold_queue) < audit_backlog_limit) {
524 skb_queue_tail(&audit_hold_queue, skb);
525 return;
526 }
527
528 /* we have no other options - drop the message */
529 audit_log_lost("kauditd hold queue overflow");
530 kfree_skb(skb);
531}
532
533/**
534 * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd
535 * @skb: audit record
536 *
537 * Description:
538 * Not as serious as kauditd_hold_skb() as we still have a connected auditd,
539 * but for some reason we are having problems sending it audit records so
540 * queue the given record and attempt to resend.
541 */
542static void kauditd_retry_skb(struct sk_buff *skb)
543{
544 /* NOTE: because records should only live in the retry queue for a
545 * short period of time, before either being sent or moved to the hold
546 * queue, we don't currently enforce a limit on this queue */
547 skb_queue_tail(&audit_retry_queue, skb);
548}
549
550/**
Paul Moore264d5092017-04-10 11:16:59 -0400551 * auditd_reset - Disconnect the auditd connection
552 *
553 * Description:
554 * Break the auditd/kauditd connection and move all the queued records into the
555 * hold queue in case auditd reconnects.
556 */
557static void auditd_reset(void)
558{
559 struct sk_buff *skb;
560
561 /* if it isn't already broken, break the connection */
562 rcu_read_lock();
563 if (auditd_conn.pid)
564 auditd_set(0, 0, NULL);
565 rcu_read_unlock();
566
567 /* flush all of the main and retry queues to the hold queue */
568 while ((skb = skb_dequeue(&audit_retry_queue)))
569 kauditd_hold_skb(skb);
570 while ((skb = skb_dequeue(&audit_queue)))
571 kauditd_hold_skb(skb);
572}
573
574/**
Paul Moore5b523302017-03-21 11:26:35 -0400575 * auditd_send_unicast_skb - Send a record via unicast to auditd
576 * @skb: audit record
Paul Moorec6480202016-11-29 16:53:25 -0500577 *
578 * Description:
Paul Moore5b523302017-03-21 11:26:35 -0400579 * Send a skb to the audit daemon, returns positive/zero values on success and
580 * negative values on failure; in all cases the skb will be consumed by this
581 * function. If the send results in -ECONNREFUSED the connection with auditd
582 * will be reset. This function may sleep so callers should not hold any locks
583 * where this would cause a problem.
Paul Moorec6480202016-11-29 16:53:25 -0500584 */
Paul Moore5b523302017-03-21 11:26:35 -0400585static int auditd_send_unicast_skb(struct sk_buff *skb)
Paul Moorec6480202016-11-29 16:53:25 -0500586{
Paul Moore5b523302017-03-21 11:26:35 -0400587 int rc;
588 u32 portid;
589 struct net *net;
590 struct sock *sk;
Paul Moorec6480202016-11-29 16:53:25 -0500591
Paul Moore5b523302017-03-21 11:26:35 -0400592 /* NOTE: we can't call netlink_unicast while in the RCU section so
593 * take a reference to the network namespace and grab local
594 * copies of the namespace, the sock, and the portid; the
595 * namespace and sock aren't going to go away while we hold a
596 * reference and if the portid does become invalid after the RCU
597 * section netlink_unicast() should safely return an error */
598
599 rcu_read_lock();
600 if (!auditd_conn.pid) {
601 rcu_read_unlock();
602 rc = -ECONNREFUSED;
603 goto err;
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500604 }
Paul Moore5b523302017-03-21 11:26:35 -0400605 net = auditd_conn.net;
606 get_net(net);
607 sk = audit_get_sk(net);
608 portid = auditd_conn.portid;
609 rcu_read_unlock();
Paul Moorec6480202016-11-29 16:53:25 -0500610
Paul Moore5b523302017-03-21 11:26:35 -0400611 rc = netlink_unicast(sk, skb, portid, 0);
612 put_net(net);
613 if (rc < 0)
614 goto err;
615
616 return rc;
617
618err:
619 if (rc == -ECONNREFUSED)
620 auditd_reset();
621 return rc;
Paul Moorec6480202016-11-29 16:53:25 -0500622}
623
624/**
Paul Moore5b523302017-03-21 11:26:35 -0400625 * kauditd_send_queue - Helper for kauditd_thread to flush skb queues
626 * @sk: the sending sock
627 * @portid: the netlink destination
628 * @queue: the skb queue to process
629 * @retry_limit: limit on number of netlink unicast failures
630 * @skb_hook: per-skb hook for additional processing
631 * @err_hook: hook called if the skb fails the netlink unicast send
632 *
633 * Description:
634 * Run through the given queue and attempt to send the audit records to auditd,
635 * returns zero on success, negative values on failure. It is up to the caller
636 * to ensure that the @sk is valid for the duration of this function.
637 *
Paul Moorec6480202016-11-29 16:53:25 -0500638 */
Paul Moore5b523302017-03-21 11:26:35 -0400639static int kauditd_send_queue(struct sock *sk, u32 portid,
640 struct sk_buff_head *queue,
641 unsigned int retry_limit,
642 void (*skb_hook)(struct sk_buff *skb),
643 void (*err_hook)(struct sk_buff *skb))
Paul Moorec6480202016-11-29 16:53:25 -0500644{
Paul Moore5b523302017-03-21 11:26:35 -0400645 int rc = 0;
646 struct sk_buff *skb;
647 static unsigned int failed = 0;
Paul Moorec6480202016-11-29 16:53:25 -0500648
Paul Moore5b523302017-03-21 11:26:35 -0400649 /* NOTE: kauditd_thread takes care of all our locking, we just use
650 * the netlink info passed to us (e.g. sk and portid) */
Paul Moore6c54e782016-11-29 16:53:26 -0500651
Paul Moore5b523302017-03-21 11:26:35 -0400652 while ((skb = skb_dequeue(queue))) {
653 /* call the skb_hook for each skb we touch */
654 if (skb_hook)
655 (*skb_hook)(skb);
656
657 /* can we send to anyone via unicast? */
658 if (!sk) {
659 if (err_hook)
660 (*err_hook)(skb);
661 continue;
662 }
663
664 /* grab an extra skb reference in case of error */
665 skb_get(skb);
666 rc = netlink_unicast(sk, skb, portid, 0);
667 if (rc < 0) {
668 /* fatal failure for our queue flush attempt? */
669 if (++failed >= retry_limit ||
670 rc == -ECONNREFUSED || rc == -EPERM) {
671 /* yes - error processing for the queue */
672 sk = NULL;
673 if (err_hook)
674 (*err_hook)(skb);
675 if (!skb_hook)
676 goto out;
677 /* keep processing with the skb_hook */
678 continue;
679 } else
680 /* no - requeue to preserve ordering */
681 skb_queue_head(queue, skb);
682 } else {
683 /* it worked - drop the extra reference and continue */
684 consume_skb(skb);
685 failed = 0;
686 }
Paul Moorec6480202016-11-29 16:53:25 -0500687 }
688
Paul Moore5b523302017-03-21 11:26:35 -0400689out:
690 return (rc >= 0 ? 0 : rc);
Eric Parisf3d357b2008-04-18 10:02:28 -0400691}
692
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500693/*
Paul Moorec6480202016-11-29 16:53:25 -0500694 * kauditd_send_multicast_skb - Send a record to any multicast listeners
695 * @skb: audit record
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400696 *
Paul Moorec6480202016-11-29 16:53:25 -0500697 * Description:
Paul Moore5b523302017-03-21 11:26:35 -0400698 * Write a multicast message to anyone listening in the initial network
699 * namespace. This function doesn't consume an skb as might be expected since
700 * it has to copy it anyways.
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400701 */
Paul Moorec6480202016-11-29 16:53:25 -0500702static void kauditd_send_multicast_skb(struct sk_buff *skb)
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400703{
Paul Moorec6480202016-11-29 16:53:25 -0500704 struct sk_buff *copy;
Paul Moore5b523302017-03-21 11:26:35 -0400705 struct sock *sock = audit_get_sk(&init_net);
Paul Moorec6480202016-11-29 16:53:25 -0500706 struct nlmsghdr *nlh;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400707
Paul Moore5b523302017-03-21 11:26:35 -0400708 /* NOTE: we are not taking an additional reference for init_net since
709 * we don't have to worry about it going away */
710
Richard Guy Briggs7f74ecd2014-04-22 21:31:58 -0400711 if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG))
712 return;
713
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400714 /*
715 * The seemingly wasteful skb_copy() rather than bumping the refcount
716 * using skb_get() is necessary because non-standard mods are made to
717 * the skb by the original kaudit unicast socket send routine. The
718 * existing auditd daemon assumes this breakage. Fixing this would
719 * require co-ordinating a change in the established protocol between
720 * the kaudit kernel subsystem and the auditd userspace code. There is
721 * no reason for new multicast clients to continue with this
722 * non-compliance.
723 */
Paul Moorec6480202016-11-29 16:53:25 -0500724 copy = skb_copy(skb, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400725 if (!copy)
726 return;
Paul Moorec6480202016-11-29 16:53:25 -0500727 nlh = nlmsg_hdr(copy);
728 nlh->nlmsg_len = skb->len;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400729
Paul Moorec6480202016-11-29 16:53:25 -0500730 nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400731}
732
Paul Moorec6480202016-11-29 16:53:25 -0500733/**
Paul Moore5b523302017-03-21 11:26:35 -0400734 * kauditd_thread - Worker thread to send audit records to userspace
735 * @dummy: unused
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500736 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800737static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100738{
Paul Moorec6480202016-11-29 16:53:25 -0500739 int rc;
Paul Moore5b523302017-03-21 11:26:35 -0400740 u32 portid = 0;
741 struct net *net = NULL;
742 struct sock *sk = NULL;
Paul Moore4aa838722016-11-29 16:53:24 -0500743
Paul Moorec6480202016-11-29 16:53:25 -0500744#define UNICAST_RETRIES 5
Paul Moorec6480202016-11-29 16:53:25 -0500745
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700746 set_freezable();
Andrew Morton4899b8b2006-10-06 00:43:48 -0700747 while (!kthread_should_stop()) {
Paul Moore5b523302017-03-21 11:26:35 -0400748 /* NOTE: see the lock comments in auditd_send_unicast_skb() */
749 rcu_read_lock();
750 if (!auditd_conn.pid) {
751 rcu_read_unlock();
752 goto main_queue;
753 }
754 net = auditd_conn.net;
755 get_net(net);
756 sk = audit_get_sk(net);
757 portid = auditd_conn.portid;
758 rcu_read_unlock();
Eric Parisf3d357b2008-04-18 10:02:28 -0400759
Paul Moorec6480202016-11-29 16:53:25 -0500760 /* attempt to flush the hold queue */
Paul Moore5b523302017-03-21 11:26:35 -0400761 rc = kauditd_send_queue(sk, portid,
762 &audit_hold_queue, UNICAST_RETRIES,
763 NULL, kauditd_rehold_skb);
764 if (rc < 0) {
765 sk = NULL;
Paul Moore264d5092017-04-10 11:16:59 -0400766 auditd_reset();
Paul Moore5b523302017-03-21 11:26:35 -0400767 goto main_queue;
David Woodhouseb7d11252005-05-19 10:56:58 +0100768 }
Richard Guy Briggs3320c512013-01-24 13:15:11 -0500769
Paul Moorec6480202016-11-29 16:53:25 -0500770 /* attempt to flush the retry queue */
Paul Moore5b523302017-03-21 11:26:35 -0400771 rc = kauditd_send_queue(sk, portid,
772 &audit_retry_queue, UNICAST_RETRIES,
773 NULL, kauditd_hold_skb);
774 if (rc < 0) {
775 sk = NULL;
Paul Moore264d5092017-04-10 11:16:59 -0400776 auditd_reset();
Paul Moore5b523302017-03-21 11:26:35 -0400777 goto main_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500778 }
779
Paul Moore5b523302017-03-21 11:26:35 -0400780main_queue:
781 /* process the main queue - do the multicast send and attempt
782 * unicast, dump failed record sends to the retry queue; if
783 * sk == NULL due to previous failures we will just do the
784 * multicast send and move the record to the retry queue */
Paul Moore264d5092017-04-10 11:16:59 -0400785 rc = kauditd_send_queue(sk, portid, &audit_queue, 1,
786 kauditd_send_multicast_skb,
787 kauditd_retry_skb);
788 if (sk == NULL || rc < 0)
789 auditd_reset();
790 sk = NULL;
Paul Moore4aa838722016-11-29 16:53:24 -0500791
Paul Moore5b523302017-03-21 11:26:35 -0400792 /* drop our netns reference, no auditd sends past this line */
793 if (net) {
794 put_net(net);
795 net = NULL;
Richard Guy Briggs3320c512013-01-24 13:15:11 -0500796 }
Paul Moore5b523302017-03-21 11:26:35 -0400797
798 /* we have processed all the queues so wake everyone */
799 wake_up(&audit_backlog_wait);
800
801 /* NOTE: we want to wake up if there is anything on the queue,
802 * regardless of if an auditd is connected, as we need to
803 * do the multicast send and rotate records from the
804 * main queue to the retry/hold queues */
805 wait_event_freezable(kauditd_wait,
806 (skb_queue_len(&audit_queue) ? 1 : 0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100807 }
Paul Moorec6480202016-11-29 16:53:25 -0500808
Andrew Morton4899b8b2006-10-06 00:43:48 -0700809 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100810}
811
Al Viro9044e6b2006-05-22 01:09:24 -0400812int audit_send_list(void *_dest)
813{
814 struct audit_netlink_list *dest = _dest;
Al Viro9044e6b2006-05-22 01:09:24 -0400815 struct sk_buff *skb;
Paul Moore5b523302017-03-21 11:26:35 -0400816 struct sock *sk = audit_get_sk(dest->net);
Al Viro9044e6b2006-05-22 01:09:24 -0400817
818 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400819 mutex_lock(&audit_cmd_mutex);
820 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400821
822 while ((skb = __skb_dequeue(&dest->q)) != NULL)
Paul Moore5b523302017-03-21 11:26:35 -0400823 netlink_unicast(sk, skb, dest->portid, 0);
Al Viro9044e6b2006-05-22 01:09:24 -0400824
Paul Moore5b523302017-03-21 11:26:35 -0400825 put_net(dest->net);
Al Viro9044e6b2006-05-22 01:09:24 -0400826 kfree(dest);
827
828 return 0;
829}
830
Paul Moore45a06422017-05-02 10:16:05 -0400831struct sk_buff *audit_make_reply(int seq, int type, int done,
Stephen Hemmingerb8800aa2010-10-20 17:23:50 -0700832 int multi, const void *payload, int size)
Al Viro9044e6b2006-05-22 01:09:24 -0400833{
834 struct sk_buff *skb;
835 struct nlmsghdr *nlh;
Al Viro9044e6b2006-05-22 01:09:24 -0400836 void *data;
837 int flags = multi ? NLM_F_MULTI : 0;
838 int t = done ? NLMSG_DONE : type;
839
Eric Parisee080e62009-06-11 14:31:35 -0400840 skb = nlmsg_new(size, GFP_KERNEL);
Al Viro9044e6b2006-05-22 01:09:24 -0400841 if (!skb)
842 return NULL;
843
Paul Moore45a06422017-05-02 10:16:05 -0400844 nlh = nlmsg_put(skb, 0, seq, t, size, flags);
David S. Millerc64e66c2012-06-26 21:45:21 -0700845 if (!nlh)
846 goto out_kfree_skb;
847 data = nlmsg_data(nlh);
Al Viro9044e6b2006-05-22 01:09:24 -0400848 memcpy(data, payload, size);
849 return skb;
850
David S. Millerc64e66c2012-06-26 21:45:21 -0700851out_kfree_skb:
852 kfree_skb(skb);
Al Viro9044e6b2006-05-22 01:09:24 -0400853 return NULL;
854}
855
Eric Parisf09ac9d2008-04-18 10:11:04 -0400856static int audit_send_reply_thread(void *arg)
857{
858 struct audit_reply *reply = (struct audit_reply *)arg;
Paul Moore5b523302017-03-21 11:26:35 -0400859 struct sock *sk = audit_get_sk(reply->net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400860
861 mutex_lock(&audit_cmd_mutex);
862 mutex_unlock(&audit_cmd_mutex);
863
864 /* Ignore failure. It'll only happen if the sender goes away,
865 because our timeout is set to infinite. */
Paul Moore5b523302017-03-21 11:26:35 -0400866 netlink_unicast(sk, reply->skb, reply->portid, 0);
867 put_net(reply->net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400868 kfree(reply);
869 return 0;
870}
Paul Moorec6480202016-11-29 16:53:25 -0500871
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700872/**
873 * audit_send_reply - send an audit reply message via netlink
Eric W. Biedermand211f1772014-03-08 15:31:54 -0800874 * @request_skb: skb of request we are replying to (used to target the reply)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700875 * @seq: sequence number
876 * @type: audit message type
877 * @done: done (last) flag
878 * @multi: multi-part message flag
879 * @payload: payload data
880 * @size: payload size
881 *
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400882 * Allocates an skb, builds the netlink message, and sends it to the port id.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700883 * No failure notifications.
884 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800885static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400886 int multi, const void *payload, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800888 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400889 struct sk_buff *skb;
890 struct task_struct *tsk;
891 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
892 GFP_KERNEL);
893
894 if (!reply)
895 return;
896
Paul Moore45a06422017-05-02 10:16:05 -0400897 skb = audit_make_reply(seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!skb)
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700899 goto out;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400900
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800901 reply->net = get_net(net);
Paul Moore45a06422017-05-02 10:16:05 -0400902 reply->portid = NETLINK_CB(request_skb).portid;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400903 reply->skb = skb;
904
905 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700906 if (!IS_ERR(tsk))
907 return;
908 kfree_skb(skb);
909out:
910 kfree(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
915 * control messages.
916 */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700917static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int err = 0;
920
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400921 /* Only support initial user namespace for now. */
Eric Parisaa4af832014-03-30 19:07:54 -0400922 /*
923 * We return ECONNREFUSED because it tricks userspace into thinking
924 * that audit was not configured into the kernel. Lots of users
925 * configure their PAM stack (because that's what the distro does)
926 * to reject login if unable to send messages to audit. If we return
927 * ECONNREFUSED the PAM stack thinks the kernel does not have audit
928 * configured in and will let login proceed. If we return EPERM
929 * userspace will reject all logins. This should be removed when we
930 * support non init namespaces!!
931 */
Linus Torvalds0b747172014-04-12 12:38:53 -0700932 if (current_user_ns() != &init_user_ns)
Eric Parisaa4af832014-03-30 19:07:54 -0400933 return -ECONNREFUSED;
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 switch (msg_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 case AUDIT_ADD:
938 case AUDIT_DEL:
Eric Paris18900902013-04-18 19:16:36 -0400939 return -EOPNOTSUPP;
940 case AUDIT_GET:
941 case AUDIT_SET:
Eric Parisb0fed402013-05-22 12:54:49 -0400942 case AUDIT_GET_FEATURE:
943 case AUDIT_SET_FEATURE:
Eric Paris18900902013-04-18 19:16:36 -0400944 case AUDIT_LIST_RULES:
945 case AUDIT_ADD_RULE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500946 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100947 case AUDIT_SIGNAL_INFO:
Miloslav Trmac522ed772007-07-15 23:40:56 -0700948 case AUDIT_TTY_GET:
949 case AUDIT_TTY_SET:
Al Viro74c3cbe2007-07-22 08:04:18 -0400950 case AUDIT_TRIM:
951 case AUDIT_MAKE_EQUIV:
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400952 /* Only support auditd and auditctl in initial pid namespace
953 * for now. */
Ameen Ali5985de62015-02-23 15:38:00 -0500954 if (task_active_pid_ns(current) != &init_pid_ns)
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400955 return -EPERM;
956
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700957 if (!netlink_capable(skb, CAP_AUDIT_CONTROL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 err = -EPERM;
959 break;
Steve Grubb05474102005-05-21 00:18:37 +0100960 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -0700961 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
962 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
Eric W. Biederman90f62cf32014-04-23 14:29:27 -0700963 if (!netlink_capable(skb, CAP_AUDIT_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 err = -EPERM;
965 break;
966 default: /* bad msg */
967 err = -EINVAL;
968 }
969
970 return err;
971}
972
Paul Moore233a6862015-11-04 08:23:52 -0500973static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type)
Eric Paris50397bd2008-01-07 18:14:19 -0500974{
Eric Parisdc9eb692013-04-19 13:23:09 -0400975 uid_t uid = from_kuid(&init_user_ns, current_uid());
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500976 pid_t pid = task_tgid_nr(current);
Eric Paris50397bd2008-01-07 18:14:19 -0500977
Tyler Hicks0868a5e2013-07-25 18:02:55 -0700978 if (!audit_enabled && msg_type != AUDIT_USER_AVC) {
Eric Paris50397bd2008-01-07 18:14:19 -0500979 *ab = NULL;
Paul Moore233a6862015-11-04 08:23:52 -0500980 return;
Eric Paris50397bd2008-01-07 18:14:19 -0500981 }
982
983 *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
Kees Cook0644ec02013-01-11 14:32:07 -0800984 if (unlikely(!*ab))
Paul Moore233a6862015-11-04 08:23:52 -0500985 return;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500986 audit_log_format(*ab, "pid=%d uid=%u", pid, uid);
Eric Paris4d3fb702013-04-30 09:53:34 -0400987 audit_log_session_info(*ab);
Eric Parisb122c372013-04-19 15:00:33 -0400988 audit_log_task_context(*ab);
Eric Paris50397bd2008-01-07 18:14:19 -0500989}
990
Eric Parisb0fed402013-05-22 12:54:49 -0400991int is_audit_feature_set(int i)
992{
993 return af.features & AUDIT_FEATURE_TO_MASK(i);
994}
995
996
997static int audit_get_feature(struct sk_buff *skb)
998{
999 u32 seq;
1000
1001 seq = nlmsg_hdr(skb)->nlmsg_seq;
1002
Richard Guy Briggs9ef91512014-08-24 20:37:52 -04001003 audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af));
Eric Parisb0fed402013-05-22 12:54:49 -04001004
1005 return 0;
1006}
1007
1008static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature,
1009 u32 old_lock, u32 new_lock, int res)
1010{
1011 struct audit_buffer *ab;
1012
Gao fengb6c50fe2013-11-01 19:34:43 +08001013 if (audit_enabled == AUDIT_OFF)
1014 return;
1015
Eric Parisb0fed402013-05-22 12:54:49 -04001016 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
Richard Guy Briggsad2ac262014-01-07 13:08:41 -05001017 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -04001018 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 -04001019 audit_feature_names[which], !!old_feature, !!new_feature,
1020 !!old_lock, !!new_lock, res);
1021 audit_log_end(ab);
1022}
1023
1024static int audit_set_feature(struct sk_buff *skb)
1025{
1026 struct audit_features *uaf;
1027 int i;
1028
Fabian Frederick6eed9b22014-06-03 22:05:10 +02001029 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -04001030 uaf = nlmsg_data(nlmsg_hdr(skb));
1031
1032 /* if there is ever a version 2 we should handle that here */
1033
1034 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1035 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1036 u32 old_feature, new_feature, old_lock, new_lock;
1037
1038 /* if we are not changing this feature, move along */
1039 if (!(feature & uaf->mask))
1040 continue;
1041
1042 old_feature = af.features & feature;
1043 new_feature = uaf->features & feature;
1044 new_lock = (uaf->lock | af.lock) & feature;
1045 old_lock = af.lock & feature;
1046
1047 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +08001048 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -04001049 audit_log_feature_change(i, old_feature, new_feature,
1050 old_lock, new_lock, 0);
1051 return -EPERM;
1052 }
1053 }
1054 /* nothing invalid, do the changes */
1055 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
1056 u32 feature = AUDIT_FEATURE_TO_MASK(i);
1057 u32 old_feature, new_feature, old_lock, new_lock;
1058
1059 /* if we are not changing this feature, move along */
1060 if (!(feature & uaf->mask))
1061 continue;
1062
1063 old_feature = af.features & feature;
1064 new_feature = uaf->features & feature;
1065 old_lock = af.lock & feature;
1066 new_lock = (uaf->lock | af.lock) & feature;
1067
1068 if (new_feature != old_feature)
1069 audit_log_feature_change(i, old_feature, new_feature,
1070 old_lock, new_lock, 1);
1071
1072 if (new_feature)
1073 af.features |= feature;
1074 else
1075 af.features &= ~feature;
1076 af.lock |= new_lock;
1077 }
1078
1079 return 0;
1080}
1081
Paul Mooreb6c7c112017-05-02 10:16:05 -04001082static int audit_replace(struct pid *pid)
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001083{
Paul Mooreb6c7c112017-05-02 10:16:05 -04001084 pid_t pvnr;
Paul Moore5b523302017-03-21 11:26:35 -04001085 struct sk_buff *skb;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001086
Paul Mooreb6c7c112017-05-02 10:16:05 -04001087 pvnr = pid_vnr(pid);
1088 skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pvnr, sizeof(pvnr));
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001089 if (!skb)
1090 return -ENOMEM;
Paul Moore5b523302017-03-21 11:26:35 -04001091 return auditd_send_unicast_skb(skb);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1095{
Eric Parisdc9eb692013-04-19 13:23:09 -04001096 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 int err;
Steve Grubbc0404992005-05-13 18:17:42 +01001099 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -04001101 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -05001102 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -04001103 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001105 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (err)
1107 return err;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -07001110 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001113 case AUDIT_GET: {
1114 struct audit_status s;
1115 memset(&s, 0, sizeof(s));
1116 s.enabled = audit_enabled;
1117 s.failure = audit_failure;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001118 /* NOTE: use pid_vnr() so the PID is relative to the current
1119 * namespace */
1120 s.pid = auditd_pid_vnr(&auditd_conn);
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001121 s.rate_limit = audit_rate_limit;
1122 s.backlog_limit = audit_backlog_limit;
1123 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001124 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -05001125 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Paul Moore31975422016-11-29 16:53:25 -05001126 s.backlog_wait_time = audit_backlog_wait_time;
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001127 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001129 }
1130 case AUDIT_SET: {
1131 struct audit_status s;
1132 memset(&s, 0, sizeof(s));
1133 /* guard against past and future API changes */
1134 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
1135 if (s.mask & AUDIT_STATUS_ENABLED) {
1136 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001137 if (err < 0)
1138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001140 if (s.mask & AUDIT_STATUS_FAILURE) {
1141 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001142 if (err < 0)
1143 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001145 if (s.mask & AUDIT_STATUS_PID) {
Paul Mooreb6c7c112017-05-02 10:16:05 -04001146 /* NOTE: we are using the vnr PID functions below
1147 * because the s.pid value is relative to the
1148 * namespace of the caller; at present this
1149 * doesn't matter much since you can really only
1150 * run auditd from the initial pid namespace, but
1151 * something to keep in mind if this changes */
1152 pid_t new_pid = s.pid;
Paul Moore5b523302017-03-21 11:26:35 -04001153 pid_t auditd_pid;
Paul Mooreb6c7c112017-05-02 10:16:05 -04001154 struct pid *req_pid = task_tgid(current);
1155
1156 /* sanity check - PID values must match */
1157 if (new_pid != pid_vnr(req_pid))
1158 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001159
Paul Moore5b523302017-03-21 11:26:35 -04001160 /* test the auditd connection */
Paul Mooreb6c7c112017-05-02 10:16:05 -04001161 audit_replace(req_pid);
Paul Moore5b523302017-03-21 11:26:35 -04001162
Paul Mooreb6c7c112017-05-02 10:16:05 -04001163 auditd_pid = auditd_pid_vnr(&auditd_conn);
Paul Moore5b523302017-03-21 11:26:35 -04001164 /* only the current auditd can unregister itself */
Paul Mooreb6c7c112017-05-02 10:16:05 -04001165 if ((!new_pid) && (new_pid != auditd_pid)) {
Paul Moore5b523302017-03-21 11:26:35 -04001166 audit_log_config_change("audit_pid", new_pid,
1167 auditd_pid, 0);
Richard Guy Briggs34eab0a2013-06-21 14:47:13 -04001168 return -EACCES;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001169 }
Paul Moore5b523302017-03-21 11:26:35 -04001170 /* replacing a healthy auditd is not allowed */
1171 if (auditd_pid && new_pid) {
Paul Moore5b523302017-03-21 11:26:35 -04001172 audit_log_config_change("audit_pid", new_pid,
1173 auditd_pid, 0);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001174 return -EEXIST;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001175 }
Paul Moore5b523302017-03-21 11:26:35 -04001176
Eric Paris1a6b9f22008-01-07 17:09:31 -05001177 if (audit_enabled != AUDIT_OFF)
Paul Moore5b523302017-03-21 11:26:35 -04001178 audit_log_config_change("audit_pid", new_pid,
1179 auditd_pid, 1);
1180
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001181 if (new_pid) {
Paul Moore5b523302017-03-21 11:26:35 -04001182 /* register a new auditd connection */
Paul Mooreb6c7c112017-05-02 10:16:05 -04001183 auditd_set(req_pid, NETLINK_CB(skb).portid,
Paul Moore5b523302017-03-21 11:26:35 -04001184 sock_net(NETLINK_CB(skb).sk));
1185 /* try to process any backlog */
1186 wake_up_interruptible(&kauditd_wait);
1187 } else
1188 /* unregister the auditd connection */
Paul Moore6c54e782016-11-29 16:53:26 -05001189 auditd_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001191 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1192 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001193 if (err < 0)
1194 return err;
1195 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001196 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001197 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001198 if (err < 0)
1199 return err;
1200 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001201 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1202 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1203 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001204 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001205 return -EINVAL;
1206 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1207 if (err < 0)
1208 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001209 }
Richard Guy Briggs92c82e82017-01-13 03:26:29 -05001210 if (s.mask == AUDIT_STATUS_LOST) {
1211 u32 lost = atomic_xchg(&audit_lost, 0);
1212
1213 audit_log_config_change("lost", 0, lost, 1);
1214 return lost;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001217 }
Eric Parisb0fed402013-05-22 12:54:49 -04001218 case AUDIT_GET_FEATURE:
1219 err = audit_get_feature(skb);
1220 if (err)
1221 return err;
1222 break;
1223 case AUDIT_SET_FEATURE:
1224 err = audit_set_feature(skb);
1225 if (err)
1226 return err;
1227 break;
Steve Grubb05474102005-05-21 00:18:37 +01001228 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001229 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1230 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001231 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1232 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001233
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001234 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001235 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001236 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001237 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001238 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001239 if (err)
1240 break;
1241 }
Eric Parisdc9eb692013-04-19 13:23:09 -04001242 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001243 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001244 audit_log_format(ab, " msg='%.*s'",
1245 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001246 (char *)data);
1247 else {
1248 int size;
1249
Eric Parisf7616102013-04-11 11:25:00 -04001250 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001251 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001252 if (size > 0 &&
1253 ((unsigned char *)data)[size - 1] == '\0')
1254 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001255 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001256 }
Eric Paris50397bd2008-01-07 18:14:19 -05001257 audit_log_end(ab);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001260 case AUDIT_ADD_RULE:
1261 case AUDIT_DEL_RULE:
1262 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1263 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001264 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001265 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1266 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001267 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001268 return -EPERM;
1269 }
Paul Moore45a06422017-05-02 10:16:05 -04001270 err = audit_rule_change(msg_type, seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001272 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001273 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001274 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001275 case AUDIT_TRIM:
1276 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001277 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001278 audit_log_format(ab, " op=trim res=1");
1279 audit_log_end(ab);
1280 break;
1281 case AUDIT_MAKE_EQUIV: {
1282 void *bufp = data;
1283 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001284 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001285 char *old, *new;
1286
1287 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001288 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001289 break;
1290 memcpy(sizes, bufp, 2 * sizeof(u32));
1291 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001292 msglen -= 2 * sizeof(u32);
1293 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001294 if (IS_ERR(old)) {
1295 err = PTR_ERR(old);
1296 break;
1297 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001298 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001299 if (IS_ERR(new)) {
1300 err = PTR_ERR(new);
1301 kfree(old);
1302 break;
1303 }
1304 /* OK, here comes... */
1305 err = audit_tag_tree(old, new);
1306
Eric Parisdc9eb692013-04-19 13:23:09 -04001307 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001308
Al Viro74c3cbe2007-07-22 08:04:18 -04001309 audit_log_format(ab, " op=make_equiv old=");
1310 audit_log_untrustedstring(ab, old);
1311 audit_log_format(ab, " new=");
1312 audit_log_untrustedstring(ab, new);
1313 audit_log_format(ab, " res=%d", !err);
1314 audit_log_end(ab);
1315 kfree(old);
1316 kfree(new);
1317 break;
1318 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001319 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001320 len = 0;
1321 if (audit_sig_sid) {
1322 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1323 if (err)
1324 return err;
1325 }
Al Viroe1396062006-05-25 10:19:47 -04001326 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1327 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001328 if (audit_sig_sid)
1329 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001330 return -ENOMEM;
1331 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001332 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001333 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001334 if (audit_sig_sid) {
1335 memcpy(sig_data->ctx, ctx, len);
1336 security_release_secctx(ctx, len);
1337 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001338 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1339 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001340 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001341 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001342 case AUDIT_TTY_GET: {
1343 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001344 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001345
Peter Hurley2e28d382016-01-09 22:55:33 -08001346 t = READ_ONCE(current->signal->audit_tty);
1347 s.enabled = t & AUDIT_TTY_ENABLE;
1348 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001349
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001350 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001351 break;
1352 }
1353 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001354 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001355 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001356 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001357
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001358 memset(&s, 0, sizeof(s));
1359 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001360 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001361 /* check if new data is valid */
1362 if ((s.enabled != 0 && s.enabled != 1) ||
1363 (s.log_passwd != 0 && s.log_passwd != 1))
1364 err = -EINVAL;
1365
Peter Hurley2e28d382016-01-09 22:55:33 -08001366 if (err)
1367 t = READ_ONCE(current->signal->audit_tty);
1368 else {
1369 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1370 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001371 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001372 old.enabled = t & AUDIT_TTY_ENABLE;
1373 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001374
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001375 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001376 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1377 " old-log_passwd=%d new-log_passwd=%d res=%d",
1378 old.enabled, s.enabled, old.log_passwd,
1379 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001380 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001381 break;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 default:
1384 err = -EINVAL;
1385 break;
1386 }
1387
1388 return err < 0 ? err : 0;
1389}
1390
Paul Moorea9d16202017-05-02 10:16:05 -04001391/**
1392 * audit_receive - receive messages from a netlink control socket
1393 * @skb: the message buffer
1394 *
1395 * Parse the provided skb and deal with any messages that may be present,
1396 * malformed skbs are discarded.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001397 */
Paul Moorea9d16202017-05-02 10:16:05 -04001398static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Eric Parisea7ae602009-06-11 14:31:35 -04001400 struct nlmsghdr *nlh;
1401 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001402 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001403 * if the nlmsg_len was not aligned
1404 */
1405 int len;
1406 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Eric Parisea7ae602009-06-11 14:31:35 -04001408 nlh = nlmsg_hdr(skb);
1409 len = skb->len;
1410
Paul Moorea9d16202017-05-02 10:16:05 -04001411 mutex_lock(&audit_cmd_mutex);
Hong zhi guo94191212013-03-27 06:49:06 +00001412 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001413 err = audit_receive_msg(skb, nlh);
1414 /* if err or if this message says it wants a response */
1415 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 netlink_ack(skb, nlh, err);
Eric Parisea7ae602009-06-11 14:31:35 -04001417
Alexandru Copot2851da52013-03-28 23:31:29 +02001418 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001420 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001423/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001424static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001425{
1426 if (!capable(CAP_AUDIT_READ))
1427 return -EPERM;
1428
1429 return 0;
1430}
1431
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001432static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001434 struct netlink_kernel_cfg cfg = {
1435 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001436 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001437 .flags = NL_CFG_F_NONROOT_RECV,
1438 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001439 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001440
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001441 struct audit_net *aunet = net_generic(net, audit_net_id);
1442
Paul Moore5b523302017-03-21 11:26:35 -04001443 aunet->sk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
1444 if (aunet->sk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001445 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001446 return -ENOMEM;
1447 }
Paul Moore5b523302017-03-21 11:26:35 -04001448 aunet->sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1449
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001450 return 0;
1451}
1452
1453static void __net_exit audit_net_exit(struct net *net)
1454{
1455 struct audit_net *aunet = net_generic(net, audit_net_id);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001456
Paul Moore5b523302017-03-21 11:26:35 -04001457 rcu_read_lock();
1458 if (net == auditd_conn.net)
1459 auditd_reset();
1460 rcu_read_unlock();
1461
1462 netlink_kernel_release(aunet->sk);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001463}
1464
Richard Guy Briggs86268772013-07-16 13:18:45 -04001465static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001466 .init = audit_net_init,
1467 .exit = audit_net_exit,
1468 .id = &audit_net_id,
1469 .size = sizeof(struct audit_net),
1470};
1471
1472/* Initialize audit support at boot time. */
1473static int __init audit_init(void)
1474{
1475 int i;
1476
Eric Parisa3f07112008-11-05 12:47:09 -05001477 if (audit_initialized == AUDIT_DISABLED)
1478 return 0;
1479
Paul Moore8cc96382017-05-02 10:16:05 -04001480 audit_buffer_cache = kmem_cache_create("audit_buffer",
1481 sizeof(struct audit_buffer),
1482 0, SLAB_PANIC, NULL);
1483
Paul Moore5b523302017-03-21 11:26:35 -04001484 memset(&auditd_conn, 0, sizeof(auditd_conn));
1485 spin_lock_init(&auditd_conn.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Paul Mooreaf8b8242016-11-29 16:53:24 -05001487 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001488 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001489 skb_queue_head_init(&audit_hold_queue);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001490
Amy Griffisf368c07d2006-04-07 16:55:56 -04001491 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1492 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001493
Paul Moore5b523302017-03-21 11:26:35 -04001494 pr_info("initializing netlink subsys (%s)\n",
1495 audit_default ? "enabled" : "disabled");
1496 register_pernet_subsys(&audit_net_ops);
1497
1498 audit_initialized = AUDIT_INITIALIZED;
1499 audit_enabled = audit_default;
1500 audit_ever_enabled |= !!audit_default;
1501
Paul Moore6c9255642016-11-29 16:53:23 -05001502 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1503 if (IS_ERR(kauditd_task)) {
1504 int err = PTR_ERR(kauditd_task);
1505 panic("audit: failed to start the kauditd thread (%d)\n", err);
1506 }
1507
Steve Grubb7c397d02016-12-14 15:59:46 -05001508 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL,
1509 "state=initialized audit_enabled=%u res=1",
1510 audit_enabled);
Paul Moore6c9255642016-11-29 16:53:23 -05001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514__initcall(audit_init);
1515
1516/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1517static int __init audit_enable(char *str)
1518{
1519 audit_default = !!simple_strtol(str, NULL, 0);
Eric Parisa3f07112008-11-05 12:47:09 -05001520 if (!audit_default)
1521 audit_initialized = AUDIT_DISABLED;
1522
Joe Perchesd957f7b2014-01-14 10:33:12 -08001523 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001524 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001525
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001526 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528__setup("audit=", audit_enable);
1529
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001530/* Process kernel command-line parameter at boot time.
1531 * audit_backlog_limit=<n> */
1532static int __init audit_backlog_limit_set(char *str)
1533{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001534 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001535
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001536 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001537 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1538 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001539 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001540 return 1;
1541 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001542
1543 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001544 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001545
1546 return 1;
1547}
1548__setup("audit_backlog_limit=", audit_backlog_limit_set);
1549
Chris Wright16e19042005-05-06 15:53:34 +01001550static void audit_buffer_free(struct audit_buffer *ab)
1551{
Chris Wright8fc61152005-05-06 15:54:17 +01001552 if (!ab)
1553 return;
1554
Markus Elfringd865e572016-01-13 09:18:55 -05001555 kfree_skb(ab->skb);
Paul Moore8cc96382017-05-02 10:16:05 -04001556 kmem_cache_free(audit_buffer_cache, ab);
Chris Wright16e19042005-05-06 15:53:34 +01001557}
1558
Paul Moore8cc96382017-05-02 10:16:05 -04001559static struct audit_buffer *audit_buffer_alloc(struct audit_context *ctx,
1560 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001561{
Paul Moore8cc96382017-05-02 10:16:05 -04001562 struct audit_buffer *ab;
Chris Wright16e19042005-05-06 15:53:34 +01001563
Paul Moore8cc96382017-05-02 10:16:05 -04001564 ab = kmem_cache_alloc(audit_buffer_cache, gfp_mask);
1565 if (!ab)
1566 return NULL;
Eric Parisee080e62009-06-11 14:31:35 -04001567
1568 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1569 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001570 goto err;
Paul Moore8cc96382017-05-02 10:16:05 -04001571 if (!nlmsg_put(ab->skb, 0, 0, type, 0, 0))
1572 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001573
Paul Moore8cc96382017-05-02 10:16:05 -04001574 ab->ctx = ctx;
1575 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001576
Chris Wright16e19042005-05-06 15:53:34 +01001577 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001578
Chris Wright8fc61152005-05-06 15:54:17 +01001579err:
1580 audit_buffer_free(ab);
1581 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001584/**
1585 * audit_serial - compute a serial number for the audit record
1586 *
1587 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001588 * written to user-space as soon as they are generated, so a complete
1589 * audit record may be written in several pieces. The timestamp of the
1590 * record and this serial number are used by the user-space tools to
1591 * determine which pieces belong to the same audit record. The
1592 * (timestamp,serial) tuple is unique for each syscall and is live from
1593 * syscall entry to syscall exit.
1594 *
David Woodhousebfb44962005-05-21 21:08:09 +01001595 * NOTE: Another possibility is to store the formatted records off the
1596 * audit context (for those records that have a context), and emit them
1597 * all at syscall exit. However, this could delay the reporting of
1598 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001599 * halts).
1600 */
David Woodhousebfb44962005-05-21 21:08:09 +01001601unsigned int audit_serial(void)
1602{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001603 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001604
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001605 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001606}
1607
Daniel Walker5600b892007-10-18 03:06:10 -07001608static inline void audit_get_stamp(struct audit_context *ctx,
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001609 struct timespec64 *t, unsigned int *serial)
David Woodhousebfb44962005-05-21 21:08:09 +01001610{
Al Viro48887e62008-12-06 01:05:50 -05001611 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001612 ktime_get_real_ts64(t);
David Woodhousebfb44962005-05-21 21:08:09 +01001613 *serial = audit_serial();
1614 }
1615}
1616
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001617/**
1618 * audit_log_start - obtain an audit buffer
1619 * @ctx: audit_context (may be NULL)
1620 * @gfp_mask: type of allocation
1621 * @type: audit message type
1622 *
1623 * Returns audit_buffer pointer on success or NULL on error.
1624 *
1625 * Obtain an audit buffer. This routine does locking to obtain the
1626 * audit buffer, but then no locking is required for calls to
1627 * audit_log_*format. If the task (ctx) is a task that is currently in a
1628 * syscall, then the syscall is marked as auditable and an audit record
1629 * will be written at syscall exit. If there is no associated task, then
1630 * task context (ctx) should be NULL.
1631 */
Al Viro9796fdd2005-10-21 03:22:03 -04001632struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001633 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Paul Moore31975422016-11-29 16:53:25 -05001635 struct audit_buffer *ab;
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001636 struct timespec64 t;
Paul Moore31975422016-11-29 16:53:25 -05001637 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Eric Parisa3f07112008-11-05 12:47:09 -05001639 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return NULL;
1641
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001642 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001643 return NULL;
1644
Paul Moore5b523302017-03-21 11:26:35 -04001645 /* NOTE: don't ever fail/sleep on these two conditions:
Paul Moorea09cfa42016-11-29 16:53:26 -05001646 * 1. auditd generated record - since we need auditd to drain the
1647 * queue; also, when we are checking for auditd, compare PIDs using
1648 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg()
1649 * using a PID anchored in the caller's namespace
Paul Moore5b523302017-03-21 11:26:35 -04001650 * 2. generator holding the audit_cmd_mutex - we don't want to block
1651 * while holding the mutex */
1652 if (!(auditd_test_task(current) ||
1653 (current == __mutex_owner(&audit_cmd_mutex)))) {
1654 long stime = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001655
Paul Moore31975422016-11-29 16:53:25 -05001656 while (audit_backlog_limit &&
1657 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1658 /* wake kauditd to try and flush the queue */
1659 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001660
Paul Moore31975422016-11-29 16:53:25 -05001661 /* sleep if we are allowed and we haven't exhausted our
1662 * backlog wait limit */
Paul Moore5b523302017-03-21 11:26:35 -04001663 if (gfpflags_allow_blocking(gfp_mask) && (stime > 0)) {
Paul Moore31975422016-11-29 16:53:25 -05001664 DECLARE_WAITQUEUE(wait, current);
1665
1666 add_wait_queue_exclusive(&audit_backlog_wait,
1667 &wait);
1668 set_current_state(TASK_UNINTERRUPTIBLE);
Paul Moore5b523302017-03-21 11:26:35 -04001669 stime = schedule_timeout(stime);
Paul Moore31975422016-11-29 16:53:25 -05001670 remove_wait_queue(&audit_backlog_wait, &wait);
1671 } else {
1672 if (audit_rate_check() && printk_ratelimit())
1673 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1674 skb_queue_len(&audit_queue),
1675 audit_backlog_limit);
1676 audit_log_lost("backlog limit exceeded");
1677 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001678 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001679 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001680 }
1681
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001682 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!ab) {
1684 audit_log_lost("out of memory in audit_log_start");
1685 return NULL;
1686 }
1687
David Woodhousebfb44962005-05-21 21:08:09 +01001688 audit_get_stamp(ab->ctx, &t, &serial);
Deepa Dinamani2115bb22017-05-02 10:16:05 -04001689 audit_log_format(ab, "audit(%llu.%03lu:%u): ",
1690 (unsigned long long)t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return ab;
1693}
1694
Chris Wright8fc61152005-05-06 15:54:17 +01001695/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001696 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001697 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001698 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001699 *
1700 * Returns 0 (no space) on failed expansion, or available space if
1701 * successful.
1702 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001703static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001704{
Chris Wright5ac52f32005-05-06 15:54:53 +01001705 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001706 int oldtail = skb_tailroom(skb);
1707 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1708 int newtail = skb_tailroom(skb);
1709
Chris Wright5ac52f32005-05-06 15:54:53 +01001710 if (ret < 0) {
1711 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001712 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001713 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001714
1715 skb->truesize += newtail - oldtail;
1716 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001719/*
1720 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * room in the audit buffer, more room will be allocated and vsnprint
1722 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001723 * can't format message larger than 1024 bytes, so we don't either.
1724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1726 va_list args)
1727{
1728 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001729 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001730 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 if (!ab)
1733 return;
1734
Chris Wright5ac52f32005-05-06 15:54:53 +01001735 BUG_ON(!ab->skb);
1736 skb = ab->skb;
1737 avail = skb_tailroom(skb);
1738 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001739 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001740 if (!avail)
1741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001743 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001744 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (len >= avail) {
1746 /* The printk buffer is 1024 bytes long, so if we get
1747 * here and AUDIT_BUFSIZ is at least 1024, then we can
1748 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001749 avail = audit_expand(ab,
1750 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001751 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001752 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001753 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Steve Grubb168b7172005-05-19 10:24:22 +01001755 if (len > 0)
1756 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001757out_va_end:
1758 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001759out:
1760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001763/**
1764 * audit_log_format - format a message into the audit buffer.
1765 * @ab: audit_buffer
1766 * @fmt: format string
1767 * @...: optional parameters matching @fmt string
1768 *
1769 * All the work is done in audit_log_vformat.
1770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1772{
1773 va_list args;
1774
1775 if (!ab)
1776 return;
1777 va_start(args, fmt);
1778 audit_log_vformat(ab, fmt, args);
1779 va_end(args);
1780}
1781
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001782/**
1783 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1784 * @ab: the audit_buffer
1785 * @buf: buffer to convert to hex
1786 * @len: length of @buf to be converted
1787 *
1788 * No return value; failure to expand is silently ignored.
1789 *
1790 * This function will take the passed buf and convert it into a string of
1791 * ascii hex digits. The new string is placed onto the skb.
1792 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001793void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001794 size_t len)
83c7d092005-04-29 15:54:44 +01001795{
Steve Grubb168b7172005-05-19 10:24:22 +01001796 int i, avail, new_len;
1797 unsigned char *ptr;
1798 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001799
Amy Griffis8ef2d302006-09-07 17:03:02 -04001800 if (!ab)
1801 return;
1802
Steve Grubb168b7172005-05-19 10:24:22 +01001803 BUG_ON(!ab->skb);
1804 skb = ab->skb;
1805 avail = skb_tailroom(skb);
1806 new_len = len<<1;
1807 if (new_len >= avail) {
1808 /* Round the buffer request up to the next multiple */
1809 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1810 avail = audit_expand(ab, new_len);
1811 if (!avail)
1812 return;
1813 }
1814
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001815 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001816 for (i = 0; i < len; i++)
1817 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001818 *ptr = 0;
1819 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001820}
1821
Amy Griffis9c937dc2006-06-08 23:19:31 -04001822/*
1823 * Format a string of no more than slen characters into the audit buffer,
1824 * enclosed in quote marks.
1825 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001826void audit_log_n_string(struct audit_buffer *ab, const char *string,
1827 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001828{
1829 int avail, new_len;
1830 unsigned char *ptr;
1831 struct sk_buff *skb;
1832
Amy Griffis8ef2d302006-09-07 17:03:02 -04001833 if (!ab)
1834 return;
1835
Amy Griffis9c937dc2006-06-08 23:19:31 -04001836 BUG_ON(!ab->skb);
1837 skb = ab->skb;
1838 avail = skb_tailroom(skb);
1839 new_len = slen + 3; /* enclosing quotes + null terminator */
1840 if (new_len > avail) {
1841 avail = audit_expand(ab, new_len);
1842 if (!avail)
1843 return;
1844 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001845 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001846 *ptr++ = '"';
1847 memcpy(ptr, string, slen);
1848 ptr += slen;
1849 *ptr++ = '"';
1850 *ptr = 0;
1851 skb_put(skb, slen + 2); /* don't include null terminator */
1852}
1853
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001854/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001855 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001856 * @string: string to be checked
1857 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001858 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001859bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001860{
1861 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001862 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001863 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001864 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001865 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001866 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001867}
1868
1869/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001870 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001871 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001872 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001873 * @string: string to be logged
1874 *
1875 * This code will escape a string that is passed to it if the string
1876 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001877 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001878 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001879 *
1880 * The caller specifies the number of characters in the string to log, which may
1881 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001882 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001883void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1884 size_t len)
83c7d092005-04-29 15:54:44 +01001885{
Eric Parisde6bbd12008-01-07 14:31:58 -05001886 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001887 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001888 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001889 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001890}
1891
Amy Griffis9c937dc2006-06-08 23:19:31 -04001892/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001893 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001894 * @ab: audit_buffer
1895 * @string: string to be logged
1896 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001897 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001898 * determine string length.
1899 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001900void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001901{
Eric Parisb556f8a2008-04-18 10:12:59 -04001902 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001903}
1904
Steve Grubb168b7172005-05-19 10:24:22 +01001905/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001907 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Jan Blunck44707fd2008-02-14 19:38:33 -08001909 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Chris Wright8fc61152005-05-06 15:54:17 +01001911 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001912 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Steve Grubb168b7172005-05-19 10:24:22 +01001914 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001915 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1916 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001917 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001918 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001920 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001921 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1922 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001923 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001924 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001925 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001926 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Eric Paris4d3fb702013-04-30 09:53:34 -04001929void audit_log_session_info(struct audit_buffer *ab)
1930{
Eric Paris4440e852013-11-27 17:35:17 -05001931 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001932 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1933
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001934 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001935}
1936
Eric Paris9d960982009-06-11 14:31:37 -04001937void audit_log_key(struct audit_buffer *ab, char *key)
1938{
1939 audit_log_format(ab, " key=");
1940 if (key)
1941 audit_log_untrustedstring(ab, key);
1942 else
1943 audit_log_format(ab, "(null)");
1944}
1945
Eric Parisb24a30a2013-04-30 15:30:32 -04001946void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1947{
1948 int i;
1949
1950 audit_log_format(ab, " %s=", prefix);
1951 CAP_FOR_EACH_U32(i) {
1952 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04001953 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04001954 }
1955}
1956
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04001957static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04001958{
1959 kernel_cap_t *perm = &name->fcap.permitted;
1960 kernel_cap_t *inh = &name->fcap.inheritable;
1961 int log = 0;
1962
1963 if (!cap_isclear(*perm)) {
1964 audit_log_cap(ab, "cap_fp", perm);
1965 log = 1;
1966 }
1967 if (!cap_isclear(*inh)) {
1968 audit_log_cap(ab, "cap_fi", inh);
1969 log = 1;
1970 }
1971
1972 if (log)
1973 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1974 name->fcap.fE, name->fcap_ver);
1975}
1976
1977static inline int audit_copy_fcaps(struct audit_names *name,
1978 const struct dentry *dentry)
1979{
1980 struct cpu_vfs_cap_data caps;
1981 int rc;
1982
1983 if (!dentry)
1984 return 0;
1985
1986 rc = get_vfs_caps_from_disk(dentry, &caps);
1987 if (rc)
1988 return rc;
1989
1990 name->fcap.permitted = caps.permitted;
1991 name->fcap.inheritable = caps.inheritable;
1992 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1993 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1994 VFS_CAP_REVISION_SHIFT;
1995
1996 return 0;
1997}
1998
1999/* Copy inode data into an audit_names. */
2000void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05002001 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04002002{
2003 name->ino = inode->i_ino;
2004 name->dev = inode->i_sb->s_dev;
2005 name->mode = inode->i_mode;
2006 name->uid = inode->i_uid;
2007 name->gid = inode->i_gid;
2008 name->rdev = inode->i_rdev;
2009 security_inode_getsecid(inode, &name->osid);
2010 audit_copy_fcaps(name, dentry);
2011}
2012
2013/**
2014 * audit_log_name - produce AUDIT_PATH record from struct audit_names
2015 * @context: audit_context for the task
2016 * @n: audit_names structure with reportable details
2017 * @path: optional path to report instead of audit_names->name
2018 * @record_num: record number to report when handling a list of names
2019 * @call_panic: optional pointer to int that will be updated if secid fails
2020 */
2021void audit_log_name(struct audit_context *context, struct audit_names *n,
Al Viro8bd10762016-11-20 20:36:51 -05002022 const struct path *path, int record_num, int *call_panic)
Eric Parisb24a30a2013-04-30 15:30:32 -04002023{
2024 struct audit_buffer *ab;
2025 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
2026 if (!ab)
2027 return;
2028
2029 audit_log_format(ab, "item=%d", record_num);
2030
2031 if (path)
2032 audit_log_d_path(ab, " name=", path);
2033 else if (n->name) {
2034 switch (n->name_len) {
2035 case AUDIT_NAME_FULL:
2036 /* log the full path */
2037 audit_log_format(ab, " name=");
2038 audit_log_untrustedstring(ab, n->name->name);
2039 break;
2040 case 0:
2041 /* name was specified as a relative path and the
2042 * directory component is the cwd */
2043 audit_log_d_path(ab, " name=", &context->pwd);
2044 break;
2045 default:
2046 /* log the name's directory component */
2047 audit_log_format(ab, " name=");
2048 audit_log_n_untrustedstring(ab, n->name->name,
2049 n->name_len);
2050 }
2051 } else
2052 audit_log_format(ab, " name=(null)");
2053
Linus Torvalds425afcf2015-09-08 13:34:59 -07002054 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04002055 audit_log_format(ab, " inode=%lu"
2056 " dev=%02x:%02x mode=%#ho"
2057 " ouid=%u ogid=%u rdev=%02x:%02x",
2058 n->ino,
2059 MAJOR(n->dev),
2060 MINOR(n->dev),
2061 n->mode,
2062 from_kuid(&init_user_ns, n->uid),
2063 from_kgid(&init_user_ns, n->gid),
2064 MAJOR(n->rdev),
2065 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04002066 if (n->osid != 0) {
2067 char *ctx = NULL;
2068 u32 len;
2069 if (security_secid_to_secctx(
2070 n->osid, &ctx, &len)) {
2071 audit_log_format(ab, " osid=%u", n->osid);
2072 if (call_panic)
2073 *call_panic = 2;
2074 } else {
2075 audit_log_format(ab, " obj=%s", ctx);
2076 security_release_secctx(ctx, len);
2077 }
2078 }
2079
Jeff Laytond3aea842013-05-08 10:32:23 -04002080 /* log the audit_names record type */
2081 audit_log_format(ab, " nametype=");
2082 switch(n->type) {
2083 case AUDIT_TYPE_NORMAL:
2084 audit_log_format(ab, "NORMAL");
2085 break;
2086 case AUDIT_TYPE_PARENT:
2087 audit_log_format(ab, "PARENT");
2088 break;
2089 case AUDIT_TYPE_CHILD_DELETE:
2090 audit_log_format(ab, "DELETE");
2091 break;
2092 case AUDIT_TYPE_CHILD_CREATE:
2093 audit_log_format(ab, "CREATE");
2094 break;
2095 default:
2096 audit_log_format(ab, "UNKNOWN");
2097 break;
2098 }
2099
Eric Parisb24a30a2013-04-30 15:30:32 -04002100 audit_log_fcaps(ab, n);
2101 audit_log_end(ab);
2102}
2103
2104int audit_log_task_context(struct audit_buffer *ab)
2105{
2106 char *ctx = NULL;
2107 unsigned len;
2108 int error;
2109 u32 sid;
2110
2111 security_task_getsecid(current, &sid);
2112 if (!sid)
2113 return 0;
2114
2115 error = security_secid_to_secctx(sid, &ctx, &len);
2116 if (error) {
2117 if (error != -EINVAL)
2118 goto error_path;
2119 return 0;
2120 }
2121
2122 audit_log_format(ab, " subj=%s", ctx);
2123 security_release_secctx(ctx, len);
2124 return 0;
2125
2126error_path:
2127 audit_panic("error in audit_log_task_context");
2128 return error;
2129}
2130EXPORT_SYMBOL(audit_log_task_context);
2131
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002132void audit_log_d_path_exe(struct audit_buffer *ab,
2133 struct mm_struct *mm)
2134{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002135 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002136
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002137 if (!mm)
2138 goto out_null;
2139
2140 exe_file = get_mm_exe_file(mm);
2141 if (!exe_file)
2142 goto out_null;
2143
2144 audit_log_d_path(ab, " exe=", &exe_file->f_path);
2145 fput(exe_file);
2146 return;
2147out_null:
2148 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002149}
2150
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04002151struct tty_struct *audit_get_tty(struct task_struct *tsk)
2152{
2153 struct tty_struct *tty = NULL;
2154 unsigned long flags;
2155
2156 spin_lock_irqsave(&tsk->sighand->siglock, flags);
2157 if (tsk->signal)
2158 tty = tty_kref_get(tsk->signal->tty);
2159 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2160 return tty;
2161}
2162
2163void audit_put_tty(struct tty_struct *tty)
2164{
2165 tty_kref_put(tty);
2166}
2167
Eric Parisb24a30a2013-04-30 15:30:32 -04002168void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2169{
2170 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002171 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002172 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002173
2174 if (!ab)
2175 return;
2176
2177 /* tsk == current */
2178 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002179 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002180 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002181 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002182 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002183 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002184 task_ppid_nr(tsk),
Paul Moorefa2bea22016-08-30 17:19:13 -04002185 task_tgid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002186 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2187 from_kuid(&init_user_ns, cred->uid),
2188 from_kgid(&init_user_ns, cred->gid),
2189 from_kuid(&init_user_ns, cred->euid),
2190 from_kuid(&init_user_ns, cred->suid),
2191 from_kuid(&init_user_ns, cred->fsuid),
2192 from_kgid(&init_user_ns, cred->egid),
2193 from_kgid(&init_user_ns, cred->sgid),
2194 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002195 tty ? tty_name(tty) : "(none)",
2196 audit_get_sessionid(tsk));
2197 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002198 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002199 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002200 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002201 audit_log_task_context(ab);
2202}
2203EXPORT_SYMBOL(audit_log_task_info);
2204
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002205/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002206 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302207 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002208 * @link: the path that triggered the restriction
2209 */
Al Viro8bd10762016-11-20 20:36:51 -05002210void audit_log_link_denied(const char *operation, const struct path *link)
Kees Cooka51d9ea2012-07-25 17:29:08 -07002211{
2212 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002213 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002214
Eric Parisb24a30a2013-04-30 15:30:32 -04002215 name = kzalloc(sizeof(*name), GFP_NOFS);
2216 if (!name)
2217 return;
2218
2219 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002220 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2221 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002222 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002223 goto out;
2224 audit_log_format(ab, "op=%s", operation);
2225 audit_log_task_info(ab, current);
2226 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002227 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002228
2229 /* Generate AUDIT_PATH record with object. */
2230 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002231 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002232 audit_log_name(current->audit_context, name, link, 0, NULL);
2233out:
2234 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002235}
2236
2237/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002238 * audit_log_end - end one audit record
2239 * @ab: the audit_buffer
2240 *
Paul Moore4aa838722016-11-29 16:53:24 -05002241 * We can not do a netlink send inside an irq context because it blocks (last
2242 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2243 * queue and a tasklet is scheduled to remove them from the queue outside the
2244 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002245 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002246void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Paul Moore5b523302017-03-21 11:26:35 -04002248 struct sk_buff *skb;
2249 struct nlmsghdr *nlh;
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (!ab)
2252 return;
Paul Moore5b523302017-03-21 11:26:35 -04002253
2254 if (audit_rate_check()) {
2255 skb = ab->skb;
Eric Parisf3d357b2008-04-18 10:02:28 -04002256 ab->skb = NULL;
Paul Moore5b523302017-03-21 11:26:35 -04002257
2258 /* setup the netlink header, see the comments in
2259 * kauditd_send_multicast_skb() for length quirks */
2260 nlh = nlmsg_hdr(skb);
2261 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
2262
2263 /* queue the netlink packet and poke the kauditd thread */
2264 skb_queue_tail(&audit_queue, skb);
2265 wake_up_interruptible(&kauditd_wait);
2266 } else
2267 audit_log_lost("rate limit exceeded");
2268
Chris Wright16e19042005-05-06 15:53:34 +01002269 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002272/**
2273 * audit_log - Log an audit record
2274 * @ctx: audit context
2275 * @gfp_mask: type of allocation
2276 * @type: audit message type
2277 * @fmt: format string to use
2278 * @...: variable parameters matching the format string
2279 *
2280 * This is a convenience function that calls audit_log_start,
2281 * audit_log_vformat, and audit_log_end. It may be called
2282 * in any context.
2283 */
Daniel Walker5600b892007-10-18 03:06:10 -07002284void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002285 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct audit_buffer *ab;
2288 va_list args;
2289
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002290 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (ab) {
2292 va_start(args, fmt);
2293 audit_log_vformat(ab, fmt, args);
2294 va_end(args);
2295 audit_log_end(ab);
2296 }
2297}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002298
Mr Dash Four131ad622011-06-30 13:31:57 +02002299#ifdef CONFIG_SECURITY
2300/**
2301 * audit_log_secctx - Converts and logs SELinux context
2302 * @ab: audit_buffer
2303 * @secid: security number
2304 *
2305 * This is a helper function that calls security_secid_to_secctx to convert
2306 * secid to secctx and then adds the (converted) SELinux context to the audit
2307 * log by calling audit_log_format, thus also preventing leak of internal secid
2308 * to userspace. If secid cannot be converted audit_panic is called.
2309 */
2310void audit_log_secctx(struct audit_buffer *ab, u32 secid)
2311{
2312 u32 len;
2313 char *secctx;
2314
2315 if (security_secid_to_secctx(secid, &secctx, &len)) {
2316 audit_panic("Cannot convert secid to context");
2317 } else {
2318 audit_log_format(ab, " obj=%s", secctx);
2319 security_release_secctx(secctx, len);
2320 }
2321}
2322EXPORT_SYMBOL(audit_log_secctx);
2323#endif
2324
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002325EXPORT_SYMBOL(audit_log_start);
2326EXPORT_SYMBOL(audit_log_end);
2327EXPORT_SYMBOL(audit_log_format);
2328EXPORT_SYMBOL(audit_log);