audit_tree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. return chunk;
  139. }
  140. enum {HASH_SIZE = 128};
  141. static struct list_head chunk_hash_heads[HASH_SIZE];
  142. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
  143. static inline struct list_head *chunk_hash(const struct inode *inode)
  144. {
  145. unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
  146. return chunk_hash_heads + n % HASH_SIZE;
  147. }
  148. /* hash_lock & entry->lock is held by caller */
  149. static void insert_hash(struct audit_chunk *chunk)
  150. {
  151. struct fsnotify_mark *entry = &chunk->mark;
  152. struct list_head *list;
  153. if (!entry->i.inode)
  154. return;
  155. list = chunk_hash(entry->i.inode);
  156. list_add_rcu(&chunk->hash, list);
  157. }
  158. /* called under rcu_read_lock */
  159. struct audit_chunk *audit_tree_lookup(const struct inode *inode)
  160. {
  161. struct list_head *list = chunk_hash(inode);
  162. struct audit_chunk *p;
  163. list_for_each_entry_rcu(p, list, hash) {
  164. /* mark.inode may have gone NULL, but who cares? */
  165. if (p->mark.i.inode == inode) {
  166. atomic_long_inc(&p->refs);
  167. return p;
  168. }
  169. }
  170. return NULL;
  171. }
  172. int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
  173. {
  174. int n;
  175. for (n = 0; n < chunk->count; n++)
  176. if (chunk->owners[n].owner == tree)
  177. return 1;
  178. return 0;
  179. }
  180. /* tagging and untagging inodes with trees */
  181. static struct audit_chunk *find_chunk(struct node *p)
  182. {
  183. int index = p->index & ~(1U<<31);
  184. p -= index;
  185. return container_of(p, struct audit_chunk, owners[0]);
  186. }
  187. static void untag_chunk(struct node *p)
  188. {
  189. struct audit_chunk *chunk = find_chunk(p);
  190. struct fsnotify_mark *entry = &chunk->mark;
  191. struct audit_chunk *new = NULL;
  192. struct audit_tree *owner;
  193. int size = chunk->count - 1;
  194. int i, j;
  195. fsnotify_get_mark(entry);
  196. spin_unlock(&hash_lock);
  197. if (size)
  198. new = alloc_chunk(size);
  199. spin_lock(&entry->lock);
  200. if (chunk->dead || !entry->i.inode) {
  201. spin_unlock(&entry->lock);
  202. if (new)
  203. free_chunk(new);
  204. goto out;
  205. }
  206. owner = p->owner;
  207. if (!size) {
  208. chunk->dead = 1;
  209. spin_lock(&hash_lock);
  210. list_del_init(&chunk->trees);
  211. if (owner->root == chunk)
  212. owner->root = NULL;
  213. list_del_init(&p->list);
  214. list_del_rcu(&chunk->hash);
  215. spin_unlock(&hash_lock);
  216. spin_unlock(&entry->lock);
  217. fsnotify_destroy_mark(entry, audit_tree_group);
  218. goto out;
  219. }
  220. if (!new)
  221. goto Fallback;
  222. fsnotify_duplicate_mark(&new->mark, entry);
  223. if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, NULL, 1)) {
  224. fsnotify_put_mark(&new->mark);
  225. goto Fallback;
  226. }
  227. chunk->dead = 1;
  228. spin_lock(&hash_lock);
  229. list_replace_init(&chunk->trees, &new->trees);
  230. if (owner->root == chunk) {
  231. list_del_init(&owner->same_root);
  232. owner->root = NULL;
  233. }
  234. for (i = j = 0; j <= size; i++, j++) {
  235. struct audit_tree *s;
  236. if (&chunk->owners[j] == p) {
  237. list_del_init(&p->list);
  238. i--;
  239. continue;
  240. }
  241. s = chunk->owners[j].owner;
  242. new->owners[i].owner = s;
  243. new->owners[i].index = chunk->owners[j].index - j + i;
  244. if (!s) /* result of earlier fallback */
  245. continue;
  246. get_tree(s);
  247. list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
  248. }
  249. list_replace_rcu(&chunk->hash, &new->hash);
  250. list_for_each_entry(owner, &new->trees, same_root)
  251. owner->root = new;
  252. spin_unlock(&hash_lock);
  253. spin_unlock(&entry->lock);
  254. fsnotify_destroy_mark(entry, audit_tree_group);
  255. fsnotify_put_mark(&new->mark); /* drop initial reference */
  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_destroy_mark(entry, audit_tree_group);
  291. fsnotify_put_mark(entry);
  292. return 0;
  293. }
  294. chunk->owners[0].index = (1U << 31);
  295. chunk->owners[0].owner = tree;
  296. get_tree(tree);
  297. list_add(&chunk->owners[0].list, &tree->chunks);
  298. if (!tree->root) {
  299. tree->root = chunk;
  300. list_add(&tree->same_root, &chunk->trees);
  301. }
  302. insert_hash(chunk);
  303. spin_unlock(&hash_lock);
  304. spin_unlock(&entry->lock);
  305. fsnotify_put_mark(entry); /* drop initial reference */
  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_destroy_mark(chunk_entry, audit_tree_group);
  361. fsnotify_put_mark(chunk_entry);
  362. fsnotify_put_mark(old_entry);
  363. return 0;
  364. }
  365. list_replace_init(&old->trees, &chunk->trees);
  366. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  367. struct audit_tree *s = old->owners[n].owner;
  368. p->owner = s;
  369. p->index = old->owners[n].index;
  370. if (!s) /* result of fallback in untag */
  371. continue;
  372. get_tree(s);
  373. list_replace_init(&old->owners[n].list, &p->list);
  374. }
  375. p->index = (chunk->count - 1) | (1U<<31);
  376. p->owner = tree;
  377. get_tree(tree);
  378. list_add(&p->list, &tree->chunks);
  379. list_replace_rcu(&old->hash, &chunk->hash);
  380. list_for_each_entry(owner, &chunk->trees, same_root)
  381. owner->root = chunk;
  382. old->dead = 1;
  383. if (!tree->root) {
  384. tree->root = chunk;
  385. list_add(&tree->same_root, &chunk->trees);
  386. }
  387. spin_unlock(&hash_lock);
  388. spin_unlock(&chunk_entry->lock);
  389. spin_unlock(&old_entry->lock);
  390. fsnotify_destroy_mark(old_entry, audit_tree_group);
  391. fsnotify_put_mark(chunk_entry); /* drop initial reference */
  392. fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
  393. return 0;
  394. }
  395. static void audit_log_remove_rule(struct audit_krule *rule)
  396. {
  397. struct audit_buffer *ab;
  398. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  399. if (unlikely(!ab))
  400. return;
  401. audit_log_format(ab, "op=");
  402. audit_log_string(ab, "remove rule");
  403. audit_log_format(ab, " dir=");
  404. audit_log_untrustedstring(ab, rule->tree->pathname);
  405. audit_log_key(ab, rule->filterkey);
  406. audit_log_format(ab, " list=%d res=1", rule->listnr);
  407. audit_log_end(ab);
  408. }
  409. static void kill_rules(struct audit_tree *tree)
  410. {
  411. struct audit_krule *rule, *next;
  412. struct audit_entry *entry;
  413. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  414. entry = container_of(rule, struct audit_entry, rule);
  415. list_del_init(&rule->rlist);
  416. if (rule->tree) {
  417. /* not a half-baked one */
  418. audit_log_remove_rule(rule);
  419. rule->tree = NULL;
  420. list_del_rcu(&entry->list);
  421. list_del(&entry->rule.list);
  422. call_rcu(&entry->rcu, audit_free_rule_rcu);
  423. }
  424. }
  425. }
  426. /*
  427. * finish killing struct audit_tree
  428. */
  429. static void prune_one(struct audit_tree *victim)
  430. {
  431. spin_lock(&hash_lock);
  432. while (!list_empty(&victim->chunks)) {
  433. struct node *p;
  434. p = list_entry(victim->chunks.next, struct node, list);
  435. untag_chunk(p);
  436. }
  437. spin_unlock(&hash_lock);
  438. put_tree(victim);
  439. }
  440. /* trim the uncommitted chunks from tree */
  441. static void trim_marked(struct audit_tree *tree)
  442. {
  443. struct list_head *p, *q;
  444. spin_lock(&hash_lock);
  445. if (tree->goner) {
  446. spin_unlock(&hash_lock);
  447. return;
  448. }
  449. /* reorder */
  450. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  451. struct node *node = list_entry(p, struct node, list);
  452. q = p->next;
  453. if (node->index & (1U<<31)) {
  454. list_del_init(p);
  455. list_add(p, &tree->chunks);
  456. }
  457. }
  458. while (!list_empty(&tree->chunks)) {
  459. struct node *node;
  460. node = list_entry(tree->chunks.next, struct node, list);
  461. /* have we run out of marked? */
  462. if (!(node->index & (1U<<31)))
  463. break;
  464. untag_chunk(node);
  465. }
  466. if (!tree->root && !tree->goner) {
  467. tree->goner = 1;
  468. spin_unlock(&hash_lock);
  469. mutex_lock(&audit_filter_mutex);
  470. kill_rules(tree);
  471. list_del_init(&tree->list);
  472. mutex_unlock(&audit_filter_mutex);
  473. prune_one(tree);
  474. } else {
  475. spin_unlock(&hash_lock);
  476. }
  477. }
  478. static void audit_schedule_prune(void);
  479. /* called with audit_filter_mutex */
  480. int audit_remove_tree_rule(struct audit_krule *rule)
  481. {
  482. struct audit_tree *tree;
  483. tree = rule->tree;
  484. if (tree) {
  485. spin_lock(&hash_lock);
  486. list_del_init(&rule->rlist);
  487. if (list_empty(&tree->rules) && !tree->goner) {
  488. tree->root = NULL;
  489. list_del_init(&tree->same_root);
  490. tree->goner = 1;
  491. list_move(&tree->list, &prune_list);
  492. rule->tree = NULL;
  493. spin_unlock(&hash_lock);
  494. audit_schedule_prune();
  495. return 1;
  496. }
  497. rule->tree = NULL;
  498. spin_unlock(&hash_lock);
  499. return 1;
  500. }
  501. return 0;
  502. }
  503. static int compare_root(struct vfsmount *mnt, void *arg)
  504. {
  505. return mnt->mnt_root->d_inode == arg;
  506. }
  507. void audit_trim_trees(void)
  508. {
  509. struct list_head cursor;
  510. mutex_lock(&audit_filter_mutex);
  511. list_add(&cursor, &tree_list);
  512. while (cursor.next != &tree_list) {
  513. struct audit_tree *tree;
  514. struct path path;
  515. struct vfsmount *root_mnt;
  516. struct node *node;
  517. int err;
  518. tree = container_of(cursor.next, struct audit_tree, list);
  519. get_tree(tree);
  520. list_del(&cursor);
  521. list_add(&cursor, &tree->list);
  522. mutex_unlock(&audit_filter_mutex);
  523. err = kern_path(tree->pathname, 0, &path);
  524. if (err)
  525. goto skip_it;
  526. root_mnt = collect_mounts(&path);
  527. path_put(&path);
  528. if (IS_ERR(root_mnt))
  529. goto skip_it;
  530. spin_lock(&hash_lock);
  531. list_for_each_entry(node, &tree->chunks, list) {
  532. struct audit_chunk *chunk = find_chunk(node);
  533. /* this could be NULL if the watch is dying else where... */
  534. struct inode *inode = chunk->mark.i.inode;
  535. node->index |= 1U<<31;
  536. if (iterate_mounts(compare_root, inode, root_mnt))
  537. node->index &= ~(1U<<31);
  538. }
  539. spin_unlock(&hash_lock);
  540. trim_marked(tree);
  541. put_tree(tree);
  542. drop_collected_mounts(root_mnt);
  543. skip_it:
  544. mutex_lock(&audit_filter_mutex);
  545. }
  546. list_del(&cursor);
  547. mutex_unlock(&audit_filter_mutex);
  548. }
  549. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  550. {
  551. if (pathname[0] != '/' ||
  552. rule->listnr != AUDIT_FILTER_EXIT ||
  553. op != Audit_equal ||
  554. rule->inode_f || rule->watch || rule->tree)
  555. return -EINVAL;
  556. rule->tree = alloc_tree(pathname);
  557. if (!rule->tree)
  558. return -ENOMEM;
  559. return 0;
  560. }
  561. void audit_put_tree(struct audit_tree *tree)
  562. {
  563. put_tree(tree);
  564. }
  565. static int tag_mount(struct vfsmount *mnt, void *arg)
  566. {
  567. return tag_chunk(mnt->mnt_root->d_inode, arg);
  568. }
  569. /* called with audit_filter_mutex */
  570. int audit_add_tree_rule(struct audit_krule *rule)
  571. {
  572. struct audit_tree *seed = rule->tree, *tree;
  573. struct path path;
  574. struct vfsmount *mnt;
  575. int err;
  576. list_for_each_entry(tree, &tree_list, list) {
  577. if (!strcmp(seed->pathname, tree->pathname)) {
  578. put_tree(seed);
  579. rule->tree = tree;
  580. list_add(&rule->rlist, &tree->rules);
  581. return 0;
  582. }
  583. }
  584. tree = seed;
  585. list_add(&tree->list, &tree_list);
  586. list_add(&rule->rlist, &tree->rules);
  587. /* do not set rule->tree yet */
  588. mutex_unlock(&audit_filter_mutex);
  589. err = kern_path(tree->pathname, 0, &path);
  590. if (err)
  591. goto Err;
  592. mnt = collect_mounts(&path);
  593. path_put(&path);
  594. if (IS_ERR(mnt)) {
  595. err = PTR_ERR(mnt);
  596. goto Err;
  597. }
  598. get_tree(tree);
  599. err = iterate_mounts(tag_mount, tree, mnt);
  600. drop_collected_mounts(mnt);
  601. if (!err) {
  602. struct node *node;
  603. spin_lock(&hash_lock);
  604. list_for_each_entry(node, &tree->chunks, list)
  605. node->index &= ~(1U<<31);
  606. spin_unlock(&hash_lock);
  607. } else {
  608. trim_marked(tree);
  609. goto Err;
  610. }
  611. mutex_lock(&audit_filter_mutex);
  612. if (list_empty(&rule->rlist)) {
  613. put_tree(tree);
  614. return -ENOENT;
  615. }
  616. rule->tree = tree;
  617. put_tree(tree);
  618. return 0;
  619. Err:
  620. mutex_lock(&audit_filter_mutex);
  621. list_del_init(&tree->list);
  622. list_del_init(&tree->rules);
  623. put_tree(tree);
  624. return err;
  625. }
  626. int audit_tag_tree(char *old, char *new)
  627. {
  628. struct list_head cursor, barrier;
  629. int failed = 0;
  630. struct path path1, path2;
  631. struct vfsmount *tagged;
  632. int err;
  633. err = kern_path(new, 0, &path2);
  634. if (err)
  635. return err;
  636. tagged = collect_mounts(&path2);
  637. path_put(&path2);
  638. if (IS_ERR(tagged))
  639. return PTR_ERR(tagged);
  640. err = kern_path(old, 0, &path1);
  641. if (err) {
  642. drop_collected_mounts(tagged);
  643. return err;
  644. }
  645. mutex_lock(&audit_filter_mutex);
  646. list_add(&barrier, &tree_list);
  647. list_add(&cursor, &barrier);
  648. while (cursor.next != &tree_list) {
  649. struct audit_tree *tree;
  650. int good_one = 0;
  651. tree = container_of(cursor.next, struct audit_tree, list);
  652. get_tree(tree);
  653. list_del(&cursor);
  654. list_add(&cursor, &tree->list);
  655. mutex_unlock(&audit_filter_mutex);
  656. err = kern_path(tree->pathname, 0, &path2);
  657. if (!err) {
  658. good_one = path_is_under(&path1, &path2);
  659. path_put(&path2);
  660. }
  661. if (!good_one) {
  662. put_tree(tree);
  663. mutex_lock(&audit_filter_mutex);
  664. continue;
  665. }
  666. failed = iterate_mounts(tag_mount, tree, tagged);
  667. if (failed) {
  668. put_tree(tree);
  669. mutex_lock(&audit_filter_mutex);
  670. break;
  671. }
  672. mutex_lock(&audit_filter_mutex);
  673. spin_lock(&hash_lock);
  674. if (!tree->goner) {
  675. list_del(&tree->list);
  676. list_add(&tree->list, &tree_list);
  677. }
  678. spin_unlock(&hash_lock);
  679. put_tree(tree);
  680. }
  681. while (barrier.prev != &tree_list) {
  682. struct audit_tree *tree;
  683. tree = container_of(barrier.prev, struct audit_tree, list);
  684. get_tree(tree);
  685. list_del(&tree->list);
  686. list_add(&tree->list, &barrier);
  687. mutex_unlock(&audit_filter_mutex);
  688. if (!failed) {
  689. struct node *node;
  690. spin_lock(&hash_lock);
  691. list_for_each_entry(node, &tree->chunks, list)
  692. node->index &= ~(1U<<31);
  693. spin_unlock(&hash_lock);
  694. } else {
  695. trim_marked(tree);
  696. }
  697. put_tree(tree);
  698. mutex_lock(&audit_filter_mutex);
  699. }
  700. list_del(&barrier);
  701. list_del(&cursor);
  702. mutex_unlock(&audit_filter_mutex);
  703. path_put(&path1);
  704. drop_collected_mounts(tagged);
  705. return failed;
  706. }
  707. /*
  708. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  709. * Runs from a separate thread.
  710. */
  711. static int prune_tree_thread(void *unused)
  712. {
  713. mutex_lock(&audit_cmd_mutex);
  714. mutex_lock(&audit_filter_mutex);
  715. while (!list_empty(&prune_list)) {
  716. struct audit_tree *victim;
  717. victim = list_entry(prune_list.next, struct audit_tree, list);
  718. list_del_init(&victim->list);
  719. mutex_unlock(&audit_filter_mutex);
  720. prune_one(victim);
  721. mutex_lock(&audit_filter_mutex);
  722. }
  723. mutex_unlock(&audit_filter_mutex);
  724. mutex_unlock(&audit_cmd_mutex);
  725. return 0;
  726. }
  727. static void audit_schedule_prune(void)
  728. {
  729. kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
  730. }
  731. /*
  732. * ... and that one is done if evict_chunk() decides to delay until the end
  733. * of syscall. Runs synchronously.
  734. */
  735. void audit_kill_trees(struct list_head *list)
  736. {
  737. mutex_lock(&audit_cmd_mutex);
  738. mutex_lock(&audit_filter_mutex);
  739. while (!list_empty(list)) {
  740. struct audit_tree *victim;
  741. victim = list_entry(list->next, struct audit_tree, list);
  742. kill_rules(victim);
  743. list_del_init(&victim->list);
  744. mutex_unlock(&audit_filter_mutex);
  745. prune_one(victim);
  746. mutex_lock(&audit_filter_mutex);
  747. }
  748. mutex_unlock(&audit_filter_mutex);
  749. mutex_unlock(&audit_cmd_mutex);
  750. }
  751. /*
  752. * Here comes the stuff asynchronous to auditctl operations
  753. */
  754. static void evict_chunk(struct audit_chunk *chunk)
  755. {
  756. struct audit_tree *owner;
  757. struct list_head *postponed = audit_killed_trees();
  758. int need_prune = 0;
  759. int n;
  760. if (chunk->dead)
  761. return;
  762. chunk->dead = 1;
  763. mutex_lock(&audit_filter_mutex);
  764. spin_lock(&hash_lock);
  765. while (!list_empty(&chunk->trees)) {
  766. owner = list_entry(chunk->trees.next,
  767. struct audit_tree, same_root);
  768. owner->goner = 1;
  769. owner->root = NULL;
  770. list_del_init(&owner->same_root);
  771. spin_unlock(&hash_lock);
  772. if (!postponed) {
  773. kill_rules(owner);
  774. list_move(&owner->list, &prune_list);
  775. need_prune = 1;
  776. } else {
  777. list_move(&owner->list, postponed);
  778. }
  779. spin_lock(&hash_lock);
  780. }
  781. list_del_rcu(&chunk->hash);
  782. for (n = 0; n < chunk->count; n++)
  783. list_del_init(&chunk->owners[n].list);
  784. spin_unlock(&hash_lock);
  785. if (need_prune)
  786. audit_schedule_prune();
  787. mutex_unlock(&audit_filter_mutex);
  788. }
  789. static int audit_tree_handle_event(struct fsnotify_group *group,
  790. struct fsnotify_mark *inode_mark,
  791. struct fsnotify_mark *vfsmonut_mark,
  792. struct fsnotify_event *event)
  793. {
  794. BUG();
  795. return -EOPNOTSUPP;
  796. }
  797. static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
  798. {
  799. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  800. evict_chunk(chunk);
  801. /*
  802. * We are guaranteed to have at least one reference to the mark from
  803. * either the inode or the caller of fsnotify_destroy_mark().
  804. */
  805. BUG_ON(atomic_read(&entry->refcnt) < 1);
  806. }
  807. static bool audit_tree_send_event(struct fsnotify_group *group, struct inode *inode,
  808. struct fsnotify_mark *inode_mark,
  809. struct fsnotify_mark *vfsmount_mark,
  810. __u32 mask, void *data, int data_type)
  811. {
  812. return false;
  813. }
  814. static const struct fsnotify_ops audit_tree_ops = {
  815. .handle_event = audit_tree_handle_event,
  816. .should_send_event = audit_tree_send_event,
  817. .free_group_priv = NULL,
  818. .free_event_priv = NULL,
  819. .freeing_mark = audit_tree_freeing_mark,
  820. };
  821. static int __init audit_tree_init(void)
  822. {
  823. int i;
  824. audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
  825. if (IS_ERR(audit_tree_group))
  826. audit_panic("cannot initialize fsnotify group for rectree watches");
  827. for (i = 0; i < HASH_SIZE; i++)
  828. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  829. return 0;
  830. }
  831. __initcall(audit_tree_init);