audit_tree.c 24 KB

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