audit_tree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. #include "audit.h"
  2. #include <linux/inotify.h>
  3. #include <linux/namei.h>
  4. #include <linux/mount.h>
  5. struct audit_tree;
  6. struct audit_chunk;
  7. struct audit_tree {
  8. atomic_t count;
  9. int goner;
  10. struct audit_chunk *root;
  11. struct list_head chunks;
  12. struct list_head rules;
  13. struct list_head list;
  14. struct list_head same_root;
  15. struct rcu_head head;
  16. char pathname[];
  17. };
  18. struct audit_chunk {
  19. struct list_head hash;
  20. struct inotify_watch watch;
  21. struct list_head trees; /* with root here */
  22. int dead;
  23. int count;
  24. atomic_long_t refs;
  25. struct rcu_head head;
  26. struct node {
  27. struct list_head list;
  28. struct audit_tree *owner;
  29. unsigned index; /* index; upper bit indicates 'will prune' */
  30. } owners[];
  31. };
  32. static LIST_HEAD(tree_list);
  33. static LIST_HEAD(prune_list);
  34. /*
  35. * One struct chunk is attached to each inode of interest.
  36. * We replace struct chunk on tagging/untagging.
  37. * Rules have pointer to struct audit_tree.
  38. * Rules have struct list_head rlist forming a list of rules over
  39. * the same tree.
  40. * References to struct chunk are collected at audit_inode{,_child}()
  41. * time and used in AUDIT_TREE rule matching.
  42. * These references are dropped at the same time we are calling
  43. * audit_free_names(), etc.
  44. *
  45. * Cyclic lists galore:
  46. * tree.chunks anchors chunk.owners[].list hash_lock
  47. * tree.rules anchors rule.rlist audit_filter_mutex
  48. * chunk.trees anchors tree.same_root hash_lock
  49. * chunk.hash is a hash with middle bits of watch.inode as
  50. * a hash function. RCU, hash_lock
  51. *
  52. * tree is refcounted; one reference for "some rules on rules_list refer to
  53. * it", one for each chunk with pointer to it.
  54. *
  55. * chunk is refcounted by embedded inotify_watch + .refs (non-zero refcount
  56. * of watch contributes 1 to .refs).
  57. *
  58. * node.index allows to get from node.list to containing chunk.
  59. * MSB of that sucker is stolen to mark taggings that we might have to
  60. * revert - several operations have very unpleasant cleanup logics and
  61. * that makes a difference. Some.
  62. */
  63. static struct inotify_handle *rtree_ih;
  64. static struct audit_tree *alloc_tree(const char *s)
  65. {
  66. struct audit_tree *tree;
  67. tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
  68. if (tree) {
  69. atomic_set(&tree->count, 1);
  70. tree->goner = 0;
  71. INIT_LIST_HEAD(&tree->chunks);
  72. INIT_LIST_HEAD(&tree->rules);
  73. INIT_LIST_HEAD(&tree->list);
  74. INIT_LIST_HEAD(&tree->same_root);
  75. tree->root = NULL;
  76. strcpy(tree->pathname, s);
  77. }
  78. return tree;
  79. }
  80. static inline void get_tree(struct audit_tree *tree)
  81. {
  82. atomic_inc(&tree->count);
  83. }
  84. static void __put_tree(struct rcu_head *rcu)
  85. {
  86. struct audit_tree *tree = container_of(rcu, struct audit_tree, head);
  87. kfree(tree);
  88. }
  89. static inline void put_tree(struct audit_tree *tree)
  90. {
  91. if (atomic_dec_and_test(&tree->count))
  92. call_rcu(&tree->head, __put_tree);
  93. }
  94. /* to avoid bringing the entire thing in audit.h */
  95. const char *audit_tree_path(struct audit_tree *tree)
  96. {
  97. return tree->pathname;
  98. }
  99. static struct audit_chunk *alloc_chunk(int count)
  100. {
  101. struct audit_chunk *chunk;
  102. size_t size;
  103. int i;
  104. size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
  105. chunk = kzalloc(size, GFP_KERNEL);
  106. if (!chunk)
  107. return NULL;
  108. INIT_LIST_HEAD(&chunk->hash);
  109. INIT_LIST_HEAD(&chunk->trees);
  110. chunk->count = count;
  111. atomic_long_set(&chunk->refs, 1);
  112. for (i = 0; i < count; i++) {
  113. INIT_LIST_HEAD(&chunk->owners[i].list);
  114. chunk->owners[i].index = i;
  115. }
  116. inotify_init_watch(&chunk->watch);
  117. return chunk;
  118. }
  119. static void free_chunk(struct audit_chunk *chunk)
  120. {
  121. int i;
  122. for (i = 0; i < chunk->count; i++) {
  123. if (chunk->owners[i].owner)
  124. put_tree(chunk->owners[i].owner);
  125. }
  126. kfree(chunk);
  127. }
  128. void audit_put_chunk(struct audit_chunk *chunk)
  129. {
  130. if (atomic_long_dec_and_test(&chunk->refs))
  131. free_chunk(chunk);
  132. }
  133. static void __put_chunk(struct rcu_head *rcu)
  134. {
  135. struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
  136. audit_put_chunk(chunk);
  137. }
  138. enum {HASH_SIZE = 128};
  139. static struct list_head chunk_hash_heads[HASH_SIZE];
  140. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
  141. static inline struct list_head *chunk_hash(const struct inode *inode)
  142. {
  143. unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
  144. return chunk_hash_heads + n % HASH_SIZE;
  145. }
  146. /* hash_lock is held by caller */
  147. static void insert_hash(struct audit_chunk *chunk)
  148. {
  149. struct list_head *list = chunk_hash(chunk->watch.inode);
  150. list_add_rcu(&chunk->hash, list);
  151. }
  152. /* called under rcu_read_lock */
  153. struct audit_chunk *audit_tree_lookup(const struct inode *inode)
  154. {
  155. struct list_head *list = chunk_hash(inode);
  156. struct audit_chunk *p;
  157. list_for_each_entry_rcu(p, list, hash) {
  158. if (p->watch.inode == inode) {
  159. atomic_long_inc(&p->refs);
  160. return p;
  161. }
  162. }
  163. return NULL;
  164. }
  165. int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
  166. {
  167. int n;
  168. for (n = 0; n < chunk->count; n++)
  169. if (chunk->owners[n].owner == tree)
  170. return 1;
  171. return 0;
  172. }
  173. /* tagging and untagging inodes with trees */
  174. static struct audit_chunk *find_chunk(struct node *p)
  175. {
  176. int index = p->index & ~(1U<<31);
  177. p -= index;
  178. return container_of(p, struct audit_chunk, owners[0]);
  179. }
  180. static void untag_chunk(struct node *p)
  181. {
  182. struct audit_chunk *chunk = find_chunk(p);
  183. struct audit_chunk *new;
  184. struct audit_tree *owner;
  185. int size = chunk->count - 1;
  186. int i, j;
  187. if (!pin_inotify_watch(&chunk->watch)) {
  188. /*
  189. * Filesystem is shutting down; all watches are getting
  190. * evicted, just take it off the node list for this
  191. * tree and let the eviction logics take care of the
  192. * rest.
  193. */
  194. owner = p->owner;
  195. if (owner->root == chunk) {
  196. list_del_init(&owner->same_root);
  197. owner->root = NULL;
  198. }
  199. list_del_init(&p->list);
  200. p->owner = NULL;
  201. put_tree(owner);
  202. return;
  203. }
  204. spin_unlock(&hash_lock);
  205. /*
  206. * pin_inotify_watch() succeeded, so the watch won't go away
  207. * from under us.
  208. */
  209. mutex_lock(&chunk->watch.inode->inotify_mutex);
  210. if (chunk->dead) {
  211. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  212. goto out;
  213. }
  214. owner = p->owner;
  215. if (!size) {
  216. chunk->dead = 1;
  217. spin_lock(&hash_lock);
  218. list_del_init(&chunk->trees);
  219. if (owner->root == chunk)
  220. owner->root = NULL;
  221. list_del_init(&p->list);
  222. list_del_rcu(&chunk->hash);
  223. spin_unlock(&hash_lock);
  224. inotify_evict_watch(&chunk->watch);
  225. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  226. put_inotify_watch(&chunk->watch);
  227. goto out;
  228. }
  229. new = alloc_chunk(size);
  230. if (!new)
  231. goto Fallback;
  232. if (inotify_clone_watch(&chunk->watch, &new->watch) < 0) {
  233. free_chunk(new);
  234. goto Fallback;
  235. }
  236. chunk->dead = 1;
  237. spin_lock(&hash_lock);
  238. list_replace_init(&chunk->trees, &new->trees);
  239. if (owner->root == chunk) {
  240. list_del_init(&owner->same_root);
  241. owner->root = NULL;
  242. }
  243. for (i = j = 0; i < size; i++, j++) {
  244. struct audit_tree *s;
  245. if (&chunk->owners[j] == p) {
  246. list_del_init(&p->list);
  247. i--;
  248. continue;
  249. }
  250. s = chunk->owners[j].owner;
  251. new->owners[i].owner = s;
  252. new->owners[i].index = chunk->owners[j].index - j + i;
  253. if (!s) /* result of earlier fallback */
  254. continue;
  255. get_tree(s);
  256. list_replace_init(&chunk->owners[i].list, &new->owners[j].list);
  257. }
  258. list_replace_rcu(&chunk->hash, &new->hash);
  259. list_for_each_entry(owner, &new->trees, same_root)
  260. owner->root = new;
  261. spin_unlock(&hash_lock);
  262. inotify_evict_watch(&chunk->watch);
  263. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  264. put_inotify_watch(&chunk->watch);
  265. goto out;
  266. Fallback:
  267. // do the best we can
  268. spin_lock(&hash_lock);
  269. if (owner->root == chunk) {
  270. list_del_init(&owner->same_root);
  271. owner->root = NULL;
  272. }
  273. list_del_init(&p->list);
  274. p->owner = NULL;
  275. put_tree(owner);
  276. spin_unlock(&hash_lock);
  277. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  278. out:
  279. unpin_inotify_watch(&chunk->watch);
  280. spin_lock(&hash_lock);
  281. }
  282. static int create_chunk(struct inode *inode, struct audit_tree *tree)
  283. {
  284. struct audit_chunk *chunk = alloc_chunk(1);
  285. if (!chunk)
  286. return -ENOMEM;
  287. if (inotify_add_watch(rtree_ih, &chunk->watch, inode, IN_IGNORED | IN_DELETE_SELF) < 0) {
  288. free_chunk(chunk);
  289. return -ENOSPC;
  290. }
  291. mutex_lock(&inode->inotify_mutex);
  292. spin_lock(&hash_lock);
  293. if (tree->goner) {
  294. spin_unlock(&hash_lock);
  295. chunk->dead = 1;
  296. inotify_evict_watch(&chunk->watch);
  297. mutex_unlock(&inode->inotify_mutex);
  298. put_inotify_watch(&chunk->watch);
  299. return 0;
  300. }
  301. chunk->owners[0].index = (1U << 31);
  302. chunk->owners[0].owner = tree;
  303. get_tree(tree);
  304. list_add(&chunk->owners[0].list, &tree->chunks);
  305. if (!tree->root) {
  306. tree->root = chunk;
  307. list_add(&tree->same_root, &chunk->trees);
  308. }
  309. insert_hash(chunk);
  310. spin_unlock(&hash_lock);
  311. mutex_unlock(&inode->inotify_mutex);
  312. return 0;
  313. }
  314. /* the first tagged inode becomes root of tree */
  315. static int tag_chunk(struct inode *inode, struct audit_tree *tree)
  316. {
  317. struct inotify_watch *watch;
  318. struct audit_tree *owner;
  319. struct audit_chunk *chunk, *old;
  320. struct node *p;
  321. int n;
  322. if (inotify_find_watch(rtree_ih, inode, &watch) < 0)
  323. return create_chunk(inode, tree);
  324. old = container_of(watch, struct audit_chunk, watch);
  325. /* are we already there? */
  326. spin_lock(&hash_lock);
  327. for (n = 0; n < old->count; n++) {
  328. if (old->owners[n].owner == tree) {
  329. spin_unlock(&hash_lock);
  330. put_inotify_watch(watch);
  331. return 0;
  332. }
  333. }
  334. spin_unlock(&hash_lock);
  335. chunk = alloc_chunk(old->count + 1);
  336. if (!chunk)
  337. return -ENOMEM;
  338. mutex_lock(&inode->inotify_mutex);
  339. if (inotify_clone_watch(&old->watch, &chunk->watch) < 0) {
  340. mutex_unlock(&inode->inotify_mutex);
  341. free_chunk(chunk);
  342. return -ENOSPC;
  343. }
  344. spin_lock(&hash_lock);
  345. if (tree->goner) {
  346. spin_unlock(&hash_lock);
  347. chunk->dead = 1;
  348. inotify_evict_watch(&chunk->watch);
  349. mutex_unlock(&inode->inotify_mutex);
  350. put_inotify_watch(&chunk->watch);
  351. return 0;
  352. }
  353. list_replace_init(&old->trees, &chunk->trees);
  354. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  355. struct audit_tree *s = old->owners[n].owner;
  356. p->owner = s;
  357. p->index = old->owners[n].index;
  358. if (!s) /* result of fallback in untag */
  359. continue;
  360. get_tree(s);
  361. list_replace_init(&old->owners[n].list, &p->list);
  362. }
  363. p->index = (chunk->count - 1) | (1U<<31);
  364. p->owner = tree;
  365. get_tree(tree);
  366. list_add(&p->list, &tree->chunks);
  367. list_replace_rcu(&old->hash, &chunk->hash);
  368. list_for_each_entry(owner, &chunk->trees, same_root)
  369. owner->root = chunk;
  370. old->dead = 1;
  371. if (!tree->root) {
  372. tree->root = chunk;
  373. list_add(&tree->same_root, &chunk->trees);
  374. }
  375. spin_unlock(&hash_lock);
  376. inotify_evict_watch(&old->watch);
  377. mutex_unlock(&inode->inotify_mutex);
  378. put_inotify_watch(&old->watch);
  379. return 0;
  380. }
  381. static void kill_rules(struct audit_tree *tree)
  382. {
  383. struct audit_krule *rule, *next;
  384. struct audit_entry *entry;
  385. struct audit_buffer *ab;
  386. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  387. entry = container_of(rule, struct audit_entry, rule);
  388. list_del_init(&rule->rlist);
  389. if (rule->tree) {
  390. /* not a half-baked one */
  391. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  392. audit_log_format(ab, "op=remove rule dir=");
  393. audit_log_untrustedstring(ab, rule->tree->pathname);
  394. if (rule->filterkey) {
  395. audit_log_format(ab, " key=");
  396. audit_log_untrustedstring(ab, rule->filterkey);
  397. } else
  398. audit_log_format(ab, " key=(null)");
  399. audit_log_format(ab, " list=%d res=1", rule->listnr);
  400. audit_log_end(ab);
  401. rule->tree = NULL;
  402. list_del_rcu(&entry->list);
  403. list_del(&entry->rule.list);
  404. call_rcu(&entry->rcu, audit_free_rule_rcu);
  405. }
  406. }
  407. }
  408. /*
  409. * finish killing struct audit_tree
  410. */
  411. static void prune_one(struct audit_tree *victim)
  412. {
  413. spin_lock(&hash_lock);
  414. while (!list_empty(&victim->chunks)) {
  415. struct node *p;
  416. p = list_entry(victim->chunks.next, struct node, list);
  417. untag_chunk(p);
  418. }
  419. spin_unlock(&hash_lock);
  420. put_tree(victim);
  421. }
  422. /* trim the uncommitted chunks from tree */
  423. static void trim_marked(struct audit_tree *tree)
  424. {
  425. struct list_head *p, *q;
  426. spin_lock(&hash_lock);
  427. if (tree->goner) {
  428. spin_unlock(&hash_lock);
  429. return;
  430. }
  431. /* reorder */
  432. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  433. struct node *node = list_entry(p, struct node, list);
  434. q = p->next;
  435. if (node->index & (1U<<31)) {
  436. list_del_init(p);
  437. list_add(p, &tree->chunks);
  438. }
  439. }
  440. while (!list_empty(&tree->chunks)) {
  441. struct node *node;
  442. node = list_entry(tree->chunks.next, struct node, list);
  443. /* have we run out of marked? */
  444. if (!(node->index & (1U<<31)))
  445. break;
  446. untag_chunk(node);
  447. }
  448. if (!tree->root && !tree->goner) {
  449. tree->goner = 1;
  450. spin_unlock(&hash_lock);
  451. mutex_lock(&audit_filter_mutex);
  452. kill_rules(tree);
  453. list_del_init(&tree->list);
  454. mutex_unlock(&audit_filter_mutex);
  455. prune_one(tree);
  456. } else {
  457. spin_unlock(&hash_lock);
  458. }
  459. }
  460. /* called with audit_filter_mutex */
  461. int audit_remove_tree_rule(struct audit_krule *rule)
  462. {
  463. struct audit_tree *tree;
  464. tree = rule->tree;
  465. if (tree) {
  466. spin_lock(&hash_lock);
  467. list_del_init(&rule->rlist);
  468. if (list_empty(&tree->rules) && !tree->goner) {
  469. tree->root = NULL;
  470. list_del_init(&tree->same_root);
  471. tree->goner = 1;
  472. list_move(&tree->list, &prune_list);
  473. rule->tree = NULL;
  474. spin_unlock(&hash_lock);
  475. audit_schedule_prune();
  476. return 1;
  477. }
  478. rule->tree = NULL;
  479. spin_unlock(&hash_lock);
  480. return 1;
  481. }
  482. return 0;
  483. }
  484. void audit_trim_trees(void)
  485. {
  486. struct list_head cursor;
  487. mutex_lock(&audit_filter_mutex);
  488. list_add(&cursor, &tree_list);
  489. while (cursor.next != &tree_list) {
  490. struct audit_tree *tree;
  491. struct path path;
  492. struct vfsmount *root_mnt;
  493. struct node *node;
  494. struct list_head list;
  495. int err;
  496. tree = container_of(cursor.next, struct audit_tree, list);
  497. get_tree(tree);
  498. list_del(&cursor);
  499. list_add(&cursor, &tree->list);
  500. mutex_unlock(&audit_filter_mutex);
  501. err = kern_path(tree->pathname, 0, &path);
  502. if (err)
  503. goto skip_it;
  504. root_mnt = collect_mounts(path.mnt, path.dentry);
  505. path_put(&path);
  506. if (!root_mnt)
  507. goto skip_it;
  508. list_add_tail(&list, &root_mnt->mnt_list);
  509. spin_lock(&hash_lock);
  510. list_for_each_entry(node, &tree->chunks, list) {
  511. struct audit_chunk *chunk = find_chunk(node);
  512. struct inode *inode = chunk->watch.inode;
  513. struct vfsmount *mnt;
  514. node->index |= 1U<<31;
  515. list_for_each_entry(mnt, &list, mnt_list) {
  516. if (mnt->mnt_root->d_inode == inode) {
  517. node->index &= ~(1U<<31);
  518. break;
  519. }
  520. }
  521. }
  522. spin_unlock(&hash_lock);
  523. trim_marked(tree);
  524. put_tree(tree);
  525. list_del_init(&list);
  526. drop_collected_mounts(root_mnt);
  527. skip_it:
  528. mutex_lock(&audit_filter_mutex);
  529. }
  530. list_del(&cursor);
  531. mutex_unlock(&audit_filter_mutex);
  532. }
  533. static int is_under(struct vfsmount *mnt, struct dentry *dentry,
  534. struct path *path)
  535. {
  536. if (mnt != path->mnt) {
  537. for (;;) {
  538. if (mnt->mnt_parent == mnt)
  539. return 0;
  540. if (mnt->mnt_parent == path->mnt)
  541. break;
  542. mnt = mnt->mnt_parent;
  543. }
  544. dentry = mnt->mnt_mountpoint;
  545. }
  546. return is_subdir(dentry, path->dentry);
  547. }
  548. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  549. {
  550. if (pathname[0] != '/' ||
  551. rule->listnr != AUDIT_FILTER_EXIT ||
  552. op != Audit_equal ||
  553. rule->inode_f || rule->watch || rule->tree)
  554. return -EINVAL;
  555. rule->tree = alloc_tree(pathname);
  556. if (!rule->tree)
  557. return -ENOMEM;
  558. return 0;
  559. }
  560. void audit_put_tree(struct audit_tree *tree)
  561. {
  562. put_tree(tree);
  563. }
  564. /* called with audit_filter_mutex */
  565. int audit_add_tree_rule(struct audit_krule *rule)
  566. {
  567. struct audit_tree *seed = rule->tree, *tree;
  568. struct path path;
  569. struct vfsmount *mnt, *p;
  570. struct list_head list;
  571. int err;
  572. list_for_each_entry(tree, &tree_list, list) {
  573. if (!strcmp(seed->pathname, tree->pathname)) {
  574. put_tree(seed);
  575. rule->tree = tree;
  576. list_add(&rule->rlist, &tree->rules);
  577. return 0;
  578. }
  579. }
  580. tree = seed;
  581. list_add(&tree->list, &tree_list);
  582. list_add(&rule->rlist, &tree->rules);
  583. /* do not set rule->tree yet */
  584. mutex_unlock(&audit_filter_mutex);
  585. err = kern_path(tree->pathname, 0, &path);
  586. if (err)
  587. goto Err;
  588. mnt = collect_mounts(path.mnt, path.dentry);
  589. path_put(&path);
  590. if (!mnt) {
  591. err = -ENOMEM;
  592. goto Err;
  593. }
  594. list_add_tail(&list, &mnt->mnt_list);
  595. get_tree(tree);
  596. list_for_each_entry(p, &list, mnt_list) {
  597. err = tag_chunk(p->mnt_root->d_inode, tree);
  598. if (err)
  599. break;
  600. }
  601. list_del(&list);
  602. drop_collected_mounts(mnt);
  603. if (!err) {
  604. struct node *node;
  605. spin_lock(&hash_lock);
  606. list_for_each_entry(node, &tree->chunks, list)
  607. node->index &= ~(1U<<31);
  608. spin_unlock(&hash_lock);
  609. } else {
  610. trim_marked(tree);
  611. goto Err;
  612. }
  613. mutex_lock(&audit_filter_mutex);
  614. if (list_empty(&rule->rlist)) {
  615. put_tree(tree);
  616. return -ENOENT;
  617. }
  618. rule->tree = tree;
  619. put_tree(tree);
  620. return 0;
  621. Err:
  622. mutex_lock(&audit_filter_mutex);
  623. list_del_init(&tree->list);
  624. list_del_init(&tree->rules);
  625. put_tree(tree);
  626. return err;
  627. }
  628. int audit_tag_tree(char *old, char *new)
  629. {
  630. struct list_head cursor, barrier;
  631. int failed = 0;
  632. struct path path;
  633. struct vfsmount *tagged;
  634. struct list_head list;
  635. struct vfsmount *mnt;
  636. struct dentry *dentry;
  637. int err;
  638. err = kern_path(new, 0, &path);
  639. if (err)
  640. return err;
  641. tagged = collect_mounts(path.mnt, path.dentry);
  642. path_put(&path);
  643. if (!tagged)
  644. return -ENOMEM;
  645. err = kern_path(old, 0, &path);
  646. if (err) {
  647. drop_collected_mounts(tagged);
  648. return err;
  649. }
  650. mnt = mntget(path.mnt);
  651. dentry = dget(path.dentry);
  652. path_put(&path);
  653. if (dentry == tagged->mnt_root && dentry == mnt->mnt_root)
  654. follow_up(&mnt, &dentry);
  655. list_add_tail(&list, &tagged->mnt_list);
  656. mutex_lock(&audit_filter_mutex);
  657. list_add(&barrier, &tree_list);
  658. list_add(&cursor, &barrier);
  659. while (cursor.next != &tree_list) {
  660. struct audit_tree *tree;
  661. struct vfsmount *p;
  662. tree = container_of(cursor.next, struct audit_tree, list);
  663. get_tree(tree);
  664. list_del(&cursor);
  665. list_add(&cursor, &tree->list);
  666. mutex_unlock(&audit_filter_mutex);
  667. err = kern_path(tree->pathname, 0, &path);
  668. if (err) {
  669. put_tree(tree);
  670. mutex_lock(&audit_filter_mutex);
  671. continue;
  672. }
  673. spin_lock(&vfsmount_lock);
  674. if (!is_under(mnt, dentry, &path)) {
  675. spin_unlock(&vfsmount_lock);
  676. path_put(&path);
  677. put_tree(tree);
  678. mutex_lock(&audit_filter_mutex);
  679. continue;
  680. }
  681. spin_unlock(&vfsmount_lock);
  682. path_put(&path);
  683. list_for_each_entry(p, &list, mnt_list) {
  684. failed = tag_chunk(p->mnt_root->d_inode, tree);
  685. if (failed)
  686. break;
  687. }
  688. if (failed) {
  689. put_tree(tree);
  690. mutex_lock(&audit_filter_mutex);
  691. break;
  692. }
  693. mutex_lock(&audit_filter_mutex);
  694. spin_lock(&hash_lock);
  695. if (!tree->goner) {
  696. list_del(&tree->list);
  697. list_add(&tree->list, &tree_list);
  698. }
  699. spin_unlock(&hash_lock);
  700. put_tree(tree);
  701. }
  702. while (barrier.prev != &tree_list) {
  703. struct audit_tree *tree;
  704. tree = container_of(barrier.prev, struct audit_tree, list);
  705. get_tree(tree);
  706. list_del(&tree->list);
  707. list_add(&tree->list, &barrier);
  708. mutex_unlock(&audit_filter_mutex);
  709. if (!failed) {
  710. struct node *node;
  711. spin_lock(&hash_lock);
  712. list_for_each_entry(node, &tree->chunks, list)
  713. node->index &= ~(1U<<31);
  714. spin_unlock(&hash_lock);
  715. } else {
  716. trim_marked(tree);
  717. }
  718. put_tree(tree);
  719. mutex_lock(&audit_filter_mutex);
  720. }
  721. list_del(&barrier);
  722. list_del(&cursor);
  723. list_del(&list);
  724. mutex_unlock(&audit_filter_mutex);
  725. dput(dentry);
  726. mntput(mnt);
  727. drop_collected_mounts(tagged);
  728. return failed;
  729. }
  730. /*
  731. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  732. * Runs from a separate thread, with audit_cmd_mutex held.
  733. */
  734. void audit_prune_trees(void)
  735. {
  736. mutex_lock(&audit_filter_mutex);
  737. while (!list_empty(&prune_list)) {
  738. struct audit_tree *victim;
  739. victim = list_entry(prune_list.next, struct audit_tree, list);
  740. list_del_init(&victim->list);
  741. mutex_unlock(&audit_filter_mutex);
  742. prune_one(victim);
  743. mutex_lock(&audit_filter_mutex);
  744. }
  745. mutex_unlock(&audit_filter_mutex);
  746. }
  747. /*
  748. * Here comes the stuff asynchronous to auditctl operations
  749. */
  750. /* inode->inotify_mutex is locked */
  751. static void evict_chunk(struct audit_chunk *chunk)
  752. {
  753. struct audit_tree *owner;
  754. int n;
  755. if (chunk->dead)
  756. return;
  757. chunk->dead = 1;
  758. mutex_lock(&audit_filter_mutex);
  759. spin_lock(&hash_lock);
  760. while (!list_empty(&chunk->trees)) {
  761. owner = list_entry(chunk->trees.next,
  762. struct audit_tree, same_root);
  763. owner->goner = 1;
  764. owner->root = NULL;
  765. list_del_init(&owner->same_root);
  766. spin_unlock(&hash_lock);
  767. kill_rules(owner);
  768. list_move(&owner->list, &prune_list);
  769. audit_schedule_prune();
  770. spin_lock(&hash_lock);
  771. }
  772. list_del_rcu(&chunk->hash);
  773. for (n = 0; n < chunk->count; n++)
  774. list_del_init(&chunk->owners[n].list);
  775. spin_unlock(&hash_lock);
  776. mutex_unlock(&audit_filter_mutex);
  777. }
  778. static void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
  779. u32 cookie, const char *dname, struct inode *inode)
  780. {
  781. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  782. if (mask & IN_IGNORED) {
  783. evict_chunk(chunk);
  784. put_inotify_watch(watch);
  785. }
  786. }
  787. static void destroy_watch(struct inotify_watch *watch)
  788. {
  789. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  790. call_rcu(&chunk->head, __put_chunk);
  791. }
  792. static const struct inotify_operations rtree_inotify_ops = {
  793. .handle_event = handle_event,
  794. .destroy_watch = destroy_watch,
  795. };
  796. static int __init audit_tree_init(void)
  797. {
  798. int i;
  799. rtree_ih = inotify_init(&rtree_inotify_ops);
  800. if (IS_ERR(rtree_ih))
  801. audit_panic("cannot initialize inotify handle for rectree watches");
  802. for (i = 0; i < HASH_SIZE; i++)
  803. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  804. return 0;
  805. }
  806. __initcall(audit_tree_init);