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