Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 2 | /* auditfilter.c -- filtering of audit events |
| 3 | * |
| 4 | * Copyright 2003-2004 Red Hat, Inc. |
| 5 | * Copyright 2005 Hewlett-Packard Development Company, L.P. |
| 6 | * Copyright 2005 IBM Corporation |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Richard Guy Briggs | f952d10 | 2014-01-27 17:38:42 -0500 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/audit.h> |
| 13 | #include <linux/kthread.h> |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 14 | #include <linux/mutex.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/namei.h> |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 17 | #include <linux/netlink.h> |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 20 | #include <linux/security.h> |
Eric W. Biederman | 48095d9 | 2014-02-03 17:25:33 -0800 | [diff] [blame] | 21 | #include <net/net_namespace.h> |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 22 | #include <net/sock.h> |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 23 | #include "audit.h" |
| 24 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 25 | /* |
| 26 | * Locking model: |
| 27 | * |
| 28 | * audit_filter_mutex: |
Scott Matheina | 725131e | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 29 | * Synchronizes writes and blocking reads of audit's filterlist |
| 30 | * data. Rcu is used to traverse the filterlist and access |
| 31 | * contents of structs audit_entry, audit_watch and opaque |
| 32 | * LSM rules during filtering. If modified, these structures |
| 33 | * must be copied and replace their counterparts in the filterlist. |
| 34 | * An audit_parent struct is not accessed during filtering, so may |
| 35 | * be written directly provided audit_filter_mutex is held. |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 36 | */ |
| 37 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 38 | /* Audit filter lists, defined in <linux/audit.h> */ |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 39 | struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
| 40 | LIST_HEAD_INIT(audit_filter_list[0]), |
| 41 | LIST_HEAD_INIT(audit_filter_list[1]), |
| 42 | LIST_HEAD_INIT(audit_filter_list[2]), |
| 43 | LIST_HEAD_INIT(audit_filter_list[3]), |
| 44 | LIST_HEAD_INIT(audit_filter_list[4]), |
| 45 | LIST_HEAD_INIT(audit_filter_list[5]), |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 46 | LIST_HEAD_INIT(audit_filter_list[6]), |
| 47 | #if AUDIT_NR_FILTERS != 7 |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 48 | #error Fix audit_filter_list initialiser |
| 49 | #endif |
| 50 | }; |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 51 | static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = { |
| 52 | LIST_HEAD_INIT(audit_rules_list[0]), |
| 53 | LIST_HEAD_INIT(audit_rules_list[1]), |
| 54 | LIST_HEAD_INIT(audit_rules_list[2]), |
| 55 | LIST_HEAD_INIT(audit_rules_list[3]), |
| 56 | LIST_HEAD_INIT(audit_rules_list[4]), |
| 57 | LIST_HEAD_INIT(audit_rules_list[5]), |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 58 | LIST_HEAD_INIT(audit_rules_list[6]), |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 59 | }; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 60 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 61 | DEFINE_MUTEX(audit_filter_mutex); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 62 | |
Richard Guy Briggs | 219ca39 | 2014-03-26 07:26:47 -0400 | [diff] [blame] | 63 | static void audit_free_lsm_field(struct audit_field *f) |
| 64 | { |
| 65 | switch (f->type) { |
| 66 | case AUDIT_SUBJ_USER: |
| 67 | case AUDIT_SUBJ_ROLE: |
| 68 | case AUDIT_SUBJ_TYPE: |
| 69 | case AUDIT_SUBJ_SEN: |
| 70 | case AUDIT_SUBJ_CLR: |
| 71 | case AUDIT_OBJ_USER: |
| 72 | case AUDIT_OBJ_ROLE: |
| 73 | case AUDIT_OBJ_TYPE: |
| 74 | case AUDIT_OBJ_LEV_LOW: |
| 75 | case AUDIT_OBJ_LEV_HIGH: |
| 76 | kfree(f->lsm_str); |
| 77 | security_audit_rule_free(f->lsm_rule); |
| 78 | } |
| 79 | } |
| 80 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 81 | static inline void audit_free_rule(struct audit_entry *e) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 82 | { |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 83 | int i; |
Zhenwen Xu | c28bb7d | 2009-03-12 22:16:12 +0800 | [diff] [blame] | 84 | struct audit_krule *erule = &e->rule; |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 85 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 86 | /* some rules don't have associated watches */ |
Zhenwen Xu | c28bb7d | 2009-03-12 22:16:12 +0800 | [diff] [blame] | 87 | if (erule->watch) |
| 88 | audit_put_watch(erule->watch); |
| 89 | if (erule->fields) |
Richard Guy Briggs | 219ca39 | 2014-03-26 07:26:47 -0400 | [diff] [blame] | 90 | for (i = 0; i < erule->field_count; i++) |
| 91 | audit_free_lsm_field(&erule->fields[i]); |
Zhenwen Xu | c28bb7d | 2009-03-12 22:16:12 +0800 | [diff] [blame] | 92 | kfree(erule->fields); |
| 93 | kfree(erule->filterkey); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 94 | kfree(e); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 97 | void audit_free_rule_rcu(struct rcu_head *head) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 98 | { |
| 99 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); |
| 100 | audit_free_rule(e); |
| 101 | } |
| 102 | |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 103 | /* Initialize an audit filterlist entry. */ |
| 104 | static inline struct audit_entry *audit_init_entry(u32 field_count) |
| 105 | { |
| 106 | struct audit_entry *entry; |
| 107 | struct audit_field *fields; |
| 108 | |
| 109 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 110 | if (unlikely(!entry)) |
| 111 | return NULL; |
| 112 | |
Fabian Frederick | bab5e2d | 2014-08-06 16:03:22 -0700 | [diff] [blame] | 113 | fields = kcalloc(field_count, sizeof(*fields), GFP_KERNEL); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 114 | if (unlikely(!fields)) { |
| 115 | kfree(entry); |
| 116 | return NULL; |
| 117 | } |
| 118 | entry->rule.fields = fields; |
| 119 | |
| 120 | return entry; |
| 121 | } |
| 122 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 123 | /* Unpack a filter field's string representation from user-space |
| 124 | * buffer. */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 125 | char *audit_unpack_string(void **bufp, size_t *remain, size_t len) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 126 | { |
| 127 | char *str; |
| 128 | |
| 129 | if (!*bufp || (len == 0) || (len > *remain)) |
| 130 | return ERR_PTR(-EINVAL); |
| 131 | |
| 132 | /* Of the currently implemented string fields, PATH_MAX |
| 133 | * defines the longest valid length. |
| 134 | */ |
| 135 | if (len > PATH_MAX) |
| 136 | return ERR_PTR(-ENAMETOOLONG); |
| 137 | |
| 138 | str = kmalloc(len + 1, GFP_KERNEL); |
| 139 | if (unlikely(!str)) |
| 140 | return ERR_PTR(-ENOMEM); |
| 141 | |
| 142 | memcpy(str, *bufp, len); |
| 143 | str[len] = 0; |
| 144 | *bufp += len; |
| 145 | *remain -= len; |
| 146 | |
| 147 | return str; |
| 148 | } |
| 149 | |
Wei Yuan | fd97646 | 2016-02-06 15:39:47 +0800 | [diff] [blame] | 150 | /* Translate an inode field to kernel representation. */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 151 | static inline int audit_to_inode(struct audit_krule *krule, |
| 152 | struct audit_field *f) |
| 153 | { |
| 154 | if (krule->listnr != AUDIT_FILTER_EXIT || |
Richard Guy Briggs | 3639f17 | 2014-10-02 22:05:18 -0400 | [diff] [blame] | 155 | krule->inode_f || krule->watch || krule->tree || |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 156 | (f->op != Audit_equal && f->op != Audit_not_equal)) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | |
| 159 | krule->inode_f = f; |
| 160 | return 0; |
| 161 | } |
| 162 | |
Al Viro | b915543 | 2006-07-01 03:56:16 -0400 | [diff] [blame] | 163 | static __u32 *classes[AUDIT_SYSCALL_CLASSES]; |
| 164 | |
| 165 | int __init audit_register_class(int class, unsigned *list) |
| 166 | { |
Fabian Frederick | bab5e2d | 2014-08-06 16:03:22 -0700 | [diff] [blame] | 167 | __u32 *p = kcalloc(AUDIT_BITMASK_SIZE, sizeof(__u32), GFP_KERNEL); |
Al Viro | b915543 | 2006-07-01 03:56:16 -0400 | [diff] [blame] | 168 | if (!p) |
| 169 | return -ENOMEM; |
| 170 | while (*list != ~0U) { |
| 171 | unsigned n = *list++; |
| 172 | if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) { |
| 173 | kfree(p); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | p[AUDIT_WORD(n)] |= AUDIT_BIT(n); |
| 177 | } |
| 178 | if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) { |
| 179 | kfree(p); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | classes[class] = p; |
| 183 | return 0; |
| 184 | } |
| 185 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 186 | int audit_match_class(int class, unsigned syscall) |
| 187 | { |
Klaus Weidner | c926e4f | 2007-05-16 17:45:42 -0500 | [diff] [blame] | 188 | if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32)) |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 189 | return 0; |
| 190 | if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class])) |
| 191 | return 0; |
| 192 | return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall); |
| 193 | } |
| 194 | |
Al Viro | 327b9ee | 2007-05-15 20:37:10 +0100 | [diff] [blame] | 195 | #ifdef CONFIG_AUDITSYSCALL |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 196 | static inline int audit_match_class_bits(int class, u32 *mask) |
| 197 | { |
| 198 | int i; |
| 199 | |
| 200 | if (classes[class]) { |
| 201 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 202 | if (mask[i] & classes[class][i]) |
| 203 | return 0; |
| 204 | } |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | static int audit_match_signal(struct audit_entry *entry) |
| 209 | { |
| 210 | struct audit_field *arch = entry->rule.arch_f; |
| 211 | |
| 212 | if (!arch) { |
| 213 | /* When arch is unspecified, we must check both masks on biarch |
| 214 | * as syscall number alone is ambiguous. */ |
| 215 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, |
| 216 | entry->rule.mask) && |
| 217 | audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, |
| 218 | entry->rule.mask)); |
| 219 | } |
| 220 | |
| 221 | switch(audit_classify_arch(arch->val)) { |
| 222 | case 0: /* native */ |
| 223 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, |
| 224 | entry->rule.mask)); |
| 225 | case 1: /* 32bit on biarch */ |
| 226 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, |
| 227 | entry->rule.mask)); |
| 228 | default: |
| 229 | return 1; |
| 230 | } |
| 231 | } |
Al Viro | 327b9ee | 2007-05-15 20:37:10 +0100 | [diff] [blame] | 232 | #endif |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 233 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 234 | /* Common user-space to kernel rule translation. */ |
Eric Paris | 56c4911 | 2014-04-02 15:46:42 -0400 | [diff] [blame] | 235 | static inline struct audit_entry *audit_to_entry_common(struct audit_rule_data *rule) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 236 | { |
| 237 | unsigned listnr; |
| 238 | struct audit_entry *entry; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 239 | int i, err; |
| 240 | |
| 241 | err = -EINVAL; |
| 242 | listnr = rule->flags & ~AUDIT_FILTER_PREPEND; |
| 243 | switch(listnr) { |
| 244 | default: |
| 245 | goto exit_err; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 246 | #ifdef CONFIG_AUDITSYSCALL |
| 247 | case AUDIT_FILTER_ENTRY: |
Richard Guy Briggs | 5260ecc | 2018-02-14 21:47:43 -0500 | [diff] [blame] | 248 | pr_err("AUDIT_FILTER_ENTRY is deprecated\n"); |
| 249 | goto exit_err; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 250 | case AUDIT_FILTER_EXIT: |
| 251 | case AUDIT_FILTER_TASK: |
| 252 | #endif |
Eric Paris | 7ff68e5 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 253 | case AUDIT_FILTER_USER: |
Richard Guy Briggs | d904ac0 | 2018-06-05 11:45:07 -0400 | [diff] [blame] | 254 | case AUDIT_FILTER_EXCLUDE: |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 255 | case AUDIT_FILTER_FS: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 256 | ; |
| 257 | } |
Al Viro | 014149c | 2006-05-23 01:36:13 -0400 | [diff] [blame] | 258 | if (unlikely(rule->action == AUDIT_POSSIBLE)) { |
Richard Guy Briggs | f952d10 | 2014-01-27 17:38:42 -0500 | [diff] [blame] | 259 | pr_err("AUDIT_POSSIBLE is deprecated\n"); |
Al Viro | 014149c | 2006-05-23 01:36:13 -0400 | [diff] [blame] | 260 | goto exit_err; |
| 261 | } |
| 262 | if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 263 | goto exit_err; |
| 264 | if (rule->field_count > AUDIT_MAX_FIELDS) |
| 265 | goto exit_err; |
| 266 | |
| 267 | err = -ENOMEM; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 268 | entry = audit_init_entry(rule->field_count); |
| 269 | if (!entry) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 270 | goto exit_err; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 271 | |
| 272 | entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND; |
| 273 | entry->rule.listnr = listnr; |
| 274 | entry->rule.action = rule->action; |
| 275 | entry->rule.field_count = rule->field_count; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 276 | |
| 277 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 278 | entry->rule.mask[i] = rule->mask[i]; |
| 279 | |
Al Viro | b915543 | 2006-07-01 03:56:16 -0400 | [diff] [blame] | 280 | for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) { |
| 281 | int bit = AUDIT_BITMASK_SIZE * 32 - i - 1; |
| 282 | __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)]; |
| 283 | __u32 *class; |
| 284 | |
| 285 | if (!(*p & AUDIT_BIT(bit))) |
| 286 | continue; |
| 287 | *p &= ~AUDIT_BIT(bit); |
| 288 | class = classes[i]; |
| 289 | if (class) { |
| 290 | int j; |
| 291 | for (j = 0; j < AUDIT_BITMASK_SIZE; j++) |
| 292 | entry->rule.mask[j] |= class[j]; |
| 293 | } |
| 294 | } |
| 295 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 296 | return entry; |
| 297 | |
| 298 | exit_err: |
| 299 | return ERR_PTR(err); |
| 300 | } |
| 301 | |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 302 | static u32 audit_ops[] = |
| 303 | { |
| 304 | [Audit_equal] = AUDIT_EQUAL, |
| 305 | [Audit_not_equal] = AUDIT_NOT_EQUAL, |
| 306 | [Audit_bitmask] = AUDIT_BIT_MASK, |
| 307 | [Audit_bittest] = AUDIT_BIT_TEST, |
| 308 | [Audit_lt] = AUDIT_LESS_THAN, |
| 309 | [Audit_gt] = AUDIT_GREATER_THAN, |
| 310 | [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL, |
| 311 | [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL, |
| 312 | }; |
| 313 | |
| 314 | static u32 audit_to_op(u32 op) |
| 315 | { |
| 316 | u32 n; |
| 317 | for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++) |
| 318 | ; |
| 319 | return n; |
| 320 | } |
| 321 | |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 322 | /* check if an audit field is valid */ |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 323 | static int audit_field_valid(struct audit_entry *entry, struct audit_field *f) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 324 | { |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 325 | switch (f->type) { |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 326 | case AUDIT_MSGTYPE: |
Richard Guy Briggs | d904ac0 | 2018-06-05 11:45:07 -0400 | [diff] [blame] | 327 | if (entry->rule.listnr != AUDIT_FILTER_EXCLUDE && |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 328 | entry->rule.listnr != AUDIT_FILTER_USER) |
| 329 | return -EINVAL; |
| 330 | break; |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 331 | case AUDIT_FSTYPE: |
| 332 | if (entry->rule.listnr != AUDIT_FILTER_FS) |
| 333 | return -EINVAL; |
| 334 | break; |
| 335 | } |
| 336 | |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 337 | switch (entry->rule.listnr) { |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 338 | case AUDIT_FILTER_FS: |
| 339 | switch(f->type) { |
| 340 | case AUDIT_FSTYPE: |
| 341 | case AUDIT_FILTERKEY: |
| 342 | break; |
| 343 | default: |
| 344 | return -EINVAL; |
| 345 | } |
Nicholas Mc Guire | b7a84de | 2017-05-02 10:16:03 -0400 | [diff] [blame] | 346 | } |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 347 | |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 348 | /* Check for valid field type and op */ |
| 349 | switch (f->type) { |
| 350 | case AUDIT_ARG0: |
| 351 | case AUDIT_ARG1: |
| 352 | case AUDIT_ARG2: |
| 353 | case AUDIT_ARG3: |
| 354 | case AUDIT_PERS: /* <uapi/linux/personality.h> */ |
| 355 | case AUDIT_DEVMINOR: |
| 356 | /* all ops are valid */ |
| 357 | break; |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 358 | case AUDIT_UID: |
| 359 | case AUDIT_EUID: |
| 360 | case AUDIT_SUID: |
| 361 | case AUDIT_FSUID: |
| 362 | case AUDIT_LOGINUID: |
| 363 | case AUDIT_OBJ_UID: |
| 364 | case AUDIT_GID: |
| 365 | case AUDIT_EGID: |
| 366 | case AUDIT_SGID: |
| 367 | case AUDIT_FSGID: |
| 368 | case AUDIT_OBJ_GID: |
| 369 | case AUDIT_PID: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 370 | case AUDIT_MSGTYPE: |
| 371 | case AUDIT_PPID: |
| 372 | case AUDIT_DEVMAJOR: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 373 | case AUDIT_EXIT: |
| 374 | case AUDIT_SUCCESS: |
Eric Paris | 7812203 | 2013-09-04 15:01:43 -0400 | [diff] [blame] | 375 | case AUDIT_INODE: |
Richard Guy Briggs | 8fae477 | 2016-11-20 16:47:55 -0500 | [diff] [blame] | 376 | case AUDIT_SESSIONID: |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 377 | case AUDIT_SUBJ_SEN: |
| 378 | case AUDIT_SUBJ_CLR: |
| 379 | case AUDIT_OBJ_LEV_LOW: |
| 380 | case AUDIT_OBJ_LEV_HIGH: |
Richard Guy Briggs | bf36123 | 2019-05-09 20:01:36 -0400 | [diff] [blame] | 381 | case AUDIT_SADDR_FAM: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 382 | /* bit ops are only useful on syscall args */ |
| 383 | if (f->op == Audit_bitmask || f->op == Audit_bittest) |
| 384 | return -EINVAL; |
| 385 | break; |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 386 | case AUDIT_SUBJ_USER: |
| 387 | case AUDIT_SUBJ_ROLE: |
| 388 | case AUDIT_SUBJ_TYPE: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 389 | case AUDIT_OBJ_USER: |
| 390 | case AUDIT_OBJ_ROLE: |
| 391 | case AUDIT_OBJ_TYPE: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 392 | case AUDIT_WATCH: |
| 393 | case AUDIT_DIR: |
| 394 | case AUDIT_FILTERKEY: |
Eric W. Biederman | 780a765 | 2013-04-09 02:22:10 -0700 | [diff] [blame] | 395 | case AUDIT_LOGINUID_SET: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 396 | case AUDIT_ARCH: |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 397 | case AUDIT_FSTYPE: |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 398 | case AUDIT_PERM: |
| 399 | case AUDIT_FILETYPE: |
| 400 | case AUDIT_FIELD_COMPARE: |
| 401 | case AUDIT_EXE: |
| 402 | /* only equal and not equal valid ops */ |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 403 | if (f->op != Audit_not_equal && f->op != Audit_equal) |
| 404 | return -EINVAL; |
| 405 | break; |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 406 | default: |
| 407 | /* field not recognized */ |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | /* Check for select valid field values */ |
| 412 | switch (f->type) { |
| 413 | case AUDIT_LOGINUID_SET: |
| 414 | if ((f->val != 0) && (f->val != 1)) |
| 415 | return -EINVAL; |
| 416 | break; |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 417 | case AUDIT_PERM: |
| 418 | if (f->val & ~15) |
| 419 | return -EINVAL; |
| 420 | break; |
| 421 | case AUDIT_FILETYPE: |
| 422 | if (f->val & ~S_IFMT) |
| 423 | return -EINVAL; |
| 424 | break; |
| 425 | case AUDIT_FIELD_COMPARE: |
| 426 | if (f->val > AUDIT_MAX_FIELD_COMPARE) |
| 427 | return -EINVAL; |
| 428 | break; |
Richard Guy Briggs | bf36123 | 2019-05-09 20:01:36 -0400 | [diff] [blame] | 429 | case AUDIT_SADDR_FAM: |
| 430 | if (f->val >= AF_MAX) |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 431 | return -EINVAL; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 432 | break; |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 433 | default: |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 434 | break; |
Nicholas Mc Guire | b7a84de | 2017-05-02 10:16:03 -0400 | [diff] [blame] | 435 | } |
Richard Guy Briggs | ecc6890 | 2019-05-22 17:51:09 -0400 | [diff] [blame] | 436 | |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 437 | return 0; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 438 | } |
| 439 | |
Wei Yuan | fd97646 | 2016-02-06 15:39:47 +0800 | [diff] [blame] | 440 | /* Translate struct audit_rule_data to kernel's rule representation. */ |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 441 | static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data, |
| 442 | size_t datasz) |
| 443 | { |
| 444 | int err = 0; |
| 445 | struct audit_entry *entry; |
| 446 | void *bufp; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 447 | size_t remain = datasz - sizeof(struct audit_rule_data); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 448 | int i; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 449 | char *str; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 450 | struct audit_fsnotify_mark *audit_mark; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 451 | |
Eric Paris | 56c4911 | 2014-04-02 15:46:42 -0400 | [diff] [blame] | 452 | entry = audit_to_entry_common(data); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 453 | if (IS_ERR(entry)) |
| 454 | goto exit_nofree; |
| 455 | |
| 456 | bufp = data->buf; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 457 | for (i = 0; i < data->field_count; i++) { |
| 458 | struct audit_field *f = &entry->rule.fields[i]; |
| 459 | |
| 460 | err = -EINVAL; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 461 | |
| 462 | f->op = audit_to_op(data->fieldflags[i]); |
| 463 | if (f->op == Audit_bad) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 464 | goto exit_free; |
| 465 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 466 | f->type = data->fields[i]; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 467 | f->val = data->values[i]; |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 468 | |
Eric W. Biederman | 780a765 | 2013-04-09 02:22:10 -0700 | [diff] [blame] | 469 | /* Support legacy tests for a valid loginuid */ |
Richard Guy Briggs | 42f7446 | 2013-05-20 15:08:18 -0400 | [diff] [blame] | 470 | if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) { |
Eric W. Biederman | 780a765 | 2013-04-09 02:22:10 -0700 | [diff] [blame] | 471 | f->type = AUDIT_LOGINUID_SET; |
| 472 | f->val = 0; |
Richard Guy Briggs | 041d7b9 | 2014-12-23 13:02:04 -0500 | [diff] [blame] | 473 | entry->rule.pflags |= AUDIT_LOGINUID_LEGACY; |
Richard Guy Briggs | f1dc486 | 2013-12-11 13:52:26 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Eric Paris | 62062cf | 2013-04-16 13:08:43 -0400 | [diff] [blame] | 476 | err = audit_field_valid(entry, f); |
| 477 | if (err) |
| 478 | goto exit_free; |
| 479 | |
| 480 | err = -EINVAL; |
Eric Paris | ab61d38 | 2013-04-16 17:26:51 -0400 | [diff] [blame] | 481 | switch (f->type) { |
Eric W. Biederman | 780a765 | 2013-04-09 02:22:10 -0700 | [diff] [blame] | 482 | case AUDIT_LOGINUID: |
Al Viro | 0a73dcc | 2006-06-05 08:15:59 -0400 | [diff] [blame] | 483 | case AUDIT_UID: |
| 484 | case AUDIT_EUID: |
| 485 | case AUDIT_SUID: |
| 486 | case AUDIT_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 487 | case AUDIT_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 488 | f->uid = make_kuid(current_user_ns(), f->val); |
| 489 | if (!uid_valid(f->uid)) |
| 490 | goto exit_free; |
| 491 | break; |
Al Viro | 0a73dcc | 2006-06-05 08:15:59 -0400 | [diff] [blame] | 492 | case AUDIT_GID: |
| 493 | case AUDIT_EGID: |
| 494 | case AUDIT_SGID: |
| 495 | case AUDIT_FSGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 496 | case AUDIT_OBJ_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 497 | f->gid = make_kgid(current_user_ns(), f->val); |
| 498 | if (!gid_valid(f->gid)) |
| 499 | goto exit_free; |
| 500 | break; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 501 | case AUDIT_ARCH: |
| 502 | entry->rule.arch_f = f; |
| 503 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 504 | case AUDIT_SUBJ_USER: |
| 505 | case AUDIT_SUBJ_ROLE: |
| 506 | case AUDIT_SUBJ_TYPE: |
| 507 | case AUDIT_SUBJ_SEN: |
| 508 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 509 | case AUDIT_OBJ_USER: |
| 510 | case AUDIT_OBJ_ROLE: |
| 511 | case AUDIT_OBJ_TYPE: |
| 512 | case AUDIT_OBJ_LEV_LOW: |
| 513 | case AUDIT_OBJ_LEV_HIGH: |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 514 | str = audit_unpack_string(&bufp, &remain, f->val); |
| 515 | if (IS_ERR(str)) |
| 516 | goto exit_free; |
| 517 | entry->rule.buflen += f->val; |
| 518 | |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 519 | err = security_audit_rule_init(f->type, f->op, str, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 520 | (void **)&f->lsm_rule); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 521 | /* Keep currently invalid fields around in case they |
| 522 | * become valid after a policy reload. */ |
| 523 | if (err == -EINVAL) { |
Richard Guy Briggs | f952d10 | 2014-01-27 17:38:42 -0500 | [diff] [blame] | 524 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
| 525 | str); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 526 | err = 0; |
| 527 | } |
| 528 | if (err) { |
| 529 | kfree(str); |
| 530 | goto exit_free; |
| 531 | } else |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 532 | f->lsm_str = str; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 533 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 534 | case AUDIT_WATCH: |
| 535 | str = audit_unpack_string(&bufp, &remain, f->val); |
| 536 | if (IS_ERR(str)) |
| 537 | goto exit_free; |
| 538 | entry->rule.buflen += f->val; |
| 539 | |
| 540 | err = audit_to_watch(&entry->rule, str, f->val, f->op); |
| 541 | if (err) { |
| 542 | kfree(str); |
| 543 | goto exit_free; |
| 544 | } |
| 545 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 546 | case AUDIT_DIR: |
| 547 | str = audit_unpack_string(&bufp, &remain, f->val); |
| 548 | if (IS_ERR(str)) |
| 549 | goto exit_free; |
| 550 | entry->rule.buflen += f->val; |
| 551 | |
| 552 | err = audit_make_tree(&entry->rule, str, f->op); |
| 553 | kfree(str); |
| 554 | if (err) |
| 555 | goto exit_free; |
| 556 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 557 | case AUDIT_INODE: |
| 558 | err = audit_to_inode(&entry->rule, f); |
| 559 | if (err) |
| 560 | goto exit_free; |
| 561 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 562 | case AUDIT_FILTERKEY: |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 563 | if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN) |
| 564 | goto exit_free; |
| 565 | str = audit_unpack_string(&bufp, &remain, f->val); |
| 566 | if (IS_ERR(str)) |
| 567 | goto exit_free; |
| 568 | entry->rule.buflen += f->val; |
| 569 | entry->rule.filterkey = str; |
| 570 | break; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 571 | case AUDIT_EXE: |
| 572 | if (entry->rule.exe || f->val > PATH_MAX) |
| 573 | goto exit_free; |
| 574 | str = audit_unpack_string(&bufp, &remain, f->val); |
| 575 | if (IS_ERR(str)) { |
| 576 | err = PTR_ERR(str); |
| 577 | goto exit_free; |
| 578 | } |
| 579 | entry->rule.buflen += f->val; |
| 580 | |
| 581 | audit_mark = audit_alloc_mark(&entry->rule, str, f->val); |
| 582 | if (IS_ERR(audit_mark)) { |
| 583 | kfree(str); |
| 584 | err = PTR_ERR(audit_mark); |
| 585 | goto exit_free; |
| 586 | } |
| 587 | entry->rule.exe = audit_mark; |
| 588 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 592 | if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal) |
| 593 | entry->rule.inode_f = NULL; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 594 | |
| 595 | exit_nofree: |
| 596 | return entry; |
| 597 | |
| 598 | exit_free: |
Chen Gang | 373e0f3 | 2013-04-29 15:05:18 -0700 | [diff] [blame] | 599 | if (entry->rule.tree) |
| 600 | audit_put_tree(entry->rule.tree); /* that's the temporary one */ |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 601 | if (entry->rule.exe) |
| 602 | audit_remove_mark(entry->rule.exe); /* that's the template one */ |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 603 | audit_free_rule(entry); |
| 604 | return ERR_PTR(err); |
| 605 | } |
| 606 | |
| 607 | /* Pack a filter field's string representation into data block. */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 608 | static inline size_t audit_pack_string(void **bufp, const char *str) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 609 | { |
| 610 | size_t len = strlen(str); |
| 611 | |
| 612 | memcpy(*bufp, str, len); |
| 613 | *bufp += len; |
| 614 | |
| 615 | return len; |
| 616 | } |
| 617 | |
Wei Yuan | fd97646 | 2016-02-06 15:39:47 +0800 | [diff] [blame] | 618 | /* Translate kernel rule representation to struct audit_rule_data. */ |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 619 | static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule) |
| 620 | { |
| 621 | struct audit_rule_data *data; |
| 622 | void *bufp; |
| 623 | int i; |
| 624 | |
| 625 | data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL); |
| 626 | if (unlikely(!data)) |
Amy Griffis | 0a3b483 | 2006-05-02 15:06:01 -0400 | [diff] [blame] | 627 | return NULL; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 628 | memset(data, 0, sizeof(*data)); |
| 629 | |
| 630 | data->flags = krule->flags | krule->listnr; |
| 631 | data->action = krule->action; |
| 632 | data->field_count = krule->field_count; |
| 633 | bufp = data->buf; |
| 634 | for (i = 0; i < data->field_count; i++) { |
| 635 | struct audit_field *f = &krule->fields[i]; |
| 636 | |
| 637 | data->fields[i] = f->type; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 638 | data->fieldflags[i] = audit_ops[f->op]; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 639 | switch(f->type) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 640 | case AUDIT_SUBJ_USER: |
| 641 | case AUDIT_SUBJ_ROLE: |
| 642 | case AUDIT_SUBJ_TYPE: |
| 643 | case AUDIT_SUBJ_SEN: |
| 644 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 645 | case AUDIT_OBJ_USER: |
| 646 | case AUDIT_OBJ_ROLE: |
| 647 | case AUDIT_OBJ_TYPE: |
| 648 | case AUDIT_OBJ_LEV_LOW: |
| 649 | case AUDIT_OBJ_LEV_HIGH: |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 650 | data->buflen += data->values[i] = |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 651 | audit_pack_string(&bufp, f->lsm_str); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 652 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 653 | case AUDIT_WATCH: |
| 654 | data->buflen += data->values[i] = |
Eric Paris | cfcad62 | 2009-06-11 14:31:36 -0400 | [diff] [blame] | 655 | audit_pack_string(&bufp, |
| 656 | audit_watch_path(krule->watch)); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 657 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 658 | case AUDIT_DIR: |
| 659 | data->buflen += data->values[i] = |
| 660 | audit_pack_string(&bufp, |
| 661 | audit_tree_path(krule->tree)); |
| 662 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 663 | case AUDIT_FILTERKEY: |
| 664 | data->buflen += data->values[i] = |
| 665 | audit_pack_string(&bufp, krule->filterkey); |
| 666 | break; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 667 | case AUDIT_EXE: |
| 668 | data->buflen += data->values[i] = |
| 669 | audit_pack_string(&bufp, audit_mark_path(krule->exe)); |
| 670 | break; |
Richard Guy Briggs | 041d7b9 | 2014-12-23 13:02:04 -0500 | [diff] [blame] | 671 | case AUDIT_LOGINUID_SET: |
| 672 | if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) { |
| 673 | data->fields[i] = AUDIT_LOGINUID; |
| 674 | data->values[i] = AUDIT_UID_UNSET; |
| 675 | break; |
| 676 | } |
Gustavo A. R. Silva | 131d34c | 2019-02-12 14:46:00 -0600 | [diff] [blame] | 677 | /* fall through - if set */ |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 678 | default: |
| 679 | data->values[i] = f->val; |
| 680 | } |
| 681 | } |
| 682 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i]; |
| 683 | |
| 684 | return data; |
| 685 | } |
| 686 | |
| 687 | /* Compare two rules in kernel format. Considered success if rules |
| 688 | * don't match. */ |
| 689 | static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 690 | { |
| 691 | int i; |
| 692 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 693 | if (a->flags != b->flags || |
Richard Guy Briggs | 041d7b9 | 2014-12-23 13:02:04 -0500 | [diff] [blame] | 694 | a->pflags != b->pflags || |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 695 | a->listnr != b->listnr || |
| 696 | a->action != b->action || |
| 697 | a->field_count != b->field_count) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 698 | return 1; |
| 699 | |
| 700 | for (i = 0; i < a->field_count; i++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 701 | if (a->fields[i].type != b->fields[i].type || |
| 702 | a->fields[i].op != b->fields[i].op) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 703 | return 1; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 704 | |
| 705 | switch(a->fields[i].type) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 706 | case AUDIT_SUBJ_USER: |
| 707 | case AUDIT_SUBJ_ROLE: |
| 708 | case AUDIT_SUBJ_TYPE: |
| 709 | case AUDIT_SUBJ_SEN: |
| 710 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 711 | case AUDIT_OBJ_USER: |
| 712 | case AUDIT_OBJ_ROLE: |
| 713 | case AUDIT_OBJ_TYPE: |
| 714 | case AUDIT_OBJ_LEV_LOW: |
| 715 | case AUDIT_OBJ_LEV_HIGH: |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 716 | if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str)) |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 717 | return 1; |
| 718 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 719 | case AUDIT_WATCH: |
Eric Paris | cfcad62 | 2009-06-11 14:31:36 -0400 | [diff] [blame] | 720 | if (strcmp(audit_watch_path(a->watch), |
| 721 | audit_watch_path(b->watch))) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 722 | return 1; |
| 723 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 724 | case AUDIT_DIR: |
| 725 | if (strcmp(audit_tree_path(a->tree), |
| 726 | audit_tree_path(b->tree))) |
| 727 | return 1; |
| 728 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 729 | case AUDIT_FILTERKEY: |
| 730 | /* both filterkeys exist based on above type compare */ |
| 731 | if (strcmp(a->filterkey, b->filterkey)) |
| 732 | return 1; |
| 733 | break; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 734 | case AUDIT_EXE: |
| 735 | /* both paths exist based on above type compare */ |
| 736 | if (strcmp(audit_mark_path(a->exe), |
| 737 | audit_mark_path(b->exe))) |
| 738 | return 1; |
| 739 | break; |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 740 | case AUDIT_UID: |
| 741 | case AUDIT_EUID: |
| 742 | case AUDIT_SUID: |
| 743 | case AUDIT_FSUID: |
| 744 | case AUDIT_LOGINUID: |
| 745 | case AUDIT_OBJ_UID: |
| 746 | if (!uid_eq(a->fields[i].uid, b->fields[i].uid)) |
| 747 | return 1; |
| 748 | break; |
| 749 | case AUDIT_GID: |
| 750 | case AUDIT_EGID: |
| 751 | case AUDIT_SGID: |
| 752 | case AUDIT_FSGID: |
| 753 | case AUDIT_OBJ_GID: |
| 754 | if (!gid_eq(a->fields[i].gid, b->fields[i].gid)) |
| 755 | return 1; |
| 756 | break; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 757 | default: |
| 758 | if (a->fields[i].val != b->fields[i].val) |
| 759 | return 1; |
| 760 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 764 | if (a->mask[i] != b->mask[i]) |
| 765 | return 1; |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 770 | /* Duplicate LSM field information. The lsm_rule is opaque, so must be |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 771 | * re-initialized. */ |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 772 | static inline int audit_dupe_lsm_field(struct audit_field *df, |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 773 | struct audit_field *sf) |
| 774 | { |
| 775 | int ret = 0; |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 776 | char *lsm_str; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 777 | |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 778 | /* our own copy of lsm_str */ |
| 779 | lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL); |
| 780 | if (unlikely(!lsm_str)) |
Akinobu Mita | 3e1fbd1 | 2006-12-22 01:10:02 -0800 | [diff] [blame] | 781 | return -ENOMEM; |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 782 | df->lsm_str = lsm_str; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 783 | |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 784 | /* our own (refreshed) copy of lsm_rule */ |
| 785 | ret = security_audit_rule_init(df->type, df->op, df->lsm_str, |
| 786 | (void **)&df->lsm_rule); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 787 | /* Keep currently invalid fields around in case they |
| 788 | * become valid after a policy reload. */ |
| 789 | if (ret == -EINVAL) { |
Richard Guy Briggs | f952d10 | 2014-01-27 17:38:42 -0500 | [diff] [blame] | 790 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
| 791 | df->lsm_str); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 792 | ret = 0; |
| 793 | } |
| 794 | |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | /* Duplicate an audit rule. This will be a deep copy with the exception |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 799 | * of the watch - that pointer is carried over. The LSM specific fields |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 800 | * will be updated in the copy. The point is to be able to replace the old |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 801 | * rule with the new rule in the filterlist, then free the old rule. |
| 802 | * The rlist element is undefined; list manipulations are handled apart from |
| 803 | * the initial copy. */ |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 804 | struct audit_entry *audit_dupe_rule(struct audit_krule *old) |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 805 | { |
| 806 | u32 fcount = old->field_count; |
| 807 | struct audit_entry *entry; |
| 808 | struct audit_krule *new; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 809 | char *fk; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 810 | int i, err = 0; |
| 811 | |
| 812 | entry = audit_init_entry(fcount); |
| 813 | if (unlikely(!entry)) |
| 814 | return ERR_PTR(-ENOMEM); |
| 815 | |
| 816 | new = &entry->rule; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 817 | new->flags = old->flags; |
Richard Guy Briggs | 041d7b9 | 2014-12-23 13:02:04 -0500 | [diff] [blame] | 818 | new->pflags = old->pflags; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 819 | new->listnr = old->listnr; |
| 820 | new->action = old->action; |
| 821 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 822 | new->mask[i] = old->mask[i]; |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 823 | new->prio = old->prio; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 824 | new->buflen = old->buflen; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 825 | new->inode_f = old->inode_f; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 826 | new->field_count = old->field_count; |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 827 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 828 | /* |
| 829 | * note that we are OK with not refcounting here; audit_match_tree() |
| 830 | * never dereferences tree and we can't get false positives there |
| 831 | * since we'd have to have rule gone from the list *and* removed |
| 832 | * before the chunks found by lookup had been allocated, i.e. before |
| 833 | * the beginning of list scan. |
| 834 | */ |
| 835 | new->tree = old->tree; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 836 | memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount); |
| 837 | |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 838 | /* deep copy this information, updating the lsm_rule fields, because |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 839 | * the originals will all be freed when the old rule is freed. */ |
| 840 | for (i = 0; i < fcount; i++) { |
| 841 | switch (new->fields[i].type) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 842 | case AUDIT_SUBJ_USER: |
| 843 | case AUDIT_SUBJ_ROLE: |
| 844 | case AUDIT_SUBJ_TYPE: |
| 845 | case AUDIT_SUBJ_SEN: |
| 846 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 847 | case AUDIT_OBJ_USER: |
| 848 | case AUDIT_OBJ_ROLE: |
| 849 | case AUDIT_OBJ_TYPE: |
| 850 | case AUDIT_OBJ_LEV_LOW: |
| 851 | case AUDIT_OBJ_LEV_HIGH: |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 852 | err = audit_dupe_lsm_field(&new->fields[i], |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 853 | &old->fields[i]); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 854 | break; |
| 855 | case AUDIT_FILTERKEY: |
| 856 | fk = kstrdup(old->filterkey, GFP_KERNEL); |
| 857 | if (unlikely(!fk)) |
| 858 | err = -ENOMEM; |
| 859 | else |
| 860 | new->filterkey = fk; |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 861 | break; |
| 862 | case AUDIT_EXE: |
| 863 | err = audit_dupe_exe(new, old); |
| 864 | break; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 865 | } |
| 866 | if (err) { |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 867 | if (new->exe) |
| 868 | audit_remove_mark(new->exe); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 869 | audit_free_rule(entry); |
| 870 | return ERR_PTR(err); |
| 871 | } |
| 872 | } |
| 873 | |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 874 | if (old->watch) { |
| 875 | audit_get_watch(old->watch); |
| 876 | new->watch = old->watch; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 879 | return entry; |
| 880 | } |
| 881 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 882 | /* Find an existing audit rule. |
| 883 | * Caller must hold audit_filter_mutex to prevent stale rule data. */ |
| 884 | static struct audit_entry *audit_find_rule(struct audit_entry *entry, |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 885 | struct list_head **p) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 886 | { |
| 887 | struct audit_entry *e, *found = NULL; |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 888 | struct list_head *list; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 889 | int h; |
| 890 | |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 891 | if (entry->rule.inode_f) { |
| 892 | h = audit_hash_ino(entry->rule.inode_f->val); |
| 893 | *p = list = &audit_inode_hash[h]; |
| 894 | } else if (entry->rule.watch) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 895 | /* we don't know the inode number, so must walk entire hash */ |
| 896 | for (h = 0; h < AUDIT_INODE_BUCKETS; h++) { |
| 897 | list = &audit_inode_hash[h]; |
| 898 | list_for_each_entry(e, list, list) |
| 899 | if (!audit_compare_rule(&entry->rule, &e->rule)) { |
| 900 | found = e; |
| 901 | goto out; |
| 902 | } |
| 903 | } |
| 904 | goto out; |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 905 | } else { |
| 906 | *p = list = &audit_filter_list[entry->rule.listnr]; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | list_for_each_entry(e, list, list) |
| 910 | if (!audit_compare_rule(&entry->rule, &e->rule)) { |
| 911 | found = e; |
| 912 | goto out; |
| 913 | } |
| 914 | |
| 915 | out: |
| 916 | return found; |
| 917 | } |
| 918 | |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 919 | static u64 prio_low = ~0ULL/2; |
| 920 | static u64 prio_high = ~0ULL/2 - 1; |
| 921 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 922 | /* Add rule to given filterlist if not a duplicate. */ |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 923 | static inline int audit_add_rule(struct audit_entry *entry) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 924 | { |
| 925 | struct audit_entry *e; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 926 | struct audit_watch *watch = entry->rule.watch; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 927 | struct audit_tree *tree = entry->rule.tree; |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 928 | struct list_head *list; |
Paul Moore | ae9d2fb | 2015-08-05 11:19:45 -0400 | [diff] [blame] | 929 | int err = 0; |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 930 | #ifdef CONFIG_AUDITSYSCALL |
| 931 | int dont_count = 0; |
| 932 | |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 933 | /* If any of these, don't count towards total */ |
| 934 | switch(entry->rule.listnr) { |
| 935 | case AUDIT_FILTER_USER: |
Richard Guy Briggs | d904ac0 | 2018-06-05 11:45:07 -0400 | [diff] [blame] | 936 | case AUDIT_FILTER_EXCLUDE: |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 937 | case AUDIT_FILTER_FS: |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 938 | dont_count = 1; |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 939 | } |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 940 | #endif |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 941 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 942 | mutex_lock(&audit_filter_mutex); |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 943 | e = audit_find_rule(entry, &list); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 944 | if (e) { |
Eric Paris | 35fe4d0 | 2009-06-11 14:31:36 -0400 | [diff] [blame] | 945 | mutex_unlock(&audit_filter_mutex); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 946 | err = -EEXIST; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 947 | /* normally audit_add_tree_rule() will free it on failure */ |
| 948 | if (tree) |
| 949 | audit_put_tree(tree); |
Richard Guy Briggs | f8259b2 | 2015-08-01 15:41:12 -0400 | [diff] [blame] | 950 | return err; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 951 | } |
| 952 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 953 | if (watch) { |
| 954 | /* audit_filter_mutex is dropped and re-taken during this call */ |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 955 | err = audit_add_watch(&entry->rule, &list); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 956 | if (err) { |
| 957 | mutex_unlock(&audit_filter_mutex); |
Chen Gang | 2f992ee | 2013-07-08 15:59:38 -0700 | [diff] [blame] | 958 | /* |
| 959 | * normally audit_add_tree_rule() will free it |
| 960 | * on failure |
| 961 | */ |
| 962 | if (tree) |
| 963 | audit_put_tree(tree); |
Richard Guy Briggs | f8259b2 | 2015-08-01 15:41:12 -0400 | [diff] [blame] | 964 | return err; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 965 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 966 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 967 | if (tree) { |
| 968 | err = audit_add_tree_rule(&entry->rule); |
| 969 | if (err) { |
| 970 | mutex_unlock(&audit_filter_mutex); |
Richard Guy Briggs | f8259b2 | 2015-08-01 15:41:12 -0400 | [diff] [blame] | 971 | return err; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 972 | } |
| 973 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 974 | |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 975 | entry->rule.prio = ~0ULL; |
| 976 | if (entry->rule.listnr == AUDIT_FILTER_EXIT) { |
| 977 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) |
| 978 | entry->rule.prio = ++prio_high; |
| 979 | else |
| 980 | entry->rule.prio = --prio_low; |
| 981 | } |
| 982 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 983 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 984 | list_add(&entry->rule.list, |
| 985 | &audit_rules_list[entry->rule.listnr]); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 986 | list_add_rcu(&entry->list, list); |
Amy Griffis | 6a2bcee | 2006-06-02 13:16:01 -0400 | [diff] [blame] | 987 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 988 | } else { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 989 | list_add_tail(&entry->rule.list, |
| 990 | &audit_rules_list[entry->rule.listnr]); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 991 | list_add_tail_rcu(&entry->list, list); |
| 992 | } |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 993 | #ifdef CONFIG_AUDITSYSCALL |
| 994 | if (!dont_count) |
| 995 | audit_n_rules++; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 996 | |
| 997 | if (!audit_match_signal(entry)) |
| 998 | audit_signals++; |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 999 | #endif |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1000 | mutex_unlock(&audit_filter_mutex); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1001 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1002 | return err; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1005 | /* Remove an existing rule from filterlist. */ |
Richard Guy Briggs | 7f49294 | 2015-08-05 16:29:36 -0400 | [diff] [blame] | 1006 | int audit_del_rule(struct audit_entry *entry) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1007 | { |
| 1008 | struct audit_entry *e; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1009 | struct audit_tree *tree = entry->rule.tree; |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 1010 | struct list_head *list; |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 1011 | int ret = 0; |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 1012 | #ifdef CONFIG_AUDITSYSCALL |
| 1013 | int dont_count = 0; |
| 1014 | |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 1015 | /* If any of these, don't count towards total */ |
| 1016 | switch(entry->rule.listnr) { |
| 1017 | case AUDIT_FILTER_USER: |
Richard Guy Briggs | d904ac0 | 2018-06-05 11:45:07 -0400 | [diff] [blame] | 1018 | case AUDIT_FILTER_EXCLUDE: |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 1019 | case AUDIT_FILTER_FS: |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 1020 | dont_count = 1; |
Richard Guy Briggs | 42d5e37 | 2017-08-23 07:03:39 -0400 | [diff] [blame] | 1021 | } |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 1022 | #endif |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1023 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1024 | mutex_lock(&audit_filter_mutex); |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 1025 | e = audit_find_rule(entry, &list); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1026 | if (!e) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1027 | ret = -ENOENT; |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
Eric Paris | cfcad62 | 2009-06-11 14:31:36 -0400 | [diff] [blame] | 1031 | if (e->rule.watch) |
Eric Paris | a05fb6c | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 1032 | audit_remove_watch_rule(&e->rule); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1033 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1034 | if (e->rule.tree) |
| 1035 | audit_remove_tree_rule(&e->rule); |
| 1036 | |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 1037 | if (e->rule.exe) |
| 1038 | audit_remove_mark_rule(&e->rule); |
| 1039 | |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 1040 | #ifdef CONFIG_AUDITSYSCALL |
| 1041 | if (!dont_count) |
| 1042 | audit_n_rules--; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1043 | |
| 1044 | if (!audit_match_signal(entry)) |
| 1045 | audit_signals--; |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 1046 | #endif |
Richard Guy Briggs | 8c85fc9 | 2015-08-05 15:23:09 -0400 | [diff] [blame] | 1047 | |
| 1048 | list_del_rcu(&e->list); |
| 1049 | list_del(&e->rule.list); |
| 1050 | call_rcu(&e->rcu, audit_free_rule_rcu); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1051 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1052 | out: |
Richard Guy Briggs | 8c85fc9 | 2015-08-05 15:23:09 -0400 | [diff] [blame] | 1053 | mutex_unlock(&audit_filter_mutex); |
| 1054 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1055 | if (tree) |
| 1056 | audit_put_tree(tree); /* that's the temporary one */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1057 | |
| 1058 | return ret; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1061 | /* List rules using struct audit_rule_data. */ |
Paul Moore | 45a0642 | 2017-05-02 10:16:05 -0400 | [diff] [blame] | 1062 | static void audit_list_rules(int seq, struct sk_buff_head *q) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1063 | { |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 1064 | struct sk_buff *skb; |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1065 | struct audit_krule *r; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1066 | int i; |
| 1067 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1068 | /* This is a blocking read, so use audit_filter_mutex instead of rcu |
| 1069 | * iterator to sync with list writers. */ |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1070 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1071 | list_for_each_entry(r, &audit_rules_list[i], list) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1072 | struct audit_rule_data *data; |
| 1073 | |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1074 | data = audit_krule_to_data(r); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1075 | if (unlikely(!data)) |
| 1076 | break; |
Paul Moore | 45a0642 | 2017-05-02 10:16:05 -0400 | [diff] [blame] | 1077 | skb = audit_make_reply(seq, AUDIT_LIST_RULES, 0, 1, |
| 1078 | data, |
Richard Guy Briggs | f944163 | 2013-08-14 11:32:45 -0400 | [diff] [blame] | 1079 | sizeof(*data) + data->buflen); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 1080 | if (skb) |
| 1081 | skb_queue_tail(q, skb); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1082 | kfree(data); |
| 1083 | } |
| 1084 | } |
Paul Moore | 45a0642 | 2017-05-02 10:16:05 -0400 | [diff] [blame] | 1085 | skb = audit_make_reply(seq, AUDIT_LIST_RULES, 1, 1, NULL, 0); |
Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 1086 | if (skb) |
| 1087 | skb_queue_tail(q, skb); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1088 | } |
| 1089 | |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1090 | /* Log rule additions and removals */ |
Eric Paris | dc9eb69 | 2013-04-19 13:23:09 -0400 | [diff] [blame] | 1091 | static void audit_log_rule_change(char *action, struct audit_krule *rule, int res) |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1092 | { |
| 1093 | struct audit_buffer *ab; |
| 1094 | |
Eric Paris | 1a6b9f2 | 2008-01-07 17:09:31 -0500 | [diff] [blame] | 1095 | if (!audit_enabled) |
| 1096 | return; |
| 1097 | |
Richard Guy Briggs | 626abcd | 2019-01-18 17:42:48 -0500 | [diff] [blame] | 1098 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1099 | if (!ab) |
| 1100 | return; |
Richard Guy Briggs | 5c5b8d8 | 2018-05-17 22:01:48 -0400 | [diff] [blame] | 1101 | audit_log_session_info(ab); |
Eric Paris | b122c37 | 2013-04-19 15:00:33 -0400 | [diff] [blame] | 1102 | audit_log_task_context(ab); |
Steve Grubb | c1e8f06 | 2016-11-16 16:14:33 -0500 | [diff] [blame] | 1103 | audit_log_format(ab, " op=%s", action); |
Eric Paris | 9d96098 | 2009-06-11 14:31:37 -0400 | [diff] [blame] | 1104 | audit_log_key(ab, rule->filterkey); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1105 | audit_log_format(ab, " list=%d res=%d", rule->listnr, res); |
| 1106 | audit_log_end(ab); |
| 1107 | } |
| 1108 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1109 | /** |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1110 | * audit_rule_change - apply all rules to the specified message type |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1111 | * @type: audit message type |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1112 | * @seq: netlink audit message sequence (serial) number |
| 1113 | * @data: payload data |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1114 | * @datasz: size of payload data |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1115 | */ |
Paul Moore | 45a0642 | 2017-05-02 10:16:05 -0400 | [diff] [blame] | 1116 | int audit_rule_change(int type, int seq, void *data, size_t datasz) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1117 | { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1118 | int err = 0; |
| 1119 | struct audit_entry *entry; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1120 | |
| 1121 | switch (type) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1122 | case AUDIT_ADD_RULE: |
Wenwen Wang | 70c4cf1 | 2019-04-19 20:49:29 -0500 | [diff] [blame] | 1123 | entry = audit_data_to_entry(data, datasz); |
| 1124 | if (IS_ERR(entry)) |
| 1125 | return PTR_ERR(entry); |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 1126 | err = audit_add_rule(entry); |
Burn Alting | e7df61f | 2014-04-04 16:00:38 +1100 | [diff] [blame] | 1127 | audit_log_rule_change("add_rule", &entry->rule, !err); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1128 | break; |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 1129 | case AUDIT_DEL_RULE: |
Wenwen Wang | 70c4cf1 | 2019-04-19 20:49:29 -0500 | [diff] [blame] | 1130 | entry = audit_data_to_entry(data, datasz); |
| 1131 | if (IS_ERR(entry)) |
| 1132 | return PTR_ERR(entry); |
Al Viro | 36c4f1b1 | 2008-12-15 01:50:28 -0500 | [diff] [blame] | 1133 | err = audit_del_rule(entry); |
Burn Alting | e7df61f | 2014-04-04 16:00:38 +1100 | [diff] [blame] | 1134 | audit_log_rule_change("remove_rule", &entry->rule, !err); |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1135 | break; |
| 1136 | default: |
Eric Paris | 739c950 | 2014-10-10 15:05:21 -0400 | [diff] [blame] | 1137 | WARN_ON(1); |
Wenwen Wang | 70c4cf1 | 2019-04-19 20:49:29 -0500 | [diff] [blame] | 1138 | return -EINVAL; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 1141 | if (err || type == AUDIT_DEL_RULE) { |
| 1142 | if (entry->rule.exe) |
| 1143 | audit_remove_mark(entry->rule.exe); |
Richard Guy Briggs | e85322d | 2014-10-02 22:05:19 -0400 | [diff] [blame] | 1144 | audit_free_rule(entry); |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 1145 | } |
Richard Guy Briggs | e85322d | 2014-10-02 22:05:19 -0400 | [diff] [blame] | 1146 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1147 | return err; |
| 1148 | } |
| 1149 | |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1150 | /** |
| 1151 | * audit_list_rules_send - list the audit rules |
Eric W. Biederman | d211f177 | 2014-03-08 15:31:54 -0800 | [diff] [blame] | 1152 | * @request_skb: skb of request we are replying to (used to target the reply) |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1153 | * @seq: netlink audit message sequence (serial) number |
| 1154 | */ |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1155 | int audit_list_rules_send(struct sk_buff *request_skb, int seq) |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1156 | { |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1157 | u32 portid = NETLINK_CB(request_skb).portid; |
| 1158 | struct net *net = sock_net(NETLINK_CB(request_skb).sk); |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1159 | struct task_struct *tsk; |
| 1160 | struct audit_netlink_list *dest; |
| 1161 | int err = 0; |
| 1162 | |
| 1163 | /* We can't just spew out the rules here because we might fill |
| 1164 | * the available socket buffer space and deadlock waiting for |
| 1165 | * auditctl to read from it... which isn't ever going to |
| 1166 | * happen if we're actually running in the context of auditctl |
| 1167 | * trying to _send_ the stuff */ |
| 1168 | |
| 1169 | dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL); |
| 1170 | if (!dest) |
| 1171 | return -ENOMEM; |
Eric W. Biederman | 6f285b1 | 2014-02-28 19:44:55 -0800 | [diff] [blame] | 1172 | dest->net = get_net(net); |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1173 | dest->portid = portid; |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1174 | skb_queue_head_init(&dest->q); |
| 1175 | |
| 1176 | mutex_lock(&audit_filter_mutex); |
Paul Moore | 45a0642 | 2017-05-02 10:16:05 -0400 | [diff] [blame] | 1177 | audit_list_rules(seq, &dest->q); |
Richard Guy Briggs | ce0d9f0 | 2013-11-20 14:01:53 -0500 | [diff] [blame] | 1178 | mutex_unlock(&audit_filter_mutex); |
| 1179 | |
| 1180 | tsk = kthread_run(audit_send_list, dest, "audit_send_list"); |
| 1181 | if (IS_ERR(tsk)) { |
| 1182 | skb_queue_purge(&dest->q); |
| 1183 | kfree(dest); |
| 1184 | err = PTR_ERR(tsk); |
| 1185 | } |
| 1186 | |
| 1187 | return err; |
| 1188 | } |
| 1189 | |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1190 | int audit_comparator(u32 left, u32 op, u32 right) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1191 | { |
| 1192 | switch (op) { |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1193 | case Audit_equal: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1194 | return (left == right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1195 | case Audit_not_equal: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1196 | return (left != right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1197 | case Audit_lt: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1198 | return (left < right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1199 | case Audit_le: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1200 | return (left <= right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1201 | case Audit_gt: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1202 | return (left > right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1203 | case Audit_ge: |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1204 | return (left >= right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1205 | case Audit_bitmask: |
Eric Paris | 74f2345 | 2007-06-04 17:00:14 -0400 | [diff] [blame] | 1206 | return (left & right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1207 | case Audit_bittest: |
Eric Paris | 74f2345 | 2007-06-04 17:00:14 -0400 | [diff] [blame] | 1208 | return ((left & right) == right); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1209 | default: |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 1210 | return 0; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 1214 | int audit_uid_comparator(kuid_t left, u32 op, kuid_t right) |
| 1215 | { |
| 1216 | switch (op) { |
| 1217 | case Audit_equal: |
| 1218 | return uid_eq(left, right); |
| 1219 | case Audit_not_equal: |
| 1220 | return !uid_eq(left, right); |
| 1221 | case Audit_lt: |
| 1222 | return uid_lt(left, right); |
| 1223 | case Audit_le: |
| 1224 | return uid_lte(left, right); |
| 1225 | case Audit_gt: |
| 1226 | return uid_gt(left, right); |
| 1227 | case Audit_ge: |
| 1228 | return uid_gte(left, right); |
| 1229 | case Audit_bitmask: |
| 1230 | case Audit_bittest: |
| 1231 | default: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 1232 | return 0; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | int audit_gid_comparator(kgid_t left, u32 op, kgid_t right) |
| 1237 | { |
| 1238 | switch (op) { |
| 1239 | case Audit_equal: |
| 1240 | return gid_eq(left, right); |
| 1241 | case Audit_not_equal: |
| 1242 | return !gid_eq(left, right); |
| 1243 | case Audit_lt: |
| 1244 | return gid_lt(left, right); |
| 1245 | case Audit_le: |
| 1246 | return gid_lte(left, right); |
| 1247 | case Audit_gt: |
| 1248 | return gid_gt(left, right); |
| 1249 | case Audit_ge: |
| 1250 | return gid_gte(left, right); |
| 1251 | case Audit_bitmask: |
| 1252 | case Audit_bittest: |
| 1253 | default: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1258 | /** |
| 1259 | * parent_len - find the length of the parent portion of a pathname |
| 1260 | * @path: pathname of which to determine length |
| 1261 | */ |
| 1262 | int parent_len(const char *path) |
| 1263 | { |
| 1264 | int plen; |
| 1265 | const char *p; |
| 1266 | |
| 1267 | plen = strlen(path); |
| 1268 | |
| 1269 | if (plen == 0) |
| 1270 | return plen; |
| 1271 | |
| 1272 | /* disregard trailing slashes */ |
| 1273 | p = path + plen - 1; |
| 1274 | while ((*p == '/') && (p > path)) |
| 1275 | p--; |
| 1276 | |
| 1277 | /* walk backward until we find the next slash or hit beginning */ |
| 1278 | while ((*p != '/') && (p > path)) |
| 1279 | p--; |
| 1280 | |
| 1281 | /* did we find a slash? Then increment to include it in path */ |
| 1282 | if (*p == '/') |
| 1283 | p++; |
| 1284 | |
| 1285 | return p - path; |
| 1286 | } |
| 1287 | |
Jeff Layton | e3d6b07 | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1288 | /** |
| 1289 | * audit_compare_dname_path - compare given dentry name with last component in |
| 1290 | * given path. Return of 0 indicates a match. |
| 1291 | * @dname: dentry name that we're comparing |
| 1292 | * @path: full pathname that we're comparing |
| 1293 | * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL |
| 1294 | * here indicates that we must compute this value. |
| 1295 | */ |
Al Viro | 795d673 | 2019-04-26 14:11:05 -0400 | [diff] [blame] | 1296 | int audit_compare_dname_path(const struct qstr *dname, const char *path, int parentlen) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1297 | { |
Jeff Layton | e3d6b07 | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1298 | int dlen, pathlen; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1299 | const char *p; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1300 | |
Al Viro | 795d673 | 2019-04-26 14:11:05 -0400 | [diff] [blame] | 1301 | dlen = dname->len; |
Eric Paris | 29e9a34 | 2012-10-10 15:25:24 -0400 | [diff] [blame] | 1302 | pathlen = strlen(path); |
| 1303 | if (pathlen < dlen) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1304 | return 1; |
| 1305 | |
Jeff Layton | e3d6b07 | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1306 | parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen; |
Eric Paris | 29e9a34 | 2012-10-10 15:25:24 -0400 | [diff] [blame] | 1307 | if (pathlen - parentlen != dlen) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1308 | return 1; |
Eric Paris | 29e9a34 | 2012-10-10 15:25:24 -0400 | [diff] [blame] | 1309 | |
| 1310 | p = path + parentlen; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1311 | |
Al Viro | 795d673 | 2019-04-26 14:11:05 -0400 | [diff] [blame] | 1312 | return strncmp(p, dname->name, dlen); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1313 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1314 | |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1315 | int audit_filter(int msgtype, unsigned int listtype) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1316 | { |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1317 | struct audit_entry *e; |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1318 | int ret = 1; /* Audit by default */ |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1319 | |
| 1320 | rcu_read_lock(); |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1321 | list_for_each_entry_rcu(e, &audit_filter_list[listtype], list) { |
| 1322 | int i, result = 0; |
| 1323 | |
| 1324 | for (i = 0; i < e->rule.field_count; i++) { |
| 1325 | struct audit_field *f = &e->rule.fields[i]; |
| 1326 | pid_t pid; |
| 1327 | u32 sid; |
| 1328 | |
| 1329 | switch (f->type) { |
| 1330 | case AUDIT_PID: |
| 1331 | pid = task_pid_nr(current); |
| 1332 | result = audit_comparator(pid, f->op, f->val); |
| 1333 | break; |
| 1334 | case AUDIT_UID: |
| 1335 | result = audit_uid_comparator(current_uid(), f->op, f->uid); |
| 1336 | break; |
| 1337 | case AUDIT_GID: |
| 1338 | result = audit_gid_comparator(current_gid(), f->op, f->gid); |
| 1339 | break; |
| 1340 | case AUDIT_LOGINUID: |
| 1341 | result = audit_uid_comparator(audit_get_loginuid(current), |
| 1342 | f->op, f->uid); |
| 1343 | break; |
| 1344 | case AUDIT_LOGINUID_SET: |
| 1345 | result = audit_comparator(audit_loginuid_set(current), |
| 1346 | f->op, f->val); |
| 1347 | break; |
| 1348 | case AUDIT_MSGTYPE: |
| 1349 | result = audit_comparator(msgtype, f->op, f->val); |
| 1350 | break; |
| 1351 | case AUDIT_SUBJ_USER: |
| 1352 | case AUDIT_SUBJ_ROLE: |
| 1353 | case AUDIT_SUBJ_TYPE: |
| 1354 | case AUDIT_SUBJ_SEN: |
| 1355 | case AUDIT_SUBJ_CLR: |
| 1356 | if (f->lsm_rule) { |
| 1357 | security_task_getsecid(current, &sid); |
| 1358 | result = security_audit_rule_match(sid, |
Richard Guy Briggs | 90462a5 | 2019-01-31 11:52:11 -0500 | [diff] [blame] | 1359 | f->type, f->op, f->lsm_rule); |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1360 | } |
| 1361 | break; |
Ondrej Mosnáček | 29c1372 | 2018-05-30 10:45:24 +0200 | [diff] [blame] | 1362 | case AUDIT_EXE: |
| 1363 | result = audit_exe_compare(current, e->rule.exe); |
| 1364 | if (f->op == Audit_not_equal) |
| 1365 | result = !result; |
| 1366 | break; |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1367 | default: |
| 1368 | goto unlock_and_return; |
| 1369 | } |
| 1370 | if (result < 0) /* error */ |
| 1371 | goto unlock_and_return; |
| 1372 | if (!result) |
| 1373 | break; |
| 1374 | } |
| 1375 | if (result > 0) { |
Richard Guy Briggs | d904ac0 | 2018-06-05 11:45:07 -0400 | [diff] [blame] | 1376 | if (e->rule.action == AUDIT_NEVER || listtype == AUDIT_FILTER_EXCLUDE) |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1377 | ret = 0; |
| 1378 | break; |
| 1379 | } |
| 1380 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1381 | unlock_and_return: |
| 1382 | rcu_read_unlock(); |
Richard Guy Briggs | 86b2efb | 2016-06-24 16:35:46 -0400 | [diff] [blame] | 1383 | return ret; |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 1384 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1385 | |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1386 | static int update_lsm_rule(struct audit_krule *r) |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1387 | { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1388 | struct audit_entry *entry = container_of(r, struct audit_entry, rule); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1389 | struct audit_entry *nentry; |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1390 | int err = 0; |
| 1391 | |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1392 | if (!security_audit_rule_known(r)) |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1393 | return 0; |
| 1394 | |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 1395 | nentry = audit_dupe_rule(r); |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 1396 | if (entry->rule.exe) |
| 1397 | audit_remove_mark(entry->rule.exe); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1398 | if (IS_ERR(nentry)) { |
| 1399 | /* save the first error encountered for the |
| 1400 | * return value */ |
| 1401 | err = PTR_ERR(nentry); |
| 1402 | audit_panic("error updating LSM filters"); |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 1403 | if (r->watch) |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1404 | list_del(&r->rlist); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1405 | list_del_rcu(&entry->list); |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1406 | list_del(&r->list); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1407 | } else { |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 1408 | if (r->watch || r->tree) |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1409 | list_replace_init(&r->rlist, &nentry->rule.rlist); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1410 | list_replace_rcu(&entry->list, &nentry->list); |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1411 | list_replace(&r->list, &nentry->rule.list); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1412 | } |
| 1413 | call_rcu(&entry->rcu, audit_free_rule_rcu); |
| 1414 | |
| 1415 | return err; |
| 1416 | } |
| 1417 | |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 1418 | /* This function will re-initialize the lsm_rule field of all applicable rules. |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 1419 | * It will traverse the filter lists serarching for rules that contain LSM |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1420 | * specific filter fields. When such a rule is found, it is copied, the |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 1421 | * LSM field is re-initialized, and the old rule is replaced with the |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1422 | * updated rule. */ |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 1423 | int audit_update_lsm_rules(void) |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1424 | { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1425 | struct audit_krule *r, *n; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1426 | int i, err = 0; |
| 1427 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1428 | /* audit_filter_mutex synchronizes the writers */ |
| 1429 | mutex_lock(&audit_filter_mutex); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1430 | |
| 1431 | for (i = 0; i < AUDIT_NR_FILTERS; i++) { |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 1432 | list_for_each_entry_safe(r, n, &audit_rules_list[i], list) { |
| 1433 | int res = update_lsm_rule(r); |
Al Viro | 1a9d0797 | 2008-12-14 12:04:02 -0500 | [diff] [blame] | 1434 | if (!err) |
| 1435 | err = res; |
| 1436 | } |
| 1437 | } |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1438 | mutex_unlock(&audit_filter_mutex); |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 1439 | |
| 1440 | return err; |
| 1441 | } |