audit_watch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* audit_watch.c -- watching inodes
  2. *
  3. * Copyright 2003-2009 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. #include <linux/file.h>
  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/fsnotify_backend.h>
  28. #include <linux/namei.h>
  29. #include <linux/netlink.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/security.h>
  33. #include "audit.h"
  34. /*
  35. * Reference counting:
  36. *
  37. * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
  38. * event. Each audit_watch holds a reference to its associated parent.
  39. *
  40. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  41. * audit_remove_watch(). Additionally, an audit_watch may exist
  42. * temporarily to assist in searching existing filter data. Each
  43. * audit_krule holds a reference to its associated watch.
  44. */
  45. struct audit_watch {
  46. atomic_t count; /* reference count */
  47. dev_t dev; /* associated superblock device */
  48. char *path; /* insertion path */
  49. unsigned long ino; /* associated inode number */
  50. struct audit_parent *parent; /* associated parent */
  51. struct list_head wlist; /* entry in parent->watches list */
  52. struct list_head rules; /* anchor for krule->rlist */
  53. };
  54. struct audit_parent {
  55. struct list_head watches; /* anchor for audit_watch->wlist */
  56. struct fsnotify_mark mark; /* fsnotify mark on the inode */
  57. };
  58. /* fsnotify handle. */
  59. static struct fsnotify_group *audit_watch_group;
  60. /* fsnotify events we care about. */
  61. #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  62. FS_MOVE_SELF | FS_EVENT_ON_CHILD)
  63. static void audit_free_parent(struct audit_parent *parent)
  64. {
  65. WARN_ON(!list_empty(&parent->watches));
  66. kfree(parent);
  67. }
  68. static void audit_watch_free_mark(struct fsnotify_mark *entry)
  69. {
  70. struct audit_parent *parent;
  71. parent = container_of(entry, struct audit_parent, mark);
  72. audit_free_parent(parent);
  73. }
  74. static void audit_get_parent(struct audit_parent *parent)
  75. {
  76. if (likely(parent))
  77. fsnotify_get_mark(&parent->mark);
  78. }
  79. static void audit_put_parent(struct audit_parent *parent)
  80. {
  81. if (likely(parent))
  82. fsnotify_put_mark(&parent->mark);
  83. }
  84. /*
  85. * Find and return the audit_parent on the given inode. If found a reference
  86. * is taken on this parent.
  87. */
  88. static inline struct audit_parent *audit_find_parent(struct inode *inode)
  89. {
  90. struct audit_parent *parent = NULL;
  91. struct fsnotify_mark *entry;
  92. entry = fsnotify_find_inode_mark(audit_watch_group, inode);
  93. if (entry)
  94. parent = container_of(entry, struct audit_parent, mark);
  95. return parent;
  96. }
  97. void audit_get_watch(struct audit_watch *watch)
  98. {
  99. atomic_inc(&watch->count);
  100. }
  101. void audit_put_watch(struct audit_watch *watch)
  102. {
  103. if (atomic_dec_and_test(&watch->count)) {
  104. WARN_ON(watch->parent);
  105. WARN_ON(!list_empty(&watch->rules));
  106. kfree(watch->path);
  107. kfree(watch);
  108. }
  109. }
  110. static void audit_remove_watch(struct audit_watch *watch)
  111. {
  112. list_del(&watch->wlist);
  113. audit_put_parent(watch->parent);
  114. watch->parent = NULL;
  115. audit_put_watch(watch); /* match initial get */
  116. }
  117. char *audit_watch_path(struct audit_watch *watch)
  118. {
  119. return watch->path;
  120. }
  121. int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
  122. {
  123. return (watch->ino != AUDIT_INO_UNSET) &&
  124. (watch->ino == ino) &&
  125. (watch->dev == dev);
  126. }
  127. /* Initialize a parent watch entry. */
  128. static struct audit_parent *audit_init_parent(struct path *path)
  129. {
  130. struct inode *inode = d_backing_inode(path->dentry);
  131. struct audit_parent *parent;
  132. int ret;
  133. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  134. if (unlikely(!parent))
  135. return ERR_PTR(-ENOMEM);
  136. INIT_LIST_HEAD(&parent->watches);
  137. fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
  138. parent->mark.mask = AUDIT_FS_WATCH;
  139. ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
  140. if (ret < 0) {
  141. audit_free_parent(parent);
  142. return ERR_PTR(ret);
  143. }
  144. return parent;
  145. }
  146. /* Initialize a watch entry. */
  147. static struct audit_watch *audit_init_watch(char *path)
  148. {
  149. struct audit_watch *watch;
  150. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  151. if (unlikely(!watch))
  152. return ERR_PTR(-ENOMEM);
  153. INIT_LIST_HEAD(&watch->rules);
  154. atomic_set(&watch->count, 1);
  155. watch->path = path;
  156. watch->dev = AUDIT_DEV_UNSET;
  157. watch->ino = AUDIT_INO_UNSET;
  158. return watch;
  159. }
  160. /* Translate a watch string to kernel representation. */
  161. int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  162. {
  163. struct audit_watch *watch;
  164. if (!audit_watch_group)
  165. return -EOPNOTSUPP;
  166. if (path[0] != '/' || path[len-1] == '/' ||
  167. krule->listnr != AUDIT_FILTER_EXIT ||
  168. op != Audit_equal ||
  169. krule->inode_f || krule->watch || krule->tree)
  170. return -EINVAL;
  171. watch = audit_init_watch(path);
  172. if (IS_ERR(watch))
  173. return PTR_ERR(watch);
  174. krule->watch = watch;
  175. return 0;
  176. }
  177. /* Duplicate the given audit watch. The new watch's rules list is initialized
  178. * to an empty list and wlist is undefined. */
  179. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  180. {
  181. char *path;
  182. struct audit_watch *new;
  183. path = kstrdup(old->path, GFP_KERNEL);
  184. if (unlikely(!path))
  185. return ERR_PTR(-ENOMEM);
  186. new = audit_init_watch(path);
  187. if (IS_ERR(new)) {
  188. kfree(path);
  189. goto out;
  190. }
  191. new->dev = old->dev;
  192. new->ino = old->ino;
  193. audit_get_parent(old->parent);
  194. new->parent = old->parent;
  195. out:
  196. return new;
  197. }
  198. static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  199. {
  200. if (audit_enabled) {
  201. struct audit_buffer *ab;
  202. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  203. if (unlikely(!ab))
  204. return;
  205. audit_log_format(ab, "auid=%u ses=%u op=",
  206. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  207. audit_get_sessionid(current));
  208. audit_log_string(ab, op);
  209. audit_log_format(ab, " path=");
  210. audit_log_untrustedstring(ab, w->path);
  211. audit_log_key(ab, r->filterkey);
  212. audit_log_format(ab, " list=%d res=1", r->listnr);
  213. audit_log_end(ab);
  214. }
  215. }
  216. /* Update inode info in audit rules based on filesystem event. */
  217. static void audit_update_watch(struct audit_parent *parent,
  218. const char *dname, dev_t dev,
  219. unsigned long ino, unsigned invalidating)
  220. {
  221. struct audit_watch *owatch, *nwatch, *nextw;
  222. struct audit_krule *r, *nextr;
  223. struct audit_entry *oentry, *nentry;
  224. mutex_lock(&audit_filter_mutex);
  225. /* Run all of the watches on this parent looking for the one that
  226. * matches the given dname */
  227. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  228. if (audit_compare_dname_path(dname, owatch->path,
  229. AUDIT_NAME_FULL))
  230. continue;
  231. /* If the update involves invalidating rules, do the inode-based
  232. * filtering now, so we don't omit records. */
  233. if (invalidating && !audit_dummy_context())
  234. audit_filter_inodes(current, current->audit_context);
  235. /* updating ino will likely change which audit_hash_list we
  236. * are on so we need a new watch for the new list */
  237. nwatch = audit_dupe_watch(owatch);
  238. if (IS_ERR(nwatch)) {
  239. mutex_unlock(&audit_filter_mutex);
  240. audit_panic("error updating watch, skipping");
  241. return;
  242. }
  243. nwatch->dev = dev;
  244. nwatch->ino = ino;
  245. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  246. oentry = container_of(r, struct audit_entry, rule);
  247. list_del(&oentry->rule.rlist);
  248. list_del_rcu(&oentry->list);
  249. nentry = audit_dupe_rule(&oentry->rule);
  250. if (IS_ERR(nentry)) {
  251. list_del(&oentry->rule.list);
  252. audit_panic("error updating watch, removing");
  253. } else {
  254. int h = audit_hash_ino((u32)ino);
  255. /*
  256. * nentry->rule.watch == oentry->rule.watch so
  257. * we must drop that reference and set it to our
  258. * new watch.
  259. */
  260. audit_put_watch(nentry->rule.watch);
  261. audit_get_watch(nwatch);
  262. nentry->rule.watch = nwatch;
  263. list_add(&nentry->rule.rlist, &nwatch->rules);
  264. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  265. list_replace(&oentry->rule.list,
  266. &nentry->rule.list);
  267. }
  268. if (oentry->rule.exe)
  269. audit_remove_mark(oentry->rule.exe);
  270. audit_watch_log_rule_change(r, owatch, "updated_rules");
  271. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  272. }
  273. audit_remove_watch(owatch);
  274. goto add_watch_to_parent; /* event applies to a single watch */
  275. }
  276. mutex_unlock(&audit_filter_mutex);
  277. return;
  278. add_watch_to_parent:
  279. list_add(&nwatch->wlist, &parent->watches);
  280. mutex_unlock(&audit_filter_mutex);
  281. return;
  282. }
  283. /* Remove all watches & rules associated with a parent that is going away. */
  284. static void audit_remove_parent_watches(struct audit_parent *parent)
  285. {
  286. struct audit_watch *w, *nextw;
  287. struct audit_krule *r, *nextr;
  288. struct audit_entry *e;
  289. mutex_lock(&audit_filter_mutex);
  290. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  291. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  292. e = container_of(r, struct audit_entry, rule);
  293. audit_watch_log_rule_change(r, w, "remove_rule");
  294. if (e->rule.exe)
  295. audit_remove_mark(e->rule.exe);
  296. list_del(&r->rlist);
  297. list_del(&r->list);
  298. list_del_rcu(&e->list);
  299. call_rcu(&e->rcu, audit_free_rule_rcu);
  300. }
  301. audit_remove_watch(w);
  302. }
  303. mutex_unlock(&audit_filter_mutex);
  304. fsnotify_destroy_mark(&parent->mark, audit_watch_group);
  305. }
  306. /* Get path information necessary for adding watches. */
  307. static int audit_get_nd(struct audit_watch *watch, struct path *parent)
  308. {
  309. struct dentry *d = kern_path_locked(watch->path, parent);
  310. if (IS_ERR(d))
  311. return PTR_ERR(d);
  312. inode_unlock(d_backing_inode(parent->dentry));
  313. if (d_is_positive(d)) {
  314. /* update watch filter fields */
  315. watch->dev = d->d_sb->s_dev;
  316. watch->ino = d_backing_inode(d)->i_ino;
  317. }
  318. dput(d);
  319. return 0;
  320. }
  321. /* Associate the given rule with an existing parent.
  322. * Caller must hold audit_filter_mutex. */
  323. static void audit_add_to_parent(struct audit_krule *krule,
  324. struct audit_parent *parent)
  325. {
  326. struct audit_watch *w, *watch = krule->watch;
  327. int watch_found = 0;
  328. BUG_ON(!mutex_is_locked(&audit_filter_mutex));
  329. list_for_each_entry(w, &parent->watches, wlist) {
  330. if (strcmp(watch->path, w->path))
  331. continue;
  332. watch_found = 1;
  333. /* put krule's ref to temporary watch */
  334. audit_put_watch(watch);
  335. audit_get_watch(w);
  336. krule->watch = watch = w;
  337. audit_put_parent(parent);
  338. break;
  339. }
  340. if (!watch_found) {
  341. watch->parent = parent;
  342. audit_get_watch(watch);
  343. list_add(&watch->wlist, &parent->watches);
  344. }
  345. list_add(&krule->rlist, &watch->rules);
  346. }
  347. /* Find a matching watch entry, or add this one.
  348. * Caller must hold audit_filter_mutex. */
  349. int audit_add_watch(struct audit_krule *krule, struct list_head **list)
  350. {
  351. struct audit_watch *watch = krule->watch;
  352. struct audit_parent *parent;
  353. struct path parent_path;
  354. int h, ret = 0;
  355. mutex_unlock(&audit_filter_mutex);
  356. /* Avoid calling path_lookup under audit_filter_mutex. */
  357. ret = audit_get_nd(watch, &parent_path);
  358. /* caller expects mutex locked */
  359. mutex_lock(&audit_filter_mutex);
  360. if (ret)
  361. return ret;
  362. /* either find an old parent or attach a new one */
  363. parent = audit_find_parent(d_backing_inode(parent_path.dentry));
  364. if (!parent) {
  365. parent = audit_init_parent(&parent_path);
  366. if (IS_ERR(parent)) {
  367. ret = PTR_ERR(parent);
  368. goto error;
  369. }
  370. }
  371. audit_add_to_parent(krule, parent);
  372. h = audit_hash_ino((u32)watch->ino);
  373. *list = &audit_inode_hash[h];
  374. error:
  375. path_put(&parent_path);
  376. return ret;
  377. }
  378. void audit_remove_watch_rule(struct audit_krule *krule)
  379. {
  380. struct audit_watch *watch = krule->watch;
  381. struct audit_parent *parent = watch->parent;
  382. list_del(&krule->rlist);
  383. if (list_empty(&watch->rules)) {
  384. /*
  385. * audit_remove_watch() drops our reference to 'parent' which
  386. * can get freed. Grab our own reference to be safe.
  387. */
  388. audit_get_parent(parent);
  389. audit_remove_watch(watch);
  390. if (list_empty(&parent->watches))
  391. fsnotify_destroy_mark(&parent->mark, audit_watch_group);
  392. audit_put_parent(parent);
  393. }
  394. }
  395. /* Update watch data in audit rules based on fsnotify events. */
  396. static int audit_watch_handle_event(struct fsnotify_group *group,
  397. struct inode *to_tell,
  398. struct fsnotify_mark *inode_mark,
  399. struct fsnotify_mark *vfsmount_mark,
  400. u32 mask, void *data, int data_type,
  401. const unsigned char *dname, u32 cookie)
  402. {
  403. struct inode *inode;
  404. struct audit_parent *parent;
  405. parent = container_of(inode_mark, struct audit_parent, mark);
  406. BUG_ON(group != audit_watch_group);
  407. switch (data_type) {
  408. case (FSNOTIFY_EVENT_PATH):
  409. inode = d_backing_inode(((struct path *)data)->dentry);
  410. break;
  411. case (FSNOTIFY_EVENT_INODE):
  412. inode = (struct inode *)data;
  413. break;
  414. default:
  415. BUG();
  416. inode = NULL;
  417. break;
  418. };
  419. if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
  420. audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
  421. else if (mask & (FS_DELETE|FS_MOVED_FROM))
  422. audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
  423. else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  424. audit_remove_parent_watches(parent);
  425. return 0;
  426. }
  427. static const struct fsnotify_ops audit_watch_fsnotify_ops = {
  428. .handle_event = audit_watch_handle_event,
  429. };
  430. static int __init audit_watch_init(void)
  431. {
  432. audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
  433. if (IS_ERR(audit_watch_group)) {
  434. audit_watch_group = NULL;
  435. audit_panic("cannot create audit fsnotify group");
  436. }
  437. return 0;
  438. }
  439. device_initcall(audit_watch_init);
  440. int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
  441. {
  442. struct audit_fsnotify_mark *audit_mark;
  443. char *pathname;
  444. pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
  445. if (!pathname)
  446. return -ENOMEM;
  447. audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
  448. if (IS_ERR(audit_mark)) {
  449. kfree(pathname);
  450. return PTR_ERR(audit_mark);
  451. }
  452. new->exe = audit_mark;
  453. return 0;
  454. }
  455. int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
  456. {
  457. struct file *exe_file;
  458. unsigned long ino;
  459. dev_t dev;
  460. exe_file = get_task_exe_file(tsk);
  461. if (!exe_file)
  462. return 0;
  463. ino = exe_file->f_inode->i_ino;
  464. dev = exe_file->f_inode->i_sb->s_dev;
  465. fput(exe_file);
  466. return audit_mark_compare(mark, ino, dev);
  467. }