audit_tree.c 22 KB

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