audit_tree.c 23 KB

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