audit_tree.c 21 KB

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