ia64/linux-2.6.18-xen.hg

annotate kernel/auditfilter.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

Signed-off-by: Wei Gang <gang.wei@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 02 10:53:59 2009 +0000 (2009-03-02)
parents 831230e53067
children
rev   line source
ian@0 1 /* auditfilter.c -- filtering of audit events
ian@0 2 *
ian@0 3 * Copyright 2003-2004 Red Hat, Inc.
ian@0 4 * Copyright 2005 Hewlett-Packard Development Company, L.P.
ian@0 5 * Copyright 2005 IBM Corporation
ian@0 6 *
ian@0 7 * This program is free software; you can redistribute it and/or modify
ian@0 8 * it under the terms of the GNU General Public License as published by
ian@0 9 * the Free Software Foundation; either version 2 of the License, or
ian@0 10 * (at your option) any later version.
ian@0 11 *
ian@0 12 * This program is distributed in the hope that it will be useful,
ian@0 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ian@0 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ian@0 15 * GNU General Public License for more details.
ian@0 16 *
ian@0 17 * You should have received a copy of the GNU General Public License
ian@0 18 * along with this program; if not, write to the Free Software
ian@0 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
ian@0 20 */
ian@0 21
ian@0 22 #include <linux/kernel.h>
ian@0 23 #include <linux/audit.h>
ian@0 24 #include <linux/kthread.h>
ian@0 25 #include <linux/mutex.h>
ian@0 26 #include <linux/fs.h>
ian@0 27 #include <linux/namei.h>
ian@0 28 #include <linux/netlink.h>
ian@0 29 #include <linux/sched.h>
ian@0 30 #include <linux/inotify.h>
ian@0 31 #include <linux/selinux.h>
ian@0 32 #include "audit.h"
ian@0 33
ian@0 34 /*
ian@0 35 * Locking model:
ian@0 36 *
ian@0 37 * audit_filter_mutex:
ian@0 38 * Synchronizes writes and blocking reads of audit's filterlist
ian@0 39 * data. Rcu is used to traverse the filterlist and access
ian@0 40 * contents of structs audit_entry, audit_watch and opaque
ian@0 41 * selinux rules during filtering. If modified, these structures
ian@0 42 * must be copied and replace their counterparts in the filterlist.
ian@0 43 * An audit_parent struct is not accessed during filtering, so may
ian@0 44 * be written directly provided audit_filter_mutex is held.
ian@0 45 */
ian@0 46
ian@0 47 /*
ian@0 48 * Reference counting:
ian@0 49 *
ian@0 50 * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
ian@0 51 * event. Each audit_watch holds a reference to its associated parent.
ian@0 52 *
ian@0 53 * audit_watch: if added to lists, lifetime is from audit_init_watch() to
ian@0 54 * audit_remove_watch(). Additionally, an audit_watch may exist
ian@0 55 * temporarily to assist in searching existing filter data. Each
ian@0 56 * audit_krule holds a reference to its associated watch.
ian@0 57 */
ian@0 58
ian@0 59 struct audit_parent {
ian@0 60 struct list_head ilist; /* entry in inotify registration list */
ian@0 61 struct list_head watches; /* associated watches */
ian@0 62 struct inotify_watch wdata; /* inotify watch data */
ian@0 63 unsigned flags; /* status flags */
ian@0 64 };
ian@0 65
ian@0 66 /*
ian@0 67 * audit_parent status flags:
ian@0 68 *
ian@0 69 * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
ian@0 70 * a filesystem event to ensure we're adding audit watches to a valid parent.
ian@0 71 * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
ian@0 72 * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
ian@0 73 * we can receive while holding nameidata.
ian@0 74 */
ian@0 75 #define AUDIT_PARENT_INVALID 0x001
ian@0 76
ian@0 77 /* Audit filter lists, defined in <linux/audit.h> */
ian@0 78 struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
ian@0 79 LIST_HEAD_INIT(audit_filter_list[0]),
ian@0 80 LIST_HEAD_INIT(audit_filter_list[1]),
ian@0 81 LIST_HEAD_INIT(audit_filter_list[2]),
ian@0 82 LIST_HEAD_INIT(audit_filter_list[3]),
ian@0 83 LIST_HEAD_INIT(audit_filter_list[4]),
ian@0 84 LIST_HEAD_INIT(audit_filter_list[5]),
ian@0 85 #if AUDIT_NR_FILTERS != 6
ian@0 86 #error Fix audit_filter_list initialiser
ian@0 87 #endif
ian@0 88 };
ian@0 89
ian@0 90 static DEFINE_MUTEX(audit_filter_mutex);
ian@0 91
ian@0 92 /* Inotify handle */
ian@0 93 extern struct inotify_handle *audit_ih;
ian@0 94
ian@0 95 /* Inotify events we care about. */
ian@0 96 #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
ian@0 97
ian@0 98 void audit_free_parent(struct inotify_watch *i_watch)
ian@0 99 {
ian@0 100 struct audit_parent *parent;
ian@0 101
ian@0 102 parent = container_of(i_watch, struct audit_parent, wdata);
ian@0 103 WARN_ON(!list_empty(&parent->watches));
ian@0 104 kfree(parent);
ian@0 105 }
ian@0 106
ian@0 107 static inline void audit_get_watch(struct audit_watch *watch)
ian@0 108 {
ian@0 109 atomic_inc(&watch->count);
ian@0 110 }
ian@0 111
ian@0 112 static void audit_put_watch(struct audit_watch *watch)
ian@0 113 {
ian@0 114 if (atomic_dec_and_test(&watch->count)) {
ian@0 115 WARN_ON(watch->parent);
ian@0 116 WARN_ON(!list_empty(&watch->rules));
ian@0 117 kfree(watch->path);
ian@0 118 kfree(watch);
ian@0 119 }
ian@0 120 }
ian@0 121
ian@0 122 static void audit_remove_watch(struct audit_watch *watch)
ian@0 123 {
ian@0 124 list_del(&watch->wlist);
ian@0 125 put_inotify_watch(&watch->parent->wdata);
ian@0 126 watch->parent = NULL;
ian@0 127 audit_put_watch(watch); /* match initial get */
ian@0 128 }
ian@0 129
ian@0 130 static inline void audit_free_rule(struct audit_entry *e)
ian@0 131 {
ian@0 132 int i;
ian@0 133
ian@0 134 /* some rules don't have associated watches */
ian@0 135 if (e->rule.watch)
ian@0 136 audit_put_watch(e->rule.watch);
ian@0 137 if (e->rule.fields)
ian@0 138 for (i = 0; i < e->rule.field_count; i++) {
ian@0 139 struct audit_field *f = &e->rule.fields[i];
ian@0 140 kfree(f->se_str);
ian@0 141 selinux_audit_rule_free(f->se_rule);
ian@0 142 }
ian@0 143 kfree(e->rule.fields);
ian@0 144 kfree(e->rule.filterkey);
ian@0 145 kfree(e);
ian@0 146 }
ian@0 147
ian@0 148 static inline void audit_free_rule_rcu(struct rcu_head *head)
ian@0 149 {
ian@0 150 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
ian@0 151 audit_free_rule(e);
ian@0 152 }
ian@0 153
ian@0 154 /* Initialize a parent watch entry. */
ian@0 155 static struct audit_parent *audit_init_parent(struct nameidata *ndp)
ian@0 156 {
ian@0 157 struct audit_parent *parent;
ian@0 158 s32 wd;
ian@0 159
ian@0 160 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
ian@0 161 if (unlikely(!parent))
ian@0 162 return ERR_PTR(-ENOMEM);
ian@0 163
ian@0 164 INIT_LIST_HEAD(&parent->watches);
ian@0 165 parent->flags = 0;
ian@0 166
ian@0 167 inotify_init_watch(&parent->wdata);
ian@0 168 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
ian@0 169 get_inotify_watch(&parent->wdata);
ian@0 170 wd = inotify_add_watch(audit_ih, &parent->wdata, ndp->dentry->d_inode,
ian@0 171 AUDIT_IN_WATCH);
ian@0 172 if (wd < 0) {
ian@0 173 audit_free_parent(&parent->wdata);
ian@0 174 return ERR_PTR(wd);
ian@0 175 }
ian@0 176
ian@0 177 return parent;
ian@0 178 }
ian@0 179
ian@0 180 /* Initialize a watch entry. */
ian@0 181 static struct audit_watch *audit_init_watch(char *path)
ian@0 182 {
ian@0 183 struct audit_watch *watch;
ian@0 184
ian@0 185 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
ian@0 186 if (unlikely(!watch))
ian@0 187 return ERR_PTR(-ENOMEM);
ian@0 188
ian@0 189 INIT_LIST_HEAD(&watch->rules);
ian@0 190 atomic_set(&watch->count, 1);
ian@0 191 watch->path = path;
ian@0 192 watch->dev = (dev_t)-1;
ian@0 193 watch->ino = (unsigned long)-1;
ian@0 194
ian@0 195 return watch;
ian@0 196 }
ian@0 197
ian@0 198 /* Initialize an audit filterlist entry. */
ian@0 199 static inline struct audit_entry *audit_init_entry(u32 field_count)
ian@0 200 {
ian@0 201 struct audit_entry *entry;
ian@0 202 struct audit_field *fields;
ian@0 203
ian@0 204 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
ian@0 205 if (unlikely(!entry))
ian@0 206 return NULL;
ian@0 207
ian@0 208 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
ian@0 209 if (unlikely(!fields)) {
ian@0 210 kfree(entry);
ian@0 211 return NULL;
ian@0 212 }
ian@0 213 entry->rule.fields = fields;
ian@0 214
ian@0 215 return entry;
ian@0 216 }
ian@0 217
ian@0 218 /* Unpack a filter field's string representation from user-space
ian@0 219 * buffer. */
ian@0 220 static char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
ian@0 221 {
ian@0 222 char *str;
ian@0 223
ian@0 224 if (!*bufp || (len == 0) || (len > *remain))
ian@0 225 return ERR_PTR(-EINVAL);
ian@0 226
ian@0 227 /* Of the currently implemented string fields, PATH_MAX
ian@0 228 * defines the longest valid length.
ian@0 229 */
ian@0 230 if (len > PATH_MAX)
ian@0 231 return ERR_PTR(-ENAMETOOLONG);
ian@0 232
ian@0 233 str = kmalloc(len + 1, GFP_KERNEL);
ian@0 234 if (unlikely(!str))
ian@0 235 return ERR_PTR(-ENOMEM);
ian@0 236
ian@0 237 memcpy(str, *bufp, len);
ian@0 238 str[len] = 0;
ian@0 239 *bufp += len;
ian@0 240 *remain -= len;
ian@0 241
ian@0 242 return str;
ian@0 243 }
ian@0 244
ian@0 245 /* Translate an inode field to kernel respresentation. */
ian@0 246 static inline int audit_to_inode(struct audit_krule *krule,
ian@0 247 struct audit_field *f)
ian@0 248 {
ian@0 249 if (krule->listnr != AUDIT_FILTER_EXIT ||
ian@0 250 krule->watch || krule->inode_f)
ian@0 251 return -EINVAL;
ian@0 252
ian@0 253 krule->inode_f = f;
ian@0 254 return 0;
ian@0 255 }
ian@0 256
ian@0 257 /* Translate a watch string to kernel respresentation. */
ian@0 258 static int audit_to_watch(struct audit_krule *krule, char *path, int len,
ian@0 259 u32 op)
ian@0 260 {
ian@0 261 struct audit_watch *watch;
ian@0 262
ian@0 263 if (!audit_ih)
ian@0 264 return -EOPNOTSUPP;
ian@0 265
ian@0 266 if (path[0] != '/' || path[len-1] == '/' ||
ian@0 267 krule->listnr != AUDIT_FILTER_EXIT ||
ian@0 268 op & ~AUDIT_EQUAL ||
ian@0 269 krule->inode_f || krule->watch) /* 1 inode # per rule, for hash */
ian@0 270 return -EINVAL;
ian@0 271
ian@0 272 watch = audit_init_watch(path);
ian@0 273 if (unlikely(IS_ERR(watch)))
ian@0 274 return PTR_ERR(watch);
ian@0 275
ian@0 276 audit_get_watch(watch);
ian@0 277 krule->watch = watch;
ian@0 278
ian@0 279 return 0;
ian@0 280 }
ian@0 281
ian@0 282 static __u32 *classes[AUDIT_SYSCALL_CLASSES];
ian@0 283
ian@0 284 int __init audit_register_class(int class, unsigned *list)
ian@0 285 {
ian@0 286 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
ian@0 287 if (!p)
ian@0 288 return -ENOMEM;
ian@0 289 while (*list != ~0U) {
ian@0 290 unsigned n = *list++;
ian@0 291 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
ian@0 292 kfree(p);
ian@0 293 return -EINVAL;
ian@0 294 }
ian@0 295 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
ian@0 296 }
ian@0 297 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
ian@0 298 kfree(p);
ian@0 299 return -EINVAL;
ian@0 300 }
ian@0 301 classes[class] = p;
ian@0 302 return 0;
ian@0 303 }
ian@0 304
ian@0 305 int audit_match_class(int class, unsigned syscall)
ian@0 306 {
ian@0 307 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * sizeof(__u32)))
ian@0 308 return 0;
ian@0 309 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
ian@0 310 return 0;
ian@0 311 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
ian@0 312 }
ian@0 313
ian@0 314 /* Common user-space to kernel rule translation. */
ian@0 315 static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
ian@0 316 {
ian@0 317 unsigned listnr;
ian@0 318 struct audit_entry *entry;
ian@0 319 int i, err;
ian@0 320
ian@0 321 err = -EINVAL;
ian@0 322 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
ian@0 323 switch(listnr) {
ian@0 324 default:
ian@0 325 goto exit_err;
ian@0 326 case AUDIT_FILTER_USER:
ian@0 327 case AUDIT_FILTER_TYPE:
ian@0 328 #ifdef CONFIG_AUDITSYSCALL
ian@0 329 case AUDIT_FILTER_ENTRY:
ian@0 330 case AUDIT_FILTER_EXIT:
ian@0 331 case AUDIT_FILTER_TASK:
ian@0 332 #endif
ian@0 333 ;
ian@0 334 }
ian@0 335 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
ian@0 336 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
ian@0 337 goto exit_err;
ian@0 338 }
ian@0 339 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
ian@0 340 goto exit_err;
ian@0 341 if (rule->field_count > AUDIT_MAX_FIELDS)
ian@0 342 goto exit_err;
ian@0 343
ian@0 344 err = -ENOMEM;
ian@0 345 entry = audit_init_entry(rule->field_count);
ian@0 346 if (!entry)
ian@0 347 goto exit_err;
ian@0 348
ian@0 349 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
ian@0 350 entry->rule.listnr = listnr;
ian@0 351 entry->rule.action = rule->action;
ian@0 352 entry->rule.field_count = rule->field_count;
ian@0 353
ian@0 354 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
ian@0 355 entry->rule.mask[i] = rule->mask[i];
ian@0 356
ian@0 357 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
ian@0 358 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
ian@0 359 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
ian@0 360 __u32 *class;
ian@0 361
ian@0 362 if (!(*p & AUDIT_BIT(bit)))
ian@0 363 continue;
ian@0 364 *p &= ~AUDIT_BIT(bit);
ian@0 365 class = classes[i];
ian@0 366 if (class) {
ian@0 367 int j;
ian@0 368 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
ian@0 369 entry->rule.mask[j] |= class[j];
ian@0 370 }
ian@0 371 }
ian@0 372
ian@0 373 return entry;
ian@0 374
ian@0 375 exit_err:
ian@0 376 return ERR_PTR(err);
ian@0 377 }
ian@0 378
ian@0 379 /* Translate struct audit_rule to kernel's rule respresentation.
ian@0 380 * Exists for backward compatibility with userspace. */
ian@0 381 static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
ian@0 382 {
ian@0 383 struct audit_entry *entry;
ian@0 384 struct audit_field *f;
ian@0 385 int err = 0;
ian@0 386 int i;
ian@0 387
ian@0 388 entry = audit_to_entry_common(rule);
ian@0 389 if (IS_ERR(entry))
ian@0 390 goto exit_nofree;
ian@0 391
ian@0 392 for (i = 0; i < rule->field_count; i++) {
ian@0 393 struct audit_field *f = &entry->rule.fields[i];
ian@0 394
ian@0 395 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
ian@0 396 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
ian@0 397 f->val = rule->values[i];
ian@0 398
ian@0 399 err = -EINVAL;
ian@0 400 switch(f->type) {
ian@0 401 default:
ian@0 402 goto exit_free;
ian@0 403 case AUDIT_PID:
ian@0 404 case AUDIT_UID:
ian@0 405 case AUDIT_EUID:
ian@0 406 case AUDIT_SUID:
ian@0 407 case AUDIT_FSUID:
ian@0 408 case AUDIT_GID:
ian@0 409 case AUDIT_EGID:
ian@0 410 case AUDIT_SGID:
ian@0 411 case AUDIT_FSGID:
ian@0 412 case AUDIT_LOGINUID:
ian@0 413 case AUDIT_PERS:
ian@0 414 case AUDIT_ARCH:
ian@0 415 case AUDIT_MSGTYPE:
ian@0 416 case AUDIT_PPID:
ian@0 417 case AUDIT_DEVMAJOR:
ian@0 418 case AUDIT_DEVMINOR:
ian@0 419 case AUDIT_EXIT:
ian@0 420 case AUDIT_SUCCESS:
ian@0 421 case AUDIT_ARG0:
ian@0 422 case AUDIT_ARG1:
ian@0 423 case AUDIT_ARG2:
ian@0 424 case AUDIT_ARG3:
ian@0 425 break;
ian@0 426 case AUDIT_PERM:
ian@0 427 if (f->val & ~15)
ian@0 428 goto exit_free;
ian@0 429 break;
ian@0 430 case AUDIT_INODE:
ian@0 431 err = audit_to_inode(&entry->rule, f);
ian@0 432 if (err)
ian@0 433 goto exit_free;
ian@0 434 break;
ian@0 435 }
ian@0 436
ian@0 437 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
ian@0 438
ian@0 439 /* Support for legacy operators where
ian@0 440 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
ian@0 441 if (f->op & AUDIT_NEGATE)
ian@0 442 f->op = AUDIT_NOT_EQUAL;
ian@0 443 else if (!f->op)
ian@0 444 f->op = AUDIT_EQUAL;
ian@0 445 else if (f->op == AUDIT_OPERATORS) {
ian@0 446 err = -EINVAL;
ian@0 447 goto exit_free;
ian@0 448 }
ian@0 449 }
ian@0 450
ian@0 451 f = entry->rule.inode_f;
ian@0 452 if (f) {
ian@0 453 switch(f->op) {
ian@0 454 case AUDIT_NOT_EQUAL:
ian@0 455 entry->rule.inode_f = NULL;
ian@0 456 case AUDIT_EQUAL:
ian@0 457 break;
ian@0 458 default:
ian@0 459 err = -EINVAL;
ian@0 460 goto exit_free;
ian@0 461 }
ian@0 462 }
ian@0 463
ian@0 464 exit_nofree:
ian@0 465 return entry;
ian@0 466
ian@0 467 exit_free:
ian@0 468 audit_free_rule(entry);
ian@0 469 return ERR_PTR(err);
ian@0 470 }
ian@0 471
ian@0 472 /* Translate struct audit_rule_data to kernel's rule respresentation. */
ian@0 473 static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
ian@0 474 size_t datasz)
ian@0 475 {
ian@0 476 int err = 0;
ian@0 477 struct audit_entry *entry;
ian@0 478 struct audit_field *f;
ian@0 479 void *bufp;
ian@0 480 size_t remain = datasz - sizeof(struct audit_rule_data);
ian@0 481 int i;
ian@0 482 char *str;
ian@0 483
ian@0 484 entry = audit_to_entry_common((struct audit_rule *)data);
ian@0 485 if (IS_ERR(entry))
ian@0 486 goto exit_nofree;
ian@0 487
ian@0 488 bufp = data->buf;
ian@0 489 entry->rule.vers_ops = 2;
ian@0 490 for (i = 0; i < data->field_count; i++) {
ian@0 491 struct audit_field *f = &entry->rule.fields[i];
ian@0 492
ian@0 493 err = -EINVAL;
ian@0 494 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
ian@0 495 data->fieldflags[i] & ~AUDIT_OPERATORS)
ian@0 496 goto exit_free;
ian@0 497
ian@0 498 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
ian@0 499 f->type = data->fields[i];
ian@0 500 f->val = data->values[i];
ian@0 501 f->se_str = NULL;
ian@0 502 f->se_rule = NULL;
ian@0 503 switch(f->type) {
ian@0 504 case AUDIT_PID:
ian@0 505 case AUDIT_UID:
ian@0 506 case AUDIT_EUID:
ian@0 507 case AUDIT_SUID:
ian@0 508 case AUDIT_FSUID:
ian@0 509 case AUDIT_GID:
ian@0 510 case AUDIT_EGID:
ian@0 511 case AUDIT_SGID:
ian@0 512 case AUDIT_FSGID:
ian@0 513 case AUDIT_LOGINUID:
ian@0 514 case AUDIT_PERS:
ian@0 515 case AUDIT_ARCH:
ian@0 516 case AUDIT_MSGTYPE:
ian@0 517 case AUDIT_PPID:
ian@0 518 case AUDIT_DEVMAJOR:
ian@0 519 case AUDIT_DEVMINOR:
ian@0 520 case AUDIT_EXIT:
ian@0 521 case AUDIT_SUCCESS:
ian@0 522 case AUDIT_ARG0:
ian@0 523 case AUDIT_ARG1:
ian@0 524 case AUDIT_ARG2:
ian@0 525 case AUDIT_ARG3:
ian@0 526 break;
ian@0 527 case AUDIT_SUBJ_USER:
ian@0 528 case AUDIT_SUBJ_ROLE:
ian@0 529 case AUDIT_SUBJ_TYPE:
ian@0 530 case AUDIT_SUBJ_SEN:
ian@0 531 case AUDIT_SUBJ_CLR:
ian@0 532 case AUDIT_OBJ_USER:
ian@0 533 case AUDIT_OBJ_ROLE:
ian@0 534 case AUDIT_OBJ_TYPE:
ian@0 535 case AUDIT_OBJ_LEV_LOW:
ian@0 536 case AUDIT_OBJ_LEV_HIGH:
ian@0 537 str = audit_unpack_string(&bufp, &remain, f->val);
ian@0 538 if (IS_ERR(str))
ian@0 539 goto exit_free;
ian@0 540 entry->rule.buflen += f->val;
ian@0 541
ian@0 542 err = selinux_audit_rule_init(f->type, f->op, str,
ian@0 543 &f->se_rule);
ian@0 544 /* Keep currently invalid fields around in case they
ian@0 545 * become valid after a policy reload. */
ian@0 546 if (err == -EINVAL) {
ian@0 547 printk(KERN_WARNING "audit rule for selinux "
ian@0 548 "\'%s\' is invalid\n", str);
ian@0 549 err = 0;
ian@0 550 }
ian@0 551 if (err) {
ian@0 552 kfree(str);
ian@0 553 goto exit_free;
ian@0 554 } else
ian@0 555 f->se_str = str;
ian@0 556 break;
ian@0 557 case AUDIT_WATCH:
ian@0 558 str = audit_unpack_string(&bufp, &remain, f->val);
ian@0 559 if (IS_ERR(str))
ian@0 560 goto exit_free;
ian@0 561 entry->rule.buflen += f->val;
ian@0 562
ian@0 563 err = audit_to_watch(&entry->rule, str, f->val, f->op);
ian@0 564 if (err) {
ian@0 565 kfree(str);
ian@0 566 goto exit_free;
ian@0 567 }
ian@0 568 break;
ian@0 569 case AUDIT_INODE:
ian@0 570 err = audit_to_inode(&entry->rule, f);
ian@0 571 if (err)
ian@0 572 goto exit_free;
ian@0 573 break;
ian@0 574 case AUDIT_FILTERKEY:
ian@0 575 err = -EINVAL;
ian@0 576 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
ian@0 577 goto exit_free;
ian@0 578 str = audit_unpack_string(&bufp, &remain, f->val);
ian@0 579 if (IS_ERR(str))
ian@0 580 goto exit_free;
ian@0 581 entry->rule.buflen += f->val;
ian@0 582 entry->rule.filterkey = str;
ian@0 583 break;
ian@0 584 case AUDIT_PERM:
ian@0 585 if (f->val & ~15)
ian@0 586 goto exit_free;
ian@0 587 break;
ian@0 588 default:
ian@0 589 goto exit_free;
ian@0 590 }
ian@0 591 }
ian@0 592
ian@0 593 f = entry->rule.inode_f;
ian@0 594 if (f) {
ian@0 595 switch(f->op) {
ian@0 596 case AUDIT_NOT_EQUAL:
ian@0 597 entry->rule.inode_f = NULL;
ian@0 598 case AUDIT_EQUAL:
ian@0 599 break;
ian@0 600 default:
ian@0 601 err = -EINVAL;
ian@0 602 goto exit_free;
ian@0 603 }
ian@0 604 }
ian@0 605
ian@0 606 exit_nofree:
ian@0 607 return entry;
ian@0 608
ian@0 609 exit_free:
ian@0 610 audit_free_rule(entry);
ian@0 611 return ERR_PTR(err);
ian@0 612 }
ian@0 613
ian@0 614 /* Pack a filter field's string representation into data block. */
ian@0 615 static inline size_t audit_pack_string(void **bufp, char *str)
ian@0 616 {
ian@0 617 size_t len = strlen(str);
ian@0 618
ian@0 619 memcpy(*bufp, str, len);
ian@0 620 *bufp += len;
ian@0 621
ian@0 622 return len;
ian@0 623 }
ian@0 624
ian@0 625 /* Translate kernel rule respresentation to struct audit_rule.
ian@0 626 * Exists for backward compatibility with userspace. */
ian@0 627 static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
ian@0 628 {
ian@0 629 struct audit_rule *rule;
ian@0 630 int i;
ian@0 631
ian@0 632 rule = kmalloc(sizeof(*rule), GFP_KERNEL);
ian@0 633 if (unlikely(!rule))
ian@0 634 return NULL;
ian@0 635 memset(rule, 0, sizeof(*rule));
ian@0 636
ian@0 637 rule->flags = krule->flags | krule->listnr;
ian@0 638 rule->action = krule->action;
ian@0 639 rule->field_count = krule->field_count;
ian@0 640 for (i = 0; i < rule->field_count; i++) {
ian@0 641 rule->values[i] = krule->fields[i].val;
ian@0 642 rule->fields[i] = krule->fields[i].type;
ian@0 643
ian@0 644 if (krule->vers_ops == 1) {
ian@0 645 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
ian@0 646 rule->fields[i] |= AUDIT_NEGATE;
ian@0 647 } else {
ian@0 648 rule->fields[i] |= krule->fields[i].op;
ian@0 649 }
ian@0 650 }
ian@0 651 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
ian@0 652
ian@0 653 return rule;
ian@0 654 }
ian@0 655
ian@0 656 /* Translate kernel rule respresentation to struct audit_rule_data. */
ian@0 657 static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
ian@0 658 {
ian@0 659 struct audit_rule_data *data;
ian@0 660 void *bufp;
ian@0 661 int i;
ian@0 662
ian@0 663 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
ian@0 664 if (unlikely(!data))
ian@0 665 return NULL;
ian@0 666 memset(data, 0, sizeof(*data));
ian@0 667
ian@0 668 data->flags = krule->flags | krule->listnr;
ian@0 669 data->action = krule->action;
ian@0 670 data->field_count = krule->field_count;
ian@0 671 bufp = data->buf;
ian@0 672 for (i = 0; i < data->field_count; i++) {
ian@0 673 struct audit_field *f = &krule->fields[i];
ian@0 674
ian@0 675 data->fields[i] = f->type;
ian@0 676 data->fieldflags[i] = f->op;
ian@0 677 switch(f->type) {
ian@0 678 case AUDIT_SUBJ_USER:
ian@0 679 case AUDIT_SUBJ_ROLE:
ian@0 680 case AUDIT_SUBJ_TYPE:
ian@0 681 case AUDIT_SUBJ_SEN:
ian@0 682 case AUDIT_SUBJ_CLR:
ian@0 683 case AUDIT_OBJ_USER:
ian@0 684 case AUDIT_OBJ_ROLE:
ian@0 685 case AUDIT_OBJ_TYPE:
ian@0 686 case AUDIT_OBJ_LEV_LOW:
ian@0 687 case AUDIT_OBJ_LEV_HIGH:
ian@0 688 data->buflen += data->values[i] =
ian@0 689 audit_pack_string(&bufp, f->se_str);
ian@0 690 break;
ian@0 691 case AUDIT_WATCH:
ian@0 692 data->buflen += data->values[i] =
ian@0 693 audit_pack_string(&bufp, krule->watch->path);
ian@0 694 break;
ian@0 695 case AUDIT_FILTERKEY:
ian@0 696 data->buflen += data->values[i] =
ian@0 697 audit_pack_string(&bufp, krule->filterkey);
ian@0 698 break;
ian@0 699 default:
ian@0 700 data->values[i] = f->val;
ian@0 701 }
ian@0 702 }
ian@0 703 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
ian@0 704
ian@0 705 return data;
ian@0 706 }
ian@0 707
ian@0 708 /* Compare two rules in kernel format. Considered success if rules
ian@0 709 * don't match. */
ian@0 710 static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
ian@0 711 {
ian@0 712 int i;
ian@0 713
ian@0 714 if (a->flags != b->flags ||
ian@0 715 a->listnr != b->listnr ||
ian@0 716 a->action != b->action ||
ian@0 717 a->field_count != b->field_count)
ian@0 718 return 1;
ian@0 719
ian@0 720 for (i = 0; i < a->field_count; i++) {
ian@0 721 if (a->fields[i].type != b->fields[i].type ||
ian@0 722 a->fields[i].op != b->fields[i].op)
ian@0 723 return 1;
ian@0 724
ian@0 725 switch(a->fields[i].type) {
ian@0 726 case AUDIT_SUBJ_USER:
ian@0 727 case AUDIT_SUBJ_ROLE:
ian@0 728 case AUDIT_SUBJ_TYPE:
ian@0 729 case AUDIT_SUBJ_SEN:
ian@0 730 case AUDIT_SUBJ_CLR:
ian@0 731 case AUDIT_OBJ_USER:
ian@0 732 case AUDIT_OBJ_ROLE:
ian@0 733 case AUDIT_OBJ_TYPE:
ian@0 734 case AUDIT_OBJ_LEV_LOW:
ian@0 735 case AUDIT_OBJ_LEV_HIGH:
ian@0 736 if (strcmp(a->fields[i].se_str, b->fields[i].se_str))
ian@0 737 return 1;
ian@0 738 break;
ian@0 739 case AUDIT_WATCH:
ian@0 740 if (strcmp(a->watch->path, b->watch->path))
ian@0 741 return 1;
ian@0 742 break;
ian@0 743 case AUDIT_FILTERKEY:
ian@0 744 /* both filterkeys exist based on above type compare */
ian@0 745 if (strcmp(a->filterkey, b->filterkey))
ian@0 746 return 1;
ian@0 747 break;
ian@0 748 default:
ian@0 749 if (a->fields[i].val != b->fields[i].val)
ian@0 750 return 1;
ian@0 751 }
ian@0 752 }
ian@0 753
ian@0 754 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
ian@0 755 if (a->mask[i] != b->mask[i])
ian@0 756 return 1;
ian@0 757
ian@0 758 return 0;
ian@0 759 }
ian@0 760
ian@0 761 /* Duplicate the given audit watch. The new watch's rules list is initialized
ian@0 762 * to an empty list and wlist is undefined. */
ian@0 763 static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
ian@0 764 {
ian@0 765 char *path;
ian@0 766 struct audit_watch *new;
ian@0 767
ian@0 768 path = kstrdup(old->path, GFP_KERNEL);
ian@0 769 if (unlikely(!path))
ian@0 770 return ERR_PTR(-ENOMEM);
ian@0 771
ian@0 772 new = audit_init_watch(path);
ian@0 773 if (unlikely(IS_ERR(new))) {
ian@0 774 kfree(path);
ian@0 775 goto out;
ian@0 776 }
ian@0 777
ian@0 778 new->dev = old->dev;
ian@0 779 new->ino = old->ino;
ian@0 780 get_inotify_watch(&old->parent->wdata);
ian@0 781 new->parent = old->parent;
ian@0 782
ian@0 783 out:
ian@0 784 return new;
ian@0 785 }
ian@0 786
ian@0 787 /* Duplicate selinux field information. The se_rule is opaque, so must be
ian@0 788 * re-initialized. */
ian@0 789 static inline int audit_dupe_selinux_field(struct audit_field *df,
ian@0 790 struct audit_field *sf)
ian@0 791 {
ian@0 792 int ret = 0;
ian@0 793 char *se_str;
ian@0 794
ian@0 795 /* our own copy of se_str */
ian@0 796 se_str = kstrdup(sf->se_str, GFP_KERNEL);
ian@0 797 if (unlikely(IS_ERR(se_str)))
ian@0 798 return -ENOMEM;
ian@0 799 df->se_str = se_str;
ian@0 800
ian@0 801 /* our own (refreshed) copy of se_rule */
ian@0 802 ret = selinux_audit_rule_init(df->type, df->op, df->se_str,
ian@0 803 &df->se_rule);
ian@0 804 /* Keep currently invalid fields around in case they
ian@0 805 * become valid after a policy reload. */
ian@0 806 if (ret == -EINVAL) {
ian@0 807 printk(KERN_WARNING "audit rule for selinux \'%s\' is "
ian@0 808 "invalid\n", df->se_str);
ian@0 809 ret = 0;
ian@0 810 }
ian@0 811
ian@0 812 return ret;
ian@0 813 }
ian@0 814
ian@0 815 /* Duplicate an audit rule. This will be a deep copy with the exception
ian@0 816 * of the watch - that pointer is carried over. The selinux specific fields
ian@0 817 * will be updated in the copy. The point is to be able to replace the old
ian@0 818 * rule with the new rule in the filterlist, then free the old rule.
ian@0 819 * The rlist element is undefined; list manipulations are handled apart from
ian@0 820 * the initial copy. */
ian@0 821 static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
ian@0 822 struct audit_watch *watch)
ian@0 823 {
ian@0 824 u32 fcount = old->field_count;
ian@0 825 struct audit_entry *entry;
ian@0 826 struct audit_krule *new;
ian@0 827 char *fk;
ian@0 828 int i, err = 0;
ian@0 829
ian@0 830 entry = audit_init_entry(fcount);
ian@0 831 if (unlikely(!entry))
ian@0 832 return ERR_PTR(-ENOMEM);
ian@0 833
ian@0 834 new = &entry->rule;
ian@0 835 new->vers_ops = old->vers_ops;
ian@0 836 new->flags = old->flags;
ian@0 837 new->listnr = old->listnr;
ian@0 838 new->action = old->action;
ian@0 839 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
ian@0 840 new->mask[i] = old->mask[i];
ian@0 841 new->buflen = old->buflen;
ian@0 842 new->inode_f = old->inode_f;
ian@0 843 new->watch = NULL;
ian@0 844 new->field_count = old->field_count;
ian@0 845 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
ian@0 846
ian@0 847 /* deep copy this information, updating the se_rule fields, because
ian@0 848 * the originals will all be freed when the old rule is freed. */
ian@0 849 for (i = 0; i < fcount; i++) {
ian@0 850 switch (new->fields[i].type) {
ian@0 851 case AUDIT_SUBJ_USER:
ian@0 852 case AUDIT_SUBJ_ROLE:
ian@0 853 case AUDIT_SUBJ_TYPE:
ian@0 854 case AUDIT_SUBJ_SEN:
ian@0 855 case AUDIT_SUBJ_CLR:
ian@0 856 case AUDIT_OBJ_USER:
ian@0 857 case AUDIT_OBJ_ROLE:
ian@0 858 case AUDIT_OBJ_TYPE:
ian@0 859 case AUDIT_OBJ_LEV_LOW:
ian@0 860 case AUDIT_OBJ_LEV_HIGH:
ian@0 861 err = audit_dupe_selinux_field(&new->fields[i],
ian@0 862 &old->fields[i]);
ian@0 863 break;
ian@0 864 case AUDIT_FILTERKEY:
ian@0 865 fk = kstrdup(old->filterkey, GFP_KERNEL);
ian@0 866 if (unlikely(!fk))
ian@0 867 err = -ENOMEM;
ian@0 868 else
ian@0 869 new->filterkey = fk;
ian@0 870 }
ian@0 871 if (err) {
ian@0 872 audit_free_rule(entry);
ian@0 873 return ERR_PTR(err);
ian@0 874 }
ian@0 875 }
ian@0 876
ian@0 877 if (watch) {
ian@0 878 audit_get_watch(watch);
ian@0 879 new->watch = watch;
ian@0 880 }
ian@0 881
ian@0 882 return entry;
ian@0 883 }
ian@0 884
ian@0 885 /* Update inode info in audit rules based on filesystem event. */
ian@0 886 static void audit_update_watch(struct audit_parent *parent,
ian@0 887 const char *dname, dev_t dev,
ian@0 888 unsigned long ino, unsigned invalidating)
ian@0 889 {
ian@0 890 struct audit_watch *owatch, *nwatch, *nextw;
ian@0 891 struct audit_krule *r, *nextr;
ian@0 892 struct audit_entry *oentry, *nentry;
ian@0 893 struct audit_buffer *ab;
ian@0 894
ian@0 895 mutex_lock(&audit_filter_mutex);
ian@0 896 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
ian@0 897 if (audit_compare_dname_path(dname, owatch->path, NULL))
ian@0 898 continue;
ian@0 899
ian@0 900 /* If the update involves invalidating rules, do the inode-based
ian@0 901 * filtering now, so we don't omit records. */
ian@0 902 if (invalidating &&
ian@0 903 audit_filter_inodes(current, current->audit_context) == AUDIT_RECORD_CONTEXT)
ian@0 904 audit_set_auditable(current->audit_context);
ian@0 905
ian@0 906 nwatch = audit_dupe_watch(owatch);
ian@0 907 if (unlikely(IS_ERR(nwatch))) {
ian@0 908 mutex_unlock(&audit_filter_mutex);
ian@0 909 audit_panic("error updating watch, skipping");
ian@0 910 return;
ian@0 911 }
ian@0 912 nwatch->dev = dev;
ian@0 913 nwatch->ino = ino;
ian@0 914
ian@0 915 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
ian@0 916
ian@0 917 oentry = container_of(r, struct audit_entry, rule);
ian@0 918 list_del(&oentry->rule.rlist);
ian@0 919 list_del_rcu(&oentry->list);
ian@0 920
ian@0 921 nentry = audit_dupe_rule(&oentry->rule, nwatch);
ian@0 922 if (unlikely(IS_ERR(nentry)))
ian@0 923 audit_panic("error updating watch, removing");
ian@0 924 else {
ian@0 925 int h = audit_hash_ino((u32)ino);
ian@0 926 list_add(&nentry->rule.rlist, &nwatch->rules);
ian@0 927 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
ian@0 928 }
ian@0 929
ian@0 930 call_rcu(&oentry->rcu, audit_free_rule_rcu);
ian@0 931 }
ian@0 932
ian@0 933 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
ian@0 934 audit_log_format(ab, "audit updated rules specifying path=");
ian@0 935 audit_log_untrustedstring(ab, owatch->path);
ian@0 936 audit_log_format(ab, " with dev=%u ino=%lu\n", dev, ino);
ian@0 937 audit_log_end(ab);
ian@0 938
ian@0 939 audit_remove_watch(owatch);
ian@0 940 goto add_watch_to_parent; /* event applies to a single watch */
ian@0 941 }
ian@0 942 mutex_unlock(&audit_filter_mutex);
ian@0 943 return;
ian@0 944
ian@0 945 add_watch_to_parent:
ian@0 946 list_add(&nwatch->wlist, &parent->watches);
ian@0 947 mutex_unlock(&audit_filter_mutex);
ian@0 948 return;
ian@0 949 }
ian@0 950
ian@0 951 /* Remove all watches & rules associated with a parent that is going away. */
ian@0 952 static void audit_remove_parent_watches(struct audit_parent *parent)
ian@0 953 {
ian@0 954 struct audit_watch *w, *nextw;
ian@0 955 struct audit_krule *r, *nextr;
ian@0 956 struct audit_entry *e;
ian@0 957 struct audit_buffer *ab;
ian@0 958
ian@0 959 mutex_lock(&audit_filter_mutex);
ian@0 960 parent->flags |= AUDIT_PARENT_INVALID;
ian@0 961 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
ian@0 962 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
ian@0 963 e = container_of(r, struct audit_entry, rule);
ian@0 964
ian@0 965 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
ian@0 966 audit_log_format(ab, "audit implicitly removed rule path=");
ian@0 967 audit_log_untrustedstring(ab, w->path);
ian@0 968 if (r->filterkey) {
ian@0 969 audit_log_format(ab, " key=");
ian@0 970 audit_log_untrustedstring(ab, r->filterkey);
ian@0 971 } else
ian@0 972 audit_log_format(ab, " key=(null)");
ian@0 973 audit_log_format(ab, " list=%d", r->listnr);
ian@0 974 audit_log_end(ab);
ian@0 975
ian@0 976 list_del(&r->rlist);
ian@0 977 list_del_rcu(&e->list);
ian@0 978 call_rcu(&e->rcu, audit_free_rule_rcu);
ian@0 979 }
ian@0 980 audit_remove_watch(w);
ian@0 981 }
ian@0 982 mutex_unlock(&audit_filter_mutex);
ian@0 983 }
ian@0 984
ian@0 985 /* Unregister inotify watches for parents on in_list.
ian@0 986 * Generates an IN_IGNORED event. */
ian@0 987 static void audit_inotify_unregister(struct list_head *in_list)
ian@0 988 {
ian@0 989 struct audit_parent *p, *n;
ian@0 990
ian@0 991 list_for_each_entry_safe(p, n, in_list, ilist) {
ian@0 992 list_del(&p->ilist);
ian@0 993 inotify_rm_watch(audit_ih, &p->wdata);
ian@0 994 /* the put matching the get in audit_do_del_rule() */
ian@0 995 put_inotify_watch(&p->wdata);
ian@0 996 }
ian@0 997 }
ian@0 998
ian@0 999 /* Find an existing audit rule.
ian@0 1000 * Caller must hold audit_filter_mutex to prevent stale rule data. */
ian@0 1001 static struct audit_entry *audit_find_rule(struct audit_entry *entry,
ian@0 1002 struct list_head *list)
ian@0 1003 {
ian@0 1004 struct audit_entry *e, *found = NULL;
ian@0 1005 int h;
ian@0 1006
ian@0 1007 if (entry->rule.watch) {
ian@0 1008 /* we don't know the inode number, so must walk entire hash */
ian@0 1009 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
ian@0 1010 list = &audit_inode_hash[h];
ian@0 1011 list_for_each_entry(e, list, list)
ian@0 1012 if (!audit_compare_rule(&entry->rule, &e->rule)) {
ian@0 1013 found = e;
ian@0 1014 goto out;
ian@0 1015 }
ian@0 1016 }
ian@0 1017 goto out;
ian@0 1018 }
ian@0 1019
ian@0 1020 list_for_each_entry(e, list, list)
ian@0 1021 if (!audit_compare_rule(&entry->rule, &e->rule)) {
ian@0 1022 found = e;
ian@0 1023 goto out;
ian@0 1024 }
ian@0 1025
ian@0 1026 out:
ian@0 1027 return found;
ian@0 1028 }
ian@0 1029
ian@0 1030 /* Get path information necessary for adding watches. */
ian@0 1031 static int audit_get_nd(char *path, struct nameidata **ndp,
ian@0 1032 struct nameidata **ndw)
ian@0 1033 {
ian@0 1034 struct nameidata *ndparent, *ndwatch;
ian@0 1035 int err;
ian@0 1036
ian@0 1037 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
ian@0 1038 if (unlikely(!ndparent))
ian@0 1039 return -ENOMEM;
ian@0 1040
ian@0 1041 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
ian@0 1042 if (unlikely(!ndwatch)) {
ian@0 1043 kfree(ndparent);
ian@0 1044 return -ENOMEM;
ian@0 1045 }
ian@0 1046
ian@0 1047 err = path_lookup(path, LOOKUP_PARENT, ndparent);
ian@0 1048 if (err) {
ian@0 1049 kfree(ndparent);
ian@0 1050 kfree(ndwatch);
ian@0 1051 return err;
ian@0 1052 }
ian@0 1053
ian@0 1054 err = path_lookup(path, 0, ndwatch);
ian@0 1055 if (err) {
ian@0 1056 kfree(ndwatch);
ian@0 1057 ndwatch = NULL;
ian@0 1058 }
ian@0 1059
ian@0 1060 *ndp = ndparent;
ian@0 1061 *ndw = ndwatch;
ian@0 1062
ian@0 1063 return 0;
ian@0 1064 }
ian@0 1065
ian@0 1066 /* Release resources used for watch path information. */
ian@0 1067 static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
ian@0 1068 {
ian@0 1069 if (ndp) {
ian@0 1070 path_release(ndp);
ian@0 1071 kfree(ndp);
ian@0 1072 }
ian@0 1073 if (ndw) {
ian@0 1074 path_release(ndw);
ian@0 1075 kfree(ndw);
ian@0 1076 }
ian@0 1077 }
ian@0 1078
ian@0 1079 /* Associate the given rule with an existing parent inotify_watch.
ian@0 1080 * Caller must hold audit_filter_mutex. */
ian@0 1081 static void audit_add_to_parent(struct audit_krule *krule,
ian@0 1082 struct audit_parent *parent)
ian@0 1083 {
ian@0 1084 struct audit_watch *w, *watch = krule->watch;
ian@0 1085 int watch_found = 0;
ian@0 1086
ian@0 1087 list_for_each_entry(w, &parent->watches, wlist) {
ian@0 1088 if (strcmp(watch->path, w->path))
ian@0 1089 continue;
ian@0 1090
ian@0 1091 watch_found = 1;
ian@0 1092
ian@0 1093 /* put krule's and initial refs to temporary watch */
ian@0 1094 audit_put_watch(watch);
ian@0 1095 audit_put_watch(watch);
ian@0 1096
ian@0 1097 audit_get_watch(w);
ian@0 1098 krule->watch = watch = w;
ian@0 1099 break;
ian@0 1100 }
ian@0 1101
ian@0 1102 if (!watch_found) {
ian@0 1103 get_inotify_watch(&parent->wdata);
ian@0 1104 watch->parent = parent;
ian@0 1105
ian@0 1106 list_add(&watch->wlist, &parent->watches);
ian@0 1107 }
ian@0 1108 list_add(&krule->rlist, &watch->rules);
ian@0 1109 }
ian@0 1110
ian@0 1111 /* Find a matching watch entry, or add this one.
ian@0 1112 * Caller must hold audit_filter_mutex. */
ian@0 1113 static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
ian@0 1114 struct nameidata *ndw)
ian@0 1115 {
ian@0 1116 struct audit_watch *watch = krule->watch;
ian@0 1117 struct inotify_watch *i_watch;
ian@0 1118 struct audit_parent *parent;
ian@0 1119 int ret = 0;
ian@0 1120
ian@0 1121 /* update watch filter fields */
ian@0 1122 if (ndw) {
ian@0 1123 watch->dev = ndw->dentry->d_inode->i_sb->s_dev;
ian@0 1124 watch->ino = ndw->dentry->d_inode->i_ino;
ian@0 1125 }
ian@0 1126
ian@0 1127 /* The audit_filter_mutex must not be held during inotify calls because
ian@0 1128 * we hold it during inotify event callback processing. If an existing
ian@0 1129 * inotify watch is found, inotify_find_watch() grabs a reference before
ian@0 1130 * returning.
ian@0 1131 */
ian@0 1132 mutex_unlock(&audit_filter_mutex);
ian@0 1133
ian@0 1134 if (inotify_find_watch(audit_ih, ndp->dentry->d_inode, &i_watch) < 0) {
ian@0 1135 parent = audit_init_parent(ndp);
ian@0 1136 if (IS_ERR(parent)) {
ian@0 1137 /* caller expects mutex locked */
ian@0 1138 mutex_lock(&audit_filter_mutex);
ian@0 1139 return PTR_ERR(parent);
ian@0 1140 }
ian@0 1141 } else
ian@0 1142 parent = container_of(i_watch, struct audit_parent, wdata);
ian@0 1143
ian@0 1144 mutex_lock(&audit_filter_mutex);
ian@0 1145
ian@0 1146 /* parent was moved before we took audit_filter_mutex */
ian@0 1147 if (parent->flags & AUDIT_PARENT_INVALID)
ian@0 1148 ret = -ENOENT;
ian@0 1149 else
ian@0 1150 audit_add_to_parent(krule, parent);
ian@0 1151
ian@0 1152 /* match get in audit_init_parent or inotify_find_watch */
ian@0 1153 put_inotify_watch(&parent->wdata);
ian@0 1154 return ret;
ian@0 1155 }
ian@0 1156
ian@0 1157 /* Add rule to given filterlist if not a duplicate. */
ian@0 1158 static inline int audit_add_rule(struct audit_entry *entry,
ian@0 1159 struct list_head *list)
ian@0 1160 {
ian@0 1161 struct audit_entry *e;
ian@0 1162 struct audit_field *inode_f = entry->rule.inode_f;
ian@0 1163 struct audit_watch *watch = entry->rule.watch;
ian@0 1164 struct nameidata *ndp, *ndw;
ian@0 1165 int h, err, putnd_needed = 0;
ian@0 1166 #ifdef CONFIG_AUDITSYSCALL
ian@0 1167 int dont_count = 0;
ian@0 1168
ian@0 1169 /* If either of these, don't count towards total */
ian@0 1170 if (entry->rule.listnr == AUDIT_FILTER_USER ||
ian@0 1171 entry->rule.listnr == AUDIT_FILTER_TYPE)
ian@0 1172 dont_count = 1;
ian@0 1173 #endif
ian@0 1174
ian@0 1175 if (inode_f) {
ian@0 1176 h = audit_hash_ino(inode_f->val);
ian@0 1177 list = &audit_inode_hash[h];
ian@0 1178 }
ian@0 1179
ian@0 1180 mutex_lock(&audit_filter_mutex);
ian@0 1181 e = audit_find_rule(entry, list);
ian@0 1182 mutex_unlock(&audit_filter_mutex);
ian@0 1183 if (e) {
ian@0 1184 err = -EEXIST;
ian@0 1185 goto error;
ian@0 1186 }
ian@0 1187
ian@0 1188 /* Avoid calling path_lookup under audit_filter_mutex. */
ian@0 1189 if (watch) {
ian@0 1190 err = audit_get_nd(watch->path, &ndp, &ndw);
ian@0 1191 if (err)
ian@0 1192 goto error;
ian@0 1193 putnd_needed = 1;
ian@0 1194 }
ian@0 1195
ian@0 1196 mutex_lock(&audit_filter_mutex);
ian@0 1197 if (watch) {
ian@0 1198 /* audit_filter_mutex is dropped and re-taken during this call */
ian@0 1199 err = audit_add_watch(&entry->rule, ndp, ndw);
ian@0 1200 if (err) {
ian@0 1201 mutex_unlock(&audit_filter_mutex);
ian@0 1202 goto error;
ian@0 1203 }
ian@0 1204 h = audit_hash_ino((u32)watch->ino);
ian@0 1205 list = &audit_inode_hash[h];
ian@0 1206 }
ian@0 1207
ian@0 1208 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
ian@0 1209 list_add_rcu(&entry->list, list);
ian@0 1210 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
ian@0 1211 } else {
ian@0 1212 list_add_tail_rcu(&entry->list, list);
ian@0 1213 }
ian@0 1214 #ifdef CONFIG_AUDITSYSCALL
ian@0 1215 if (!dont_count)
ian@0 1216 audit_n_rules++;
ian@0 1217 #endif
ian@0 1218 mutex_unlock(&audit_filter_mutex);
ian@0 1219
ian@0 1220 if (putnd_needed)
ian@0 1221 audit_put_nd(ndp, ndw);
ian@0 1222
ian@0 1223 return 0;
ian@0 1224
ian@0 1225 error:
ian@0 1226 if (putnd_needed)
ian@0 1227 audit_put_nd(ndp, ndw);
ian@0 1228 if (watch)
ian@0 1229 audit_put_watch(watch); /* tmp watch, matches initial get */
ian@0 1230 return err;
ian@0 1231 }
ian@0 1232
ian@0 1233 /* Remove an existing rule from filterlist. */
ian@0 1234 static inline int audit_del_rule(struct audit_entry *entry,
ian@0 1235 struct list_head *list)
ian@0 1236 {
ian@0 1237 struct audit_entry *e;
ian@0 1238 struct audit_field *inode_f = entry->rule.inode_f;
ian@0 1239 struct audit_watch *watch, *tmp_watch = entry->rule.watch;
ian@0 1240 LIST_HEAD(inotify_list);
ian@0 1241 int h, ret = 0;
ian@0 1242 #ifdef CONFIG_AUDITSYSCALL
ian@0 1243 int dont_count = 0;
ian@0 1244
ian@0 1245 /* If either of these, don't count towards total */
ian@0 1246 if (entry->rule.listnr == AUDIT_FILTER_USER ||
ian@0 1247 entry->rule.listnr == AUDIT_FILTER_TYPE)
ian@0 1248 dont_count = 1;
ian@0 1249 #endif
ian@0 1250
ian@0 1251 if (inode_f) {
ian@0 1252 h = audit_hash_ino(inode_f->val);
ian@0 1253 list = &audit_inode_hash[h];
ian@0 1254 }
ian@0 1255
ian@0 1256 mutex_lock(&audit_filter_mutex);
ian@0 1257 e = audit_find_rule(entry, list);
ian@0 1258 if (!e) {
ian@0 1259 mutex_unlock(&audit_filter_mutex);
ian@0 1260 ret = -ENOENT;
ian@0 1261 goto out;
ian@0 1262 }
ian@0 1263
ian@0 1264 watch = e->rule.watch;
ian@0 1265 if (watch) {
ian@0 1266 struct audit_parent *parent = watch->parent;
ian@0 1267
ian@0 1268 list_del(&e->rule.rlist);
ian@0 1269
ian@0 1270 if (list_empty(&watch->rules)) {
ian@0 1271 audit_remove_watch(watch);
ian@0 1272
ian@0 1273 if (list_empty(&parent->watches)) {
ian@0 1274 /* Put parent on the inotify un-registration
ian@0 1275 * list. Grab a reference before releasing
ian@0 1276 * audit_filter_mutex, to be released in
ian@0 1277 * audit_inotify_unregister(). */
ian@0 1278 list_add(&parent->ilist, &inotify_list);
ian@0 1279 get_inotify_watch(&parent->wdata);
ian@0 1280 }
ian@0 1281 }
ian@0 1282 }
ian@0 1283
ian@0 1284 list_del_rcu(&e->list);
ian@0 1285 call_rcu(&e->rcu, audit_free_rule_rcu);
ian@0 1286
ian@0 1287 #ifdef CONFIG_AUDITSYSCALL
ian@0 1288 if (!dont_count)
ian@0 1289 audit_n_rules--;
ian@0 1290 #endif
ian@0 1291 mutex_unlock(&audit_filter_mutex);
ian@0 1292
ian@0 1293 if (!list_empty(&inotify_list))
ian@0 1294 audit_inotify_unregister(&inotify_list);
ian@0 1295
ian@0 1296 out:
ian@0 1297 if (tmp_watch)
ian@0 1298 audit_put_watch(tmp_watch); /* match initial get */
ian@0 1299
ian@0 1300 return ret;
ian@0 1301 }
ian@0 1302
ian@0 1303 /* List rules using struct audit_rule. Exists for backward
ian@0 1304 * compatibility with userspace. */
ian@0 1305 static void audit_list(int pid, int seq, struct sk_buff_head *q)
ian@0 1306 {
ian@0 1307 struct sk_buff *skb;
ian@0 1308 struct audit_entry *entry;
ian@0 1309 int i;
ian@0 1310
ian@0 1311 /* This is a blocking read, so use audit_filter_mutex instead of rcu
ian@0 1312 * iterator to sync with list writers. */
ian@0 1313 for (i=0; i<AUDIT_NR_FILTERS; i++) {
ian@0 1314 list_for_each_entry(entry, &audit_filter_list[i], list) {
ian@0 1315 struct audit_rule *rule;
ian@0 1316
ian@0 1317 rule = audit_krule_to_rule(&entry->rule);
ian@0 1318 if (unlikely(!rule))
ian@0 1319 break;
ian@0 1320 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
ian@0 1321 rule, sizeof(*rule));
ian@0 1322 if (skb)
ian@0 1323 skb_queue_tail(q, skb);
ian@0 1324 kfree(rule);
ian@0 1325 }
ian@0 1326 }
ian@0 1327 for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
ian@0 1328 list_for_each_entry(entry, &audit_inode_hash[i], list) {
ian@0 1329 struct audit_rule *rule;
ian@0 1330
ian@0 1331 rule = audit_krule_to_rule(&entry->rule);
ian@0 1332 if (unlikely(!rule))
ian@0 1333 break;
ian@0 1334 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
ian@0 1335 rule, sizeof(*rule));
ian@0 1336 if (skb)
ian@0 1337 skb_queue_tail(q, skb);
ian@0 1338 kfree(rule);
ian@0 1339 }
ian@0 1340 }
ian@0 1341 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
ian@0 1342 if (skb)
ian@0 1343 skb_queue_tail(q, skb);
ian@0 1344 }
ian@0 1345
ian@0 1346 /* List rules using struct audit_rule_data. */
ian@0 1347 static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
ian@0 1348 {
ian@0 1349 struct sk_buff *skb;
ian@0 1350 struct audit_entry *e;
ian@0 1351 int i;
ian@0 1352
ian@0 1353 /* This is a blocking read, so use audit_filter_mutex instead of rcu
ian@0 1354 * iterator to sync with list writers. */
ian@0 1355 for (i=0; i<AUDIT_NR_FILTERS; i++) {
ian@0 1356 list_for_each_entry(e, &audit_filter_list[i], list) {
ian@0 1357 struct audit_rule_data *data;
ian@0 1358
ian@0 1359 data = audit_krule_to_data(&e->rule);
ian@0 1360 if (unlikely(!data))
ian@0 1361 break;
ian@0 1362 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
ian@0 1363 data, sizeof(*data) + data->buflen);
ian@0 1364 if (skb)
ian@0 1365 skb_queue_tail(q, skb);
ian@0 1366 kfree(data);
ian@0 1367 }
ian@0 1368 }
ian@0 1369 for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
ian@0 1370 list_for_each_entry(e, &audit_inode_hash[i], list) {
ian@0 1371 struct audit_rule_data *data;
ian@0 1372
ian@0 1373 data = audit_krule_to_data(&e->rule);
ian@0 1374 if (unlikely(!data))
ian@0 1375 break;
ian@0 1376 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
ian@0 1377 data, sizeof(*data) + data->buflen);
ian@0 1378 if (skb)
ian@0 1379 skb_queue_tail(q, skb);
ian@0 1380 kfree(data);
ian@0 1381 }
ian@0 1382 }
ian@0 1383 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
ian@0 1384 if (skb)
ian@0 1385 skb_queue_tail(q, skb);
ian@0 1386 }
ian@0 1387
ian@0 1388 /* Log rule additions and removals */
ian@0 1389 static void audit_log_rule_change(uid_t loginuid, u32 sid, char *action,
ian@0 1390 struct audit_krule *rule, int res)
ian@0 1391 {
ian@0 1392 struct audit_buffer *ab;
ian@0 1393
ian@0 1394 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
ian@0 1395 if (!ab)
ian@0 1396 return;
ian@0 1397 audit_log_format(ab, "auid=%u", loginuid);
ian@0 1398 if (sid) {
ian@0 1399 char *ctx = NULL;
ian@0 1400 u32 len;
ian@0 1401 if (selinux_ctxid_to_string(sid, &ctx, &len))
ian@0 1402 audit_log_format(ab, " ssid=%u", sid);
ian@0 1403 else
ian@0 1404 audit_log_format(ab, " subj=%s", ctx);
ian@0 1405 kfree(ctx);
ian@0 1406 }
ian@0 1407 audit_log_format(ab, " %s rule key=", action);
ian@0 1408 if (rule->filterkey)
ian@0 1409 audit_log_untrustedstring(ab, rule->filterkey);
ian@0 1410 else
ian@0 1411 audit_log_format(ab, "(null)");
ian@0 1412 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
ian@0 1413 audit_log_end(ab);
ian@0 1414 }
ian@0 1415
ian@0 1416 /**
ian@0 1417 * audit_receive_filter - apply all rules to the specified message type
ian@0 1418 * @type: audit message type
ian@0 1419 * @pid: target pid for netlink audit messages
ian@0 1420 * @uid: target uid for netlink audit messages
ian@0 1421 * @seq: netlink audit message sequence (serial) number
ian@0 1422 * @data: payload data
ian@0 1423 * @datasz: size of payload data
ian@0 1424 * @loginuid: loginuid of sender
ian@0 1425 * @sid: SE Linux Security ID of sender
ian@0 1426 */
ian@0 1427 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
ian@0 1428 size_t datasz, uid_t loginuid, u32 sid)
ian@0 1429 {
ian@0 1430 struct task_struct *tsk;
ian@0 1431 struct audit_netlink_list *dest;
ian@0 1432 int err = 0;
ian@0 1433 struct audit_entry *entry;
ian@0 1434
ian@0 1435 switch (type) {
ian@0 1436 case AUDIT_LIST:
ian@0 1437 case AUDIT_LIST_RULES:
ian@0 1438 /* We can't just spew out the rules here because we might fill
ian@0 1439 * the available socket buffer space and deadlock waiting for
ian@0 1440 * auditctl to read from it... which isn't ever going to
ian@0 1441 * happen if we're actually running in the context of auditctl
ian@0 1442 * trying to _send_ the stuff */
ian@0 1443
ian@0 1444 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
ian@0 1445 if (!dest)
ian@0 1446 return -ENOMEM;
ian@0 1447 dest->pid = pid;
ian@0 1448 skb_queue_head_init(&dest->q);
ian@0 1449
ian@0 1450 mutex_lock(&audit_filter_mutex);
ian@0 1451 if (type == AUDIT_LIST)
ian@0 1452 audit_list(pid, seq, &dest->q);
ian@0 1453 else
ian@0 1454 audit_list_rules(pid, seq, &dest->q);
ian@0 1455 mutex_unlock(&audit_filter_mutex);
ian@0 1456
ian@0 1457 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
ian@0 1458 if (IS_ERR(tsk)) {
ian@0 1459 skb_queue_purge(&dest->q);
ian@0 1460 kfree(dest);
ian@0 1461 err = PTR_ERR(tsk);
ian@0 1462 }
ian@0 1463 break;
ian@0 1464 case AUDIT_ADD:
ian@0 1465 case AUDIT_ADD_RULE:
ian@0 1466 if (type == AUDIT_ADD)
ian@0 1467 entry = audit_rule_to_entry(data);
ian@0 1468 else
ian@0 1469 entry = audit_data_to_entry(data, datasz);
ian@0 1470 if (IS_ERR(entry))
ian@0 1471 return PTR_ERR(entry);
ian@0 1472
ian@0 1473 err = audit_add_rule(entry,
ian@0 1474 &audit_filter_list[entry->rule.listnr]);
ian@0 1475 audit_log_rule_change(loginuid, sid, "add", &entry->rule, !err);
ian@0 1476
ian@0 1477 if (err)
ian@0 1478 audit_free_rule(entry);
ian@0 1479 break;
ian@0 1480 case AUDIT_DEL:
ian@0 1481 case AUDIT_DEL_RULE:
ian@0 1482 if (type == AUDIT_DEL)
ian@0 1483 entry = audit_rule_to_entry(data);
ian@0 1484 else
ian@0 1485 entry = audit_data_to_entry(data, datasz);
ian@0 1486 if (IS_ERR(entry))
ian@0 1487 return PTR_ERR(entry);
ian@0 1488
ian@0 1489 err = audit_del_rule(entry,
ian@0 1490 &audit_filter_list[entry->rule.listnr]);
ian@0 1491 audit_log_rule_change(loginuid, sid, "remove", &entry->rule,
ian@0 1492 !err);
ian@0 1493
ian@0 1494 audit_free_rule(entry);
ian@0 1495 break;
ian@0 1496 default:
ian@0 1497 return -EINVAL;
ian@0 1498 }
ian@0 1499
ian@0 1500 return err;
ian@0 1501 }
ian@0 1502
ian@0 1503 int audit_comparator(const u32 left, const u32 op, const u32 right)
ian@0 1504 {
ian@0 1505 switch (op) {
ian@0 1506 case AUDIT_EQUAL:
ian@0 1507 return (left == right);
ian@0 1508 case AUDIT_NOT_EQUAL:
ian@0 1509 return (left != right);
ian@0 1510 case AUDIT_LESS_THAN:
ian@0 1511 return (left < right);
ian@0 1512 case AUDIT_LESS_THAN_OR_EQUAL:
ian@0 1513 return (left <= right);
ian@0 1514 case AUDIT_GREATER_THAN:
ian@0 1515 return (left > right);
ian@0 1516 case AUDIT_GREATER_THAN_OR_EQUAL:
ian@0 1517 return (left >= right);
ian@0 1518 }
ian@0 1519 BUG();
ian@0 1520 return 0;
ian@0 1521 }
ian@0 1522
ian@0 1523 /* Compare given dentry name with last component in given path,
ian@0 1524 * return of 0 indicates a match. */
ian@0 1525 int audit_compare_dname_path(const char *dname, const char *path,
ian@0 1526 int *dirlen)
ian@0 1527 {
ian@0 1528 int dlen, plen;
ian@0 1529 const char *p;
ian@0 1530
ian@0 1531 if (!dname || !path)
ian@0 1532 return 1;
ian@0 1533
ian@0 1534 dlen = strlen(dname);
ian@0 1535 plen = strlen(path);
ian@0 1536 if (plen < dlen)
ian@0 1537 return 1;
ian@0 1538
ian@0 1539 /* disregard trailing slashes */
ian@0 1540 p = path + plen - 1;
ian@0 1541 while ((*p == '/') && (p > path))
ian@0 1542 p--;
ian@0 1543
ian@0 1544 /* find last path component */
ian@0 1545 p = p - dlen + 1;
ian@0 1546 if (p < path)
ian@0 1547 return 1;
ian@0 1548 else if (p > path) {
ian@0 1549 if (*--p != '/')
ian@0 1550 return 1;
ian@0 1551 else
ian@0 1552 p++;
ian@0 1553 }
ian@0 1554
ian@0 1555 /* return length of path's directory component */
ian@0 1556 if (dirlen)
ian@0 1557 *dirlen = p - path;
ian@0 1558 return strncmp(p, dname, dlen);
ian@0 1559 }
ian@0 1560
ian@0 1561 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
ian@0 1562 struct audit_krule *rule,
ian@0 1563 enum audit_state *state)
ian@0 1564 {
ian@0 1565 int i;
ian@0 1566
ian@0 1567 for (i = 0; i < rule->field_count; i++) {
ian@0 1568 struct audit_field *f = &rule->fields[i];
ian@0 1569 int result = 0;
ian@0 1570
ian@0 1571 switch (f->type) {
ian@0 1572 case AUDIT_PID:
ian@0 1573 result = audit_comparator(cb->creds.pid, f->op, f->val);
ian@0 1574 break;
ian@0 1575 case AUDIT_UID:
ian@0 1576 result = audit_comparator(cb->creds.uid, f->op, f->val);
ian@0 1577 break;
ian@0 1578 case AUDIT_GID:
ian@0 1579 result = audit_comparator(cb->creds.gid, f->op, f->val);
ian@0 1580 break;
ian@0 1581 case AUDIT_LOGINUID:
ian@0 1582 result = audit_comparator(cb->loginuid, f->op, f->val);
ian@0 1583 break;
ian@0 1584 }
ian@0 1585
ian@0 1586 if (!result)
ian@0 1587 return 0;
ian@0 1588 }
ian@0 1589 switch (rule->action) {
ian@0 1590 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
ian@0 1591 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
ian@0 1592 }
ian@0 1593 return 1;
ian@0 1594 }
ian@0 1595
ian@0 1596 int audit_filter_user(struct netlink_skb_parms *cb, int type)
ian@0 1597 {
ian@0 1598 struct audit_entry *e;
ian@0 1599 enum audit_state state;
ian@0 1600 int ret = 1;
ian@0 1601
ian@0 1602 rcu_read_lock();
ian@0 1603 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
ian@0 1604 if (audit_filter_user_rules(cb, &e->rule, &state)) {
ian@0 1605 if (state == AUDIT_DISABLED)
ian@0 1606 ret = 0;
ian@0 1607 break;
ian@0 1608 }
ian@0 1609 }
ian@0 1610 rcu_read_unlock();
ian@0 1611
ian@0 1612 return ret; /* Audit by default */
ian@0 1613 }
ian@0 1614
ian@0 1615 int audit_filter_type(int type)
ian@0 1616 {
ian@0 1617 struct audit_entry *e;
ian@0 1618 int result = 0;
ian@0 1619
ian@0 1620 rcu_read_lock();
ian@0 1621 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
ian@0 1622 goto unlock_and_return;
ian@0 1623
ian@0 1624 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
ian@0 1625 list) {
ian@0 1626 int i;
ian@0 1627 for (i = 0; i < e->rule.field_count; i++) {
ian@0 1628 struct audit_field *f = &e->rule.fields[i];
ian@0 1629 if (f->type == AUDIT_MSGTYPE) {
ian@0 1630 result = audit_comparator(type, f->op, f->val);
ian@0 1631 if (!result)
ian@0 1632 break;
ian@0 1633 }
ian@0 1634 }
ian@0 1635 if (result)
ian@0 1636 goto unlock_and_return;
ian@0 1637 }
ian@0 1638 unlock_and_return:
ian@0 1639 rcu_read_unlock();
ian@0 1640 return result;
ian@0 1641 }
ian@0 1642
ian@0 1643 /* Check to see if the rule contains any selinux fields. Returns 1 if there
ian@0 1644 are selinux fields specified in the rule, 0 otherwise. */
ian@0 1645 static inline int audit_rule_has_selinux(struct audit_krule *rule)
ian@0 1646 {
ian@0 1647 int i;
ian@0 1648
ian@0 1649 for (i = 0; i < rule->field_count; i++) {
ian@0 1650 struct audit_field *f = &rule->fields[i];
ian@0 1651 switch (f->type) {
ian@0 1652 case AUDIT_SUBJ_USER:
ian@0 1653 case AUDIT_SUBJ_ROLE:
ian@0 1654 case AUDIT_SUBJ_TYPE:
ian@0 1655 case AUDIT_SUBJ_SEN:
ian@0 1656 case AUDIT_SUBJ_CLR:
ian@0 1657 case AUDIT_OBJ_USER:
ian@0 1658 case AUDIT_OBJ_ROLE:
ian@0 1659 case AUDIT_OBJ_TYPE:
ian@0 1660 case AUDIT_OBJ_LEV_LOW:
ian@0 1661 case AUDIT_OBJ_LEV_HIGH:
ian@0 1662 return 1;
ian@0 1663 }
ian@0 1664 }
ian@0 1665
ian@0 1666 return 0;
ian@0 1667 }
ian@0 1668
ian@0 1669 /* This function will re-initialize the se_rule field of all applicable rules.
ian@0 1670 * It will traverse the filter lists serarching for rules that contain selinux
ian@0 1671 * specific filter fields. When such a rule is found, it is copied, the
ian@0 1672 * selinux field is re-initialized, and the old rule is replaced with the
ian@0 1673 * updated rule. */
ian@0 1674 int selinux_audit_rule_update(void)
ian@0 1675 {
ian@0 1676 struct audit_entry *entry, *n, *nentry;
ian@0 1677 struct audit_watch *watch;
ian@0 1678 int i, err = 0;
ian@0 1679
ian@0 1680 /* audit_filter_mutex synchronizes the writers */
ian@0 1681 mutex_lock(&audit_filter_mutex);
ian@0 1682
ian@0 1683 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
ian@0 1684 list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
ian@0 1685 if (!audit_rule_has_selinux(&entry->rule))
ian@0 1686 continue;
ian@0 1687
ian@0 1688 watch = entry->rule.watch;
ian@0 1689 nentry = audit_dupe_rule(&entry->rule, watch);
ian@0 1690 if (unlikely(IS_ERR(nentry))) {
ian@0 1691 /* save the first error encountered for the
ian@0 1692 * return value */
ian@0 1693 if (!err)
ian@0 1694 err = PTR_ERR(nentry);
ian@0 1695 audit_panic("error updating selinux filters");
ian@0 1696 if (watch)
ian@0 1697 list_del(&entry->rule.rlist);
ian@0 1698 list_del_rcu(&entry->list);
ian@0 1699 } else {
ian@0 1700 if (watch) {
ian@0 1701 list_add(&nentry->rule.rlist,
ian@0 1702 &watch->rules);
ian@0 1703 list_del(&entry->rule.rlist);
ian@0 1704 }
ian@0 1705 list_replace_rcu(&entry->list, &nentry->list);
ian@0 1706 }
ian@0 1707 call_rcu(&entry->rcu, audit_free_rule_rcu);
ian@0 1708 }
ian@0 1709 }
ian@0 1710
ian@0 1711 mutex_unlock(&audit_filter_mutex);
ian@0 1712
ian@0 1713 return err;
ian@0 1714 }
ian@0 1715
ian@0 1716 /* Update watch data in audit rules based on inotify events. */
ian@0 1717 void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
ian@0 1718 u32 cookie, const char *dname, struct inode *inode)
ian@0 1719 {
ian@0 1720 struct audit_parent *parent;
ian@0 1721
ian@0 1722 parent = container_of(i_watch, struct audit_parent, wdata);
ian@0 1723
ian@0 1724 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
ian@0 1725 audit_update_watch(parent, dname, inode->i_sb->s_dev,
ian@0 1726 inode->i_ino, 0);
ian@0 1727 else if (mask & (IN_DELETE|IN_MOVED_FROM))
ian@0 1728 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
ian@0 1729 /* inotify automatically removes the watch and sends IN_IGNORED */
ian@0 1730 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
ian@0 1731 audit_remove_parent_watches(parent);
ian@0 1732 /* inotify does not remove the watch, so remove it manually */
ian@0 1733 else if(mask & IN_MOVE_SELF) {
ian@0 1734 audit_remove_parent_watches(parent);
ian@0 1735 inotify_remove_watch_locked(audit_ih, i_watch);
ian@0 1736 } else if (mask & IN_IGNORED)
ian@0 1737 put_inotify_watch(i_watch);
ian@0 1738 }