blob: 25dd70a588b27b6ede925fce4244f4bc173012e6 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* audit.c -- Auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Gateway between the kernel (e.g., selinux) and the user-space audit daemon.
3 * System-call specific features have moved to auditsc.c
4 *
Steve Grubb6a01b07f2007-01-19 14:39:55 -05005 * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 *
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +020024 * Goals: 1) Integrate fully with Security Modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 2) Minimal run-time overhead:
26 * a) Minimal when syscall auditing is disabled (audit_enable=0).
27 * b) Small when syscall auditing is enabled and no audit record
28 * is generated (defer as much work as possible to record
29 * generation time):
30 * i) context is allocated,
31 * ii) names from getname are stored without a copy, and
32 * iii) inode information stored from path_lookup.
33 * 3) Ability to disable syscall auditing at boot time (audit=0).
34 * 4) Usable by other parts of the kernel (if audit_log* is called,
35 * then a syscall record will be generated automatically for the
36 * current syscall).
37 * 5) Netlink interface to user-space.
38 * 6) Support low-overhead kernel-based filtering to minimize the
39 * information that must be passed to user-space.
40 *
85c87212005-04-29 16:23:29 +010041 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
43
Joe Perchesd957f7b2014-01-14 10:33:12 -080044#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
Davidlohr Bueso5b282552015-02-22 18:20:09 -080046#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/init.h>
Paul McQuade7153e402014-06-06 14:37:37 -070048#include <linux/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040051#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
David Woodhouseb7d11252005-05-19 10:56:58 +010053#include <linux/err.h>
54#include <linux/kthread.h>
Richard Guy Briggs46e959e2013-05-03 14:03:50 -040055#include <linux/kernel.h>
Eric Parisb24a30a2013-04-30 15:30:32 -040056#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <linux/audit.h>
59
60#include <net/sock.h>
Amy Griffis93315ed2006-02-07 12:05:27 -050061#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/skbuff.h>
Mr Dash Four131ad622011-06-30 13:31:57 +020063#ifdef CONFIG_SECURITY
64#include <linux/security.h>
65#endif
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080066#include <linux/freezer.h>
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -070067#include <linux/pid_namespace.h>
Richard Guy Briggs33faba72013-07-16 13:18:45 -040068#include <net/netns/generic.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060069
70#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Parisa3f07112008-11-05 12:47:09 -050072/* No auditing will take place until audit_initialized == AUDIT_INITIALIZED.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * (Initialization happens after skb_init is called.) */
Eric Parisa3f07112008-11-05 12:47:09 -050074#define AUDIT_DISABLED -1
75#define AUDIT_UNINITIALIZED 0
76#define AUDIT_INITIALIZED 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int audit_initialized;
78
Eric Paris1a6b9f22008-01-07 17:09:31 -050079#define AUDIT_OFF 0
80#define AUDIT_ON 1
81#define AUDIT_LOCKED 2
Joe Perches3e1d0bb2014-01-14 10:33:13 -080082u32 audit_enabled;
83u32 audit_ever_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jan Engelhardtae9d67af2011-01-18 06:48:12 +010085EXPORT_SYMBOL_GPL(audit_enabled);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Default state when kernel boots without any parameters. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080088static u32 audit_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* If auditing cannot proceed, audit_failure selects what happens. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -080091static u32 audit_failure = AUDIT_FAIL_PRINTK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Pavel Emelyanov75c03712008-03-20 15:39:41 -070093/*
94 * If audit records are to be written to the netlink socket, audit_pid
Eric W. Biederman15e47302012-09-07 20:12:54 +000095 * contains the pid of the auditd process and audit_nlk_portid contains
96 * the portid to use to send netlink messages to that process.
Pavel Emelyanov75c03712008-03-20 15:39:41 -070097 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010098int audit_pid;
Richard Guy Briggsf9441632013-08-14 11:32:45 -040099static __u32 audit_nlk_portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700101/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * to that number per second. This prevents DoS attacks, but results in
103 * audit records being dropped. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800104static u32 audit_rate_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Richard Guy Briggs40c07752013-10-22 13:28:49 -0400106/* Number of outstanding audit_buffers allowed.
107 * When set to zero, this means unlimited. */
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800108static u32 audit_backlog_limit = 64;
Richard Guy Briggse789e562013-09-12 23:03:51 -0400109#define AUDIT_BACKLOG_WAIT_TIME (60 * HZ)
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800110static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100112/* The identity of the user shutting down the audit system. */
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800113kuid_t audit_sig_uid = INVALID_UID;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100114pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -0400115u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Records can be lost in several ways:
118 0) [suppressed in audit_alloc]
119 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
120 2) out of memory in audit_log_move [alloc_skb]
121 3) suppressed due to audit_rate_limit
122 4) suppressed due to audit_backlog_limit
123*/
Richard Guy Briggs92c82e82017-01-13 03:26:29 -0500124static atomic_t audit_lost = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* The netlink socket. */
127static struct sock *audit_sock;
Richard Guy Briggsc0a8d9b2014-05-26 10:59:28 -0400128static int audit_net_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Amy Griffisf368c07d2006-04-07 16:55:56 -0400130/* Hash for inode-based rules */
131struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
132
David Woodhouseb7d11252005-05-19 10:56:58 +0100133/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
135 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700137static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static LIST_HEAD(audit_freelist);
139
Paul Moorec6480202016-11-29 16:53:25 -0500140/* queue msgs to send via kauditd_task */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500141static struct sk_buff_head audit_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500142/* queue msgs due to temporary unicast send problems */
143static struct sk_buff_head audit_retry_queue;
144/* queue msgs waiting for new auditd connection */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500145static struct sk_buff_head audit_hold_queue;
Paul Moorec6480202016-11-29 16:53:25 -0500146
147/* queue servicing thread */
David Woodhouseb7d11252005-05-19 10:56:58 +0100148static struct task_struct *kauditd_task;
149static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500150
151/* waitqueue for callers who are blocked on the audit backlog */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100152static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Parisb0fed402013-05-22 12:54:49 -0400154static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION,
155 .mask = -1,
156 .features = 0,
157 .lock = 0,};
158
Eric Paris21b85c32013-05-23 14:26:00 -0400159static char *audit_feature_names[2] = {
Eric Parisd040e5a2013-05-24 09:18:04 -0400160 "only_unset_loginuid",
Eric Paris21b85c32013-05-23 14:26:00 -0400161 "loginuid_immutable",
Eric Parisb0fed402013-05-22 12:54:49 -0400162};
163
164
Amy Griffisf368c07d2006-04-07 16:55:56 -0400165/* Serialize requests from userspace. */
Al Viro916d7572009-06-24 00:02:38 -0400166DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
169 * audit records. Since printk uses a 1024 byte buffer, this buffer
170 * should be at least that large. */
171#define AUDIT_BUFSIZ 1024
172
173/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
174 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
175#define AUDIT_MAXFREE (2*NR_CPUS)
176
177/* The audit_buffer is used when formatting an audit record. The caller
178 * locks briefly to get the record off the freelist or to allocate the
179 * buffer, and locks briefly to send the buffer to the netlink layer or
180 * to place it on a transmit queue. Multiple audit_buffers can be in
181 * use simultaneously. */
182struct audit_buffer {
183 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100184 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400186 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Eric Parisf09ac9d2008-04-18 10:11:04 -0400189struct audit_reply {
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400190 __u32 portid;
Eric W. Biederman638a0fd2014-02-28 10:49:05 -0800191 struct net *net;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400192 struct sk_buff *skb;
193};
194
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400195static void audit_set_portid(struct audit_buffer *ab, __u32 portid)
Steve Grubbc0404992005-05-13 18:17:42 +0100196{
Eric Paris50397bd2008-01-07 18:14:19 -0500197 if (ab) {
198 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400199 nlh->nlmsg_pid = portid;
Eric Paris50397bd2008-01-07 18:14:19 -0500200 }
Steve Grubbc0404992005-05-13 18:17:42 +0100201}
202
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000203void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Joe Perchesd957f7b2014-01-14 10:33:12 -0800205 switch (audit_failure) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case AUDIT_FAIL_SILENT:
207 break;
208 case AUDIT_FAIL_PRINTK:
Eric Paris320f1b12008-01-23 22:55:05 -0500209 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800210 pr_err("%s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 case AUDIT_FAIL_PANIC:
Eric Parisb29ee872008-02-21 15:53:05 -0500213 /* test audit_pid since printk is always losey, why bother? */
214 if (audit_pid)
215 panic("audit: %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
218}
219
220static inline int audit_rate_check(void)
221{
222 static unsigned long last_check = 0;
223 static int messages = 0;
224 static DEFINE_SPINLOCK(lock);
225 unsigned long flags;
226 unsigned long now;
227 unsigned long elapsed;
228 int retval = 0;
229
230 if (!audit_rate_limit) return 1;
231
232 spin_lock_irqsave(&lock, flags);
233 if (++messages < audit_rate_limit) {
234 retval = 1;
235 } else {
236 now = jiffies;
237 elapsed = now - last_check;
238 if (elapsed > HZ) {
239 last_check = now;
240 messages = 0;
241 retval = 1;
242 }
243 }
244 spin_unlock_irqrestore(&lock, flags);
245
246 return retval;
247}
248
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700249/**
250 * audit_log_lost - conditionally log lost audit message event
251 * @message: the message stating reason for lost audit message
252 *
253 * Emit at least 1 message per second, even if audit_rate_check is
254 * throttling.
255 * Always increment the lost messages counter.
256*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257void audit_log_lost(const char *message)
258{
259 static unsigned long last_msg = 0;
260 static DEFINE_SPINLOCK(lock);
261 unsigned long flags;
262 unsigned long now;
263 int print;
264
265 atomic_inc(&audit_lost);
266
267 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
268
269 if (!print) {
270 spin_lock_irqsave(&lock, flags);
271 now = jiffies;
272 if (now - last_msg > HZ) {
273 print = 1;
274 last_msg = now;
275 }
276 spin_unlock_irqrestore(&lock, flags);
277 }
278
279 if (print) {
Eric Paris320f1b12008-01-23 22:55:05 -0500280 if (printk_ratelimit())
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800281 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
Eric Paris320f1b12008-01-23 22:55:05 -0500282 atomic_read(&audit_lost),
283 audit_rate_limit,
284 audit_backlog_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 audit_panic(message);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800289static int audit_log_config_change(char *function_name, u32 new, u32 old,
Eric Paris25323862008-04-18 10:09:25 -0400290 int allow_changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Eric Paris1a6b9f22008-01-07 17:09:31 -0500292 struct audit_buffer *ab;
293 int rc = 0;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500294
Eric Paris1a6b9f22008-01-07 17:09:31 -0500295 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
Kees Cook0644ec02013-01-11 14:32:07 -0800296 if (unlikely(!ab))
297 return rc;
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800298 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
Eric Paris4d3fb702013-04-30 09:53:34 -0400299 audit_log_session_info(ab);
Eric Parisb122c372013-04-19 15:00:33 -0400300 rc = audit_log_task_context(ab);
301 if (rc)
302 allow_changes = 0; /* Something weird, deny request */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500303 audit_log_format(ab, " res=%d", allow_changes);
304 audit_log_end(ab);
305 return rc;
306}
307
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800308static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500309{
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800310 int allow_changes, rc = 0;
311 u32 old = *to_change;
Eric Paris1a6b9f22008-01-07 17:09:31 -0500312
313 /* check if we are locked */
314 if (audit_enabled == AUDIT_LOCKED)
315 allow_changes = 0;
316 else
317 allow_changes = 1;
318
319 if (audit_enabled != AUDIT_OFF) {
Eric Parisdc9eb692013-04-19 13:23:09 -0400320 rc = audit_log_config_change(function_name, new, old, allow_changes);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500321 if (rc)
322 allow_changes = 0;
323 }
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500324
325 /* If we are allowed, make the change */
Eric Paris1a6b9f22008-01-07 17:09:31 -0500326 if (allow_changes == 1)
327 *to_change = new;
Steve Grubb6a01b07f2007-01-19 14:39:55 -0500328 /* Not allowed, update reason */
329 else if (rc == 0)
330 rc = -EPERM;
331 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800334static int audit_set_rate_limit(u32 limit)
Eric Paris1a6b9f22008-01-07 17:09:31 -0500335{
Eric Parisdc9eb692013-04-19 13:23:09 -0400336 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
Eric Paris1a6b9f22008-01-07 17:09:31 -0500337}
338
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800339static int audit_set_backlog_limit(u32 limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Eric Parisdc9eb692013-04-19 13:23:09 -0400341 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800344static int audit_set_backlog_wait_time(u32 timeout)
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400345{
346 return audit_do_config_change("audit_backlog_wait_time",
Paul Moore31975422016-11-29 16:53:25 -0500347 &audit_backlog_wait_time, timeout);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -0400348}
349
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800350static int audit_set_enabled(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Eric Parisb593d382008-01-08 17:38:31 -0500352 int rc;
Pranith Kumar724e7bf2015-03-11 14:08:19 -0400353 if (state > AUDIT_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500355
Eric Parisdc9eb692013-04-19 13:23:09 -0400356 rc = audit_do_config_change("audit_enabled", &audit_enabled, state);
Eric Parisb593d382008-01-08 17:38:31 -0500357 if (!rc)
358 audit_ever_enabled |= !!state;
359
360 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Joe Perches3e1d0bb2014-01-14 10:33:13 -0800363static int audit_set_failure(u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (state != AUDIT_FAIL_SILENT
366 && state != AUDIT_FAIL_PRINTK
367 && state != AUDIT_FAIL_PANIC)
368 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500369
Eric Parisdc9eb692013-04-19 13:23:09 -0400370 return audit_do_config_change("audit_failure", &audit_failure, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Eric Parisf3d357b2008-04-18 10:02:28 -0400373/*
Eric Paris038cbcf2009-06-11 14:31:35 -0400374 * For one reason or another this nlh isn't getting delivered to the userspace
375 * audit daemon, just send it to printk.
376 */
Paul Mooreaf8b8242016-11-29 16:53:24 -0500377static void kauditd_printk_skb(struct sk_buff *skb)
Eric Paris038cbcf2009-06-11 14:31:35 -0400378{
379 struct nlmsghdr *nlh = nlmsg_hdr(skb);
David S. Millerc64e66c2012-06-26 21:45:21 -0700380 char *data = nlmsg_data(nlh);
Eric Paris038cbcf2009-06-11 14:31:35 -0400381
382 if (nlh->nlmsg_type != AUDIT_EOE) {
383 if (printk_ratelimit())
Joe Perchesd957f7b2014-01-14 10:33:12 -0800384 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
Eric Paris038cbcf2009-06-11 14:31:35 -0400385 else
Josh Boyerf1283522014-03-05 16:29:55 -0500386 audit_log_lost("printk limit exceeded");
Eric Paris038cbcf2009-06-11 14:31:35 -0400387 }
Eric Paris038cbcf2009-06-11 14:31:35 -0400388}
389
Paul Moorec6480202016-11-29 16:53:25 -0500390/**
391 * kauditd_hold_skb - Queue an audit record, waiting for auditd
392 * @skb: audit record
393 *
394 * Description:
395 * Queue the audit record, waiting for an instance of auditd. When this
396 * function is called we haven't given up yet on sending the record, but things
397 * are not looking good. The first thing we want to do is try to write the
398 * record via printk and then see if we want to try and hold on to the record
399 * and queue it, if we have room. If we want to hold on to the record, but we
400 * don't have room, record a record lost message.
401 */
402static void kauditd_hold_skb(struct sk_buff *skb)
Eric Parisf3d357b2008-04-18 10:02:28 -0400403{
Paul Moorec6480202016-11-29 16:53:25 -0500404 /* at this point it is uncertain if we will ever send this to auditd so
405 * try to send the message via printk before we go any further */
406 kauditd_printk_skb(skb);
Richard Guy Briggs32a1dba2015-11-04 08:23:50 -0500407
Paul Moorec6480202016-11-29 16:53:25 -0500408 /* can we just silently drop the message? */
409 if (!audit_default) {
410 kfree_skb(skb);
411 return;
412 }
413
414 /* if we have room, queue the message */
415 if (!audit_backlog_limit ||
416 skb_queue_len(&audit_hold_queue) < audit_backlog_limit) {
417 skb_queue_tail(&audit_hold_queue, skb);
418 return;
419 }
420
421 /* we have no other options - drop the message */
422 audit_log_lost("kauditd hold queue overflow");
423 kfree_skb(skb);
424}
425
426/**
427 * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd
428 * @skb: audit record
429 *
430 * Description:
431 * Not as serious as kauditd_hold_skb() as we still have a connected auditd,
432 * but for some reason we are having problems sending it audit records so
433 * queue the given record and attempt to resend.
434 */
435static void kauditd_retry_skb(struct sk_buff *skb)
436{
437 /* NOTE: because records should only live in the retry queue for a
438 * short period of time, before either being sent or moved to the hold
439 * queue, we don't currently enforce a limit on this queue */
440 skb_queue_tail(&audit_retry_queue, skb);
441}
442
443/**
444 * auditd_reset - Disconnect the auditd connection
445 *
446 * Description:
447 * Break the auditd/kauditd connection and move all the records in the retry
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500448 * queue into the hold queue in case auditd reconnects. The audit_cmd_mutex
449 * must be held when calling this function.
Paul Moorec6480202016-11-29 16:53:25 -0500450 */
451static void auditd_reset(void)
452{
453 struct sk_buff *skb;
454
455 /* break the connection */
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500456 if (audit_sock) {
457 sock_put(audit_sock);
458 audit_sock = NULL;
459 }
Paul Moorec6480202016-11-29 16:53:25 -0500460 audit_pid = 0;
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500461 audit_nlk_portid = 0;
Paul Moorec6480202016-11-29 16:53:25 -0500462
463 /* flush all of the retry queue to the hold queue */
464 while ((skb = skb_dequeue(&audit_retry_queue)))
465 kauditd_hold_skb(skb);
466}
467
468/**
469 * kauditd_send_unicast_skb - Send a record via unicast to auditd
470 * @skb: audit record
471 */
472static int kauditd_send_unicast_skb(struct sk_buff *skb)
473{
474 int rc;
475
Paul Moore6c54e782016-11-29 16:53:26 -0500476 /* if we know nothing is connected, don't even try the netlink call */
477 if (!audit_pid)
478 return -ECONNREFUSED;
479
Paul Moorec6480202016-11-29 16:53:25 -0500480 /* get an extra skb reference in case we fail to send */
Eric Parisf3d357b2008-04-18 10:02:28 -0400481 skb_get(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500482 rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
483 if (rc >= 0) {
Neil Horman70d4bf62010-07-20 06:45:56 +0000484 consume_skb(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500485 rc = 0;
486 }
487
488 return rc;
Eric Parisf3d357b2008-04-18 10:02:28 -0400489}
490
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500491/*
Paul Moorec6480202016-11-29 16:53:25 -0500492 * kauditd_send_multicast_skb - Send a record to any multicast listeners
493 * @skb: audit record
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400494 *
Paul Moorec6480202016-11-29 16:53:25 -0500495 * Description:
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400496 * This function doesn't consume an skb as might be expected since it has to
497 * copy it anyways.
498 */
Paul Moorec6480202016-11-29 16:53:25 -0500499static void kauditd_send_multicast_skb(struct sk_buff *skb)
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400500{
Paul Moorec6480202016-11-29 16:53:25 -0500501 struct sk_buff *copy;
502 struct audit_net *aunet = net_generic(&init_net, audit_net_id);
503 struct sock *sock = aunet->nlsk;
504 struct nlmsghdr *nlh;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400505
Richard Guy Briggs7f74ecd2014-04-22 21:31:58 -0400506 if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG))
507 return;
508
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400509 /*
510 * The seemingly wasteful skb_copy() rather than bumping the refcount
511 * using skb_get() is necessary because non-standard mods are made to
512 * the skb by the original kaudit unicast socket send routine. The
513 * existing auditd daemon assumes this breakage. Fixing this would
514 * require co-ordinating a change in the established protocol between
515 * the kaudit kernel subsystem and the auditd userspace code. There is
516 * no reason for new multicast clients to continue with this
517 * non-compliance.
518 */
Paul Moorec6480202016-11-29 16:53:25 -0500519 copy = skb_copy(skb, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400520 if (!copy)
521 return;
Paul Moorec6480202016-11-29 16:53:25 -0500522 nlh = nlmsg_hdr(copy);
523 nlh->nlmsg_len = skb->len;
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400524
Paul Moorec6480202016-11-29 16:53:25 -0500525 nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL);
Richard Guy Briggs451f9212014-04-22 21:31:57 -0400526}
527
Paul Moorec6480202016-11-29 16:53:25 -0500528/**
529 * kauditd_wake_condition - Return true when it is time to wake kauditd_thread
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500530 *
Paul Moorec6480202016-11-29 16:53:25 -0500531 * Description:
532 * This function is for use by the wait_event_freezable() call in
533 * kauditd_thread().
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500534 */
Paul Moorec6480202016-11-29 16:53:25 -0500535static int kauditd_wake_condition(void)
David Woodhouseb7d11252005-05-19 10:56:58 +0100536{
Paul Moorec6480202016-11-29 16:53:25 -0500537 static int pid_last = 0;
538 int rc;
539 int pid = audit_pid;
David Woodhouseb7d11252005-05-19 10:56:58 +0100540
Paul Moorec6480202016-11-29 16:53:25 -0500541 /* wake on new messages or a change in the connected auditd */
542 rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last);
543 if (rc)
544 pid_last = pid;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500545
Paul Moorec6480202016-11-29 16:53:25 -0500546 return rc;
Richard Guy Briggsb551d1d2013-01-24 13:15:10 -0500547}
548
David Woodhouseb7d11252005-05-19 10:56:58 +0100549static int kauditd_thread(void *dummy)
550{
Paul Moorec6480202016-11-29 16:53:25 -0500551 int rc;
552 int auditd = 0;
553 int reschedule = 0;
Paul Moore4aa838722016-11-29 16:53:24 -0500554 struct sk_buff *skb;
555 struct nlmsghdr *nlh;
556
Paul Moorec6480202016-11-29 16:53:25 -0500557#define UNICAST_RETRIES 5
558#define AUDITD_BAD(x,y) \
559 ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES)
560
561 /* NOTE: we do invalidate the auditd connection flag on any sending
562 * errors, but we only "restore" the connection flag at specific places
563 * in the loop in order to help ensure proper ordering of audit
564 * records */
565
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700566 set_freezable();
Andrew Morton4899b8b2006-10-06 00:43:48 -0700567 while (!kthread_should_stop()) {
Paul Moorec6480202016-11-29 16:53:25 -0500568 /* NOTE: possible area for future improvement is to look at
569 * the hold and retry queues, since only this thread
570 * has access to these queues we might be able to do
571 * our own queuing and skip some/all of the locking */
Eric Parisf3d357b2008-04-18 10:02:28 -0400572
Paul Moorec6480202016-11-29 16:53:25 -0500573 /* NOTE: it might be a fun experiment to split the hold and
574 * retry queue handling to another thread, but the
575 * synchronization issues and other overhead might kill
576 * any performance gains */
577
578 /* attempt to flush the hold queue */
579 while (auditd && (skb = skb_dequeue(&audit_hold_queue))) {
580 rc = kauditd_send_unicast_skb(skb);
581 if (rc) {
582 /* requeue to the same spot */
583 skb_queue_head(&audit_hold_queue, skb);
584
585 auditd = 0;
586 if (AUDITD_BAD(rc, reschedule)) {
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500587 mutex_lock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500588 auditd_reset();
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500589 mutex_unlock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500590 reschedule = 0;
591 }
592 } else
593 /* we were able to send successfully */
594 reschedule = 0;
595 }
596
597 /* attempt to flush the retry queue */
598 while (auditd && (skb = skb_dequeue(&audit_retry_queue))) {
599 rc = kauditd_send_unicast_skb(skb);
600 if (rc) {
601 auditd = 0;
602 if (AUDITD_BAD(rc, reschedule)) {
603 kauditd_hold_skb(skb);
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500604 mutex_lock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500605 auditd_reset();
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500606 mutex_unlock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500607 reschedule = 0;
608 } else
609 /* temporary problem (we hope), queue
610 * to the same spot and retry */
611 skb_queue_head(&audit_retry_queue, skb);
612 } else
613 /* we were able to send successfully */
614 reschedule = 0;
615 }
616
617 /* standard queue processing, try to be as quick as possible */
618quick_loop:
Paul Mooreaf8b8242016-11-29 16:53:24 -0500619 skb = skb_dequeue(&audit_queue);
David Woodhouseb7d11252005-05-19 10:56:58 +0100620 if (skb) {
Paul Moorec6480202016-11-29 16:53:25 -0500621 /* setup the netlink header, see the comments in
622 * kauditd_send_multicast_skb() for length quirks */
Paul Moore4aa838722016-11-29 16:53:24 -0500623 nlh = nlmsg_hdr(skb);
Paul Moorec6480202016-11-29 16:53:25 -0500624 nlh->nlmsg_len = skb->len - NLMSG_HDRLEN;
Paul Moore4aa838722016-11-29 16:53:24 -0500625
Paul Moorec6480202016-11-29 16:53:25 -0500626 /* attempt to send to any multicast listeners */
627 kauditd_send_multicast_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500628
Paul Moorec6480202016-11-29 16:53:25 -0500629 /* attempt to send to auditd, queue on failure */
630 if (auditd) {
631 rc = kauditd_send_unicast_skb(skb);
632 if (rc) {
633 auditd = 0;
634 if (AUDITD_BAD(rc, reschedule)) {
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500635 mutex_lock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500636 auditd_reset();
Richard Guy Briggs533c7b62016-12-13 10:03:01 -0500637 mutex_unlock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -0500638 reschedule = 0;
639 }
Paul Moore4aa838722016-11-29 16:53:24 -0500640
Paul Moorec6480202016-11-29 16:53:25 -0500641 /* move to the retry queue */
642 kauditd_retry_skb(skb);
643 } else
644 /* everything is working so go fast! */
645 goto quick_loop;
646 } else if (reschedule)
647 /* we are currently having problems, move to
648 * the retry queue */
649 kauditd_retry_skb(skb);
Eric Paris038cbcf2009-06-11 14:31:35 -0400650 else
Paul Moorec6480202016-11-29 16:53:25 -0500651 /* dump the message via printk and hold it */
652 kauditd_hold_skb(skb);
Paul Moore4aa838722016-11-29 16:53:24 -0500653 } else {
Paul Moorec6480202016-11-29 16:53:25 -0500654 /* we have flushed the backlog so wake everyone */
Paul Moore4aa838722016-11-29 16:53:24 -0500655 wake_up(&audit_backlog_wait);
Paul Moorec6480202016-11-29 16:53:25 -0500656
657 /* if everything is okay with auditd (if present), go
658 * to sleep until there is something new in the queue
659 * or we have a change in the connected auditd;
660 * otherwise simply reschedule to give things a chance
661 * to recover */
662 if (reschedule) {
663 set_current_state(TASK_INTERRUPTIBLE);
664 schedule();
665 } else
666 wait_event_freezable(kauditd_wait,
667 kauditd_wake_condition());
668
669 /* update the auditd connection status */
670 auditd = (audit_pid ? 1 : 0);
David Woodhouseb7d11252005-05-19 10:56:58 +0100671 }
672 }
Paul Moorec6480202016-11-29 16:53:25 -0500673
Andrew Morton4899b8b2006-10-06 00:43:48 -0700674 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100675}
676
Al Viro9044e6b2006-05-22 01:09:24 -0400677int audit_send_list(void *_dest)
678{
679 struct audit_netlink_list *dest = _dest;
Al Viro9044e6b2006-05-22 01:09:24 -0400680 struct sk_buff *skb;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800681 struct net *net = dest->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400682 struct audit_net *aunet = net_generic(net, audit_net_id);
Al Viro9044e6b2006-05-22 01:09:24 -0400683
684 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400685 mutex_lock(&audit_cmd_mutex);
686 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400687
688 while ((skb = __skb_dequeue(&dest->q)) != NULL)
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400689 netlink_unicast(aunet->nlsk, skb, dest->portid, 0);
Al Viro9044e6b2006-05-22 01:09:24 -0400690
Eric W. Biederman48095d92014-02-03 17:25:33 -0800691 put_net(net);
Al Viro9044e6b2006-05-22 01:09:24 -0400692 kfree(dest);
693
694 return 0;
695}
696
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400697struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
Stephen Hemmingerb8800aa2010-10-20 17:23:50 -0700698 int multi, const void *payload, int size)
Al Viro9044e6b2006-05-22 01:09:24 -0400699{
700 struct sk_buff *skb;
701 struct nlmsghdr *nlh;
Al Viro9044e6b2006-05-22 01:09:24 -0400702 void *data;
703 int flags = multi ? NLM_F_MULTI : 0;
704 int t = done ? NLMSG_DONE : type;
705
Eric Parisee080e62009-06-11 14:31:35 -0400706 skb = nlmsg_new(size, GFP_KERNEL);
Al Viro9044e6b2006-05-22 01:09:24 -0400707 if (!skb)
708 return NULL;
709
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400710 nlh = nlmsg_put(skb, portid, seq, t, size, flags);
David S. Millerc64e66c2012-06-26 21:45:21 -0700711 if (!nlh)
712 goto out_kfree_skb;
713 data = nlmsg_data(nlh);
Al Viro9044e6b2006-05-22 01:09:24 -0400714 memcpy(data, payload, size);
715 return skb;
716
David S. Millerc64e66c2012-06-26 21:45:21 -0700717out_kfree_skb:
718 kfree_skb(skb);
Al Viro9044e6b2006-05-22 01:09:24 -0400719 return NULL;
720}
721
Eric Parisf09ac9d2008-04-18 10:11:04 -0400722static int audit_send_reply_thread(void *arg)
723{
724 struct audit_reply *reply = (struct audit_reply *)arg;
Eric W. Biederman48095d92014-02-03 17:25:33 -0800725 struct net *net = reply->net;
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400726 struct audit_net *aunet = net_generic(net, audit_net_id);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400727
728 mutex_lock(&audit_cmd_mutex);
729 mutex_unlock(&audit_cmd_mutex);
730
731 /* Ignore failure. It'll only happen if the sender goes away,
732 because our timeout is set to infinite. */
Richard Guy Briggs33faba72013-07-16 13:18:45 -0400733 netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0);
Eric W. Biederman48095d92014-02-03 17:25:33 -0800734 put_net(net);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400735 kfree(reply);
736 return 0;
737}
Paul Moorec6480202016-11-29 16:53:25 -0500738
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700739/**
740 * audit_send_reply - send an audit reply message via netlink
Eric W. Biedermand211f1772014-03-08 15:31:54 -0800741 * @request_skb: skb of request we are replying to (used to target the reply)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700742 * @seq: sequence number
743 * @type: audit message type
744 * @done: done (last) flag
745 * @multi: multi-part message flag
746 * @payload: payload data
747 * @size: payload size
748 *
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400749 * Allocates an skb, builds the netlink message, and sends it to the port id.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700750 * No failure notifications.
751 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800752static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400753 int multi, const void *payload, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800755 u32 portid = NETLINK_CB(request_skb).portid;
756 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Eric Parisf09ac9d2008-04-18 10:11:04 -0400757 struct sk_buff *skb;
758 struct task_struct *tsk;
759 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
760 GFP_KERNEL);
761
762 if (!reply)
763 return;
764
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400765 skb = audit_make_reply(portid, seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!skb)
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700767 goto out;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400768
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800769 reply->net = get_net(net);
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400770 reply->portid = portid;
Eric Parisf09ac9d2008-04-18 10:11:04 -0400771 reply->skb = skb;
772
773 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
Andrew Mortonfcaf1eb2008-05-14 16:11:48 -0700774 if (!IS_ERR(tsk))
775 return;
776 kfree_skb(skb);
777out:
778 kfree(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
782 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
783 * control messages.
784 */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700785static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 int err = 0;
788
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400789 /* Only support initial user namespace for now. */
Eric Parisaa4af832014-03-30 19:07:54 -0400790 /*
791 * We return ECONNREFUSED because it tricks userspace into thinking
792 * that audit was not configured into the kernel. Lots of users
793 * configure their PAM stack (because that's what the distro does)
794 * to reject login if unable to send messages to audit. If we return
795 * ECONNREFUSED the PAM stack thinks the kernel does not have audit
796 * configured in and will let login proceed. If we return EPERM
797 * userspace will reject all logins. This should be removed when we
798 * support non init namespaces!!
799 */
Linus Torvalds0b747172014-04-12 12:38:53 -0700800 if (current_user_ns() != &init_user_ns)
Eric Parisaa4af832014-03-30 19:07:54 -0400801 return -ECONNREFUSED;
Eric W. Biederman34e36d8e2012-09-10 23:20:20 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 switch (msg_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 case AUDIT_ADD:
806 case AUDIT_DEL:
Eric Paris18900902013-04-18 19:16:36 -0400807 return -EOPNOTSUPP;
808 case AUDIT_GET:
809 case AUDIT_SET:
Eric Parisb0fed402013-05-22 12:54:49 -0400810 case AUDIT_GET_FEATURE:
811 case AUDIT_SET_FEATURE:
Eric Paris18900902013-04-18 19:16:36 -0400812 case AUDIT_LIST_RULES:
813 case AUDIT_ADD_RULE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500814 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100815 case AUDIT_SIGNAL_INFO:
Miloslav Trmac522ed772007-07-15 23:40:56 -0700816 case AUDIT_TTY_GET:
817 case AUDIT_TTY_SET:
Al Viro74c3cbe2007-07-22 08:04:18 -0400818 case AUDIT_TRIM:
819 case AUDIT_MAKE_EQUIV:
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400820 /* Only support auditd and auditctl in initial pid namespace
821 * for now. */
Ameen Ali5985de62015-02-23 15:38:00 -0500822 if (task_active_pid_ns(current) != &init_pid_ns)
Richard Guy Briggs5a3cb3b2013-08-16 00:04:46 -0400823 return -EPERM;
824
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700825 if (!netlink_capable(skb, CAP_AUDIT_CONTROL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 err = -EPERM;
827 break;
Steve Grubb05474102005-05-21 00:18:37 +0100828 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -0700829 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
830 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700831 if (!netlink_capable(skb, CAP_AUDIT_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = -EPERM;
833 break;
834 default: /* bad msg */
835 err = -EINVAL;
836 }
837
838 return err;
839}
840
Paul Moore233a6862015-11-04 08:23:52 -0500841static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type)
Eric Paris50397bd2008-01-07 18:14:19 -0500842{
Eric Parisdc9eb692013-04-19 13:23:09 -0400843 uid_t uid = from_kuid(&init_user_ns, current_uid());
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500844 pid_t pid = task_tgid_nr(current);
Eric Paris50397bd2008-01-07 18:14:19 -0500845
Tyler Hicks0868a5e2013-07-25 18:02:55 -0700846 if (!audit_enabled && msg_type != AUDIT_USER_AVC) {
Eric Paris50397bd2008-01-07 18:14:19 -0500847 *ab = NULL;
Paul Moore233a6862015-11-04 08:23:52 -0500848 return;
Eric Paris50397bd2008-01-07 18:14:19 -0500849 }
850
851 *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
Kees Cook0644ec02013-01-11 14:32:07 -0800852 if (unlikely(!*ab))
Paul Moore233a6862015-11-04 08:23:52 -0500853 return;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500854 audit_log_format(*ab, "pid=%d uid=%u", pid, uid);
Eric Paris4d3fb702013-04-30 09:53:34 -0400855 audit_log_session_info(*ab);
Eric Parisb122c372013-04-19 15:00:33 -0400856 audit_log_task_context(*ab);
Eric Paris50397bd2008-01-07 18:14:19 -0500857}
858
Eric Parisb0fed402013-05-22 12:54:49 -0400859int is_audit_feature_set(int i)
860{
861 return af.features & AUDIT_FEATURE_TO_MASK(i);
862}
863
864
865static int audit_get_feature(struct sk_buff *skb)
866{
867 u32 seq;
868
869 seq = nlmsg_hdr(skb)->nlmsg_seq;
870
Richard Guy Briggs9ef91512014-08-24 20:37:52 -0400871 audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af));
Eric Parisb0fed402013-05-22 12:54:49 -0400872
873 return 0;
874}
875
876static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature,
877 u32 old_lock, u32 new_lock, int res)
878{
879 struct audit_buffer *ab;
880
Gao fengb6c50fe2013-11-01 19:34:43 +0800881 if (audit_enabled == AUDIT_OFF)
882 return;
883
Eric Parisb0fed402013-05-22 12:54:49 -0400884 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
Richard Guy Briggsad2ac262014-01-07 13:08:41 -0500885 audit_log_task_info(ab, current);
Richard Guy Briggs897f1ac2014-10-30 11:22:53 -0400886 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 -0400887 audit_feature_names[which], !!old_feature, !!new_feature,
888 !!old_lock, !!new_lock, res);
889 audit_log_end(ab);
890}
891
892static int audit_set_feature(struct sk_buff *skb)
893{
894 struct audit_features *uaf;
895 int i;
896
Fabian Frederick6eed9b22014-06-03 22:05:10 +0200897 BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names));
Eric Parisb0fed402013-05-22 12:54:49 -0400898 uaf = nlmsg_data(nlmsg_hdr(skb));
899
900 /* if there is ever a version 2 we should handle that here */
901
902 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
903 u32 feature = AUDIT_FEATURE_TO_MASK(i);
904 u32 old_feature, new_feature, old_lock, new_lock;
905
906 /* if we are not changing this feature, move along */
907 if (!(feature & uaf->mask))
908 continue;
909
910 old_feature = af.features & feature;
911 new_feature = uaf->features & feature;
912 new_lock = (uaf->lock | af.lock) & feature;
913 old_lock = af.lock & feature;
914
915 /* are we changing a locked feature? */
Gao feng4547b3b2013-11-01 19:34:44 +0800916 if (old_lock && (new_feature != old_feature)) {
Eric Parisb0fed402013-05-22 12:54:49 -0400917 audit_log_feature_change(i, old_feature, new_feature,
918 old_lock, new_lock, 0);
919 return -EPERM;
920 }
921 }
922 /* nothing invalid, do the changes */
923 for (i = 0; i <= AUDIT_LAST_FEATURE; i++) {
924 u32 feature = AUDIT_FEATURE_TO_MASK(i);
925 u32 old_feature, new_feature, old_lock, new_lock;
926
927 /* if we are not changing this feature, move along */
928 if (!(feature & uaf->mask))
929 continue;
930
931 old_feature = af.features & feature;
932 new_feature = uaf->features & feature;
933 old_lock = af.lock & feature;
934 new_lock = (uaf->lock | af.lock) & feature;
935
936 if (new_feature != old_feature)
937 audit_log_feature_change(i, old_feature, new_feature,
938 old_lock, new_lock, 1);
939
940 if (new_feature)
941 af.features |= feature;
942 else
943 af.features &= ~feature;
944 af.lock |= new_lock;
945 }
946
947 return 0;
948}
949
Richard Guy Briggs133e1e52016-01-25 18:04:15 -0500950static int audit_replace(pid_t pid)
951{
952 struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0,
953 &pid, sizeof(pid));
954
955 if (!skb)
956 return -ENOMEM;
957 return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
961{
Eric Parisdc9eb692013-04-19 13:23:09 -0400962 u32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100965 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 u16 msg_type = nlh->nlmsg_type;
Al Viroe1396062006-05-25 10:19:47 -0400967 struct audit_sig_info *sig_data;
Eric Paris50397bd2008-01-07 18:14:19 -0500968 char *ctx = NULL;
Al Viroe1396062006-05-25 10:19:47 -0400969 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700971 err = audit_netlink_ok(skb, msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (err)
973 return err;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 seq = nlh->nlmsg_seq;
David S. Millerc64e66c2012-06-26 21:45:21 -0700976 data = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 switch (msg_type) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400979 case AUDIT_GET: {
980 struct audit_status s;
981 memset(&s, 0, sizeof(s));
982 s.enabled = audit_enabled;
983 s.failure = audit_failure;
984 s.pid = audit_pid;
985 s.rate_limit = audit_rate_limit;
986 s.backlog_limit = audit_backlog_limit;
987 s.lost = atomic_read(&audit_lost);
Paul Mooreaf8b8242016-11-29 16:53:24 -0500988 s.backlog = skb_queue_len(&audit_queue);
Richard Guy Briggs0288d712014-11-17 15:51:01 -0500989 s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL;
Paul Moore31975422016-11-29 16:53:25 -0500990 s.backlog_wait_time = audit_backlog_wait_time;
Eric W. Biederman6f285b12014-02-28 19:44:55 -0800991 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -0400993 }
994 case AUDIT_SET: {
995 struct audit_status s;
996 memset(&s, 0, sizeof(s));
997 /* guard against past and future API changes */
998 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
999 if (s.mask & AUDIT_STATUS_ENABLED) {
1000 err = audit_set_enabled(s.enabled);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001001 if (err < 0)
1002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001004 if (s.mask & AUDIT_STATUS_FAILURE) {
1005 err = audit_set_failure(s.failure);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001006 if (err < 0)
1007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001009 if (s.mask & AUDIT_STATUS_PID) {
1010 int new_pid = s.pid;
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001011 pid_t requesting_pid = task_tgid_vnr(current);
Eric Paris1a6b9f22008-01-07 17:09:31 -05001012
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001013 if ((!new_pid) && (requesting_pid != audit_pid)) {
1014 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs34eab0a2013-06-21 14:47:13 -04001015 return -EACCES;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001016 }
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001017 if (audit_pid && new_pid &&
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001018 audit_replace(requesting_pid) != -ECONNREFUSED) {
1019 audit_log_config_change("audit_pid", new_pid, audit_pid, 0);
Richard Guy Briggs133e1e52016-01-25 18:04:15 -05001020 return -EEXIST;
Richard Guy Briggs935c9e72016-01-25 18:04:15 -05001021 }
Eric Paris1a6b9f22008-01-07 17:09:31 -05001022 if (audit_enabled != AUDIT_OFF)
Eric Parisdc9eb692013-04-19 13:23:09 -04001023 audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001024 if (new_pid) {
1025 if (audit_sock)
1026 sock_put(audit_sock);
1027 audit_pid = new_pid;
1028 audit_nlk_portid = NETLINK_CB(skb).portid;
1029 sock_hold(skb->sk);
1030 audit_sock = skb->sk;
1031 } else {
Paul Moore6c54e782016-11-29 16:53:26 -05001032 auditd_reset();
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001033 }
Paul Mooree1d16622016-11-29 16:53:25 -05001034 wake_up_interruptible(&kauditd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001036 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
1037 err = audit_set_rate_limit(s.rate_limit);
zhangxiliang20c6aaa2008-07-31 10:11:19 +08001038 if (err < 0)
1039 return err;
1040 }
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001041 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001042 err = audit_set_backlog_limit(s.backlog_limit);
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001043 if (err < 0)
1044 return err;
1045 }
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001046 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
1047 if (sizeof(s) > (size_t)nlh->nlmsg_len)
1048 return -EINVAL;
Pranith Kumar724e7bf2015-03-11 14:08:19 -04001049 if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
Eric Paris3f0c5fa2014-01-13 16:49:28 -05001050 return -EINVAL;
1051 err = audit_set_backlog_wait_time(s.backlog_wait_time);
1052 if (err < 0)
1053 return err;
Richard Guy Briggs51cc83f2013-09-18 11:55:12 -04001054 }
Richard Guy Briggs92c82e82017-01-13 03:26:29 -05001055 if (s.mask == AUDIT_STATUS_LOST) {
1056 u32 lost = atomic_xchg(&audit_lost, 0);
1057
1058 audit_log_config_change("lost", 0, lost, 1);
1059 return lost;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
Richard Guy Briggs09f883a2013-09-18 09:32:24 -04001062 }
Eric Parisb0fed402013-05-22 12:54:49 -04001063 case AUDIT_GET_FEATURE:
1064 err = audit_get_feature(skb);
1065 if (err)
1066 return err;
1067 break;
1068 case AUDIT_SET_FEATURE:
1069 err = audit_set_feature(skb);
1070 if (err)
1071 return err;
1072 break;
Steve Grubb05474102005-05-21 00:18:37 +01001073 case AUDIT_USER:
Robert P. J. Day039b6b32007-05-08 00:29:20 -07001074 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1075 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +01001076 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
1077 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001078
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001079 err = audit_filter(msg_type, AUDIT_FILTER_USER);
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001080 if (err == 1) { /* match or error */
David Woodhouse4a4cd632005-06-22 14:56:47 +01001081 err = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001082 if (msg_type == AUDIT_USER_TTY) {
Peter Hurley37282a72016-01-09 22:55:31 -08001083 err = tty_audit_push();
Miloslav Trmac522ed772007-07-15 23:40:56 -07001084 if (err)
1085 break;
1086 }
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001087 mutex_unlock(&audit_cmd_mutex);
Eric Parisdc9eb692013-04-19 13:23:09 -04001088 audit_log_common_recv_msg(&ab, msg_type);
Eric Paris50397bd2008-01-07 18:14:19 -05001089 if (msg_type != AUDIT_USER_TTY)
Richard Guy Briggsb50eba72013-09-16 18:20:42 -04001090 audit_log_format(ab, " msg='%.*s'",
1091 AUDIT_MESSAGE_TEXT_MAX,
Eric Paris50397bd2008-01-07 18:14:19 -05001092 (char *)data);
1093 else {
1094 int size;
1095
Eric Parisf7616102013-04-11 11:25:00 -04001096 audit_log_format(ab, " data=");
Eric Paris50397bd2008-01-07 18:14:19 -05001097 size = nlmsg_len(nlh);
Miloslav Trmac55ad2f82009-03-19 09:52:47 -04001098 if (size > 0 &&
1099 ((unsigned char *)data)[size - 1] == '\0')
1100 size--;
Eric Parisb556f8a2008-04-18 10:12:59 -04001101 audit_log_n_untrustedstring(ab, data, size);
David Woodhouse4a4cd632005-06-22 14:56:47 +01001102 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001103 audit_set_portid(ab, NETLINK_CB(skb).portid);
Eric Paris50397bd2008-01-07 18:14:19 -05001104 audit_log_end(ab);
Richard Guy Briggs1b7b5332013-12-02 11:33:01 -05001105 mutex_lock(&audit_cmd_mutex);
David Woodhouse0f45aa12005-06-19 19:35:50 +01001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001108 case AUDIT_ADD_RULE:
1109 case AUDIT_DEL_RULE:
1110 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
1111 return -EINVAL;
Eric Paris1a6b9f22008-01-07 17:09:31 -05001112 if (audit_enabled == AUDIT_LOCKED) {
Eric Parisdc9eb692013-04-19 13:23:09 -04001113 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1114 audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled);
Eric Paris50397bd2008-01-07 18:14:19 -05001115 audit_log_end(ab);
Steve Grubb6a01b07f2007-01-19 14:39:55 -05001116 return -EPERM;
1117 }
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001118 err = audit_rule_change(msg_type, NETLINK_CB(skb).portid,
Eric Parisdc9eb692013-04-19 13:23:09 -04001119 seq, data, nlmsg_len(nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001121 case AUDIT_LIST_RULES:
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001122 err = audit_list_rules_send(skb, seq);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001123 break;
Al Viro74c3cbe2007-07-22 08:04:18 -04001124 case AUDIT_TRIM:
1125 audit_trim_trees();
Eric Parisdc9eb692013-04-19 13:23:09 -04001126 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Al Viro74c3cbe2007-07-22 08:04:18 -04001127 audit_log_format(ab, " op=trim res=1");
1128 audit_log_end(ab);
1129 break;
1130 case AUDIT_MAKE_EQUIV: {
1131 void *bufp = data;
1132 u32 sizes[2];
Harvey Harrison7719e432008-04-27 02:39:56 -07001133 size_t msglen = nlmsg_len(nlh);
Al Viro74c3cbe2007-07-22 08:04:18 -04001134 char *old, *new;
1135
1136 err = -EINVAL;
Harvey Harrison7719e432008-04-27 02:39:56 -07001137 if (msglen < 2 * sizeof(u32))
Al Viro74c3cbe2007-07-22 08:04:18 -04001138 break;
1139 memcpy(sizes, bufp, 2 * sizeof(u32));
1140 bufp += 2 * sizeof(u32);
Harvey Harrison7719e432008-04-27 02:39:56 -07001141 msglen -= 2 * sizeof(u32);
1142 old = audit_unpack_string(&bufp, &msglen, sizes[0]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001143 if (IS_ERR(old)) {
1144 err = PTR_ERR(old);
1145 break;
1146 }
Harvey Harrison7719e432008-04-27 02:39:56 -07001147 new = audit_unpack_string(&bufp, &msglen, sizes[1]);
Al Viro74c3cbe2007-07-22 08:04:18 -04001148 if (IS_ERR(new)) {
1149 err = PTR_ERR(new);
1150 kfree(old);
1151 break;
1152 }
1153 /* OK, here comes... */
1154 err = audit_tag_tree(old, new);
1155
Eric Parisdc9eb692013-04-19 13:23:09 -04001156 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris50397bd2008-01-07 18:14:19 -05001157
Al Viro74c3cbe2007-07-22 08:04:18 -04001158 audit_log_format(ab, " op=make_equiv old=");
1159 audit_log_untrustedstring(ab, old);
1160 audit_log_format(ab, " new=");
1161 audit_log_untrustedstring(ab, new);
1162 audit_log_format(ab, " res=%d", !err);
1163 audit_log_end(ab);
1164 kfree(old);
1165 kfree(new);
1166 break;
1167 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001168 case AUDIT_SIGNAL_INFO:
Eric Paris939cbf22009-09-23 13:46:00 -04001169 len = 0;
1170 if (audit_sig_sid) {
1171 err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
1172 if (err)
1173 return err;
1174 }
Al Viroe1396062006-05-25 10:19:47 -04001175 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
1176 if (!sig_data) {
Eric Paris939cbf22009-09-23 13:46:00 -04001177 if (audit_sig_sid)
1178 security_release_secctx(ctx, len);
Al Viroe1396062006-05-25 10:19:47 -04001179 return -ENOMEM;
1180 }
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001181 sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid);
Al Viroe1396062006-05-25 10:19:47 -04001182 sig_data->pid = audit_sig_pid;
Eric Paris939cbf22009-09-23 13:46:00 -04001183 if (audit_sig_sid) {
1184 memcpy(sig_data->ctx, ctx, len);
1185 security_release_secctx(ctx, len);
1186 }
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001187 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
1188 sig_data, sizeof(*sig_data) + len);
Al Viroe1396062006-05-25 10:19:47 -04001189 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001190 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001191 case AUDIT_TTY_GET: {
1192 struct audit_tty_status s;
Peter Hurley2e28d382016-01-09 22:55:33 -08001193 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001194
Peter Hurley2e28d382016-01-09 22:55:33 -08001195 t = READ_ONCE(current->signal->audit_tty);
1196 s.enabled = t & AUDIT_TTY_ENABLE;
1197 s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Thomas Gleixner20703202009-12-09 14:19:35 +00001198
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001199 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
Miloslav Trmac522ed772007-07-15 23:40:56 -07001200 break;
1201 }
1202 case AUDIT_TTY_SET: {
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001203 struct audit_tty_status s, old;
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001204 struct audit_buffer *ab;
Peter Hurley2e28d382016-01-09 22:55:33 -08001205 unsigned int t;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001206
Richard Guy Briggs46e959e2013-05-03 14:03:50 -04001207 memset(&s, 0, sizeof(s));
1208 /* guard against past and future API changes */
Mathias Krause4d8fe732013-09-30 22:04:25 +02001209 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
Eric Paris0e23bac2014-01-13 21:12:34 -05001210 /* check if new data is valid */
1211 if ((s.enabled != 0 && s.enabled != 1) ||
1212 (s.log_passwd != 0 && s.log_passwd != 1))
1213 err = -EINVAL;
1214
Peter Hurley2e28d382016-01-09 22:55:33 -08001215 if (err)
1216 t = READ_ONCE(current->signal->audit_tty);
1217 else {
1218 t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD);
1219 t = xchg(&current->signal->audit_tty, t);
Eric Paris0e23bac2014-01-13 21:12:34 -05001220 }
Peter Hurley2e28d382016-01-09 22:55:33 -08001221 old.enabled = t & AUDIT_TTY_ENABLE;
1222 old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD);
Eric Paris0e23bac2014-01-13 21:12:34 -05001223
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001224 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
Eric Paris1ce319f2014-01-13 21:16:59 -05001225 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1226 " old-log_passwd=%d new-log_passwd=%d res=%d",
1227 old.enabled, s.enabled, old.log_passwd,
1228 s.log_passwd, !err);
Richard Guy Briggsa06e56b2013-11-15 11:29:02 -05001229 audit_log_end(ab);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001230 break;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 default:
1233 err = -EINVAL;
1234 break;
1235 }
1236
1237 return err < 0 ? err : 0;
1238}
1239
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001240/*
Eric Parisea7ae602009-06-11 14:31:35 -04001241 * Get message from skb. Each message is processed by audit_receive_msg.
1242 * Malformed skbs with wrong length are discarded silently.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001243 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001244static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Eric Parisea7ae602009-06-11 14:31:35 -04001246 struct nlmsghdr *nlh;
1247 /*
Hong zhi guo94191212013-03-27 06:49:06 +00001248 * len MUST be signed for nlmsg_next to be able to dec it below 0
Eric Parisea7ae602009-06-11 14:31:35 -04001249 * if the nlmsg_len was not aligned
1250 */
1251 int len;
1252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Eric Parisea7ae602009-06-11 14:31:35 -04001254 nlh = nlmsg_hdr(skb);
1255 len = skb->len;
1256
Hong zhi guo94191212013-03-27 06:49:06 +00001257 while (nlmsg_ok(nlh, len)) {
Eric Parisea7ae602009-06-11 14:31:35 -04001258 err = audit_receive_msg(skb, nlh);
1259 /* if err or if this message says it wants a response */
1260 if (err || (nlh->nlmsg_flags & NLM_F_ACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 netlink_ack(skb, nlh, err);
Eric Parisea7ae602009-06-11 14:31:35 -04001262
Alexandru Copot2851da52013-03-28 23:31:29 +02001263 nlh = nlmsg_next(nlh, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267/* Receive messages from netlink socket. */
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001268static void audit_receive(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Amy Griffisf368c07d2006-04-07 16:55:56 -04001270 mutex_lock(&audit_cmd_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001271 audit_receive_skb(skb);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001272 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001275/* Run custom bind function on netlink socket group connect or bind requests. */
Johannes Berg023e2cf2014-12-23 21:00:06 +01001276static int audit_bind(struct net *net, int group)
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001277{
1278 if (!capable(CAP_AUDIT_READ))
1279 return -EPERM;
1280
1281 return 0;
1282}
1283
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001284static int __net_init audit_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001286 struct netlink_kernel_cfg cfg = {
1287 .input = audit_receive,
Richard Guy Briggs3a101b82014-04-22 21:31:56 -04001288 .bind = audit_bind,
Richard Guy Briggs451f9212014-04-22 21:31:57 -04001289 .flags = NL_CFG_F_NONROOT_RECV,
1290 .groups = AUDIT_NLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001291 };
Amy Griffisf368c07d2006-04-07 16:55:56 -04001292
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001293 struct audit_net *aunet = net_generic(net, audit_net_id);
1294
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001295 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
Gao feng11ee39e2013-12-17 11:10:41 +08001296 if (aunet->nlsk == NULL) {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001297 audit_panic("cannot initialize netlink socket in namespace");
Gao feng11ee39e2013-12-17 11:10:41 +08001298 return -ENOMEM;
1299 }
1300 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001301 return 0;
1302}
1303
1304static void __net_exit audit_net_exit(struct net *net)
1305{
1306 struct audit_net *aunet = net_generic(net, audit_net_id);
1307 struct sock *sock = aunet->nlsk;
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001308 mutex_lock(&audit_cmd_mutex);
Paul Moorec6480202016-11-29 16:53:25 -05001309 if (sock == audit_sock)
1310 auditd_reset();
Richard Guy Briggs533c7b62016-12-13 10:03:01 -05001311 mutex_unlock(&audit_cmd_mutex);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001312
Monam Agarwale231d542014-03-24 00:16:19 +05301313 RCU_INIT_POINTER(aunet->nlsk, NULL);
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001314 synchronize_net();
1315 netlink_kernel_release(sock);
1316}
1317
Richard Guy Briggs86268772013-07-16 13:18:45 -04001318static struct pernet_operations audit_net_ops __net_initdata = {
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001319 .init = audit_net_init,
1320 .exit = audit_net_exit,
1321 .id = &audit_net_id,
1322 .size = sizeof(struct audit_net),
1323};
1324
1325/* Initialize audit support at boot time. */
1326static int __init audit_init(void)
1327{
1328 int i;
1329
Eric Parisa3f07112008-11-05 12:47:09 -05001330 if (audit_initialized == AUDIT_DISABLED)
1331 return 0;
1332
Joe Perchesd957f7b2014-01-14 10:33:12 -08001333 pr_info("initializing netlink subsys (%s)\n",
1334 audit_default ? "enabled" : "disabled");
Richard Guy Briggs33faba72013-07-16 13:18:45 -04001335 register_pernet_subsys(&audit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Paul Mooreaf8b8242016-11-29 16:53:24 -05001337 skb_queue_head_init(&audit_queue);
Paul Moorec6480202016-11-29 16:53:25 -05001338 skb_queue_head_init(&audit_retry_queue);
Paul Mooreaf8b8242016-11-29 16:53:24 -05001339 skb_queue_head_init(&audit_hold_queue);
Eric Parisa3f07112008-11-05 12:47:09 -05001340 audit_initialized = AUDIT_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 audit_enabled = audit_default;
Eric Parisb593d382008-01-08 17:38:31 -05001342 audit_ever_enabled |= !!audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001343
Amy Griffisf368c07d2006-04-07 16:55:56 -04001344 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
1345 INIT_LIST_HEAD(&audit_inode_hash[i]);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001346
Paul Moore6c925562016-11-29 16:53:23 -05001347 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
1348 if (IS_ERR(kauditd_task)) {
1349 int err = PTR_ERR(kauditd_task);
1350 panic("audit: failed to start the kauditd thread (%d)\n", err);
1351 }
1352
Steve Grubb7c397d02016-12-14 15:59:46 -05001353 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL,
1354 "state=initialized audit_enabled=%u res=1",
1355 audit_enabled);
Paul Moore6c925562016-11-29 16:53:23 -05001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359__initcall(audit_init);
1360
1361/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
1362static int __init audit_enable(char *str)
1363{
1364 audit_default = !!simple_strtol(str, NULL, 0);
Eric Parisa3f07112008-11-05 12:47:09 -05001365 if (!audit_default)
1366 audit_initialized = AUDIT_DISABLED;
1367
Joe Perchesd957f7b2014-01-14 10:33:12 -08001368 pr_info("%s\n", audit_default ?
Gao fengd3ca0342013-10-31 14:31:01 +08001369 "enabled (after initialization)" : "disabled (until reboot)");
Eric Parisa3f07112008-11-05 12:47:09 -05001370
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001371 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373__setup("audit=", audit_enable);
1374
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001375/* Process kernel command-line parameter at boot time.
1376 * audit_backlog_limit=<n> */
1377static int __init audit_backlog_limit_set(char *str)
1378{
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001379 u32 audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001380
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001381 pr_info("audit_backlog_limit: ");
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001382 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1383 pr_cont("using default of %u, unable to parse %s\n",
Joe Perchesd957f7b2014-01-14 10:33:12 -08001384 audit_backlog_limit, str);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001385 return 1;
1386 }
Joe Perches3e1d0bb2014-01-14 10:33:13 -08001387
1388 audit_backlog_limit = audit_backlog_limit_arg;
Joe Perchesd957f7b2014-01-14 10:33:12 -08001389 pr_cont("%d\n", audit_backlog_limit);
Richard Guy Briggsf910fde2013-09-17 12:34:52 -04001390
1391 return 1;
1392}
1393__setup("audit_backlog_limit=", audit_backlog_limit_set);
1394
Chris Wright16e19042005-05-06 15:53:34 +01001395static void audit_buffer_free(struct audit_buffer *ab)
1396{
1397 unsigned long flags;
1398
Chris Wright8fc61152005-05-06 15:54:17 +01001399 if (!ab)
1400 return;
1401
Markus Elfringd865e572016-01-13 09:18:55 -05001402 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +01001403 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001404 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +01001405 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001406 else {
1407 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +01001408 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -05001409 }
Chris Wright16e19042005-05-06 15:53:34 +01001410 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1411}
1412
Steve Grubbc0404992005-05-13 18:17:42 +01001413static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +01001414 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +01001415{
1416 unsigned long flags;
1417 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +01001418 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +01001419
1420 spin_lock_irqsave(&audit_freelist_lock, flags);
1421 if (!list_empty(&audit_freelist)) {
1422 ab = list_entry(audit_freelist.next,
1423 struct audit_buffer, list);
1424 list_del(&ab->list);
1425 --audit_freelist_count;
1426 }
1427 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1428
1429 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +01001430 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +01001431 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +01001432 goto err;
Chris Wright16e19042005-05-06 15:53:34 +01001433 }
Chris Wright8fc61152005-05-06 15:54:17 +01001434
David Woodhouseb7d11252005-05-19 10:56:58 +01001435 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001436 ab->gfp_mask = gfp_mask;
Eric Parisee080e62009-06-11 14:31:35 -04001437
1438 ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
1439 if (!ab->skb)
David S. Millerc64e66c2012-06-26 21:45:21 -07001440 goto err;
Eric Parisee080e62009-06-11 14:31:35 -04001441
David S. Millerc64e66c2012-06-26 21:45:21 -07001442 nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0);
1443 if (!nlh)
1444 goto out_kfree_skb;
Eric Parisee080e62009-06-11 14:31:35 -04001445
Chris Wright16e19042005-05-06 15:53:34 +01001446 return ab;
Eric Parisee080e62009-06-11 14:31:35 -04001447
David S. Millerc64e66c2012-06-26 21:45:21 -07001448out_kfree_skb:
Eric Parisee080e62009-06-11 14:31:35 -04001449 kfree_skb(ab->skb);
1450 ab->skb = NULL;
Chris Wright8fc61152005-05-06 15:54:17 +01001451err:
1452 audit_buffer_free(ab);
1453 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +01001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001456/**
1457 * audit_serial - compute a serial number for the audit record
1458 *
1459 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +01001460 * written to user-space as soon as they are generated, so a complete
1461 * audit record may be written in several pieces. The timestamp of the
1462 * record and this serial number are used by the user-space tools to
1463 * determine which pieces belong to the same audit record. The
1464 * (timestamp,serial) tuple is unique for each syscall and is live from
1465 * syscall entry to syscall exit.
1466 *
David Woodhousebfb44962005-05-21 21:08:09 +01001467 * NOTE: Another possibility is to store the formatted records off the
1468 * audit context (for those records that have a context), and emit them
1469 * all at syscall exit. However, this could delay the reporting of
1470 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001471 * halts).
1472 */
David Woodhousebfb44962005-05-21 21:08:09 +01001473unsigned int audit_serial(void)
1474{
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001475 static atomic_t serial = ATOMIC_INIT(0);
David Woodhousebfb44962005-05-21 21:08:09 +01001476
Richard Guy Briggs01478d72014-06-13 18:22:00 -04001477 return atomic_add_return(1, &serial);
David Woodhousebfb44962005-05-21 21:08:09 +01001478}
1479
Daniel Walker5600b892007-10-18 03:06:10 -07001480static inline void audit_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001481 struct timespec *t, unsigned int *serial)
1482{
Al Viro48887e62008-12-06 01:05:50 -05001483 if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
David Woodhousebfb44962005-05-21 21:08:09 +01001484 *t = CURRENT_TIME;
1485 *serial = audit_serial();
1486 }
1487}
1488
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001489/**
1490 * audit_log_start - obtain an audit buffer
1491 * @ctx: audit_context (may be NULL)
1492 * @gfp_mask: type of allocation
1493 * @type: audit message type
1494 *
1495 * Returns audit_buffer pointer on success or NULL on error.
1496 *
1497 * Obtain an audit buffer. This routine does locking to obtain the
1498 * audit buffer, but then no locking is required for calls to
1499 * audit_log_*format. If the task (ctx) is a task that is currently in a
1500 * syscall, then the syscall is marked as auditable and an audit record
1501 * will be written at syscall exit. If there is no associated task, then
1502 * task context (ctx) should be NULL.
1503 */
Al Viro9796fdd2005-10-21 03:22:03 -04001504struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001505 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Paul Moore31975422016-11-29 16:53:25 -05001507 struct audit_buffer *ab;
1508 struct timespec t;
1509 unsigned int uninitialized_var(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Eric Parisa3f07112008-11-05 12:47:09 -05001511 if (audit_initialized != AUDIT_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return NULL;
1513
Richard Guy Briggs86b2efb2016-06-24 16:35:46 -04001514 if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE)))
Dustin Kirklandc8edc802005-11-03 16:12:36 +00001515 return NULL;
1516
Paul Moorea09cfa42016-11-29 16:53:26 -05001517 /* don't ever fail/sleep on these two conditions:
1518 * 1. auditd generated record - since we need auditd to drain the
1519 * queue; also, when we are checking for auditd, compare PIDs using
1520 * task_tgid_vnr() since auditd_pid is set in audit_receive_msg()
1521 * using a PID anchored in the caller's namespace
1522 * 2. audit command message - record types 1000 through 1099 inclusive
1523 * are command messages/records used to manage the kernel subsystem
1524 * and the audit userspace, blocking on these messages could cause
1525 * problems under load so don't do it (note: not all of these
1526 * command types are valid as record types, but it is quicker to
1527 * just check two ints than a series of ints in a if/switch stmt) */
1528 if (!((audit_pid && audit_pid == task_tgid_vnr(current)) ||
1529 (type >= 1000 && type <= 1099))) {
Paul Moore31975422016-11-29 16:53:25 -05001530 long sleep_time = audit_backlog_wait_time;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001531
Paul Moore31975422016-11-29 16:53:25 -05001532 while (audit_backlog_limit &&
1533 (skb_queue_len(&audit_queue) > audit_backlog_limit)) {
1534 /* wake kauditd to try and flush the queue */
1535 wake_up_interruptible(&kauditd_wait);
David Woodhouseac4cec42005-07-02 14:08:48 +01001536
Paul Moore31975422016-11-29 16:53:25 -05001537 /* sleep if we are allowed and we haven't exhausted our
1538 * backlog wait limit */
1539 if ((gfp_mask & __GFP_DIRECT_RECLAIM) &&
1540 (sleep_time > 0)) {
1541 DECLARE_WAITQUEUE(wait, current);
1542
1543 add_wait_queue_exclusive(&audit_backlog_wait,
1544 &wait);
1545 set_current_state(TASK_UNINTERRUPTIBLE);
1546 sleep_time = schedule_timeout(sleep_time);
1547 remove_wait_queue(&audit_backlog_wait, &wait);
1548 } else {
1549 if (audit_rate_check() && printk_ratelimit())
1550 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1551 skb_queue_len(&audit_queue),
1552 audit_backlog_limit);
1553 audit_log_lost("backlog limit exceeded");
1554 return NULL;
Konstantin Khlebnikov8ac1c8d2013-09-24 15:27:42 -07001555 }
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001556 }
David Woodhousefb19b4c2005-05-19 14:55:56 +01001557 }
1558
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001559 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!ab) {
1561 audit_log_lost("out of memory in audit_log_start");
1562 return NULL;
1563 }
1564
David Woodhousebfb44962005-05-21 21:08:09 +01001565 audit_get_stamp(ab->ctx, &t, &serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
1567 t.tv_sec, t.tv_nsec/1000000, serial);
Paul Moore31975422016-11-29 16:53:25 -05001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return ab;
1570}
1571
Chris Wright8fc61152005-05-06 15:54:17 +01001572/**
Chris Wright5ac52f32005-05-06 15:54:53 +01001573 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +01001574 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001575 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +01001576 *
1577 * Returns 0 (no space) on failed expansion, or available space if
1578 * successful.
1579 */
David Woodhousee3b926b2005-05-10 18:56:08 +01001580static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +01001581{
Chris Wright5ac52f32005-05-06 15:54:53 +01001582 struct sk_buff *skb = ab->skb;
Herbert Xu406a1d82008-01-28 20:47:09 -08001583 int oldtail = skb_tailroom(skb);
1584 int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1585 int newtail = skb_tailroom(skb);
1586
Chris Wright5ac52f32005-05-06 15:54:53 +01001587 if (ret < 0) {
1588 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +01001589 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +01001590 }
Herbert Xu406a1d82008-01-28 20:47:09 -08001591
1592 skb->truesize += newtail - oldtail;
1593 return newtail;
Chris Wright8fc61152005-05-06 15:54:17 +01001594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001596/*
1597 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * room in the audit buffer, more room will be allocated and vsnprint
1599 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001600 * can't format message larger than 1024 bytes, so we don't either.
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1603 va_list args)
1604{
1605 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +01001606 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +01001607 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 if (!ab)
1610 return;
1611
Chris Wright5ac52f32005-05-06 15:54:53 +01001612 BUG_ON(!ab->skb);
1613 skb = ab->skb;
1614 avail = skb_tailroom(skb);
1615 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +01001616 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +01001617 if (!avail)
1618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
David Woodhouseeecb0a72005-05-10 18:58:51 +01001620 va_copy(args2, args);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001621 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (len >= avail) {
1623 /* The printk buffer is 1024 bytes long, so if we get
1624 * here and AUDIT_BUFSIZ is at least 1024, then we can
1625 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001626 avail = audit_expand(ab,
1627 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +01001628 if (!avail)
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001629 goto out_va_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001630 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Steve Grubb168b7172005-05-19 10:24:22 +01001632 if (len > 0)
1633 skb_put(skb, len);
Jesper Juhla0e86bd2012-01-08 22:44:29 +01001634out_va_end:
1635 va_end(args2);
Chris Wright8fc61152005-05-06 15:54:17 +01001636out:
1637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001640/**
1641 * audit_log_format - format a message into the audit buffer.
1642 * @ab: audit_buffer
1643 * @fmt: format string
1644 * @...: optional parameters matching @fmt string
1645 *
1646 * All the work is done in audit_log_vformat.
1647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1649{
1650 va_list args;
1651
1652 if (!ab)
1653 return;
1654 va_start(args, fmt);
1655 audit_log_vformat(ab, fmt, args);
1656 va_end(args);
1657}
1658
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001659/**
1660 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1661 * @ab: the audit_buffer
1662 * @buf: buffer to convert to hex
1663 * @len: length of @buf to be converted
1664 *
1665 * No return value; failure to expand is silently ignored.
1666 *
1667 * This function will take the passed buf and convert it into a string of
1668 * ascii hex digits. The new string is placed onto the skb.
1669 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001670void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001671 size_t len)
83c7d092005-04-29 15:54:44 +01001672{
Steve Grubb168b7172005-05-19 10:24:22 +01001673 int i, avail, new_len;
1674 unsigned char *ptr;
1675 struct sk_buff *skb;
83c7d092005-04-29 15:54:44 +01001676
Amy Griffis8ef2d302006-09-07 17:03:02 -04001677 if (!ab)
1678 return;
1679
Steve Grubb168b7172005-05-19 10:24:22 +01001680 BUG_ON(!ab->skb);
1681 skb = ab->skb;
1682 avail = skb_tailroom(skb);
1683 new_len = len<<1;
1684 if (new_len >= avail) {
1685 /* Round the buffer request up to the next multiple */
1686 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1687 avail = audit_expand(ab, new_len);
1688 if (!avail)
1689 return;
1690 }
1691
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001692 ptr = skb_tail_pointer(skb);
Joe Perchesb8dbc322014-01-13 23:31:27 -08001693 for (i = 0; i < len; i++)
1694 ptr = hex_byte_pack_upper(ptr, buf[i]);
Steve Grubb168b7172005-05-19 10:24:22 +01001695 *ptr = 0;
1696 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001697}
1698
Amy Griffis9c937dc2006-06-08 23:19:31 -04001699/*
1700 * Format a string of no more than slen characters into the audit buffer,
1701 * enclosed in quote marks.
1702 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001703void audit_log_n_string(struct audit_buffer *ab, const char *string,
1704 size_t slen)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001705{
1706 int avail, new_len;
1707 unsigned char *ptr;
1708 struct sk_buff *skb;
1709
Amy Griffis8ef2d302006-09-07 17:03:02 -04001710 if (!ab)
1711 return;
1712
Amy Griffis9c937dc2006-06-08 23:19:31 -04001713 BUG_ON(!ab->skb);
1714 skb = ab->skb;
1715 avail = skb_tailroom(skb);
1716 new_len = slen + 3; /* enclosing quotes + null terminator */
1717 if (new_len > avail) {
1718 avail = audit_expand(ab, new_len);
1719 if (!avail)
1720 return;
1721 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001722 ptr = skb_tail_pointer(skb);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001723 *ptr++ = '"';
1724 memcpy(ptr, string, slen);
1725 ptr += slen;
1726 *ptr++ = '"';
1727 *ptr = 0;
1728 skb_put(skb, slen + 2); /* don't include null terminator */
1729}
1730
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001731/**
Eric Parisde6bbd12008-01-07 14:31:58 -05001732 * audit_string_contains_control - does a string need to be logged in hex
Dave Jonesf706d5d2008-03-28 14:15:56 -07001733 * @string: string to be checked
1734 * @len: max length of the string to check
Eric Parisde6bbd12008-01-07 14:31:58 -05001735 */
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001736bool audit_string_contains_control(const char *string, size_t len)
Eric Parisde6bbd12008-01-07 14:31:58 -05001737{
1738 const unsigned char *p;
Miloslav Trmacb3897f52009-03-19 09:48:27 -04001739 for (p = string; p < (const unsigned char *)string + len; p++) {
Vesa-Matti J Kari1d6c9642008-07-23 00:06:13 +03001740 if (*p == '"' || *p < 0x21 || *p > 0x7e)
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001741 return true;
Eric Parisde6bbd12008-01-07 14:31:58 -05001742 }
Yaowei Bai9fcf8362015-11-04 08:23:51 -05001743 return false;
Eric Parisde6bbd12008-01-07 14:31:58 -05001744}
1745
1746/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001747 * audit_log_n_untrustedstring - log a string that may contain random characters
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001748 * @ab: audit_buffer
Dave Jonesf706d5d2008-03-28 14:15:56 -07001749 * @len: length of string (not including trailing null)
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001750 * @string: string to be logged
1751 *
1752 * This code will escape a string that is passed to it if the string
1753 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001754 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001755 * Strings that are escaped are printed in hex (2 digits per char).
Amy Griffis9c937dc2006-06-08 23:19:31 -04001756 *
1757 * The caller specifies the number of characters in the string to log, which may
1758 * or may not be the entire string.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001759 */
Eric Parisb556f8a2008-04-18 10:12:59 -04001760void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
1761 size_t len)
83c7d092005-04-29 15:54:44 +01001762{
Eric Parisde6bbd12008-01-07 14:31:58 -05001763 if (audit_string_contains_control(string, len))
Eric Parisb556f8a2008-04-18 10:12:59 -04001764 audit_log_n_hex(ab, string, len);
Eric Parisde6bbd12008-01-07 14:31:58 -05001765 else
Eric Parisb556f8a2008-04-18 10:12:59 -04001766 audit_log_n_string(ab, string, len);
83c7d092005-04-29 15:54:44 +01001767}
1768
Amy Griffis9c937dc2006-06-08 23:19:31 -04001769/**
Miloslav Trmac522ed772007-07-15 23:40:56 -07001770 * audit_log_untrustedstring - log a string that may contain random characters
Amy Griffis9c937dc2006-06-08 23:19:31 -04001771 * @ab: audit_buffer
1772 * @string: string to be logged
1773 *
Miloslav Trmac522ed772007-07-15 23:40:56 -07001774 * Same as audit_log_n_untrustedstring(), except that strlen is used to
Amy Griffis9c937dc2006-06-08 23:19:31 -04001775 * determine string length.
1776 */
Eric Parisde6bbd12008-01-07 14:31:58 -05001777void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001778{
Eric Parisb556f8a2008-04-18 10:12:59 -04001779 audit_log_n_untrustedstring(ab, string, strlen(string));
Amy Griffis9c937dc2006-06-08 23:19:31 -04001780}
1781
Steve Grubb168b7172005-05-19 10:24:22 +01001782/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
Al Viro66b3fad2012-03-14 21:48:20 -04001784 const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Jan Blunck44707fd2008-02-14 19:38:33 -08001786 char *p, *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Chris Wright8fc61152005-05-06 15:54:17 +01001788 if (prefix)
Kees Cookc158a352012-01-06 14:07:10 -08001789 audit_log_format(ab, "%s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Steve Grubb168b7172005-05-19 10:24:22 +01001791 /* We will allow 11 spaces for ' (deleted)' to be appended */
Jan Blunck44707fd2008-02-14 19:38:33 -08001792 pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
1793 if (!pathname) {
Eric Parisdef57542009-03-10 18:00:14 -04001794 audit_log_string(ab, "<no_memory>");
Steve Grubb168b7172005-05-19 10:24:22 +01001795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Jan Blunckcf28b482008-02-14 19:38:44 -08001797 p = d_path(path, pathname, PATH_MAX+11);
Steve Grubb168b7172005-05-19 10:24:22 +01001798 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1799 /* FIXME: can we save some information here? */
Eric Parisdef57542009-03-10 18:00:14 -04001800 audit_log_string(ab, "<too_long>");
Daniel Walker5600b892007-10-18 03:06:10 -07001801 } else
Steve Grubb168b7172005-05-19 10:24:22 +01001802 audit_log_untrustedstring(ab, p);
Jan Blunck44707fd2008-02-14 19:38:33 -08001803 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Eric Paris4d3fb702013-04-30 09:53:34 -04001806void audit_log_session_info(struct audit_buffer *ab)
1807{
Eric Paris4440e852013-11-27 17:35:17 -05001808 unsigned int sessionid = audit_get_sessionid(current);
Eric Paris4d3fb702013-04-30 09:53:34 -04001809 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1810
Richard Guy Briggsb8f89ca2013-09-18 11:17:43 -04001811 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
Eric Paris4d3fb702013-04-30 09:53:34 -04001812}
1813
Eric Paris9d960982009-06-11 14:31:37 -04001814void audit_log_key(struct audit_buffer *ab, char *key)
1815{
1816 audit_log_format(ab, " key=");
1817 if (key)
1818 audit_log_untrustedstring(ab, key);
1819 else
1820 audit_log_format(ab, "(null)");
1821}
1822
Eric Parisb24a30a2013-04-30 15:30:32 -04001823void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1824{
1825 int i;
1826
1827 audit_log_format(ab, " %s=", prefix);
1828 CAP_FOR_EACH_U32(i) {
1829 audit_log_format(ab, "%08x",
Eric Paris7d8b6c62014-07-23 15:36:26 -04001830 cap->cap[CAP_LAST_U32 - i]);
Eric Parisb24a30a2013-04-30 15:30:32 -04001831 }
1832}
1833
Richard Guy Briggs691e6d52014-05-26 11:02:48 -04001834static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
Eric Parisb24a30a2013-04-30 15:30:32 -04001835{
1836 kernel_cap_t *perm = &name->fcap.permitted;
1837 kernel_cap_t *inh = &name->fcap.inheritable;
1838 int log = 0;
1839
1840 if (!cap_isclear(*perm)) {
1841 audit_log_cap(ab, "cap_fp", perm);
1842 log = 1;
1843 }
1844 if (!cap_isclear(*inh)) {
1845 audit_log_cap(ab, "cap_fi", inh);
1846 log = 1;
1847 }
1848
1849 if (log)
1850 audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1851 name->fcap.fE, name->fcap_ver);
1852}
1853
1854static inline int audit_copy_fcaps(struct audit_names *name,
1855 const struct dentry *dentry)
1856{
1857 struct cpu_vfs_cap_data caps;
1858 int rc;
1859
1860 if (!dentry)
1861 return 0;
1862
1863 rc = get_vfs_caps_from_disk(dentry, &caps);
1864 if (rc)
1865 return rc;
1866
1867 name->fcap.permitted = caps.permitted;
1868 name->fcap.inheritable = caps.inheritable;
1869 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1870 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1871 VFS_CAP_REVISION_SHIFT;
1872
1873 return 0;
1874}
1875
1876/* Copy inode data into an audit_names. */
1877void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001878 struct inode *inode)
Eric Parisb24a30a2013-04-30 15:30:32 -04001879{
1880 name->ino = inode->i_ino;
1881 name->dev = inode->i_sb->s_dev;
1882 name->mode = inode->i_mode;
1883 name->uid = inode->i_uid;
1884 name->gid = inode->i_gid;
1885 name->rdev = inode->i_rdev;
1886 security_inode_getsecid(inode, &name->osid);
1887 audit_copy_fcaps(name, dentry);
1888}
1889
1890/**
1891 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1892 * @context: audit_context for the task
1893 * @n: audit_names structure with reportable details
1894 * @path: optional path to report instead of audit_names->name
1895 * @record_num: record number to report when handling a list of names
1896 * @call_panic: optional pointer to int that will be updated if secid fails
1897 */
1898void audit_log_name(struct audit_context *context, struct audit_names *n,
1899 struct path *path, int record_num, int *call_panic)
1900{
1901 struct audit_buffer *ab;
1902 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1903 if (!ab)
1904 return;
1905
1906 audit_log_format(ab, "item=%d", record_num);
1907
1908 if (path)
1909 audit_log_d_path(ab, " name=", path);
1910 else if (n->name) {
1911 switch (n->name_len) {
1912 case AUDIT_NAME_FULL:
1913 /* log the full path */
1914 audit_log_format(ab, " name=");
1915 audit_log_untrustedstring(ab, n->name->name);
1916 break;
1917 case 0:
1918 /* name was specified as a relative path and the
1919 * directory component is the cwd */
1920 audit_log_d_path(ab, " name=", &context->pwd);
1921 break;
1922 default:
1923 /* log the name's directory component */
1924 audit_log_format(ab, " name=");
1925 audit_log_n_untrustedstring(ab, n->name->name,
1926 n->name_len);
1927 }
1928 } else
1929 audit_log_format(ab, " name=(null)");
1930
Linus Torvalds425afcf2015-09-08 13:34:59 -07001931 if (n->ino != AUDIT_INO_UNSET)
Eric Parisb24a30a2013-04-30 15:30:32 -04001932 audit_log_format(ab, " inode=%lu"
1933 " dev=%02x:%02x mode=%#ho"
1934 " ouid=%u ogid=%u rdev=%02x:%02x",
1935 n->ino,
1936 MAJOR(n->dev),
1937 MINOR(n->dev),
1938 n->mode,
1939 from_kuid(&init_user_ns, n->uid),
1940 from_kgid(&init_user_ns, n->gid),
1941 MAJOR(n->rdev),
1942 MINOR(n->rdev));
Eric Parisb24a30a2013-04-30 15:30:32 -04001943 if (n->osid != 0) {
1944 char *ctx = NULL;
1945 u32 len;
1946 if (security_secid_to_secctx(
1947 n->osid, &ctx, &len)) {
1948 audit_log_format(ab, " osid=%u", n->osid);
1949 if (call_panic)
1950 *call_panic = 2;
1951 } else {
1952 audit_log_format(ab, " obj=%s", ctx);
1953 security_release_secctx(ctx, len);
1954 }
1955 }
1956
Jeff Laytond3aea842013-05-08 10:32:23 -04001957 /* log the audit_names record type */
1958 audit_log_format(ab, " nametype=");
1959 switch(n->type) {
1960 case AUDIT_TYPE_NORMAL:
1961 audit_log_format(ab, "NORMAL");
1962 break;
1963 case AUDIT_TYPE_PARENT:
1964 audit_log_format(ab, "PARENT");
1965 break;
1966 case AUDIT_TYPE_CHILD_DELETE:
1967 audit_log_format(ab, "DELETE");
1968 break;
1969 case AUDIT_TYPE_CHILD_CREATE:
1970 audit_log_format(ab, "CREATE");
1971 break;
1972 default:
1973 audit_log_format(ab, "UNKNOWN");
1974 break;
1975 }
1976
Eric Parisb24a30a2013-04-30 15:30:32 -04001977 audit_log_fcaps(ab, n);
1978 audit_log_end(ab);
1979}
1980
1981int audit_log_task_context(struct audit_buffer *ab)
1982{
1983 char *ctx = NULL;
1984 unsigned len;
1985 int error;
1986 u32 sid;
1987
1988 security_task_getsecid(current, &sid);
1989 if (!sid)
1990 return 0;
1991
1992 error = security_secid_to_secctx(sid, &ctx, &len);
1993 if (error) {
1994 if (error != -EINVAL)
1995 goto error_path;
1996 return 0;
1997 }
1998
1999 audit_log_format(ab, " subj=%s", ctx);
2000 security_release_secctx(ctx, len);
2001 return 0;
2002
2003error_path:
2004 audit_panic("error in audit_log_task_context");
2005 return error;
2006}
2007EXPORT_SYMBOL(audit_log_task_context);
2008
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002009void audit_log_d_path_exe(struct audit_buffer *ab,
2010 struct mm_struct *mm)
2011{
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002012 struct file *exe_file;
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002013
Davidlohr Bueso5b282552015-02-22 18:20:09 -08002014 if (!mm)
2015 goto out_null;
2016
2017 exe_file = get_mm_exe_file(mm);
2018 if (!exe_file)
2019 goto out_null;
2020
2021 audit_log_d_path(ab, " exe=", &exe_file->f_path);
2022 fput(exe_file);
2023 return;
2024out_null:
2025 audit_log_format(ab, " exe=(null)");
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002026}
2027
Richard Guy Briggs3f5be2d2016-06-28 12:07:50 -04002028struct tty_struct *audit_get_tty(struct task_struct *tsk)
2029{
2030 struct tty_struct *tty = NULL;
2031 unsigned long flags;
2032
2033 spin_lock_irqsave(&tsk->sighand->siglock, flags);
2034 if (tsk->signal)
2035 tty = tty_kref_get(tsk->signal->tty);
2036 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2037 return tty;
2038}
2039
2040void audit_put_tty(struct tty_struct *tty)
2041{
2042 tty_kref_put(tty);
2043}
2044
Eric Parisb24a30a2013-04-30 15:30:32 -04002045void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2046{
2047 const struct cred *cred;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002048 char comm[sizeof(tsk->comm)];
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002049 struct tty_struct *tty;
Eric Parisb24a30a2013-04-30 15:30:32 -04002050
2051 if (!ab)
2052 return;
2053
2054 /* tsk == current */
2055 cred = current_cred();
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002056 tty = audit_get_tty(tsk);
Eric Parisb24a30a2013-04-30 15:30:32 -04002057 audit_log_format(ab,
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002058 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Eric Parisb24a30a2013-04-30 15:30:32 -04002059 " euid=%u suid=%u fsuid=%u"
Richard Guy Briggs2f2ad102013-07-15 10:23:11 -04002060 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -05002061 task_ppid_nr(tsk),
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002062 task_pid_nr(tsk),
Eric Parisb24a30a2013-04-30 15:30:32 -04002063 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2064 from_kuid(&init_user_ns, cred->uid),
2065 from_kgid(&init_user_ns, cred->gid),
2066 from_kuid(&init_user_ns, cred->euid),
2067 from_kuid(&init_user_ns, cred->suid),
2068 from_kuid(&init_user_ns, cred->fsuid),
2069 from_kgid(&init_user_ns, cred->egid),
2070 from_kgid(&init_user_ns, cred->sgid),
2071 from_kgid(&init_user_ns, cred->fsgid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04002072 tty ? tty_name(tty) : "(none)",
2073 audit_get_sessionid(tsk));
2074 audit_put_tty(tty);
Eric Parisb24a30a2013-04-30 15:30:32 -04002075 audit_log_format(ab, " comm=");
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002076 audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002077 audit_log_d_path_exe(ab, tsk->mm);
Eric Parisb24a30a2013-04-30 15:30:32 -04002078 audit_log_task_context(ab);
2079}
2080EXPORT_SYMBOL(audit_log_task_info);
2081
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002082/**
Kees Cooka51d9ea2012-07-25 17:29:08 -07002083 * audit_log_link_denied - report a link restriction denial
Shailendra Verma22011962015-05-23 10:40:27 +05302084 * @operation: specific link operation
Kees Cooka51d9ea2012-07-25 17:29:08 -07002085 * @link: the path that triggered the restriction
2086 */
2087void audit_log_link_denied(const char *operation, struct path *link)
2088{
2089 struct audit_buffer *ab;
Eric Parisb24a30a2013-04-30 15:30:32 -04002090 struct audit_names *name;
Kees Cooka51d9ea2012-07-25 17:29:08 -07002091
Eric Parisb24a30a2013-04-30 15:30:32 -04002092 name = kzalloc(sizeof(*name), GFP_NOFS);
2093 if (!name)
2094 return;
2095
2096 /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
Kees Cooka51d9ea2012-07-25 17:29:08 -07002097 ab = audit_log_start(current->audit_context, GFP_KERNEL,
2098 AUDIT_ANOM_LINK);
Sasha Levind1c7d972012-10-04 19:57:31 -04002099 if (!ab)
Eric Parisb24a30a2013-04-30 15:30:32 -04002100 goto out;
2101 audit_log_format(ab, "op=%s", operation);
2102 audit_log_task_info(ab, current);
2103 audit_log_format(ab, " res=0");
Kees Cooka51d9ea2012-07-25 17:29:08 -07002104 audit_log_end(ab);
Eric Parisb24a30a2013-04-30 15:30:32 -04002105
2106 /* Generate AUDIT_PATH record with object. */
2107 name->type = AUDIT_TYPE_NORMAL;
David Howells3b362152015-03-17 22:26:21 +00002108 audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry));
Eric Parisb24a30a2013-04-30 15:30:32 -04002109 audit_log_name(current->audit_context, name, link, 0, NULL);
2110out:
2111 kfree(name);
Kees Cooka51d9ea2012-07-25 17:29:08 -07002112}
2113
2114/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002115 * audit_log_end - end one audit record
2116 * @ab: the audit_buffer
2117 *
Paul Moore4aa838722016-11-29 16:53:24 -05002118 * We can not do a netlink send inside an irq context because it blocks (last
2119 * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a
2120 * queue and a tasklet is scheduled to remove them from the queue outside the
2121 * irq context. May be called in any context.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002122 */
David Woodhouseb7d11252005-05-19 10:56:58 +01002123void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!ab)
2126 return;
2127 if (!audit_rate_check()) {
2128 audit_log_lost("rate limit exceeded");
2129 } else {
Paul Mooreaf8b8242016-11-29 16:53:24 -05002130 skb_queue_tail(&audit_queue, ab->skb);
Paul Moore4aa838722016-11-29 16:53:24 -05002131 wake_up_interruptible(&kauditd_wait);
Eric Parisf3d357b2008-04-18 10:02:28 -04002132 ab->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Chris Wright16e19042005-05-06 15:53:34 +01002134 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002137/**
2138 * audit_log - Log an audit record
2139 * @ctx: audit context
2140 * @gfp_mask: type of allocation
2141 * @type: audit message type
2142 * @fmt: format string to use
2143 * @...: variable parameters matching the format string
2144 *
2145 * This is a convenience function that calls audit_log_start,
2146 * audit_log_vformat, and audit_log_end. It may be called
2147 * in any context.
2148 */
Daniel Walker5600b892007-10-18 03:06:10 -07002149void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002150 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct audit_buffer *ab;
2153 va_list args;
2154
David Woodhouse9ad9ad32005-06-22 15:04:33 +01002155 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (ab) {
2157 va_start(args, fmt);
2158 audit_log_vformat(ab, fmt, args);
2159 va_end(args);
2160 audit_log_end(ab);
2161 }
2162}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002163
Mr Dash Four131ad622011-06-30 13:31:57 +02002164#ifdef CONFIG_SECURITY
2165/**
2166 * audit_log_secctx - Converts and logs SELinux context
2167 * @ab: audit_buffer
2168 * @secid: security number
2169 *
2170 * This is a helper function that calls security_secid_to_secctx to convert
2171 * secid to secctx and then adds the (converted) SELinux context to the audit
2172 * log by calling audit_log_format, thus also preventing leak of internal secid
2173 * to userspace. If secid cannot be converted audit_panic is called.
2174 */
2175void audit_log_secctx(struct audit_buffer *ab, u32 secid)
2176{
2177 u32 len;
2178 char *secctx;
2179
2180 if (security_secid_to_secctx(secid, &secctx, &len)) {
2181 audit_panic("Cannot convert secid to context");
2182 } else {
2183 audit_log_format(ab, " obj=%s", secctx);
2184 security_release_secctx(secctx, len);
2185 }
2186}
2187EXPORT_SYMBOL(audit_log_secctx);
2188#endif
2189
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01002190EXPORT_SYMBOL(audit_log_start);
2191EXPORT_SYMBOL(audit_log_end);
2192EXPORT_SYMBOL(audit_log_format);
2193EXPORT_SYMBOL(audit_log);