audit_watch.c 15 KB

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