audit_tree.c 24 KB

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