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