audit_tree.c 22 KB

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