audit_watch.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /* audit_watch.c -- watching inodes
  2. *
  3. * Copyright 2003-2009 Red Hat, Inc.
  4. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  5. * Copyright 2005 IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/audit.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/fs.h>
  26. #include <linux/fsnotify_backend.h>
  27. #include <linux/namei.h>
  28. #include <linux/netlink.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/security.h>
  32. #include "audit.h"
  33. /*
  34. * Reference counting:
  35. *
  36. * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
  37. * event. Each audit_watch holds a reference to its associated parent.
  38. *
  39. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  40. * audit_remove_watch(). Additionally, an audit_watch may exist
  41. * temporarily to assist in searching existing filter data. Each
  42. * audit_krule holds a reference to its associated watch.
  43. */
  44. struct audit_watch {
  45. atomic_t count; /* reference count */
  46. dev_t dev; /* associated superblock device */
  47. char *path; /* insertion path */
  48. unsigned long ino; /* associated inode number */
  49. struct audit_parent *parent; /* associated parent */
  50. struct list_head wlist; /* entry in parent->watches list */
  51. struct list_head rules; /* anchor for krule->rlist */
  52. };
  53. struct audit_parent {
  54. struct list_head ilist; /* tmp list used to free parents */
  55. struct list_head watches; /* anchor for audit_watch->wlist */
  56. struct fsnotify_mark_entry mark; /* fsnotify mark on the inode */
  57. unsigned flags; /* status flags */
  58. };
  59. /* fsnotify handle. */
  60. struct fsnotify_group *audit_watch_group;
  61. /*
  62. * audit_parent status flags:
  63. *
  64. * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
  65. * a filesystem event to ensure we're adding audit watches to a valid parent.
  66. * Technically not needed for FS_DELETE_SELF or FS_UNMOUNT events, as we cannot
  67. * receive them while we have nameidata, but must be used for FS_MOVE_SELF which
  68. * we can receive while holding nameidata.
  69. */
  70. #define AUDIT_PARENT_INVALID 0x001
  71. /* fsnotify events we care about. */
  72. #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  73. FS_MOVE_SELF | FS_EVENT_ON_CHILD)
  74. static void audit_free_parent(struct audit_parent *parent)
  75. {
  76. WARN_ON(!list_empty(&parent->watches));
  77. kfree(parent);
  78. }
  79. static void audit_watch_free_mark(struct fsnotify_mark_entry *entry)
  80. {
  81. struct audit_parent *parent;
  82. parent = container_of(entry, struct audit_parent, mark);
  83. audit_free_parent(parent);
  84. }
  85. static void audit_get_parent(struct audit_parent *parent)
  86. {
  87. if (likely(parent))
  88. fsnotify_get_mark(&parent->mark);
  89. }
  90. static void audit_put_parent(struct audit_parent *parent)
  91. {
  92. if (likely(parent))
  93. fsnotify_put_mark(&parent->mark);
  94. }
  95. /*
  96. * Find and return the audit_parent on the given inode. If found a reference
  97. * is taken on this parent.
  98. */
  99. static inline struct audit_parent *audit_find_parent(struct inode *inode)
  100. {
  101. struct audit_parent *parent = NULL;
  102. struct fsnotify_mark_entry *entry;
  103. spin_lock(&inode->i_lock);
  104. entry = fsnotify_find_mark_entry(audit_watch_group, inode);
  105. spin_unlock(&inode->i_lock);
  106. if (entry)
  107. parent = container_of(entry, struct audit_parent, mark);
  108. return parent;
  109. }
  110. void audit_get_watch(struct audit_watch *watch)
  111. {
  112. atomic_inc(&watch->count);
  113. }
  114. void audit_put_watch(struct audit_watch *watch)
  115. {
  116. if (atomic_dec_and_test(&watch->count)) {
  117. WARN_ON(watch->parent);
  118. WARN_ON(!list_empty(&watch->rules));
  119. kfree(watch->path);
  120. kfree(watch);
  121. }
  122. }
  123. void audit_remove_watch(struct audit_watch *watch)
  124. {
  125. list_del(&watch->wlist);
  126. audit_put_parent(watch->parent);
  127. watch->parent = NULL;
  128. audit_put_watch(watch); /* match initial get */
  129. }
  130. char *audit_watch_path(struct audit_watch *watch)
  131. {
  132. return watch->path;
  133. }
  134. int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
  135. {
  136. return (watch->ino != (unsigned long)-1) &&
  137. (watch->ino == ino) &&
  138. (watch->dev == dev);
  139. }
  140. /* Initialize a parent watch entry. */
  141. static struct audit_parent *audit_init_parent(struct nameidata *ndp)
  142. {
  143. struct inode *inode = ndp->path.dentry->d_inode;
  144. struct audit_parent *parent;
  145. int ret;
  146. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  147. if (unlikely(!parent))
  148. return ERR_PTR(-ENOMEM);
  149. INIT_LIST_HEAD(&parent->watches);
  150. parent->flags = 0;
  151. fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
  152. parent->mark.mask = AUDIT_FS_WATCH;
  153. /* grab a ref so fsnotify mark hangs around until we take audit_filter_mutex */
  154. audit_get_parent(parent);
  155. ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode);
  156. if (ret < 0) {
  157. audit_free_parent(parent);
  158. return ERR_PTR(ret);
  159. }
  160. return parent;
  161. }
  162. /* Initialize a watch entry. */
  163. static struct audit_watch *audit_init_watch(char *path)
  164. {
  165. struct audit_watch *watch;
  166. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  167. if (unlikely(!watch))
  168. return ERR_PTR(-ENOMEM);
  169. INIT_LIST_HEAD(&watch->rules);
  170. atomic_set(&watch->count, 1);
  171. watch->path = path;
  172. watch->dev = (dev_t)-1;
  173. watch->ino = (unsigned long)-1;
  174. return watch;
  175. }
  176. /* Translate a watch string to kernel respresentation. */
  177. int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  178. {
  179. struct audit_watch *watch;
  180. if (!audit_watch_group)
  181. return -EOPNOTSUPP;
  182. if (path[0] != '/' || path[len-1] == '/' ||
  183. krule->listnr != AUDIT_FILTER_EXIT ||
  184. op != Audit_equal ||
  185. krule->inode_f || krule->watch || krule->tree)
  186. return -EINVAL;
  187. watch = audit_init_watch(path);
  188. if (IS_ERR(watch))
  189. return PTR_ERR(watch);
  190. audit_get_watch(watch);
  191. krule->watch = watch;
  192. return 0;
  193. }
  194. /* Duplicate the given audit watch. The new watch's rules list is initialized
  195. * to an empty list and wlist is undefined. */
  196. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  197. {
  198. char *path;
  199. struct audit_watch *new;
  200. path = kstrdup(old->path, GFP_KERNEL);
  201. if (unlikely(!path))
  202. return ERR_PTR(-ENOMEM);
  203. new = audit_init_watch(path);
  204. if (IS_ERR(new)) {
  205. kfree(path);
  206. goto out;
  207. }
  208. new->dev = old->dev;
  209. new->ino = old->ino;
  210. audit_get_parent(old->parent);
  211. new->parent = old->parent;
  212. out:
  213. return new;
  214. }
  215. static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  216. {
  217. if (audit_enabled) {
  218. struct audit_buffer *ab;
  219. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  220. audit_log_format(ab, "auid=%u ses=%u op=",
  221. audit_get_loginuid(current),
  222. audit_get_sessionid(current));
  223. audit_log_string(ab, op);
  224. audit_log_format(ab, " path=");
  225. audit_log_untrustedstring(ab, w->path);
  226. audit_log_key(ab, r->filterkey);
  227. audit_log_format(ab, " list=%d res=1", r->listnr);
  228. audit_log_end(ab);
  229. }
  230. }
  231. /* Update inode info in audit rules based on filesystem event. */
  232. static void audit_update_watch(struct audit_parent *parent,
  233. const char *dname, dev_t dev,
  234. unsigned long ino, unsigned invalidating)
  235. {
  236. struct audit_watch *owatch, *nwatch, *nextw;
  237. struct audit_krule *r, *nextr;
  238. struct audit_entry *oentry, *nentry;
  239. mutex_lock(&audit_filter_mutex);
  240. /* Run all of the watches on this parent looking for the one that
  241. * matches the given dname */
  242. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  243. if (audit_compare_dname_path(dname, owatch->path, NULL))
  244. continue;
  245. /* If the update involves invalidating rules, do the inode-based
  246. * filtering now, so we don't omit records. */
  247. if (invalidating && !audit_dummy_context())
  248. audit_filter_inodes(current, current->audit_context);
  249. /* updating ino will likely change which audit_hash_list we
  250. * are on so we need a new watch for the new list */
  251. nwatch = audit_dupe_watch(owatch);
  252. if (IS_ERR(nwatch)) {
  253. mutex_unlock(&audit_filter_mutex);
  254. audit_panic("error updating watch, skipping");
  255. return;
  256. }
  257. nwatch->dev = dev;
  258. nwatch->ino = ino;
  259. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  260. oentry = container_of(r, struct audit_entry, rule);
  261. list_del(&oentry->rule.rlist);
  262. list_del_rcu(&oentry->list);
  263. nentry = audit_dupe_rule(&oentry->rule);
  264. if (IS_ERR(nentry)) {
  265. list_del(&oentry->rule.list);
  266. audit_panic("error updating watch, removing");
  267. } else {
  268. int h = audit_hash_ino((u32)ino);
  269. /*
  270. * nentry->rule.watch == oentry->rule.watch so
  271. * we must drop that reference and set it to our
  272. * new watch.
  273. */
  274. audit_put_watch(nentry->rule.watch);
  275. audit_get_watch(nwatch);
  276. nentry->rule.watch = nwatch;
  277. list_add(&nentry->rule.rlist, &nwatch->rules);
  278. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  279. list_replace(&oentry->rule.list,
  280. &nentry->rule.list);
  281. }
  282. audit_watch_log_rule_change(r, owatch, "updated rules");
  283. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  284. }
  285. audit_remove_watch(owatch);
  286. goto add_watch_to_parent; /* event applies to a single watch */
  287. }
  288. mutex_unlock(&audit_filter_mutex);
  289. return;
  290. add_watch_to_parent:
  291. list_add(&nwatch->wlist, &parent->watches);
  292. mutex_unlock(&audit_filter_mutex);
  293. return;
  294. }
  295. /* Remove all watches & rules associated with a parent that is going away. */
  296. static void audit_remove_parent_watches(struct audit_parent *parent)
  297. {
  298. struct audit_watch *w, *nextw;
  299. struct audit_krule *r, *nextr;
  300. struct audit_entry *e;
  301. mutex_lock(&audit_filter_mutex);
  302. parent->flags |= AUDIT_PARENT_INVALID;
  303. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  304. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  305. e = container_of(r, struct audit_entry, rule);
  306. audit_watch_log_rule_change(r, w, "remove rule");
  307. list_del(&r->rlist);
  308. list_del(&r->list);
  309. list_del_rcu(&e->list);
  310. call_rcu(&e->rcu, audit_free_rule_rcu);
  311. }
  312. audit_remove_watch(w);
  313. }
  314. mutex_unlock(&audit_filter_mutex);
  315. fsnotify_destroy_mark_by_entry(&parent->mark);
  316. }
  317. /* Unregister inotify watches for parents on in_list.
  318. * Generates an FS_IGNORED event. */
  319. void audit_watch_inotify_unregister(struct list_head *in_list)
  320. {
  321. struct audit_parent *p, *n;
  322. list_for_each_entry_safe(p, n, in_list, ilist) {
  323. list_del(&p->ilist);
  324. fsnotify_destroy_mark_by_entry(&p->mark);
  325. /* matches the get in audit_remove_watch_rule() */
  326. audit_put_parent(p);
  327. }
  328. }
  329. /* Get path information necessary for adding watches. */
  330. static int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw)
  331. {
  332. struct nameidata *ndparent, *ndwatch;
  333. int err;
  334. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  335. if (unlikely(!ndparent))
  336. return -ENOMEM;
  337. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  338. if (unlikely(!ndwatch)) {
  339. kfree(ndparent);
  340. return -ENOMEM;
  341. }
  342. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  343. if (err) {
  344. kfree(ndparent);
  345. kfree(ndwatch);
  346. return err;
  347. }
  348. err = path_lookup(path, 0, ndwatch);
  349. if (err) {
  350. kfree(ndwatch);
  351. ndwatch = NULL;
  352. }
  353. *ndp = ndparent;
  354. *ndw = ndwatch;
  355. return 0;
  356. }
  357. /* Release resources used for watch path information. */
  358. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  359. {
  360. if (ndp) {
  361. path_put(&ndp->path);
  362. kfree(ndp);
  363. }
  364. if (ndw) {
  365. path_put(&ndw->path);
  366. kfree(ndw);
  367. }
  368. }
  369. /* Associate the given rule with an existing parent.
  370. * Caller must hold audit_filter_mutex. */
  371. static void audit_add_to_parent(struct audit_krule *krule,
  372. struct audit_parent *parent)
  373. {
  374. struct audit_watch *w, *watch = krule->watch;
  375. int watch_found = 0;
  376. BUG_ON(!mutex_is_locked(&audit_filter_mutex));
  377. list_for_each_entry(w, &parent->watches, wlist) {
  378. if (strcmp(watch->path, w->path))
  379. continue;
  380. watch_found = 1;
  381. /* put krule's and initial refs to temporary watch */
  382. audit_put_watch(watch);
  383. audit_put_watch(watch);
  384. audit_get_watch(w);
  385. krule->watch = watch = w;
  386. break;
  387. }
  388. if (!watch_found) {
  389. audit_get_parent(parent);
  390. watch->parent = parent;
  391. list_add(&watch->wlist, &parent->watches);
  392. }
  393. list_add(&krule->rlist, &watch->rules);
  394. }
  395. /* Find a matching watch entry, or add this one.
  396. * Caller must hold audit_filter_mutex. */
  397. int audit_add_watch(struct audit_krule *krule, struct list_head **list)
  398. {
  399. struct audit_watch *watch = krule->watch;
  400. struct audit_parent *parent;
  401. struct nameidata *ndp = NULL, *ndw = NULL;
  402. int h, ret = 0;
  403. mutex_unlock(&audit_filter_mutex);
  404. /* Avoid calling path_lookup under audit_filter_mutex. */
  405. ret = audit_get_nd(watch->path, &ndp, &ndw);
  406. if (ret) {
  407. /* caller expects mutex locked */
  408. mutex_lock(&audit_filter_mutex);
  409. goto error;
  410. }
  411. /* update watch filter fields */
  412. if (ndw) {
  413. watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
  414. watch->ino = ndw->path.dentry->d_inode->i_ino;
  415. }
  416. /* The audit_filter_mutex must not be held during inotify calls because
  417. * we hold it during inotify event callback processing. If an existing
  418. * inotify watch is found, inotify_find_watch() grabs a reference before
  419. * returning.
  420. */
  421. parent = audit_find_parent(ndp->path.dentry->d_inode);
  422. if (!parent) {
  423. parent = audit_init_parent(ndp);
  424. if (IS_ERR(parent)) {
  425. /* caller expects mutex locked */
  426. mutex_lock(&audit_filter_mutex);
  427. ret = PTR_ERR(parent);
  428. goto error;
  429. }
  430. }
  431. mutex_lock(&audit_filter_mutex);
  432. /* parent was moved before we took audit_filter_mutex */
  433. if (parent->flags & AUDIT_PARENT_INVALID)
  434. ret = -ENOENT;
  435. else
  436. audit_add_to_parent(krule, parent);
  437. /* match get in audit_find_parent or audit_init_parent */
  438. audit_put_parent(parent);
  439. h = audit_hash_ino((u32)watch->ino);
  440. *list = &audit_inode_hash[h];
  441. error:
  442. audit_put_nd(ndp, ndw); /* NULL args OK */
  443. return ret;
  444. }
  445. void audit_remove_watch_rule(struct audit_krule *krule, struct list_head *list)
  446. {
  447. struct audit_watch *watch = krule->watch;
  448. struct audit_parent *parent = watch->parent;
  449. list_del(&krule->rlist);
  450. if (list_empty(&watch->rules)) {
  451. audit_remove_watch(watch);
  452. if (list_empty(&parent->watches)) {
  453. /* Put parent on the un-registration list.
  454. * Grab a reference before releasing
  455. * audit_filter_mutex, to be released in
  456. * audit_watch_inotify_unregister().
  457. * If filesystem is going away, just leave
  458. * the sucker alone, eviction will take
  459. * care of it. */
  460. audit_get_parent(parent);
  461. list_add(&parent->ilist, list);
  462. }
  463. }
  464. }
  465. static bool audit_watch_should_send_event(struct fsnotify_group *group, struct inode *inode, __u32 mask)
  466. {
  467. struct fsnotify_mark_entry *entry;
  468. bool send;
  469. spin_lock(&inode->i_lock);
  470. entry = fsnotify_find_mark_entry(group, inode);
  471. spin_unlock(&inode->i_lock);
  472. if (!entry)
  473. return false;
  474. mask = (mask & ~FS_EVENT_ON_CHILD);
  475. send = (entry->mask & mask);
  476. /* find took a reference */
  477. fsnotify_put_mark(entry);
  478. return send;
  479. }
  480. /* Update watch data in audit rules based on fsnotify events. */
  481. static int audit_watch_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  482. {
  483. struct inode *inode;
  484. __u32 mask = event->mask;
  485. const char *dname = event->file_name;
  486. struct audit_parent *parent;
  487. BUG_ON(group != audit_watch_group);
  488. parent = audit_find_parent(event->to_tell);
  489. if (unlikely(!parent))
  490. return 0;
  491. switch (event->data_type) {
  492. case (FSNOTIFY_EVENT_PATH):
  493. inode = event->path.dentry->d_inode;
  494. break;
  495. case (FSNOTIFY_EVENT_INODE):
  496. inode = event->inode;
  497. break;
  498. default:
  499. BUG();
  500. inode = NULL;
  501. break;
  502. };
  503. if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
  504. audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
  505. else if (mask & (FS_DELETE|FS_MOVED_FROM))
  506. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  507. else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  508. audit_remove_parent_watches(parent);
  509. /* moved put_inotify_watch to freeing mark */
  510. /* matched the ref taken by audit_find_parent */
  511. audit_put_parent(parent);
  512. return 0;
  513. }
  514. static void audit_watch_freeing_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *group)
  515. {
  516. struct audit_parent *parent;
  517. parent = container_of(entry, struct audit_parent, mark);
  518. /* taken from audit_handle_ievent & FS_IGNORED please figure out what I match... */
  519. audit_put_parent(parent);
  520. }
  521. static const struct fsnotify_ops audit_watch_fsnotify_ops = {
  522. .should_send_event = audit_watch_should_send_event,
  523. .handle_event = audit_watch_handle_event,
  524. .free_group_priv = NULL,
  525. .freeing_mark = audit_watch_freeing_mark,
  526. .free_event_priv = NULL,
  527. };
  528. static int __init audit_watch_init(void)
  529. {
  530. audit_watch_group = fsnotify_obtain_group(AUDIT_WATCH_GROUP_NUM, AUDIT_FS_WATCH,
  531. &audit_watch_fsnotify_ops);
  532. if (IS_ERR(audit_watch_group)) {
  533. audit_watch_group = NULL;
  534. audit_panic("cannot create audit fsnotify group");
  535. }
  536. return 0;
  537. }
  538. subsys_initcall(audit_watch_init);