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