audit_tree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. #include "audit.h"
  2. #include <linux/fsnotify_backend.h>
  3. #include <linux/namei.h>
  4. #include <linux/mount.h>
  5. #include <linux/kthread.h>
  6. #include <linux/slab.h>
  7. struct audit_tree;
  8. struct audit_chunk;
  9. struct audit_tree {
  10. atomic_t count;
  11. int goner;
  12. struct audit_chunk *root;
  13. struct list_head chunks;
  14. struct list_head rules;
  15. struct list_head list;
  16. struct list_head same_root;
  17. struct rcu_head head;
  18. char pathname[];
  19. };
  20. struct audit_chunk {
  21. struct list_head hash;
  22. struct fsnotify_mark mark;
  23. struct list_head trees; /* with root here */
  24. int dead;
  25. int count;
  26. atomic_long_t refs;
  27. struct rcu_head head;
  28. struct node {
  29. struct list_head list;
  30. struct audit_tree *owner;
  31. unsigned index; /* index; upper bit indicates 'will prune' */
  32. } owners[];
  33. };
  34. static LIST_HEAD(tree_list);
  35. static LIST_HEAD(prune_list);
  36. /*
  37. * One struct chunk is attached to each inode of interest.
  38. * We replace struct chunk on tagging/untagging.
  39. * Rules have pointer to struct audit_tree.
  40. * Rules have struct list_head rlist forming a list of rules over
  41. * the same tree.
  42. * References to struct chunk are collected at audit_inode{,_child}()
  43. * time and used in AUDIT_TREE rule matching.
  44. * These references are dropped at the same time we are calling
  45. * audit_free_names(), etc.
  46. *
  47. * Cyclic lists galore:
  48. * tree.chunks anchors chunk.owners[].list hash_lock
  49. * tree.rules anchors rule.rlist audit_filter_mutex
  50. * chunk.trees anchors tree.same_root hash_lock
  51. * chunk.hash is a hash with middle bits of watch.inode as
  52. * a hash function. RCU, hash_lock
  53. *
  54. * tree is refcounted; one reference for "some rules on rules_list refer to
  55. * it", one for each chunk with pointer to it.
  56. *
  57. * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
  58. * of watch contributes 1 to .refs).
  59. *
  60. * node.index allows to get from node.list to containing chunk.
  61. * MSB of that sucker is stolen to mark taggings that we might have to
  62. * revert - several operations have very unpleasant cleanup logics and
  63. * that makes a difference. Some.
  64. */
  65. static struct fsnotify_group *audit_tree_group;
  66. static struct audit_tree *alloc_tree(const char *s)
  67. {
  68. struct audit_tree *tree;
  69. tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
  70. if (tree) {
  71. atomic_set(&tree->count, 1);
  72. tree->goner = 0;
  73. INIT_LIST_HEAD(&tree->chunks);
  74. INIT_LIST_HEAD(&tree->rules);
  75. INIT_LIST_HEAD(&tree->list);
  76. INIT_LIST_HEAD(&tree->same_root);
  77. tree->root = NULL;
  78. strcpy(tree->pathname, s);
  79. }
  80. return tree;
  81. }
  82. static inline void get_tree(struct audit_tree *tree)
  83. {
  84. atomic_inc(&tree->count);
  85. }
  86. static 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;
  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->i.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(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.i.inode, NULL, 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(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;
  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, NULL, 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(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 *old_entry, *chunk_entry;
  314. struct audit_tree *owner;
  315. struct audit_chunk *chunk, *old;
  316. struct node *p;
  317. int n;
  318. old_entry = fsnotify_find_inode_mark(audit_tree_group, inode);
  319. if (!old_entry)
  320. return create_chunk(inode, tree);
  321. old = container_of(old_entry, struct audit_chunk, mark);
  322. /* are we already there? */
  323. spin_lock(&hash_lock);
  324. for (n = 0; n < old->count; n++) {
  325. if (old->owners[n].owner == tree) {
  326. spin_unlock(&hash_lock);
  327. fsnotify_put_mark(old_entry);
  328. return 0;
  329. }
  330. }
  331. spin_unlock(&hash_lock);
  332. chunk = alloc_chunk(old->count + 1);
  333. if (!chunk) {
  334. fsnotify_put_mark(old_entry);
  335. return -ENOMEM;
  336. }
  337. chunk_entry = &chunk->mark;
  338. spin_lock(&old_entry->lock);
  339. if (!old_entry->i.inode) {
  340. /* old_entry is being shot, lets just lie */
  341. spin_unlock(&old_entry->lock);
  342. fsnotify_put_mark(old_entry);
  343. free_chunk(chunk);
  344. return -ENOENT;
  345. }
  346. fsnotify_duplicate_mark(chunk_entry, old_entry);
  347. if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, NULL, 1)) {
  348. spin_unlock(&old_entry->lock);
  349. free_chunk(chunk);
  350. fsnotify_put_mark(old_entry);
  351. return -ENOSPC;
  352. }
  353. /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
  354. spin_lock(&chunk_entry->lock);
  355. spin_lock(&hash_lock);
  356. /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
  357. if (tree->goner) {
  358. spin_unlock(&hash_lock);
  359. chunk->dead = 1;
  360. spin_unlock(&chunk_entry->lock);
  361. spin_unlock(&old_entry->lock);
  362. fsnotify_destroy_mark(chunk_entry);
  363. fsnotify_put_mark(chunk_entry);
  364. fsnotify_put_mark(old_entry);
  365. return 0;
  366. }
  367. list_replace_init(&old->trees, &chunk->trees);
  368. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  369. struct audit_tree *s = old->owners[n].owner;
  370. p->owner = s;
  371. p->index = old->owners[n].index;
  372. if (!s) /* result of fallback in untag */
  373. continue;
  374. get_tree(s);
  375. list_replace_init(&old->owners[n].list, &p->list);
  376. }
  377. p->index = (chunk->count - 1) | (1U<<31);
  378. p->owner = tree;
  379. get_tree(tree);
  380. list_add(&p->list, &tree->chunks);
  381. list_replace_rcu(&old->hash, &chunk->hash);
  382. list_for_each_entry(owner, &chunk->trees, same_root)
  383. owner->root = chunk;
  384. old->dead = 1;
  385. if (!tree->root) {
  386. tree->root = chunk;
  387. list_add(&tree->same_root, &chunk->trees);
  388. }
  389. spin_unlock(&hash_lock);
  390. spin_unlock(&chunk_entry->lock);
  391. spin_unlock(&old_entry->lock);
  392. fsnotify_destroy_mark(old_entry);
  393. fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
  394. fsnotify_put_mark(old_entry); /* and kill it */
  395. return 0;
  396. }
  397. static void kill_rules(struct audit_tree *tree)
  398. {
  399. struct audit_krule *rule, *next;
  400. struct audit_entry *entry;
  401. struct audit_buffer *ab;
  402. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  403. entry = container_of(rule, struct audit_entry, rule);
  404. list_del_init(&rule->rlist);
  405. if (rule->tree) {
  406. /* not a half-baked one */
  407. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  408. audit_log_format(ab, "op=");
  409. audit_log_string(ab, "remove rule");
  410. audit_log_format(ab, " dir=");
  411. audit_log_untrustedstring(ab, rule->tree->pathname);
  412. audit_log_key(ab, rule->filterkey);
  413. audit_log_format(ab, " list=%d res=1", rule->listnr);
  414. audit_log_end(ab);
  415. rule->tree = NULL;
  416. list_del_rcu(&entry->list);
  417. list_del(&entry->rule.list);
  418. call_rcu(&entry->rcu, audit_free_rule_rcu);
  419. }
  420. }
  421. }
  422. /*
  423. * finish killing struct audit_tree
  424. */
  425. static void prune_one(struct audit_tree *victim)
  426. {
  427. spin_lock(&hash_lock);
  428. while (!list_empty(&victim->chunks)) {
  429. struct node *p;
  430. p = list_entry(victim->chunks.next, struct node, list);
  431. untag_chunk(p);
  432. }
  433. spin_unlock(&hash_lock);
  434. put_tree(victim);
  435. }
  436. /* trim the uncommitted chunks from tree */
  437. static void trim_marked(struct audit_tree *tree)
  438. {
  439. struct list_head *p, *q;
  440. spin_lock(&hash_lock);
  441. if (tree->goner) {
  442. spin_unlock(&hash_lock);
  443. return;
  444. }
  445. /* reorder */
  446. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  447. struct node *node = list_entry(p, struct node, list);
  448. q = p->next;
  449. if (node->index & (1U<<31)) {
  450. list_del_init(p);
  451. list_add(p, &tree->chunks);
  452. }
  453. }
  454. while (!list_empty(&tree->chunks)) {
  455. struct node *node;
  456. node = list_entry(tree->chunks.next, struct node, list);
  457. /* have we run out of marked? */
  458. if (!(node->index & (1U<<31)))
  459. break;
  460. untag_chunk(node);
  461. }
  462. if (!tree->root && !tree->goner) {
  463. tree->goner = 1;
  464. spin_unlock(&hash_lock);
  465. mutex_lock(&audit_filter_mutex);
  466. kill_rules(tree);
  467. list_del_init(&tree->list);
  468. mutex_unlock(&audit_filter_mutex);
  469. prune_one(tree);
  470. } else {
  471. spin_unlock(&hash_lock);
  472. }
  473. }
  474. static void audit_schedule_prune(void);
  475. /* called with audit_filter_mutex */
  476. int audit_remove_tree_rule(struct audit_krule *rule)
  477. {
  478. struct audit_tree *tree;
  479. tree = rule->tree;
  480. if (tree) {
  481. spin_lock(&hash_lock);
  482. list_del_init(&rule->rlist);
  483. if (list_empty(&tree->rules) && !tree->goner) {
  484. tree->root = NULL;
  485. list_del_init(&tree->same_root);
  486. tree->goner = 1;
  487. list_move(&tree->list, &prune_list);
  488. rule->tree = NULL;
  489. spin_unlock(&hash_lock);
  490. audit_schedule_prune();
  491. return 1;
  492. }
  493. rule->tree = NULL;
  494. spin_unlock(&hash_lock);
  495. return 1;
  496. }
  497. return 0;
  498. }
  499. static int compare_root(struct vfsmount *mnt, void *arg)
  500. {
  501. return mnt->mnt_root->d_inode == arg;
  502. }
  503. void audit_trim_trees(void)
  504. {
  505. struct list_head cursor;
  506. mutex_lock(&audit_filter_mutex);
  507. list_add(&cursor, &tree_list);
  508. while (cursor.next != &tree_list) {
  509. struct audit_tree *tree;
  510. struct path path;
  511. struct vfsmount *root_mnt;
  512. struct node *node;
  513. int err;
  514. tree = container_of(cursor.next, struct audit_tree, list);
  515. get_tree(tree);
  516. list_del(&cursor);
  517. list_add(&cursor, &tree->list);
  518. mutex_unlock(&audit_filter_mutex);
  519. err = kern_path(tree->pathname, 0, &path);
  520. if (err)
  521. goto skip_it;
  522. root_mnt = collect_mounts(&path);
  523. path_put(&path);
  524. if (!root_mnt)
  525. goto skip_it;
  526. spin_lock(&hash_lock);
  527. list_for_each_entry(node, &tree->chunks, list) {
  528. struct audit_chunk *chunk = find_chunk(node);
  529. /* this could be NULL if the watch is dieing else where... */
  530. struct inode *inode = chunk->mark.i.inode;
  531. node->index |= 1U<<31;
  532. if (iterate_mounts(compare_root, inode, root_mnt))
  533. node->index &= ~(1U<<31);
  534. }
  535. spin_unlock(&hash_lock);
  536. trim_marked(tree);
  537. put_tree(tree);
  538. drop_collected_mounts(root_mnt);
  539. skip_it:
  540. mutex_lock(&audit_filter_mutex);
  541. }
  542. list_del(&cursor);
  543. mutex_unlock(&audit_filter_mutex);
  544. }
  545. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  546. {
  547. if (pathname[0] != '/' ||
  548. rule->listnr != AUDIT_FILTER_EXIT ||
  549. op != Audit_equal ||
  550. rule->inode_f || rule->watch || rule->tree)
  551. return -EINVAL;
  552. rule->tree = alloc_tree(pathname);
  553. if (!rule->tree)
  554. return -ENOMEM;
  555. return 0;
  556. }
  557. void audit_put_tree(struct audit_tree *tree)
  558. {
  559. put_tree(tree);
  560. }
  561. static int tag_mount(struct vfsmount *mnt, void *arg)
  562. {
  563. return tag_chunk(mnt->mnt_root->d_inode, arg);
  564. }
  565. /* called with audit_filter_mutex */
  566. int audit_add_tree_rule(struct audit_krule *rule)
  567. {
  568. struct audit_tree *seed = rule->tree, *tree;
  569. struct path path;
  570. struct vfsmount *mnt;
  571. int err;
  572. list_for_each_entry(tree, &tree_list, list) {
  573. if (!strcmp(seed->pathname, tree->pathname)) {
  574. put_tree(seed);
  575. rule->tree = tree;
  576. list_add(&rule->rlist, &tree->rules);
  577. return 0;
  578. }
  579. }
  580. tree = seed;
  581. list_add(&tree->list, &tree_list);
  582. list_add(&rule->rlist, &tree->rules);
  583. /* do not set rule->tree yet */
  584. mutex_unlock(&audit_filter_mutex);
  585. err = kern_path(tree->pathname, 0, &path);
  586. if (err)
  587. goto Err;
  588. mnt = collect_mounts(&path);
  589. path_put(&path);
  590. if (!mnt) {
  591. err = -ENOMEM;
  592. goto Err;
  593. }
  594. get_tree(tree);
  595. err = iterate_mounts(tag_mount, tree, mnt);
  596. drop_collected_mounts(mnt);
  597. if (!err) {
  598. struct node *node;
  599. spin_lock(&hash_lock);
  600. list_for_each_entry(node, &tree->chunks, list)
  601. node->index &= ~(1U<<31);
  602. spin_unlock(&hash_lock);
  603. } else {
  604. trim_marked(tree);
  605. goto Err;
  606. }
  607. mutex_lock(&audit_filter_mutex);
  608. if (list_empty(&rule->rlist)) {
  609. put_tree(tree);
  610. return -ENOENT;
  611. }
  612. rule->tree = tree;
  613. put_tree(tree);
  614. return 0;
  615. Err:
  616. mutex_lock(&audit_filter_mutex);
  617. list_del_init(&tree->list);
  618. list_del_init(&tree->rules);
  619. put_tree(tree);
  620. return err;
  621. }
  622. int audit_tag_tree(char *old, char *new)
  623. {
  624. struct list_head cursor, barrier;
  625. int failed = 0;
  626. struct path path1, path2;
  627. struct vfsmount *tagged;
  628. int err;
  629. err = kern_path(new, 0, &path2);
  630. if (err)
  631. return err;
  632. tagged = collect_mounts(&path2);
  633. path_put(&path2);
  634. if (!tagged)
  635. return -ENOMEM;
  636. err = kern_path(old, 0, &path1);
  637. if (err) {
  638. drop_collected_mounts(tagged);
  639. return err;
  640. }
  641. mutex_lock(&audit_filter_mutex);
  642. list_add(&barrier, &tree_list);
  643. list_add(&cursor, &barrier);
  644. while (cursor.next != &tree_list) {
  645. struct audit_tree *tree;
  646. int good_one = 0;
  647. tree = container_of(cursor.next, struct audit_tree, list);
  648. get_tree(tree);
  649. list_del(&cursor);
  650. list_add(&cursor, &tree->list);
  651. mutex_unlock(&audit_filter_mutex);
  652. err = kern_path(tree->pathname, 0, &path2);
  653. if (!err) {
  654. good_one = path_is_under(&path1, &path2);
  655. path_put(&path2);
  656. }
  657. if (!good_one) {
  658. put_tree(tree);
  659. mutex_lock(&audit_filter_mutex);
  660. continue;
  661. }
  662. failed = iterate_mounts(tag_mount, tree, tagged);
  663. if (failed) {
  664. put_tree(tree);
  665. mutex_lock(&audit_filter_mutex);
  666. break;
  667. }
  668. mutex_lock(&audit_filter_mutex);
  669. spin_lock(&hash_lock);
  670. if (!tree->goner) {
  671. list_del(&tree->list);
  672. list_add(&tree->list, &tree_list);
  673. }
  674. spin_unlock(&hash_lock);
  675. put_tree(tree);
  676. }
  677. while (barrier.prev != &tree_list) {
  678. struct audit_tree *tree;
  679. tree = container_of(barrier.prev, struct audit_tree, list);
  680. get_tree(tree);
  681. list_del(&tree->list);
  682. list_add(&tree->list, &barrier);
  683. mutex_unlock(&audit_filter_mutex);
  684. if (!failed) {
  685. struct node *node;
  686. spin_lock(&hash_lock);
  687. list_for_each_entry(node, &tree->chunks, list)
  688. node->index &= ~(1U<<31);
  689. spin_unlock(&hash_lock);
  690. } else {
  691. trim_marked(tree);
  692. }
  693. put_tree(tree);
  694. mutex_lock(&audit_filter_mutex);
  695. }
  696. list_del(&barrier);
  697. list_del(&cursor);
  698. mutex_unlock(&audit_filter_mutex);
  699. path_put(&path1);
  700. drop_collected_mounts(tagged);
  701. return failed;
  702. }
  703. /*
  704. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  705. * Runs from a separate thread.
  706. */
  707. static int prune_tree_thread(void *unused)
  708. {
  709. mutex_lock(&audit_cmd_mutex);
  710. mutex_lock(&audit_filter_mutex);
  711. while (!list_empty(&prune_list)) {
  712. struct audit_tree *victim;
  713. victim = list_entry(prune_list.next, struct audit_tree, list);
  714. list_del_init(&victim->list);
  715. mutex_unlock(&audit_filter_mutex);
  716. prune_one(victim);
  717. mutex_lock(&audit_filter_mutex);
  718. }
  719. mutex_unlock(&audit_filter_mutex);
  720. mutex_unlock(&audit_cmd_mutex);
  721. return 0;
  722. }
  723. static void audit_schedule_prune(void)
  724. {
  725. kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
  726. }
  727. /*
  728. * ... and that one is done if evict_chunk() decides to delay until the end
  729. * of syscall. Runs synchronously.
  730. */
  731. void audit_kill_trees(struct list_head *list)
  732. {
  733. mutex_lock(&audit_cmd_mutex);
  734. mutex_lock(&audit_filter_mutex);
  735. while (!list_empty(list)) {
  736. struct audit_tree *victim;
  737. victim = list_entry(list->next, struct audit_tree, list);
  738. kill_rules(victim);
  739. list_del_init(&victim->list);
  740. mutex_unlock(&audit_filter_mutex);
  741. prune_one(victim);
  742. mutex_lock(&audit_filter_mutex);
  743. }
  744. mutex_unlock(&audit_filter_mutex);
  745. mutex_unlock(&audit_cmd_mutex);
  746. }
  747. /*
  748. * Here comes the stuff asynchronous to auditctl operations
  749. */
  750. static void evict_chunk(struct audit_chunk *chunk)
  751. {
  752. struct audit_tree *owner;
  753. struct list_head *postponed = audit_killed_trees();
  754. int need_prune = 0;
  755. int n;
  756. if (chunk->dead)
  757. return;
  758. chunk->dead = 1;
  759. mutex_lock(&audit_filter_mutex);
  760. spin_lock(&hash_lock);
  761. while (!list_empty(&chunk->trees)) {
  762. owner = list_entry(chunk->trees.next,
  763. struct audit_tree, same_root);
  764. owner->goner = 1;
  765. owner->root = NULL;
  766. list_del_init(&owner->same_root);
  767. spin_unlock(&hash_lock);
  768. if (!postponed) {
  769. kill_rules(owner);
  770. list_move(&owner->list, &prune_list);
  771. need_prune = 1;
  772. } else {
  773. list_move(&owner->list, postponed);
  774. }
  775. spin_lock(&hash_lock);
  776. }
  777. list_del_rcu(&chunk->hash);
  778. for (n = 0; n < chunk->count; n++)
  779. list_del_init(&chunk->owners[n].list);
  780. spin_unlock(&hash_lock);
  781. if (need_prune)
  782. audit_schedule_prune();
  783. mutex_unlock(&audit_filter_mutex);
  784. }
  785. static int audit_tree_handle_event(struct fsnotify_group *group,
  786. struct fsnotify_mark *inode_mark,
  787. struct fsnotify_mark *vfsmonut_mark,
  788. struct fsnotify_event *event)
  789. {
  790. BUG();
  791. return -EOPNOTSUPP;
  792. }
  793. static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
  794. {
  795. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  796. evict_chunk(chunk);
  797. fsnotify_put_mark(entry);
  798. }
  799. static bool audit_tree_send_event(struct fsnotify_group *group, struct inode *inode,
  800. struct fsnotify_mark *inode_mark,
  801. struct fsnotify_mark *vfsmount_mark,
  802. __u32 mask, void *data, int data_type)
  803. {
  804. return false;
  805. }
  806. static const struct fsnotify_ops audit_tree_ops = {
  807. .handle_event = audit_tree_handle_event,
  808. .should_send_event = audit_tree_send_event,
  809. .free_group_priv = NULL,
  810. .free_event_priv = NULL,
  811. .freeing_mark = audit_tree_freeing_mark,
  812. };
  813. static int __init audit_tree_init(void)
  814. {
  815. int i;
  816. audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
  817. if (IS_ERR(audit_tree_group))
  818. audit_panic("cannot initialize fsnotify group for rectree watches");
  819. for (i = 0; i < HASH_SIZE; i++)
  820. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  821. return 0;
  822. }
  823. __initcall(audit_tree_init);