audit_tree.c 24 KB

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