Back to home page

LXR

 
 

    


0001 /* audit_watch.c -- watching inodes
0002  *
0003  * Copyright 2003-2009 Red Hat, Inc.
0004  * Copyright 2005 Hewlett-Packard Development Company, L.P.
0005  * Copyright 2005 IBM Corporation
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License as published by
0009  * the Free Software Foundation; either version 2 of the License, or
0010  * (at your option) any later version.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program; if not, write to the Free Software
0019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0020  */
0021 
0022 #include <linux/file.h>
0023 #include <linux/kernel.h>
0024 #include <linux/audit.h>
0025 #include <linux/kthread.h>
0026 #include <linux/mutex.h>
0027 #include <linux/fs.h>
0028 #include <linux/fsnotify_backend.h>
0029 #include <linux/namei.h>
0030 #include <linux/netlink.h>
0031 #include <linux/sched.h>
0032 #include <linux/slab.h>
0033 #include <linux/security.h>
0034 #include "audit.h"
0035 
0036 /*
0037  * Reference counting:
0038  *
0039  * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
0040  *  event.  Each audit_watch holds a reference to its associated parent.
0041  *
0042  * audit_watch: if added to lists, lifetime is from audit_init_watch() to
0043  *  audit_remove_watch().  Additionally, an audit_watch may exist
0044  *  temporarily to assist in searching existing filter data.  Each
0045  *  audit_krule holds a reference to its associated watch.
0046  */
0047 
0048 struct audit_watch {
0049     atomic_t        count;  /* reference count */
0050     dev_t           dev;    /* associated superblock device */
0051     char            *path;  /* insertion path */
0052     unsigned long       ino;    /* associated inode number */
0053     struct audit_parent *parent; /* associated parent */
0054     struct list_head    wlist;  /* entry in parent->watches list */
0055     struct list_head    rules;  /* anchor for krule->rlist */
0056 };
0057 
0058 struct audit_parent {
0059     struct list_head    watches; /* anchor for audit_watch->wlist */
0060     struct fsnotify_mark mark; /* fsnotify mark on the inode */
0061 };
0062 
0063 /* fsnotify handle. */
0064 static struct fsnotify_group *audit_watch_group;
0065 
0066 /* fsnotify events we care about. */
0067 #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
0068             FS_MOVE_SELF | FS_EVENT_ON_CHILD)
0069 
0070 static void audit_free_parent(struct audit_parent *parent)
0071 {
0072     WARN_ON(!list_empty(&parent->watches));
0073     kfree(parent);
0074 }
0075 
0076 static void audit_watch_free_mark(struct fsnotify_mark *entry)
0077 {
0078     struct audit_parent *parent;
0079 
0080     parent = container_of(entry, struct audit_parent, mark);
0081     audit_free_parent(parent);
0082 }
0083 
0084 static void audit_get_parent(struct audit_parent *parent)
0085 {
0086     if (likely(parent))
0087         fsnotify_get_mark(&parent->mark);
0088 }
0089 
0090 static void audit_put_parent(struct audit_parent *parent)
0091 {
0092     if (likely(parent))
0093         fsnotify_put_mark(&parent->mark);
0094 }
0095 
0096 /*
0097  * Find and return the audit_parent on the given inode.  If found a reference
0098  * is taken on this parent.
0099  */
0100 static inline struct audit_parent *audit_find_parent(struct inode *inode)
0101 {
0102     struct audit_parent *parent = NULL;
0103     struct fsnotify_mark *entry;
0104 
0105     entry = fsnotify_find_inode_mark(audit_watch_group, inode);
0106     if (entry)
0107         parent = container_of(entry, struct audit_parent, mark);
0108 
0109     return parent;
0110 }
0111 
0112 void audit_get_watch(struct audit_watch *watch)
0113 {
0114     atomic_inc(&watch->count);
0115 }
0116 
0117 void audit_put_watch(struct audit_watch *watch)
0118 {
0119     if (atomic_dec_and_test(&watch->count)) {
0120         WARN_ON(watch->parent);
0121         WARN_ON(!list_empty(&watch->rules));
0122         kfree(watch->path);
0123         kfree(watch);
0124     }
0125 }
0126 
0127 static void audit_remove_watch(struct audit_watch *watch)
0128 {
0129     list_del(&watch->wlist);
0130     audit_put_parent(watch->parent);
0131     watch->parent = NULL;
0132     audit_put_watch(watch); /* match initial get */
0133 }
0134 
0135 char *audit_watch_path(struct audit_watch *watch)
0136 {
0137     return watch->path;
0138 }
0139 
0140 int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
0141 {
0142     return (watch->ino != AUDIT_INO_UNSET) &&
0143         (watch->ino == ino) &&
0144         (watch->dev == dev);
0145 }
0146 
0147 /* Initialize a parent watch entry. */
0148 static struct audit_parent *audit_init_parent(struct path *path)
0149 {
0150     struct inode *inode = d_backing_inode(path->dentry);
0151     struct audit_parent *parent;
0152     int ret;
0153 
0154     parent = kzalloc(sizeof(*parent), GFP_KERNEL);
0155     if (unlikely(!parent))
0156         return ERR_PTR(-ENOMEM);
0157 
0158     INIT_LIST_HEAD(&parent->watches);
0159 
0160     fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
0161     parent->mark.mask = AUDIT_FS_WATCH;
0162     ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
0163     if (ret < 0) {
0164         audit_free_parent(parent);
0165         return ERR_PTR(ret);
0166     }
0167 
0168     return parent;
0169 }
0170 
0171 /* Initialize a watch entry. */
0172 static struct audit_watch *audit_init_watch(char *path)
0173 {
0174     struct audit_watch *watch;
0175 
0176     watch = kzalloc(sizeof(*watch), GFP_KERNEL);
0177     if (unlikely(!watch))
0178         return ERR_PTR(-ENOMEM);
0179 
0180     INIT_LIST_HEAD(&watch->rules);
0181     atomic_set(&watch->count, 1);
0182     watch->path = path;
0183     watch->dev = AUDIT_DEV_UNSET;
0184     watch->ino = AUDIT_INO_UNSET;
0185 
0186     return watch;
0187 }
0188 
0189 /* Translate a watch string to kernel representation. */
0190 int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
0191 {
0192     struct audit_watch *watch;
0193 
0194     if (!audit_watch_group)
0195         return -EOPNOTSUPP;
0196 
0197     if (path[0] != '/' || path[len-1] == '/' ||
0198         krule->listnr != AUDIT_FILTER_EXIT ||
0199         op != Audit_equal ||
0200         krule->inode_f || krule->watch || krule->tree)
0201         return -EINVAL;
0202 
0203     watch = audit_init_watch(path);
0204     if (IS_ERR(watch))
0205         return PTR_ERR(watch);
0206 
0207     krule->watch = watch;
0208 
0209     return 0;
0210 }
0211 
0212 /* Duplicate the given audit watch.  The new watch's rules list is initialized
0213  * to an empty list and wlist is undefined. */
0214 static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
0215 {
0216     char *path;
0217     struct audit_watch *new;
0218 
0219     path = kstrdup(old->path, GFP_KERNEL);
0220     if (unlikely(!path))
0221         return ERR_PTR(-ENOMEM);
0222 
0223     new = audit_init_watch(path);
0224     if (IS_ERR(new)) {
0225         kfree(path);
0226         goto out;
0227     }
0228 
0229     new->dev = old->dev;
0230     new->ino = old->ino;
0231     audit_get_parent(old->parent);
0232     new->parent = old->parent;
0233 
0234 out:
0235     return new;
0236 }
0237 
0238 static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
0239 {
0240     if (audit_enabled) {
0241         struct audit_buffer *ab;
0242         ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
0243         if (unlikely(!ab))
0244             return;
0245         audit_log_format(ab, "auid=%u ses=%u op=%s",
0246                  from_kuid(&init_user_ns, audit_get_loginuid(current)),
0247                  audit_get_sessionid(current), op);
0248         audit_log_format(ab, " path=");
0249         audit_log_untrustedstring(ab, w->path);
0250         audit_log_key(ab, r->filterkey);
0251         audit_log_format(ab, " list=%d res=1", r->listnr);
0252         audit_log_end(ab);
0253     }
0254 }
0255 
0256 /* Update inode info in audit rules based on filesystem event. */
0257 static void audit_update_watch(struct audit_parent *parent,
0258                    const char *dname, dev_t dev,
0259                    unsigned long ino, unsigned invalidating)
0260 {
0261     struct audit_watch *owatch, *nwatch, *nextw;
0262     struct audit_krule *r, *nextr;
0263     struct audit_entry *oentry, *nentry;
0264 
0265     mutex_lock(&audit_filter_mutex);
0266     /* Run all of the watches on this parent looking for the one that
0267      * matches the given dname */
0268     list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
0269         if (audit_compare_dname_path(dname, owatch->path,
0270                          AUDIT_NAME_FULL))
0271             continue;
0272 
0273         /* If the update involves invalidating rules, do the inode-based
0274          * filtering now, so we don't omit records. */
0275         if (invalidating && !audit_dummy_context())
0276             audit_filter_inodes(current, current->audit_context);
0277 
0278         /* updating ino will likely change which audit_hash_list we
0279          * are on so we need a new watch for the new list */
0280         nwatch = audit_dupe_watch(owatch);
0281         if (IS_ERR(nwatch)) {
0282             mutex_unlock(&audit_filter_mutex);
0283             audit_panic("error updating watch, skipping");
0284             return;
0285         }
0286         nwatch->dev = dev;
0287         nwatch->ino = ino;
0288 
0289         list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
0290 
0291             oentry = container_of(r, struct audit_entry, rule);
0292             list_del(&oentry->rule.rlist);
0293             list_del_rcu(&oentry->list);
0294 
0295             nentry = audit_dupe_rule(&oentry->rule);
0296             if (IS_ERR(nentry)) {
0297                 list_del(&oentry->rule.list);
0298                 audit_panic("error updating watch, removing");
0299             } else {
0300                 int h = audit_hash_ino((u32)ino);
0301 
0302                 /*
0303                  * nentry->rule.watch == oentry->rule.watch so
0304                  * we must drop that reference and set it to our
0305                  * new watch.
0306                  */
0307                 audit_put_watch(nentry->rule.watch);
0308                 audit_get_watch(nwatch);
0309                 nentry->rule.watch = nwatch;
0310                 list_add(&nentry->rule.rlist, &nwatch->rules);
0311                 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
0312                 list_replace(&oentry->rule.list,
0313                          &nentry->rule.list);
0314             }
0315             if (oentry->rule.exe)
0316                 audit_remove_mark(oentry->rule.exe);
0317 
0318             audit_watch_log_rule_change(r, owatch, "updated_rules");
0319 
0320             call_rcu(&oentry->rcu, audit_free_rule_rcu);
0321         }
0322 
0323         audit_remove_watch(owatch);
0324         goto add_watch_to_parent; /* event applies to a single watch */
0325     }
0326     mutex_unlock(&audit_filter_mutex);
0327     return;
0328 
0329 add_watch_to_parent:
0330     list_add(&nwatch->wlist, &parent->watches);
0331     mutex_unlock(&audit_filter_mutex);
0332     return;
0333 }
0334 
0335 /* Remove all watches & rules associated with a parent that is going away. */
0336 static void audit_remove_parent_watches(struct audit_parent *parent)
0337 {
0338     struct audit_watch *w, *nextw;
0339     struct audit_krule *r, *nextr;
0340     struct audit_entry *e;
0341 
0342     mutex_lock(&audit_filter_mutex);
0343     list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
0344         list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
0345             e = container_of(r, struct audit_entry, rule);
0346             audit_watch_log_rule_change(r, w, "remove_rule");
0347             if (e->rule.exe)
0348                 audit_remove_mark(e->rule.exe);
0349             list_del(&r->rlist);
0350             list_del(&r->list);
0351             list_del_rcu(&e->list);
0352             call_rcu(&e->rcu, audit_free_rule_rcu);
0353         }
0354         audit_remove_watch(w);
0355     }
0356     mutex_unlock(&audit_filter_mutex);
0357 
0358     fsnotify_destroy_mark(&parent->mark, audit_watch_group);
0359 }
0360 
0361 /* Get path information necessary for adding watches. */
0362 static int audit_get_nd(struct audit_watch *watch, struct path *parent)
0363 {
0364     struct dentry *d = kern_path_locked(watch->path, parent);
0365     if (IS_ERR(d))
0366         return PTR_ERR(d);
0367     inode_unlock(d_backing_inode(parent->dentry));
0368     if (d_is_positive(d)) {
0369         /* update watch filter fields */
0370         watch->dev = d->d_sb->s_dev;
0371         watch->ino = d_backing_inode(d)->i_ino;
0372     }
0373     dput(d);
0374     return 0;
0375 }
0376 
0377 /* Associate the given rule with an existing parent.
0378  * Caller must hold audit_filter_mutex. */
0379 static void audit_add_to_parent(struct audit_krule *krule,
0380                 struct audit_parent *parent)
0381 {
0382     struct audit_watch *w, *watch = krule->watch;
0383     int watch_found = 0;
0384 
0385     BUG_ON(!mutex_is_locked(&audit_filter_mutex));
0386 
0387     list_for_each_entry(w, &parent->watches, wlist) {
0388         if (strcmp(watch->path, w->path))
0389             continue;
0390 
0391         watch_found = 1;
0392 
0393         /* put krule's ref to temporary watch */
0394         audit_put_watch(watch);
0395 
0396         audit_get_watch(w);
0397         krule->watch = watch = w;
0398 
0399         audit_put_parent(parent);
0400         break;
0401     }
0402 
0403     if (!watch_found) {
0404         watch->parent = parent;
0405 
0406         audit_get_watch(watch);
0407         list_add(&watch->wlist, &parent->watches);
0408     }
0409     list_add(&krule->rlist, &watch->rules);
0410 }
0411 
0412 /* Find a matching watch entry, or add this one.
0413  * Caller must hold audit_filter_mutex. */
0414 int audit_add_watch(struct audit_krule *krule, struct list_head **list)
0415 {
0416     struct audit_watch *watch = krule->watch;
0417     struct audit_parent *parent;
0418     struct path parent_path;
0419     int h, ret = 0;
0420 
0421     mutex_unlock(&audit_filter_mutex);
0422 
0423     /* Avoid calling path_lookup under audit_filter_mutex. */
0424     ret = audit_get_nd(watch, &parent_path);
0425 
0426     /* caller expects mutex locked */
0427     mutex_lock(&audit_filter_mutex);
0428 
0429     if (ret)
0430         return ret;
0431 
0432     /* either find an old parent or attach a new one */
0433     parent = audit_find_parent(d_backing_inode(parent_path.dentry));
0434     if (!parent) {
0435         parent = audit_init_parent(&parent_path);
0436         if (IS_ERR(parent)) {
0437             ret = PTR_ERR(parent);
0438             goto error;
0439         }
0440     }
0441 
0442     audit_add_to_parent(krule, parent);
0443 
0444     h = audit_hash_ino((u32)watch->ino);
0445     *list = &audit_inode_hash[h];
0446 error:
0447     path_put(&parent_path);
0448     return ret;
0449 }
0450 
0451 void audit_remove_watch_rule(struct audit_krule *krule)
0452 {
0453     struct audit_watch *watch = krule->watch;
0454     struct audit_parent *parent = watch->parent;
0455 
0456     list_del(&krule->rlist);
0457 
0458     if (list_empty(&watch->rules)) {
0459         audit_remove_watch(watch);
0460 
0461         if (list_empty(&parent->watches)) {
0462             audit_get_parent(parent);
0463             fsnotify_destroy_mark(&parent->mark, audit_watch_group);
0464             audit_put_parent(parent);
0465         }
0466     }
0467 }
0468 
0469 /* Update watch data in audit rules based on fsnotify events. */
0470 static int audit_watch_handle_event(struct fsnotify_group *group,
0471                     struct inode *to_tell,
0472                     struct fsnotify_mark *inode_mark,
0473                     struct fsnotify_mark *vfsmount_mark,
0474                     u32 mask, const void *data, int data_type,
0475                     const unsigned char *dname, u32 cookie)
0476 {
0477     const struct inode *inode;
0478     struct audit_parent *parent;
0479 
0480     parent = container_of(inode_mark, struct audit_parent, mark);
0481 
0482     BUG_ON(group != audit_watch_group);
0483 
0484     switch (data_type) {
0485     case (FSNOTIFY_EVENT_PATH):
0486         inode = d_backing_inode(((const struct path *)data)->dentry);
0487         break;
0488     case (FSNOTIFY_EVENT_INODE):
0489         inode = (const struct inode *)data;
0490         break;
0491     default:
0492         BUG();
0493         inode = NULL;
0494         break;
0495     };
0496 
0497     if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
0498         audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
0499     else if (mask & (FS_DELETE|FS_MOVED_FROM))
0500         audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
0501     else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
0502         audit_remove_parent_watches(parent);
0503 
0504     return 0;
0505 }
0506 
0507 static const struct fsnotify_ops audit_watch_fsnotify_ops = {
0508     .handle_event =     audit_watch_handle_event,
0509 };
0510 
0511 static int __init audit_watch_init(void)
0512 {
0513     audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
0514     if (IS_ERR(audit_watch_group)) {
0515         audit_watch_group = NULL;
0516         audit_panic("cannot create audit fsnotify group");
0517     }
0518     return 0;
0519 }
0520 device_initcall(audit_watch_init);
0521 
0522 int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
0523 {
0524     struct audit_fsnotify_mark *audit_mark;
0525     char *pathname;
0526 
0527     pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
0528     if (!pathname)
0529         return -ENOMEM;
0530 
0531     audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
0532     if (IS_ERR(audit_mark)) {
0533         kfree(pathname);
0534         return PTR_ERR(audit_mark);
0535     }
0536     new->exe = audit_mark;
0537 
0538     return 0;
0539 }
0540 
0541 int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
0542 {
0543     struct file *exe_file;
0544     unsigned long ino;
0545     dev_t dev;
0546 
0547     exe_file = get_task_exe_file(tsk);
0548     if (!exe_file)
0549         return 0;
0550     ino = file_inode(exe_file)->i_ino;
0551     dev = file_inode(exe_file)->i_sb->s_dev;
0552     fput(exe_file);
0553     return audit_mark_compare(mark, ino, dev);
0554 }