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