0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/audit.h>
0011 #include <linux/kthread.h>
0012 #include <linux/mutex.h>
0013 #include <linux/fs.h>
0014 #include <linux/fsnotify_backend.h>
0015 #include <linux/namei.h>
0016 #include <linux/netlink.h>
0017 #include <linux/sched.h>
0018 #include <linux/slab.h>
0019 #include <linux/security.h>
0020 #include "audit.h"
0021
0022
0023
0024
0025
0026 struct audit_fsnotify_mark {
0027 dev_t dev;
0028 unsigned long ino;
0029 char *path;
0030 struct fsnotify_mark mark;
0031 struct audit_krule *rule;
0032 };
0033
0034
0035 static struct fsnotify_group *audit_fsnotify_group;
0036
0037
0038 #define AUDIT_FS_EVENTS (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
0039 FS_MOVE_SELF)
0040
0041 static void audit_fsnotify_mark_free(struct audit_fsnotify_mark *audit_mark)
0042 {
0043 kfree(audit_mark->path);
0044 kfree(audit_mark);
0045 }
0046
0047 static void audit_fsnotify_free_mark(struct fsnotify_mark *mark)
0048 {
0049 struct audit_fsnotify_mark *audit_mark;
0050
0051 audit_mark = container_of(mark, struct audit_fsnotify_mark, mark);
0052 audit_fsnotify_mark_free(audit_mark);
0053 }
0054
0055 char *audit_mark_path(struct audit_fsnotify_mark *mark)
0056 {
0057 return mark->path;
0058 }
0059
0060 int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long ino, dev_t dev)
0061 {
0062 if (mark->ino == AUDIT_INO_UNSET)
0063 return 0;
0064 return (mark->ino == ino) && (mark->dev == dev);
0065 }
0066
0067 static void audit_update_mark(struct audit_fsnotify_mark *audit_mark,
0068 const struct inode *inode)
0069 {
0070 audit_mark->dev = inode ? inode->i_sb->s_dev : AUDIT_DEV_UNSET;
0071 audit_mark->ino = inode ? inode->i_ino : AUDIT_INO_UNSET;
0072 }
0073
0074 struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pathname, int len)
0075 {
0076 struct audit_fsnotify_mark *audit_mark;
0077 struct path path;
0078 struct dentry *dentry;
0079 struct inode *inode;
0080 int ret;
0081
0082 if (pathname[0] != '/' || pathname[len-1] == '/')
0083 return ERR_PTR(-EINVAL);
0084
0085 dentry = kern_path_locked(pathname, &path);
0086 if (IS_ERR(dentry))
0087 return ERR_CAST(dentry);
0088 inode = path.dentry->d_inode;
0089 inode_unlock(inode);
0090
0091 audit_mark = kzalloc(sizeof(*audit_mark), GFP_KERNEL);
0092 if (unlikely(!audit_mark)) {
0093 audit_mark = ERR_PTR(-ENOMEM);
0094 goto out;
0095 }
0096
0097 fsnotify_init_mark(&audit_mark->mark, audit_fsnotify_group);
0098 audit_mark->mark.mask = AUDIT_FS_EVENTS;
0099 audit_mark->path = pathname;
0100 audit_update_mark(audit_mark, dentry->d_inode);
0101 audit_mark->rule = krule;
0102
0103 ret = fsnotify_add_inode_mark(&audit_mark->mark, inode, 0);
0104 if (ret < 0) {
0105 audit_mark->path = NULL;
0106 fsnotify_put_mark(&audit_mark->mark);
0107 audit_mark = ERR_PTR(ret);
0108 }
0109 out:
0110 dput(dentry);
0111 path_put(&path);
0112 return audit_mark;
0113 }
0114
0115 static void audit_mark_log_rule_change(struct audit_fsnotify_mark *audit_mark, char *op)
0116 {
0117 struct audit_buffer *ab;
0118 struct audit_krule *rule = audit_mark->rule;
0119
0120 if (!audit_enabled)
0121 return;
0122 ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
0123 if (unlikely(!ab))
0124 return;
0125 audit_log_session_info(ab);
0126 audit_log_format(ab, " op=%s path=", op);
0127 audit_log_untrustedstring(ab, audit_mark->path);
0128 audit_log_key(ab, rule->filterkey);
0129 audit_log_format(ab, " list=%d res=1", rule->listnr);
0130 audit_log_end(ab);
0131 }
0132
0133 void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
0134 {
0135 fsnotify_destroy_mark(&audit_mark->mark, audit_fsnotify_group);
0136 fsnotify_put_mark(&audit_mark->mark);
0137 }
0138
0139 void audit_remove_mark_rule(struct audit_krule *krule)
0140 {
0141 struct audit_fsnotify_mark *mark = krule->exe;
0142
0143 audit_remove_mark(mark);
0144 }
0145
0146 static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
0147 {
0148 struct audit_krule *rule = audit_mark->rule;
0149 struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
0150
0151 audit_mark_log_rule_change(audit_mark, "autoremove_rule");
0152 audit_del_rule(entry);
0153 }
0154
0155
0156 static int audit_mark_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
0157 struct inode *inode, struct inode *dir,
0158 const struct qstr *dname, u32 cookie)
0159 {
0160 struct audit_fsnotify_mark *audit_mark;
0161
0162 audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
0163
0164 if (WARN_ON_ONCE(inode_mark->group != audit_fsnotify_group))
0165 return 0;
0166
0167 if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
0168 if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
0169 return 0;
0170 audit_update_mark(audit_mark, inode);
0171 } else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF)) {
0172 audit_autoremove_mark_rule(audit_mark);
0173 }
0174
0175 return 0;
0176 }
0177
0178 static const struct fsnotify_ops audit_mark_fsnotify_ops = {
0179 .handle_inode_event = audit_mark_handle_event,
0180 .free_mark = audit_fsnotify_free_mark,
0181 };
0182
0183 static int __init audit_fsnotify_init(void)
0184 {
0185 audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops,
0186 FSNOTIFY_GROUP_DUPS);
0187 if (IS_ERR(audit_fsnotify_group)) {
0188 audit_fsnotify_group = NULL;
0189 audit_panic("cannot create audit fsnotify group");
0190 }
0191 return 0;
0192 }
0193 device_initcall(audit_fsnotify_init);