1*4882a593Smuzhiyun /* auditsc.c -- System-call auditing support
2*4882a593Smuzhiyun * Handles all system-call specific auditing features.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5*4882a593Smuzhiyun * Copyright 2005 Hewlett-Packard Development Company, L.P.
6*4882a593Smuzhiyun * Copyright (C) 2005, 2006 IBM Corporation
7*4882a593Smuzhiyun * All Rights Reserved.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun * it under the terms of the GNU General Public License as published by
11*4882a593Smuzhiyun * the Free Software Foundation; either version 2 of the License, or
12*4882a593Smuzhiyun * (at your option) any later version.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
15*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*4882a593Smuzhiyun * GNU General Public License for more details.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun * along with this program; if not, write to the Free Software
21*4882a593Smuzhiyun * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Many of the ideas implemented here are from Stephen C. Tweedie,
26*4882a593Smuzhiyun * especially the idea of avoiding a copy by using getname.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * The method for actual interception of syscall entry and exit (not in
29*4882a593Smuzhiyun * this file -- see entry.S) is based on a GPL'd patch written by
30*4882a593Smuzhiyun * okir@suse.de and Copyright 2003 SuSE Linux AG.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33*4882a593Smuzhiyun * 2006.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * The support of additional filter rules compares (>, <, >=, <=) was
36*4882a593Smuzhiyun * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39*4882a593Smuzhiyun * filesystem information.
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * Subject and object context labeling support added by <danjones@us.ibm.com>
42*4882a593Smuzhiyun * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #include <linux/init.h>
48*4882a593Smuzhiyun #include <asm/types.h>
49*4882a593Smuzhiyun #include <linux/atomic.h>
50*4882a593Smuzhiyun #include <linux/fs.h>
51*4882a593Smuzhiyun #include <linux/namei.h>
52*4882a593Smuzhiyun #include <linux/mm.h>
53*4882a593Smuzhiyun #include <linux/export.h>
54*4882a593Smuzhiyun #include <linux/slab.h>
55*4882a593Smuzhiyun #include <linux/mount.h>
56*4882a593Smuzhiyun #include <linux/socket.h>
57*4882a593Smuzhiyun #include <linux/mqueue.h>
58*4882a593Smuzhiyun #include <linux/audit.h>
59*4882a593Smuzhiyun #include <linux/personality.h>
60*4882a593Smuzhiyun #include <linux/time.h>
61*4882a593Smuzhiyun #include <linux/netlink.h>
62*4882a593Smuzhiyun #include <linux/compiler.h>
63*4882a593Smuzhiyun #include <asm/unistd.h>
64*4882a593Smuzhiyun #include <linux/security.h>
65*4882a593Smuzhiyun #include <linux/list.h>
66*4882a593Smuzhiyun #include <linux/binfmts.h>
67*4882a593Smuzhiyun #include <linux/highmem.h>
68*4882a593Smuzhiyun #include <linux/syscalls.h>
69*4882a593Smuzhiyun #include <asm/syscall.h>
70*4882a593Smuzhiyun #include <linux/capability.h>
71*4882a593Smuzhiyun #include <linux/fs_struct.h>
72*4882a593Smuzhiyun #include <linux/compat.h>
73*4882a593Smuzhiyun #include <linux/ctype.h>
74*4882a593Smuzhiyun #include <linux/string.h>
75*4882a593Smuzhiyun #include <linux/uaccess.h>
76*4882a593Smuzhiyun #include <linux/fsnotify_backend.h>
77*4882a593Smuzhiyun #include <uapi/linux/limits.h>
78*4882a593Smuzhiyun #include <uapi/linux/netfilter/nf_tables.h>
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #include "audit.h"
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* flags stating the success for a syscall */
83*4882a593Smuzhiyun #define AUDITSC_INVALID 0
84*4882a593Smuzhiyun #define AUDITSC_SUCCESS 1
85*4882a593Smuzhiyun #define AUDITSC_FAILURE 2
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* no execve audit message should be longer than this (userspace limits),
88*4882a593Smuzhiyun * see the note near the top of audit_log_execve_info() about this value */
89*4882a593Smuzhiyun #define MAX_EXECVE_AUDIT_LEN 7500
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* max length to print of cmdline/proctitle value during audit */
92*4882a593Smuzhiyun #define MAX_PROCTITLE_AUDIT_LEN 128
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* number of audit rules */
95*4882a593Smuzhiyun int audit_n_rules;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* determines whether we collect data for signals sent */
98*4882a593Smuzhiyun int audit_signals;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun struct audit_aux_data {
101*4882a593Smuzhiyun struct audit_aux_data *next;
102*4882a593Smuzhiyun int type;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun #define AUDIT_AUX_IPCPERM 0
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* Number of target pids per aux struct. */
108*4882a593Smuzhiyun #define AUDIT_AUX_PIDS 16
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun struct audit_aux_data_pids {
111*4882a593Smuzhiyun struct audit_aux_data d;
112*4882a593Smuzhiyun pid_t target_pid[AUDIT_AUX_PIDS];
113*4882a593Smuzhiyun kuid_t target_auid[AUDIT_AUX_PIDS];
114*4882a593Smuzhiyun kuid_t target_uid[AUDIT_AUX_PIDS];
115*4882a593Smuzhiyun unsigned int target_sessionid[AUDIT_AUX_PIDS];
116*4882a593Smuzhiyun u32 target_sid[AUDIT_AUX_PIDS];
117*4882a593Smuzhiyun char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
118*4882a593Smuzhiyun int pid_count;
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun struct audit_aux_data_bprm_fcaps {
122*4882a593Smuzhiyun struct audit_aux_data d;
123*4882a593Smuzhiyun struct audit_cap_data fcap;
124*4882a593Smuzhiyun unsigned int fcap_ver;
125*4882a593Smuzhiyun struct audit_cap_data old_pcap;
126*4882a593Smuzhiyun struct audit_cap_data new_pcap;
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun struct audit_tree_refs {
130*4882a593Smuzhiyun struct audit_tree_refs *next;
131*4882a593Smuzhiyun struct audit_chunk *c[31];
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun struct audit_nfcfgop_tab {
135*4882a593Smuzhiyun enum audit_nfcfgop op;
136*4882a593Smuzhiyun const char *s;
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
140*4882a593Smuzhiyun { AUDIT_XT_OP_REGISTER, "xt_register" },
141*4882a593Smuzhiyun { AUDIT_XT_OP_REPLACE, "xt_replace" },
142*4882a593Smuzhiyun { AUDIT_XT_OP_UNREGISTER, "xt_unregister" },
143*4882a593Smuzhiyun { AUDIT_NFT_OP_TABLE_REGISTER, "nft_register_table" },
144*4882a593Smuzhiyun { AUDIT_NFT_OP_TABLE_UNREGISTER, "nft_unregister_table" },
145*4882a593Smuzhiyun { AUDIT_NFT_OP_CHAIN_REGISTER, "nft_register_chain" },
146*4882a593Smuzhiyun { AUDIT_NFT_OP_CHAIN_UNREGISTER, "nft_unregister_chain" },
147*4882a593Smuzhiyun { AUDIT_NFT_OP_RULE_REGISTER, "nft_register_rule" },
148*4882a593Smuzhiyun { AUDIT_NFT_OP_RULE_UNREGISTER, "nft_unregister_rule" },
149*4882a593Smuzhiyun { AUDIT_NFT_OP_SET_REGISTER, "nft_register_set" },
150*4882a593Smuzhiyun { AUDIT_NFT_OP_SET_UNREGISTER, "nft_unregister_set" },
151*4882a593Smuzhiyun { AUDIT_NFT_OP_SETELEM_REGISTER, "nft_register_setelem" },
152*4882a593Smuzhiyun { AUDIT_NFT_OP_SETELEM_UNREGISTER, "nft_unregister_setelem" },
153*4882a593Smuzhiyun { AUDIT_NFT_OP_GEN_REGISTER, "nft_register_gen" },
154*4882a593Smuzhiyun { AUDIT_NFT_OP_OBJ_REGISTER, "nft_register_obj" },
155*4882a593Smuzhiyun { AUDIT_NFT_OP_OBJ_UNREGISTER, "nft_unregister_obj" },
156*4882a593Smuzhiyun { AUDIT_NFT_OP_OBJ_RESET, "nft_reset_obj" },
157*4882a593Smuzhiyun { AUDIT_NFT_OP_FLOWTABLE_REGISTER, "nft_register_flowtable" },
158*4882a593Smuzhiyun { AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, "nft_unregister_flowtable" },
159*4882a593Smuzhiyun { AUDIT_NFT_OP_INVALID, "nft_invalid" },
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun
audit_match_perm(struct audit_context * ctx,int mask)162*4882a593Smuzhiyun static int audit_match_perm(struct audit_context *ctx, int mask)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun unsigned n;
165*4882a593Smuzhiyun if (unlikely(!ctx))
166*4882a593Smuzhiyun return 0;
167*4882a593Smuzhiyun n = ctx->major;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun switch (audit_classify_syscall(ctx->arch, n)) {
170*4882a593Smuzhiyun case 0: /* native */
171*4882a593Smuzhiyun if ((mask & AUDIT_PERM_WRITE) &&
172*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_WRITE, n))
173*4882a593Smuzhiyun return 1;
174*4882a593Smuzhiyun if ((mask & AUDIT_PERM_READ) &&
175*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_READ, n))
176*4882a593Smuzhiyun return 1;
177*4882a593Smuzhiyun if ((mask & AUDIT_PERM_ATTR) &&
178*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_CHATTR, n))
179*4882a593Smuzhiyun return 1;
180*4882a593Smuzhiyun return 0;
181*4882a593Smuzhiyun case 1: /* 32bit on biarch */
182*4882a593Smuzhiyun if ((mask & AUDIT_PERM_WRITE) &&
183*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_WRITE_32, n))
184*4882a593Smuzhiyun return 1;
185*4882a593Smuzhiyun if ((mask & AUDIT_PERM_READ) &&
186*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_READ_32, n))
187*4882a593Smuzhiyun return 1;
188*4882a593Smuzhiyun if ((mask & AUDIT_PERM_ATTR) &&
189*4882a593Smuzhiyun audit_match_class(AUDIT_CLASS_CHATTR_32, n))
190*4882a593Smuzhiyun return 1;
191*4882a593Smuzhiyun return 0;
192*4882a593Smuzhiyun case 2: /* open */
193*4882a593Smuzhiyun return mask & ACC_MODE(ctx->argv[1]);
194*4882a593Smuzhiyun case 3: /* openat */
195*4882a593Smuzhiyun return mask & ACC_MODE(ctx->argv[2]);
196*4882a593Smuzhiyun case 4: /* socketcall */
197*4882a593Smuzhiyun return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
198*4882a593Smuzhiyun case 5: /* execve */
199*4882a593Smuzhiyun return mask & AUDIT_PERM_EXEC;
200*4882a593Smuzhiyun default:
201*4882a593Smuzhiyun return 0;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
audit_match_filetype(struct audit_context * ctx,int val)205*4882a593Smuzhiyun static int audit_match_filetype(struct audit_context *ctx, int val)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct audit_names *n;
208*4882a593Smuzhiyun umode_t mode = (umode_t)val;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (unlikely(!ctx))
211*4882a593Smuzhiyun return 0;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
214*4882a593Smuzhiyun if ((n->ino != AUDIT_INO_UNSET) &&
215*4882a593Smuzhiyun ((n->mode & S_IFMT) == mode))
216*4882a593Smuzhiyun return 1;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
224*4882a593Smuzhiyun * ->first_trees points to its beginning, ->trees - to the current end of data.
225*4882a593Smuzhiyun * ->tree_count is the number of free entries in array pointed to by ->trees.
226*4882a593Smuzhiyun * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
227*4882a593Smuzhiyun * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
228*4882a593Smuzhiyun * it's going to remain 1-element for almost any setup) until we free context itself.
229*4882a593Smuzhiyun * References in it _are_ dropped - at the same time we free/drop aux stuff.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun
audit_set_auditable(struct audit_context * ctx)232*4882a593Smuzhiyun static void audit_set_auditable(struct audit_context *ctx)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun if (!ctx->prio) {
235*4882a593Smuzhiyun ctx->prio = 1;
236*4882a593Smuzhiyun ctx->current_state = AUDIT_RECORD_CONTEXT;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
put_tree_ref(struct audit_context * ctx,struct audit_chunk * chunk)240*4882a593Smuzhiyun static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun struct audit_tree_refs *p = ctx->trees;
243*4882a593Smuzhiyun int left = ctx->tree_count;
244*4882a593Smuzhiyun if (likely(left)) {
245*4882a593Smuzhiyun p->c[--left] = chunk;
246*4882a593Smuzhiyun ctx->tree_count = left;
247*4882a593Smuzhiyun return 1;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun if (!p)
250*4882a593Smuzhiyun return 0;
251*4882a593Smuzhiyun p = p->next;
252*4882a593Smuzhiyun if (p) {
253*4882a593Smuzhiyun p->c[30] = chunk;
254*4882a593Smuzhiyun ctx->trees = p;
255*4882a593Smuzhiyun ctx->tree_count = 30;
256*4882a593Smuzhiyun return 1;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun return 0;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
grow_tree_refs(struct audit_context * ctx)261*4882a593Smuzhiyun static int grow_tree_refs(struct audit_context *ctx)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun struct audit_tree_refs *p = ctx->trees;
264*4882a593Smuzhiyun ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
265*4882a593Smuzhiyun if (!ctx->trees) {
266*4882a593Smuzhiyun ctx->trees = p;
267*4882a593Smuzhiyun return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun if (p)
270*4882a593Smuzhiyun p->next = ctx->trees;
271*4882a593Smuzhiyun else
272*4882a593Smuzhiyun ctx->first_trees = ctx->trees;
273*4882a593Smuzhiyun ctx->tree_count = 31;
274*4882a593Smuzhiyun return 1;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
unroll_tree_refs(struct audit_context * ctx,struct audit_tree_refs * p,int count)277*4882a593Smuzhiyun static void unroll_tree_refs(struct audit_context *ctx,
278*4882a593Smuzhiyun struct audit_tree_refs *p, int count)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun struct audit_tree_refs *q;
281*4882a593Smuzhiyun int n;
282*4882a593Smuzhiyun if (!p) {
283*4882a593Smuzhiyun /* we started with empty chain */
284*4882a593Smuzhiyun p = ctx->first_trees;
285*4882a593Smuzhiyun count = 31;
286*4882a593Smuzhiyun /* if the very first allocation has failed, nothing to do */
287*4882a593Smuzhiyun if (!p)
288*4882a593Smuzhiyun return;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun n = count;
291*4882a593Smuzhiyun for (q = p; q != ctx->trees; q = q->next, n = 31) {
292*4882a593Smuzhiyun while (n--) {
293*4882a593Smuzhiyun audit_put_chunk(q->c[n]);
294*4882a593Smuzhiyun q->c[n] = NULL;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun while (n-- > ctx->tree_count) {
298*4882a593Smuzhiyun audit_put_chunk(q->c[n]);
299*4882a593Smuzhiyun q->c[n] = NULL;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun ctx->trees = p;
302*4882a593Smuzhiyun ctx->tree_count = count;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
free_tree_refs(struct audit_context * ctx)305*4882a593Smuzhiyun static void free_tree_refs(struct audit_context *ctx)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun struct audit_tree_refs *p, *q;
308*4882a593Smuzhiyun for (p = ctx->first_trees; p; p = q) {
309*4882a593Smuzhiyun q = p->next;
310*4882a593Smuzhiyun kfree(p);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
match_tree_refs(struct audit_context * ctx,struct audit_tree * tree)314*4882a593Smuzhiyun static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun struct audit_tree_refs *p;
317*4882a593Smuzhiyun int n;
318*4882a593Smuzhiyun if (!tree)
319*4882a593Smuzhiyun return 0;
320*4882a593Smuzhiyun /* full ones */
321*4882a593Smuzhiyun for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
322*4882a593Smuzhiyun for (n = 0; n < 31; n++)
323*4882a593Smuzhiyun if (audit_tree_match(p->c[n], tree))
324*4882a593Smuzhiyun return 1;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun /* partial */
327*4882a593Smuzhiyun if (p) {
328*4882a593Smuzhiyun for (n = ctx->tree_count; n < 31; n++)
329*4882a593Smuzhiyun if (audit_tree_match(p->c[n], tree))
330*4882a593Smuzhiyun return 1;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun return 0;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
audit_compare_uid(kuid_t uid,struct audit_names * name,struct audit_field * f,struct audit_context * ctx)335*4882a593Smuzhiyun static int audit_compare_uid(kuid_t uid,
336*4882a593Smuzhiyun struct audit_names *name,
337*4882a593Smuzhiyun struct audit_field *f,
338*4882a593Smuzhiyun struct audit_context *ctx)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun struct audit_names *n;
341*4882a593Smuzhiyun int rc;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun if (name) {
344*4882a593Smuzhiyun rc = audit_uid_comparator(uid, f->op, name->uid);
345*4882a593Smuzhiyun if (rc)
346*4882a593Smuzhiyun return rc;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun if (ctx) {
350*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
351*4882a593Smuzhiyun rc = audit_uid_comparator(uid, f->op, n->uid);
352*4882a593Smuzhiyun if (rc)
353*4882a593Smuzhiyun return rc;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun return 0;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
audit_compare_gid(kgid_t gid,struct audit_names * name,struct audit_field * f,struct audit_context * ctx)359*4882a593Smuzhiyun static int audit_compare_gid(kgid_t gid,
360*4882a593Smuzhiyun struct audit_names *name,
361*4882a593Smuzhiyun struct audit_field *f,
362*4882a593Smuzhiyun struct audit_context *ctx)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun struct audit_names *n;
365*4882a593Smuzhiyun int rc;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun if (name) {
368*4882a593Smuzhiyun rc = audit_gid_comparator(gid, f->op, name->gid);
369*4882a593Smuzhiyun if (rc)
370*4882a593Smuzhiyun return rc;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun if (ctx) {
374*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
375*4882a593Smuzhiyun rc = audit_gid_comparator(gid, f->op, n->gid);
376*4882a593Smuzhiyun if (rc)
377*4882a593Smuzhiyun return rc;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun return 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
audit_field_compare(struct task_struct * tsk,const struct cred * cred,struct audit_field * f,struct audit_context * ctx,struct audit_names * name)383*4882a593Smuzhiyun static int audit_field_compare(struct task_struct *tsk,
384*4882a593Smuzhiyun const struct cred *cred,
385*4882a593Smuzhiyun struct audit_field *f,
386*4882a593Smuzhiyun struct audit_context *ctx,
387*4882a593Smuzhiyun struct audit_names *name)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun switch (f->val) {
390*4882a593Smuzhiyun /* process to file object comparisons */
391*4882a593Smuzhiyun case AUDIT_COMPARE_UID_TO_OBJ_UID:
392*4882a593Smuzhiyun return audit_compare_uid(cred->uid, name, f, ctx);
393*4882a593Smuzhiyun case AUDIT_COMPARE_GID_TO_OBJ_GID:
394*4882a593Smuzhiyun return audit_compare_gid(cred->gid, name, f, ctx);
395*4882a593Smuzhiyun case AUDIT_COMPARE_EUID_TO_OBJ_UID:
396*4882a593Smuzhiyun return audit_compare_uid(cred->euid, name, f, ctx);
397*4882a593Smuzhiyun case AUDIT_COMPARE_EGID_TO_OBJ_GID:
398*4882a593Smuzhiyun return audit_compare_gid(cred->egid, name, f, ctx);
399*4882a593Smuzhiyun case AUDIT_COMPARE_AUID_TO_OBJ_UID:
400*4882a593Smuzhiyun return audit_compare_uid(audit_get_loginuid(tsk), name, f, ctx);
401*4882a593Smuzhiyun case AUDIT_COMPARE_SUID_TO_OBJ_UID:
402*4882a593Smuzhiyun return audit_compare_uid(cred->suid, name, f, ctx);
403*4882a593Smuzhiyun case AUDIT_COMPARE_SGID_TO_OBJ_GID:
404*4882a593Smuzhiyun return audit_compare_gid(cred->sgid, name, f, ctx);
405*4882a593Smuzhiyun case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
406*4882a593Smuzhiyun return audit_compare_uid(cred->fsuid, name, f, ctx);
407*4882a593Smuzhiyun case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
408*4882a593Smuzhiyun return audit_compare_gid(cred->fsgid, name, f, ctx);
409*4882a593Smuzhiyun /* uid comparisons */
410*4882a593Smuzhiyun case AUDIT_COMPARE_UID_TO_AUID:
411*4882a593Smuzhiyun return audit_uid_comparator(cred->uid, f->op,
412*4882a593Smuzhiyun audit_get_loginuid(tsk));
413*4882a593Smuzhiyun case AUDIT_COMPARE_UID_TO_EUID:
414*4882a593Smuzhiyun return audit_uid_comparator(cred->uid, f->op, cred->euid);
415*4882a593Smuzhiyun case AUDIT_COMPARE_UID_TO_SUID:
416*4882a593Smuzhiyun return audit_uid_comparator(cred->uid, f->op, cred->suid);
417*4882a593Smuzhiyun case AUDIT_COMPARE_UID_TO_FSUID:
418*4882a593Smuzhiyun return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
419*4882a593Smuzhiyun /* auid comparisons */
420*4882a593Smuzhiyun case AUDIT_COMPARE_AUID_TO_EUID:
421*4882a593Smuzhiyun return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
422*4882a593Smuzhiyun cred->euid);
423*4882a593Smuzhiyun case AUDIT_COMPARE_AUID_TO_SUID:
424*4882a593Smuzhiyun return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
425*4882a593Smuzhiyun cred->suid);
426*4882a593Smuzhiyun case AUDIT_COMPARE_AUID_TO_FSUID:
427*4882a593Smuzhiyun return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
428*4882a593Smuzhiyun cred->fsuid);
429*4882a593Smuzhiyun /* euid comparisons */
430*4882a593Smuzhiyun case AUDIT_COMPARE_EUID_TO_SUID:
431*4882a593Smuzhiyun return audit_uid_comparator(cred->euid, f->op, cred->suid);
432*4882a593Smuzhiyun case AUDIT_COMPARE_EUID_TO_FSUID:
433*4882a593Smuzhiyun return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
434*4882a593Smuzhiyun /* suid comparisons */
435*4882a593Smuzhiyun case AUDIT_COMPARE_SUID_TO_FSUID:
436*4882a593Smuzhiyun return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
437*4882a593Smuzhiyun /* gid comparisons */
438*4882a593Smuzhiyun case AUDIT_COMPARE_GID_TO_EGID:
439*4882a593Smuzhiyun return audit_gid_comparator(cred->gid, f->op, cred->egid);
440*4882a593Smuzhiyun case AUDIT_COMPARE_GID_TO_SGID:
441*4882a593Smuzhiyun return audit_gid_comparator(cred->gid, f->op, cred->sgid);
442*4882a593Smuzhiyun case AUDIT_COMPARE_GID_TO_FSGID:
443*4882a593Smuzhiyun return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
444*4882a593Smuzhiyun /* egid comparisons */
445*4882a593Smuzhiyun case AUDIT_COMPARE_EGID_TO_SGID:
446*4882a593Smuzhiyun return audit_gid_comparator(cred->egid, f->op, cred->sgid);
447*4882a593Smuzhiyun case AUDIT_COMPARE_EGID_TO_FSGID:
448*4882a593Smuzhiyun return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
449*4882a593Smuzhiyun /* sgid comparison */
450*4882a593Smuzhiyun case AUDIT_COMPARE_SGID_TO_FSGID:
451*4882a593Smuzhiyun return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
452*4882a593Smuzhiyun default:
453*4882a593Smuzhiyun WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
454*4882a593Smuzhiyun return 0;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun /* Determine if any context name data matches a rule's watch data */
460*4882a593Smuzhiyun /* Compare a task_struct with an audit_rule. Return 1 on match, 0
461*4882a593Smuzhiyun * otherwise.
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * If task_creation is true, this is an explicit indication that we are
464*4882a593Smuzhiyun * filtering a task rule at task creation time. This and tsk == current are
465*4882a593Smuzhiyun * the only situations where tsk->cred may be accessed without an rcu read lock.
466*4882a593Smuzhiyun */
audit_filter_rules(struct task_struct * tsk,struct audit_krule * rule,struct audit_context * ctx,struct audit_names * name,enum audit_state * state,bool task_creation)467*4882a593Smuzhiyun static int audit_filter_rules(struct task_struct *tsk,
468*4882a593Smuzhiyun struct audit_krule *rule,
469*4882a593Smuzhiyun struct audit_context *ctx,
470*4882a593Smuzhiyun struct audit_names *name,
471*4882a593Smuzhiyun enum audit_state *state,
472*4882a593Smuzhiyun bool task_creation)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun const struct cred *cred;
475*4882a593Smuzhiyun int i, need_sid = 1;
476*4882a593Smuzhiyun u32 sid;
477*4882a593Smuzhiyun unsigned int sessionid;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun for (i = 0; i < rule->field_count; i++) {
482*4882a593Smuzhiyun struct audit_field *f = &rule->fields[i];
483*4882a593Smuzhiyun struct audit_names *n;
484*4882a593Smuzhiyun int result = 0;
485*4882a593Smuzhiyun pid_t pid;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun switch (f->type) {
488*4882a593Smuzhiyun case AUDIT_PID:
489*4882a593Smuzhiyun pid = task_tgid_nr(tsk);
490*4882a593Smuzhiyun result = audit_comparator(pid, f->op, f->val);
491*4882a593Smuzhiyun break;
492*4882a593Smuzhiyun case AUDIT_PPID:
493*4882a593Smuzhiyun if (ctx) {
494*4882a593Smuzhiyun if (!ctx->ppid)
495*4882a593Smuzhiyun ctx->ppid = task_ppid_nr(tsk);
496*4882a593Smuzhiyun result = audit_comparator(ctx->ppid, f->op, f->val);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun break;
499*4882a593Smuzhiyun case AUDIT_EXE:
500*4882a593Smuzhiyun result = audit_exe_compare(tsk, rule->exe);
501*4882a593Smuzhiyun if (f->op == Audit_not_equal)
502*4882a593Smuzhiyun result = !result;
503*4882a593Smuzhiyun break;
504*4882a593Smuzhiyun case AUDIT_UID:
505*4882a593Smuzhiyun result = audit_uid_comparator(cred->uid, f->op, f->uid);
506*4882a593Smuzhiyun break;
507*4882a593Smuzhiyun case AUDIT_EUID:
508*4882a593Smuzhiyun result = audit_uid_comparator(cred->euid, f->op, f->uid);
509*4882a593Smuzhiyun break;
510*4882a593Smuzhiyun case AUDIT_SUID:
511*4882a593Smuzhiyun result = audit_uid_comparator(cred->suid, f->op, f->uid);
512*4882a593Smuzhiyun break;
513*4882a593Smuzhiyun case AUDIT_FSUID:
514*4882a593Smuzhiyun result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
515*4882a593Smuzhiyun break;
516*4882a593Smuzhiyun case AUDIT_GID:
517*4882a593Smuzhiyun result = audit_gid_comparator(cred->gid, f->op, f->gid);
518*4882a593Smuzhiyun if (f->op == Audit_equal) {
519*4882a593Smuzhiyun if (!result)
520*4882a593Smuzhiyun result = groups_search(cred->group_info, f->gid);
521*4882a593Smuzhiyun } else if (f->op == Audit_not_equal) {
522*4882a593Smuzhiyun if (result)
523*4882a593Smuzhiyun result = !groups_search(cred->group_info, f->gid);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun break;
526*4882a593Smuzhiyun case AUDIT_EGID:
527*4882a593Smuzhiyun result = audit_gid_comparator(cred->egid, f->op, f->gid);
528*4882a593Smuzhiyun if (f->op == Audit_equal) {
529*4882a593Smuzhiyun if (!result)
530*4882a593Smuzhiyun result = groups_search(cred->group_info, f->gid);
531*4882a593Smuzhiyun } else if (f->op == Audit_not_equal) {
532*4882a593Smuzhiyun if (result)
533*4882a593Smuzhiyun result = !groups_search(cred->group_info, f->gid);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun break;
536*4882a593Smuzhiyun case AUDIT_SGID:
537*4882a593Smuzhiyun result = audit_gid_comparator(cred->sgid, f->op, f->gid);
538*4882a593Smuzhiyun break;
539*4882a593Smuzhiyun case AUDIT_FSGID:
540*4882a593Smuzhiyun result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
541*4882a593Smuzhiyun break;
542*4882a593Smuzhiyun case AUDIT_SESSIONID:
543*4882a593Smuzhiyun sessionid = audit_get_sessionid(tsk);
544*4882a593Smuzhiyun result = audit_comparator(sessionid, f->op, f->val);
545*4882a593Smuzhiyun break;
546*4882a593Smuzhiyun case AUDIT_PERS:
547*4882a593Smuzhiyun result = audit_comparator(tsk->personality, f->op, f->val);
548*4882a593Smuzhiyun break;
549*4882a593Smuzhiyun case AUDIT_ARCH:
550*4882a593Smuzhiyun if (ctx)
551*4882a593Smuzhiyun result = audit_comparator(ctx->arch, f->op, f->val);
552*4882a593Smuzhiyun break;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun case AUDIT_EXIT:
555*4882a593Smuzhiyun if (ctx && ctx->return_valid)
556*4882a593Smuzhiyun result = audit_comparator(ctx->return_code, f->op, f->val);
557*4882a593Smuzhiyun break;
558*4882a593Smuzhiyun case AUDIT_SUCCESS:
559*4882a593Smuzhiyun if (ctx && ctx->return_valid) {
560*4882a593Smuzhiyun if (f->val)
561*4882a593Smuzhiyun result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
562*4882a593Smuzhiyun else
563*4882a593Smuzhiyun result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun break;
566*4882a593Smuzhiyun case AUDIT_DEVMAJOR:
567*4882a593Smuzhiyun if (name) {
568*4882a593Smuzhiyun if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
569*4882a593Smuzhiyun audit_comparator(MAJOR(name->rdev), f->op, f->val))
570*4882a593Smuzhiyun ++result;
571*4882a593Smuzhiyun } else if (ctx) {
572*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
573*4882a593Smuzhiyun if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
574*4882a593Smuzhiyun audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
575*4882a593Smuzhiyun ++result;
576*4882a593Smuzhiyun break;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun break;
581*4882a593Smuzhiyun case AUDIT_DEVMINOR:
582*4882a593Smuzhiyun if (name) {
583*4882a593Smuzhiyun if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
584*4882a593Smuzhiyun audit_comparator(MINOR(name->rdev), f->op, f->val))
585*4882a593Smuzhiyun ++result;
586*4882a593Smuzhiyun } else if (ctx) {
587*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
588*4882a593Smuzhiyun if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
589*4882a593Smuzhiyun audit_comparator(MINOR(n->rdev), f->op, f->val)) {
590*4882a593Smuzhiyun ++result;
591*4882a593Smuzhiyun break;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun break;
596*4882a593Smuzhiyun case AUDIT_INODE:
597*4882a593Smuzhiyun if (name)
598*4882a593Smuzhiyun result = audit_comparator(name->ino, f->op, f->val);
599*4882a593Smuzhiyun else if (ctx) {
600*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
601*4882a593Smuzhiyun if (audit_comparator(n->ino, f->op, f->val)) {
602*4882a593Smuzhiyun ++result;
603*4882a593Smuzhiyun break;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun break;
608*4882a593Smuzhiyun case AUDIT_OBJ_UID:
609*4882a593Smuzhiyun if (name) {
610*4882a593Smuzhiyun result = audit_uid_comparator(name->uid, f->op, f->uid);
611*4882a593Smuzhiyun } else if (ctx) {
612*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
613*4882a593Smuzhiyun if (audit_uid_comparator(n->uid, f->op, f->uid)) {
614*4882a593Smuzhiyun ++result;
615*4882a593Smuzhiyun break;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun break;
620*4882a593Smuzhiyun case AUDIT_OBJ_GID:
621*4882a593Smuzhiyun if (name) {
622*4882a593Smuzhiyun result = audit_gid_comparator(name->gid, f->op, f->gid);
623*4882a593Smuzhiyun } else if (ctx) {
624*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
625*4882a593Smuzhiyun if (audit_gid_comparator(n->gid, f->op, f->gid)) {
626*4882a593Smuzhiyun ++result;
627*4882a593Smuzhiyun break;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun break;
632*4882a593Smuzhiyun case AUDIT_WATCH:
633*4882a593Smuzhiyun if (name) {
634*4882a593Smuzhiyun result = audit_watch_compare(rule->watch,
635*4882a593Smuzhiyun name->ino,
636*4882a593Smuzhiyun name->dev);
637*4882a593Smuzhiyun if (f->op == Audit_not_equal)
638*4882a593Smuzhiyun result = !result;
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun break;
641*4882a593Smuzhiyun case AUDIT_DIR:
642*4882a593Smuzhiyun if (ctx) {
643*4882a593Smuzhiyun result = match_tree_refs(ctx, rule->tree);
644*4882a593Smuzhiyun if (f->op == Audit_not_equal)
645*4882a593Smuzhiyun result = !result;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun break;
648*4882a593Smuzhiyun case AUDIT_LOGINUID:
649*4882a593Smuzhiyun result = audit_uid_comparator(audit_get_loginuid(tsk),
650*4882a593Smuzhiyun f->op, f->uid);
651*4882a593Smuzhiyun break;
652*4882a593Smuzhiyun case AUDIT_LOGINUID_SET:
653*4882a593Smuzhiyun result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
654*4882a593Smuzhiyun break;
655*4882a593Smuzhiyun case AUDIT_SADDR_FAM:
656*4882a593Smuzhiyun if (ctx && ctx->sockaddr)
657*4882a593Smuzhiyun result = audit_comparator(ctx->sockaddr->ss_family,
658*4882a593Smuzhiyun f->op, f->val);
659*4882a593Smuzhiyun break;
660*4882a593Smuzhiyun case AUDIT_SUBJ_USER:
661*4882a593Smuzhiyun case AUDIT_SUBJ_ROLE:
662*4882a593Smuzhiyun case AUDIT_SUBJ_TYPE:
663*4882a593Smuzhiyun case AUDIT_SUBJ_SEN:
664*4882a593Smuzhiyun case AUDIT_SUBJ_CLR:
665*4882a593Smuzhiyun /* NOTE: this may return negative values indicating
666*4882a593Smuzhiyun a temporary error. We simply treat this as a
667*4882a593Smuzhiyun match for now to avoid losing information that
668*4882a593Smuzhiyun may be wanted. An error message will also be
669*4882a593Smuzhiyun logged upon error */
670*4882a593Smuzhiyun if (f->lsm_rule) {
671*4882a593Smuzhiyun if (need_sid) {
672*4882a593Smuzhiyun security_task_getsecid(tsk, &sid);
673*4882a593Smuzhiyun need_sid = 0;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun result = security_audit_rule_match(sid, f->type,
676*4882a593Smuzhiyun f->op,
677*4882a593Smuzhiyun f->lsm_rule);
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun break;
680*4882a593Smuzhiyun case AUDIT_OBJ_USER:
681*4882a593Smuzhiyun case AUDIT_OBJ_ROLE:
682*4882a593Smuzhiyun case AUDIT_OBJ_TYPE:
683*4882a593Smuzhiyun case AUDIT_OBJ_LEV_LOW:
684*4882a593Smuzhiyun case AUDIT_OBJ_LEV_HIGH:
685*4882a593Smuzhiyun /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
686*4882a593Smuzhiyun also applies here */
687*4882a593Smuzhiyun if (f->lsm_rule) {
688*4882a593Smuzhiyun /* Find files that match */
689*4882a593Smuzhiyun if (name) {
690*4882a593Smuzhiyun result = security_audit_rule_match(
691*4882a593Smuzhiyun name->osid,
692*4882a593Smuzhiyun f->type,
693*4882a593Smuzhiyun f->op,
694*4882a593Smuzhiyun f->lsm_rule);
695*4882a593Smuzhiyun } else if (ctx) {
696*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
697*4882a593Smuzhiyun if (security_audit_rule_match(
698*4882a593Smuzhiyun n->osid,
699*4882a593Smuzhiyun f->type,
700*4882a593Smuzhiyun f->op,
701*4882a593Smuzhiyun f->lsm_rule)) {
702*4882a593Smuzhiyun ++result;
703*4882a593Smuzhiyun break;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun /* Find ipc objects that match */
708*4882a593Smuzhiyun if (!ctx || ctx->type != AUDIT_IPC)
709*4882a593Smuzhiyun break;
710*4882a593Smuzhiyun if (security_audit_rule_match(ctx->ipc.osid,
711*4882a593Smuzhiyun f->type, f->op,
712*4882a593Smuzhiyun f->lsm_rule))
713*4882a593Smuzhiyun ++result;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun break;
716*4882a593Smuzhiyun case AUDIT_ARG0:
717*4882a593Smuzhiyun case AUDIT_ARG1:
718*4882a593Smuzhiyun case AUDIT_ARG2:
719*4882a593Smuzhiyun case AUDIT_ARG3:
720*4882a593Smuzhiyun if (ctx)
721*4882a593Smuzhiyun result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
722*4882a593Smuzhiyun break;
723*4882a593Smuzhiyun case AUDIT_FILTERKEY:
724*4882a593Smuzhiyun /* ignore this field for filtering */
725*4882a593Smuzhiyun result = 1;
726*4882a593Smuzhiyun break;
727*4882a593Smuzhiyun case AUDIT_PERM:
728*4882a593Smuzhiyun result = audit_match_perm(ctx, f->val);
729*4882a593Smuzhiyun if (f->op == Audit_not_equal)
730*4882a593Smuzhiyun result = !result;
731*4882a593Smuzhiyun break;
732*4882a593Smuzhiyun case AUDIT_FILETYPE:
733*4882a593Smuzhiyun result = audit_match_filetype(ctx, f->val);
734*4882a593Smuzhiyun if (f->op == Audit_not_equal)
735*4882a593Smuzhiyun result = !result;
736*4882a593Smuzhiyun break;
737*4882a593Smuzhiyun case AUDIT_FIELD_COMPARE:
738*4882a593Smuzhiyun result = audit_field_compare(tsk, cred, f, ctx, name);
739*4882a593Smuzhiyun break;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun if (!result)
742*4882a593Smuzhiyun return 0;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun if (ctx) {
746*4882a593Smuzhiyun if (rule->prio <= ctx->prio)
747*4882a593Smuzhiyun return 0;
748*4882a593Smuzhiyun if (rule->filterkey) {
749*4882a593Smuzhiyun kfree(ctx->filterkey);
750*4882a593Smuzhiyun ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun ctx->prio = rule->prio;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun switch (rule->action) {
755*4882a593Smuzhiyun case AUDIT_NEVER:
756*4882a593Smuzhiyun *state = AUDIT_DISABLED;
757*4882a593Smuzhiyun break;
758*4882a593Smuzhiyun case AUDIT_ALWAYS:
759*4882a593Smuzhiyun *state = AUDIT_RECORD_CONTEXT;
760*4882a593Smuzhiyun break;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun return 1;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun /* At process creation time, we can determine if system-call auditing is
766*4882a593Smuzhiyun * completely disabled for this task. Since we only have the task
767*4882a593Smuzhiyun * structure at this point, we can only check uid and gid.
768*4882a593Smuzhiyun */
audit_filter_task(struct task_struct * tsk,char ** key)769*4882a593Smuzhiyun static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun struct audit_entry *e;
772*4882a593Smuzhiyun enum audit_state state;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun rcu_read_lock();
775*4882a593Smuzhiyun list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
776*4882a593Smuzhiyun if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
777*4882a593Smuzhiyun &state, true)) {
778*4882a593Smuzhiyun if (state == AUDIT_RECORD_CONTEXT)
779*4882a593Smuzhiyun *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
780*4882a593Smuzhiyun rcu_read_unlock();
781*4882a593Smuzhiyun return state;
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun rcu_read_unlock();
785*4882a593Smuzhiyun return AUDIT_BUILD_CONTEXT;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
audit_in_mask(const struct audit_krule * rule,unsigned long val)788*4882a593Smuzhiyun static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun int word, bit;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun if (val > 0xffffffff)
793*4882a593Smuzhiyun return false;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun word = AUDIT_WORD(val);
796*4882a593Smuzhiyun if (word >= AUDIT_BITMASK_SIZE)
797*4882a593Smuzhiyun return false;
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun bit = AUDIT_BIT(val);
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun return rule->mask[word] & bit;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun /* At syscall entry and exit time, this filter is called if the
805*4882a593Smuzhiyun * audit_state is not low enough that auditing cannot take place, but is
806*4882a593Smuzhiyun * also not high enough that we already know we have to write an audit
807*4882a593Smuzhiyun * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
808*4882a593Smuzhiyun */
audit_filter_syscall(struct task_struct * tsk,struct audit_context * ctx,struct list_head * list)809*4882a593Smuzhiyun static enum audit_state audit_filter_syscall(struct task_struct *tsk,
810*4882a593Smuzhiyun struct audit_context *ctx,
811*4882a593Smuzhiyun struct list_head *list)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun struct audit_entry *e;
814*4882a593Smuzhiyun enum audit_state state;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if (auditd_test_task(tsk))
817*4882a593Smuzhiyun return AUDIT_DISABLED;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun rcu_read_lock();
820*4882a593Smuzhiyun list_for_each_entry_rcu(e, list, list) {
821*4882a593Smuzhiyun if (audit_in_mask(&e->rule, ctx->major) &&
822*4882a593Smuzhiyun audit_filter_rules(tsk, &e->rule, ctx, NULL,
823*4882a593Smuzhiyun &state, false)) {
824*4882a593Smuzhiyun rcu_read_unlock();
825*4882a593Smuzhiyun ctx->current_state = state;
826*4882a593Smuzhiyun return state;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun rcu_read_unlock();
830*4882a593Smuzhiyun return AUDIT_BUILD_CONTEXT;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /*
834*4882a593Smuzhiyun * Given an audit_name check the inode hash table to see if they match.
835*4882a593Smuzhiyun * Called holding the rcu read lock to protect the use of audit_inode_hash
836*4882a593Smuzhiyun */
audit_filter_inode_name(struct task_struct * tsk,struct audit_names * n,struct audit_context * ctx)837*4882a593Smuzhiyun static int audit_filter_inode_name(struct task_struct *tsk,
838*4882a593Smuzhiyun struct audit_names *n,
839*4882a593Smuzhiyun struct audit_context *ctx) {
840*4882a593Smuzhiyun int h = audit_hash_ino((u32)n->ino);
841*4882a593Smuzhiyun struct list_head *list = &audit_inode_hash[h];
842*4882a593Smuzhiyun struct audit_entry *e;
843*4882a593Smuzhiyun enum audit_state state;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun list_for_each_entry_rcu(e, list, list) {
846*4882a593Smuzhiyun if (audit_in_mask(&e->rule, ctx->major) &&
847*4882a593Smuzhiyun audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
848*4882a593Smuzhiyun ctx->current_state = state;
849*4882a593Smuzhiyun return 1;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun return 0;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /* At syscall exit time, this filter is called if any audit_names have been
856*4882a593Smuzhiyun * collected during syscall processing. We only check rules in sublists at hash
857*4882a593Smuzhiyun * buckets applicable to the inode numbers in audit_names.
858*4882a593Smuzhiyun * Regarding audit_state, same rules apply as for audit_filter_syscall().
859*4882a593Smuzhiyun */
audit_filter_inodes(struct task_struct * tsk,struct audit_context * ctx)860*4882a593Smuzhiyun void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun struct audit_names *n;
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun if (auditd_test_task(tsk))
865*4882a593Smuzhiyun return;
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun rcu_read_lock();
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun list_for_each_entry(n, &ctx->names_list, list) {
870*4882a593Smuzhiyun if (audit_filter_inode_name(tsk, n, ctx))
871*4882a593Smuzhiyun break;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun rcu_read_unlock();
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun
audit_proctitle_free(struct audit_context * context)876*4882a593Smuzhiyun static inline void audit_proctitle_free(struct audit_context *context)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun kfree(context->proctitle.value);
879*4882a593Smuzhiyun context->proctitle.value = NULL;
880*4882a593Smuzhiyun context->proctitle.len = 0;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun
audit_free_module(struct audit_context * context)883*4882a593Smuzhiyun static inline void audit_free_module(struct audit_context *context)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun if (context->type == AUDIT_KERN_MODULE) {
886*4882a593Smuzhiyun kfree(context->module.name);
887*4882a593Smuzhiyun context->module.name = NULL;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun }
audit_free_names(struct audit_context * context)890*4882a593Smuzhiyun static inline void audit_free_names(struct audit_context *context)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun struct audit_names *n, *next;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun list_for_each_entry_safe(n, next, &context->names_list, list) {
895*4882a593Smuzhiyun list_del(&n->list);
896*4882a593Smuzhiyun if (n->name)
897*4882a593Smuzhiyun putname(n->name);
898*4882a593Smuzhiyun if (n->should_free)
899*4882a593Smuzhiyun kfree(n);
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun context->name_count = 0;
902*4882a593Smuzhiyun path_put(&context->pwd);
903*4882a593Smuzhiyun context->pwd.dentry = NULL;
904*4882a593Smuzhiyun context->pwd.mnt = NULL;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun
audit_free_aux(struct audit_context * context)907*4882a593Smuzhiyun static inline void audit_free_aux(struct audit_context *context)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun struct audit_aux_data *aux;
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun while ((aux = context->aux)) {
912*4882a593Smuzhiyun context->aux = aux->next;
913*4882a593Smuzhiyun kfree(aux);
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun while ((aux = context->aux_pids)) {
916*4882a593Smuzhiyun context->aux_pids = aux->next;
917*4882a593Smuzhiyun kfree(aux);
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun
audit_alloc_context(enum audit_state state)921*4882a593Smuzhiyun static inline struct audit_context *audit_alloc_context(enum audit_state state)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun struct audit_context *context;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun context = kzalloc(sizeof(*context), GFP_KERNEL);
926*4882a593Smuzhiyun if (!context)
927*4882a593Smuzhiyun return NULL;
928*4882a593Smuzhiyun context->state = state;
929*4882a593Smuzhiyun context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
930*4882a593Smuzhiyun INIT_LIST_HEAD(&context->killed_trees);
931*4882a593Smuzhiyun INIT_LIST_HEAD(&context->names_list);
932*4882a593Smuzhiyun return context;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun /**
936*4882a593Smuzhiyun * audit_alloc - allocate an audit context block for a task
937*4882a593Smuzhiyun * @tsk: task
938*4882a593Smuzhiyun *
939*4882a593Smuzhiyun * Filter on the task information and allocate a per-task audit context
940*4882a593Smuzhiyun * if necessary. Doing so turns on system call auditing for the
941*4882a593Smuzhiyun * specified task. This is called from copy_process, so no lock is
942*4882a593Smuzhiyun * needed.
943*4882a593Smuzhiyun */
audit_alloc(struct task_struct * tsk)944*4882a593Smuzhiyun int audit_alloc(struct task_struct *tsk)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun struct audit_context *context;
947*4882a593Smuzhiyun enum audit_state state;
948*4882a593Smuzhiyun char *key = NULL;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun if (likely(!audit_ever_enabled))
951*4882a593Smuzhiyun return 0; /* Return if not auditing. */
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun state = audit_filter_task(tsk, &key);
954*4882a593Smuzhiyun if (state == AUDIT_DISABLED) {
955*4882a593Smuzhiyun clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
956*4882a593Smuzhiyun return 0;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun if (!(context = audit_alloc_context(state))) {
960*4882a593Smuzhiyun kfree(key);
961*4882a593Smuzhiyun audit_log_lost("out of memory in audit_alloc");
962*4882a593Smuzhiyun return -ENOMEM;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun context->filterkey = key;
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun audit_set_context(tsk, context);
967*4882a593Smuzhiyun set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
968*4882a593Smuzhiyun return 0;
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun
audit_free_context(struct audit_context * context)971*4882a593Smuzhiyun static inline void audit_free_context(struct audit_context *context)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun audit_free_module(context);
974*4882a593Smuzhiyun audit_free_names(context);
975*4882a593Smuzhiyun unroll_tree_refs(context, NULL, 0);
976*4882a593Smuzhiyun free_tree_refs(context);
977*4882a593Smuzhiyun audit_free_aux(context);
978*4882a593Smuzhiyun kfree(context->filterkey);
979*4882a593Smuzhiyun kfree(context->sockaddr);
980*4882a593Smuzhiyun audit_proctitle_free(context);
981*4882a593Smuzhiyun kfree(context);
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
audit_log_pid_context(struct audit_context * context,pid_t pid,kuid_t auid,kuid_t uid,unsigned int sessionid,u32 sid,char * comm)984*4882a593Smuzhiyun static int audit_log_pid_context(struct audit_context *context, pid_t pid,
985*4882a593Smuzhiyun kuid_t auid, kuid_t uid, unsigned int sessionid,
986*4882a593Smuzhiyun u32 sid, char *comm)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun struct audit_buffer *ab;
989*4882a593Smuzhiyun char *ctx = NULL;
990*4882a593Smuzhiyun u32 len;
991*4882a593Smuzhiyun int rc = 0;
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
994*4882a593Smuzhiyun if (!ab)
995*4882a593Smuzhiyun return rc;
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
998*4882a593Smuzhiyun from_kuid(&init_user_ns, auid),
999*4882a593Smuzhiyun from_kuid(&init_user_ns, uid), sessionid);
1000*4882a593Smuzhiyun if (sid) {
1001*4882a593Smuzhiyun if (security_secid_to_secctx(sid, &ctx, &len)) {
1002*4882a593Smuzhiyun audit_log_format(ab, " obj=(none)");
1003*4882a593Smuzhiyun rc = 1;
1004*4882a593Smuzhiyun } else {
1005*4882a593Smuzhiyun audit_log_format(ab, " obj=%s", ctx);
1006*4882a593Smuzhiyun security_release_secctx(ctx, len);
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun audit_log_format(ab, " ocomm=");
1010*4882a593Smuzhiyun audit_log_untrustedstring(ab, comm);
1011*4882a593Smuzhiyun audit_log_end(ab);
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun return rc;
1014*4882a593Smuzhiyun }
1015*4882a593Smuzhiyun
audit_log_execve_info(struct audit_context * context,struct audit_buffer ** ab)1016*4882a593Smuzhiyun static void audit_log_execve_info(struct audit_context *context,
1017*4882a593Smuzhiyun struct audit_buffer **ab)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun long len_max;
1020*4882a593Smuzhiyun long len_rem;
1021*4882a593Smuzhiyun long len_full;
1022*4882a593Smuzhiyun long len_buf;
1023*4882a593Smuzhiyun long len_abuf = 0;
1024*4882a593Smuzhiyun long len_tmp;
1025*4882a593Smuzhiyun bool require_data;
1026*4882a593Smuzhiyun bool encode;
1027*4882a593Smuzhiyun unsigned int iter;
1028*4882a593Smuzhiyun unsigned int arg;
1029*4882a593Smuzhiyun char *buf_head;
1030*4882a593Smuzhiyun char *buf;
1031*4882a593Smuzhiyun const char __user *p = (const char __user *)current->mm->arg_start;
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun /* NOTE: this buffer needs to be large enough to hold all the non-arg
1034*4882a593Smuzhiyun * data we put in the audit record for this argument (see the
1035*4882a593Smuzhiyun * code below) ... at this point in time 96 is plenty */
1036*4882a593Smuzhiyun char abuf[96];
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1039*4882a593Smuzhiyun * current value of 7500 is not as important as the fact that it
1040*4882a593Smuzhiyun * is less than 8k, a setting of 7500 gives us plenty of wiggle
1041*4882a593Smuzhiyun * room if we go over a little bit in the logging below */
1042*4882a593Smuzhiyun WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1043*4882a593Smuzhiyun len_max = MAX_EXECVE_AUDIT_LEN;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun /* scratch buffer to hold the userspace args */
1046*4882a593Smuzhiyun buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1047*4882a593Smuzhiyun if (!buf_head) {
1048*4882a593Smuzhiyun audit_panic("out of memory for argv string");
1049*4882a593Smuzhiyun return;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun buf = buf_head;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun audit_log_format(*ab, "argc=%d", context->execve.argc);
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun len_rem = len_max;
1056*4882a593Smuzhiyun len_buf = 0;
1057*4882a593Smuzhiyun len_full = 0;
1058*4882a593Smuzhiyun require_data = true;
1059*4882a593Smuzhiyun encode = false;
1060*4882a593Smuzhiyun iter = 0;
1061*4882a593Smuzhiyun arg = 0;
1062*4882a593Smuzhiyun do {
1063*4882a593Smuzhiyun /* NOTE: we don't ever want to trust this value for anything
1064*4882a593Smuzhiyun * serious, but the audit record format insists we
1065*4882a593Smuzhiyun * provide an argument length for really long arguments,
1066*4882a593Smuzhiyun * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1067*4882a593Smuzhiyun * to use strncpy_from_user() to obtain this value for
1068*4882a593Smuzhiyun * recording in the log, although we don't use it
1069*4882a593Smuzhiyun * anywhere here to avoid a double-fetch problem */
1070*4882a593Smuzhiyun if (len_full == 0)
1071*4882a593Smuzhiyun len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /* read more data from userspace */
1074*4882a593Smuzhiyun if (require_data) {
1075*4882a593Smuzhiyun /* can we make more room in the buffer? */
1076*4882a593Smuzhiyun if (buf != buf_head) {
1077*4882a593Smuzhiyun memmove(buf_head, buf, len_buf);
1078*4882a593Smuzhiyun buf = buf_head;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun /* fetch as much as we can of the argument */
1082*4882a593Smuzhiyun len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1083*4882a593Smuzhiyun len_max - len_buf);
1084*4882a593Smuzhiyun if (len_tmp == -EFAULT) {
1085*4882a593Smuzhiyun /* unable to copy from userspace */
1086*4882a593Smuzhiyun send_sig(SIGKILL, current, 0);
1087*4882a593Smuzhiyun goto out;
1088*4882a593Smuzhiyun } else if (len_tmp == (len_max - len_buf)) {
1089*4882a593Smuzhiyun /* buffer is not large enough */
1090*4882a593Smuzhiyun require_data = true;
1091*4882a593Smuzhiyun /* NOTE: if we are going to span multiple
1092*4882a593Smuzhiyun * buffers force the encoding so we stand
1093*4882a593Smuzhiyun * a chance at a sane len_full value and
1094*4882a593Smuzhiyun * consistent record encoding */
1095*4882a593Smuzhiyun encode = true;
1096*4882a593Smuzhiyun len_full = len_full * 2;
1097*4882a593Smuzhiyun p += len_tmp;
1098*4882a593Smuzhiyun } else {
1099*4882a593Smuzhiyun require_data = false;
1100*4882a593Smuzhiyun if (!encode)
1101*4882a593Smuzhiyun encode = audit_string_contains_control(
1102*4882a593Smuzhiyun buf, len_tmp);
1103*4882a593Smuzhiyun /* try to use a trusted value for len_full */
1104*4882a593Smuzhiyun if (len_full < len_max)
1105*4882a593Smuzhiyun len_full = (encode ?
1106*4882a593Smuzhiyun len_tmp * 2 : len_tmp);
1107*4882a593Smuzhiyun p += len_tmp + 1;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun len_buf += len_tmp;
1110*4882a593Smuzhiyun buf_head[len_buf] = '\0';
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun /* length of the buffer in the audit record? */
1113*4882a593Smuzhiyun len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun /* write as much as we can to the audit log */
1117*4882a593Smuzhiyun if (len_buf >= 0) {
1118*4882a593Smuzhiyun /* NOTE: some magic numbers here - basically if we
1119*4882a593Smuzhiyun * can't fit a reasonable amount of data into the
1120*4882a593Smuzhiyun * existing audit buffer, flush it and start with
1121*4882a593Smuzhiyun * a new buffer */
1122*4882a593Smuzhiyun if ((sizeof(abuf) + 8) > len_rem) {
1123*4882a593Smuzhiyun len_rem = len_max;
1124*4882a593Smuzhiyun audit_log_end(*ab);
1125*4882a593Smuzhiyun *ab = audit_log_start(context,
1126*4882a593Smuzhiyun GFP_KERNEL, AUDIT_EXECVE);
1127*4882a593Smuzhiyun if (!*ab)
1128*4882a593Smuzhiyun goto out;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun /* create the non-arg portion of the arg record */
1132*4882a593Smuzhiyun len_tmp = 0;
1133*4882a593Smuzhiyun if (require_data || (iter > 0) ||
1134*4882a593Smuzhiyun ((len_abuf + sizeof(abuf)) > len_rem)) {
1135*4882a593Smuzhiyun if (iter == 0) {
1136*4882a593Smuzhiyun len_tmp += snprintf(&abuf[len_tmp],
1137*4882a593Smuzhiyun sizeof(abuf) - len_tmp,
1138*4882a593Smuzhiyun " a%d_len=%lu",
1139*4882a593Smuzhiyun arg, len_full);
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun len_tmp += snprintf(&abuf[len_tmp],
1142*4882a593Smuzhiyun sizeof(abuf) - len_tmp,
1143*4882a593Smuzhiyun " a%d[%d]=", arg, iter++);
1144*4882a593Smuzhiyun } else
1145*4882a593Smuzhiyun len_tmp += snprintf(&abuf[len_tmp],
1146*4882a593Smuzhiyun sizeof(abuf) - len_tmp,
1147*4882a593Smuzhiyun " a%d=", arg);
1148*4882a593Smuzhiyun WARN_ON(len_tmp >= sizeof(abuf));
1149*4882a593Smuzhiyun abuf[sizeof(abuf) - 1] = '\0';
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun /* log the arg in the audit record */
1152*4882a593Smuzhiyun audit_log_format(*ab, "%s", abuf);
1153*4882a593Smuzhiyun len_rem -= len_tmp;
1154*4882a593Smuzhiyun len_tmp = len_buf;
1155*4882a593Smuzhiyun if (encode) {
1156*4882a593Smuzhiyun if (len_abuf > len_rem)
1157*4882a593Smuzhiyun len_tmp = len_rem / 2; /* encoding */
1158*4882a593Smuzhiyun audit_log_n_hex(*ab, buf, len_tmp);
1159*4882a593Smuzhiyun len_rem -= len_tmp * 2;
1160*4882a593Smuzhiyun len_abuf -= len_tmp * 2;
1161*4882a593Smuzhiyun } else {
1162*4882a593Smuzhiyun if (len_abuf > len_rem)
1163*4882a593Smuzhiyun len_tmp = len_rem - 2; /* quotes */
1164*4882a593Smuzhiyun audit_log_n_string(*ab, buf, len_tmp);
1165*4882a593Smuzhiyun len_rem -= len_tmp + 2;
1166*4882a593Smuzhiyun /* don't subtract the "2" because we still need
1167*4882a593Smuzhiyun * to add quotes to the remaining string */
1168*4882a593Smuzhiyun len_abuf -= len_tmp;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun len_buf -= len_tmp;
1171*4882a593Smuzhiyun buf += len_tmp;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun /* ready to move to the next argument? */
1175*4882a593Smuzhiyun if ((len_buf == 0) && !require_data) {
1176*4882a593Smuzhiyun arg++;
1177*4882a593Smuzhiyun iter = 0;
1178*4882a593Smuzhiyun len_full = 0;
1179*4882a593Smuzhiyun require_data = true;
1180*4882a593Smuzhiyun encode = false;
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun } while (arg < context->execve.argc);
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun /* NOTE: the caller handles the final audit_log_end() call */
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun out:
1187*4882a593Smuzhiyun kfree(buf_head);
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun
audit_log_cap(struct audit_buffer * ab,char * prefix,kernel_cap_t * cap)1190*4882a593Smuzhiyun static void audit_log_cap(struct audit_buffer *ab, char *prefix,
1191*4882a593Smuzhiyun kernel_cap_t *cap)
1192*4882a593Smuzhiyun {
1193*4882a593Smuzhiyun int i;
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun if (cap_isclear(*cap)) {
1196*4882a593Smuzhiyun audit_log_format(ab, " %s=0", prefix);
1197*4882a593Smuzhiyun return;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun audit_log_format(ab, " %s=", prefix);
1200*4882a593Smuzhiyun CAP_FOR_EACH_U32(i)
1201*4882a593Smuzhiyun audit_log_format(ab, "%08x", cap->cap[CAP_LAST_U32 - i]);
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun
audit_log_fcaps(struct audit_buffer * ab,struct audit_names * name)1204*4882a593Smuzhiyun static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun if (name->fcap_ver == -1) {
1207*4882a593Smuzhiyun audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
1208*4882a593Smuzhiyun return;
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
1211*4882a593Smuzhiyun audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
1212*4882a593Smuzhiyun audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
1213*4882a593Smuzhiyun name->fcap.fE, name->fcap_ver,
1214*4882a593Smuzhiyun from_kuid(&init_user_ns, name->fcap.rootid));
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun
audit_log_time(struct audit_context * context,struct audit_buffer ** ab)1217*4882a593Smuzhiyun static void audit_log_time(struct audit_context *context, struct audit_buffer **ab)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun const struct audit_ntp_data *ntp = &context->time.ntp_data;
1220*4882a593Smuzhiyun const struct timespec64 *tk = &context->time.tk_injoffset;
1221*4882a593Smuzhiyun static const char * const ntp_name[] = {
1222*4882a593Smuzhiyun "offset",
1223*4882a593Smuzhiyun "freq",
1224*4882a593Smuzhiyun "status",
1225*4882a593Smuzhiyun "tai",
1226*4882a593Smuzhiyun "tick",
1227*4882a593Smuzhiyun "adjust",
1228*4882a593Smuzhiyun };
1229*4882a593Smuzhiyun int type;
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun if (context->type == AUDIT_TIME_ADJNTPVAL) {
1232*4882a593Smuzhiyun for (type = 0; type < AUDIT_NTP_NVALS; type++) {
1233*4882a593Smuzhiyun if (ntp->vals[type].newval != ntp->vals[type].oldval) {
1234*4882a593Smuzhiyun if (!*ab) {
1235*4882a593Smuzhiyun *ab = audit_log_start(context,
1236*4882a593Smuzhiyun GFP_KERNEL,
1237*4882a593Smuzhiyun AUDIT_TIME_ADJNTPVAL);
1238*4882a593Smuzhiyun if (!*ab)
1239*4882a593Smuzhiyun return;
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun audit_log_format(*ab, "op=%s old=%lli new=%lli",
1242*4882a593Smuzhiyun ntp_name[type],
1243*4882a593Smuzhiyun ntp->vals[type].oldval,
1244*4882a593Smuzhiyun ntp->vals[type].newval);
1245*4882a593Smuzhiyun audit_log_end(*ab);
1246*4882a593Smuzhiyun *ab = NULL;
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun if (tk->tv_sec != 0 || tk->tv_nsec != 0) {
1251*4882a593Smuzhiyun if (!*ab) {
1252*4882a593Smuzhiyun *ab = audit_log_start(context, GFP_KERNEL,
1253*4882a593Smuzhiyun AUDIT_TIME_INJOFFSET);
1254*4882a593Smuzhiyun if (!*ab)
1255*4882a593Smuzhiyun return;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun audit_log_format(*ab, "sec=%lli nsec=%li",
1258*4882a593Smuzhiyun (long long)tk->tv_sec, tk->tv_nsec);
1259*4882a593Smuzhiyun audit_log_end(*ab);
1260*4882a593Smuzhiyun *ab = NULL;
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun
show_special(struct audit_context * context,int * call_panic)1264*4882a593Smuzhiyun static void show_special(struct audit_context *context, int *call_panic)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun struct audit_buffer *ab;
1267*4882a593Smuzhiyun int i;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, context->type);
1270*4882a593Smuzhiyun if (!ab)
1271*4882a593Smuzhiyun return;
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun switch (context->type) {
1274*4882a593Smuzhiyun case AUDIT_SOCKETCALL: {
1275*4882a593Smuzhiyun int nargs = context->socketcall.nargs;
1276*4882a593Smuzhiyun audit_log_format(ab, "nargs=%d", nargs);
1277*4882a593Smuzhiyun for (i = 0; i < nargs; i++)
1278*4882a593Smuzhiyun audit_log_format(ab, " a%d=%lx", i,
1279*4882a593Smuzhiyun context->socketcall.args[i]);
1280*4882a593Smuzhiyun break; }
1281*4882a593Smuzhiyun case AUDIT_IPC: {
1282*4882a593Smuzhiyun u32 osid = context->ipc.osid;
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1285*4882a593Smuzhiyun from_kuid(&init_user_ns, context->ipc.uid),
1286*4882a593Smuzhiyun from_kgid(&init_user_ns, context->ipc.gid),
1287*4882a593Smuzhiyun context->ipc.mode);
1288*4882a593Smuzhiyun if (osid) {
1289*4882a593Smuzhiyun char *ctx = NULL;
1290*4882a593Smuzhiyun u32 len;
1291*4882a593Smuzhiyun if (security_secid_to_secctx(osid, &ctx, &len)) {
1292*4882a593Smuzhiyun audit_log_format(ab, " osid=%u", osid);
1293*4882a593Smuzhiyun *call_panic = 1;
1294*4882a593Smuzhiyun } else {
1295*4882a593Smuzhiyun audit_log_format(ab, " obj=%s", ctx);
1296*4882a593Smuzhiyun security_release_secctx(ctx, len);
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun if (context->ipc.has_perm) {
1300*4882a593Smuzhiyun audit_log_end(ab);
1301*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL,
1302*4882a593Smuzhiyun AUDIT_IPC_SET_PERM);
1303*4882a593Smuzhiyun if (unlikely(!ab))
1304*4882a593Smuzhiyun return;
1305*4882a593Smuzhiyun audit_log_format(ab,
1306*4882a593Smuzhiyun "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1307*4882a593Smuzhiyun context->ipc.qbytes,
1308*4882a593Smuzhiyun context->ipc.perm_uid,
1309*4882a593Smuzhiyun context->ipc.perm_gid,
1310*4882a593Smuzhiyun context->ipc.perm_mode);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun break; }
1313*4882a593Smuzhiyun case AUDIT_MQ_OPEN:
1314*4882a593Smuzhiyun audit_log_format(ab,
1315*4882a593Smuzhiyun "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1316*4882a593Smuzhiyun "mq_msgsize=%ld mq_curmsgs=%ld",
1317*4882a593Smuzhiyun context->mq_open.oflag, context->mq_open.mode,
1318*4882a593Smuzhiyun context->mq_open.attr.mq_flags,
1319*4882a593Smuzhiyun context->mq_open.attr.mq_maxmsg,
1320*4882a593Smuzhiyun context->mq_open.attr.mq_msgsize,
1321*4882a593Smuzhiyun context->mq_open.attr.mq_curmsgs);
1322*4882a593Smuzhiyun break;
1323*4882a593Smuzhiyun case AUDIT_MQ_SENDRECV:
1324*4882a593Smuzhiyun audit_log_format(ab,
1325*4882a593Smuzhiyun "mqdes=%d msg_len=%zd msg_prio=%u "
1326*4882a593Smuzhiyun "abs_timeout_sec=%lld abs_timeout_nsec=%ld",
1327*4882a593Smuzhiyun context->mq_sendrecv.mqdes,
1328*4882a593Smuzhiyun context->mq_sendrecv.msg_len,
1329*4882a593Smuzhiyun context->mq_sendrecv.msg_prio,
1330*4882a593Smuzhiyun (long long) context->mq_sendrecv.abs_timeout.tv_sec,
1331*4882a593Smuzhiyun context->mq_sendrecv.abs_timeout.tv_nsec);
1332*4882a593Smuzhiyun break;
1333*4882a593Smuzhiyun case AUDIT_MQ_NOTIFY:
1334*4882a593Smuzhiyun audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1335*4882a593Smuzhiyun context->mq_notify.mqdes,
1336*4882a593Smuzhiyun context->mq_notify.sigev_signo);
1337*4882a593Smuzhiyun break;
1338*4882a593Smuzhiyun case AUDIT_MQ_GETSETATTR: {
1339*4882a593Smuzhiyun struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1340*4882a593Smuzhiyun audit_log_format(ab,
1341*4882a593Smuzhiyun "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1342*4882a593Smuzhiyun "mq_curmsgs=%ld ",
1343*4882a593Smuzhiyun context->mq_getsetattr.mqdes,
1344*4882a593Smuzhiyun attr->mq_flags, attr->mq_maxmsg,
1345*4882a593Smuzhiyun attr->mq_msgsize, attr->mq_curmsgs);
1346*4882a593Smuzhiyun break; }
1347*4882a593Smuzhiyun case AUDIT_CAPSET:
1348*4882a593Smuzhiyun audit_log_format(ab, "pid=%d", context->capset.pid);
1349*4882a593Smuzhiyun audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1350*4882a593Smuzhiyun audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1351*4882a593Smuzhiyun audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1352*4882a593Smuzhiyun audit_log_cap(ab, "cap_pa", &context->capset.cap.ambient);
1353*4882a593Smuzhiyun break;
1354*4882a593Smuzhiyun case AUDIT_MMAP:
1355*4882a593Smuzhiyun audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1356*4882a593Smuzhiyun context->mmap.flags);
1357*4882a593Smuzhiyun break;
1358*4882a593Smuzhiyun case AUDIT_EXECVE:
1359*4882a593Smuzhiyun audit_log_execve_info(context, &ab);
1360*4882a593Smuzhiyun break;
1361*4882a593Smuzhiyun case AUDIT_KERN_MODULE:
1362*4882a593Smuzhiyun audit_log_format(ab, "name=");
1363*4882a593Smuzhiyun if (context->module.name) {
1364*4882a593Smuzhiyun audit_log_untrustedstring(ab, context->module.name);
1365*4882a593Smuzhiyun } else
1366*4882a593Smuzhiyun audit_log_format(ab, "(null)");
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun break;
1369*4882a593Smuzhiyun case AUDIT_TIME_ADJNTPVAL:
1370*4882a593Smuzhiyun case AUDIT_TIME_INJOFFSET:
1371*4882a593Smuzhiyun /* this call deviates from the rest, eating the buffer */
1372*4882a593Smuzhiyun audit_log_time(context, &ab);
1373*4882a593Smuzhiyun break;
1374*4882a593Smuzhiyun }
1375*4882a593Smuzhiyun audit_log_end(ab);
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun
audit_proctitle_rtrim(char * proctitle,int len)1378*4882a593Smuzhiyun static inline int audit_proctitle_rtrim(char *proctitle, int len)
1379*4882a593Smuzhiyun {
1380*4882a593Smuzhiyun char *end = proctitle + len - 1;
1381*4882a593Smuzhiyun while (end > proctitle && !isprint(*end))
1382*4882a593Smuzhiyun end--;
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun /* catch the case where proctitle is only 1 non-print character */
1385*4882a593Smuzhiyun len = end - proctitle + 1;
1386*4882a593Smuzhiyun len -= isprint(proctitle[len-1]) == 0;
1387*4882a593Smuzhiyun return len;
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun /*
1391*4882a593Smuzhiyun * audit_log_name - produce AUDIT_PATH record from struct audit_names
1392*4882a593Smuzhiyun * @context: audit_context for the task
1393*4882a593Smuzhiyun * @n: audit_names structure with reportable details
1394*4882a593Smuzhiyun * @path: optional path to report instead of audit_names->name
1395*4882a593Smuzhiyun * @record_num: record number to report when handling a list of names
1396*4882a593Smuzhiyun * @call_panic: optional pointer to int that will be updated if secid fails
1397*4882a593Smuzhiyun */
audit_log_name(struct audit_context * context,struct audit_names * n,const struct path * path,int record_num,int * call_panic)1398*4882a593Smuzhiyun static void audit_log_name(struct audit_context *context, struct audit_names *n,
1399*4882a593Smuzhiyun const struct path *path, int record_num, int *call_panic)
1400*4882a593Smuzhiyun {
1401*4882a593Smuzhiyun struct audit_buffer *ab;
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1404*4882a593Smuzhiyun if (!ab)
1405*4882a593Smuzhiyun return;
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun audit_log_format(ab, "item=%d", record_num);
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun if (path)
1410*4882a593Smuzhiyun audit_log_d_path(ab, " name=", path);
1411*4882a593Smuzhiyun else if (n->name) {
1412*4882a593Smuzhiyun switch (n->name_len) {
1413*4882a593Smuzhiyun case AUDIT_NAME_FULL:
1414*4882a593Smuzhiyun /* log the full path */
1415*4882a593Smuzhiyun audit_log_format(ab, " name=");
1416*4882a593Smuzhiyun audit_log_untrustedstring(ab, n->name->name);
1417*4882a593Smuzhiyun break;
1418*4882a593Smuzhiyun case 0:
1419*4882a593Smuzhiyun /* name was specified as a relative path and the
1420*4882a593Smuzhiyun * directory component is the cwd
1421*4882a593Smuzhiyun */
1422*4882a593Smuzhiyun audit_log_d_path(ab, " name=", &context->pwd);
1423*4882a593Smuzhiyun break;
1424*4882a593Smuzhiyun default:
1425*4882a593Smuzhiyun /* log the name's directory component */
1426*4882a593Smuzhiyun audit_log_format(ab, " name=");
1427*4882a593Smuzhiyun audit_log_n_untrustedstring(ab, n->name->name,
1428*4882a593Smuzhiyun n->name_len);
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun } else
1431*4882a593Smuzhiyun audit_log_format(ab, " name=(null)");
1432*4882a593Smuzhiyun
1433*4882a593Smuzhiyun if (n->ino != AUDIT_INO_UNSET)
1434*4882a593Smuzhiyun audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
1435*4882a593Smuzhiyun n->ino,
1436*4882a593Smuzhiyun MAJOR(n->dev),
1437*4882a593Smuzhiyun MINOR(n->dev),
1438*4882a593Smuzhiyun n->mode,
1439*4882a593Smuzhiyun from_kuid(&init_user_ns, n->uid),
1440*4882a593Smuzhiyun from_kgid(&init_user_ns, n->gid),
1441*4882a593Smuzhiyun MAJOR(n->rdev),
1442*4882a593Smuzhiyun MINOR(n->rdev));
1443*4882a593Smuzhiyun if (n->osid != 0) {
1444*4882a593Smuzhiyun char *ctx = NULL;
1445*4882a593Smuzhiyun u32 len;
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun if (security_secid_to_secctx(
1448*4882a593Smuzhiyun n->osid, &ctx, &len)) {
1449*4882a593Smuzhiyun audit_log_format(ab, " osid=%u", n->osid);
1450*4882a593Smuzhiyun if (call_panic)
1451*4882a593Smuzhiyun *call_panic = 2;
1452*4882a593Smuzhiyun } else {
1453*4882a593Smuzhiyun audit_log_format(ab, " obj=%s", ctx);
1454*4882a593Smuzhiyun security_release_secctx(ctx, len);
1455*4882a593Smuzhiyun }
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun /* log the audit_names record type */
1459*4882a593Smuzhiyun switch (n->type) {
1460*4882a593Smuzhiyun case AUDIT_TYPE_NORMAL:
1461*4882a593Smuzhiyun audit_log_format(ab, " nametype=NORMAL");
1462*4882a593Smuzhiyun break;
1463*4882a593Smuzhiyun case AUDIT_TYPE_PARENT:
1464*4882a593Smuzhiyun audit_log_format(ab, " nametype=PARENT");
1465*4882a593Smuzhiyun break;
1466*4882a593Smuzhiyun case AUDIT_TYPE_CHILD_DELETE:
1467*4882a593Smuzhiyun audit_log_format(ab, " nametype=DELETE");
1468*4882a593Smuzhiyun break;
1469*4882a593Smuzhiyun case AUDIT_TYPE_CHILD_CREATE:
1470*4882a593Smuzhiyun audit_log_format(ab, " nametype=CREATE");
1471*4882a593Smuzhiyun break;
1472*4882a593Smuzhiyun default:
1473*4882a593Smuzhiyun audit_log_format(ab, " nametype=UNKNOWN");
1474*4882a593Smuzhiyun break;
1475*4882a593Smuzhiyun }
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun audit_log_fcaps(ab, n);
1478*4882a593Smuzhiyun audit_log_end(ab);
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun
audit_log_proctitle(void)1481*4882a593Smuzhiyun static void audit_log_proctitle(void)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun int res;
1484*4882a593Smuzhiyun char *buf;
1485*4882a593Smuzhiyun char *msg = "(null)";
1486*4882a593Smuzhiyun int len = strlen(msg);
1487*4882a593Smuzhiyun struct audit_context *context = audit_context();
1488*4882a593Smuzhiyun struct audit_buffer *ab;
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun if (!context || context->dummy)
1491*4882a593Smuzhiyun return;
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1494*4882a593Smuzhiyun if (!ab)
1495*4882a593Smuzhiyun return; /* audit_panic or being filtered */
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun audit_log_format(ab, "proctitle=");
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun /* Not cached */
1500*4882a593Smuzhiyun if (!context->proctitle.value) {
1501*4882a593Smuzhiyun buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1502*4882a593Smuzhiyun if (!buf)
1503*4882a593Smuzhiyun goto out;
1504*4882a593Smuzhiyun /* Historically called this from procfs naming */
1505*4882a593Smuzhiyun res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
1506*4882a593Smuzhiyun if (res == 0) {
1507*4882a593Smuzhiyun kfree(buf);
1508*4882a593Smuzhiyun goto out;
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun res = audit_proctitle_rtrim(buf, res);
1511*4882a593Smuzhiyun if (res == 0) {
1512*4882a593Smuzhiyun kfree(buf);
1513*4882a593Smuzhiyun goto out;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun context->proctitle.value = buf;
1516*4882a593Smuzhiyun context->proctitle.len = res;
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun msg = context->proctitle.value;
1519*4882a593Smuzhiyun len = context->proctitle.len;
1520*4882a593Smuzhiyun out:
1521*4882a593Smuzhiyun audit_log_n_untrustedstring(ab, msg, len);
1522*4882a593Smuzhiyun audit_log_end(ab);
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun
audit_log_exit(void)1525*4882a593Smuzhiyun static void audit_log_exit(void)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun int i, call_panic = 0;
1528*4882a593Smuzhiyun struct audit_context *context = audit_context();
1529*4882a593Smuzhiyun struct audit_buffer *ab;
1530*4882a593Smuzhiyun struct audit_aux_data *aux;
1531*4882a593Smuzhiyun struct audit_names *n;
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun context->personality = current->personality;
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1536*4882a593Smuzhiyun if (!ab)
1537*4882a593Smuzhiyun return; /* audit_panic has been called */
1538*4882a593Smuzhiyun audit_log_format(ab, "arch=%x syscall=%d",
1539*4882a593Smuzhiyun context->arch, context->major);
1540*4882a593Smuzhiyun if (context->personality != PER_LINUX)
1541*4882a593Smuzhiyun audit_log_format(ab, " per=%lx", context->personality);
1542*4882a593Smuzhiyun if (context->return_valid)
1543*4882a593Smuzhiyun audit_log_format(ab, " success=%s exit=%ld",
1544*4882a593Smuzhiyun (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1545*4882a593Smuzhiyun context->return_code);
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun audit_log_format(ab,
1548*4882a593Smuzhiyun " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1549*4882a593Smuzhiyun context->argv[0],
1550*4882a593Smuzhiyun context->argv[1],
1551*4882a593Smuzhiyun context->argv[2],
1552*4882a593Smuzhiyun context->argv[3],
1553*4882a593Smuzhiyun context->name_count);
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun audit_log_task_info(ab);
1556*4882a593Smuzhiyun audit_log_key(ab, context->filterkey);
1557*4882a593Smuzhiyun audit_log_end(ab);
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun for (aux = context->aux; aux; aux = aux->next) {
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, aux->type);
1562*4882a593Smuzhiyun if (!ab)
1563*4882a593Smuzhiyun continue; /* audit_panic has been called */
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun switch (aux->type) {
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun case AUDIT_BPRM_FCAPS: {
1568*4882a593Smuzhiyun struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1569*4882a593Smuzhiyun audit_log_format(ab, "fver=%x", axs->fcap_ver);
1570*4882a593Smuzhiyun audit_log_cap(ab, "fp", &axs->fcap.permitted);
1571*4882a593Smuzhiyun audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1572*4882a593Smuzhiyun audit_log_format(ab, " fe=%d", axs->fcap.fE);
1573*4882a593Smuzhiyun audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1574*4882a593Smuzhiyun audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1575*4882a593Smuzhiyun audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1576*4882a593Smuzhiyun audit_log_cap(ab, "old_pa", &axs->old_pcap.ambient);
1577*4882a593Smuzhiyun audit_log_cap(ab, "pp", &axs->new_pcap.permitted);
1578*4882a593Smuzhiyun audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
1579*4882a593Smuzhiyun audit_log_cap(ab, "pe", &axs->new_pcap.effective);
1580*4882a593Smuzhiyun audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
1581*4882a593Smuzhiyun audit_log_format(ab, " frootid=%d",
1582*4882a593Smuzhiyun from_kuid(&init_user_ns,
1583*4882a593Smuzhiyun axs->fcap.rootid));
1584*4882a593Smuzhiyun break; }
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun }
1587*4882a593Smuzhiyun audit_log_end(ab);
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun
1590*4882a593Smuzhiyun if (context->type)
1591*4882a593Smuzhiyun show_special(context, &call_panic);
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun if (context->fds[0] >= 0) {
1594*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1595*4882a593Smuzhiyun if (ab) {
1596*4882a593Smuzhiyun audit_log_format(ab, "fd0=%d fd1=%d",
1597*4882a593Smuzhiyun context->fds[0], context->fds[1]);
1598*4882a593Smuzhiyun audit_log_end(ab);
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun if (context->sockaddr_len) {
1603*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1604*4882a593Smuzhiyun if (ab) {
1605*4882a593Smuzhiyun audit_log_format(ab, "saddr=");
1606*4882a593Smuzhiyun audit_log_n_hex(ab, (void *)context->sockaddr,
1607*4882a593Smuzhiyun context->sockaddr_len);
1608*4882a593Smuzhiyun audit_log_end(ab);
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun for (aux = context->aux_pids; aux; aux = aux->next) {
1613*4882a593Smuzhiyun struct audit_aux_data_pids *axs = (void *)aux;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun for (i = 0; i < axs->pid_count; i++)
1616*4882a593Smuzhiyun if (audit_log_pid_context(context, axs->target_pid[i],
1617*4882a593Smuzhiyun axs->target_auid[i],
1618*4882a593Smuzhiyun axs->target_uid[i],
1619*4882a593Smuzhiyun axs->target_sessionid[i],
1620*4882a593Smuzhiyun axs->target_sid[i],
1621*4882a593Smuzhiyun axs->target_comm[i]))
1622*4882a593Smuzhiyun call_panic = 1;
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun if (context->target_pid &&
1626*4882a593Smuzhiyun audit_log_pid_context(context, context->target_pid,
1627*4882a593Smuzhiyun context->target_auid, context->target_uid,
1628*4882a593Smuzhiyun context->target_sessionid,
1629*4882a593Smuzhiyun context->target_sid, context->target_comm))
1630*4882a593Smuzhiyun call_panic = 1;
1631*4882a593Smuzhiyun
1632*4882a593Smuzhiyun if (context->pwd.dentry && context->pwd.mnt) {
1633*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1634*4882a593Smuzhiyun if (ab) {
1635*4882a593Smuzhiyun audit_log_d_path(ab, "cwd=", &context->pwd);
1636*4882a593Smuzhiyun audit_log_end(ab);
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun i = 0;
1641*4882a593Smuzhiyun list_for_each_entry(n, &context->names_list, list) {
1642*4882a593Smuzhiyun if (n->hidden)
1643*4882a593Smuzhiyun continue;
1644*4882a593Smuzhiyun audit_log_name(context, n, NULL, i++, &call_panic);
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun audit_log_proctitle();
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun /* Send end of event record to help user space know we are finished */
1650*4882a593Smuzhiyun ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1651*4882a593Smuzhiyun if (ab)
1652*4882a593Smuzhiyun audit_log_end(ab);
1653*4882a593Smuzhiyun if (call_panic)
1654*4882a593Smuzhiyun audit_panic("error converting sid to string");
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun
1657*4882a593Smuzhiyun /**
1658*4882a593Smuzhiyun * __audit_free - free a per-task audit context
1659*4882a593Smuzhiyun * @tsk: task whose audit context block to free
1660*4882a593Smuzhiyun *
1661*4882a593Smuzhiyun * Called from copy_process and do_exit
1662*4882a593Smuzhiyun */
__audit_free(struct task_struct * tsk)1663*4882a593Smuzhiyun void __audit_free(struct task_struct *tsk)
1664*4882a593Smuzhiyun {
1665*4882a593Smuzhiyun struct audit_context *context = tsk->audit_context;
1666*4882a593Smuzhiyun
1667*4882a593Smuzhiyun if (!context)
1668*4882a593Smuzhiyun return;
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun if (!list_empty(&context->killed_trees))
1671*4882a593Smuzhiyun audit_kill_trees(context);
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun /* We are called either by do_exit() or the fork() error handling code;
1674*4882a593Smuzhiyun * in the former case tsk == current and in the latter tsk is a
1675*4882a593Smuzhiyun * random task_struct that doesn't doesn't have any meaningful data we
1676*4882a593Smuzhiyun * need to log via audit_log_exit().
1677*4882a593Smuzhiyun */
1678*4882a593Smuzhiyun if (tsk == current && !context->dummy && context->in_syscall) {
1679*4882a593Smuzhiyun context->return_valid = 0;
1680*4882a593Smuzhiyun context->return_code = 0;
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun audit_filter_syscall(tsk, context,
1683*4882a593Smuzhiyun &audit_filter_list[AUDIT_FILTER_EXIT]);
1684*4882a593Smuzhiyun audit_filter_inodes(tsk, context);
1685*4882a593Smuzhiyun if (context->current_state == AUDIT_RECORD_CONTEXT)
1686*4882a593Smuzhiyun audit_log_exit();
1687*4882a593Smuzhiyun }
1688*4882a593Smuzhiyun
1689*4882a593Smuzhiyun audit_set_context(tsk, NULL);
1690*4882a593Smuzhiyun audit_free_context(context);
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun /**
1694*4882a593Smuzhiyun * __audit_syscall_entry - fill in an audit record at syscall entry
1695*4882a593Smuzhiyun * @major: major syscall type (function)
1696*4882a593Smuzhiyun * @a1: additional syscall register 1
1697*4882a593Smuzhiyun * @a2: additional syscall register 2
1698*4882a593Smuzhiyun * @a3: additional syscall register 3
1699*4882a593Smuzhiyun * @a4: additional syscall register 4
1700*4882a593Smuzhiyun *
1701*4882a593Smuzhiyun * Fill in audit context at syscall entry. This only happens if the
1702*4882a593Smuzhiyun * audit context was created when the task was created and the state or
1703*4882a593Smuzhiyun * filters demand the audit context be built. If the state from the
1704*4882a593Smuzhiyun * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1705*4882a593Smuzhiyun * then the record will be written at syscall exit time (otherwise, it
1706*4882a593Smuzhiyun * will only be written if another part of the kernel requests that it
1707*4882a593Smuzhiyun * be written).
1708*4882a593Smuzhiyun */
__audit_syscall_entry(int major,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4)1709*4882a593Smuzhiyun void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
1710*4882a593Smuzhiyun unsigned long a3, unsigned long a4)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun struct audit_context *context = audit_context();
1713*4882a593Smuzhiyun enum audit_state state;
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun if (!audit_enabled || !context)
1716*4882a593Smuzhiyun return;
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun BUG_ON(context->in_syscall || context->name_count);
1719*4882a593Smuzhiyun
1720*4882a593Smuzhiyun state = context->state;
1721*4882a593Smuzhiyun if (state == AUDIT_DISABLED)
1722*4882a593Smuzhiyun return;
1723*4882a593Smuzhiyun
1724*4882a593Smuzhiyun context->dummy = !audit_n_rules;
1725*4882a593Smuzhiyun if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1726*4882a593Smuzhiyun context->prio = 0;
1727*4882a593Smuzhiyun if (auditd_test_task(current))
1728*4882a593Smuzhiyun return;
1729*4882a593Smuzhiyun }
1730*4882a593Smuzhiyun
1731*4882a593Smuzhiyun context->arch = syscall_get_arch(current);
1732*4882a593Smuzhiyun context->major = major;
1733*4882a593Smuzhiyun context->argv[0] = a1;
1734*4882a593Smuzhiyun context->argv[1] = a2;
1735*4882a593Smuzhiyun context->argv[2] = a3;
1736*4882a593Smuzhiyun context->argv[3] = a4;
1737*4882a593Smuzhiyun context->serial = 0;
1738*4882a593Smuzhiyun context->in_syscall = 1;
1739*4882a593Smuzhiyun context->current_state = state;
1740*4882a593Smuzhiyun context->ppid = 0;
1741*4882a593Smuzhiyun ktime_get_coarse_real_ts64(&context->ctime);
1742*4882a593Smuzhiyun }
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun /**
1745*4882a593Smuzhiyun * __audit_syscall_exit - deallocate audit context after a system call
1746*4882a593Smuzhiyun * @success: success value of the syscall
1747*4882a593Smuzhiyun * @return_code: return value of the syscall
1748*4882a593Smuzhiyun *
1749*4882a593Smuzhiyun * Tear down after system call. If the audit context has been marked as
1750*4882a593Smuzhiyun * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1751*4882a593Smuzhiyun * filtering, or because some other part of the kernel wrote an audit
1752*4882a593Smuzhiyun * message), then write out the syscall information. In call cases,
1753*4882a593Smuzhiyun * free the names stored from getname().
1754*4882a593Smuzhiyun */
__audit_syscall_exit(int success,long return_code)1755*4882a593Smuzhiyun void __audit_syscall_exit(int success, long return_code)
1756*4882a593Smuzhiyun {
1757*4882a593Smuzhiyun struct audit_context *context;
1758*4882a593Smuzhiyun
1759*4882a593Smuzhiyun context = audit_context();
1760*4882a593Smuzhiyun if (!context)
1761*4882a593Smuzhiyun return;
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun if (!list_empty(&context->killed_trees))
1764*4882a593Smuzhiyun audit_kill_trees(context);
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun if (!context->dummy && context->in_syscall) {
1767*4882a593Smuzhiyun if (success)
1768*4882a593Smuzhiyun context->return_valid = AUDITSC_SUCCESS;
1769*4882a593Smuzhiyun else
1770*4882a593Smuzhiyun context->return_valid = AUDITSC_FAILURE;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun /*
1773*4882a593Smuzhiyun * we need to fix up the return code in the audit logs if the
1774*4882a593Smuzhiyun * actual return codes are later going to be fixed up by the
1775*4882a593Smuzhiyun * arch specific signal handlers
1776*4882a593Smuzhiyun *
1777*4882a593Smuzhiyun * This is actually a test for:
1778*4882a593Smuzhiyun * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1779*4882a593Smuzhiyun * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1780*4882a593Smuzhiyun *
1781*4882a593Smuzhiyun * but is faster than a bunch of ||
1782*4882a593Smuzhiyun */
1783*4882a593Smuzhiyun if (unlikely(return_code <= -ERESTARTSYS) &&
1784*4882a593Smuzhiyun (return_code >= -ERESTART_RESTARTBLOCK) &&
1785*4882a593Smuzhiyun (return_code != -ENOIOCTLCMD))
1786*4882a593Smuzhiyun context->return_code = -EINTR;
1787*4882a593Smuzhiyun else
1788*4882a593Smuzhiyun context->return_code = return_code;
1789*4882a593Smuzhiyun
1790*4882a593Smuzhiyun audit_filter_syscall(current, context,
1791*4882a593Smuzhiyun &audit_filter_list[AUDIT_FILTER_EXIT]);
1792*4882a593Smuzhiyun audit_filter_inodes(current, context);
1793*4882a593Smuzhiyun if (context->current_state == AUDIT_RECORD_CONTEXT)
1794*4882a593Smuzhiyun audit_log_exit();
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun context->in_syscall = 0;
1798*4882a593Smuzhiyun context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun audit_free_module(context);
1801*4882a593Smuzhiyun audit_free_names(context);
1802*4882a593Smuzhiyun unroll_tree_refs(context, NULL, 0);
1803*4882a593Smuzhiyun audit_free_aux(context);
1804*4882a593Smuzhiyun context->aux = NULL;
1805*4882a593Smuzhiyun context->aux_pids = NULL;
1806*4882a593Smuzhiyun context->target_pid = 0;
1807*4882a593Smuzhiyun context->target_sid = 0;
1808*4882a593Smuzhiyun context->sockaddr_len = 0;
1809*4882a593Smuzhiyun context->type = 0;
1810*4882a593Smuzhiyun context->fds[0] = -1;
1811*4882a593Smuzhiyun if (context->state != AUDIT_RECORD_CONTEXT) {
1812*4882a593Smuzhiyun kfree(context->filterkey);
1813*4882a593Smuzhiyun context->filterkey = NULL;
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
handle_one(const struct inode * inode)1817*4882a593Smuzhiyun static inline void handle_one(const struct inode *inode)
1818*4882a593Smuzhiyun {
1819*4882a593Smuzhiyun struct audit_context *context;
1820*4882a593Smuzhiyun struct audit_tree_refs *p;
1821*4882a593Smuzhiyun struct audit_chunk *chunk;
1822*4882a593Smuzhiyun int count;
1823*4882a593Smuzhiyun if (likely(!inode->i_fsnotify_marks))
1824*4882a593Smuzhiyun return;
1825*4882a593Smuzhiyun context = audit_context();
1826*4882a593Smuzhiyun p = context->trees;
1827*4882a593Smuzhiyun count = context->tree_count;
1828*4882a593Smuzhiyun rcu_read_lock();
1829*4882a593Smuzhiyun chunk = audit_tree_lookup(inode);
1830*4882a593Smuzhiyun rcu_read_unlock();
1831*4882a593Smuzhiyun if (!chunk)
1832*4882a593Smuzhiyun return;
1833*4882a593Smuzhiyun if (likely(put_tree_ref(context, chunk)))
1834*4882a593Smuzhiyun return;
1835*4882a593Smuzhiyun if (unlikely(!grow_tree_refs(context))) {
1836*4882a593Smuzhiyun pr_warn("out of memory, audit has lost a tree reference\n");
1837*4882a593Smuzhiyun audit_set_auditable(context);
1838*4882a593Smuzhiyun audit_put_chunk(chunk);
1839*4882a593Smuzhiyun unroll_tree_refs(context, p, count);
1840*4882a593Smuzhiyun return;
1841*4882a593Smuzhiyun }
1842*4882a593Smuzhiyun put_tree_ref(context, chunk);
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun
handle_path(const struct dentry * dentry)1845*4882a593Smuzhiyun static void handle_path(const struct dentry *dentry)
1846*4882a593Smuzhiyun {
1847*4882a593Smuzhiyun struct audit_context *context;
1848*4882a593Smuzhiyun struct audit_tree_refs *p;
1849*4882a593Smuzhiyun const struct dentry *d, *parent;
1850*4882a593Smuzhiyun struct audit_chunk *drop;
1851*4882a593Smuzhiyun unsigned long seq;
1852*4882a593Smuzhiyun int count;
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun context = audit_context();
1855*4882a593Smuzhiyun p = context->trees;
1856*4882a593Smuzhiyun count = context->tree_count;
1857*4882a593Smuzhiyun retry:
1858*4882a593Smuzhiyun drop = NULL;
1859*4882a593Smuzhiyun d = dentry;
1860*4882a593Smuzhiyun rcu_read_lock();
1861*4882a593Smuzhiyun seq = read_seqbegin(&rename_lock);
1862*4882a593Smuzhiyun for(;;) {
1863*4882a593Smuzhiyun struct inode *inode = d_backing_inode(d);
1864*4882a593Smuzhiyun if (inode && unlikely(inode->i_fsnotify_marks)) {
1865*4882a593Smuzhiyun struct audit_chunk *chunk;
1866*4882a593Smuzhiyun chunk = audit_tree_lookup(inode);
1867*4882a593Smuzhiyun if (chunk) {
1868*4882a593Smuzhiyun if (unlikely(!put_tree_ref(context, chunk))) {
1869*4882a593Smuzhiyun drop = chunk;
1870*4882a593Smuzhiyun break;
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun }
1873*4882a593Smuzhiyun }
1874*4882a593Smuzhiyun parent = d->d_parent;
1875*4882a593Smuzhiyun if (parent == d)
1876*4882a593Smuzhiyun break;
1877*4882a593Smuzhiyun d = parent;
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1880*4882a593Smuzhiyun rcu_read_unlock();
1881*4882a593Smuzhiyun if (!drop) {
1882*4882a593Smuzhiyun /* just a race with rename */
1883*4882a593Smuzhiyun unroll_tree_refs(context, p, count);
1884*4882a593Smuzhiyun goto retry;
1885*4882a593Smuzhiyun }
1886*4882a593Smuzhiyun audit_put_chunk(drop);
1887*4882a593Smuzhiyun if (grow_tree_refs(context)) {
1888*4882a593Smuzhiyun /* OK, got more space */
1889*4882a593Smuzhiyun unroll_tree_refs(context, p, count);
1890*4882a593Smuzhiyun goto retry;
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun /* too bad */
1893*4882a593Smuzhiyun pr_warn("out of memory, audit has lost a tree reference\n");
1894*4882a593Smuzhiyun unroll_tree_refs(context, p, count);
1895*4882a593Smuzhiyun audit_set_auditable(context);
1896*4882a593Smuzhiyun return;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun rcu_read_unlock();
1899*4882a593Smuzhiyun }
1900*4882a593Smuzhiyun
audit_alloc_name(struct audit_context * context,unsigned char type)1901*4882a593Smuzhiyun static struct audit_names *audit_alloc_name(struct audit_context *context,
1902*4882a593Smuzhiyun unsigned char type)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun struct audit_names *aname;
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun if (context->name_count < AUDIT_NAMES) {
1907*4882a593Smuzhiyun aname = &context->preallocated_names[context->name_count];
1908*4882a593Smuzhiyun memset(aname, 0, sizeof(*aname));
1909*4882a593Smuzhiyun } else {
1910*4882a593Smuzhiyun aname = kzalloc(sizeof(*aname), GFP_NOFS);
1911*4882a593Smuzhiyun if (!aname)
1912*4882a593Smuzhiyun return NULL;
1913*4882a593Smuzhiyun aname->should_free = true;
1914*4882a593Smuzhiyun }
1915*4882a593Smuzhiyun
1916*4882a593Smuzhiyun aname->ino = AUDIT_INO_UNSET;
1917*4882a593Smuzhiyun aname->type = type;
1918*4882a593Smuzhiyun list_add_tail(&aname->list, &context->names_list);
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun context->name_count++;
1921*4882a593Smuzhiyun return aname;
1922*4882a593Smuzhiyun }
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun /**
1925*4882a593Smuzhiyun * __audit_reusename - fill out filename with info from existing entry
1926*4882a593Smuzhiyun * @uptr: userland ptr to pathname
1927*4882a593Smuzhiyun *
1928*4882a593Smuzhiyun * Search the audit_names list for the current audit context. If there is an
1929*4882a593Smuzhiyun * existing entry with a matching "uptr" then return the filename
1930*4882a593Smuzhiyun * associated with that audit_name. If not, return NULL.
1931*4882a593Smuzhiyun */
1932*4882a593Smuzhiyun struct filename *
__audit_reusename(const __user char * uptr)1933*4882a593Smuzhiyun __audit_reusename(const __user char *uptr)
1934*4882a593Smuzhiyun {
1935*4882a593Smuzhiyun struct audit_context *context = audit_context();
1936*4882a593Smuzhiyun struct audit_names *n;
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun list_for_each_entry(n, &context->names_list, list) {
1939*4882a593Smuzhiyun if (!n->name)
1940*4882a593Smuzhiyun continue;
1941*4882a593Smuzhiyun if (n->name->uptr == uptr) {
1942*4882a593Smuzhiyun n->name->refcnt++;
1943*4882a593Smuzhiyun return n->name;
1944*4882a593Smuzhiyun }
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun return NULL;
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun
_audit_getcwd(struct audit_context * context)1949*4882a593Smuzhiyun inline void _audit_getcwd(struct audit_context *context)
1950*4882a593Smuzhiyun {
1951*4882a593Smuzhiyun if (!context->pwd.dentry)
1952*4882a593Smuzhiyun get_fs_pwd(current->fs, &context->pwd);
1953*4882a593Smuzhiyun }
1954*4882a593Smuzhiyun
__audit_getcwd(void)1955*4882a593Smuzhiyun void __audit_getcwd(void)
1956*4882a593Smuzhiyun {
1957*4882a593Smuzhiyun struct audit_context *context = audit_context();
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun if (context->in_syscall)
1960*4882a593Smuzhiyun _audit_getcwd(context);
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun /**
1964*4882a593Smuzhiyun * __audit_getname - add a name to the list
1965*4882a593Smuzhiyun * @name: name to add
1966*4882a593Smuzhiyun *
1967*4882a593Smuzhiyun * Add a name to the list of audit names for this context.
1968*4882a593Smuzhiyun * Called from fs/namei.c:getname().
1969*4882a593Smuzhiyun */
__audit_getname(struct filename * name)1970*4882a593Smuzhiyun void __audit_getname(struct filename *name)
1971*4882a593Smuzhiyun {
1972*4882a593Smuzhiyun struct audit_context *context = audit_context();
1973*4882a593Smuzhiyun struct audit_names *n;
1974*4882a593Smuzhiyun
1975*4882a593Smuzhiyun if (!context->in_syscall)
1976*4882a593Smuzhiyun return;
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
1979*4882a593Smuzhiyun if (!n)
1980*4882a593Smuzhiyun return;
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun n->name = name;
1983*4882a593Smuzhiyun n->name_len = AUDIT_NAME_FULL;
1984*4882a593Smuzhiyun name->aname = n;
1985*4882a593Smuzhiyun name->refcnt++;
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun _audit_getcwd(context);
1988*4882a593Smuzhiyun }
1989*4882a593Smuzhiyun
audit_copy_fcaps(struct audit_names * name,const struct dentry * dentry)1990*4882a593Smuzhiyun static inline int audit_copy_fcaps(struct audit_names *name,
1991*4882a593Smuzhiyun const struct dentry *dentry)
1992*4882a593Smuzhiyun {
1993*4882a593Smuzhiyun struct cpu_vfs_cap_data caps;
1994*4882a593Smuzhiyun int rc;
1995*4882a593Smuzhiyun
1996*4882a593Smuzhiyun if (!dentry)
1997*4882a593Smuzhiyun return 0;
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun rc = get_vfs_caps_from_disk(dentry, &caps);
2000*4882a593Smuzhiyun if (rc)
2001*4882a593Smuzhiyun return rc;
2002*4882a593Smuzhiyun
2003*4882a593Smuzhiyun name->fcap.permitted = caps.permitted;
2004*4882a593Smuzhiyun name->fcap.inheritable = caps.inheritable;
2005*4882a593Smuzhiyun name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2006*4882a593Smuzhiyun name->fcap.rootid = caps.rootid;
2007*4882a593Smuzhiyun name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
2008*4882a593Smuzhiyun VFS_CAP_REVISION_SHIFT;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun return 0;
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun
2013*4882a593Smuzhiyun /* Copy inode data into an audit_names. */
audit_copy_inode(struct audit_names * name,const struct dentry * dentry,struct inode * inode,unsigned int flags)2014*4882a593Smuzhiyun static void audit_copy_inode(struct audit_names *name,
2015*4882a593Smuzhiyun const struct dentry *dentry,
2016*4882a593Smuzhiyun struct inode *inode, unsigned int flags)
2017*4882a593Smuzhiyun {
2018*4882a593Smuzhiyun name->ino = inode->i_ino;
2019*4882a593Smuzhiyun name->dev = inode->i_sb->s_dev;
2020*4882a593Smuzhiyun name->mode = inode->i_mode;
2021*4882a593Smuzhiyun name->uid = inode->i_uid;
2022*4882a593Smuzhiyun name->gid = inode->i_gid;
2023*4882a593Smuzhiyun name->rdev = inode->i_rdev;
2024*4882a593Smuzhiyun security_inode_getsecid(inode, &name->osid);
2025*4882a593Smuzhiyun if (flags & AUDIT_INODE_NOEVAL) {
2026*4882a593Smuzhiyun name->fcap_ver = -1;
2027*4882a593Smuzhiyun return;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun audit_copy_fcaps(name, dentry);
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun /**
2033*4882a593Smuzhiyun * __audit_inode - store the inode and device from a lookup
2034*4882a593Smuzhiyun * @name: name being audited
2035*4882a593Smuzhiyun * @dentry: dentry being audited
2036*4882a593Smuzhiyun * @flags: attributes for this particular entry
2037*4882a593Smuzhiyun */
__audit_inode(struct filename * name,const struct dentry * dentry,unsigned int flags)2038*4882a593Smuzhiyun void __audit_inode(struct filename *name, const struct dentry *dentry,
2039*4882a593Smuzhiyun unsigned int flags)
2040*4882a593Smuzhiyun {
2041*4882a593Smuzhiyun struct audit_context *context = audit_context();
2042*4882a593Smuzhiyun struct inode *inode = d_backing_inode(dentry);
2043*4882a593Smuzhiyun struct audit_names *n;
2044*4882a593Smuzhiyun bool parent = flags & AUDIT_INODE_PARENT;
2045*4882a593Smuzhiyun struct audit_entry *e;
2046*4882a593Smuzhiyun struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2047*4882a593Smuzhiyun int i;
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun if (!context->in_syscall)
2050*4882a593Smuzhiyun return;
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun rcu_read_lock();
2053*4882a593Smuzhiyun list_for_each_entry_rcu(e, list, list) {
2054*4882a593Smuzhiyun for (i = 0; i < e->rule.field_count; i++) {
2055*4882a593Smuzhiyun struct audit_field *f = &e->rule.fields[i];
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun if (f->type == AUDIT_FSTYPE
2058*4882a593Smuzhiyun && audit_comparator(inode->i_sb->s_magic,
2059*4882a593Smuzhiyun f->op, f->val)
2060*4882a593Smuzhiyun && e->rule.action == AUDIT_NEVER) {
2061*4882a593Smuzhiyun rcu_read_unlock();
2062*4882a593Smuzhiyun return;
2063*4882a593Smuzhiyun }
2064*4882a593Smuzhiyun }
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun rcu_read_unlock();
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun if (!name)
2069*4882a593Smuzhiyun goto out_alloc;
2070*4882a593Smuzhiyun
2071*4882a593Smuzhiyun /*
2072*4882a593Smuzhiyun * If we have a pointer to an audit_names entry already, then we can
2073*4882a593Smuzhiyun * just use it directly if the type is correct.
2074*4882a593Smuzhiyun */
2075*4882a593Smuzhiyun n = name->aname;
2076*4882a593Smuzhiyun if (n) {
2077*4882a593Smuzhiyun if (parent) {
2078*4882a593Smuzhiyun if (n->type == AUDIT_TYPE_PARENT ||
2079*4882a593Smuzhiyun n->type == AUDIT_TYPE_UNKNOWN)
2080*4882a593Smuzhiyun goto out;
2081*4882a593Smuzhiyun } else {
2082*4882a593Smuzhiyun if (n->type != AUDIT_TYPE_PARENT)
2083*4882a593Smuzhiyun goto out;
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun }
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun list_for_each_entry_reverse(n, &context->names_list, list) {
2088*4882a593Smuzhiyun if (n->ino) {
2089*4882a593Smuzhiyun /* valid inode number, use that for the comparison */
2090*4882a593Smuzhiyun if (n->ino != inode->i_ino ||
2091*4882a593Smuzhiyun n->dev != inode->i_sb->s_dev)
2092*4882a593Smuzhiyun continue;
2093*4882a593Smuzhiyun } else if (n->name) {
2094*4882a593Smuzhiyun /* inode number has not been set, check the name */
2095*4882a593Smuzhiyun if (strcmp(n->name->name, name->name))
2096*4882a593Smuzhiyun continue;
2097*4882a593Smuzhiyun } else
2098*4882a593Smuzhiyun /* no inode and no name (?!) ... this is odd ... */
2099*4882a593Smuzhiyun continue;
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun /* match the correct record type */
2102*4882a593Smuzhiyun if (parent) {
2103*4882a593Smuzhiyun if (n->type == AUDIT_TYPE_PARENT ||
2104*4882a593Smuzhiyun n->type == AUDIT_TYPE_UNKNOWN)
2105*4882a593Smuzhiyun goto out;
2106*4882a593Smuzhiyun } else {
2107*4882a593Smuzhiyun if (n->type != AUDIT_TYPE_PARENT)
2108*4882a593Smuzhiyun goto out;
2109*4882a593Smuzhiyun }
2110*4882a593Smuzhiyun }
2111*4882a593Smuzhiyun
2112*4882a593Smuzhiyun out_alloc:
2113*4882a593Smuzhiyun /* unable to find an entry with both a matching name and type */
2114*4882a593Smuzhiyun n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
2115*4882a593Smuzhiyun if (!n)
2116*4882a593Smuzhiyun return;
2117*4882a593Smuzhiyun if (name) {
2118*4882a593Smuzhiyun n->name = name;
2119*4882a593Smuzhiyun name->refcnt++;
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun out:
2123*4882a593Smuzhiyun if (parent) {
2124*4882a593Smuzhiyun n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
2125*4882a593Smuzhiyun n->type = AUDIT_TYPE_PARENT;
2126*4882a593Smuzhiyun if (flags & AUDIT_INODE_HIDDEN)
2127*4882a593Smuzhiyun n->hidden = true;
2128*4882a593Smuzhiyun } else {
2129*4882a593Smuzhiyun n->name_len = AUDIT_NAME_FULL;
2130*4882a593Smuzhiyun n->type = AUDIT_TYPE_NORMAL;
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun handle_path(dentry);
2133*4882a593Smuzhiyun audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
2134*4882a593Smuzhiyun }
2135*4882a593Smuzhiyun
__audit_file(const struct file * file)2136*4882a593Smuzhiyun void __audit_file(const struct file *file)
2137*4882a593Smuzhiyun {
2138*4882a593Smuzhiyun __audit_inode(NULL, file->f_path.dentry, 0);
2139*4882a593Smuzhiyun }
2140*4882a593Smuzhiyun
2141*4882a593Smuzhiyun /**
2142*4882a593Smuzhiyun * __audit_inode_child - collect inode info for created/removed objects
2143*4882a593Smuzhiyun * @parent: inode of dentry parent
2144*4882a593Smuzhiyun * @dentry: dentry being audited
2145*4882a593Smuzhiyun * @type: AUDIT_TYPE_* value that we're looking for
2146*4882a593Smuzhiyun *
2147*4882a593Smuzhiyun * For syscalls that create or remove filesystem objects, audit_inode
2148*4882a593Smuzhiyun * can only collect information for the filesystem object's parent.
2149*4882a593Smuzhiyun * This call updates the audit context with the child's information.
2150*4882a593Smuzhiyun * Syscalls that create a new filesystem object must be hooked after
2151*4882a593Smuzhiyun * the object is created. Syscalls that remove a filesystem object
2152*4882a593Smuzhiyun * must be hooked prior, in order to capture the target inode during
2153*4882a593Smuzhiyun * unsuccessful attempts.
2154*4882a593Smuzhiyun */
__audit_inode_child(struct inode * parent,const struct dentry * dentry,const unsigned char type)2155*4882a593Smuzhiyun void __audit_inode_child(struct inode *parent,
2156*4882a593Smuzhiyun const struct dentry *dentry,
2157*4882a593Smuzhiyun const unsigned char type)
2158*4882a593Smuzhiyun {
2159*4882a593Smuzhiyun struct audit_context *context = audit_context();
2160*4882a593Smuzhiyun struct inode *inode = d_backing_inode(dentry);
2161*4882a593Smuzhiyun const struct qstr *dname = &dentry->d_name;
2162*4882a593Smuzhiyun struct audit_names *n, *found_parent = NULL, *found_child = NULL;
2163*4882a593Smuzhiyun struct audit_entry *e;
2164*4882a593Smuzhiyun struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2165*4882a593Smuzhiyun int i;
2166*4882a593Smuzhiyun
2167*4882a593Smuzhiyun if (!context->in_syscall)
2168*4882a593Smuzhiyun return;
2169*4882a593Smuzhiyun
2170*4882a593Smuzhiyun rcu_read_lock();
2171*4882a593Smuzhiyun list_for_each_entry_rcu(e, list, list) {
2172*4882a593Smuzhiyun for (i = 0; i < e->rule.field_count; i++) {
2173*4882a593Smuzhiyun struct audit_field *f = &e->rule.fields[i];
2174*4882a593Smuzhiyun
2175*4882a593Smuzhiyun if (f->type == AUDIT_FSTYPE
2176*4882a593Smuzhiyun && audit_comparator(parent->i_sb->s_magic,
2177*4882a593Smuzhiyun f->op, f->val)
2178*4882a593Smuzhiyun && e->rule.action == AUDIT_NEVER) {
2179*4882a593Smuzhiyun rcu_read_unlock();
2180*4882a593Smuzhiyun return;
2181*4882a593Smuzhiyun }
2182*4882a593Smuzhiyun }
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun rcu_read_unlock();
2185*4882a593Smuzhiyun
2186*4882a593Smuzhiyun if (inode)
2187*4882a593Smuzhiyun handle_one(inode);
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun /* look for a parent entry first */
2190*4882a593Smuzhiyun list_for_each_entry(n, &context->names_list, list) {
2191*4882a593Smuzhiyun if (!n->name ||
2192*4882a593Smuzhiyun (n->type != AUDIT_TYPE_PARENT &&
2193*4882a593Smuzhiyun n->type != AUDIT_TYPE_UNKNOWN))
2194*4882a593Smuzhiyun continue;
2195*4882a593Smuzhiyun
2196*4882a593Smuzhiyun if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
2197*4882a593Smuzhiyun !audit_compare_dname_path(dname,
2198*4882a593Smuzhiyun n->name->name, n->name_len)) {
2199*4882a593Smuzhiyun if (n->type == AUDIT_TYPE_UNKNOWN)
2200*4882a593Smuzhiyun n->type = AUDIT_TYPE_PARENT;
2201*4882a593Smuzhiyun found_parent = n;
2202*4882a593Smuzhiyun break;
2203*4882a593Smuzhiyun }
2204*4882a593Smuzhiyun }
2205*4882a593Smuzhiyun
2206*4882a593Smuzhiyun /* is there a matching child entry? */
2207*4882a593Smuzhiyun list_for_each_entry(n, &context->names_list, list) {
2208*4882a593Smuzhiyun /* can only match entries that have a name */
2209*4882a593Smuzhiyun if (!n->name ||
2210*4882a593Smuzhiyun (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
2211*4882a593Smuzhiyun continue;
2212*4882a593Smuzhiyun
2213*4882a593Smuzhiyun if (!strcmp(dname->name, n->name->name) ||
2214*4882a593Smuzhiyun !audit_compare_dname_path(dname, n->name->name,
2215*4882a593Smuzhiyun found_parent ?
2216*4882a593Smuzhiyun found_parent->name_len :
2217*4882a593Smuzhiyun AUDIT_NAME_FULL)) {
2218*4882a593Smuzhiyun if (n->type == AUDIT_TYPE_UNKNOWN)
2219*4882a593Smuzhiyun n->type = type;
2220*4882a593Smuzhiyun found_child = n;
2221*4882a593Smuzhiyun break;
2222*4882a593Smuzhiyun }
2223*4882a593Smuzhiyun }
2224*4882a593Smuzhiyun
2225*4882a593Smuzhiyun if (!found_parent) {
2226*4882a593Smuzhiyun /* create a new, "anonymous" parent record */
2227*4882a593Smuzhiyun n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
2228*4882a593Smuzhiyun if (!n)
2229*4882a593Smuzhiyun return;
2230*4882a593Smuzhiyun audit_copy_inode(n, NULL, parent, 0);
2231*4882a593Smuzhiyun }
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun if (!found_child) {
2234*4882a593Smuzhiyun found_child = audit_alloc_name(context, type);
2235*4882a593Smuzhiyun if (!found_child)
2236*4882a593Smuzhiyun return;
2237*4882a593Smuzhiyun
2238*4882a593Smuzhiyun /* Re-use the name belonging to the slot for a matching parent
2239*4882a593Smuzhiyun * directory. All names for this context are relinquished in
2240*4882a593Smuzhiyun * audit_free_names() */
2241*4882a593Smuzhiyun if (found_parent) {
2242*4882a593Smuzhiyun found_child->name = found_parent->name;
2243*4882a593Smuzhiyun found_child->name_len = AUDIT_NAME_FULL;
2244*4882a593Smuzhiyun found_child->name->refcnt++;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun }
2247*4882a593Smuzhiyun
2248*4882a593Smuzhiyun if (inode)
2249*4882a593Smuzhiyun audit_copy_inode(found_child, dentry, inode, 0);
2250*4882a593Smuzhiyun else
2251*4882a593Smuzhiyun found_child->ino = AUDIT_INO_UNSET;
2252*4882a593Smuzhiyun }
2253*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__audit_inode_child);
2254*4882a593Smuzhiyun
2255*4882a593Smuzhiyun /**
2256*4882a593Smuzhiyun * auditsc_get_stamp - get local copies of audit_context values
2257*4882a593Smuzhiyun * @ctx: audit_context for the task
2258*4882a593Smuzhiyun * @t: timespec64 to store time recorded in the audit_context
2259*4882a593Smuzhiyun * @serial: serial value that is recorded in the audit_context
2260*4882a593Smuzhiyun *
2261*4882a593Smuzhiyun * Also sets the context as auditable.
2262*4882a593Smuzhiyun */
auditsc_get_stamp(struct audit_context * ctx,struct timespec64 * t,unsigned int * serial)2263*4882a593Smuzhiyun int auditsc_get_stamp(struct audit_context *ctx,
2264*4882a593Smuzhiyun struct timespec64 *t, unsigned int *serial)
2265*4882a593Smuzhiyun {
2266*4882a593Smuzhiyun if (!ctx->in_syscall)
2267*4882a593Smuzhiyun return 0;
2268*4882a593Smuzhiyun if (!ctx->serial)
2269*4882a593Smuzhiyun ctx->serial = audit_serial();
2270*4882a593Smuzhiyun t->tv_sec = ctx->ctime.tv_sec;
2271*4882a593Smuzhiyun t->tv_nsec = ctx->ctime.tv_nsec;
2272*4882a593Smuzhiyun *serial = ctx->serial;
2273*4882a593Smuzhiyun if (!ctx->prio) {
2274*4882a593Smuzhiyun ctx->prio = 1;
2275*4882a593Smuzhiyun ctx->current_state = AUDIT_RECORD_CONTEXT;
2276*4882a593Smuzhiyun }
2277*4882a593Smuzhiyun return 1;
2278*4882a593Smuzhiyun }
2279*4882a593Smuzhiyun
2280*4882a593Smuzhiyun /**
2281*4882a593Smuzhiyun * __audit_mq_open - record audit data for a POSIX MQ open
2282*4882a593Smuzhiyun * @oflag: open flag
2283*4882a593Smuzhiyun * @mode: mode bits
2284*4882a593Smuzhiyun * @attr: queue attributes
2285*4882a593Smuzhiyun *
2286*4882a593Smuzhiyun */
__audit_mq_open(int oflag,umode_t mode,struct mq_attr * attr)2287*4882a593Smuzhiyun void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2288*4882a593Smuzhiyun {
2289*4882a593Smuzhiyun struct audit_context *context = audit_context();
2290*4882a593Smuzhiyun
2291*4882a593Smuzhiyun if (attr)
2292*4882a593Smuzhiyun memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2293*4882a593Smuzhiyun else
2294*4882a593Smuzhiyun memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2295*4882a593Smuzhiyun
2296*4882a593Smuzhiyun context->mq_open.oflag = oflag;
2297*4882a593Smuzhiyun context->mq_open.mode = mode;
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun context->type = AUDIT_MQ_OPEN;
2300*4882a593Smuzhiyun }
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun /**
2303*4882a593Smuzhiyun * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2304*4882a593Smuzhiyun * @mqdes: MQ descriptor
2305*4882a593Smuzhiyun * @msg_len: Message length
2306*4882a593Smuzhiyun * @msg_prio: Message priority
2307*4882a593Smuzhiyun * @abs_timeout: Message timeout in absolute time
2308*4882a593Smuzhiyun *
2309*4882a593Smuzhiyun */
__audit_mq_sendrecv(mqd_t mqdes,size_t msg_len,unsigned int msg_prio,const struct timespec64 * abs_timeout)2310*4882a593Smuzhiyun void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2311*4882a593Smuzhiyun const struct timespec64 *abs_timeout)
2312*4882a593Smuzhiyun {
2313*4882a593Smuzhiyun struct audit_context *context = audit_context();
2314*4882a593Smuzhiyun struct timespec64 *p = &context->mq_sendrecv.abs_timeout;
2315*4882a593Smuzhiyun
2316*4882a593Smuzhiyun if (abs_timeout)
2317*4882a593Smuzhiyun memcpy(p, abs_timeout, sizeof(*p));
2318*4882a593Smuzhiyun else
2319*4882a593Smuzhiyun memset(p, 0, sizeof(*p));
2320*4882a593Smuzhiyun
2321*4882a593Smuzhiyun context->mq_sendrecv.mqdes = mqdes;
2322*4882a593Smuzhiyun context->mq_sendrecv.msg_len = msg_len;
2323*4882a593Smuzhiyun context->mq_sendrecv.msg_prio = msg_prio;
2324*4882a593Smuzhiyun
2325*4882a593Smuzhiyun context->type = AUDIT_MQ_SENDRECV;
2326*4882a593Smuzhiyun }
2327*4882a593Smuzhiyun
2328*4882a593Smuzhiyun /**
2329*4882a593Smuzhiyun * __audit_mq_notify - record audit data for a POSIX MQ notify
2330*4882a593Smuzhiyun * @mqdes: MQ descriptor
2331*4882a593Smuzhiyun * @notification: Notification event
2332*4882a593Smuzhiyun *
2333*4882a593Smuzhiyun */
2334*4882a593Smuzhiyun
__audit_mq_notify(mqd_t mqdes,const struct sigevent * notification)2335*4882a593Smuzhiyun void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2336*4882a593Smuzhiyun {
2337*4882a593Smuzhiyun struct audit_context *context = audit_context();
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun if (notification)
2340*4882a593Smuzhiyun context->mq_notify.sigev_signo = notification->sigev_signo;
2341*4882a593Smuzhiyun else
2342*4882a593Smuzhiyun context->mq_notify.sigev_signo = 0;
2343*4882a593Smuzhiyun
2344*4882a593Smuzhiyun context->mq_notify.mqdes = mqdes;
2345*4882a593Smuzhiyun context->type = AUDIT_MQ_NOTIFY;
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun
2348*4882a593Smuzhiyun /**
2349*4882a593Smuzhiyun * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2350*4882a593Smuzhiyun * @mqdes: MQ descriptor
2351*4882a593Smuzhiyun * @mqstat: MQ flags
2352*4882a593Smuzhiyun *
2353*4882a593Smuzhiyun */
__audit_mq_getsetattr(mqd_t mqdes,struct mq_attr * mqstat)2354*4882a593Smuzhiyun void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2355*4882a593Smuzhiyun {
2356*4882a593Smuzhiyun struct audit_context *context = audit_context();
2357*4882a593Smuzhiyun context->mq_getsetattr.mqdes = mqdes;
2358*4882a593Smuzhiyun context->mq_getsetattr.mqstat = *mqstat;
2359*4882a593Smuzhiyun context->type = AUDIT_MQ_GETSETATTR;
2360*4882a593Smuzhiyun }
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun /**
2363*4882a593Smuzhiyun * __audit_ipc_obj - record audit data for ipc object
2364*4882a593Smuzhiyun * @ipcp: ipc permissions
2365*4882a593Smuzhiyun *
2366*4882a593Smuzhiyun */
__audit_ipc_obj(struct kern_ipc_perm * ipcp)2367*4882a593Smuzhiyun void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2368*4882a593Smuzhiyun {
2369*4882a593Smuzhiyun struct audit_context *context = audit_context();
2370*4882a593Smuzhiyun context->ipc.uid = ipcp->uid;
2371*4882a593Smuzhiyun context->ipc.gid = ipcp->gid;
2372*4882a593Smuzhiyun context->ipc.mode = ipcp->mode;
2373*4882a593Smuzhiyun context->ipc.has_perm = 0;
2374*4882a593Smuzhiyun security_ipc_getsecid(ipcp, &context->ipc.osid);
2375*4882a593Smuzhiyun context->type = AUDIT_IPC;
2376*4882a593Smuzhiyun }
2377*4882a593Smuzhiyun
2378*4882a593Smuzhiyun /**
2379*4882a593Smuzhiyun * __audit_ipc_set_perm - record audit data for new ipc permissions
2380*4882a593Smuzhiyun * @qbytes: msgq bytes
2381*4882a593Smuzhiyun * @uid: msgq user id
2382*4882a593Smuzhiyun * @gid: msgq group id
2383*4882a593Smuzhiyun * @mode: msgq mode (permissions)
2384*4882a593Smuzhiyun *
2385*4882a593Smuzhiyun * Called only after audit_ipc_obj().
2386*4882a593Smuzhiyun */
__audit_ipc_set_perm(unsigned long qbytes,uid_t uid,gid_t gid,umode_t mode)2387*4882a593Smuzhiyun void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2388*4882a593Smuzhiyun {
2389*4882a593Smuzhiyun struct audit_context *context = audit_context();
2390*4882a593Smuzhiyun
2391*4882a593Smuzhiyun context->ipc.qbytes = qbytes;
2392*4882a593Smuzhiyun context->ipc.perm_uid = uid;
2393*4882a593Smuzhiyun context->ipc.perm_gid = gid;
2394*4882a593Smuzhiyun context->ipc.perm_mode = mode;
2395*4882a593Smuzhiyun context->ipc.has_perm = 1;
2396*4882a593Smuzhiyun }
2397*4882a593Smuzhiyun
__audit_bprm(struct linux_binprm * bprm)2398*4882a593Smuzhiyun void __audit_bprm(struct linux_binprm *bprm)
2399*4882a593Smuzhiyun {
2400*4882a593Smuzhiyun struct audit_context *context = audit_context();
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun context->type = AUDIT_EXECVE;
2403*4882a593Smuzhiyun context->execve.argc = bprm->argc;
2404*4882a593Smuzhiyun }
2405*4882a593Smuzhiyun
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun /**
2408*4882a593Smuzhiyun * __audit_socketcall - record audit data for sys_socketcall
2409*4882a593Smuzhiyun * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2410*4882a593Smuzhiyun * @args: args array
2411*4882a593Smuzhiyun *
2412*4882a593Smuzhiyun */
__audit_socketcall(int nargs,unsigned long * args)2413*4882a593Smuzhiyun int __audit_socketcall(int nargs, unsigned long *args)
2414*4882a593Smuzhiyun {
2415*4882a593Smuzhiyun struct audit_context *context = audit_context();
2416*4882a593Smuzhiyun
2417*4882a593Smuzhiyun if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2418*4882a593Smuzhiyun return -EINVAL;
2419*4882a593Smuzhiyun context->type = AUDIT_SOCKETCALL;
2420*4882a593Smuzhiyun context->socketcall.nargs = nargs;
2421*4882a593Smuzhiyun memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2422*4882a593Smuzhiyun return 0;
2423*4882a593Smuzhiyun }
2424*4882a593Smuzhiyun
2425*4882a593Smuzhiyun /**
2426*4882a593Smuzhiyun * __audit_fd_pair - record audit data for pipe and socketpair
2427*4882a593Smuzhiyun * @fd1: the first file descriptor
2428*4882a593Smuzhiyun * @fd2: the second file descriptor
2429*4882a593Smuzhiyun *
2430*4882a593Smuzhiyun */
__audit_fd_pair(int fd1,int fd2)2431*4882a593Smuzhiyun void __audit_fd_pair(int fd1, int fd2)
2432*4882a593Smuzhiyun {
2433*4882a593Smuzhiyun struct audit_context *context = audit_context();
2434*4882a593Smuzhiyun context->fds[0] = fd1;
2435*4882a593Smuzhiyun context->fds[1] = fd2;
2436*4882a593Smuzhiyun }
2437*4882a593Smuzhiyun
2438*4882a593Smuzhiyun /**
2439*4882a593Smuzhiyun * __audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2440*4882a593Smuzhiyun * @len: data length in user space
2441*4882a593Smuzhiyun * @a: data address in kernel space
2442*4882a593Smuzhiyun *
2443*4882a593Smuzhiyun * Returns 0 for success or NULL context or < 0 on error.
2444*4882a593Smuzhiyun */
__audit_sockaddr(int len,void * a)2445*4882a593Smuzhiyun int __audit_sockaddr(int len, void *a)
2446*4882a593Smuzhiyun {
2447*4882a593Smuzhiyun struct audit_context *context = audit_context();
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun if (!context->sockaddr) {
2450*4882a593Smuzhiyun void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2451*4882a593Smuzhiyun if (!p)
2452*4882a593Smuzhiyun return -ENOMEM;
2453*4882a593Smuzhiyun context->sockaddr = p;
2454*4882a593Smuzhiyun }
2455*4882a593Smuzhiyun
2456*4882a593Smuzhiyun context->sockaddr_len = len;
2457*4882a593Smuzhiyun memcpy(context->sockaddr, a, len);
2458*4882a593Smuzhiyun return 0;
2459*4882a593Smuzhiyun }
2460*4882a593Smuzhiyun
__audit_ptrace(struct task_struct * t)2461*4882a593Smuzhiyun void __audit_ptrace(struct task_struct *t)
2462*4882a593Smuzhiyun {
2463*4882a593Smuzhiyun struct audit_context *context = audit_context();
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun context->target_pid = task_tgid_nr(t);
2466*4882a593Smuzhiyun context->target_auid = audit_get_loginuid(t);
2467*4882a593Smuzhiyun context->target_uid = task_uid(t);
2468*4882a593Smuzhiyun context->target_sessionid = audit_get_sessionid(t);
2469*4882a593Smuzhiyun security_task_getsecid(t, &context->target_sid);
2470*4882a593Smuzhiyun memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2471*4882a593Smuzhiyun }
2472*4882a593Smuzhiyun
2473*4882a593Smuzhiyun /**
2474*4882a593Smuzhiyun * audit_signal_info_syscall - record signal info for syscalls
2475*4882a593Smuzhiyun * @t: task being signaled
2476*4882a593Smuzhiyun *
2477*4882a593Smuzhiyun * If the audit subsystem is being terminated, record the task (pid)
2478*4882a593Smuzhiyun * and uid that is doing that.
2479*4882a593Smuzhiyun */
audit_signal_info_syscall(struct task_struct * t)2480*4882a593Smuzhiyun int audit_signal_info_syscall(struct task_struct *t)
2481*4882a593Smuzhiyun {
2482*4882a593Smuzhiyun struct audit_aux_data_pids *axp;
2483*4882a593Smuzhiyun struct audit_context *ctx = audit_context();
2484*4882a593Smuzhiyun kuid_t t_uid = task_uid(t);
2485*4882a593Smuzhiyun
2486*4882a593Smuzhiyun if (!audit_signals || audit_dummy_context())
2487*4882a593Smuzhiyun return 0;
2488*4882a593Smuzhiyun
2489*4882a593Smuzhiyun /* optimize the common case by putting first signal recipient directly
2490*4882a593Smuzhiyun * in audit_context */
2491*4882a593Smuzhiyun if (!ctx->target_pid) {
2492*4882a593Smuzhiyun ctx->target_pid = task_tgid_nr(t);
2493*4882a593Smuzhiyun ctx->target_auid = audit_get_loginuid(t);
2494*4882a593Smuzhiyun ctx->target_uid = t_uid;
2495*4882a593Smuzhiyun ctx->target_sessionid = audit_get_sessionid(t);
2496*4882a593Smuzhiyun security_task_getsecid(t, &ctx->target_sid);
2497*4882a593Smuzhiyun memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2498*4882a593Smuzhiyun return 0;
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun
2501*4882a593Smuzhiyun axp = (void *)ctx->aux_pids;
2502*4882a593Smuzhiyun if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2503*4882a593Smuzhiyun axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2504*4882a593Smuzhiyun if (!axp)
2505*4882a593Smuzhiyun return -ENOMEM;
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun axp->d.type = AUDIT_OBJ_PID;
2508*4882a593Smuzhiyun axp->d.next = ctx->aux_pids;
2509*4882a593Smuzhiyun ctx->aux_pids = (void *)axp;
2510*4882a593Smuzhiyun }
2511*4882a593Smuzhiyun BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2512*4882a593Smuzhiyun
2513*4882a593Smuzhiyun axp->target_pid[axp->pid_count] = task_tgid_nr(t);
2514*4882a593Smuzhiyun axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2515*4882a593Smuzhiyun axp->target_uid[axp->pid_count] = t_uid;
2516*4882a593Smuzhiyun axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2517*4882a593Smuzhiyun security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2518*4882a593Smuzhiyun memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2519*4882a593Smuzhiyun axp->pid_count++;
2520*4882a593Smuzhiyun
2521*4882a593Smuzhiyun return 0;
2522*4882a593Smuzhiyun }
2523*4882a593Smuzhiyun
2524*4882a593Smuzhiyun /**
2525*4882a593Smuzhiyun * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2526*4882a593Smuzhiyun * @bprm: pointer to the bprm being processed
2527*4882a593Smuzhiyun * @new: the proposed new credentials
2528*4882a593Smuzhiyun * @old: the old credentials
2529*4882a593Smuzhiyun *
2530*4882a593Smuzhiyun * Simply check if the proc already has the caps given by the file and if not
2531*4882a593Smuzhiyun * store the priv escalation info for later auditing at the end of the syscall
2532*4882a593Smuzhiyun *
2533*4882a593Smuzhiyun * -Eric
2534*4882a593Smuzhiyun */
__audit_log_bprm_fcaps(struct linux_binprm * bprm,const struct cred * new,const struct cred * old)2535*4882a593Smuzhiyun int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2536*4882a593Smuzhiyun const struct cred *new, const struct cred *old)
2537*4882a593Smuzhiyun {
2538*4882a593Smuzhiyun struct audit_aux_data_bprm_fcaps *ax;
2539*4882a593Smuzhiyun struct audit_context *context = audit_context();
2540*4882a593Smuzhiyun struct cpu_vfs_cap_data vcaps;
2541*4882a593Smuzhiyun
2542*4882a593Smuzhiyun ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2543*4882a593Smuzhiyun if (!ax)
2544*4882a593Smuzhiyun return -ENOMEM;
2545*4882a593Smuzhiyun
2546*4882a593Smuzhiyun ax->d.type = AUDIT_BPRM_FCAPS;
2547*4882a593Smuzhiyun ax->d.next = context->aux;
2548*4882a593Smuzhiyun context->aux = (void *)ax;
2549*4882a593Smuzhiyun
2550*4882a593Smuzhiyun get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
2551*4882a593Smuzhiyun
2552*4882a593Smuzhiyun ax->fcap.permitted = vcaps.permitted;
2553*4882a593Smuzhiyun ax->fcap.inheritable = vcaps.inheritable;
2554*4882a593Smuzhiyun ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2555*4882a593Smuzhiyun ax->fcap.rootid = vcaps.rootid;
2556*4882a593Smuzhiyun ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2557*4882a593Smuzhiyun
2558*4882a593Smuzhiyun ax->old_pcap.permitted = old->cap_permitted;
2559*4882a593Smuzhiyun ax->old_pcap.inheritable = old->cap_inheritable;
2560*4882a593Smuzhiyun ax->old_pcap.effective = old->cap_effective;
2561*4882a593Smuzhiyun ax->old_pcap.ambient = old->cap_ambient;
2562*4882a593Smuzhiyun
2563*4882a593Smuzhiyun ax->new_pcap.permitted = new->cap_permitted;
2564*4882a593Smuzhiyun ax->new_pcap.inheritable = new->cap_inheritable;
2565*4882a593Smuzhiyun ax->new_pcap.effective = new->cap_effective;
2566*4882a593Smuzhiyun ax->new_pcap.ambient = new->cap_ambient;
2567*4882a593Smuzhiyun return 0;
2568*4882a593Smuzhiyun }
2569*4882a593Smuzhiyun
2570*4882a593Smuzhiyun /**
2571*4882a593Smuzhiyun * __audit_log_capset - store information about the arguments to the capset syscall
2572*4882a593Smuzhiyun * @new: the new credentials
2573*4882a593Smuzhiyun * @old: the old (current) credentials
2574*4882a593Smuzhiyun *
2575*4882a593Smuzhiyun * Record the arguments userspace sent to sys_capset for later printing by the
2576*4882a593Smuzhiyun * audit system if applicable
2577*4882a593Smuzhiyun */
__audit_log_capset(const struct cred * new,const struct cred * old)2578*4882a593Smuzhiyun void __audit_log_capset(const struct cred *new, const struct cred *old)
2579*4882a593Smuzhiyun {
2580*4882a593Smuzhiyun struct audit_context *context = audit_context();
2581*4882a593Smuzhiyun context->capset.pid = task_tgid_nr(current);
2582*4882a593Smuzhiyun context->capset.cap.effective = new->cap_effective;
2583*4882a593Smuzhiyun context->capset.cap.inheritable = new->cap_effective;
2584*4882a593Smuzhiyun context->capset.cap.permitted = new->cap_permitted;
2585*4882a593Smuzhiyun context->capset.cap.ambient = new->cap_ambient;
2586*4882a593Smuzhiyun context->type = AUDIT_CAPSET;
2587*4882a593Smuzhiyun }
2588*4882a593Smuzhiyun
__audit_mmap_fd(int fd,int flags)2589*4882a593Smuzhiyun void __audit_mmap_fd(int fd, int flags)
2590*4882a593Smuzhiyun {
2591*4882a593Smuzhiyun struct audit_context *context = audit_context();
2592*4882a593Smuzhiyun context->mmap.fd = fd;
2593*4882a593Smuzhiyun context->mmap.flags = flags;
2594*4882a593Smuzhiyun context->type = AUDIT_MMAP;
2595*4882a593Smuzhiyun }
2596*4882a593Smuzhiyun
__audit_log_kern_module(char * name)2597*4882a593Smuzhiyun void __audit_log_kern_module(char *name)
2598*4882a593Smuzhiyun {
2599*4882a593Smuzhiyun struct audit_context *context = audit_context();
2600*4882a593Smuzhiyun
2601*4882a593Smuzhiyun context->module.name = kstrdup(name, GFP_KERNEL);
2602*4882a593Smuzhiyun if (!context->module.name)
2603*4882a593Smuzhiyun audit_log_lost("out of memory in __audit_log_kern_module");
2604*4882a593Smuzhiyun context->type = AUDIT_KERN_MODULE;
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun
__audit_fanotify(unsigned int response)2607*4882a593Smuzhiyun void __audit_fanotify(unsigned int response)
2608*4882a593Smuzhiyun {
2609*4882a593Smuzhiyun audit_log(audit_context(), GFP_KERNEL,
2610*4882a593Smuzhiyun AUDIT_FANOTIFY, "resp=%u", response);
2611*4882a593Smuzhiyun }
2612*4882a593Smuzhiyun
__audit_tk_injoffset(struct timespec64 offset)2613*4882a593Smuzhiyun void __audit_tk_injoffset(struct timespec64 offset)
2614*4882a593Smuzhiyun {
2615*4882a593Smuzhiyun struct audit_context *context = audit_context();
2616*4882a593Smuzhiyun
2617*4882a593Smuzhiyun /* only set type if not already set by NTP */
2618*4882a593Smuzhiyun if (!context->type)
2619*4882a593Smuzhiyun context->type = AUDIT_TIME_INJOFFSET;
2620*4882a593Smuzhiyun memcpy(&context->time.tk_injoffset, &offset, sizeof(offset));
2621*4882a593Smuzhiyun }
2622*4882a593Smuzhiyun
__audit_ntp_log(const struct audit_ntp_data * ad)2623*4882a593Smuzhiyun void __audit_ntp_log(const struct audit_ntp_data *ad)
2624*4882a593Smuzhiyun {
2625*4882a593Smuzhiyun struct audit_context *context = audit_context();
2626*4882a593Smuzhiyun int type;
2627*4882a593Smuzhiyun
2628*4882a593Smuzhiyun for (type = 0; type < AUDIT_NTP_NVALS; type++)
2629*4882a593Smuzhiyun if (ad->vals[type].newval != ad->vals[type].oldval) {
2630*4882a593Smuzhiyun /* unconditionally set type, overwriting TK */
2631*4882a593Smuzhiyun context->type = AUDIT_TIME_ADJNTPVAL;
2632*4882a593Smuzhiyun memcpy(&context->time.ntp_data, ad, sizeof(*ad));
2633*4882a593Smuzhiyun break;
2634*4882a593Smuzhiyun }
2635*4882a593Smuzhiyun }
2636*4882a593Smuzhiyun
__audit_log_nfcfg(const char * name,u8 af,unsigned int nentries,enum audit_nfcfgop op,gfp_t gfp)2637*4882a593Smuzhiyun void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
2638*4882a593Smuzhiyun enum audit_nfcfgop op, gfp_t gfp)
2639*4882a593Smuzhiyun {
2640*4882a593Smuzhiyun struct audit_buffer *ab;
2641*4882a593Smuzhiyun char comm[sizeof(current->comm)];
2642*4882a593Smuzhiyun
2643*4882a593Smuzhiyun ab = audit_log_start(audit_context(), gfp, AUDIT_NETFILTER_CFG);
2644*4882a593Smuzhiyun if (!ab)
2645*4882a593Smuzhiyun return;
2646*4882a593Smuzhiyun audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
2647*4882a593Smuzhiyun name, af, nentries, audit_nfcfgs[op].s);
2648*4882a593Smuzhiyun
2649*4882a593Smuzhiyun audit_log_format(ab, " pid=%u", task_pid_nr(current));
2650*4882a593Smuzhiyun audit_log_task_context(ab); /* subj= */
2651*4882a593Smuzhiyun audit_log_format(ab, " comm=");
2652*4882a593Smuzhiyun audit_log_untrustedstring(ab, get_task_comm(comm, current));
2653*4882a593Smuzhiyun audit_log_end(ab);
2654*4882a593Smuzhiyun }
2655*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
2656*4882a593Smuzhiyun
audit_log_task(struct audit_buffer * ab)2657*4882a593Smuzhiyun static void audit_log_task(struct audit_buffer *ab)
2658*4882a593Smuzhiyun {
2659*4882a593Smuzhiyun kuid_t auid, uid;
2660*4882a593Smuzhiyun kgid_t gid;
2661*4882a593Smuzhiyun unsigned int sessionid;
2662*4882a593Smuzhiyun char comm[sizeof(current->comm)];
2663*4882a593Smuzhiyun
2664*4882a593Smuzhiyun auid = audit_get_loginuid(current);
2665*4882a593Smuzhiyun sessionid = audit_get_sessionid(current);
2666*4882a593Smuzhiyun current_uid_gid(&uid, &gid);
2667*4882a593Smuzhiyun
2668*4882a593Smuzhiyun audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2669*4882a593Smuzhiyun from_kuid(&init_user_ns, auid),
2670*4882a593Smuzhiyun from_kuid(&init_user_ns, uid),
2671*4882a593Smuzhiyun from_kgid(&init_user_ns, gid),
2672*4882a593Smuzhiyun sessionid);
2673*4882a593Smuzhiyun audit_log_task_context(ab);
2674*4882a593Smuzhiyun audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
2675*4882a593Smuzhiyun audit_log_untrustedstring(ab, get_task_comm(comm, current));
2676*4882a593Smuzhiyun audit_log_d_path_exe(ab, current->mm);
2677*4882a593Smuzhiyun }
2678*4882a593Smuzhiyun
2679*4882a593Smuzhiyun /**
2680*4882a593Smuzhiyun * audit_core_dumps - record information about processes that end abnormally
2681*4882a593Smuzhiyun * @signr: signal value
2682*4882a593Smuzhiyun *
2683*4882a593Smuzhiyun * If a process ends with a core dump, something fishy is going on and we
2684*4882a593Smuzhiyun * should record the event for investigation.
2685*4882a593Smuzhiyun */
audit_core_dumps(long signr)2686*4882a593Smuzhiyun void audit_core_dumps(long signr)
2687*4882a593Smuzhiyun {
2688*4882a593Smuzhiyun struct audit_buffer *ab;
2689*4882a593Smuzhiyun
2690*4882a593Smuzhiyun if (!audit_enabled)
2691*4882a593Smuzhiyun return;
2692*4882a593Smuzhiyun
2693*4882a593Smuzhiyun if (signr == SIGQUIT) /* don't care for those */
2694*4882a593Smuzhiyun return;
2695*4882a593Smuzhiyun
2696*4882a593Smuzhiyun ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_ABEND);
2697*4882a593Smuzhiyun if (unlikely(!ab))
2698*4882a593Smuzhiyun return;
2699*4882a593Smuzhiyun audit_log_task(ab);
2700*4882a593Smuzhiyun audit_log_format(ab, " sig=%ld res=1", signr);
2701*4882a593Smuzhiyun audit_log_end(ab);
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun
2704*4882a593Smuzhiyun /**
2705*4882a593Smuzhiyun * audit_seccomp - record information about a seccomp action
2706*4882a593Smuzhiyun * @syscall: syscall number
2707*4882a593Smuzhiyun * @signr: signal value
2708*4882a593Smuzhiyun * @code: the seccomp action
2709*4882a593Smuzhiyun *
2710*4882a593Smuzhiyun * Record the information associated with a seccomp action. Event filtering for
2711*4882a593Smuzhiyun * seccomp actions that are not to be logged is done in seccomp_log().
2712*4882a593Smuzhiyun * Therefore, this function forces auditing independent of the audit_enabled
2713*4882a593Smuzhiyun * and dummy context state because seccomp actions should be logged even when
2714*4882a593Smuzhiyun * audit is not in use.
2715*4882a593Smuzhiyun */
audit_seccomp(unsigned long syscall,long signr,int code)2716*4882a593Smuzhiyun void audit_seccomp(unsigned long syscall, long signr, int code)
2717*4882a593Smuzhiyun {
2718*4882a593Smuzhiyun struct audit_buffer *ab;
2719*4882a593Smuzhiyun
2720*4882a593Smuzhiyun ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_SECCOMP);
2721*4882a593Smuzhiyun if (unlikely(!ab))
2722*4882a593Smuzhiyun return;
2723*4882a593Smuzhiyun audit_log_task(ab);
2724*4882a593Smuzhiyun audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
2725*4882a593Smuzhiyun signr, syscall_get_arch(current), syscall,
2726*4882a593Smuzhiyun in_compat_syscall(), KSTK_EIP(current), code);
2727*4882a593Smuzhiyun audit_log_end(ab);
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun
audit_seccomp_actions_logged(const char * names,const char * old_names,int res)2730*4882a593Smuzhiyun void audit_seccomp_actions_logged(const char *names, const char *old_names,
2731*4882a593Smuzhiyun int res)
2732*4882a593Smuzhiyun {
2733*4882a593Smuzhiyun struct audit_buffer *ab;
2734*4882a593Smuzhiyun
2735*4882a593Smuzhiyun if (!audit_enabled)
2736*4882a593Smuzhiyun return;
2737*4882a593Smuzhiyun
2738*4882a593Smuzhiyun ab = audit_log_start(audit_context(), GFP_KERNEL,
2739*4882a593Smuzhiyun AUDIT_CONFIG_CHANGE);
2740*4882a593Smuzhiyun if (unlikely(!ab))
2741*4882a593Smuzhiyun return;
2742*4882a593Smuzhiyun
2743*4882a593Smuzhiyun audit_log_format(ab,
2744*4882a593Smuzhiyun "op=seccomp-logging actions=%s old-actions=%s res=%d",
2745*4882a593Smuzhiyun names, old_names, res);
2746*4882a593Smuzhiyun audit_log_end(ab);
2747*4882a593Smuzhiyun }
2748*4882a593Smuzhiyun
audit_killed_trees(void)2749*4882a593Smuzhiyun struct list_head *audit_killed_trees(void)
2750*4882a593Smuzhiyun {
2751*4882a593Smuzhiyun struct audit_context *ctx = audit_context();
2752*4882a593Smuzhiyun if (likely(!ctx || !ctx->in_syscall))
2753*4882a593Smuzhiyun return NULL;
2754*4882a593Smuzhiyun return &ctx->killed_trees;
2755*4882a593Smuzhiyun }
2756