audit_tree.c 22 KB

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