audit_tree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. call_rcu(&entry->rcu, audit_free_rule_rcu);
  404. }
  405. }
  406. }
  407. /*
  408. * finish killing struct audit_tree
  409. */
  410. static void prune_one(struct audit_tree *victim)
  411. {
  412. spin_lock(&hash_lock);
  413. while (!list_empty(&victim->chunks)) {
  414. struct node *p;
  415. p = list_entry(victim->chunks.next, struct node, list);
  416. untag_chunk(p);
  417. }
  418. spin_unlock(&hash_lock);
  419. put_tree(victim);
  420. }
  421. /* trim the uncommitted chunks from tree */
  422. static void trim_marked(struct audit_tree *tree)
  423. {
  424. struct list_head *p, *q;
  425. spin_lock(&hash_lock);
  426. if (tree->goner) {
  427. spin_unlock(&hash_lock);
  428. return;
  429. }
  430. /* reorder */
  431. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  432. struct node *node = list_entry(p, struct node, list);
  433. q = p->next;
  434. if (node->index & (1U<<31)) {
  435. list_del_init(p);
  436. list_add(p, &tree->chunks);
  437. }
  438. }
  439. while (!list_empty(&tree->chunks)) {
  440. struct node *node;
  441. node = list_entry(tree->chunks.next, struct node, list);
  442. /* have we run out of marked? */
  443. if (!(node->index & (1U<<31)))
  444. break;
  445. untag_chunk(node);
  446. }
  447. if (!tree->root && !tree->goner) {
  448. tree->goner = 1;
  449. spin_unlock(&hash_lock);
  450. mutex_lock(&audit_filter_mutex);
  451. kill_rules(tree);
  452. list_del_init(&tree->list);
  453. mutex_unlock(&audit_filter_mutex);
  454. prune_one(tree);
  455. } else {
  456. spin_unlock(&hash_lock);
  457. }
  458. }
  459. /* called with audit_filter_mutex */
  460. int audit_remove_tree_rule(struct audit_krule *rule)
  461. {
  462. struct audit_tree *tree;
  463. tree = rule->tree;
  464. if (tree) {
  465. spin_lock(&hash_lock);
  466. list_del_init(&rule->rlist);
  467. if (list_empty(&tree->rules) && !tree->goner) {
  468. tree->root = NULL;
  469. list_del_init(&tree->same_root);
  470. tree->goner = 1;
  471. list_move(&tree->list, &prune_list);
  472. rule->tree = NULL;
  473. spin_unlock(&hash_lock);
  474. audit_schedule_prune();
  475. return 1;
  476. }
  477. rule->tree = NULL;
  478. spin_unlock(&hash_lock);
  479. return 1;
  480. }
  481. return 0;
  482. }
  483. void audit_trim_trees(void)
  484. {
  485. struct list_head cursor;
  486. mutex_lock(&audit_filter_mutex);
  487. list_add(&cursor, &tree_list);
  488. while (cursor.next != &tree_list) {
  489. struct audit_tree *tree;
  490. struct path path;
  491. struct vfsmount *root_mnt;
  492. struct node *node;
  493. struct list_head list;
  494. int err;
  495. tree = container_of(cursor.next, struct audit_tree, list);
  496. get_tree(tree);
  497. list_del(&cursor);
  498. list_add(&cursor, &tree->list);
  499. mutex_unlock(&audit_filter_mutex);
  500. err = kern_path(tree->pathname, 0, &path);
  501. if (err)
  502. goto skip_it;
  503. root_mnt = collect_mounts(path.mnt, path.dentry);
  504. path_put(&path);
  505. if (!root_mnt)
  506. goto skip_it;
  507. list_add_tail(&list, &root_mnt->mnt_list);
  508. spin_lock(&hash_lock);
  509. list_for_each_entry(node, &tree->chunks, list) {
  510. struct audit_chunk *chunk = find_chunk(node);
  511. struct inode *inode = chunk->watch.inode;
  512. struct vfsmount *mnt;
  513. node->index |= 1U<<31;
  514. list_for_each_entry(mnt, &list, mnt_list) {
  515. if (mnt->mnt_root->d_inode == inode) {
  516. node->index &= ~(1U<<31);
  517. break;
  518. }
  519. }
  520. }
  521. spin_unlock(&hash_lock);
  522. trim_marked(tree);
  523. put_tree(tree);
  524. list_del_init(&list);
  525. drop_collected_mounts(root_mnt);
  526. skip_it:
  527. mutex_lock(&audit_filter_mutex);
  528. }
  529. list_del(&cursor);
  530. mutex_unlock(&audit_filter_mutex);
  531. }
  532. static int is_under(struct vfsmount *mnt, struct dentry *dentry,
  533. struct path *path)
  534. {
  535. if (mnt != path->mnt) {
  536. for (;;) {
  537. if (mnt->mnt_parent == mnt)
  538. return 0;
  539. if (mnt->mnt_parent == path->mnt)
  540. break;
  541. mnt = mnt->mnt_parent;
  542. }
  543. dentry = mnt->mnt_mountpoint;
  544. }
  545. return is_subdir(dentry, path->dentry);
  546. }
  547. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  548. {
  549. if (pathname[0] != '/' ||
  550. rule->listnr != AUDIT_FILTER_EXIT ||
  551. op & ~AUDIT_EQUAL ||
  552. rule->inode_f || rule->watch || rule->tree)
  553. return -EINVAL;
  554. rule->tree = alloc_tree(pathname);
  555. if (!rule->tree)
  556. return -ENOMEM;
  557. return 0;
  558. }
  559. void audit_put_tree(struct audit_tree *tree)
  560. {
  561. put_tree(tree);
  562. }
  563. /* called with audit_filter_mutex */
  564. int audit_add_tree_rule(struct audit_krule *rule)
  565. {
  566. struct audit_tree *seed = rule->tree, *tree;
  567. struct path path;
  568. struct vfsmount *mnt, *p;
  569. struct list_head list;
  570. int err;
  571. list_for_each_entry(tree, &tree_list, list) {
  572. if (!strcmp(seed->pathname, tree->pathname)) {
  573. put_tree(seed);
  574. rule->tree = tree;
  575. list_add(&rule->rlist, &tree->rules);
  576. return 0;
  577. }
  578. }
  579. tree = seed;
  580. list_add(&tree->list, &tree_list);
  581. list_add(&rule->rlist, &tree->rules);
  582. /* do not set rule->tree yet */
  583. mutex_unlock(&audit_filter_mutex);
  584. err = kern_path(tree->pathname, 0, &path);
  585. if (err)
  586. goto Err;
  587. mnt = collect_mounts(path.mnt, path.dentry);
  588. path_put(&path);
  589. if (!mnt) {
  590. err = -ENOMEM;
  591. goto Err;
  592. }
  593. list_add_tail(&list, &mnt->mnt_list);
  594. get_tree(tree);
  595. list_for_each_entry(p, &list, mnt_list) {
  596. err = tag_chunk(p->mnt_root->d_inode, tree);
  597. if (err)
  598. break;
  599. }
  600. list_del(&list);
  601. drop_collected_mounts(mnt);
  602. if (!err) {
  603. struct node *node;
  604. spin_lock(&hash_lock);
  605. list_for_each_entry(node, &tree->chunks, list)
  606. node->index &= ~(1U<<31);
  607. spin_unlock(&hash_lock);
  608. } else {
  609. trim_marked(tree);
  610. goto Err;
  611. }
  612. mutex_lock(&audit_filter_mutex);
  613. if (list_empty(&rule->rlist)) {
  614. put_tree(tree);
  615. return -ENOENT;
  616. }
  617. rule->tree = tree;
  618. put_tree(tree);
  619. return 0;
  620. Err:
  621. mutex_lock(&audit_filter_mutex);
  622. list_del_init(&tree->list);
  623. list_del_init(&tree->rules);
  624. put_tree(tree);
  625. return err;
  626. }
  627. int audit_tag_tree(char *old, char *new)
  628. {
  629. struct list_head cursor, barrier;
  630. int failed = 0;
  631. struct path path;
  632. struct vfsmount *tagged;
  633. struct list_head list;
  634. struct vfsmount *mnt;
  635. struct dentry *dentry;
  636. int err;
  637. err = kern_path(new, 0, &path);
  638. if (err)
  639. return err;
  640. tagged = collect_mounts(path.mnt, path.dentry);
  641. path_put(&path);
  642. if (!tagged)
  643. return -ENOMEM;
  644. err = kern_path(old, 0, &path);
  645. if (err) {
  646. drop_collected_mounts(tagged);
  647. return err;
  648. }
  649. mnt = mntget(path.mnt);
  650. dentry = dget(path.dentry);
  651. path_put(&path);
  652. if (dentry == tagged->mnt_root && dentry == mnt->mnt_root)
  653. follow_up(&mnt, &dentry);
  654. list_add_tail(&list, &tagged->mnt_list);
  655. mutex_lock(&audit_filter_mutex);
  656. list_add(&barrier, &tree_list);
  657. list_add(&cursor, &barrier);
  658. while (cursor.next != &tree_list) {
  659. struct audit_tree *tree;
  660. struct vfsmount *p;
  661. tree = container_of(cursor.next, struct audit_tree, list);
  662. get_tree(tree);
  663. list_del(&cursor);
  664. list_add(&cursor, &tree->list);
  665. mutex_unlock(&audit_filter_mutex);
  666. err = kern_path(tree->pathname, 0, &path);
  667. if (err) {
  668. put_tree(tree);
  669. mutex_lock(&audit_filter_mutex);
  670. continue;
  671. }
  672. spin_lock(&vfsmount_lock);
  673. if (!is_under(mnt, dentry, &path)) {
  674. spin_unlock(&vfsmount_lock);
  675. path_put(&path);
  676. put_tree(tree);
  677. mutex_lock(&audit_filter_mutex);
  678. continue;
  679. }
  680. spin_unlock(&vfsmount_lock);
  681. path_put(&path);
  682. list_for_each_entry(p, &list, mnt_list) {
  683. failed = tag_chunk(p->mnt_root->d_inode, tree);
  684. if (failed)
  685. break;
  686. }
  687. if (failed) {
  688. put_tree(tree);
  689. mutex_lock(&audit_filter_mutex);
  690. break;
  691. }
  692. mutex_lock(&audit_filter_mutex);
  693. spin_lock(&hash_lock);
  694. if (!tree->goner) {
  695. list_del(&tree->list);
  696. list_add(&tree->list, &tree_list);
  697. }
  698. spin_unlock(&hash_lock);
  699. put_tree(tree);
  700. }
  701. while (barrier.prev != &tree_list) {
  702. struct audit_tree *tree;
  703. tree = container_of(barrier.prev, struct audit_tree, list);
  704. get_tree(tree);
  705. list_del(&tree->list);
  706. list_add(&tree->list, &barrier);
  707. mutex_unlock(&audit_filter_mutex);
  708. if (!failed) {
  709. struct node *node;
  710. spin_lock(&hash_lock);
  711. list_for_each_entry(node, &tree->chunks, list)
  712. node->index &= ~(1U<<31);
  713. spin_unlock(&hash_lock);
  714. } else {
  715. trim_marked(tree);
  716. }
  717. put_tree(tree);
  718. mutex_lock(&audit_filter_mutex);
  719. }
  720. list_del(&barrier);
  721. list_del(&cursor);
  722. list_del(&list);
  723. mutex_unlock(&audit_filter_mutex);
  724. dput(dentry);
  725. mntput(mnt);
  726. drop_collected_mounts(tagged);
  727. return failed;
  728. }
  729. /*
  730. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  731. * Runs from a separate thread, with audit_cmd_mutex held.
  732. */
  733. void audit_prune_trees(void)
  734. {
  735. mutex_lock(&audit_filter_mutex);
  736. while (!list_empty(&prune_list)) {
  737. struct audit_tree *victim;
  738. victim = list_entry(prune_list.next, struct audit_tree, list);
  739. list_del_init(&victim->list);
  740. mutex_unlock(&audit_filter_mutex);
  741. prune_one(victim);
  742. mutex_lock(&audit_filter_mutex);
  743. }
  744. mutex_unlock(&audit_filter_mutex);
  745. }
  746. /*
  747. * Here comes the stuff asynchronous to auditctl operations
  748. */
  749. /* inode->inotify_mutex is locked */
  750. static void evict_chunk(struct audit_chunk *chunk)
  751. {
  752. struct audit_tree *owner;
  753. int n;
  754. if (chunk->dead)
  755. return;
  756. chunk->dead = 1;
  757. mutex_lock(&audit_filter_mutex);
  758. spin_lock(&hash_lock);
  759. while (!list_empty(&chunk->trees)) {
  760. owner = list_entry(chunk->trees.next,
  761. struct audit_tree, same_root);
  762. owner->goner = 1;
  763. owner->root = NULL;
  764. list_del_init(&owner->same_root);
  765. spin_unlock(&hash_lock);
  766. kill_rules(owner);
  767. list_move(&owner->list, &prune_list);
  768. audit_schedule_prune();
  769. spin_lock(&hash_lock);
  770. }
  771. list_del_rcu(&chunk->hash);
  772. for (n = 0; n < chunk->count; n++)
  773. list_del_init(&chunk->owners[n].list);
  774. spin_unlock(&hash_lock);
  775. mutex_unlock(&audit_filter_mutex);
  776. }
  777. static void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
  778. u32 cookie, const char *dname, struct inode *inode)
  779. {
  780. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  781. if (mask & IN_IGNORED) {
  782. evict_chunk(chunk);
  783. put_inotify_watch(watch);
  784. }
  785. }
  786. static void destroy_watch(struct inotify_watch *watch)
  787. {
  788. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  789. call_rcu(&chunk->head, __put_chunk);
  790. }
  791. static const struct inotify_operations rtree_inotify_ops = {
  792. .handle_event = handle_event,
  793. .destroy_watch = destroy_watch,
  794. };
  795. static int __init audit_tree_init(void)
  796. {
  797. int i;
  798. rtree_ih = inotify_init(&rtree_inotify_ops);
  799. if (IS_ERR(rtree_ih))
  800. audit_panic("cannot initialize inotify handle for rectree watches");
  801. for (i = 0; i < HASH_SIZE; i++)
  802. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  803. return 0;
  804. }
  805. __initcall(audit_tree_init);