audit_tree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. #include "audit.h"
  2. #include <linux/fsnotify_backend.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 fsnotify_mark mark;
  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 fsnotify_mark + .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 fsnotify_group *audit_tree_group;
  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 inline void put_tree(struct audit_tree *tree)
  87. {
  88. if (atomic_dec_and_test(&tree->count))
  89. kfree_rcu(tree, head);
  90. }
  91. /* to avoid bringing the entire thing in audit.h */
  92. const char *audit_tree_path(struct audit_tree *tree)
  93. {
  94. return tree->pathname;
  95. }
  96. static void free_chunk(struct audit_chunk *chunk)
  97. {
  98. int i;
  99. for (i = 0; i < chunk->count; i++) {
  100. if (chunk->owners[i].owner)
  101. put_tree(chunk->owners[i].owner);
  102. }
  103. kfree(chunk);
  104. }
  105. void audit_put_chunk(struct audit_chunk *chunk)
  106. {
  107. if (atomic_long_dec_and_test(&chunk->refs))
  108. free_chunk(chunk);
  109. }
  110. static void __put_chunk(struct rcu_head *rcu)
  111. {
  112. struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
  113. audit_put_chunk(chunk);
  114. }
  115. static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
  116. {
  117. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  118. call_rcu(&chunk->head, __put_chunk);
  119. }
  120. static struct audit_chunk *alloc_chunk(int count)
  121. {
  122. struct audit_chunk *chunk;
  123. size_t size;
  124. int i;
  125. size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
  126. chunk = kzalloc(size, GFP_KERNEL);
  127. if (!chunk)
  128. return NULL;
  129. INIT_LIST_HEAD(&chunk->hash);
  130. INIT_LIST_HEAD(&chunk->trees);
  131. chunk->count = count;
  132. atomic_long_set(&chunk->refs, 1);
  133. for (i = 0; i < count; i++) {
  134. INIT_LIST_HEAD(&chunk->owners[i].list);
  135. chunk->owners[i].index = i;
  136. }
  137. fsnotify_init_mark(&chunk->mark, audit_tree_destroy_watch);
  138. chunk->mark.mask = FS_IN_IGNORED;
  139. return chunk;
  140. }
  141. enum {HASH_SIZE = 128};
  142. static struct list_head chunk_hash_heads[HASH_SIZE];
  143. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
  144. static inline struct list_head *chunk_hash(const struct inode *inode)
  145. {
  146. unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
  147. return chunk_hash_heads + n % HASH_SIZE;
  148. }
  149. /* hash_lock & entry->lock is held by caller */
  150. static void insert_hash(struct audit_chunk *chunk)
  151. {
  152. struct fsnotify_mark *entry = &chunk->mark;
  153. struct list_head *list;
  154. if (!entry->i.inode)
  155. return;
  156. list = chunk_hash(entry->i.inode);
  157. list_add_rcu(&chunk->hash, list);
  158. }
  159. /* called under rcu_read_lock */
  160. struct audit_chunk *audit_tree_lookup(const struct inode *inode)
  161. {
  162. struct list_head *list = chunk_hash(inode);
  163. struct audit_chunk *p;
  164. list_for_each_entry_rcu(p, list, hash) {
  165. /* mark.inode may have gone NULL, but who cares? */
  166. if (p->mark.i.inode == inode) {
  167. atomic_long_inc(&p->refs);
  168. return p;
  169. }
  170. }
  171. return NULL;
  172. }
  173. int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
  174. {
  175. int n;
  176. for (n = 0; n < chunk->count; n++)
  177. if (chunk->owners[n].owner == tree)
  178. return 1;
  179. return 0;
  180. }
  181. /* tagging and untagging inodes with trees */
  182. static struct audit_chunk *find_chunk(struct node *p)
  183. {
  184. int index = p->index & ~(1U<<31);
  185. p -= index;
  186. return container_of(p, struct audit_chunk, owners[0]);
  187. }
  188. static void untag_chunk(struct node *p)
  189. {
  190. struct audit_chunk *chunk = find_chunk(p);
  191. struct fsnotify_mark *entry = &chunk->mark;
  192. struct audit_chunk *new = NULL;
  193. struct audit_tree *owner;
  194. int size = chunk->count - 1;
  195. int i, j;
  196. fsnotify_get_mark(entry);
  197. spin_unlock(&hash_lock);
  198. if (size)
  199. new = alloc_chunk(size);
  200. spin_lock(&entry->lock);
  201. if (chunk->dead || !entry->i.inode) {
  202. spin_unlock(&entry->lock);
  203. if (new)
  204. free_chunk(new);
  205. goto out;
  206. }
  207. owner = p->owner;
  208. if (!size) {
  209. chunk->dead = 1;
  210. spin_lock(&hash_lock);
  211. list_del_init(&chunk->trees);
  212. if (owner->root == chunk)
  213. owner->root = NULL;
  214. list_del_init(&p->list);
  215. list_del_rcu(&chunk->hash);
  216. spin_unlock(&hash_lock);
  217. spin_unlock(&entry->lock);
  218. fsnotify_destroy_mark(entry);
  219. goto out;
  220. }
  221. if (!new)
  222. goto Fallback;
  223. fsnotify_duplicate_mark(&new->mark, entry);
  224. if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, NULL, 1)) {
  225. fsnotify_put_mark(&new->mark);
  226. goto Fallback;
  227. }
  228. chunk->dead = 1;
  229. spin_lock(&hash_lock);
  230. list_replace_init(&chunk->trees, &new->trees);
  231. if (owner->root == chunk) {
  232. list_del_init(&owner->same_root);
  233. owner->root = NULL;
  234. }
  235. for (i = j = 0; j <= size; i++, j++) {
  236. struct audit_tree *s;
  237. if (&chunk->owners[j] == p) {
  238. list_del_init(&p->list);
  239. i--;
  240. continue;
  241. }
  242. s = chunk->owners[j].owner;
  243. new->owners[i].owner = s;
  244. new->owners[i].index = chunk->owners[j].index - j + i;
  245. if (!s) /* result of earlier fallback */
  246. continue;
  247. get_tree(s);
  248. list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
  249. }
  250. list_replace_rcu(&chunk->hash, &new->hash);
  251. list_for_each_entry(owner, &new->trees, same_root)
  252. owner->root = new;
  253. spin_unlock(&hash_lock);
  254. spin_unlock(&entry->lock);
  255. fsnotify_destroy_mark(entry);
  256. goto out;
  257. Fallback:
  258. // do the best we can
  259. spin_lock(&hash_lock);
  260. if (owner->root == chunk) {
  261. list_del_init(&owner->same_root);
  262. owner->root = NULL;
  263. }
  264. list_del_init(&p->list);
  265. p->owner = NULL;
  266. put_tree(owner);
  267. spin_unlock(&hash_lock);
  268. spin_unlock(&entry->lock);
  269. out:
  270. fsnotify_put_mark(entry);
  271. spin_lock(&hash_lock);
  272. }
  273. static int create_chunk(struct inode *inode, struct audit_tree *tree)
  274. {
  275. struct fsnotify_mark *entry;
  276. struct audit_chunk *chunk = alloc_chunk(1);
  277. if (!chunk)
  278. return -ENOMEM;
  279. entry = &chunk->mark;
  280. if (fsnotify_add_mark(entry, audit_tree_group, inode, NULL, 0)) {
  281. fsnotify_put_mark(entry);
  282. return -ENOSPC;
  283. }
  284. spin_lock(&entry->lock);
  285. spin_lock(&hash_lock);
  286. if (tree->goner) {
  287. spin_unlock(&hash_lock);
  288. chunk->dead = 1;
  289. spin_unlock(&entry->lock);
  290. fsnotify_get_mark(entry);
  291. fsnotify_destroy_mark(entry);
  292. fsnotify_put_mark(entry);
  293. return 0;
  294. }
  295. chunk->owners[0].index = (1U << 31);
  296. chunk->owners[0].owner = tree;
  297. get_tree(tree);
  298. list_add(&chunk->owners[0].list, &tree->chunks);
  299. if (!tree->root) {
  300. tree->root = chunk;
  301. list_add(&tree->same_root, &chunk->trees);
  302. }
  303. insert_hash(chunk);
  304. spin_unlock(&hash_lock);
  305. spin_unlock(&entry->lock);
  306. return 0;
  307. }
  308. /* the first tagged inode becomes root of tree */
  309. static int tag_chunk(struct inode *inode, struct audit_tree *tree)
  310. {
  311. struct fsnotify_mark *old_entry, *chunk_entry;
  312. struct audit_tree *owner;
  313. struct audit_chunk *chunk, *old;
  314. struct node *p;
  315. int n;
  316. old_entry = fsnotify_find_inode_mark(audit_tree_group, inode);
  317. if (!old_entry)
  318. return create_chunk(inode, tree);
  319. old = container_of(old_entry, struct audit_chunk, mark);
  320. /* are we already there? */
  321. spin_lock(&hash_lock);
  322. for (n = 0; n < old->count; n++) {
  323. if (old->owners[n].owner == tree) {
  324. spin_unlock(&hash_lock);
  325. fsnotify_put_mark(old_entry);
  326. return 0;
  327. }
  328. }
  329. spin_unlock(&hash_lock);
  330. chunk = alloc_chunk(old->count + 1);
  331. if (!chunk) {
  332. fsnotify_put_mark(old_entry);
  333. return -ENOMEM;
  334. }
  335. chunk_entry = &chunk->mark;
  336. spin_lock(&old_entry->lock);
  337. if (!old_entry->i.inode) {
  338. /* old_entry is being shot, lets just lie */
  339. spin_unlock(&old_entry->lock);
  340. fsnotify_put_mark(old_entry);
  341. free_chunk(chunk);
  342. return -ENOENT;
  343. }
  344. fsnotify_duplicate_mark(chunk_entry, old_entry);
  345. if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, NULL, 1)) {
  346. spin_unlock(&old_entry->lock);
  347. fsnotify_put_mark(chunk_entry);
  348. fsnotify_put_mark(old_entry);
  349. return -ENOSPC;
  350. }
  351. /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
  352. spin_lock(&chunk_entry->lock);
  353. spin_lock(&hash_lock);
  354. /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
  355. if (tree->goner) {
  356. spin_unlock(&hash_lock);
  357. chunk->dead = 1;
  358. spin_unlock(&chunk_entry->lock);
  359. spin_unlock(&old_entry->lock);
  360. fsnotify_get_mark(chunk_entry);
  361. fsnotify_destroy_mark(chunk_entry);
  362. fsnotify_put_mark(chunk_entry);
  363. fsnotify_put_mark(old_entry);
  364. return 0;
  365. }
  366. list_replace_init(&old->trees, &chunk->trees);
  367. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  368. struct audit_tree *s = old->owners[n].owner;
  369. p->owner = s;
  370. p->index = old->owners[n].index;
  371. if (!s) /* result of fallback in untag */
  372. continue;
  373. get_tree(s);
  374. list_replace_init(&old->owners[n].list, &p->list);
  375. }
  376. p->index = (chunk->count - 1) | (1U<<31);
  377. p->owner = tree;
  378. get_tree(tree);
  379. list_add(&p->list, &tree->chunks);
  380. list_replace_rcu(&old->hash, &chunk->hash);
  381. list_for_each_entry(owner, &chunk->trees, same_root)
  382. owner->root = chunk;
  383. old->dead = 1;
  384. if (!tree->root) {
  385. tree->root = chunk;
  386. list_add(&tree->same_root, &chunk->trees);
  387. }
  388. spin_unlock(&hash_lock);
  389. spin_unlock(&chunk_entry->lock);
  390. spin_unlock(&old_entry->lock);
  391. fsnotify_destroy_mark(old_entry);
  392. fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
  393. return 0;
  394. }
  395. static void kill_rules(struct audit_tree *tree)
  396. {
  397. struct audit_krule *rule, *next;
  398. struct audit_entry *entry;
  399. struct audit_buffer *ab;
  400. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  401. entry = container_of(rule, struct audit_entry, rule);
  402. list_del_init(&rule->rlist);
  403. if (rule->tree) {
  404. /* not a half-baked one */
  405. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  406. audit_log_format(ab, "op=");
  407. audit_log_string(ab, "remove rule");
  408. audit_log_format(ab, " dir=");
  409. audit_log_untrustedstring(ab, rule->tree->pathname);
  410. audit_log_key(ab, rule->filterkey);
  411. audit_log_format(ab, " list=%d res=1", rule->listnr);
  412. audit_log_end(ab);
  413. rule->tree = NULL;
  414. list_del_rcu(&entry->list);
  415. list_del(&entry->rule.list);
  416. call_rcu(&entry->rcu, audit_free_rule_rcu);
  417. }
  418. }
  419. }
  420. /*
  421. * finish killing struct audit_tree
  422. */
  423. static void prune_one(struct audit_tree *victim)
  424. {
  425. spin_lock(&hash_lock);
  426. while (!list_empty(&victim->chunks)) {
  427. struct node *p;
  428. p = list_entry(victim->chunks.next, struct node, list);
  429. untag_chunk(p);
  430. }
  431. spin_unlock(&hash_lock);
  432. put_tree(victim);
  433. }
  434. /* trim the uncommitted chunks from tree */
  435. static void trim_marked(struct audit_tree *tree)
  436. {
  437. struct list_head *p, *q;
  438. spin_lock(&hash_lock);
  439. if (tree->goner) {
  440. spin_unlock(&hash_lock);
  441. return;
  442. }
  443. /* reorder */
  444. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  445. struct node *node = list_entry(p, struct node, list);
  446. q = p->next;
  447. if (node->index & (1U<<31)) {
  448. list_del_init(p);
  449. list_add(p, &tree->chunks);
  450. }
  451. }
  452. while (!list_empty(&tree->chunks)) {
  453. struct node *node;
  454. node = list_entry(tree->chunks.next, struct node, list);
  455. /* have we run out of marked? */
  456. if (!(node->index & (1U<<31)))
  457. break;
  458. untag_chunk(node);
  459. }
  460. if (!tree->root && !tree->goner) {
  461. tree->goner = 1;
  462. spin_unlock(&hash_lock);
  463. mutex_lock(&audit_filter_mutex);
  464. kill_rules(tree);
  465. list_del_init(&tree->list);
  466. mutex_unlock(&audit_filter_mutex);
  467. prune_one(tree);
  468. } else {
  469. spin_unlock(&hash_lock);
  470. }
  471. }
  472. static void audit_schedule_prune(void);
  473. /* called with audit_filter_mutex */
  474. int audit_remove_tree_rule(struct audit_krule *rule)
  475. {
  476. struct audit_tree *tree;
  477. tree = rule->tree;
  478. if (tree) {
  479. spin_lock(&hash_lock);
  480. list_del_init(&rule->rlist);
  481. if (list_empty(&tree->rules) && !tree->goner) {
  482. tree->root = NULL;
  483. list_del_init(&tree->same_root);
  484. tree->goner = 1;
  485. list_move(&tree->list, &prune_list);
  486. rule->tree = NULL;
  487. spin_unlock(&hash_lock);
  488. audit_schedule_prune();
  489. return 1;
  490. }
  491. rule->tree = NULL;
  492. spin_unlock(&hash_lock);
  493. return 1;
  494. }
  495. return 0;
  496. }
  497. static int compare_root(struct vfsmount *mnt, void *arg)
  498. {
  499. return mnt->mnt_root->d_inode == arg;
  500. }
  501. void audit_trim_trees(void)
  502. {
  503. struct list_head cursor;
  504. mutex_lock(&audit_filter_mutex);
  505. list_add(&cursor, &tree_list);
  506. while (cursor.next != &tree_list) {
  507. struct audit_tree *tree;
  508. struct path path;
  509. struct vfsmount *root_mnt;
  510. struct node *node;
  511. int err;
  512. tree = container_of(cursor.next, struct audit_tree, list);
  513. get_tree(tree);
  514. list_del(&cursor);
  515. list_add(&cursor, &tree->list);
  516. mutex_unlock(&audit_filter_mutex);
  517. err = kern_path(tree->pathname, 0, &path);
  518. if (err)
  519. goto skip_it;
  520. root_mnt = collect_mounts(&path);
  521. path_put(&path);
  522. if (IS_ERR(root_mnt))
  523. goto skip_it;
  524. spin_lock(&hash_lock);
  525. list_for_each_entry(node, &tree->chunks, list) {
  526. struct audit_chunk *chunk = find_chunk(node);
  527. /* this could be NULL if the watch is dying else where... */
  528. struct inode *inode = chunk->mark.i.inode;
  529. node->index |= 1U<<31;
  530. if (iterate_mounts(compare_root, inode, root_mnt))
  531. node->index &= ~(1U<<31);
  532. }
  533. spin_unlock(&hash_lock);
  534. trim_marked(tree);
  535. drop_collected_mounts(root_mnt);
  536. skip_it:
  537. put_tree(tree);
  538. mutex_lock(&audit_filter_mutex);
  539. }
  540. list_del(&cursor);
  541. mutex_unlock(&audit_filter_mutex);
  542. }
  543. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  544. {
  545. if (pathname[0] != '/' ||
  546. rule->listnr != AUDIT_FILTER_EXIT ||
  547. op != Audit_equal ||
  548. rule->inode_f || rule->watch || rule->tree)
  549. return -EINVAL;
  550. rule->tree = alloc_tree(pathname);
  551. if (!rule->tree)
  552. return -ENOMEM;
  553. return 0;
  554. }
  555. void audit_put_tree(struct audit_tree *tree)
  556. {
  557. put_tree(tree);
  558. }
  559. static int tag_mount(struct vfsmount *mnt, void *arg)
  560. {
  561. return tag_chunk(mnt->mnt_root->d_inode, arg);
  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;
  569. int err;
  570. list_for_each_entry(tree, &tree_list, list) {
  571. if (!strcmp(seed->pathname, tree->pathname)) {
  572. put_tree(seed);
  573. rule->tree = tree;
  574. list_add(&rule->rlist, &tree->rules);
  575. return 0;
  576. }
  577. }
  578. tree = seed;
  579. list_add(&tree->list, &tree_list);
  580. list_add(&rule->rlist, &tree->rules);
  581. /* do not set rule->tree yet */
  582. mutex_unlock(&audit_filter_mutex);
  583. err = kern_path(tree->pathname, 0, &path);
  584. if (err)
  585. goto Err;
  586. mnt = collect_mounts(&path);
  587. path_put(&path);
  588. if (IS_ERR(mnt)) {
  589. err = PTR_ERR(mnt);
  590. goto Err;
  591. }
  592. get_tree(tree);
  593. err = iterate_mounts(tag_mount, tree, mnt);
  594. drop_collected_mounts(mnt);
  595. if (!err) {
  596. struct node *node;
  597. spin_lock(&hash_lock);
  598. list_for_each_entry(node, &tree->chunks, list)
  599. node->index &= ~(1U<<31);
  600. spin_unlock(&hash_lock);
  601. } else {
  602. trim_marked(tree);
  603. goto Err;
  604. }
  605. mutex_lock(&audit_filter_mutex);
  606. if (list_empty(&rule->rlist)) {
  607. put_tree(tree);
  608. return -ENOENT;
  609. }
  610. rule->tree = tree;
  611. put_tree(tree);
  612. return 0;
  613. Err:
  614. mutex_lock(&audit_filter_mutex);
  615. list_del_init(&tree->list);
  616. list_del_init(&tree->rules);
  617. put_tree(tree);
  618. return err;
  619. }
  620. int audit_tag_tree(char *old, char *new)
  621. {
  622. struct list_head cursor, barrier;
  623. int failed = 0;
  624. struct path path1, path2;
  625. struct vfsmount *tagged;
  626. int err;
  627. err = kern_path(new, 0, &path2);
  628. if (err)
  629. return err;
  630. tagged = collect_mounts(&path2);
  631. path_put(&path2);
  632. if (IS_ERR(tagged))
  633. return PTR_ERR(tagged);
  634. err = kern_path(old, 0, &path1);
  635. if (err) {
  636. drop_collected_mounts(tagged);
  637. return err;
  638. }
  639. mutex_lock(&audit_filter_mutex);
  640. list_add(&barrier, &tree_list);
  641. list_add(&cursor, &barrier);
  642. while (cursor.next != &tree_list) {
  643. struct audit_tree *tree;
  644. int good_one = 0;
  645. tree = container_of(cursor.next, struct audit_tree, list);
  646. get_tree(tree);
  647. list_del(&cursor);
  648. list_add(&cursor, &tree->list);
  649. mutex_unlock(&audit_filter_mutex);
  650. err = kern_path(tree->pathname, 0, &path2);
  651. if (!err) {
  652. good_one = path_is_under(&path1, &path2);
  653. path_put(&path2);
  654. }
  655. if (!good_one) {
  656. put_tree(tree);
  657. mutex_lock(&audit_filter_mutex);
  658. continue;
  659. }
  660. failed = iterate_mounts(tag_mount, tree, tagged);
  661. if (failed) {
  662. put_tree(tree);
  663. mutex_lock(&audit_filter_mutex);
  664. break;
  665. }
  666. mutex_lock(&audit_filter_mutex);
  667. spin_lock(&hash_lock);
  668. if (!tree->goner) {
  669. list_del(&tree->list);
  670. list_add(&tree->list, &tree_list);
  671. }
  672. spin_unlock(&hash_lock);
  673. put_tree(tree);
  674. }
  675. while (barrier.prev != &tree_list) {
  676. struct audit_tree *tree;
  677. tree = container_of(barrier.prev, struct audit_tree, list);
  678. get_tree(tree);
  679. list_del(&tree->list);
  680. list_add(&tree->list, &barrier);
  681. mutex_unlock(&audit_filter_mutex);
  682. if (!failed) {
  683. struct node *node;
  684. spin_lock(&hash_lock);
  685. list_for_each_entry(node, &tree->chunks, list)
  686. node->index &= ~(1U<<31);
  687. spin_unlock(&hash_lock);
  688. } else {
  689. trim_marked(tree);
  690. }
  691. put_tree(tree);
  692. mutex_lock(&audit_filter_mutex);
  693. }
  694. list_del(&barrier);
  695. list_del(&cursor);
  696. mutex_unlock(&audit_filter_mutex);
  697. path_put(&path1);
  698. drop_collected_mounts(tagged);
  699. return failed;
  700. }
  701. /*
  702. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  703. * Runs from a separate thread.
  704. */
  705. static int prune_tree_thread(void *unused)
  706. {
  707. mutex_lock(&audit_cmd_mutex);
  708. mutex_lock(&audit_filter_mutex);
  709. while (!list_empty(&prune_list)) {
  710. struct audit_tree *victim;
  711. victim = list_entry(prune_list.next, struct audit_tree, list);
  712. list_del_init(&victim->list);
  713. mutex_unlock(&audit_filter_mutex);
  714. prune_one(victim);
  715. mutex_lock(&audit_filter_mutex);
  716. }
  717. mutex_unlock(&audit_filter_mutex);
  718. mutex_unlock(&audit_cmd_mutex);
  719. return 0;
  720. }
  721. static void audit_schedule_prune(void)
  722. {
  723. kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
  724. }
  725. /*
  726. * ... and that one is done if evict_chunk() decides to delay until the end
  727. * of syscall. Runs synchronously.
  728. */
  729. void audit_kill_trees(struct list_head *list)
  730. {
  731. mutex_lock(&audit_cmd_mutex);
  732. mutex_lock(&audit_filter_mutex);
  733. while (!list_empty(list)) {
  734. struct audit_tree *victim;
  735. victim = list_entry(list->next, struct audit_tree, list);
  736. kill_rules(victim);
  737. list_del_init(&victim->list);
  738. mutex_unlock(&audit_filter_mutex);
  739. prune_one(victim);
  740. mutex_lock(&audit_filter_mutex);
  741. }
  742. mutex_unlock(&audit_filter_mutex);
  743. mutex_unlock(&audit_cmd_mutex);
  744. }
  745. /*
  746. * Here comes the stuff asynchronous to auditctl operations
  747. */
  748. static void evict_chunk(struct audit_chunk *chunk)
  749. {
  750. struct audit_tree *owner;
  751. struct list_head *postponed = audit_killed_trees();
  752. int need_prune = 0;
  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. if (!postponed) {
  767. kill_rules(owner);
  768. list_move(&owner->list, &prune_list);
  769. need_prune = 1;
  770. } else {
  771. list_move(&owner->list, postponed);
  772. }
  773. spin_lock(&hash_lock);
  774. }
  775. list_del_rcu(&chunk->hash);
  776. for (n = 0; n < chunk->count; n++)
  777. list_del_init(&chunk->owners[n].list);
  778. spin_unlock(&hash_lock);
  779. if (need_prune)
  780. audit_schedule_prune();
  781. mutex_unlock(&audit_filter_mutex);
  782. }
  783. static int audit_tree_handle_event(struct fsnotify_group *group,
  784. struct fsnotify_mark *inode_mark,
  785. struct fsnotify_mark *vfsmonut_mark,
  786. struct fsnotify_event *event)
  787. {
  788. BUG();
  789. return -EOPNOTSUPP;
  790. }
  791. static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
  792. {
  793. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  794. evict_chunk(chunk);
  795. fsnotify_put_mark(entry);
  796. }
  797. static bool audit_tree_send_event(struct fsnotify_group *group, struct inode *inode,
  798. struct fsnotify_mark *inode_mark,
  799. struct fsnotify_mark *vfsmount_mark,
  800. __u32 mask, void *data, int data_type)
  801. {
  802. return false;
  803. }
  804. static const struct fsnotify_ops audit_tree_ops = {
  805. .handle_event = audit_tree_handle_event,
  806. .should_send_event = audit_tree_send_event,
  807. .free_group_priv = NULL,
  808. .free_event_priv = NULL,
  809. .freeing_mark = audit_tree_freeing_mark,
  810. };
  811. static int __init audit_tree_init(void)
  812. {
  813. int i;
  814. audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
  815. if (IS_ERR(audit_tree_group))
  816. audit_panic("cannot initialize fsnotify group for rectree watches");
  817. for (i = 0; i < HASH_SIZE; i++)
  818. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  819. return 0;
  820. }
  821. __initcall(audit_tree_init);