audit_tree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. #include "audit.h"
  2. #include <linux/inotify.h>
  3. #include <linux/namei.h>
  4. #include <linux/mount.h>
  5. struct audit_tree;
  6. struct audit_chunk;
  7. struct audit_tree {
  8. atomic_t count;
  9. int goner;
  10. struct audit_chunk *root;
  11. struct list_head chunks;
  12. struct list_head rules;
  13. struct list_head list;
  14. struct list_head same_root;
  15. struct rcu_head head;
  16. char pathname[];
  17. };
  18. struct audit_chunk {
  19. struct list_head hash;
  20. struct inotify_watch watch;
  21. struct list_head trees; /* with root here */
  22. int dead;
  23. int count;
  24. atomic_long_t refs;
  25. struct rcu_head head;
  26. struct node {
  27. struct list_head list;
  28. struct audit_tree *owner;
  29. unsigned index; /* index; upper bit indicates 'will prune' */
  30. } owners[];
  31. };
  32. static LIST_HEAD(tree_list);
  33. static LIST_HEAD(prune_list);
  34. /*
  35. * One struct chunk is attached to each inode of interest.
  36. * We replace struct chunk on tagging/untagging.
  37. * Rules have pointer to struct audit_tree.
  38. * Rules have struct list_head rlist forming a list of rules over
  39. * the same tree.
  40. * References to struct chunk are collected at audit_inode{,_child}()
  41. * time and used in AUDIT_TREE rule matching.
  42. * These references are dropped at the same time we are calling
  43. * audit_free_names(), etc.
  44. *
  45. * Cyclic lists galore:
  46. * tree.chunks anchors chunk.owners[].list hash_lock
  47. * tree.rules anchors rule.rlist audit_filter_mutex
  48. * chunk.trees anchors tree.same_root hash_lock
  49. * chunk.hash is a hash with middle bits of watch.inode as
  50. * a hash function. RCU, hash_lock
  51. *
  52. * tree is refcounted; one reference for "some rules on rules_list refer to
  53. * it", one for each chunk with pointer to it.
  54. *
  55. * chunk is refcounted by embedded inotify_watch + .refs (non-zero refcount
  56. * of watch contributes 1 to .refs).
  57. *
  58. * node.index allows to get from node.list to containing chunk.
  59. * MSB of that sucker is stolen to mark taggings that we might have to
  60. * revert - several operations have very unpleasant cleanup logics and
  61. * that makes a difference. Some.
  62. */
  63. static struct inotify_handle *rtree_ih;
  64. static struct audit_tree *alloc_tree(const char *s)
  65. {
  66. struct audit_tree *tree;
  67. tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
  68. if (tree) {
  69. atomic_set(&tree->count, 1);
  70. tree->goner = 0;
  71. INIT_LIST_HEAD(&tree->chunks);
  72. INIT_LIST_HEAD(&tree->rules);
  73. INIT_LIST_HEAD(&tree->list);
  74. INIT_LIST_HEAD(&tree->same_root);
  75. tree->root = NULL;
  76. strcpy(tree->pathname, s);
  77. }
  78. return tree;
  79. }
  80. static inline void get_tree(struct audit_tree *tree)
  81. {
  82. atomic_inc(&tree->count);
  83. }
  84. static void __put_tree(struct rcu_head *rcu)
  85. {
  86. struct audit_tree *tree = container_of(rcu, struct audit_tree, head);
  87. kfree(tree);
  88. }
  89. static inline void put_tree(struct audit_tree *tree)
  90. {
  91. if (atomic_dec_and_test(&tree->count))
  92. call_rcu(&tree->head, __put_tree);
  93. }
  94. /* to avoid bringing the entire thing in audit.h */
  95. const char *audit_tree_path(struct audit_tree *tree)
  96. {
  97. return tree->pathname;
  98. }
  99. static struct audit_chunk *alloc_chunk(int count)
  100. {
  101. struct audit_chunk *chunk;
  102. size_t size;
  103. int i;
  104. size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
  105. chunk = kzalloc(size, GFP_KERNEL);
  106. if (!chunk)
  107. return NULL;
  108. INIT_LIST_HEAD(&chunk->hash);
  109. INIT_LIST_HEAD(&chunk->trees);
  110. chunk->count = count;
  111. atomic_long_set(&chunk->refs, 1);
  112. for (i = 0; i < count; i++) {
  113. INIT_LIST_HEAD(&chunk->owners[i].list);
  114. chunk->owners[i].index = i;
  115. }
  116. inotify_init_watch(&chunk->watch);
  117. return chunk;
  118. }
  119. static void free_chunk(struct audit_chunk *chunk)
  120. {
  121. int i;
  122. for (i = 0; i < chunk->count; i++) {
  123. if (chunk->owners[i].owner)
  124. put_tree(chunk->owners[i].owner);
  125. }
  126. kfree(chunk);
  127. }
  128. void audit_put_chunk(struct audit_chunk *chunk)
  129. {
  130. if (atomic_long_dec_and_test(&chunk->refs))
  131. free_chunk(chunk);
  132. }
  133. static void __put_chunk(struct rcu_head *rcu)
  134. {
  135. struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
  136. audit_put_chunk(chunk);
  137. }
  138. enum {HASH_SIZE = 128};
  139. static struct list_head chunk_hash_heads[HASH_SIZE];
  140. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
  141. static inline struct list_head *chunk_hash(const struct inode *inode)
  142. {
  143. unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
  144. return chunk_hash_heads + n % HASH_SIZE;
  145. }
  146. /* hash_lock is held by caller */
  147. static void insert_hash(struct audit_chunk *chunk)
  148. {
  149. struct list_head *list = chunk_hash(chunk->watch.inode);
  150. list_add_rcu(&chunk->hash, list);
  151. }
  152. /* called under rcu_read_lock */
  153. struct audit_chunk *audit_tree_lookup(const struct inode *inode)
  154. {
  155. struct list_head *list = chunk_hash(inode);
  156. struct audit_chunk *p;
  157. list_for_each_entry_rcu(p, list, hash) {
  158. if (p->watch.inode == inode) {
  159. atomic_long_inc(&p->refs);
  160. return p;
  161. }
  162. }
  163. return NULL;
  164. }
  165. int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
  166. {
  167. int n;
  168. for (n = 0; n < chunk->count; n++)
  169. if (chunk->owners[n].owner == tree)
  170. return 1;
  171. return 0;
  172. }
  173. /* tagging and untagging inodes with trees */
  174. static struct audit_chunk *find_chunk(struct node *p)
  175. {
  176. int index = p->index & ~(1U<<31);
  177. p -= index;
  178. return container_of(p, struct audit_chunk, owners[0]);
  179. }
  180. static void untag_chunk(struct node *p)
  181. {
  182. struct audit_chunk *chunk = find_chunk(p);
  183. struct audit_chunk *new;
  184. struct audit_tree *owner;
  185. int size = chunk->count - 1;
  186. int i, j;
  187. if (!pin_inotify_watch(&chunk->watch)) {
  188. /*
  189. * Filesystem is shutting down; all watches are getting
  190. * evicted, just take it off the node list for this
  191. * tree and let the eviction logics take care of the
  192. * rest.
  193. */
  194. owner = p->owner;
  195. if (owner->root == chunk) {
  196. list_del_init(&owner->same_root);
  197. owner->root = NULL;
  198. }
  199. list_del_init(&p->list);
  200. p->owner = NULL;
  201. put_tree(owner);
  202. return;
  203. }
  204. spin_unlock(&hash_lock);
  205. /*
  206. * pin_inotify_watch() succeeded, so the watch won't go away
  207. * from under us.
  208. */
  209. mutex_lock(&chunk->watch.inode->inotify_mutex);
  210. if (chunk->dead) {
  211. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  212. goto out;
  213. }
  214. owner = p->owner;
  215. if (!size) {
  216. chunk->dead = 1;
  217. spin_lock(&hash_lock);
  218. list_del_init(&chunk->trees);
  219. if (owner->root == chunk)
  220. owner->root = NULL;
  221. list_del_init(&p->list);
  222. list_del_rcu(&chunk->hash);
  223. spin_unlock(&hash_lock);
  224. inotify_evict_watch(&chunk->watch);
  225. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  226. put_inotify_watch(&chunk->watch);
  227. goto out;
  228. }
  229. new = alloc_chunk(size);
  230. if (!new)
  231. goto Fallback;
  232. if (inotify_clone_watch(&chunk->watch, &new->watch) < 0) {
  233. free_chunk(new);
  234. goto Fallback;
  235. }
  236. chunk->dead = 1;
  237. spin_lock(&hash_lock);
  238. list_replace_init(&chunk->trees, &new->trees);
  239. if (owner->root == chunk) {
  240. list_del_init(&owner->same_root);
  241. owner->root = NULL;
  242. }
  243. for (i = j = 0; i < size; i++, j++) {
  244. struct audit_tree *s;
  245. if (&chunk->owners[j] == p) {
  246. list_del_init(&p->list);
  247. i--;
  248. continue;
  249. }
  250. s = chunk->owners[j].owner;
  251. new->owners[i].owner = s;
  252. new->owners[i].index = chunk->owners[j].index - j + i;
  253. if (!s) /* result of earlier fallback */
  254. continue;
  255. get_tree(s);
  256. list_replace_init(&chunk->owners[i].list, &new->owners[j].list);
  257. }
  258. list_replace_rcu(&chunk->hash, &new->hash);
  259. list_for_each_entry(owner, &new->trees, same_root)
  260. owner->root = new;
  261. spin_unlock(&hash_lock);
  262. inotify_evict_watch(&chunk->watch);
  263. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  264. put_inotify_watch(&chunk->watch);
  265. goto out;
  266. Fallback:
  267. // do the best we can
  268. spin_lock(&hash_lock);
  269. if (owner->root == chunk) {
  270. list_del_init(&owner->same_root);
  271. owner->root = NULL;
  272. }
  273. list_del_init(&p->list);
  274. p->owner = NULL;
  275. put_tree(owner);
  276. spin_unlock(&hash_lock);
  277. mutex_unlock(&chunk->watch.inode->inotify_mutex);
  278. out:
  279. unpin_inotify_watch(&chunk->watch);
  280. spin_lock(&hash_lock);
  281. }
  282. static int create_chunk(struct inode *inode, struct audit_tree *tree)
  283. {
  284. struct audit_chunk *chunk = alloc_chunk(1);
  285. if (!chunk)
  286. return -ENOMEM;
  287. if (inotify_add_watch(rtree_ih, &chunk->watch, inode, IN_IGNORED | IN_DELETE_SELF) < 0) {
  288. free_chunk(chunk);
  289. return -ENOSPC;
  290. }
  291. mutex_lock(&inode->inotify_mutex);
  292. spin_lock(&hash_lock);
  293. if (tree->goner) {
  294. spin_unlock(&hash_lock);
  295. chunk->dead = 1;
  296. inotify_evict_watch(&chunk->watch);
  297. mutex_unlock(&inode->inotify_mutex);
  298. put_inotify_watch(&chunk->watch);
  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. mutex_unlock(&inode->inotify_mutex);
  312. return 0;
  313. }
  314. /* the first tagged inode becomes root of tree */
  315. static int tag_chunk(struct inode *inode, struct audit_tree *tree)
  316. {
  317. struct inotify_watch *watch;
  318. struct audit_tree *owner;
  319. struct audit_chunk *chunk, *old;
  320. struct node *p;
  321. int n;
  322. if (inotify_find_watch(rtree_ih, inode, &watch) < 0)
  323. return create_chunk(inode, tree);
  324. old = container_of(watch, struct audit_chunk, watch);
  325. /* are we already there? */
  326. spin_lock(&hash_lock);
  327. for (n = 0; n < old->count; n++) {
  328. if (old->owners[n].owner == tree) {
  329. spin_unlock(&hash_lock);
  330. put_inotify_watch(watch);
  331. return 0;
  332. }
  333. }
  334. spin_unlock(&hash_lock);
  335. chunk = alloc_chunk(old->count + 1);
  336. if (!chunk)
  337. return -ENOMEM;
  338. mutex_lock(&inode->inotify_mutex);
  339. if (inotify_clone_watch(&old->watch, &chunk->watch) < 0) {
  340. mutex_unlock(&inode->inotify_mutex);
  341. put_inotify_watch(&old->watch);
  342. free_chunk(chunk);
  343. return -ENOSPC;
  344. }
  345. spin_lock(&hash_lock);
  346. if (tree->goner) {
  347. spin_unlock(&hash_lock);
  348. chunk->dead = 1;
  349. inotify_evict_watch(&chunk->watch);
  350. mutex_unlock(&inode->inotify_mutex);
  351. put_inotify_watch(&old->watch);
  352. put_inotify_watch(&chunk->watch);
  353. return 0;
  354. }
  355. list_replace_init(&old->trees, &chunk->trees);
  356. for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
  357. struct audit_tree *s = old->owners[n].owner;
  358. p->owner = s;
  359. p->index = old->owners[n].index;
  360. if (!s) /* result of fallback in untag */
  361. continue;
  362. get_tree(s);
  363. list_replace_init(&old->owners[n].list, &p->list);
  364. }
  365. p->index = (chunk->count - 1) | (1U<<31);
  366. p->owner = tree;
  367. get_tree(tree);
  368. list_add(&p->list, &tree->chunks);
  369. list_replace_rcu(&old->hash, &chunk->hash);
  370. list_for_each_entry(owner, &chunk->trees, same_root)
  371. owner->root = chunk;
  372. old->dead = 1;
  373. if (!tree->root) {
  374. tree->root = chunk;
  375. list_add(&tree->same_root, &chunk->trees);
  376. }
  377. spin_unlock(&hash_lock);
  378. inotify_evict_watch(&old->watch);
  379. mutex_unlock(&inode->inotify_mutex);
  380. put_inotify_watch(&old->watch);
  381. return 0;
  382. }
  383. static void kill_rules(struct audit_tree *tree)
  384. {
  385. struct audit_krule *rule, *next;
  386. struct audit_entry *entry;
  387. struct audit_buffer *ab;
  388. list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
  389. entry = container_of(rule, struct audit_entry, rule);
  390. list_del_init(&rule->rlist);
  391. if (rule->tree) {
  392. /* not a half-baked one */
  393. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  394. audit_log_format(ab, "op=remove rule dir=");
  395. audit_log_untrustedstring(ab, rule->tree->pathname);
  396. if (rule->filterkey) {
  397. audit_log_format(ab, " key=");
  398. audit_log_untrustedstring(ab, rule->filterkey);
  399. } else
  400. audit_log_format(ab, " key=(null)");
  401. audit_log_format(ab, " list=%d res=1", rule->listnr);
  402. audit_log_end(ab);
  403. rule->tree = NULL;
  404. list_del_rcu(&entry->list);
  405. list_del(&entry->rule.list);
  406. call_rcu(&entry->rcu, audit_free_rule_rcu);
  407. }
  408. }
  409. }
  410. /*
  411. * finish killing struct audit_tree
  412. */
  413. static void prune_one(struct audit_tree *victim)
  414. {
  415. spin_lock(&hash_lock);
  416. while (!list_empty(&victim->chunks)) {
  417. struct node *p;
  418. p = list_entry(victim->chunks.next, struct node, list);
  419. untag_chunk(p);
  420. }
  421. spin_unlock(&hash_lock);
  422. put_tree(victim);
  423. }
  424. /* trim the uncommitted chunks from tree */
  425. static void trim_marked(struct audit_tree *tree)
  426. {
  427. struct list_head *p, *q;
  428. spin_lock(&hash_lock);
  429. if (tree->goner) {
  430. spin_unlock(&hash_lock);
  431. return;
  432. }
  433. /* reorder */
  434. for (p = tree->chunks.next; p != &tree->chunks; p = q) {
  435. struct node *node = list_entry(p, struct node, list);
  436. q = p->next;
  437. if (node->index & (1U<<31)) {
  438. list_del_init(p);
  439. list_add(p, &tree->chunks);
  440. }
  441. }
  442. while (!list_empty(&tree->chunks)) {
  443. struct node *node;
  444. node = list_entry(tree->chunks.next, struct node, list);
  445. /* have we run out of marked? */
  446. if (!(node->index & (1U<<31)))
  447. break;
  448. untag_chunk(node);
  449. }
  450. if (!tree->root && !tree->goner) {
  451. tree->goner = 1;
  452. spin_unlock(&hash_lock);
  453. mutex_lock(&audit_filter_mutex);
  454. kill_rules(tree);
  455. list_del_init(&tree->list);
  456. mutex_unlock(&audit_filter_mutex);
  457. prune_one(tree);
  458. } else {
  459. spin_unlock(&hash_lock);
  460. }
  461. }
  462. /* called with audit_filter_mutex */
  463. int audit_remove_tree_rule(struct audit_krule *rule)
  464. {
  465. struct audit_tree *tree;
  466. tree = rule->tree;
  467. if (tree) {
  468. spin_lock(&hash_lock);
  469. list_del_init(&rule->rlist);
  470. if (list_empty(&tree->rules) && !tree->goner) {
  471. tree->root = NULL;
  472. list_del_init(&tree->same_root);
  473. tree->goner = 1;
  474. list_move(&tree->list, &prune_list);
  475. rule->tree = NULL;
  476. spin_unlock(&hash_lock);
  477. audit_schedule_prune();
  478. return 1;
  479. }
  480. rule->tree = NULL;
  481. spin_unlock(&hash_lock);
  482. return 1;
  483. }
  484. return 0;
  485. }
  486. void audit_trim_trees(void)
  487. {
  488. struct list_head cursor;
  489. mutex_lock(&audit_filter_mutex);
  490. list_add(&cursor, &tree_list);
  491. while (cursor.next != &tree_list) {
  492. struct audit_tree *tree;
  493. struct path path;
  494. struct vfsmount *root_mnt;
  495. struct node *node;
  496. struct list_head list;
  497. int err;
  498. tree = container_of(cursor.next, struct audit_tree, list);
  499. get_tree(tree);
  500. list_del(&cursor);
  501. list_add(&cursor, &tree->list);
  502. mutex_unlock(&audit_filter_mutex);
  503. err = kern_path(tree->pathname, 0, &path);
  504. if (err)
  505. goto skip_it;
  506. root_mnt = collect_mounts(path.mnt, path.dentry);
  507. path_put(&path);
  508. if (!root_mnt)
  509. goto skip_it;
  510. list_add_tail(&list, &root_mnt->mnt_list);
  511. spin_lock(&hash_lock);
  512. list_for_each_entry(node, &tree->chunks, list) {
  513. struct audit_chunk *chunk = find_chunk(node);
  514. struct inode *inode = chunk->watch.inode;
  515. struct vfsmount *mnt;
  516. node->index |= 1U<<31;
  517. list_for_each_entry(mnt, &list, mnt_list) {
  518. if (mnt->mnt_root->d_inode == inode) {
  519. node->index &= ~(1U<<31);
  520. break;
  521. }
  522. }
  523. }
  524. spin_unlock(&hash_lock);
  525. trim_marked(tree);
  526. put_tree(tree);
  527. list_del_init(&list);
  528. drop_collected_mounts(root_mnt);
  529. skip_it:
  530. mutex_lock(&audit_filter_mutex);
  531. }
  532. list_del(&cursor);
  533. mutex_unlock(&audit_filter_mutex);
  534. }
  535. static int is_under(struct vfsmount *mnt, struct dentry *dentry,
  536. struct path *path)
  537. {
  538. if (mnt != path->mnt) {
  539. for (;;) {
  540. if (mnt->mnt_parent == mnt)
  541. return 0;
  542. if (mnt->mnt_parent == path->mnt)
  543. break;
  544. mnt = mnt->mnt_parent;
  545. }
  546. dentry = mnt->mnt_mountpoint;
  547. }
  548. return is_subdir(dentry, path->dentry);
  549. }
  550. int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
  551. {
  552. if (pathname[0] != '/' ||
  553. rule->listnr != AUDIT_FILTER_EXIT ||
  554. op != Audit_equal ||
  555. rule->inode_f || rule->watch || rule->tree)
  556. return -EINVAL;
  557. rule->tree = alloc_tree(pathname);
  558. if (!rule->tree)
  559. return -ENOMEM;
  560. return 0;
  561. }
  562. void audit_put_tree(struct audit_tree *tree)
  563. {
  564. put_tree(tree);
  565. }
  566. /* called with audit_filter_mutex */
  567. int audit_add_tree_rule(struct audit_krule *rule)
  568. {
  569. struct audit_tree *seed = rule->tree, *tree;
  570. struct path path;
  571. struct vfsmount *mnt, *p;
  572. struct list_head list;
  573. int err;
  574. list_for_each_entry(tree, &tree_list, list) {
  575. if (!strcmp(seed->pathname, tree->pathname)) {
  576. put_tree(seed);
  577. rule->tree = tree;
  578. list_add(&rule->rlist, &tree->rules);
  579. return 0;
  580. }
  581. }
  582. tree = seed;
  583. list_add(&tree->list, &tree_list);
  584. list_add(&rule->rlist, &tree->rules);
  585. /* do not set rule->tree yet */
  586. mutex_unlock(&audit_filter_mutex);
  587. err = kern_path(tree->pathname, 0, &path);
  588. if (err)
  589. goto Err;
  590. mnt = collect_mounts(path.mnt, path.dentry);
  591. path_put(&path);
  592. if (!mnt) {
  593. err = -ENOMEM;
  594. goto Err;
  595. }
  596. list_add_tail(&list, &mnt->mnt_list);
  597. get_tree(tree);
  598. list_for_each_entry(p, &list, mnt_list) {
  599. err = tag_chunk(p->mnt_root->d_inode, tree);
  600. if (err)
  601. break;
  602. }
  603. list_del(&list);
  604. drop_collected_mounts(mnt);
  605. if (!err) {
  606. struct node *node;
  607. spin_lock(&hash_lock);
  608. list_for_each_entry(node, &tree->chunks, list)
  609. node->index &= ~(1U<<31);
  610. spin_unlock(&hash_lock);
  611. } else {
  612. trim_marked(tree);
  613. goto Err;
  614. }
  615. mutex_lock(&audit_filter_mutex);
  616. if (list_empty(&rule->rlist)) {
  617. put_tree(tree);
  618. return -ENOENT;
  619. }
  620. rule->tree = tree;
  621. put_tree(tree);
  622. return 0;
  623. Err:
  624. mutex_lock(&audit_filter_mutex);
  625. list_del_init(&tree->list);
  626. list_del_init(&tree->rules);
  627. put_tree(tree);
  628. return err;
  629. }
  630. int audit_tag_tree(char *old, char *new)
  631. {
  632. struct list_head cursor, barrier;
  633. int failed = 0;
  634. struct path path;
  635. struct vfsmount *tagged;
  636. struct list_head list;
  637. struct vfsmount *mnt;
  638. struct dentry *dentry;
  639. int err;
  640. err = kern_path(new, 0, &path);
  641. if (err)
  642. return err;
  643. tagged = collect_mounts(path.mnt, path.dentry);
  644. path_put(&path);
  645. if (!tagged)
  646. return -ENOMEM;
  647. err = kern_path(old, 0, &path);
  648. if (err) {
  649. drop_collected_mounts(tagged);
  650. return err;
  651. }
  652. mnt = mntget(path.mnt);
  653. dentry = dget(path.dentry);
  654. path_put(&path);
  655. list_add_tail(&list, &tagged->mnt_list);
  656. mutex_lock(&audit_filter_mutex);
  657. list_add(&barrier, &tree_list);
  658. list_add(&cursor, &barrier);
  659. while (cursor.next != &tree_list) {
  660. struct audit_tree *tree;
  661. struct vfsmount *p;
  662. tree = container_of(cursor.next, struct audit_tree, list);
  663. get_tree(tree);
  664. list_del(&cursor);
  665. list_add(&cursor, &tree->list);
  666. mutex_unlock(&audit_filter_mutex);
  667. err = kern_path(tree->pathname, 0, &path);
  668. if (err) {
  669. put_tree(tree);
  670. mutex_lock(&audit_filter_mutex);
  671. continue;
  672. }
  673. spin_lock(&vfsmount_lock);
  674. if (!is_under(mnt, dentry, &path)) {
  675. spin_unlock(&vfsmount_lock);
  676. path_put(&path);
  677. put_tree(tree);
  678. mutex_lock(&audit_filter_mutex);
  679. continue;
  680. }
  681. spin_unlock(&vfsmount_lock);
  682. path_put(&path);
  683. list_for_each_entry(p, &list, mnt_list) {
  684. failed = tag_chunk(p->mnt_root->d_inode, tree);
  685. if (failed)
  686. break;
  687. }
  688. if (failed) {
  689. put_tree(tree);
  690. mutex_lock(&audit_filter_mutex);
  691. break;
  692. }
  693. mutex_lock(&audit_filter_mutex);
  694. spin_lock(&hash_lock);
  695. if (!tree->goner) {
  696. list_del(&tree->list);
  697. list_add(&tree->list, &tree_list);
  698. }
  699. spin_unlock(&hash_lock);
  700. put_tree(tree);
  701. }
  702. while (barrier.prev != &tree_list) {
  703. struct audit_tree *tree;
  704. tree = container_of(barrier.prev, struct audit_tree, list);
  705. get_tree(tree);
  706. list_del(&tree->list);
  707. list_add(&tree->list, &barrier);
  708. mutex_unlock(&audit_filter_mutex);
  709. if (!failed) {
  710. struct node *node;
  711. spin_lock(&hash_lock);
  712. list_for_each_entry(node, &tree->chunks, list)
  713. node->index &= ~(1U<<31);
  714. spin_unlock(&hash_lock);
  715. } else {
  716. trim_marked(tree);
  717. }
  718. put_tree(tree);
  719. mutex_lock(&audit_filter_mutex);
  720. }
  721. list_del(&barrier);
  722. list_del(&cursor);
  723. list_del(&list);
  724. mutex_unlock(&audit_filter_mutex);
  725. dput(dentry);
  726. mntput(mnt);
  727. drop_collected_mounts(tagged);
  728. return failed;
  729. }
  730. /*
  731. * That gets run when evict_chunk() ends up needing to kill audit_tree.
  732. * Runs from a separate thread, with audit_cmd_mutex held.
  733. */
  734. void audit_prune_trees(void)
  735. {
  736. mutex_lock(&audit_filter_mutex);
  737. while (!list_empty(&prune_list)) {
  738. struct audit_tree *victim;
  739. victim = list_entry(prune_list.next, struct audit_tree, list);
  740. list_del_init(&victim->list);
  741. mutex_unlock(&audit_filter_mutex);
  742. prune_one(victim);
  743. mutex_lock(&audit_filter_mutex);
  744. }
  745. mutex_unlock(&audit_filter_mutex);
  746. }
  747. /*
  748. * Here comes the stuff asynchronous to auditctl operations
  749. */
  750. /* inode->inotify_mutex is locked */
  751. static void evict_chunk(struct audit_chunk *chunk)
  752. {
  753. struct audit_tree *owner;
  754. int n;
  755. if (chunk->dead)
  756. return;
  757. chunk->dead = 1;
  758. mutex_lock(&audit_filter_mutex);
  759. spin_lock(&hash_lock);
  760. while (!list_empty(&chunk->trees)) {
  761. owner = list_entry(chunk->trees.next,
  762. struct audit_tree, same_root);
  763. owner->goner = 1;
  764. owner->root = NULL;
  765. list_del_init(&owner->same_root);
  766. spin_unlock(&hash_lock);
  767. kill_rules(owner);
  768. list_move(&owner->list, &prune_list);
  769. audit_schedule_prune();
  770. spin_lock(&hash_lock);
  771. }
  772. list_del_rcu(&chunk->hash);
  773. for (n = 0; n < chunk->count; n++)
  774. list_del_init(&chunk->owners[n].list);
  775. spin_unlock(&hash_lock);
  776. mutex_unlock(&audit_filter_mutex);
  777. }
  778. static void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
  779. u32 cookie, const char *dname, struct inode *inode)
  780. {
  781. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  782. if (mask & IN_IGNORED) {
  783. evict_chunk(chunk);
  784. put_inotify_watch(watch);
  785. }
  786. }
  787. static void destroy_watch(struct inotify_watch *watch)
  788. {
  789. struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
  790. call_rcu(&chunk->head, __put_chunk);
  791. }
  792. static const struct inotify_operations rtree_inotify_ops = {
  793. .handle_event = handle_event,
  794. .destroy_watch = destroy_watch,
  795. };
  796. static int __init audit_tree_init(void)
  797. {
  798. int i;
  799. rtree_ih = inotify_init(&rtree_inotify_ops);
  800. if (IS_ERR(rtree_ih))
  801. audit_panic("cannot initialize inotify handle for rectree watches");
  802. for (i = 0; i < HASH_SIZE; i++)
  803. INIT_LIST_HEAD(&chunk_hash_heads[i]);
  804. return 0;
  805. }
  806. __initcall(audit_tree_init);