audit_tree.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "audit.h"
  3. #include <linux/fsnotify_backend.h>
  4. #include <linux/namei.h>
  5. #include <linux/mount.h>
  6. #include <linux/kthread.h>
  7. #include <linux/refcount.h>
  8. #include <linux/slab.h>
  9. struct audit_tree;
  10. struct audit_chunk;
  11. struct audit_tree {
  12. refcount_t count;
  13. int goner;
  14. struct audit_chunk *root;
  15. struct list_head chunks;
  16. struct list_head rules;
  17. struct list_head list;
  18. struct list_head same_root;
  19. struct rcu_head head;
  20. char pathname[];
  21. };
  22. struct audit_chunk {
  23. struct list_head hash;
  24. unsigned long key;
  25. struct fsnotify_mark mark;
  26. struct list_head trees; /* with root here */
  27. int dead;
  28. int count;
  29. atomic_long_t refs;
  30. struct rcu_head head;
  31. struct node {
  32. struct list_head list;
  33. struct audit_tree *owner;
  34. unsigned index; /* index; upper bit indicates 'will prune' */
  35. } owners[];
  36. };
  37. static LIST_HEAD(tree_list);
  38. static LIST_HEAD(prune_list);
  39. static struct task_struct *prune_thread;
  40. /*
  41. * One struct chunk is attached to each inode of interest.
  42. * We replace struct chunk on tagging/untagging.
  43. * Rules have pointer to struct audit_tree.
  44. * Rules have struct list_head rlist forming a list of rules over
  45. * the same tree.
  46. * References to struct chunk are collected at audit_inode{,_child}()
  47. * time and used in AUDIT_TREE rule matching.
  48. * These references are dropped at the same time we are calling
  49. * audit_free_names(), etc.
  50. *
  51. * Cyclic lists galore:
  52. * tree.chunks anchors chunk.owners[].list hash_lock
  53. * tree.rules anchors rule.rlist audit_filter_mutex
  54. * chunk.trees anchors tree.same_root hash_lock
  55. * chunk.hash is a hash with middle bits of watch.inode as
  56. * a hash function. RCU, hash_lock
  57. *
  58. * tree is refcounted; one reference for "some rules on rules_list refer to
  59. * it", one for each chunk with pointer to it.
  60. *
  61. * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
  62. * of watch contributes 1 to .refs).
  63. *
  64. * node.index allows to get from node.list to containing chunk.
  65. * MSB of that sucker is stolen to mark taggings that we might have to
  66. * revert - several operations have very unpleasant cleanup logics and
  67. * that makes a difference. Some.
  68. */
  69. static struct fsnotify_group *audit_tree_group;
  70. static struct audit_tree *alloc_tree(const char *s)
  71. {
  72. struct audit_tree *tree;
  73. tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
  74. if (tree) {
  75. refcount_set(&tree->count, 1);
  76. tree->goner = 0;
  77. INIT_LIST_HEAD(&tree->chunks);
  78. INIT_LIST_HEAD(&tree->rules);
  79. INIT_LIST_HEAD(&tree->list);
  80. INIT_LIST_HEAD(&tree->same_root);
  81. tree->root = NULL;
  82. strcpy(tree->pathname, s);
  83. }
  84. return tree;
  85. }
  86. static inline void get_tree(struct audit_tree *tree)
  87. {
  88. refcount_inc(&tree->count);
  89. }
  90. static inline void put_tree(struct audit_tree *tree)
  91. {
  92. if (refcount_dec_and_test(&tree->count))
  93. kfree_rcu(tree, head);
  94. }
  95. /* to avoid bringing the entire thing in audit.h */
  96. const char *audit_tree_path(struct audit_tree *tree)
  97. {
  98. return tree->pathname;
  99. }
  100. static void free_chunk(struct audit_chunk *chunk)
  101. {
  102. int i;
  103. for (i = 0; i < chunk->count; i++) {
  104. if (chunk->owners[i].owner)
  105. put_tree(chunk->owners[i].owner);
  106. }
  107. kfree(chunk);
  108. }
  109. void audit_put_chunk(struct audit_chunk *chunk)
  110. {
  111. if (atomic_long_dec_and_test(&chunk->refs))
  112. free_chunk(chunk);
  113. }
  114. static void __put_chunk(struct rcu_head *rcu)
  115. {
  116. struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
  117. audit_put_chunk(chunk);
  118. }
  119. static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
  120. {
  121. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  122. call_rcu(&chunk->head, __put_chunk);
  123. }
  124. static struct audit_chunk *alloc_chunk(int count)
  125. {
  126. struct audit_chunk *chunk;
  127. size_t size;
  128. int i;
  129. size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
  130. chunk = kzalloc(size, GFP_KERNEL);
  131. if (!chunk)
  132. return NULL;
  133. INIT_LIST_HEAD(&chunk->hash);
  134. INIT_LIST_HEAD(&chunk->trees);
  135. chunk->count = count;
  136. atomic_long_set(&chunk->refs, 1);
  137. for (i = 0; i < count; i++) {
  138. INIT_LIST_HEAD(&chunk->owners[i].list);
  139. chunk->owners[i].index = i;
  140. }
  141. fsnotify_init_mark(&chunk->mark, audit_tree_group);
  142. chunk->mark.mask = FS_IN_IGNORED;
  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. /* Function to return search key in our hash from inode. */
  149. static unsigned long inode_to_key(const struct inode *inode)
  150. {
  151. /* Use address pointed to by connector->obj as the key */
  152. return (unsigned long)&inode->i_fsnotify_marks;
  153. }
  154. static inline struct list_head *chunk_hash(unsigned long key)
  155. {
  156. unsigned long n = key / L1_CACHE_BYTES;
  157. return chunk_hash_heads + n % HASH_SIZE;
  158. }
  159. /* hash_lock & entry->lock is held by caller */
  160. static void insert_hash(struct audit_chunk *chunk)
  161. {
  162. struct list_head *list;
  163. if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED))
  164. return;
  165. WARN_ON_ONCE(!chunk->key);
  166. list = chunk_hash(chunk->key);
  167. list_add_rcu(&chunk->hash, list);
  168. }
  169. /* called under rcu_read_lock */
  170. struct audit_chunk *audit_tree_lookup(const struct inode *inode)
  171. {
  172. unsigned long key = inode_to_key(inode);
  173. struct list_head *list = chunk_hash(key);
  174. struct audit_chunk *p;
  175. list_for_each_entry_rcu(p, list, hash) {
  176. if (p->key == key) {
  177. atomic_long_inc(&p->refs);
  178. return p;
  179. }
  180. }
  181. return NULL;
  182. }
  183. bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
  184. {
  185. int n;
  186. for (n = 0; n < chunk->count; n++)
  187. if (chunk->owners[n].owner == tree)
  188. return true;
  189. return false;
  190. }
  191. /* tagging and untagging inodes with trees */
  192. static struct audit_chunk *find_chunk(struct node *p)
  193. {
  194. int index = p->index & ~(1U<<31);
  195. p -= index;
  196. return container_of(p, struct audit_chunk, owners[0]);
  197. }
  198. static void untag_chunk(struct node *p)
  199. {
  200. struct audit_chunk *chunk = find_chunk(p);
  201. struct fsnotify_mark *entry = &chunk->mark;
  202. struct audit_chunk *new = NULL;
  203. struct audit_tree *owner;
  204. int size = chunk->count - 1;
  205. int i, j;
  206. fsnotify_get_mark(entry);
  207. spin_unlock(&hash_lock);
  208. if (size)
  209. new = alloc_chunk(size);
  210. mutex_lock(&entry->group->mark_mutex);
  211. spin_lock(&entry->lock);
  212. /*
  213. * mark_mutex protects mark from getting detached and thus also from
  214. * mark->connector->obj getting NULL.
  215. */
  216. if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  217. spin_unlock(&entry->lock);
  218. mutex_unlock(&entry->group->mark_mutex);
  219. if (new)
  220. fsnotify_put_mark(&new->mark);
  221. goto out;
  222. }
  223. owner = p->owner;
  224. if (!size) {
  225. chunk->dead = 1;
  226. spin_lock(&hash_lock);
  227. list_del_init(&chunk->trees);
  228. if (owner->root == chunk)
  229. owner->root = NULL;
  230. list_del_init(&p->list);
  231. list_del_rcu(&chunk->hash);
  232. spin_unlock(&hash_lock);
  233. spin_unlock(&entry->lock);
  234. mutex_unlock(&entry->group->mark_mutex);
  235. fsnotify_destroy_mark(entry, audit_tree_group);
  236. goto out;
  237. }
  238. if (!new)
  239. goto Fallback;
  240. if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
  241. FSNOTIFY_OBJ_TYPE_INODE, 1)) {
  242. fsnotify_put_mark(&new->mark);
  243. goto Fallback;
  244. }
  245. chunk->dead = 1;
  246. spin_lock(&hash_lock);
  247. new->key = chunk->key;
  248. list_replace_init(&chunk->trees, &new->trees);
  249. if (owner->root == chunk) {
  250. list_del_init(&owner->same_root);
  251. owner->root = NULL;
  252. }
  253. for (i = j = 0; j <= size; i++, j++) {
  254. struct audit_tree *s;
  255. if (&chunk->owners[j] == p) {
  256. list_del_init(&p->list);
  257. i--;
  258. continue;
  259. }
  260. s = chunk->owners[j].owner;
  261. new->owners[i].owner = s;
  262. new->owners[i].index = chunk->owners[j].index - j + i;
  263. if (!s) /* result of earlier fallback */
  264. continue;
  265. get_tree(s);
  266. list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
  267. }
  268. list_replace_rcu(&chunk->hash, &new->hash);
  269. list_for_each_entry(owner, &new->trees, same_root)
  270. owner->root = new;
  271. spin_unlock(&hash_lock);
  272. spin_unlock(&entry->lock);
  273. mutex_unlock(&entry->group->mark_mutex);
  274. fsnotify_destroy_mark(entry, audit_tree_group);
  275. fsnotify_put_mark(&new->mark); /* drop initial reference */
  276. goto out;
  277. Fallback:
  278. // do the best we can
  279. spin_lock(&hash_lock);
  280. if (owner->root == chunk) {
  281. list_del_init(&owner->same_root);
  282. owner->root = NULL;
  283. }
  284. list_del_init(&p->list);
  285. p->owner = NULL;
  286. put_tree(owner);
  287. spin_unlock(&hash_lock);
  288. spin_unlock(&entry->lock);
  289. mutex_unlock(&entry->group->mark_mutex);
  290. out:
  291. fsnotify_put_mark(entry);
  292. spin_lock(&hash_lock);
  293. }
  294. static int create_chunk(struct inode *inode, struct audit_tree *tree)
  295. {
  296. struct fsnotify_mark *entry;
  297. struct audit_chunk *chunk = alloc_chunk(1);
  298. if (!chunk)
  299. return -ENOMEM;
  300. entry = &chunk->mark;
  301. if (fsnotify_add_inode_mark(entry, inode, 0)) {
  302. fsnotify_put_mark(entry);
  303. return -ENOSPC;
  304. }
  305. spin_lock(&entry->lock);
  306. spin_lock(&hash_lock);
  307. if (tree->goner) {
  308. spin_unlock(&hash_lock);
  309. chunk->dead = 1;
  310. spin_unlock(&entry->lock);
  311. fsnotify_destroy_mark(entry, audit_tree_group);
  312. fsnotify_put_mark(entry);
  313. return 0;
  314. }
  315. chunk->owners[0].index = (1U << 31);
  316. chunk->owners[0].owner = tree;
  317. get_tree(tree);
  318. list_add(&chunk->owners[0].list, &tree->chunks);
  319. if (!tree->root) {
  320. tree->root = chunk;
  321. list_add(&tree->same_root, &chunk->trees);
  322. }
  323. chunk->key = inode_to_key(inode);
  324. insert_hash(chunk);
  325. spin_unlock(&hash_lock);
  326. spin_unlock(&entry->lock);
  327. fsnotify_put_mark(entry); /* drop initial reference */
  328. return 0;
  329. }
  330. /* the first tagged inode becomes root of tree */
  331. static int tag_chunk(struct inode *inode, struct audit_tree *tree)
  332. {
  333. struct fsnotify_mark *old_entry, *chunk_entry;
  334. struct audit_tree *owner;
  335. struct audit_chunk *chunk, *old;
  336. struct node *p;
  337. int n;
  338. old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks,
  339. audit_tree_group);
  340. if (!old_entry)
  341. return create_chunk(inode, tree);
  342. old = container_of(old_entry, struct audit_chunk, mark);
  343. /* are we already there? */
  344. spin_lock(&hash_lock);
  345. for (n = 0; n < old->count; n++) {
  346. if (old->owners[n].owner == tree) {
  347. spin_unlock(&hash_lock);
  348. fsnotify_put_mark(old_entry);
  349. return 0;
  350. }
  351. }
  352. spin_unlock(&hash_lock);
  353. chunk = alloc_chunk(old->count + 1);
  354. if (!chunk) {
  355. fsnotify_put_mark(old_entry);
  356. return -ENOMEM;
  357. }
  358. chunk_entry = &chunk->mark;
  359. mutex_lock(&old_entry->group->mark_mutex);
  360. spin_lock(&old_entry->lock);
  361. /*
  362. * mark_mutex protects mark from getting detached and thus also from
  363. * mark->connector->obj getting NULL.
  364. */
  365. if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  366. /* old_entry is being shot, lets just lie */
  367. spin_unlock(&old_entry->lock);
  368. mutex_unlock(&old_entry->group->mark_mutex);
  369. fsnotify_put_mark(old_entry);
  370. fsnotify_put_mark(&chunk->mark);
  371. return -ENOENT;
  372. }
  373. if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
  374. FSNOTIFY_OBJ_TYPE_INODE, 1)) {
  375. spin_unlock(&old_entry->lock);
  376. mutex_unlock(&old_entry->group->mark_mutex);
  377. fsnotify_put_mark(chunk_entry);
  378. fsnotify_put_mark(old_entry);
  379. return -ENOSPC;
  380. }
  381. /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
  382. spin_lock(&chunk_entry->lock);
  383. spin_lock(&hash_lock);
  384. /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
  385. if (tree->goner) {
  386. spin_unlock(&hash_lock);
  387. chunk->dead = 1;
  388. spin_unlock(&chunk_entry->lock);
  389. spin_unlock(&old_entry->lock);
  390. mutex_unlock(&old_entry->group->mark_mutex);
  391. fsnotify_destroy_mark(chunk_entry, audit_tree_group);
  392. fsnotify_put_mark(chunk_entry);
  393. fsnotify_put_mark(old_entry);
  394. return 0;
  395. }
  396. chunk->key = old->key;
  397. list_replace_init(&old->trees, &chunk->trees);
  398. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  399. struct audit_tree *s = old->owners[n].owner;
  400. p->owner = s;
  401. p->index = old->owners[n].index;
  402. if (!s) /* result of fallback in untag */
  403. continue;
  404. get_tree(s);
  405. list_replace_init(&old->owners[n].list, &p->list);
  406. }
  407. p->index = (chunk->count - 1) | (1U<<31);
  408. p->owner = tree;
  409. get_tree(tree);
  410. list_add(&p->list, &tree->chunks);
  411. list_replace_rcu(&old->hash, &chunk->hash);
  412. list_for_each_entry(owner, &chunk->trees, same_root)
  413. owner->root = chunk;
  414. old->dead = 1;
  415. if (!tree->root) {
  416. tree->root = chunk;
  417. list_add(&tree->same_root, &chunk->trees);
  418. }
  419. spin_unlock(&hash_lock);
  420. spin_unlock(&chunk_entry->lock);
  421. spin_unlock(&old_entry->lock);
  422. mutex_unlock(&old_entry->group->mark_mutex);
  423. fsnotify_destroy_mark(old_entry, audit_tree_group);
  424. fsnotify_put_mark(chunk_entry); /* drop initial reference */
  425. fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
  426. return 0;
  427. }
  428. static void audit_tree_log_remove_rule(struct audit_krule *rule)
  429. {
  430. struct audit_buffer *ab;
  431. if (!audit_enabled)
  432. return;
  433. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  434. if (unlikely(!ab))
  435. return;
  436. audit_log_format(ab, "op=remove_rule");
  437. audit_log_format(ab, " dir=");
  438. audit_log_untrustedstring(ab, rule->tree->pathname);
  439. audit_log_key(ab, rule->filterkey);
  440. audit_log_format(ab, " list=%d res=1", rule->listnr);
  441. audit_log_end(ab);
  442. }
  443. static void kill_rules(struct audit_tree *tree)
  444. {
  445. struct audit_krule *rule, *next;
  446. struct audit_entry *entry;
  447. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  448. entry = container_of(rule, struct audit_entry, rule);
  449. list_del_init(&rule->rlist);
  450. if (rule->tree) {
  451. /* not a half-baked one */
  452. audit_tree_log_remove_rule(rule);
  453. if (entry->rule.exe)
  454. audit_remove_mark(entry->rule.exe);
  455. rule->tree = NULL;
  456. list_del_rcu(&entry->list);
  457. list_del(&entry->rule.list);
  458. call_rcu(&entry->rcu, audit_free_rule_rcu);
  459. }
  460. }
  461. }
  462. /*
  463. * finish killing struct audit_tree
  464. */
  465. static void prune_one(struct audit_tree *victim)
  466. {
  467. spin_lock(&hash_lock);
  468. while (!list_empty(&victim->chunks)) {
  469. struct node *p;
  470. p = list_entry(victim->chunks.next, struct node, list);
  471. untag_chunk(p);
  472. }
  473. spin_unlock(&hash_lock);
  474. put_tree(victim);
  475. }
  476. /* trim the uncommitted chunks from tree */
  477. static void trim_marked(struct audit_tree *tree)
  478. {
  479. struct list_head *p, *q;
  480. spin_lock(&hash_lock);
  481. if (tree->goner) {
  482. spin_unlock(&hash_lock);
  483. return;
  484. }
  485. /* reorder */
  486. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  487. struct node *node = list_entry(p, struct node, list);
  488. q = p->next;
  489. if (node->index & (1U<<31)) {
  490. list_del_init(p);
  491. list_add(p, &tree->chunks);
  492. }
  493. }
  494. while (!list_empty(&tree->chunks)) {
  495. struct node *node;
  496. node = list_entry(tree->chunks.next, struct node, list);
  497. /* have we run out of marked? */
  498. if (!(node->index & (1U<<31)))
  499. break;
  500. untag_chunk(node);
  501. }
  502. if (!tree->root && !tree->goner) {
  503. tree->goner = 1;
  504. spin_unlock(&hash_lock);
  505. mutex_lock(&audit_filter_mutex);
  506. kill_rules(tree);
  507. list_del_init(&tree->list);
  508. mutex_unlock(&audit_filter_mutex);
  509. prune_one(tree);
  510. } else {
  511. spin_unlock(&hash_lock);
  512. }
  513. }
  514. static void audit_schedule_prune(void);
  515. /* called with audit_filter_mutex */
  516. int audit_remove_tree_rule(struct audit_krule *rule)
  517. {
  518. struct audit_tree *tree;
  519. tree = rule->tree;
  520. if (tree) {
  521. spin_lock(&hash_lock);
  522. list_del_init(&rule->rlist);
  523. if (list_empty(&tree->rules) && !tree->goner) {
  524. tree->root = NULL;
  525. list_del_init(&tree->same_root);
  526. tree->goner = 1;
  527. list_move(&tree->list, &prune_list);
  528. rule->tree = NULL;
  529. spin_unlock(&hash_lock);
  530. audit_schedule_prune();
  531. return 1;
  532. }
  533. rule->tree = NULL;
  534. spin_unlock(&hash_lock);
  535. return 1;
  536. }
  537. return 0;
  538. }
  539. static int compare_root(struct vfsmount *mnt, void *arg)
  540. {
  541. return inode_to_key(d_backing_inode(mnt->mnt_root)) ==
  542. (unsigned long)arg;
  543. }
  544. void audit_trim_trees(void)
  545. {
  546. struct list_head cursor;
  547. mutex_lock(&audit_filter_mutex);
  548. list_add(&cursor, &tree_list);
  549. while (cursor.next != &tree_list) {
  550. struct audit_tree *tree;
  551. struct path path;
  552. struct vfsmount *root_mnt;
  553. struct node *node;
  554. int err;
  555. tree = container_of(cursor.next, struct audit_tree, list);
  556. get_tree(tree);
  557. list_del(&cursor);
  558. list_add(&cursor, &tree->list);
  559. mutex_unlock(&audit_filter_mutex);
  560. err = kern_path(tree->pathname, 0, &path);
  561. if (err)
  562. goto skip_it;
  563. root_mnt = collect_mounts(&path);
  564. path_put(&path);
  565. if (IS_ERR(root_mnt))
  566. goto skip_it;
  567. spin_lock(&hash_lock);
  568. list_for_each_entry(node, &tree->chunks, list) {
  569. struct audit_chunk *chunk = find_chunk(node);
  570. /* this could be NULL if the watch is dying else where... */
  571. node->index |= 1U<<31;
  572. if (iterate_mounts(compare_root,
  573. (void *)(chunk->key),
  574. root_mnt))
  575. node->index &= ~(1U<<31);
  576. }
  577. spin_unlock(&hash_lock);
  578. trim_marked(tree);
  579. drop_collected_mounts(root_mnt);
  580. skip_it:
  581. put_tree(tree);
  582. mutex_lock(&audit_filter_mutex);
  583. }
  584. list_del(&cursor);
  585. mutex_unlock(&audit_filter_mutex);
  586. }
  587. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  588. {
  589. if (pathname[0] != '/' ||
  590. rule->listnr != AUDIT_FILTER_EXIT ||
  591. op != Audit_equal ||
  592. rule->inode_f || rule->watch || rule->tree)
  593. return -EINVAL;
  594. rule->tree = alloc_tree(pathname);
  595. if (!rule->tree)
  596. return -ENOMEM;
  597. return 0;
  598. }
  599. void audit_put_tree(struct audit_tree *tree)
  600. {
  601. put_tree(tree);
  602. }
  603. static int tag_mount(struct vfsmount *mnt, void *arg)
  604. {
  605. return tag_chunk(d_backing_inode(mnt->mnt_root), arg);
  606. }
  607. /*
  608. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  609. * Runs from a separate thread.
  610. */
  611. static int prune_tree_thread(void *unused)
  612. {
  613. for (;;) {
  614. if (list_empty(&prune_list)) {
  615. set_current_state(TASK_INTERRUPTIBLE);
  616. schedule();
  617. }
  618. audit_ctl_lock();
  619. mutex_lock(&audit_filter_mutex);
  620. while (!list_empty(&prune_list)) {
  621. struct audit_tree *victim;
  622. victim = list_entry(prune_list.next,
  623. struct audit_tree, list);
  624. list_del_init(&victim->list);
  625. mutex_unlock(&audit_filter_mutex);
  626. prune_one(victim);
  627. mutex_lock(&audit_filter_mutex);
  628. }
  629. mutex_unlock(&audit_filter_mutex);
  630. audit_ctl_unlock();
  631. }
  632. return 0;
  633. }
  634. static int audit_launch_prune(void)
  635. {
  636. if (prune_thread)
  637. return 0;
  638. prune_thread = kthread_run(prune_tree_thread, NULL,
  639. "audit_prune_tree");
  640. if (IS_ERR(prune_thread)) {
  641. pr_err("cannot start thread audit_prune_tree");
  642. prune_thread = NULL;
  643. return -ENOMEM;
  644. }
  645. return 0;
  646. }
  647. /* called with audit_filter_mutex */
  648. int audit_add_tree_rule(struct audit_krule *rule)
  649. {
  650. struct audit_tree *seed = rule->tree, *tree;
  651. struct path path;
  652. struct vfsmount *mnt;
  653. int err;
  654. rule->tree = NULL;
  655. list_for_each_entry(tree, &tree_list, list) {
  656. if (!strcmp(seed->pathname, tree->pathname)) {
  657. put_tree(seed);
  658. rule->tree = tree;
  659. list_add(&rule->rlist, &tree->rules);
  660. return 0;
  661. }
  662. }
  663. tree = seed;
  664. list_add(&tree->list, &tree_list);
  665. list_add(&rule->rlist, &tree->rules);
  666. /* do not set rule->tree yet */
  667. mutex_unlock(&audit_filter_mutex);
  668. if (unlikely(!prune_thread)) {
  669. err = audit_launch_prune();
  670. if (err)
  671. goto Err;
  672. }
  673. err = kern_path(tree->pathname, 0, &path);
  674. if (err)
  675. goto Err;
  676. mnt = collect_mounts(&path);
  677. path_put(&path);
  678. if (IS_ERR(mnt)) {
  679. err = PTR_ERR(mnt);
  680. goto Err;
  681. }
  682. get_tree(tree);
  683. err = iterate_mounts(tag_mount, tree, mnt);
  684. drop_collected_mounts(mnt);
  685. if (!err) {
  686. struct node *node;
  687. spin_lock(&hash_lock);
  688. list_for_each_entry(node, &tree->chunks, list)
  689. node->index &= ~(1U<<31);
  690. spin_unlock(&hash_lock);
  691. } else {
  692. trim_marked(tree);
  693. goto Err;
  694. }
  695. mutex_lock(&audit_filter_mutex);
  696. if (list_empty(&rule->rlist)) {
  697. put_tree(tree);
  698. return -ENOENT;
  699. }
  700. rule->tree = tree;
  701. put_tree(tree);
  702. return 0;
  703. Err:
  704. mutex_lock(&audit_filter_mutex);
  705. list_del_init(&tree->list);
  706. list_del_init(&tree->rules);
  707. put_tree(tree);
  708. return err;
  709. }
  710. int audit_tag_tree(char *old, char *new)
  711. {
  712. struct list_head cursor, barrier;
  713. int failed = 0;
  714. struct path path1, path2;
  715. struct vfsmount *tagged;
  716. int err;
  717. err = kern_path(new, 0, &path2);
  718. if (err)
  719. return err;
  720. tagged = collect_mounts(&path2);
  721. path_put(&path2);
  722. if (IS_ERR(tagged))
  723. return PTR_ERR(tagged);
  724. err = kern_path(old, 0, &path1);
  725. if (err) {
  726. drop_collected_mounts(tagged);
  727. return err;
  728. }
  729. mutex_lock(&audit_filter_mutex);
  730. list_add(&barrier, &tree_list);
  731. list_add(&cursor, &barrier);
  732. while (cursor.next != &tree_list) {
  733. struct audit_tree *tree;
  734. int good_one = 0;
  735. tree = container_of(cursor.next, struct audit_tree, list);
  736. get_tree(tree);
  737. list_del(&cursor);
  738. list_add(&cursor, &tree->list);
  739. mutex_unlock(&audit_filter_mutex);
  740. err = kern_path(tree->pathname, 0, &path2);
  741. if (!err) {
  742. good_one = path_is_under(&path1, &path2);
  743. path_put(&path2);
  744. }
  745. if (!good_one) {
  746. put_tree(tree);
  747. mutex_lock(&audit_filter_mutex);
  748. continue;
  749. }
  750. failed = iterate_mounts(tag_mount, tree, tagged);
  751. if (failed) {
  752. put_tree(tree);
  753. mutex_lock(&audit_filter_mutex);
  754. break;
  755. }
  756. mutex_lock(&audit_filter_mutex);
  757. spin_lock(&hash_lock);
  758. if (!tree->goner) {
  759. list_del(&tree->list);
  760. list_add(&tree->list, &tree_list);
  761. }
  762. spin_unlock(&hash_lock);
  763. put_tree(tree);
  764. }
  765. while (barrier.prev != &tree_list) {
  766. struct audit_tree *tree;
  767. tree = container_of(barrier.prev, struct audit_tree, list);
  768. get_tree(tree);
  769. list_del(&tree->list);
  770. list_add(&tree->list, &barrier);
  771. mutex_unlock(&audit_filter_mutex);
  772. if (!failed) {
  773. struct node *node;
  774. spin_lock(&hash_lock);
  775. list_for_each_entry(node, &tree->chunks, list)
  776. node->index &= ~(1U<<31);
  777. spin_unlock(&hash_lock);
  778. } else {
  779. trim_marked(tree);
  780. }
  781. put_tree(tree);
  782. mutex_lock(&audit_filter_mutex);
  783. }
  784. list_del(&barrier);
  785. list_del(&cursor);
  786. mutex_unlock(&audit_filter_mutex);
  787. path_put(&path1);
  788. drop_collected_mounts(tagged);
  789. return failed;
  790. }
  791. static void audit_schedule_prune(void)
  792. {
  793. wake_up_process(prune_thread);
  794. }
  795. /*
  796. * ... and that one is done if evict_chunk() decides to delay until the end
  797. * of syscall. Runs synchronously.
  798. */
  799. void audit_kill_trees(struct list_head *list)
  800. {
  801. audit_ctl_lock();
  802. mutex_lock(&audit_filter_mutex);
  803. while (!list_empty(list)) {
  804. struct audit_tree *victim;
  805. victim = list_entry(list->next, struct audit_tree, list);
  806. kill_rules(victim);
  807. list_del_init(&victim->list);
  808. mutex_unlock(&audit_filter_mutex);
  809. prune_one(victim);
  810. mutex_lock(&audit_filter_mutex);
  811. }
  812. mutex_unlock(&audit_filter_mutex);
  813. audit_ctl_unlock();
  814. }
  815. /*
  816. * Here comes the stuff asynchronous to auditctl operations
  817. */
  818. static void evict_chunk(struct audit_chunk *chunk)
  819. {
  820. struct audit_tree *owner;
  821. struct list_head *postponed = audit_killed_trees();
  822. int need_prune = 0;
  823. int n;
  824. if (chunk->dead)
  825. return;
  826. chunk->dead = 1;
  827. mutex_lock(&audit_filter_mutex);
  828. spin_lock(&hash_lock);
  829. while (!list_empty(&chunk->trees)) {
  830. owner = list_entry(chunk->trees.next,
  831. struct audit_tree, same_root);
  832. owner->goner = 1;
  833. owner->root = NULL;
  834. list_del_init(&owner->same_root);
  835. spin_unlock(&hash_lock);
  836. if (!postponed) {
  837. kill_rules(owner);
  838. list_move(&owner->list, &prune_list);
  839. need_prune = 1;
  840. } else {
  841. list_move(&owner->list, postponed);
  842. }
  843. spin_lock(&hash_lock);
  844. }
  845. list_del_rcu(&chunk->hash);
  846. for (n = 0; n < chunk->count; n++)
  847. list_del_init(&chunk->owners[n].list);
  848. spin_unlock(&hash_lock);
  849. mutex_unlock(&audit_filter_mutex);
  850. if (need_prune)
  851. audit_schedule_prune();
  852. }
  853. static int audit_tree_handle_event(struct fsnotify_group *group,
  854. struct inode *to_tell,
  855. u32 mask, const void *data, int data_type,
  856. const unsigned char *file_name, u32 cookie,
  857. struct fsnotify_iter_info *iter_info)
  858. {
  859. return 0;
  860. }
  861. static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
  862. {
  863. struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
  864. evict_chunk(chunk);
  865. /*
  866. * We are guaranteed to have at least one reference to the mark from
  867. * either the inode or the caller of fsnotify_destroy_mark().
  868. */
  869. BUG_ON(refcount_read(&entry->refcnt) < 1);
  870. }
  871. static const struct fsnotify_ops audit_tree_ops = {
  872. .handle_event = audit_tree_handle_event,
  873. .freeing_mark = audit_tree_freeing_mark,
  874. .free_mark = audit_tree_destroy_watch,
  875. };
  876. static int __init audit_tree_init(void)
  877. {
  878. int i;
  879. audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
  880. if (IS_ERR(audit_tree_group))
  881. audit_panic("cannot initialize fsnotify group for rectree watches");
  882. for (i = 0; i < HASH_SIZE; i++)
  883. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  884. return 0;
  885. }
  886. __initcall(audit_tree_init);