blob: 468a2339045784c9260c2d80a1335b1090b3280a [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
George C. Wilson20ca73b2006-05-24 16:09:55 -05006 * Copyright (C) 2005, 2006 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050032 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33 * 2006.
34 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000035 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37 *
Amy Griffis73241cc2005-11-03 16:00:25 +000038 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000040 *
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
Richard Guy Briggsf952d102014-01-27 17:38:42 -050045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000050#include <linux/fs.h>
51#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040053#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Stephen Smalley01116102005-05-21 00:15:52 +010055#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010056#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050057#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/audit.h>
59#include <linux/personality.h>
60#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010061#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010062#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000064#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000065#include <linux/list.h>
Al Viro473ae302006-04-26 14:04:08 -040066#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040067#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040068#include <linux/syscalls.h>
Richard Guy Briggs84db5642014-01-29 16:17:58 -050069#include <asm/syscall.h>
Eric Paris851f7ff2008-11-11 21:48:14 +110070#include <linux/capability.h>
Al Viro5ad4e532009-03-29 19:50:06 -040071#include <linux/fs_struct.h>
Kees Cook3dc1c1b2012-04-12 16:47:58 -050072#include <linux/compat.h>
William Roberts3f1c8252014-02-11 10:12:01 -080073#include <linux/ctype.h>
Paul Moorefcf22d82014-12-30 09:26:21 -050074#include <linux/string.h>
Paul Moore437614732016-07-19 17:42:57 -040075#include <linux/uaccess.h>
Jan Kara9dd813c2017-03-14 12:31:02 +010076#include <linux/fsnotify_backend.h>
Paul Moorefcf22d82014-12-30 09:26:21 -050077#include <uapi/linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Woodhousefe7752b2005-12-15 18:33:52 +000079#include "audit.h"
80
Eric Parisd7e75282012-01-03 14:23:06 -050081/* flags stating the success for a syscall */
82#define AUDITSC_INVALID 0
83#define AUDITSC_SUCCESS 1
84#define AUDITSC_FAILURE 2
85
Paul Moore437614732016-07-19 17:42:57 -040086/* no execve audit message should be longer than this (userspace limits),
87 * see the note near the top of audit_log_execve_info() about this value */
Eric Parisde6bbd12008-01-07 14:31:58 -050088#define MAX_EXECVE_AUDIT_LEN 7500
89
William Roberts3f1c8252014-02-11 10:12:01 -080090/* max length to print of cmdline/proctitle value during audit */
91#define MAX_PROCTITLE_AUDIT_LEN 128
92
Al Viro471a5c72006-07-10 08:29:24 -040093/* number of audit rules */
94int audit_n_rules;
95
Amy Griffise54dc242007-03-29 18:01:04 -040096/* determines whether we collect data for signals sent */
97int audit_signals;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct audit_aux_data {
100 struct audit_aux_data *next;
101 int type;
102};
103
104#define AUDIT_AUX_IPCPERM 0
105
Amy Griffise54dc242007-03-29 18:01:04 -0400106/* Number of target pids per aux struct. */
107#define AUDIT_AUX_PIDS 16
108
Amy Griffise54dc242007-03-29 18:01:04 -0400109struct audit_aux_data_pids {
110 struct audit_aux_data d;
111 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700112 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800113 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500114 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400115 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500116 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400117 int pid_count;
118};
119
Eric Paris3fc689e2008-11-11 21:48:18 +1100120struct audit_aux_data_bprm_fcaps {
121 struct audit_aux_data d;
122 struct audit_cap_data fcap;
123 unsigned int fcap_ver;
124 struct audit_cap_data old_pcap;
125 struct audit_cap_data new_pcap;
126};
127
Al Viro74c3cbe2007-07-22 08:04:18 -0400128struct audit_tree_refs {
129 struct audit_tree_refs *next;
130 struct audit_chunk *c[31];
131};
132
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -0400133struct audit_nfcfgop_tab {
134 enum audit_nfcfgop op;
135 const char *s;
136};
137
Zheng Bindb9ff6e2020-04-29 17:26:48 +0800138static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
Richard Guy Briggsa45d8852020-04-22 17:39:29 -0400139 { AUDIT_XT_OP_REGISTER, "register" },
140 { AUDIT_XT_OP_REPLACE, "replace" },
141 { AUDIT_XT_OP_UNREGISTER, "unregister" },
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -0400142};
143
Al Viro55669bf2006-08-31 19:26:40 -0400144static int audit_match_perm(struct audit_context *ctx, int mask)
145{
Cordeliac4bacef2008-08-18 09:45:51 -0700146 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800147 if (unlikely(!ctx))
148 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700149 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100150
Al Viro55669bf2006-08-31 19:26:40 -0400151 switch (audit_classify_syscall(ctx->arch, n)) {
152 case 0: /* native */
153 if ((mask & AUDIT_PERM_WRITE) &&
154 audit_match_class(AUDIT_CLASS_WRITE, n))
155 return 1;
156 if ((mask & AUDIT_PERM_READ) &&
157 audit_match_class(AUDIT_CLASS_READ, n))
158 return 1;
159 if ((mask & AUDIT_PERM_ATTR) &&
160 audit_match_class(AUDIT_CLASS_CHATTR, n))
161 return 1;
162 return 0;
163 case 1: /* 32bit on biarch */
164 if ((mask & AUDIT_PERM_WRITE) &&
165 audit_match_class(AUDIT_CLASS_WRITE_32, n))
166 return 1;
167 if ((mask & AUDIT_PERM_READ) &&
168 audit_match_class(AUDIT_CLASS_READ_32, n))
169 return 1;
170 if ((mask & AUDIT_PERM_ATTR) &&
171 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
172 return 1;
173 return 0;
174 case 2: /* open */
175 return mask & ACC_MODE(ctx->argv[1]);
176 case 3: /* openat */
177 return mask & ACC_MODE(ctx->argv[2]);
178 case 4: /* socketcall */
179 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
180 case 5: /* execve */
181 return mask & AUDIT_PERM_EXEC;
182 default:
183 return 0;
184 }
185}
186
Eric Paris5ef30ee2012-01-03 14:23:05 -0500187static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400188{
Eric Paris5195d8e2012-01-03 14:23:05 -0500189 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500190 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800191
192 if (unlikely(!ctx))
193 return 0;
194
Eric Paris5195d8e2012-01-03 14:23:05 -0500195 list_for_each_entry(n, &ctx->names_list, list) {
Richard Guy Briggs84cb7772015-08-05 23:48:20 -0400196 if ((n->ino != AUDIT_INO_UNSET) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500197 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500198 return 1;
199 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500200
Eric Paris5ef30ee2012-01-03 14:23:05 -0500201 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400202}
203
Al Viro74c3cbe2007-07-22 08:04:18 -0400204/*
205 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
206 * ->first_trees points to its beginning, ->trees - to the current end of data.
207 * ->tree_count is the number of free entries in array pointed to by ->trees.
208 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
209 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
210 * it's going to remain 1-element for almost any setup) until we free context itself.
211 * References in it _are_ dropped - at the same time we free/drop aux stuff.
212 */
213
Eric Paris679173b2009-01-26 18:09:45 -0500214static void audit_set_auditable(struct audit_context *ctx)
215{
216 if (!ctx->prio) {
217 ctx->prio = 1;
218 ctx->current_state = AUDIT_RECORD_CONTEXT;
219 }
220}
221
Al Viro74c3cbe2007-07-22 08:04:18 -0400222static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
223{
224 struct audit_tree_refs *p = ctx->trees;
225 int left = ctx->tree_count;
226 if (likely(left)) {
227 p->c[--left] = chunk;
228 ctx->tree_count = left;
229 return 1;
230 }
231 if (!p)
232 return 0;
233 p = p->next;
234 if (p) {
235 p->c[30] = chunk;
236 ctx->trees = p;
237 ctx->tree_count = 30;
238 return 1;
239 }
240 return 0;
241}
242
243static int grow_tree_refs(struct audit_context *ctx)
244{
245 struct audit_tree_refs *p = ctx->trees;
246 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
247 if (!ctx->trees) {
248 ctx->trees = p;
249 return 0;
250 }
251 if (p)
252 p->next = ctx->trees;
253 else
254 ctx->first_trees = ctx->trees;
255 ctx->tree_count = 31;
256 return 1;
257}
Al Viro74c3cbe2007-07-22 08:04:18 -0400258
259static void unroll_tree_refs(struct audit_context *ctx,
260 struct audit_tree_refs *p, int count)
261{
Al Viro74c3cbe2007-07-22 08:04:18 -0400262 struct audit_tree_refs *q;
263 int n;
264 if (!p) {
265 /* we started with empty chain */
266 p = ctx->first_trees;
267 count = 31;
268 /* if the very first allocation has failed, nothing to do */
269 if (!p)
270 return;
271 }
272 n = count;
273 for (q = p; q != ctx->trees; q = q->next, n = 31) {
274 while (n--) {
275 audit_put_chunk(q->c[n]);
276 q->c[n] = NULL;
277 }
278 }
279 while (n-- > ctx->tree_count) {
280 audit_put_chunk(q->c[n]);
281 q->c[n] = NULL;
282 }
283 ctx->trees = p;
284 ctx->tree_count = count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400285}
286
287static void free_tree_refs(struct audit_context *ctx)
288{
289 struct audit_tree_refs *p, *q;
290 for (p = ctx->first_trees; p; p = q) {
291 q = p->next;
292 kfree(p);
293 }
294}
295
296static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
297{
Al Viro74c3cbe2007-07-22 08:04:18 -0400298 struct audit_tree_refs *p;
299 int n;
300 if (!tree)
301 return 0;
302 /* full ones */
303 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
304 for (n = 0; n < 31; n++)
305 if (audit_tree_match(p->c[n], tree))
306 return 1;
307 }
308 /* partial */
309 if (p) {
310 for (n = ctx->tree_count; n < 31; n++)
311 if (audit_tree_match(p->c[n], tree))
312 return 1;
313 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400314 return 0;
315}
316
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700317static int audit_compare_uid(kuid_t uid,
318 struct audit_names *name,
319 struct audit_field *f,
320 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500321{
322 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500323 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700324
Eric Parisb34b0392012-01-03 14:23:08 -0500325 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700326 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500327 if (rc)
328 return rc;
329 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700330
Eric Parisb34b0392012-01-03 14:23:08 -0500331 if (ctx) {
332 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700333 rc = audit_uid_comparator(uid, f->op, n->uid);
334 if (rc)
335 return rc;
336 }
337 }
338 return 0;
339}
Eric Parisb34b0392012-01-03 14:23:08 -0500340
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700341static int audit_compare_gid(kgid_t gid,
342 struct audit_names *name,
343 struct audit_field *f,
344 struct audit_context *ctx)
345{
346 struct audit_names *n;
347 int rc;
348
349 if (name) {
350 rc = audit_gid_comparator(gid, f->op, name->gid);
351 if (rc)
352 return rc;
353 }
354
355 if (ctx) {
356 list_for_each_entry(n, &ctx->names_list, list) {
357 rc = audit_gid_comparator(gid, f->op, n->gid);
Eric Parisb34b0392012-01-03 14:23:08 -0500358 if (rc)
359 return rc;
360 }
361 }
362 return 0;
363}
364
Eric Paris02d86a52012-01-03 14:23:08 -0500365static int audit_field_compare(struct task_struct *tsk,
366 const struct cred *cred,
367 struct audit_field *f,
368 struct audit_context *ctx,
369 struct audit_names *name)
370{
Eric Paris02d86a52012-01-03 14:23:08 -0500371 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800372 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500373 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700374 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500375 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700376 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800377 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700378 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800379 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700380 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800381 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400382 return audit_compare_uid(audit_get_loginuid(tsk), name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800383 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700384 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800385 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700386 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800387 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700388 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800389 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700390 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500391 /* uid comparisons */
392 case AUDIT_COMPARE_UID_TO_AUID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400393 return audit_uid_comparator(cred->uid, f->op,
394 audit_get_loginuid(tsk));
Peter Moody10d68362012-01-04 15:24:31 -0500395 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700396 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500397 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700398 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500399 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700400 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500401 /* auid comparisons */
402 case AUDIT_COMPARE_AUID_TO_EUID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400403 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
404 cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500405 case AUDIT_COMPARE_AUID_TO_SUID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400406 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
407 cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500408 case AUDIT_COMPARE_AUID_TO_FSUID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400409 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
410 cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500411 /* euid comparisons */
412 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700413 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500414 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700415 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500416 /* suid comparisons */
417 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700418 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500419 /* gid comparisons */
420 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700421 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500422 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700423 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500424 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700425 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500426 /* egid comparisons */
427 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700428 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500429 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700430 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500431 /* sgid comparison */
432 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700433 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500434 default:
435 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
436 return 0;
437 }
438 return 0;
439}
440
Amy Griffisf368c07d2006-04-07 16:55:56 -0400441/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200443 * otherwise.
444 *
445 * If task_creation is true, this is an explicit indication that we are
446 * filtering a task rule at task creation time. This and tsk == current are
447 * the only situations where tsk->cred may be accessed without an rcu read lock.
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500450 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400452 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200453 enum audit_state *state,
454 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Tony Jonesf5629882011-04-27 15:10:49 +0200456 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500457 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600458 u32 sid;
Richard Guy Briggs8fae4772016-11-20 16:47:55 -0500459 unsigned int sessionid;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600460
Tony Jonesf5629882011-04-27 15:10:49 +0200461 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500464 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500465 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int result = 0;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500467 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Amy Griffis93315ed2006-02-07 12:05:27 -0500469 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 case AUDIT_PID:
Paul Moorefa2bea22016-08-30 17:19:13 -0400471 pid = task_tgid_nr(tsk);
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500472 result = audit_comparator(pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
Al Viro3c662512006-05-06 08:26:27 -0400474 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400475 if (ctx) {
476 if (!ctx->ppid)
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -0500477 ctx->ppid = task_ppid_nr(tsk);
Al Viro3c662512006-05-06 08:26:27 -0400478 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400479 }
Al Viro3c662512006-05-06 08:26:27 -0400480 break;
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400481 case AUDIT_EXE:
482 result = audit_exe_compare(tsk, rule->exe);
Ondrej Mosnáček23bcc482018-04-09 10:00:06 +0200483 if (f->op == Audit_not_equal)
484 result = !result;
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700487 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700490 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700493 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700496 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700499 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300500 if (f->op == Audit_equal) {
501 if (!result)
Ondrej Mosnáčekaf85d172018-06-05 11:00:10 +0200502 result = groups_search(cred->group_info, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300503 } else if (f->op == Audit_not_equal) {
504 if (result)
Ondrej Mosnáčekaf85d172018-06-05 11:00:10 +0200505 result = !groups_search(cred->group_info, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700509 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300510 if (f->op == Audit_equal) {
511 if (!result)
Ondrej Mosnáčekaf85d172018-06-05 11:00:10 +0200512 result = groups_search(cred->group_info, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300513 } else if (f->op == Audit_not_equal) {
514 if (result)
Ondrej Mosnáčekaf85d172018-06-05 11:00:10 +0200515 result = !groups_search(cred->group_info, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700519 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700522 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
Richard Guy Briggs8fae4772016-11-20 16:47:55 -0500524 case AUDIT_SESSIONID:
Ondrej Mosnáček5b713882018-05-17 17:31:14 +0200525 sessionid = audit_get_sessionid(tsk);
Richard Guy Briggs8fae4772016-11-20 16:47:55 -0500526 result = audit_comparator(sessionid, f->op, f->val);
527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500529 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
2fd6f582005-04-29 16:08:28 +0100531 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700532 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500533 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 case AUDIT_EXIT:
537 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500538 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100541 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500542 if (f->val)
543 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100544 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500545 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 case AUDIT_DEVMAJOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500549 if (name) {
550 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
551 audit_comparator(MAJOR(name->rdev), f->op, f->val))
552 ++result;
553 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500554 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500555 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
556 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ++result;
558 break;
559 }
560 }
561 }
562 break;
563 case AUDIT_DEVMINOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500564 if (name) {
565 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
566 audit_comparator(MINOR(name->rdev), f->op, f->val))
567 ++result;
568 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500569 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500570 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
571 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ++result;
573 break;
574 }
575 }
576 }
577 break;
578 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400579 if (name)
Richard Guy Briggsdb510fc2013-07-04 12:56:11 -0400580 result = audit_comparator(name->ino, f->op, f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400581 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500582 list_for_each_entry(n, &ctx->names_list, list) {
583 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ++result;
585 break;
586 }
587 }
588 }
589 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500590 case AUDIT_OBJ_UID:
591 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700592 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500593 } else if (ctx) {
594 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700595 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500596 ++result;
597 break;
598 }
599 }
600 }
601 break;
Eric Paris54d32182012-01-03 14:23:07 -0500602 case AUDIT_OBJ_GID:
603 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700604 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500605 } else if (ctx) {
606 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700607 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500608 ++result;
609 break;
610 }
611 }
612 }
613 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400614 case AUDIT_WATCH:
Richard Guy Briggs0223fad2019-05-22 17:52:02 -0400615 if (name) {
616 result = audit_watch_compare(rule->watch,
617 name->ino,
618 name->dev);
619 if (f->op == Audit_not_equal)
620 result = !result;
621 }
Amy Griffisf368c07d2006-04-07 16:55:56 -0400622 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400623 case AUDIT_DIR:
Richard Guy Briggs0223fad2019-05-22 17:52:02 -0400624 if (ctx) {
Al Viro74c3cbe2007-07-22 08:04:18 -0400625 result = match_tree_refs(ctx, rule->tree);
Richard Guy Briggs0223fad2019-05-22 17:52:02 -0400626 if (f->op == Audit_not_equal)
627 result = !result;
628 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 case AUDIT_LOGINUID:
Richard Guy Briggs38f80592018-05-16 07:55:46 -0400631 result = audit_uid_comparator(audit_get_loginuid(tsk),
632 f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700634 case AUDIT_LOGINUID_SET:
635 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
636 break;
Richard Guy Briggsbf361232019-05-09 20:01:36 -0400637 case AUDIT_SADDR_FAM:
638 if (ctx->sockaddr)
639 result = audit_comparator(ctx->sockaddr->ss_family,
640 f->op, f->val);
641 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500642 case AUDIT_SUBJ_USER:
643 case AUDIT_SUBJ_ROLE:
644 case AUDIT_SUBJ_TYPE:
645 case AUDIT_SUBJ_SEN:
646 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600647 /* NOTE: this may return negative values indicating
648 a temporary error. We simply treat this as a
649 match for now to avoid losing information that
650 may be wanted. An error message will also be
651 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000652 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400653 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200654 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400655 need_sid = 0;
656 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200657 result = security_audit_rule_match(sid, f->type,
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500658 f->op,
659 f->lsm_rule);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400660 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600661 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500662 case AUDIT_OBJ_USER:
663 case AUDIT_OBJ_ROLE:
664 case AUDIT_OBJ_TYPE:
665 case AUDIT_OBJ_LEV_LOW:
666 case AUDIT_OBJ_LEV_HIGH:
667 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
668 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000669 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500670 /* Find files that match */
671 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200672 result = security_audit_rule_match(
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500673 name->osid,
674 f->type,
675 f->op,
676 f->lsm_rule);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500677 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500678 list_for_each_entry(n, &ctx->names_list, list) {
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500679 if (security_audit_rule_match(
680 n->osid,
681 f->type,
682 f->op,
683 f->lsm_rule)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500684 ++result;
685 break;
686 }
687 }
688 }
689 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500690 if (!ctx || ctx->type != AUDIT_IPC)
691 break;
692 if (security_audit_rule_match(ctx->ipc.osid,
693 f->type, f->op,
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500694 f->lsm_rule))
Al Viroa33e6752008-12-10 03:40:06 -0500695 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500696 }
697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case AUDIT_ARG0:
699 case AUDIT_ARG1:
700 case AUDIT_ARG2:
701 case AUDIT_ARG3:
702 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500703 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400705 case AUDIT_FILTERKEY:
706 /* ignore this field for filtering */
707 result = 1;
708 break;
Al Viro55669bf2006-08-31 19:26:40 -0400709 case AUDIT_PERM:
710 result = audit_match_perm(ctx, f->val);
Richard Guy Briggs0223fad2019-05-22 17:52:02 -0400711 if (f->op == Audit_not_equal)
712 result = !result;
Al Viro55669bf2006-08-31 19:26:40 -0400713 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400714 case AUDIT_FILETYPE:
715 result = audit_match_filetype(ctx, f->val);
Richard Guy Briggs0223fad2019-05-22 17:52:02 -0400716 if (f->op == Audit_not_equal)
717 result = !result;
Al Viro8b67dca2008-04-28 04:15:49 -0400718 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500719 case AUDIT_FIELD_COMPARE:
720 result = audit_field_compare(tsk, cred, f, ctx, name);
721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Tony Jonesf5629882011-04-27 15:10:49 +0200723 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725 }
Al Viro0590b932008-12-14 23:45:27 -0500726
727 if (ctx) {
728 if (rule->prio <= ctx->prio)
729 return 0;
730 if (rule->filterkey) {
731 kfree(ctx->filterkey);
732 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
733 }
734 ctx->prio = rule->prio;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 switch (rule->action) {
Paul Moore66b12ab2016-06-16 17:08:19 -0400737 case AUDIT_NEVER:
738 *state = AUDIT_DISABLED;
739 break;
740 case AUDIT_ALWAYS:
741 *state = AUDIT_RECORD_CONTEXT;
742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 return 1;
745}
746
747/* At process creation time, we can determine if system-call auditing is
748 * completely disabled for this task. Since we only have the task
749 * structure at this point, we can only check uid and gid.
750 */
Al Viroe048e022008-12-16 03:51:22 -0500751static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct audit_entry *e;
754 enum audit_state state;
755
756 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100757 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200758 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
759 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500760 if (state == AUDIT_RECORD_CONTEXT)
761 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rcu_read_unlock();
763 return state;
764 }
765 }
766 rcu_read_unlock();
767 return AUDIT_BUILD_CONTEXT;
768}
769
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400770static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
771{
772 int word, bit;
773
774 if (val > 0xffffffff)
775 return false;
776
777 word = AUDIT_WORD(val);
778 if (word >= AUDIT_BITMASK_SIZE)
779 return false;
780
781 bit = AUDIT_BIT(val);
782
783 return rule->mask[word] & bit;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/* At syscall entry and exit time, this filter is called if the
787 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100788 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700789 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
791static enum audit_state audit_filter_syscall(struct task_struct *tsk,
792 struct audit_context *ctx,
793 struct list_head *list)
794{
795 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100796 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Paul Moore5b523302017-03-21 11:26:35 -0400798 if (auditd_test_task(tsk))
David Woodhousef7056d62005-06-20 16:07:33 +0100799 return AUDIT_DISABLED;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 rcu_read_lock();
Richard Guy Briggs699c1862019-04-08 12:50:57 -0400802 list_for_each_entry_rcu(e, list, list) {
803 if (audit_in_mask(&e->rule, ctx->major) &&
804 audit_filter_rules(tsk, &e->rule, ctx, NULL,
805 &state, false)) {
806 rcu_read_unlock();
807 ctx->current_state = state;
808 return state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 rcu_read_unlock();
812 return AUDIT_BUILD_CONTEXT;
813}
814
Eric Paris5195d8e2012-01-03 14:23:05 -0500815/*
816 * Given an audit_name check the inode hash table to see if they match.
817 * Called holding the rcu read lock to protect the use of audit_inode_hash
818 */
819static int audit_filter_inode_name(struct task_struct *tsk,
820 struct audit_names *n,
821 struct audit_context *ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500822 int h = audit_hash_ino((u32)n->ino);
823 struct list_head *list = &audit_inode_hash[h];
824 struct audit_entry *e;
825 enum audit_state state;
826
Eric Paris5195d8e2012-01-03 14:23:05 -0500827 list_for_each_entry_rcu(e, list, list) {
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400828 if (audit_in_mask(&e->rule, ctx->major) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500829 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
830 ctx->current_state = state;
831 return 1;
832 }
833 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500834 return 0;
835}
836
837/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400838 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500839 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400840 * Regarding audit_state, same rules apply as for audit_filter_syscall().
841 */
Al Viro0590b932008-12-14 23:45:27 -0500842void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400843{
Eric Paris5195d8e2012-01-03 14:23:05 -0500844 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400845
Paul Moore5b523302017-03-21 11:26:35 -0400846 if (auditd_test_task(tsk))
Al Viro0590b932008-12-14 23:45:27 -0500847 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400848
849 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400850
Eric Paris5195d8e2012-01-03 14:23:05 -0500851 list_for_each_entry(n, &ctx->names_list, list) {
852 if (audit_filter_inode_name(tsk, n, ctx))
853 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400854 }
855 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400856}
857
William Roberts3f1c8252014-02-11 10:12:01 -0800858static inline void audit_proctitle_free(struct audit_context *context)
859{
860 kfree(context->proctitle.value);
861 context->proctitle.value = NULL;
862 context->proctitle.len = 0;
863}
864
Li RongQing95e0b462019-03-07 09:16:24 +0800865static inline void audit_free_module(struct audit_context *context)
866{
867 if (context->type == AUDIT_KERN_MODULE) {
868 kfree(context->module.name);
869 context->module.name = NULL;
870 }
871}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static inline void audit_free_names(struct audit_context *context)
873{
Eric Paris5195d8e2012-01-03 14:23:05 -0500874 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Eric Paris5195d8e2012-01-03 14:23:05 -0500876 list_for_each_entry_safe(n, next, &context->names_list, list) {
877 list_del(&n->list);
Paul Moore55422d02015-01-22 00:00:23 -0500878 if (n->name)
879 putname(n->name);
Eric Paris5195d8e2012-01-03 14:23:05 -0500880 if (n->should_free)
881 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800884 path_put(&context->pwd);
885 context->pwd.dentry = NULL;
886 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889static inline void audit_free_aux(struct audit_context *context)
890{
891 struct audit_aux_data *aux;
892
893 while ((aux = context->aux)) {
894 context->aux = aux->next;
895 kfree(aux);
896 }
Amy Griffise54dc242007-03-29 18:01:04 -0400897 while ((aux = context->aux_pids)) {
898 context->aux_pids = aux->next;
899 kfree(aux);
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903static inline struct audit_context *audit_alloc_context(enum audit_state state)
904{
905 struct audit_context *context;
906
Rakib Mullick17c6ee72013-04-07 16:14:18 +0600907 context = kzalloc(sizeof(*context), GFP_KERNEL);
908 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return NULL;
Andrew Mortone2c5adc2013-04-08 14:43:41 -0700910 context->state = state;
911 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Al Viro916d7572009-06-24 00:02:38 -0400912 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -0500913 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return context;
915}
916
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700917/**
918 * audit_alloc - allocate an audit context block for a task
919 * @tsk: task
920 *
921 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * if necessary. Doing so turns on system call auditing for the
923 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700924 * needed.
925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926int audit_alloc(struct task_struct *tsk)
927{
928 struct audit_context *context;
929 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500930 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Parisb593d382008-01-08 17:38:31 -0500932 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0; /* Return if not auditing. */
934
Al Viroe048e022008-12-16 03:51:22 -0500935 state = audit_filter_task(tsk, &key);
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200936 if (state == AUDIT_DISABLED) {
937 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500942 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 audit_log_lost("out of memory in audit_alloc");
944 return -ENOMEM;
945 }
Al Viroe048e022008-12-16 03:51:22 -0500946 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Richard Guy Briggsc0b0ae82018-05-12 21:58:21 -0400948 audit_set_context(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
950 return 0;
951}
952
953static inline void audit_free_context(struct audit_context *context)
954{
Li RongQing95e0b462019-03-07 09:16:24 +0800955 audit_free_module(context);
Al Viroc62d7732012-10-20 15:07:18 -0400956 audit_free_names(context);
957 unroll_tree_refs(context, NULL, 0);
958 free_tree_refs(context);
959 audit_free_aux(context);
960 kfree(context->filterkey);
961 kfree(context->sockaddr);
William Roberts3f1c8252014-02-11 10:12:01 -0800962 audit_proctitle_free(context);
Al Viroc62d7732012-10-20 15:07:18 -0400963 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Amy Griffise54dc242007-03-29 18:01:04 -0400966static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800967 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -0500968 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400969{
970 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200971 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400972 u32 len;
973 int rc = 0;
974
975 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
976 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500977 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400978
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700979 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
980 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800981 from_kuid(&init_user_ns, uid), sessionid);
Eric Parisad395ab2012-10-23 08:58:35 -0400982 if (sid) {
983 if (security_secid_to_secctx(sid, &ctx, &len)) {
984 audit_log_format(ab, " obj=(none)");
985 rc = 1;
986 } else {
987 audit_log_format(ab, " obj=%s", ctx);
988 security_release_secctx(ctx, len);
989 }
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200990 }
Eric Parisc2a77802008-01-07 13:40:17 -0500991 audit_log_format(ab, " ocomm=");
992 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400993 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -0400994
995 return rc;
996}
997
Eric Parisde6bbd12008-01-07 14:31:58 -0500998static void audit_log_execve_info(struct audit_context *context,
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -0400999 struct audit_buffer **ab)
Eric Parisde6bbd12008-01-07 14:31:58 -05001000{
Paul Moore437614732016-07-19 17:42:57 -04001001 long len_max;
1002 long len_rem;
1003 long len_full;
1004 long len_buf;
Richard Guy Briggs84430752016-11-10 01:39:49 -05001005 long len_abuf = 0;
Paul Moore437614732016-07-19 17:42:57 -04001006 long len_tmp;
1007 bool require_data;
1008 bool encode;
1009 unsigned int iter;
1010 unsigned int arg;
1011 char *buf_head;
Eric Parisde6bbd12008-01-07 14:31:58 -05001012 char *buf;
Paul Moore437614732016-07-19 17:42:57 -04001013 const char __user *p = (const char __user *)current->mm->arg_start;
Eric Parisde6bbd12008-01-07 14:31:58 -05001014
Paul Moore437614732016-07-19 17:42:57 -04001015 /* NOTE: this buffer needs to be large enough to hold all the non-arg
1016 * data we put in the audit record for this argument (see the
1017 * code below) ... at this point in time 96 is plenty */
1018 char abuf[96];
Eric Parisde6bbd12008-01-07 14:31:58 -05001019
Paul Moore437614732016-07-19 17:42:57 -04001020 /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1021 * current value of 7500 is not as important as the fact that it
1022 * is less than 8k, a setting of 7500 gives us plenty of wiggle
1023 * room if we go over a little bit in the logging below */
1024 WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1025 len_max = MAX_EXECVE_AUDIT_LEN;
Eric Parisde6bbd12008-01-07 14:31:58 -05001026
Paul Moore437614732016-07-19 17:42:57 -04001027 /* scratch buffer to hold the userspace args */
1028 buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1029 if (!buf_head) {
Joe Perchesb7550782014-03-05 14:34:36 -08001030 audit_panic("out of memory for argv string");
Eric Parisde6bbd12008-01-07 14:31:58 -05001031 return;
1032 }
Paul Moore437614732016-07-19 17:42:57 -04001033 buf = buf_head;
Eric Parisde6bbd12008-01-07 14:31:58 -05001034
Paul Moore437614732016-07-19 17:42:57 -04001035 audit_log_format(*ab, "argc=%d", context->execve.argc);
1036
1037 len_rem = len_max;
1038 len_buf = 0;
1039 len_full = 0;
1040 require_data = true;
1041 encode = false;
1042 iter = 0;
1043 arg = 0;
1044 do {
1045 /* NOTE: we don't ever want to trust this value for anything
1046 * serious, but the audit record format insists we
1047 * provide an argument length for really long arguments,
1048 * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1049 * to use strncpy_from_user() to obtain this value for
1050 * recording in the log, although we don't use it
1051 * anywhere here to avoid a double-fetch problem */
1052 if (len_full == 0)
1053 len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1054
1055 /* read more data from userspace */
1056 if (require_data) {
1057 /* can we make more room in the buffer? */
1058 if (buf != buf_head) {
1059 memmove(buf_head, buf, len_buf);
1060 buf = buf_head;
1061 }
1062
1063 /* fetch as much as we can of the argument */
1064 len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1065 len_max - len_buf);
1066 if (len_tmp == -EFAULT) {
1067 /* unable to copy from userspace */
1068 send_sig(SIGKILL, current, 0);
1069 goto out;
1070 } else if (len_tmp == (len_max - len_buf)) {
1071 /* buffer is not large enough */
1072 require_data = true;
1073 /* NOTE: if we are going to span multiple
1074 * buffers force the encoding so we stand
1075 * a chance at a sane len_full value and
1076 * consistent record encoding */
1077 encode = true;
1078 len_full = len_full * 2;
1079 p += len_tmp;
1080 } else {
1081 require_data = false;
1082 if (!encode)
1083 encode = audit_string_contains_control(
1084 buf, len_tmp);
1085 /* try to use a trusted value for len_full */
1086 if (len_full < len_max)
1087 len_full = (encode ?
1088 len_tmp * 2 : len_tmp);
1089 p += len_tmp + 1;
1090 }
1091 len_buf += len_tmp;
1092 buf_head[len_buf] = '\0';
1093
1094 /* length of the buffer in the audit record? */
1095 len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1096 }
1097
1098 /* write as much as we can to the audit log */
Richard Guy Briggsea956d82018-10-10 16:22:57 -04001099 if (len_buf >= 0) {
Paul Moore437614732016-07-19 17:42:57 -04001100 /* NOTE: some magic numbers here - basically if we
1101 * can't fit a reasonable amount of data into the
1102 * existing audit buffer, flush it and start with
1103 * a new buffer */
1104 if ((sizeof(abuf) + 8) > len_rem) {
1105 len_rem = len_max;
1106 audit_log_end(*ab);
1107 *ab = audit_log_start(context,
1108 GFP_KERNEL, AUDIT_EXECVE);
1109 if (!*ab)
1110 goto out;
1111 }
1112
1113 /* create the non-arg portion of the arg record */
1114 len_tmp = 0;
1115 if (require_data || (iter > 0) ||
1116 ((len_abuf + sizeof(abuf)) > len_rem)) {
1117 if (iter == 0) {
1118 len_tmp += snprintf(&abuf[len_tmp],
1119 sizeof(abuf) - len_tmp,
1120 " a%d_len=%lu",
1121 arg, len_full);
1122 }
1123 len_tmp += snprintf(&abuf[len_tmp],
1124 sizeof(abuf) - len_tmp,
1125 " a%d[%d]=", arg, iter++);
1126 } else
1127 len_tmp += snprintf(&abuf[len_tmp],
1128 sizeof(abuf) - len_tmp,
1129 " a%d=", arg);
1130 WARN_ON(len_tmp >= sizeof(abuf));
1131 abuf[sizeof(abuf) - 1] = '\0';
1132
1133 /* log the arg in the audit record */
1134 audit_log_format(*ab, "%s", abuf);
1135 len_rem -= len_tmp;
1136 len_tmp = len_buf;
1137 if (encode) {
1138 if (len_abuf > len_rem)
1139 len_tmp = len_rem / 2; /* encoding */
1140 audit_log_n_hex(*ab, buf, len_tmp);
1141 len_rem -= len_tmp * 2;
1142 len_abuf -= len_tmp * 2;
1143 } else {
1144 if (len_abuf > len_rem)
1145 len_tmp = len_rem - 2; /* quotes */
1146 audit_log_n_string(*ab, buf, len_tmp);
1147 len_rem -= len_tmp + 2;
1148 /* don't subtract the "2" because we still need
1149 * to add quotes to the remaining string */
1150 len_abuf -= len_tmp;
1151 }
1152 len_buf -= len_tmp;
1153 buf += len_tmp;
1154 }
1155
1156 /* ready to move to the next argument? */
1157 if ((len_buf == 0) && !require_data) {
1158 arg++;
1159 iter = 0;
1160 len_full = 0;
1161 require_data = true;
1162 encode = false;
1163 }
1164 } while (arg < context->execve.argc);
1165
1166 /* NOTE: the caller handles the final audit_log_end() call */
1167
1168out:
1169 kfree(buf_head);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001170}
1171
YueHaibing2efa48f2019-03-20 21:59:22 +08001172static void audit_log_cap(struct audit_buffer *ab, char *prefix,
1173 kernel_cap_t *cap)
Richard Guy Briggs5f3d5442019-02-01 22:45:17 -05001174{
1175 int i;
1176
1177 if (cap_isclear(*cap)) {
1178 audit_log_format(ab, " %s=0", prefix);
1179 return;
1180 }
1181 audit_log_format(ab, " %s=", prefix);
1182 CAP_FOR_EACH_U32(i)
1183 audit_log_format(ab, "%08x", cap->cap[CAP_LAST_U32 - i]);
1184}
1185
1186static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1187{
1188 if (name->fcap_ver == -1) {
1189 audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
1190 return;
1191 }
1192 audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
1193 audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
1194 audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
1195 name->fcap.fE, name->fcap_ver,
1196 from_kuid(&init_user_ns, name->fcap.rootid));
1197}
1198
Al Viroa33e6752008-12-10 03:40:06 -05001199static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001200{
1201 struct audit_buffer *ab;
1202 int i;
1203
1204 ab = audit_log_start(context, GFP_KERNEL, context->type);
1205 if (!ab)
1206 return;
1207
1208 switch (context->type) {
1209 case AUDIT_SOCKETCALL: {
1210 int nargs = context->socketcall.nargs;
1211 audit_log_format(ab, "nargs=%d", nargs);
1212 for (i = 0; i < nargs; i++)
1213 audit_log_format(ab, " a%d=%lx", i,
1214 context->socketcall.args[i]);
1215 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001216 case AUDIT_IPC: {
1217 u32 osid = context->ipc.osid;
1218
Al Viro2570ebb2011-07-27 14:03:22 -04001219 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001220 from_kuid(&init_user_ns, context->ipc.uid),
1221 from_kgid(&init_user_ns, context->ipc.gid),
1222 context->ipc.mode);
Al Viroa33e6752008-12-10 03:40:06 -05001223 if (osid) {
1224 char *ctx = NULL;
1225 u32 len;
1226 if (security_secid_to_secctx(osid, &ctx, &len)) {
1227 audit_log_format(ab, " osid=%u", osid);
1228 *call_panic = 1;
1229 } else {
1230 audit_log_format(ab, " obj=%s", ctx);
1231 security_release_secctx(ctx, len);
1232 }
1233 }
Al Viroe816f372008-12-10 03:47:15 -05001234 if (context->ipc.has_perm) {
1235 audit_log_end(ab);
1236 ab = audit_log_start(context, GFP_KERNEL,
1237 AUDIT_IPC_SET_PERM);
Kees Cook0644ec02013-01-11 14:32:07 -08001238 if (unlikely(!ab))
1239 return;
Al Viroe816f372008-12-10 03:47:15 -05001240 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001241 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001242 context->ipc.qbytes,
1243 context->ipc.perm_uid,
1244 context->ipc.perm_gid,
1245 context->ipc.perm_mode);
Al Viroe816f372008-12-10 03:47:15 -05001246 }
Al Viroa33e6752008-12-10 03:40:06 -05001247 break; }
Paul Moorefe8e52b92017-02-13 16:21:25 -05001248 case AUDIT_MQ_OPEN:
Al Viro564f6992008-12-14 04:02:26 -05001249 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001250 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001251 "mq_msgsize=%ld mq_curmsgs=%ld",
1252 context->mq_open.oflag, context->mq_open.mode,
1253 context->mq_open.attr.mq_flags,
1254 context->mq_open.attr.mq_maxmsg,
1255 context->mq_open.attr.mq_msgsize,
1256 context->mq_open.attr.mq_curmsgs);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001257 break;
1258 case AUDIT_MQ_SENDRECV:
Al Viroc32c8af2008-12-14 03:46:48 -05001259 audit_log_format(ab,
1260 "mqdes=%d msg_len=%zd msg_prio=%u "
Deepa Dinamanib9047722017-08-02 19:51:11 -07001261 "abs_timeout_sec=%lld abs_timeout_nsec=%ld",
Al Viroc32c8af2008-12-14 03:46:48 -05001262 context->mq_sendrecv.mqdes,
1263 context->mq_sendrecv.msg_len,
1264 context->mq_sendrecv.msg_prio,
Deepa Dinamanib9047722017-08-02 19:51:11 -07001265 (long long) context->mq_sendrecv.abs_timeout.tv_sec,
Al Viroc32c8af2008-12-14 03:46:48 -05001266 context->mq_sendrecv.abs_timeout.tv_nsec);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001267 break;
1268 case AUDIT_MQ_NOTIFY:
Al Viro20114f72008-12-10 07:16:12 -05001269 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1270 context->mq_notify.mqdes,
1271 context->mq_notify.sigev_signo);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001272 break;
Al Viro73929062008-12-10 06:58:59 -05001273 case AUDIT_MQ_GETSETATTR: {
1274 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1275 audit_log_format(ab,
1276 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1277 "mq_curmsgs=%ld ",
1278 context->mq_getsetattr.mqdes,
1279 attr->mq_flags, attr->mq_maxmsg,
1280 attr->mq_msgsize, attr->mq_curmsgs);
1281 break; }
Paul Moorefe8e52b92017-02-13 16:21:25 -05001282 case AUDIT_CAPSET:
Al Viro57f71a02009-01-04 14:52:57 -05001283 audit_log_format(ab, "pid=%d", context->capset.pid);
1284 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1285 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1286 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
Richard Guy Briggs7786f6b2017-04-07 10:17:27 -04001287 audit_log_cap(ab, "cap_pa", &context->capset.cap.ambient);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001288 break;
1289 case AUDIT_MMAP:
Al Viro120a7952010-10-30 02:54:44 -04001290 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1291 context->mmap.flags);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001292 break;
1293 case AUDIT_EXECVE:
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001294 audit_log_execve_info(context, &ab);
Paul Moorefe8e52b92017-02-13 16:21:25 -05001295 break;
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05001296 case AUDIT_KERN_MODULE:
1297 audit_log_format(ab, "name=");
Yi Wangb305f7e2018-07-25 10:26:19 +08001298 if (context->module.name) {
1299 audit_log_untrustedstring(ab, context->module.name);
Yi Wangb305f7e2018-07-25 10:26:19 +08001300 } else
1301 audit_log_format(ab, "(null)");
1302
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05001303 break;
Al Virof3298dc2008-12-10 03:16:51 -05001304 }
1305 audit_log_end(ab);
1306}
1307
William Roberts3f1c8252014-02-11 10:12:01 -08001308static inline int audit_proctitle_rtrim(char *proctitle, int len)
1309{
1310 char *end = proctitle + len - 1;
1311 while (end > proctitle && !isprint(*end))
1312 end--;
1313
1314 /* catch the case where proctitle is only 1 non-print character */
1315 len = end - proctitle + 1;
1316 len -= isprint(proctitle[len-1]) == 0;
1317 return len;
1318}
1319
Richard Guy Briggs5f3d5442019-02-01 22:45:17 -05001320/*
1321 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1322 * @context: audit_context for the task
1323 * @n: audit_names structure with reportable details
1324 * @path: optional path to report instead of audit_names->name
1325 * @record_num: record number to report when handling a list of names
1326 * @call_panic: optional pointer to int that will be updated if secid fails
1327 */
1328static void audit_log_name(struct audit_context *context, struct audit_names *n,
1329 const struct path *path, int record_num, int *call_panic)
1330{
1331 struct audit_buffer *ab;
1332
1333 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1334 if (!ab)
1335 return;
1336
1337 audit_log_format(ab, "item=%d", record_num);
1338
1339 if (path)
1340 audit_log_d_path(ab, " name=", path);
1341 else if (n->name) {
1342 switch (n->name_len) {
1343 case AUDIT_NAME_FULL:
1344 /* log the full path */
1345 audit_log_format(ab, " name=");
1346 audit_log_untrustedstring(ab, n->name->name);
1347 break;
1348 case 0:
1349 /* name was specified as a relative path and the
1350 * directory component is the cwd
1351 */
1352 audit_log_d_path(ab, " name=", &context->pwd);
1353 break;
1354 default:
1355 /* log the name's directory component */
1356 audit_log_format(ab, " name=");
1357 audit_log_n_untrustedstring(ab, n->name->name,
1358 n->name_len);
1359 }
1360 } else
1361 audit_log_format(ab, " name=(null)");
1362
1363 if (n->ino != AUDIT_INO_UNSET)
1364 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
1365 n->ino,
1366 MAJOR(n->dev),
1367 MINOR(n->dev),
1368 n->mode,
1369 from_kuid(&init_user_ns, n->uid),
1370 from_kgid(&init_user_ns, n->gid),
1371 MAJOR(n->rdev),
1372 MINOR(n->rdev));
1373 if (n->osid != 0) {
1374 char *ctx = NULL;
1375 u32 len;
1376
1377 if (security_secid_to_secctx(
1378 n->osid, &ctx, &len)) {
1379 audit_log_format(ab, " osid=%u", n->osid);
1380 if (call_panic)
1381 *call_panic = 2;
1382 } else {
1383 audit_log_format(ab, " obj=%s", ctx);
1384 security_release_secctx(ctx, len);
1385 }
1386 }
1387
1388 /* log the audit_names record type */
1389 switch (n->type) {
1390 case AUDIT_TYPE_NORMAL:
1391 audit_log_format(ab, " nametype=NORMAL");
1392 break;
1393 case AUDIT_TYPE_PARENT:
1394 audit_log_format(ab, " nametype=PARENT");
1395 break;
1396 case AUDIT_TYPE_CHILD_DELETE:
1397 audit_log_format(ab, " nametype=DELETE");
1398 break;
1399 case AUDIT_TYPE_CHILD_CREATE:
1400 audit_log_format(ab, " nametype=CREATE");
1401 break;
1402 default:
1403 audit_log_format(ab, " nametype=UNKNOWN");
1404 break;
1405 }
1406
1407 audit_log_fcaps(ab, n);
1408 audit_log_end(ab);
1409}
1410
Paul Moore2a1fe212018-11-26 18:40:07 -05001411static void audit_log_proctitle(void)
William Roberts3f1c8252014-02-11 10:12:01 -08001412{
1413 int res;
1414 char *buf;
1415 char *msg = "(null)";
1416 int len = strlen(msg);
Paul Moore2a1fe212018-11-26 18:40:07 -05001417 struct audit_context *context = audit_context();
William Roberts3f1c8252014-02-11 10:12:01 -08001418 struct audit_buffer *ab;
1419
1420 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1421 if (!ab)
1422 return; /* audit_panic or being filtered */
1423
1424 audit_log_format(ab, "proctitle=");
1425
1426 /* Not cached */
1427 if (!context->proctitle.value) {
1428 buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1429 if (!buf)
1430 goto out;
1431 /* Historically called this from procfs naming */
Paul Moore2a1fe212018-11-26 18:40:07 -05001432 res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
William Roberts3f1c8252014-02-11 10:12:01 -08001433 if (res == 0) {
1434 kfree(buf);
1435 goto out;
1436 }
1437 res = audit_proctitle_rtrim(buf, res);
1438 if (res == 0) {
1439 kfree(buf);
1440 goto out;
1441 }
1442 context->proctitle.value = buf;
1443 context->proctitle.len = res;
1444 }
1445 msg = context->proctitle.value;
1446 len = context->proctitle.len;
1447out:
1448 audit_log_n_untrustedstring(ab, msg, len);
1449 audit_log_end(ab);
1450}
1451
Paul Moore2a1fe212018-11-26 18:40:07 -05001452static void audit_log_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001454 int i, call_panic = 0;
Paul Moore2a1fe212018-11-26 18:40:07 -05001455 struct audit_context *context = audit_context();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001457 struct audit_aux_data *aux;
Eric Paris5195d8e2012-01-03 14:23:05 -05001458 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Paul Moore2a1fe212018-11-26 18:40:07 -05001460 context->personality = current->personality;
Al Viroe4951492006-03-29 20:17:10 -05001461
1462 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!ab)
1464 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001465 audit_log_format(ab, "arch=%x syscall=%d",
1466 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (context->personality != PER_LINUX)
1468 audit_log_format(ab, " per=%lx", context->personality);
1469 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001470 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001471 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1472 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 audit_log_format(ab,
Peter Moodye23eb922012-06-14 10:04:35 -07001475 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1476 context->argv[0],
1477 context->argv[1],
1478 context->argv[2],
1479 context->argv[3],
1480 context->name_count);
Alan Coxeb84a202006-09-29 02:01:41 -07001481
Paul Moore2a1fe212018-11-26 18:40:07 -05001482 audit_log_task_info(ab);
Eric Paris9d960982009-06-11 14:31:37 -04001483 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David Woodhouse7551ced2005-05-26 12:04:57 +01001486 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001487
Al Viroe4951492006-03-29 20:17:10 -05001488 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (!ab)
1490 continue; /* audit_panic has been called */
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001493
Eric Paris3fc689e2008-11-11 21:48:18 +11001494 case AUDIT_BPRM_FCAPS: {
1495 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1496 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1497 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1498 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1499 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1500 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1501 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1502 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
Richard Guy Briggs7786f6b2017-04-07 10:17:27 -04001503 audit_log_cap(ab, "old_pa", &axs->old_pcap.ambient);
1504 audit_log_cap(ab, "pp", &axs->new_pcap.permitted);
1505 audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
1506 audit_log_cap(ab, "pe", &axs->new_pcap.effective);
1507 audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
Richard Guy Briggs2fec30e2019-01-23 21:36:25 -05001508 audit_log_format(ab, " frootid=%d",
1509 from_kuid(&init_user_ns,
1510 axs->fcap.rootid));
Eric Paris3fc689e2008-11-11 21:48:18 +11001511 break; }
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
Al Virof3298dc2008-12-10 03:16:51 -05001517 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001518 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001519
Al Viro157cf642008-12-14 04:57:47 -05001520 if (context->fds[0] >= 0) {
1521 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1522 if (ab) {
1523 audit_log_format(ab, "fd0=%d fd1=%d",
1524 context->fds[0], context->fds[1]);
1525 audit_log_end(ab);
1526 }
1527 }
1528
Al Viro4f6b4342008-12-09 19:50:34 -05001529 if (context->sockaddr_len) {
1530 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1531 if (ab) {
1532 audit_log_format(ab, "saddr=");
1533 audit_log_n_hex(ab, (void *)context->sockaddr,
1534 context->sockaddr_len);
1535 audit_log_end(ab);
1536 }
1537 }
1538
Amy Griffise54dc242007-03-29 18:01:04 -04001539 for (aux = context->aux_pids; aux; aux = aux->next) {
1540 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001541
1542 for (i = 0; i < axs->pid_count; i++)
1543 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001544 axs->target_auid[i],
1545 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001546 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001547 axs->target_sid[i],
1548 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001549 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001550 }
1551
Amy Griffise54dc242007-03-29 18:01:04 -04001552 if (context->target_pid &&
1553 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001554 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001555 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001556 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001557 call_panic = 1;
1558
Jan Blunck44707fd2008-02-14 19:38:33 -08001559 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001560 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001561 if (ab) {
Steve Grubb0b7a0fd2016-07-14 10:59:19 -04001562 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001563 audit_log_end(ab);
1564 }
1565 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001566
Eric Paris5195d8e2012-01-03 14:23:05 -05001567 i = 0;
Jeff Layton79f65302013-07-08 15:59:36 -07001568 list_for_each_entry(n, &context->names_list, list) {
1569 if (n->hidden)
1570 continue;
Eric Parisb24a30a2013-04-30 15:30:32 -04001571 audit_log_name(context, n, NULL, i++, &call_panic);
Jeff Layton79f65302013-07-08 15:59:36 -07001572 }
Eric Parisc0641f22008-01-07 13:49:15 -05001573
Paul Moore2a1fe212018-11-26 18:40:07 -05001574 audit_log_proctitle();
William Roberts3f1c8252014-02-11 10:12:01 -08001575
Eric Parisc0641f22008-01-07 13:49:15 -05001576 /* Send end of event record to help user space know we are finished */
1577 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1578 if (ab)
1579 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001580 if (call_panic)
1581 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001584/**
Geliang Tang196a5082017-08-07 21:44:24 +08001585 * __audit_free - free a per-task audit context
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001586 * @tsk: task whose audit context block to free
1587 *
Al Virofa84cb92006-03-29 20:30:19 -05001588 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001589 */
Eric Parisa4ff8db2012-01-03 14:23:07 -05001590void __audit_free(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Paul Moore2a1fe212018-11-26 18:40:07 -05001592 struct audit_context *context = tsk->audit_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Eric Paris56179a62012-01-03 14:23:06 -05001594 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return;
1596
Richard Guy Briggs9e36a5d2018-12-10 17:17:50 -05001597 if (!list_empty(&context->killed_trees))
1598 audit_kill_trees(context);
1599
Paul Moore2a1fe212018-11-26 18:40:07 -05001600 /* We are called either by do_exit() or the fork() error handling code;
1601 * in the former case tsk == current and in the latter tsk is a
1602 * random task_struct that doesn't doesn't have any meaningful data we
1603 * need to log via audit_log_exit().
1604 */
1605 if (tsk == current && !context->dummy && context->in_syscall) {
1606 context->return_valid = 0;
1607 context->return_code = 0;
1608
1609 audit_filter_syscall(tsk, context,
1610 &audit_filter_list[AUDIT_FILTER_EXIT]);
1611 audit_filter_inodes(tsk, context);
1612 if (context->current_state == AUDIT_RECORD_CONTEXT)
1613 audit_log_exit();
1614 }
1615
Paul Moore2a1fe212018-11-26 18:40:07 -05001616 audit_set_context(tsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 audit_free_context(context);
1618}
1619
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001620/**
Geliang Tang196a5082017-08-07 21:44:24 +08001621 * __audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001622 * @major: major syscall type (function)
1623 * @a1: additional syscall register 1
1624 * @a2: additional syscall register 2
1625 * @a3: additional syscall register 3
1626 * @a4: additional syscall register 4
1627 *
1628 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * audit context was created when the task was created and the state or
1630 * filters demand the audit context be built. If the state from the
1631 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1632 * then the record will be written at syscall exit time (otherwise, it
1633 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001634 * be written).
1635 */
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -05001636void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
1637 unsigned long a3, unsigned long a4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001639 struct audit_context *context = audit_context();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 enum audit_state state;
1641
Richard Guy Briggs94d14e32018-02-14 21:47:44 -05001642 if (!audit_enabled || !context)
Roland McGrath86a1c342008-06-23 15:37:04 -07001643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 BUG_ON(context->in_syscall || context->name_count);
1646
Richard Guy Briggs5260ecc2018-02-14 21:47:43 -05001647 state = context->state;
1648 if (state == AUDIT_DISABLED)
1649 return;
1650
1651 context->dummy = !audit_n_rules;
1652 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1653 context->prio = 0;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001654 if (auditd_test_task(current))
Richard Guy Briggs5260ecc2018-02-14 21:47:43 -05001655 return;
1656 }
1657
Dmitry V. Levin16add412019-03-18 02:30:18 +03001658 context->arch = syscall_get_arch(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 context->major = major;
1660 context->argv[0] = a1;
1661 context->argv[1] = a2;
1662 context->argv[2] = a3;
1663 context->argv[3] = a4;
David Woodhousece625a82005-07-18 14:24:46 -04001664 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001666 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001667 context->ppid = 0;
Paul Moore290e44b2018-07-17 14:45:08 -04001668 ktime_get_coarse_real_ts64(&context->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001671/**
Geliang Tang196a5082017-08-07 21:44:24 +08001672 * __audit_syscall_exit - deallocate audit context after a system call
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001673 * @success: success value of the syscall
1674 * @return_code: return value of the syscall
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001675 *
1676 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001678 * filtering, or because some other part of the kernel wrote an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001680 * free the names stored from getname().
1681 */
Eric Parisd7e75282012-01-03 14:23:06 -05001682void __audit_syscall_exit(int success, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct audit_context *context;
1685
Paul Moore2a1fe212018-11-26 18:40:07 -05001686 context = audit_context();
Eric Paris56179a62012-01-03 14:23:06 -05001687 if (!context)
Al Viro97e94c42006-03-29 20:26:24 -05001688 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Richard Guy Briggs9e36a5d2018-12-10 17:17:50 -05001690 if (!list_empty(&context->killed_trees))
1691 audit_kill_trees(context);
1692
Paul Moore2a1fe212018-11-26 18:40:07 -05001693 if (!context->dummy && context->in_syscall) {
1694 if (success)
1695 context->return_valid = AUDITSC_SUCCESS;
1696 else
1697 context->return_valid = AUDITSC_FAILURE;
1698
1699 /*
1700 * we need to fix up the return code in the audit logs if the
1701 * actual return codes are later going to be fixed up by the
1702 * arch specific signal handlers
1703 *
1704 * This is actually a test for:
1705 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1706 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1707 *
1708 * but is faster than a bunch of ||
1709 */
1710 if (unlikely(return_code <= -ERESTARTSYS) &&
1711 (return_code >= -ERESTART_RESTARTBLOCK) &&
1712 (return_code != -ENOIOCTLCMD))
1713 context->return_code = -EINTR;
1714 else
1715 context->return_code = return_code;
1716
1717 audit_filter_syscall(current, context,
1718 &audit_filter_list[AUDIT_FILTER_EXIT]);
1719 audit_filter_inodes(current, context);
1720 if (context->current_state == AUDIT_RECORD_CONTEXT)
1721 audit_log_exit();
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001725 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001726
Li RongQing95e0b462019-03-07 09:16:24 +08001727 audit_free_module(context);
Al Viroc62d7732012-10-20 15:07:18 -04001728 audit_free_names(context);
1729 unroll_tree_refs(context, NULL, 0);
1730 audit_free_aux(context);
1731 context->aux = NULL;
1732 context->aux_pids = NULL;
1733 context->target_pid = 0;
1734 context->target_sid = 0;
1735 context->sockaddr_len = 0;
1736 context->type = 0;
1737 context->fds[0] = -1;
1738 if (context->state != AUDIT_RECORD_CONTEXT) {
1739 kfree(context->filterkey);
1740 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Al Viro74c3cbe2007-07-22 08:04:18 -04001744static inline void handle_one(const struct inode *inode)
1745{
Al Viro74c3cbe2007-07-22 08:04:18 -04001746 struct audit_context *context;
1747 struct audit_tree_refs *p;
1748 struct audit_chunk *chunk;
1749 int count;
Jan Kara08991e82017-02-01 09:21:58 +01001750 if (likely(!inode->i_fsnotify_marks))
Al Viro74c3cbe2007-07-22 08:04:18 -04001751 return;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001752 context = audit_context();
Al Viro74c3cbe2007-07-22 08:04:18 -04001753 p = context->trees;
1754 count = context->tree_count;
1755 rcu_read_lock();
1756 chunk = audit_tree_lookup(inode);
1757 rcu_read_unlock();
1758 if (!chunk)
1759 return;
1760 if (likely(put_tree_ref(context, chunk)))
1761 return;
1762 if (unlikely(!grow_tree_refs(context))) {
Richard Guy Briggsf952d102014-01-27 17:38:42 -05001763 pr_warn("out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001764 audit_set_auditable(context);
1765 audit_put_chunk(chunk);
1766 unroll_tree_refs(context, p, count);
1767 return;
1768 }
1769 put_tree_ref(context, chunk);
Al Viro74c3cbe2007-07-22 08:04:18 -04001770}
1771
1772static void handle_path(const struct dentry *dentry)
1773{
Al Viro74c3cbe2007-07-22 08:04:18 -04001774 struct audit_context *context;
1775 struct audit_tree_refs *p;
1776 const struct dentry *d, *parent;
1777 struct audit_chunk *drop;
1778 unsigned long seq;
1779 int count;
1780
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001781 context = audit_context();
Al Viro74c3cbe2007-07-22 08:04:18 -04001782 p = context->trees;
1783 count = context->tree_count;
1784retry:
1785 drop = NULL;
1786 d = dentry;
1787 rcu_read_lock();
1788 seq = read_seqbegin(&rename_lock);
1789 for(;;) {
David Howells3b362152015-03-17 22:26:21 +00001790 struct inode *inode = d_backing_inode(d);
Jan Kara08991e82017-02-01 09:21:58 +01001791 if (inode && unlikely(inode->i_fsnotify_marks)) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001792 struct audit_chunk *chunk;
1793 chunk = audit_tree_lookup(inode);
1794 if (chunk) {
1795 if (unlikely(!put_tree_ref(context, chunk))) {
1796 drop = chunk;
1797 break;
1798 }
1799 }
1800 }
1801 parent = d->d_parent;
1802 if (parent == d)
1803 break;
1804 d = parent;
1805 }
1806 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1807 rcu_read_unlock();
1808 if (!drop) {
1809 /* just a race with rename */
1810 unroll_tree_refs(context, p, count);
1811 goto retry;
1812 }
1813 audit_put_chunk(drop);
1814 if (grow_tree_refs(context)) {
1815 /* OK, got more space */
1816 unroll_tree_refs(context, p, count);
1817 goto retry;
1818 }
1819 /* too bad */
Richard Guy Briggsf952d102014-01-27 17:38:42 -05001820 pr_warn("out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001821 unroll_tree_refs(context, p, count);
1822 audit_set_auditable(context);
1823 return;
1824 }
1825 rcu_read_unlock();
Al Viro74c3cbe2007-07-22 08:04:18 -04001826}
1827
Jeff Layton78e2e802012-10-10 15:25:22 -04001828static struct audit_names *audit_alloc_name(struct audit_context *context,
1829 unsigned char type)
Eric Paris5195d8e2012-01-03 14:23:05 -05001830{
1831 struct audit_names *aname;
1832
1833 if (context->name_count < AUDIT_NAMES) {
1834 aname = &context->preallocated_names[context->name_count];
1835 memset(aname, 0, sizeof(*aname));
1836 } else {
1837 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1838 if (!aname)
1839 return NULL;
1840 aname->should_free = true;
1841 }
1842
Richard Guy Briggs84cb7772015-08-05 23:48:20 -04001843 aname->ino = AUDIT_INO_UNSET;
Jeff Layton78e2e802012-10-10 15:25:22 -04001844 aname->type = type;
Eric Paris5195d8e2012-01-03 14:23:05 -05001845 list_add_tail(&aname->list, &context->names_list);
1846
1847 context->name_count++;
Eric Paris5195d8e2012-01-03 14:23:05 -05001848 return aname;
1849}
1850
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001851/**
Geliang Tang196a5082017-08-07 21:44:24 +08001852 * __audit_reusename - fill out filename with info from existing entry
Jeff Layton7ac86262012-10-10 15:25:28 -04001853 * @uptr: userland ptr to pathname
1854 *
1855 * Search the audit_names list for the current audit context. If there is an
1856 * existing entry with a matching "uptr" then return the filename
1857 * associated with that audit_name. If not, return NULL.
1858 */
1859struct filename *
1860__audit_reusename(const __user char *uptr)
1861{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001862 struct audit_context *context = audit_context();
Jeff Layton7ac86262012-10-10 15:25:28 -04001863 struct audit_names *n;
1864
1865 list_for_each_entry(n, &context->names_list, list) {
1866 if (!n->name)
1867 continue;
Paul Moore55422d02015-01-22 00:00:23 -05001868 if (n->name->uptr == uptr) {
1869 n->name->refcnt++;
Jeff Layton7ac86262012-10-10 15:25:28 -04001870 return n->name;
Paul Moore55422d02015-01-22 00:00:23 -05001871 }
Jeff Layton7ac86262012-10-10 15:25:28 -04001872 }
1873 return NULL;
1874}
1875
1876/**
Geliang Tang196a5082017-08-07 21:44:24 +08001877 * __audit_getname - add a name to the list
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001878 * @name: name to add
1879 *
1880 * Add a name to the list of audit names for this context.
1881 * Called from fs/namei.c:getname().
1882 */
Jeff Layton91a27b22012-10-10 15:25:28 -04001883void __audit_getname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001885 struct audit_context *context = audit_context();
Eric Paris5195d8e2012-01-03 14:23:05 -05001886 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Paul Moore55422d02015-01-22 00:00:23 -05001888 if (!context->in_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return;
Jeff Layton91a27b22012-10-10 15:25:28 -04001890
Jeff Layton78e2e802012-10-10 15:25:22 -04001891 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05001892 if (!n)
1893 return;
1894
1895 n->name = name;
1896 n->name_len = AUDIT_NAME_FULL;
Jeff Laytonadb5c242012-10-10 16:43:13 -04001897 name->aname = n;
Paul Moore55422d02015-01-22 00:00:23 -05001898 name->refcnt++;
Eric Paris5195d8e2012-01-03 14:23:05 -05001899
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001900 if (!context->pwd.dentry)
1901 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Richard Guy Briggs5f3d5442019-02-01 22:45:17 -05001904static inline int audit_copy_fcaps(struct audit_names *name,
1905 const struct dentry *dentry)
1906{
1907 struct cpu_vfs_cap_data caps;
1908 int rc;
1909
1910 if (!dentry)
1911 return 0;
1912
1913 rc = get_vfs_caps_from_disk(dentry, &caps);
1914 if (rc)
1915 return rc;
1916
1917 name->fcap.permitted = caps.permitted;
1918 name->fcap.inheritable = caps.inheritable;
1919 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1920 name->fcap.rootid = caps.rootid;
1921 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1922 VFS_CAP_REVISION_SHIFT;
1923
1924 return 0;
1925}
1926
1927/* Copy inode data into an audit_names. */
YueHaibing2efa48f2019-03-20 21:59:22 +08001928static void audit_copy_inode(struct audit_names *name,
1929 const struct dentry *dentry,
1930 struct inode *inode, unsigned int flags)
Richard Guy Briggs5f3d5442019-02-01 22:45:17 -05001931{
1932 name->ino = inode->i_ino;
1933 name->dev = inode->i_sb->s_dev;
1934 name->mode = inode->i_mode;
1935 name->uid = inode->i_uid;
1936 name->gid = inode->i_gid;
1937 name->rdev = inode->i_rdev;
1938 security_inode_getsecid(inode, &name->osid);
1939 if (flags & AUDIT_INODE_NOEVAL) {
1940 name->fcap_ver = -1;
1941 return;
1942 }
1943 audit_copy_fcaps(name, dentry);
1944}
1945
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001946/**
Jeff Laytonbfcec702012-10-10 15:25:23 -04001947 * __audit_inode - store the inode and device from a lookup
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001948 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001949 * @dentry: dentry being audited
Jeff Layton79f65302013-07-08 15:59:36 -07001950 * @flags: attributes for this particular entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001951 */
Jeff Laytonadb5c242012-10-10 16:43:13 -04001952void __audit_inode(struct filename *name, const struct dentry *dentry,
Jeff Layton79f65302013-07-08 15:59:36 -07001953 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04001955 struct audit_context *context = audit_context();
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001956 struct inode *inode = d_backing_inode(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05001957 struct audit_names *n;
Jeff Layton79f65302013-07-08 15:59:36 -07001958 bool parent = flags & AUDIT_INODE_PARENT;
Richard Guy Briggsa252f562019-01-23 13:34:59 -05001959 struct audit_entry *e;
1960 struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
1961 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (!context->in_syscall)
1964 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05001965
Richard Guy Briggsa252f562019-01-23 13:34:59 -05001966 rcu_read_lock();
Richard Guy Briggs699c1862019-04-08 12:50:57 -04001967 list_for_each_entry_rcu(e, list, list) {
1968 for (i = 0; i < e->rule.field_count; i++) {
1969 struct audit_field *f = &e->rule.fields[i];
Richard Guy Briggsa252f562019-01-23 13:34:59 -05001970
Richard Guy Briggs699c1862019-04-08 12:50:57 -04001971 if (f->type == AUDIT_FSTYPE
1972 && audit_comparator(inode->i_sb->s_magic,
1973 f->op, f->val)
1974 && e->rule.action == AUDIT_NEVER) {
1975 rcu_read_unlock();
1976 return;
Richard Guy Briggsa252f562019-01-23 13:34:59 -05001977 }
1978 }
1979 }
1980 rcu_read_unlock();
1981
Jeff Layton9cec9d62012-10-10 15:25:21 -04001982 if (!name)
1983 goto out_alloc;
1984
Jeff Laytonadb5c242012-10-10 16:43:13 -04001985 /*
1986 * If we have a pointer to an audit_names entry already, then we can
1987 * just use it directly if the type is correct.
1988 */
1989 n = name->aname;
1990 if (n) {
1991 if (parent) {
1992 if (n->type == AUDIT_TYPE_PARENT ||
1993 n->type == AUDIT_TYPE_UNKNOWN)
1994 goto out;
1995 } else {
1996 if (n->type != AUDIT_TYPE_PARENT)
1997 goto out;
1998 }
1999 }
2000
Eric Paris5195d8e2012-01-03 14:23:05 -05002001 list_for_each_entry_reverse(n, &context->names_list, list) {
Paul Moore57c59f52015-01-22 00:00:16 -05002002 if (n->ino) {
2003 /* valid inode number, use that for the comparison */
2004 if (n->ino != inode->i_ino ||
2005 n->dev != inode->i_sb->s_dev)
2006 continue;
2007 } else if (n->name) {
2008 /* inode number has not been set, check the name */
2009 if (strcmp(n->name->name, name->name))
2010 continue;
2011 } else
2012 /* no inode and no name (?!) ... this is odd ... */
Jeff Laytonbfcec702012-10-10 15:25:23 -04002013 continue;
2014
2015 /* match the correct record type */
2016 if (parent) {
2017 if (n->type == AUDIT_TYPE_PARENT ||
2018 n->type == AUDIT_TYPE_UNKNOWN)
2019 goto out;
2020 } else {
2021 if (n->type != AUDIT_TYPE_PARENT)
2022 goto out;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002025
Jeff Layton9cec9d62012-10-10 15:25:21 -04002026out_alloc:
Paul Moore4a928432014-12-22 12:27:39 -05002027 /* unable to find an entry with both a matching name and type */
2028 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05002029 if (!n)
2030 return;
Paul Moorefcf22d82014-12-30 09:26:21 -05002031 if (name) {
Paul Moorefd3522f2015-01-22 00:00:10 -05002032 n->name = name;
Paul Moore55422d02015-01-22 00:00:23 -05002033 name->refcnt++;
Paul Moorefcf22d82014-12-30 09:26:21 -05002034 }
Jeff Laytonbfcec702012-10-10 15:25:23 -04002035
Eric Paris5195d8e2012-01-03 14:23:05 -05002036out:
Jeff Laytonbfcec702012-10-10 15:25:23 -04002037 if (parent) {
Jeff Layton91a27b22012-10-10 15:25:28 -04002038 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002039 n->type = AUDIT_TYPE_PARENT;
Jeff Layton79f65302013-07-08 15:59:36 -07002040 if (flags & AUDIT_INODE_HIDDEN)
2041 n->hidden = true;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002042 } else {
2043 n->name_len = AUDIT_NAME_FULL;
2044 n->type = AUDIT_TYPE_NORMAL;
2045 }
Al Viro74c3cbe2007-07-22 08:04:18 -04002046 handle_path(dentry);
Richard Guy Briggs57d46572019-01-23 13:35:00 -05002047 audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Al Viro9f45f5b2014-10-31 17:44:57 -04002050void __audit_file(const struct file *file)
2051{
2052 __audit_inode(NULL, file->f_path.dentry, 0);
2053}
2054
Amy Griffis73241cc2005-11-03 16:00:25 +00002055/**
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002056 * __audit_inode_child - collect inode info for created/removed objects
Amy Griffis73d3ec52006-07-13 13:16:39 -04002057 * @parent: inode of dentry parent
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002058 * @dentry: dentry being audited
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002059 * @type: AUDIT_TYPE_* value that we're looking for
Amy Griffis73241cc2005-11-03 16:00:25 +00002060 *
2061 * For syscalls that create or remove filesystem objects, audit_inode
2062 * can only collect information for the filesystem object's parent.
2063 * This call updates the audit context with the child's information.
2064 * Syscalls that create a new filesystem object must be hooked after
2065 * the object is created. Syscalls that remove a filesystem object
2066 * must be hooked prior, in order to capture the target inode during
2067 * unsuccessful attempts.
2068 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05002069void __audit_inode_child(struct inode *parent,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002070 const struct dentry *dentry,
2071 const unsigned char type)
Amy Griffis73241cc2005-11-03 16:00:25 +00002072{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002073 struct audit_context *context = audit_context();
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05002074 struct inode *inode = d_backing_inode(dentry);
Al Viro795d6732019-04-26 14:11:05 -04002075 const struct qstr *dname = &dentry->d_name;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002076 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
Richard Guy Briggs42d5e372017-08-23 07:03:39 -04002077 struct audit_entry *e;
2078 struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2079 int i;
Amy Griffis73241cc2005-11-03 16:00:25 +00002080
2081 if (!context->in_syscall)
2082 return;
2083
Richard Guy Briggs42d5e372017-08-23 07:03:39 -04002084 rcu_read_lock();
Richard Guy Briggs699c1862019-04-08 12:50:57 -04002085 list_for_each_entry_rcu(e, list, list) {
2086 for (i = 0; i < e->rule.field_count; i++) {
2087 struct audit_field *f = &e->rule.fields[i];
Richard Guy Briggs42d5e372017-08-23 07:03:39 -04002088
Richard Guy Briggs699c1862019-04-08 12:50:57 -04002089 if (f->type == AUDIT_FSTYPE
2090 && audit_comparator(parent->i_sb->s_magic,
2091 f->op, f->val)
2092 && e->rule.action == AUDIT_NEVER) {
2093 rcu_read_unlock();
2094 return;
Richard Guy Briggs42d5e372017-08-23 07:03:39 -04002095 }
2096 }
2097 }
2098 rcu_read_unlock();
2099
Al Viro74c3cbe2007-07-22 08:04:18 -04002100 if (inode)
2101 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002102
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002103 /* look for a parent entry first */
Eric Paris5195d8e2012-01-03 14:23:05 -05002104 list_for_each_entry(n, &context->names_list, list) {
Paul Moore57c59f52015-01-22 00:00:16 -05002105 if (!n->name ||
2106 (n->type != AUDIT_TYPE_PARENT &&
2107 n->type != AUDIT_TYPE_UNKNOWN))
Amy Griffis5712e882007-02-13 14:15:22 -05002108 continue;
2109
Paul Moore57c59f52015-01-22 00:00:16 -05002110 if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
2111 !audit_compare_dname_path(dname,
2112 n->name->name, n->name_len)) {
2113 if (n->type == AUDIT_TYPE_UNKNOWN)
2114 n->type = AUDIT_TYPE_PARENT;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002115 found_parent = n;
2116 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002117 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002118 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002119
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002120 /* is there a matching child entry? */
Eric Paris5195d8e2012-01-03 14:23:05 -05002121 list_for_each_entry(n, &context->names_list, list) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002122 /* can only match entries that have a name */
Paul Moore57c59f52015-01-22 00:00:16 -05002123 if (!n->name ||
2124 (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002125 continue;
2126
Al Viro795d6732019-04-26 14:11:05 -04002127 if (!strcmp(dname->name, n->name->name) ||
Jeff Layton91a27b22012-10-10 15:25:28 -04002128 !audit_compare_dname_path(dname, n->name->name,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002129 found_parent ?
2130 found_parent->name_len :
Jeff Laytone3d6b072012-10-10 15:25:25 -04002131 AUDIT_NAME_FULL)) {
Paul Moore57c59f52015-01-22 00:00:16 -05002132 if (n->type == AUDIT_TYPE_UNKNOWN)
2133 n->type = type;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002134 found_child = n;
2135 break;
Steve Grubbac9910c2006-09-28 14:31:32 -04002136 }
Amy Griffis5712e882007-02-13 14:15:22 -05002137 }
2138
Amy Griffis5712e882007-02-13 14:15:22 -05002139 if (!found_parent) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002140 /* create a new, "anonymous" parent record */
2141 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
Eric Paris5195d8e2012-01-03 14:23:05 -05002142 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002143 return;
Richard Guy Briggs57d46572019-01-23 13:35:00 -05002144 audit_copy_inode(n, NULL, parent, 0);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002145 }
Amy Griffis5712e882007-02-13 14:15:22 -05002146
2147 if (!found_child) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002148 found_child = audit_alloc_name(context, type);
2149 if (!found_child)
Amy Griffis5712e882007-02-13 14:15:22 -05002150 return;
Amy Griffis5712e882007-02-13 14:15:22 -05002151
2152 /* Re-use the name belonging to the slot for a matching parent
2153 * directory. All names for this context are relinquished in
2154 * audit_free_names() */
2155 if (found_parent) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002156 found_child->name = found_parent->name;
2157 found_child->name_len = AUDIT_NAME_FULL;
Paul Moore55422d02015-01-22 00:00:23 -05002158 found_child->name->refcnt++;
Amy Griffis5712e882007-02-13 14:15:22 -05002159 }
Amy Griffis5712e882007-02-13 14:15:22 -05002160 }
Paul Moore57c59f52015-01-22 00:00:16 -05002161
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002162 if (inode)
Richard Guy Briggs57d46572019-01-23 13:35:00 -05002163 audit_copy_inode(found_child, dentry, inode, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002164 else
Richard Guy Briggs84cb7772015-08-05 23:48:20 -04002165 found_child->ino = AUDIT_INO_UNSET;
Amy Griffis3e2efce2006-07-13 13:16:02 -04002166}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002167EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002168
2169/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002170 * auditsc_get_stamp - get local copies of audit_context values
2171 * @ctx: audit_context for the task
Deepa Dinamani2115bb22017-05-02 10:16:05 -04002172 * @t: timespec64 to store time recorded in the audit_context
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002173 * @serial: serial value that is recorded in the audit_context
2174 *
2175 * Also sets the context as auditable.
2176 */
Al Viro48887e62008-12-06 01:05:50 -05002177int auditsc_get_stamp(struct audit_context *ctx,
Deepa Dinamani2115bb22017-05-02 10:16:05 -04002178 struct timespec64 *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Al Viro48887e62008-12-06 01:05:50 -05002180 if (!ctx->in_syscall)
2181 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002182 if (!ctx->serial)
2183 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002184 t->tv_sec = ctx->ctime.tv_sec;
2185 t->tv_nsec = ctx->ctime.tv_nsec;
2186 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002187 if (!ctx->prio) {
2188 ctx->prio = 1;
2189 ctx->current_state = AUDIT_RECORD_CONTEXT;
2190 }
Al Viro48887e62008-12-06 01:05:50 -05002191 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002194/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002195 * __audit_mq_open - record audit data for a POSIX MQ open
2196 * @oflag: open flag
2197 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002198 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002199 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002200 */
Al Virodf0a4282011-07-26 05:26:10 -04002201void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002202{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002203 struct audit_context *context = audit_context();
George C. Wilson20ca73b2006-05-24 16:09:55 -05002204
Al Viro564f6992008-12-14 04:02:26 -05002205 if (attr)
2206 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2207 else
2208 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002209
Al Viro564f6992008-12-14 04:02:26 -05002210 context->mq_open.oflag = oflag;
2211 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002212
Al Viro564f6992008-12-14 04:02:26 -05002213 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002214}
2215
2216/**
Al Viroc32c8af2008-12-14 03:46:48 -05002217 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002218 * @mqdes: MQ descriptor
2219 * @msg_len: Message length
2220 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002221 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002222 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002223 */
Al Viroc32c8af2008-12-14 03:46:48 -05002224void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
Deepa Dinamanib9047722017-08-02 19:51:11 -07002225 const struct timespec64 *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002226{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002227 struct audit_context *context = audit_context();
Deepa Dinamanib9047722017-08-02 19:51:11 -07002228 struct timespec64 *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002229
Al Viroc32c8af2008-12-14 03:46:48 -05002230 if (abs_timeout)
Deepa Dinamanib9047722017-08-02 19:51:11 -07002231 memcpy(p, abs_timeout, sizeof(*p));
Al Viroc32c8af2008-12-14 03:46:48 -05002232 else
Deepa Dinamanib9047722017-08-02 19:51:11 -07002233 memset(p, 0, sizeof(*p));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002234
Al Viroc32c8af2008-12-14 03:46:48 -05002235 context->mq_sendrecv.mqdes = mqdes;
2236 context->mq_sendrecv.msg_len = msg_len;
2237 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002238
Al Viroc32c8af2008-12-14 03:46:48 -05002239 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002240}
2241
2242/**
2243 * __audit_mq_notify - record audit data for a POSIX MQ notify
2244 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002245 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002246 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002247 */
2248
Al Viro20114f72008-12-10 07:16:12 -05002249void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002250{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002251 struct audit_context *context = audit_context();
George C. Wilson20ca73b2006-05-24 16:09:55 -05002252
Al Viro20114f72008-12-10 07:16:12 -05002253 if (notification)
2254 context->mq_notify.sigev_signo = notification->sigev_signo;
2255 else
2256 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002257
Al Viro20114f72008-12-10 07:16:12 -05002258 context->mq_notify.mqdes = mqdes;
2259 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002260}
2261
2262/**
2263 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2264 * @mqdes: MQ descriptor
2265 * @mqstat: MQ flags
2266 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002267 */
Al Viro73929062008-12-10 06:58:59 -05002268void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002269{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002270 struct audit_context *context = audit_context();
Al Viro73929062008-12-10 06:58:59 -05002271 context->mq_getsetattr.mqdes = mqdes;
2272 context->mq_getsetattr.mqstat = *mqstat;
2273 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002274}
2275
2276/**
Geliang Tang196a5082017-08-07 21:44:24 +08002277 * __audit_ipc_obj - record audit data for ipc object
Steve Grubb073115d2006-04-02 17:07:33 -04002278 * @ipcp: ipc permissions
2279 *
Steve Grubb073115d2006-04-02 17:07:33 -04002280 */
Al Viroa33e6752008-12-10 03:40:06 -05002281void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002282{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002283 struct audit_context *context = audit_context();
Al Viroa33e6752008-12-10 03:40:06 -05002284 context->ipc.uid = ipcp->uid;
2285 context->ipc.gid = ipcp->gid;
2286 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002287 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002288 security_ipc_getsecid(ipcp, &context->ipc.osid);
2289 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002290}
2291
2292/**
Geliang Tang196a5082017-08-07 21:44:24 +08002293 * __audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002294 * @qbytes: msgq bytes
2295 * @uid: msgq user id
2296 * @gid: msgq group id
2297 * @mode: msgq mode (permissions)
2298 *
Al Viroe816f372008-12-10 03:47:15 -05002299 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002300 */
Al Viro2570ebb2011-07-27 14:03:22 -04002301void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002303 struct audit_context *context = audit_context();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Al Viroe816f372008-12-10 03:47:15 -05002305 context->ipc.qbytes = qbytes;
2306 context->ipc.perm_uid = uid;
2307 context->ipc.perm_gid = gid;
2308 context->ipc.perm_mode = mode;
2309 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002311
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002312void __audit_bprm(struct linux_binprm *bprm)
Al Viro473ae302006-04-26 14:04:08 -04002313{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002314 struct audit_context *context = audit_context();
Al Viro473ae302006-04-26 14:04:08 -04002315
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002316 context->type = AUDIT_EXECVE;
2317 context->execve.argc = bprm->argc;
Al Viro473ae302006-04-26 14:04:08 -04002318}
2319
2320
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002321/**
Geliang Tang196a5082017-08-07 21:44:24 +08002322 * __audit_socketcall - record audit data for sys_socketcall
Chen Gang2950fa92013-04-07 16:55:23 +08002323 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002324 * @args: args array
2325 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002326 */
Chen Gang2950fa92013-04-07 16:55:23 +08002327int __audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002328{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002329 struct audit_context *context = audit_context();
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002330
Chen Gang2950fa92013-04-07 16:55:23 +08002331 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2332 return -EINVAL;
Al Virof3298dc2008-12-10 03:16:51 -05002333 context->type = AUDIT_SOCKETCALL;
2334 context->socketcall.nargs = nargs;
2335 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
Chen Gang2950fa92013-04-07 16:55:23 +08002336 return 0;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002337}
2338
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002339/**
Al Virodb349502007-02-07 01:48:00 -05002340 * __audit_fd_pair - record audit data for pipe and socketpair
2341 * @fd1: the first file descriptor
2342 * @fd2: the second file descriptor
2343 *
Al Virodb349502007-02-07 01:48:00 -05002344 */
Al Viro157cf642008-12-14 04:57:47 -05002345void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002346{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002347 struct audit_context *context = audit_context();
Al Viro157cf642008-12-14 04:57:47 -05002348 context->fds[0] = fd1;
2349 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002350}
2351
2352/**
Geliang Tang196a5082017-08-07 21:44:24 +08002353 * __audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002354 * @len: data length in user space
2355 * @a: data address in kernel space
2356 *
2357 * Returns 0 for success or NULL context or < 0 on error.
2358 */
Eric Paris07c49412012-01-03 14:23:07 -05002359int __audit_sockaddr(int len, void *a)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002360{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002361 struct audit_context *context = audit_context();
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002362
Al Viro4f6b4342008-12-09 19:50:34 -05002363 if (!context->sockaddr) {
2364 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2365 if (!p)
2366 return -ENOMEM;
2367 context->sockaddr = p;
2368 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002369
Al Viro4f6b4342008-12-09 19:50:34 -05002370 context->sockaddr_len = len;
2371 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002372 return 0;
2373}
2374
Al Viroa5cb0132007-03-20 13:58:35 -04002375void __audit_ptrace(struct task_struct *t)
2376{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002377 struct audit_context *context = audit_context();
Al Viroa5cb0132007-03-20 13:58:35 -04002378
Paul Moorefa2bea22016-08-30 17:19:13 -04002379 context->target_pid = task_tgid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002380 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002381 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002382 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002383 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002384 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002385}
2386
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002387/**
Richard Guy Briggsb48345a2019-05-10 12:21:49 -04002388 * audit_signal_info_syscall - record signal info for syscalls
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002389 * @t: task being signaled
2390 *
2391 * If the audit subsystem is being terminated, record the task (pid)
2392 * and uid that is doing that.
2393 */
Richard Guy Briggsb48345a2019-05-10 12:21:49 -04002394int audit_signal_info_syscall(struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002395{
Amy Griffise54dc242007-03-29 18:01:04 -04002396 struct audit_aux_data_pids *axp;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002397 struct audit_context *ctx = audit_context();
Richard Guy Briggsb48345a2019-05-10 12:21:49 -04002398 kuid_t t_uid = task_uid(t);
Amy Griffise54dc242007-03-29 18:01:04 -04002399
Paul Mooreab6434a2017-03-27 14:30:06 -04002400 if (!audit_signals || audit_dummy_context())
2401 return 0;
2402
Amy Griffise54dc242007-03-29 18:01:04 -04002403 /* optimize the common case by putting first signal recipient directly
2404 * in audit_context */
2405 if (!ctx->target_pid) {
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002406 ctx->target_pid = task_tgid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002407 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002408 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002409 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002410 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002411 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002412 return 0;
2413 }
2414
2415 axp = (void *)ctx->aux_pids;
2416 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2417 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2418 if (!axp)
2419 return -ENOMEM;
2420
2421 axp->d.type = AUDIT_OBJ_PID;
2422 axp->d.next = ctx->aux_pids;
2423 ctx->aux_pids = (void *)axp;
2424 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002425 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002426
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002427 axp->target_pid[axp->pid_count] = task_tgid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002428 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002429 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002430 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002431 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002432 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002433 axp->pid_count++;
2434
2435 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002436}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002437
2438/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002439 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002440 * @bprm: pointer to the bprm being processed
2441 * @new: the proposed new credentials
2442 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002443 *
2444 * Simply check if the proc already has the caps given by the file and if not
2445 * store the priv escalation info for later auditing at the end of the syscall
2446 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002447 * -Eric
2448 */
David Howellsd84f4f92008-11-14 10:39:23 +11002449int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2450 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002451{
2452 struct audit_aux_data_bprm_fcaps *ax;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002453 struct audit_context *context = audit_context();
Eric Paris3fc689e2008-11-11 21:48:18 +11002454 struct cpu_vfs_cap_data vcaps;
Eric Paris3fc689e2008-11-11 21:48:18 +11002455
2456 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2457 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002458 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002459
2460 ax->d.type = AUDIT_BPRM_FCAPS;
2461 ax->d.next = context->aux;
2462 context->aux = (void *)ax;
2463
Al Virof4a4a8b2014-12-28 09:27:07 -05002464 get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Paris3fc689e2008-11-11 21:48:18 +11002465
2466 ax->fcap.permitted = vcaps.permitted;
2467 ax->fcap.inheritable = vcaps.inheritable;
2468 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
Richard Guy Briggs2fec30e2019-01-23 21:36:25 -05002469 ax->fcap.rootid = vcaps.rootid;
Eric Paris3fc689e2008-11-11 21:48:18 +11002470 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2471
David Howellsd84f4f92008-11-14 10:39:23 +11002472 ax->old_pcap.permitted = old->cap_permitted;
2473 ax->old_pcap.inheritable = old->cap_inheritable;
2474 ax->old_pcap.effective = old->cap_effective;
Richard Guy Briggs7786f6b2017-04-07 10:17:27 -04002475 ax->old_pcap.ambient = old->cap_ambient;
Eric Paris3fc689e2008-11-11 21:48:18 +11002476
David Howellsd84f4f92008-11-14 10:39:23 +11002477 ax->new_pcap.permitted = new->cap_permitted;
2478 ax->new_pcap.inheritable = new->cap_inheritable;
2479 ax->new_pcap.effective = new->cap_effective;
Richard Guy Briggs7786f6b2017-04-07 10:17:27 -04002480 ax->new_pcap.ambient = new->cap_ambient;
David Howellsd84f4f92008-11-14 10:39:23 +11002481 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002482}
2483
2484/**
Eric Parise68b75a02008-11-11 21:48:22 +11002485 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002486 * @new: the new credentials
2487 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002488 *
Masanari Iidada3dae52014-09-09 01:27:23 +09002489 * Record the arguments userspace sent to sys_capset for later printing by the
Eric Parise68b75a02008-11-11 21:48:22 +11002490 * audit system if applicable
2491 */
Eric W. Biedermanca24a232013-03-19 00:02:25 -07002492void __audit_log_capset(const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002493{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002494 struct audit_context *context = audit_context();
Paul Moorefa2bea22016-08-30 17:19:13 -04002495 context->capset.pid = task_tgid_nr(current);
Al Viro57f71a02009-01-04 14:52:57 -05002496 context->capset.cap.effective = new->cap_effective;
2497 context->capset.cap.inheritable = new->cap_effective;
2498 context->capset.cap.permitted = new->cap_permitted;
Richard Guy Briggs7786f6b2017-04-07 10:17:27 -04002499 context->capset.cap.ambient = new->cap_ambient;
Al Viro57f71a02009-01-04 14:52:57 -05002500 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002501}
2502
Al Viro120a7952010-10-30 02:54:44 -04002503void __audit_mmap_fd(int fd, int flags)
2504{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002505 struct audit_context *context = audit_context();
Al Viro120a7952010-10-30 02:54:44 -04002506 context->mmap.fd = fd;
2507 context->mmap.flags = flags;
2508 context->type = AUDIT_MMAP;
2509}
2510
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05002511void __audit_log_kern_module(char *name)
2512{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002513 struct audit_context *context = audit_context();
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05002514
Yi Wangb305f7e2018-07-25 10:26:19 +08002515 context->module.name = kstrdup(name, GFP_KERNEL);
2516 if (!context->module.name)
2517 audit_log_lost("out of memory in __audit_log_kern_module");
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05002518 context->type = AUDIT_KERN_MODULE;
2519}
2520
Steve Grubbde8cd832017-10-02 20:21:39 -04002521void __audit_fanotify(unsigned int response)
2522{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002523 audit_log(audit_context(), GFP_KERNEL,
Steve Grubbde8cd832017-10-02 20:21:39 -04002524 AUDIT_FANOTIFY, "resp=%u", response);
2525}
2526
Ondrej Mosnacek2d87a062019-04-10 11:14:19 +02002527void __audit_tk_injoffset(struct timespec64 offset)
2528{
2529 audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_INJOFFSET,
2530 "sec=%lli nsec=%li",
2531 (long long)offset.tv_sec, offset.tv_nsec);
2532}
2533
Ondrej Mosnacek7e8eda72019-04-10 11:14:20 +02002534static void audit_log_ntp_val(const struct audit_ntp_data *ad,
2535 const char *op, enum audit_ntp_type type)
2536{
2537 const struct audit_ntp_val *val = &ad->vals[type];
2538
2539 if (val->newval == val->oldval)
2540 return;
2541
2542 audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_ADJNTPVAL,
2543 "op=%s old=%lli new=%lli", op, val->oldval, val->newval);
2544}
2545
2546void __audit_ntp_log(const struct audit_ntp_data *ad)
2547{
2548 audit_log_ntp_val(ad, "offset", AUDIT_NTP_OFFSET);
2549 audit_log_ntp_val(ad, "freq", AUDIT_NTP_FREQ);
2550 audit_log_ntp_val(ad, "status", AUDIT_NTP_STATUS);
2551 audit_log_ntp_val(ad, "tai", AUDIT_NTP_TAI);
2552 audit_log_ntp_val(ad, "tick", AUDIT_NTP_TICK);
2553 audit_log_ntp_val(ad, "adjust", AUDIT_NTP_ADJUST);
2554}
2555
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -04002556void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
2557 enum audit_nfcfgop op)
2558{
2559 struct audit_buffer *ab;
Richard Guy Briggs9d44a122020-05-20 14:47:13 -04002560 char comm[sizeof(current->comm)];
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -04002561
2562 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_NETFILTER_CFG);
2563 if (!ab)
2564 return;
2565 audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
2566 name, af, nentries, audit_nfcfgs[op].s);
Richard Guy Briggs9d44a122020-05-20 14:47:13 -04002567
2568 audit_log_format(ab, " pid=%u", task_pid_nr(current));
2569 audit_log_task_context(ab); /* subj= */
2570 audit_log_format(ab, " comm=");
2571 audit_log_untrustedstring(ab, get_task_comm(comm, current));
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -04002572 audit_log_end(ab);
2573}
2574EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
2575
Kees Cook7b9205b2013-01-11 14:32:05 -08002576static void audit_log_task(struct audit_buffer *ab)
Eric Paris85e7bac32012-01-03 14:23:05 -05002577{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002578 kuid_t auid, uid;
2579 kgid_t gid;
Eric Paris85e7bac32012-01-03 14:23:05 -05002580 unsigned int sessionid;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002581 char comm[sizeof(current->comm)];
Eric Paris85e7bac32012-01-03 14:23:05 -05002582
2583 auid = audit_get_loginuid(current);
2584 sessionid = audit_get_sessionid(current);
2585 current_uid_gid(&uid, &gid);
2586
2587 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002588 from_kuid(&init_user_ns, auid),
2589 from_kuid(&init_user_ns, uid),
2590 from_kgid(&init_user_ns, gid),
2591 sessionid);
Eric Paris85e7bac32012-01-03 14:23:05 -05002592 audit_log_task_context(ab);
Paul Moorefa2bea22016-08-30 17:19:13 -04002593 audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002594 audit_log_untrustedstring(ab, get_task_comm(comm, current));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002595 audit_log_d_path_exe(ab, current->mm);
Kees Cook7b9205b2013-01-11 14:32:05 -08002596}
2597
Eric Parise68b75a02008-11-11 21:48:22 +11002598/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002599 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002600 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002601 *
2602 * If a process ends with a core dump, something fishy is going on and we
2603 * should record the event for investigation.
2604 */
2605void audit_core_dumps(long signr)
2606{
2607 struct audit_buffer *ab;
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002608
2609 if (!audit_enabled)
2610 return;
2611
2612 if (signr == SIGQUIT) /* don't care for those */
2613 return;
2614
Richard Guy Briggsd87de4a2018-05-31 16:28:12 -04002615 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_ABEND);
Kees Cook0644ec02013-01-11 14:32:07 -08002616 if (unlikely(!ab))
2617 return;
Paul Davies C61c0ee82013-11-08 09:57:39 +05302618 audit_log_task(ab);
Steve Grubb89670af2016-12-14 16:00:13 -05002619 audit_log_format(ab, " sig=%ld res=1", signr);
Eric Paris85e7bac32012-01-03 14:23:05 -05002620 audit_log_end(ab);
2621}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002622
Tyler Hicks326bee02018-05-04 01:08:15 +00002623/**
2624 * audit_seccomp - record information about a seccomp action
2625 * @syscall: syscall number
2626 * @signr: signal value
2627 * @code: the seccomp action
2628 *
2629 * Record the information associated with a seccomp action. Event filtering for
2630 * seccomp actions that are not to be logged is done in seccomp_log().
2631 * Therefore, this function forces auditing independent of the audit_enabled
2632 * and dummy context state because seccomp actions should be logged even when
2633 * audit is not in use.
2634 */
2635void audit_seccomp(unsigned long syscall, long signr, int code)
Eric Paris85e7bac32012-01-03 14:23:05 -05002636{
2637 struct audit_buffer *ab;
2638
Richard Guy Briggs9b8753f2018-05-31 16:27:24 -04002639 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_SECCOMP);
Kees Cook7b9205b2013-01-11 14:32:05 -08002640 if (unlikely(!ab))
2641 return;
2642 audit_log_task(ab);
Richard Guy Briggs84db5642014-01-29 16:17:58 -05002643 audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
Dmitry V. Levin16add412019-03-18 02:30:18 +03002644 signr, syscall_get_arch(current), syscall,
Andy Lutomirskiefbc0fb2016-03-22 14:24:58 -07002645 in_compat_syscall(), KSTK_EIP(current), code);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002646 audit_log_end(ab);
2647}
Al Viro916d7572009-06-24 00:02:38 -04002648
Tyler Hicksea6eca72018-05-04 01:08:14 +00002649void audit_seccomp_actions_logged(const char *names, const char *old_names,
2650 int res)
2651{
2652 struct audit_buffer *ab;
2653
2654 if (!audit_enabled)
2655 return;
2656
Richard Guy Briggs8982a1f2018-05-16 07:55:45 -04002657 ab = audit_log_start(audit_context(), GFP_KERNEL,
Tyler Hicksea6eca72018-05-04 01:08:14 +00002658 AUDIT_CONFIG_CHANGE);
2659 if (unlikely(!ab))
2660 return;
2661
Paul Moored0a3f182018-08-02 17:56:50 -04002662 audit_log_format(ab,
2663 "op=seccomp-logging actions=%s old-actions=%s res=%d",
2664 names, old_names, res);
Tyler Hicksea6eca72018-05-04 01:08:14 +00002665 audit_log_end(ab);
2666}
2667
Al Viro916d7572009-06-24 00:02:38 -04002668struct list_head *audit_killed_trees(void)
2669{
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04002670 struct audit_context *ctx = audit_context();
Al Viro916d7572009-06-24 00:02:38 -04002671 if (likely(!ctx || !ctx->in_syscall))
2672 return NULL;
2673 return &ctx->killed_trees;
2674}