diff options
Diffstat (limited to 'kernel/auditfilter.c')
| -rw-r--r-- | kernel/auditfilter.c | 518 |
1 files changed, 22 insertions, 496 deletions
diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c index 713098ee5a02..a70604047f3c 100644 --- a/kernel/auditfilter.c +++ b/kernel/auditfilter.c | |||
| @@ -27,7 +27,6 @@ | |||
| 27 | #include <linux/namei.h> | 27 | #include <linux/namei.h> |
| 28 | #include <linux/netlink.h> | 28 | #include <linux/netlink.h> |
| 29 | #include <linux/sched.h> | 29 | #include <linux/sched.h> |
| 30 | #include <linux/inotify.h> | ||
| 31 | #include <linux/security.h> | 30 | #include <linux/security.h> |
| 32 | #include "audit.h" | 31 | #include "audit.h" |
| 33 | 32 | ||
| @@ -44,36 +43,6 @@ | |||
| 44 | * be written directly provided audit_filter_mutex is held. | 43 | * be written directly provided audit_filter_mutex is held. |
| 45 | */ | 44 | */ |
| 46 | 45 | ||
| 47 | /* | ||
| 48 | * Reference counting: | ||
| 49 | * | ||
| 50 | * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED | ||
| 51 | * event. Each audit_watch holds a reference to its associated parent. | ||
| 52 | * | ||
| 53 | * audit_watch: if added to lists, lifetime is from audit_init_watch() to | ||
| 54 | * audit_remove_watch(). Additionally, an audit_watch may exist | ||
| 55 | * temporarily to assist in searching existing filter data. Each | ||
| 56 | * audit_krule holds a reference to its associated watch. | ||
| 57 | */ | ||
| 58 | |||
| 59 | struct audit_parent { | ||
| 60 | struct list_head ilist; /* entry in inotify registration list */ | ||
| 61 | struct list_head watches; /* associated watches */ | ||
| 62 | struct inotify_watch wdata; /* inotify watch data */ | ||
| 63 | unsigned flags; /* status flags */ | ||
| 64 | }; | ||
| 65 | |||
| 66 | /* | ||
| 67 | * audit_parent status flags: | ||
| 68 | * | ||
| 69 | * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to | ||
| 70 | * a filesystem event to ensure we're adding audit watches to a valid parent. | ||
| 71 | * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot | ||
| 72 | * receive them while we have nameidata, but must be used for IN_MOVE_SELF which | ||
| 73 | * we can receive while holding nameidata. | ||
| 74 | */ | ||
| 75 | #define AUDIT_PARENT_INVALID 0x001 | ||
| 76 | |||
| 77 | /* Audit filter lists, defined in <linux/audit.h> */ | 46 | /* Audit filter lists, defined in <linux/audit.h> */ |
| 78 | struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { | 47 | struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
| 79 | LIST_HEAD_INIT(audit_filter_list[0]), | 48 | LIST_HEAD_INIT(audit_filter_list[0]), |
| @@ -97,41 +66,6 @@ static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = { | |||
| 97 | 66 | ||
| 98 | DEFINE_MUTEX(audit_filter_mutex); | 67 | DEFINE_MUTEX(audit_filter_mutex); |
| 99 | 68 | ||
| 100 | /* Inotify events we care about. */ | ||
| 101 | #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF | ||
| 102 | |||
| 103 | void audit_free_parent(struct inotify_watch *i_watch) | ||
| 104 | { | ||
| 105 | struct audit_parent *parent; | ||
| 106 | |||
| 107 | parent = container_of(i_watch, struct audit_parent, wdata); | ||
| 108 | WARN_ON(!list_empty(&parent->watches)); | ||
| 109 | kfree(parent); | ||
| 110 | } | ||
| 111 | |||
| 112 | static inline void audit_get_watch(struct audit_watch *watch) | ||
| 113 | { | ||
| 114 | atomic_inc(&watch->count); | ||
| 115 | } | ||
| 116 | |||
| 117 | static void audit_put_watch(struct audit_watch *watch) | ||
| 118 | { | ||
| 119 | if (atomic_dec_and_test(&watch->count)) { | ||
| 120 | WARN_ON(watch->parent); | ||
| 121 | WARN_ON(!list_empty(&watch->rules)); | ||
| 122 | kfree(watch->path); | ||
| 123 | kfree(watch); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | static void audit_remove_watch(struct audit_watch *watch) | ||
| 128 | { | ||
| 129 | list_del(&watch->wlist); | ||
| 130 | put_inotify_watch(&watch->parent->wdata); | ||
| 131 | watch->parent = NULL; | ||
| 132 | audit_put_watch(watch); /* match initial get */ | ||
| 133 | } | ||
| 134 | |||
| 135 | static inline void audit_free_rule(struct audit_entry *e) | 69 | static inline void audit_free_rule(struct audit_entry *e) |
| 136 | { | 70 | { |
| 137 | int i; | 71 | int i; |
| @@ -156,50 +90,6 @@ void audit_free_rule_rcu(struct rcu_head *head) | |||
| 156 | audit_free_rule(e); | 90 | audit_free_rule(e); |
| 157 | } | 91 | } |
| 158 | 92 | ||
| 159 | /* Initialize a parent watch entry. */ | ||
| 160 | static struct audit_parent *audit_init_parent(struct nameidata *ndp) | ||
| 161 | { | ||
| 162 | struct audit_parent *parent; | ||
| 163 | s32 wd; | ||
| 164 | |||
| 165 | parent = kzalloc(sizeof(*parent), GFP_KERNEL); | ||
| 166 | if (unlikely(!parent)) | ||
| 167 | return ERR_PTR(-ENOMEM); | ||
| 168 | |||
| 169 | INIT_LIST_HEAD(&parent->watches); | ||
| 170 | parent->flags = 0; | ||
| 171 | |||
| 172 | inotify_init_watch(&parent->wdata); | ||
| 173 | /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */ | ||
| 174 | get_inotify_watch(&parent->wdata); | ||
| 175 | wd = inotify_add_watch(audit_ih, &parent->wdata, | ||
| 176 | ndp->path.dentry->d_inode, AUDIT_IN_WATCH); | ||
| 177 | if (wd < 0) { | ||
| 178 | audit_free_parent(&parent->wdata); | ||
| 179 | return ERR_PTR(wd); | ||
| 180 | } | ||
| 181 | |||
| 182 | return parent; | ||
| 183 | } | ||
| 184 | |||
| 185 | /* Initialize a watch entry. */ | ||
| 186 | static struct audit_watch *audit_init_watch(char *path) | ||
| 187 | { | ||
| 188 | struct audit_watch *watch; | ||
| 189 | |||
| 190 | watch = kzalloc(sizeof(*watch), GFP_KERNEL); | ||
| 191 | if (unlikely(!watch)) | ||
| 192 | return ERR_PTR(-ENOMEM); | ||
| 193 | |||
| 194 | INIT_LIST_HEAD(&watch->rules); | ||
| 195 | atomic_set(&watch->count, 1); | ||
| 196 | watch->path = path; | ||
| 197 | watch->dev = (dev_t)-1; | ||
| 198 | watch->ino = (unsigned long)-1; | ||
| 199 | |||
| 200 | return watch; | ||
| 201 | } | ||
| 202 | |||
| 203 | /* Initialize an audit filterlist entry. */ | 93 | /* Initialize an audit filterlist entry. */ |
| 204 | static inline struct audit_entry *audit_init_entry(u32 field_count) | 94 | static inline struct audit_entry *audit_init_entry(u32 field_count) |
| 205 | { | 95 | { |
| @@ -260,31 +150,6 @@ static inline int audit_to_inode(struct audit_krule *krule, | |||
| 260 | return 0; | 150 | return 0; |
| 261 | } | 151 | } |
| 262 | 152 | ||
| 263 | /* Translate a watch string to kernel respresentation. */ | ||
| 264 | static int audit_to_watch(struct audit_krule *krule, char *path, int len, | ||
| 265 | u32 op) | ||
| 266 | { | ||
| 267 | struct audit_watch *watch; | ||
| 268 | |||
| 269 | if (!audit_ih) | ||
| 270 | return -EOPNOTSUPP; | ||
| 271 | |||
| 272 | if (path[0] != '/' || path[len-1] == '/' || | ||
| 273 | krule->listnr != AUDIT_FILTER_EXIT || | ||
| 274 | op != Audit_equal || | ||
| 275 | krule->inode_f || krule->watch || krule->tree) | ||
| 276 | return -EINVAL; | ||
| 277 | |||
| 278 | watch = audit_init_watch(path); | ||
| 279 | if (IS_ERR(watch)) | ||
| 280 | return PTR_ERR(watch); | ||
| 281 | |||
| 282 | audit_get_watch(watch); | ||
| 283 | krule->watch = watch; | ||
| 284 | |||
| 285 | return 0; | ||
| 286 | } | ||
| 287 | |||
| 288 | static __u32 *classes[AUDIT_SYSCALL_CLASSES]; | 153 | static __u32 *classes[AUDIT_SYSCALL_CLASSES]; |
| 289 | 154 | ||
| 290 | int __init audit_register_class(int class, unsigned *list) | 155 | int __init audit_register_class(int class, unsigned *list) |
| @@ -766,7 +631,8 @@ static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule) | |||
| 766 | break; | 631 | break; |
| 767 | case AUDIT_WATCH: | 632 | case AUDIT_WATCH: |
| 768 | data->buflen += data->values[i] = | 633 | data->buflen += data->values[i] = |
| 769 | audit_pack_string(&bufp, krule->watch->path); | 634 | audit_pack_string(&bufp, |
| 635 | audit_watch_path(krule->watch)); | ||
| 770 | break; | 636 | break; |
| 771 | case AUDIT_DIR: | 637 | case AUDIT_DIR: |
| 772 | data->buflen += data->values[i] = | 638 | data->buflen += data->values[i] = |
| @@ -818,7 +684,8 @@ static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b) | |||
| 818 | return 1; | 684 | return 1; |
| 819 | break; | 685 | break; |
| 820 | case AUDIT_WATCH: | 686 | case AUDIT_WATCH: |
| 821 | if (strcmp(a->watch->path, b->watch->path)) | 687 | if (strcmp(audit_watch_path(a->watch), |
| 688 | audit_watch_path(b->watch))) | ||
| 822 | return 1; | 689 | return 1; |
| 823 | break; | 690 | break; |
| 824 | case AUDIT_DIR: | 691 | case AUDIT_DIR: |
| @@ -844,32 +711,6 @@ static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b) | |||
| 844 | return 0; | 711 | return 0; |
| 845 | } | 712 | } |
| 846 | 713 | ||
| 847 | /* Duplicate the given audit watch. The new watch's rules list is initialized | ||
| 848 | * to an empty list and wlist is undefined. */ | ||
| 849 | static struct audit_watch *audit_dupe_watch(struct audit_watch *old) | ||
| 850 | { | ||
| 851 | char *path; | ||
| 852 | struct audit_watch *new; | ||
| 853 | |||
| 854 | path = kstrdup(old->path, GFP_KERNEL); | ||
| 855 | if (unlikely(!path)) | ||
| 856 | return ERR_PTR(-ENOMEM); | ||
| 857 | |||
| 858 | new = audit_init_watch(path); | ||
| 859 | if (IS_ERR(new)) { | ||
| 860 | kfree(path); | ||
| 861 | goto out; | ||
| 862 | } | ||
| 863 | |||
| 864 | new->dev = old->dev; | ||
| 865 | new->ino = old->ino; | ||
| 866 | get_inotify_watch(&old->parent->wdata); | ||
| 867 | new->parent = old->parent; | ||
| 868 | |||
| 869 | out: | ||
| 870 | return new; | ||
| 871 | } | ||
| 872 | |||
| 873 | /* Duplicate LSM field information. The lsm_rule is opaque, so must be | 714 | /* Duplicate LSM field information. The lsm_rule is opaque, so must be |
| 874 | * re-initialized. */ | 715 | * re-initialized. */ |
| 875 | static inline int audit_dupe_lsm_field(struct audit_field *df, | 716 | static inline int audit_dupe_lsm_field(struct audit_field *df, |
| @@ -904,8 +745,8 @@ static inline int audit_dupe_lsm_field(struct audit_field *df, | |||
| 904 | * rule with the new rule in the filterlist, then free the old rule. | 745 | * rule with the new rule in the filterlist, then free the old rule. |
| 905 | * The rlist element is undefined; list manipulations are handled apart from | 746 | * The rlist element is undefined; list manipulations are handled apart from |
| 906 | * the initial copy. */ | 747 | * the initial copy. */ |
| 907 | static struct audit_entry *audit_dupe_rule(struct audit_krule *old, | 748 | struct audit_entry *audit_dupe_rule(struct audit_krule *old, |
| 908 | struct audit_watch *watch) | 749 | struct audit_watch *watch) |
| 909 | { | 750 | { |
| 910 | u32 fcount = old->field_count; | 751 | u32 fcount = old->field_count; |
| 911 | struct audit_entry *entry; | 752 | struct audit_entry *entry; |
| @@ -977,137 +818,6 @@ static struct audit_entry *audit_dupe_rule(struct audit_krule *old, | |||
| 977 | return entry; | 818 | return entry; |
| 978 | } | 819 | } |
| 979 | 820 | ||
| 980 | /* Update inode info in audit rules based on filesystem event. */ | ||
| 981 | static void audit_update_watch(struct audit_parent *parent, | ||
| 982 | const char *dname, dev_t dev, | ||
| 983 | unsigned long ino, unsigned invalidating) | ||
| 984 | { | ||
| 985 | struct audit_watch *owatch, *nwatch, *nextw; | ||
| 986 | struct audit_krule *r, *nextr; | ||
| 987 | struct audit_entry *oentry, *nentry; | ||
| 988 | |||
| 989 | mutex_lock(&audit_filter_mutex); | ||
| 990 | list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) { | ||
| 991 | if (audit_compare_dname_path(dname, owatch->path, NULL)) | ||
| 992 | continue; | ||
| 993 | |||
| 994 | /* If the update involves invalidating rules, do the inode-based | ||
| 995 | * filtering now, so we don't omit records. */ | ||
| 996 | if (invalidating && current->audit_context) | ||
| 997 | audit_filter_inodes(current, current->audit_context); | ||
| 998 | |||
| 999 | nwatch = audit_dupe_watch(owatch); | ||
| 1000 | if (IS_ERR(nwatch)) { | ||
| 1001 | mutex_unlock(&audit_filter_mutex); | ||
| 1002 | audit_panic("error updating watch, skipping"); | ||
| 1003 | return; | ||
| 1004 | } | ||
| 1005 | nwatch->dev = dev; | ||
| 1006 | nwatch->ino = ino; | ||
| 1007 | |||
| 1008 | list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) { | ||
| 1009 | |||
| 1010 | oentry = container_of(r, struct audit_entry, rule); | ||
| 1011 | list_del(&oentry->rule.rlist); | ||
| 1012 | list_del_rcu(&oentry->list); | ||
| 1013 | |||
| 1014 | nentry = audit_dupe_rule(&oentry->rule, nwatch); | ||
| 1015 | if (IS_ERR(nentry)) { | ||
| 1016 | list_del(&oentry->rule.list); | ||
| 1017 | audit_panic("error updating watch, removing"); | ||
| 1018 | } else { | ||
| 1019 | int h = audit_hash_ino((u32)ino); | ||
| 1020 | list_add(&nentry->rule.rlist, &nwatch->rules); | ||
| 1021 | list_add_rcu(&nentry->list, &audit_inode_hash[h]); | ||
| 1022 | list_replace(&oentry->rule.list, | ||
| 1023 | &nentry->rule.list); | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | call_rcu(&oentry->rcu, audit_free_rule_rcu); | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | if (audit_enabled) { | ||
| 1030 | struct audit_buffer *ab; | ||
| 1031 | ab = audit_log_start(NULL, GFP_NOFS, | ||
| 1032 | AUDIT_CONFIG_CHANGE); | ||
| 1033 | audit_log_format(ab, "auid=%u ses=%u", | ||
| 1034 | audit_get_loginuid(current), | ||
| 1035 | audit_get_sessionid(current)); | ||
| 1036 | audit_log_format(ab, | ||
| 1037 | " op=updated rules specifying path="); | ||
| 1038 | audit_log_untrustedstring(ab, owatch->path); | ||
| 1039 | audit_log_format(ab, " with dev=%u ino=%lu\n", | ||
| 1040 | dev, ino); | ||
| 1041 | audit_log_format(ab, " list=%d res=1", r->listnr); | ||
| 1042 | audit_log_end(ab); | ||
| 1043 | } | ||
| 1044 | audit_remove_watch(owatch); | ||
| 1045 | goto add_watch_to_parent; /* event applies to a single watch */ | ||
| 1046 | } | ||
| 1047 | mutex_unlock(&audit_filter_mutex); | ||
| 1048 | return; | ||
| 1049 | |||
| 1050 | add_watch_to_parent: | ||
| 1051 | list_add(&nwatch->wlist, &parent->watches); | ||
| 1052 | mutex_unlock(&audit_filter_mutex); | ||
| 1053 | return; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | /* Remove all watches & rules associated with a parent that is going away. */ | ||
| 1057 | static void audit_remove_parent_watches(struct audit_parent *parent) | ||
| 1058 | { | ||
| 1059 | struct audit_watch *w, *nextw; | ||
| 1060 | struct audit_krule *r, *nextr; | ||
| 1061 | struct audit_entry *e; | ||
| 1062 | |||
| 1063 | mutex_lock(&audit_filter_mutex); | ||
| 1064 | parent->flags |= AUDIT_PARENT_INVALID; | ||
| 1065 | list_for_each_entry_safe(w, nextw, &parent->watches, wlist) { | ||
| 1066 | list_for_each_entry_safe(r, nextr, &w->rules, rlist) { | ||
| 1067 | e = container_of(r, struct audit_entry, rule); | ||
| 1068 | if (audit_enabled) { | ||
| 1069 | struct audit_buffer *ab; | ||
| 1070 | ab = audit_log_start(NULL, GFP_NOFS, | ||
| 1071 | AUDIT_CONFIG_CHANGE); | ||
| 1072 | audit_log_format(ab, "auid=%u ses=%u", | ||
| 1073 | audit_get_loginuid(current), | ||
| 1074 | audit_get_sessionid(current)); | ||
| 1075 | audit_log_format(ab, " op=remove rule path="); | ||
| 1076 | audit_log_untrustedstring(ab, w->path); | ||
| 1077 | if (r->filterkey) { | ||
| 1078 | audit_log_format(ab, " key="); | ||
| 1079 | audit_log_untrustedstring(ab, | ||
| 1080 | r->filterkey); | ||
| 1081 | } else | ||
| 1082 | audit_log_format(ab, " key=(null)"); | ||
| 1083 | audit_log_format(ab, " list=%d res=1", | ||
| 1084 | r->listnr); | ||
| 1085 | audit_log_end(ab); | ||
| 1086 | } | ||
| 1087 | list_del(&r->rlist); | ||
| 1088 | list_del(&r->list); | ||
| 1089 | list_del_rcu(&e->list); | ||
| 1090 | call_rcu(&e->rcu, audit_free_rule_rcu); | ||
| 1091 | } | ||
| 1092 | audit_remove_watch(w); | ||
| 1093 | } | ||
| 1094 | mutex_unlock(&audit_filter_mutex); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | /* Unregister inotify watches for parents on in_list. | ||
| 1098 | * Generates an IN_IGNORED event. */ | ||
| 1099 | static void audit_inotify_unregister(struct list_head *in_list) | ||
| 1100 | { | ||
| 1101 | struct audit_parent *p, *n; | ||
| 1102 | |||
| 1103 | list_for_each_entry_safe(p, n, in_list, ilist) { | ||
| 1104 | list_del(&p->ilist); | ||
| 1105 | inotify_rm_watch(audit_ih, &p->wdata); | ||
| 1106 | /* the unpin matching the pin in audit_do_del_rule() */ | ||
| 1107 | unpin_inotify_watch(&p->wdata); | ||
| 1108 | } | ||
| 1109 | } | ||
| 1110 | |||
| 1111 | /* Find an existing audit rule. | 821 | /* Find an existing audit rule. |
| 1112 | * Caller must hold audit_filter_mutex to prevent stale rule data. */ | 822 | * Caller must hold audit_filter_mutex to prevent stale rule data. */ |
| 1113 | static struct audit_entry *audit_find_rule(struct audit_entry *entry, | 823 | static struct audit_entry *audit_find_rule(struct audit_entry *entry, |
| @@ -1145,134 +855,6 @@ out: | |||
| 1145 | return found; | 855 | return found; |
| 1146 | } | 856 | } |
| 1147 | 857 | ||
| 1148 | /* Get path information necessary for adding watches. */ | ||
| 1149 | static int audit_get_nd(char *path, struct nameidata **ndp, | ||
| 1150 | struct nameidata **ndw) | ||
| 1151 | { | ||
| 1152 | struct nameidata *ndparent, *ndwatch; | ||
| 1153 | int err; | ||
| 1154 | |||
| 1155 | ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL); | ||
| 1156 | if (unlikely(!ndparent)) | ||
| 1157 | return -ENOMEM; | ||
| 1158 | |||
| 1159 | ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL); | ||
| 1160 | if (unlikely(!ndwatch)) { | ||
| 1161 | kfree(ndparent); | ||
| 1162 | return -ENOMEM; | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | err = path_lookup(path, LOOKUP_PARENT, ndparent); | ||
| 1166 | if (err) { | ||
| 1167 | kfree(ndparent); | ||
| 1168 | kfree(ndwatch); | ||
| 1169 | return err; | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | err = path_lookup(path, 0, ndwatch); | ||
| 1173 | if (err) { | ||
| 1174 | kfree(ndwatch); | ||
| 1175 | ndwatch = NULL; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | *ndp = ndparent; | ||
| 1179 | *ndw = ndwatch; | ||
| 1180 | |||
| 1181 | return 0; | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | /* Release resources used for watch path information. */ | ||
| 1185 | static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw) | ||
| 1186 | { | ||
| 1187 | if (ndp) { | ||
| 1188 | path_put(&ndp->path); | ||
| 1189 | kfree(ndp); | ||
| 1190 | } | ||
| 1191 | if (ndw) { | ||
| 1192 | path_put(&ndw->path); | ||
| 1193 | kfree(ndw); | ||
| 1194 | } | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | /* Associate the given rule with an existing parent inotify_watch. | ||
| 1198 | * Caller must hold audit_filter_mutex. */ | ||
| 1199 | static void audit_add_to_parent(struct audit_krule *krule, | ||
| 1200 | struct audit_parent *parent) | ||
| 1201 | { | ||
| 1202 | struct audit_watch *w, *watch = krule->watch; | ||
| 1203 | int watch_found = 0; | ||
| 1204 | |||
| 1205 | list_for_each_entry(w, &parent->watches, wlist) { | ||
| 1206 | if (strcmp(watch->path, w->path)) | ||
| 1207 | continue; | ||
| 1208 | |||
| 1209 | watch_found = 1; | ||
| 1210 | |||
| 1211 | /* put krule's and initial refs to temporary watch */ | ||
| 1212 | audit_put_watch(watch); | ||
| 1213 | audit_put_watch(watch); | ||
| 1214 | |||
| 1215 | audit_get_watch(w); | ||
| 1216 | krule->watch = watch = w; | ||
| 1217 | break; | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | if (!watch_found) { | ||
| 1221 | get_inotify_watch(&parent->wdata); | ||
| 1222 | watch->parent = parent; | ||
| 1223 | |||
| 1224 | list_add(&watch->wlist, &parent->watches); | ||
| 1225 | } | ||
| 1226 | list_add(&krule->rlist, &watch->rules); | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | /* Find a matching watch entry, or add this one. | ||
| 1230 | * Caller must hold audit_filter_mutex. */ | ||
| 1231 | static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp, | ||
| 1232 | struct nameidata *ndw) | ||
| 1233 | { | ||
| 1234 | struct audit_watch *watch = krule->watch; | ||
| 1235 | struct inotify_watch *i_watch; | ||
| 1236 | struct audit_parent *parent; | ||
| 1237 | int ret = 0; | ||
| 1238 | |||
| 1239 | /* update watch filter fields */ | ||
| 1240 | if (ndw) { | ||
| 1241 | watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev; | ||
| 1242 | watch->ino = ndw->path.dentry->d_inode->i_ino; | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | /* The audit_filter_mutex must not be held during inotify calls because | ||
| 1246 | * we hold it during inotify event callback processing. If an existing | ||
| 1247 | * inotify watch is found, inotify_find_watch() grabs a reference before | ||
| 1248 | * returning. | ||
| 1249 | */ | ||
| 1250 | mutex_unlock(&audit_filter_mutex); | ||
| 1251 | |||
| 1252 | if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode, | ||
| 1253 | &i_watch) < 0) { | ||
| 1254 | parent = audit_init_parent(ndp); | ||
| 1255 | if (IS_ERR(parent)) { | ||
| 1256 | /* caller expects mutex locked */ | ||
| 1257 | mutex_lock(&audit_filter_mutex); | ||
| 1258 | return PTR_ERR(parent); | ||
| 1259 | } | ||
| 1260 | } else | ||
| 1261 | parent = container_of(i_watch, struct audit_parent, wdata); | ||
| 1262 | |||
| 1263 | mutex_lock(&audit_filter_mutex); | ||
| 1264 | |||
| 1265 | /* parent was moved before we took audit_filter_mutex */ | ||
| 1266 | if (parent->flags & AUDIT_PARENT_INVALID) | ||
| 1267 | ret = -ENOENT; | ||
| 1268 | else | ||
| 1269 | audit_add_to_parent(krule, parent); | ||
| 1270 | |||
| 1271 | /* match get in audit_init_parent or inotify_find_watch */ | ||
| 1272 | put_inotify_watch(&parent->wdata); | ||
| 1273 | return ret; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | static u64 prio_low = ~0ULL/2; | 858 | static u64 prio_low = ~0ULL/2; |
| 1277 | static u64 prio_high = ~0ULL/2 - 1; | 859 | static u64 prio_high = ~0ULL/2 - 1; |
| 1278 | 860 | ||
| @@ -1282,7 +864,6 @@ static inline int audit_add_rule(struct audit_entry *entry) | |||
| 1282 | struct audit_entry *e; | 864 | struct audit_entry *e; |
| 1283 | struct audit_watch *watch = entry->rule.watch; | 865 | struct audit_watch *watch = entry->rule.watch; |
| 1284 | struct audit_tree *tree = entry->rule.tree; | 866 | struct audit_tree *tree = entry->rule.tree; |
| 1285 | struct nameidata *ndp = NULL, *ndw = NULL; | ||
| 1286 | struct list_head *list; | 867 | struct list_head *list; |
| 1287 | int h, err; | 868 | int h, err; |
| 1288 | #ifdef CONFIG_AUDITSYSCALL | 869 | #ifdef CONFIG_AUDITSYSCALL |
| @@ -1296,8 +877,8 @@ static inline int audit_add_rule(struct audit_entry *entry) | |||
| 1296 | 877 | ||
| 1297 | mutex_lock(&audit_filter_mutex); | 878 | mutex_lock(&audit_filter_mutex); |
| 1298 | e = audit_find_rule(entry, &list); | 879 | e = audit_find_rule(entry, &list); |
| 1299 | mutex_unlock(&audit_filter_mutex); | ||
| 1300 | if (e) { | 880 | if (e) { |
| 881 | mutex_unlock(&audit_filter_mutex); | ||
| 1301 | err = -EEXIST; | 882 | err = -EEXIST; |
| 1302 | /* normally audit_add_tree_rule() will free it on failure */ | 883 | /* normally audit_add_tree_rule() will free it on failure */ |
| 1303 | if (tree) | 884 | if (tree) |
| @@ -1305,22 +886,16 @@ static inline int audit_add_rule(struct audit_entry *entry) | |||
| 1305 | goto error; | 886 | goto error; |
| 1306 | } | 887 | } |
| 1307 | 888 | ||
| 1308 | /* Avoid calling path_lookup under audit_filter_mutex. */ | ||
| 1309 | if (watch) { | ||
| 1310 | err = audit_get_nd(watch->path, &ndp, &ndw); | ||
| 1311 | if (err) | ||
| 1312 | goto error; | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | mutex_lock(&audit_filter_mutex); | ||
| 1316 | if (watch) { | 889 | if (watch) { |
| 1317 | /* audit_filter_mutex is dropped and re-taken during this call */ | 890 | /* audit_filter_mutex is dropped and re-taken during this call */ |
| 1318 | err = audit_add_watch(&entry->rule, ndp, ndw); | 891 | err = audit_add_watch(&entry->rule); |
| 1319 | if (err) { | 892 | if (err) { |
| 1320 | mutex_unlock(&audit_filter_mutex); | 893 | mutex_unlock(&audit_filter_mutex); |
| 1321 | goto error; | 894 | goto error; |
| 1322 | } | 895 | } |
| 1323 | h = audit_hash_ino((u32)watch->ino); | 896 | /* entry->rule.watch may have changed during audit_add_watch() */ |
| 897 | watch = entry->rule.watch; | ||
| 898 | h = audit_hash_ino((u32)audit_watch_inode(watch)); | ||
| 1324 | list = &audit_inode_hash[h]; | 899 | list = &audit_inode_hash[h]; |
| 1325 | } | 900 | } |
| 1326 | if (tree) { | 901 | if (tree) { |
| @@ -1358,11 +933,9 @@ static inline int audit_add_rule(struct audit_entry *entry) | |||
| 1358 | #endif | 933 | #endif |
| 1359 | mutex_unlock(&audit_filter_mutex); | 934 | mutex_unlock(&audit_filter_mutex); |
| 1360 | 935 | ||
| 1361 | audit_put_nd(ndp, ndw); /* NULL args OK */ | ||
| 1362 | return 0; | 936 | return 0; |
| 1363 | 937 | ||
| 1364 | error: | 938 | error: |
| 1365 | audit_put_nd(ndp, ndw); /* NULL args OK */ | ||
| 1366 | if (watch) | 939 | if (watch) |
| 1367 | audit_put_watch(watch); /* tmp watch, matches initial get */ | 940 | audit_put_watch(watch); /* tmp watch, matches initial get */ |
| 1368 | return err; | 941 | return err; |
| @@ -1372,7 +945,7 @@ error: | |||
| 1372 | static inline int audit_del_rule(struct audit_entry *entry) | 945 | static inline int audit_del_rule(struct audit_entry *entry) |
| 1373 | { | 946 | { |
| 1374 | struct audit_entry *e; | 947 | struct audit_entry *e; |
| 1375 | struct audit_watch *watch, *tmp_watch = entry->rule.watch; | 948 | struct audit_watch *watch = entry->rule.watch; |
| 1376 | struct audit_tree *tree = entry->rule.tree; | 949 | struct audit_tree *tree = entry->rule.tree; |
| 1377 | struct list_head *list; | 950 | struct list_head *list; |
| 1378 | LIST_HEAD(inotify_list); | 951 | LIST_HEAD(inotify_list); |
| @@ -1394,29 +967,8 @@ static inline int audit_del_rule(struct audit_entry *entry) | |||
| 1394 | goto out; | 967 | goto out; |
| 1395 | } | 968 | } |
| 1396 | 969 | ||
| 1397 | watch = e->rule.watch; | 970 | if (e->rule.watch) |
| 1398 | if (watch) { | 971 | audit_remove_watch_rule(&e->rule, &inotify_list); |
| 1399 | struct audit_parent *parent = watch->parent; | ||
| 1400 | |||
| 1401 | list_del(&e->rule.rlist); | ||
| 1402 | |||
| 1403 | if (list_empty(&watch->rules)) { | ||
| 1404 | audit_remove_watch(watch); | ||
| 1405 | |||
| 1406 | if (list_empty(&parent->watches)) { | ||
| 1407 | /* Put parent on the inotify un-registration | ||
| 1408 | * list. Grab a reference before releasing | ||
| 1409 | * audit_filter_mutex, to be released in | ||
| 1410 | * audit_inotify_unregister(). | ||
| 1411 | * If filesystem is going away, just leave | ||
| 1412 | * the sucker alone, eviction will take | ||
| 1413 | * care of it. | ||
| 1414 | */ | ||
| 1415 | if (pin_inotify_watch(&parent->wdata)) | ||
| 1416 | list_add(&parent->ilist, &inotify_list); | ||
| 1417 | } | ||
| 1418 | } | ||
| 1419 | } | ||
| 1420 | 972 | ||
| 1421 | if (e->rule.tree) | 973 | if (e->rule.tree) |
| 1422 | audit_remove_tree_rule(&e->rule); | 974 | audit_remove_tree_rule(&e->rule); |
| @@ -1438,8 +990,8 @@ static inline int audit_del_rule(struct audit_entry *entry) | |||
| 1438 | audit_inotify_unregister(&inotify_list); | 990 | audit_inotify_unregister(&inotify_list); |
| 1439 | 991 | ||
| 1440 | out: | 992 | out: |
| 1441 | if (tmp_watch) | 993 | if (watch) |
| 1442 | audit_put_watch(tmp_watch); /* match initial get */ | 994 | audit_put_watch(watch); /* match initial get */ |
| 1443 | if (tree) | 995 | if (tree) |
| 1444 | audit_put_tree(tree); /* that's the temporary one */ | 996 | audit_put_tree(tree); /* that's the temporary one */ |
| 1445 | 997 | ||
| @@ -1527,11 +1079,9 @@ static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid, | |||
| 1527 | security_release_secctx(ctx, len); | 1079 | security_release_secctx(ctx, len); |
| 1528 | } | 1080 | } |
| 1529 | } | 1081 | } |
| 1530 | audit_log_format(ab, " op=%s rule key=", action); | 1082 | audit_log_format(ab, " op="); |
| 1531 | if (rule->filterkey) | 1083 | audit_log_string(ab, action); |
| 1532 | audit_log_untrustedstring(ab, rule->filterkey); | 1084 | audit_log_key(ab, rule->filterkey); |
| 1533 | else | ||
| 1534 | audit_log_format(ab, "(null)"); | ||
| 1535 | audit_log_format(ab, " list=%d res=%d", rule->listnr, res); | 1085 | audit_log_format(ab, " list=%d res=%d", rule->listnr, res); |
| 1536 | audit_log_end(ab); | 1086 | audit_log_end(ab); |
| 1537 | } | 1087 | } |
| @@ -1595,7 +1145,7 @@ int audit_receive_filter(int type, int pid, int uid, int seq, void *data, | |||
| 1595 | return PTR_ERR(entry); | 1145 | return PTR_ERR(entry); |
| 1596 | 1146 | ||
| 1597 | err = audit_add_rule(entry); | 1147 | err = audit_add_rule(entry); |
| 1598 | audit_log_rule_change(loginuid, sessionid, sid, "add", | 1148 | audit_log_rule_change(loginuid, sessionid, sid, "add rule", |
| 1599 | &entry->rule, !err); | 1149 | &entry->rule, !err); |
| 1600 | 1150 | ||
| 1601 | if (err) | 1151 | if (err) |
| @@ -1611,7 +1161,7 @@ int audit_receive_filter(int type, int pid, int uid, int seq, void *data, | |||
| 1611 | return PTR_ERR(entry); | 1161 | return PTR_ERR(entry); |
| 1612 | 1162 | ||
| 1613 | err = audit_del_rule(entry); | 1163 | err = audit_del_rule(entry); |
| 1614 | audit_log_rule_change(loginuid, sessionid, sid, "remove", | 1164 | audit_log_rule_change(loginuid, sessionid, sid, "remove rule", |
| 1615 | &entry->rule, !err); | 1165 | &entry->rule, !err); |
| 1616 | 1166 | ||
| 1617 | audit_free_rule(entry); | 1167 | audit_free_rule(entry); |
| @@ -1793,7 +1343,7 @@ static int update_lsm_rule(struct audit_krule *r) | |||
| 1793 | list_del(&r->list); | 1343 | list_del(&r->list); |
| 1794 | } else { | 1344 | } else { |
| 1795 | if (watch) { | 1345 | if (watch) { |
| 1796 | list_add(&nentry->rule.rlist, &watch->rules); | 1346 | list_add(&nentry->rule.rlist, audit_watch_rules(watch)); |
| 1797 | list_del(&r->rlist); | 1347 | list_del(&r->rlist); |
| 1798 | } else if (tree) | 1348 | } else if (tree) |
| 1799 | list_replace_init(&r->rlist, &nentry->rule.rlist); | 1349 | list_replace_init(&r->rlist, &nentry->rule.rlist); |
| @@ -1829,27 +1379,3 @@ int audit_update_lsm_rules(void) | |||
| 1829 | 1379 | ||
| 1830 | return err; | 1380 | return err; |
| 1831 | } | 1381 | } |
| 1832 | |||
| 1833 | /* Update watch data in audit rules based on inotify events. */ | ||
| 1834 | void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask, | ||
| 1835 | u32 cookie, const char *dname, struct inode *inode) | ||
| 1836 | { | ||
| 1837 | struct audit_parent *parent; | ||
| 1838 | |||
| 1839 | parent = container_of(i_watch, struct audit_parent, wdata); | ||
| 1840 | |||
| 1841 | if (mask & (IN_CREATE|IN_MOVED_TO) && inode) | ||
| 1842 | audit_update_watch(parent, dname, inode->i_sb->s_dev, | ||
| 1843 | inode->i_ino, 0); | ||
| 1844 | else if (mask & (IN_DELETE|IN_MOVED_FROM)) | ||
| 1845 | audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1); | ||
| 1846 | /* inotify automatically removes the watch and sends IN_IGNORED */ | ||
| 1847 | else if (mask & (IN_DELETE_SELF|IN_UNMOUNT)) | ||
| 1848 | audit_remove_parent_watches(parent); | ||
| 1849 | /* inotify does not remove the watch, so remove it manually */ | ||
| 1850 | else if(mask & IN_MOVE_SELF) { | ||
| 1851 | audit_remove_parent_watches(parent); | ||
| 1852 | inotify_remove_watch_locked(audit_ih, i_watch); | ||
| 1853 | } else if (mask & IN_IGNORED) | ||
| 1854 | put_inotify_watch(i_watch); | ||
| 1855 | } | ||
