audit_watch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 watches; /* anchor for audit_watch->wlist */
  55. struct fsnotify_mark mark; /* fsnotify mark on the inode */
  56. };
  57. /* fsnotify handle. */
  58. struct fsnotify_group *audit_watch_group;
  59. /* fsnotify events we care about. */
  60. #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  61. FS_MOVE_SELF | FS_EVENT_ON_CHILD)
  62. static void audit_free_parent(struct audit_parent *parent)
  63. {
  64. WARN_ON(!list_empty(&parent->watches));
  65. kfree(parent);
  66. }
  67. static void audit_watch_free_mark(struct fsnotify_mark *entry)
  68. {
  69. struct audit_parent *parent;
  70. parent = container_of(entry, struct audit_parent, mark);
  71. audit_free_parent(parent);
  72. }
  73. static void audit_get_parent(struct audit_parent *parent)
  74. {
  75. if (likely(parent))
  76. fsnotify_get_mark(&parent->mark);
  77. }
  78. static void audit_put_parent(struct audit_parent *parent)
  79. {
  80. if (likely(parent))
  81. fsnotify_put_mark(&parent->mark);
  82. }
  83. /*
  84. * Find and return the audit_parent on the given inode. If found a reference
  85. * is taken on this parent.
  86. */
  87. static inline struct audit_parent *audit_find_parent(struct inode *inode)
  88. {
  89. struct audit_parent *parent = NULL;
  90. struct fsnotify_mark *entry;
  91. entry = fsnotify_find_inode_mark(audit_watch_group, inode);
  92. if (entry)
  93. parent = container_of(entry, struct audit_parent, mark);
  94. return parent;
  95. }
  96. void audit_get_watch(struct audit_watch *watch)
  97. {
  98. atomic_inc(&watch->count);
  99. }
  100. void audit_put_watch(struct audit_watch *watch)
  101. {
  102. if (atomic_dec_and_test(&watch->count)) {
  103. WARN_ON(watch->parent);
  104. WARN_ON(!list_empty(&watch->rules));
  105. kfree(watch->path);
  106. kfree(watch);
  107. }
  108. }
  109. void audit_remove_watch(struct audit_watch *watch)
  110. {
  111. list_del(&watch->wlist);
  112. audit_put_parent(watch->parent);
  113. watch->parent = NULL;
  114. audit_put_watch(watch); /* match initial get */
  115. }
  116. char *audit_watch_path(struct audit_watch *watch)
  117. {
  118. return watch->path;
  119. }
  120. int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
  121. {
  122. return (watch->ino != (unsigned long)-1) &&
  123. (watch->ino == ino) &&
  124. (watch->dev == dev);
  125. }
  126. /* Initialize a parent watch entry. */
  127. static struct audit_parent *audit_init_parent(struct nameidata *ndp)
  128. {
  129. struct inode *inode = ndp->path.dentry->d_inode;
  130. struct audit_parent *parent;
  131. int ret;
  132. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  133. if (unlikely(!parent))
  134. return ERR_PTR(-ENOMEM);
  135. INIT_LIST_HEAD(&parent->watches);
  136. fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
  137. parent->mark.mask = AUDIT_FS_WATCH;
  138. ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
  139. if (ret < 0) {
  140. audit_free_parent(parent);
  141. return ERR_PTR(ret);
  142. }
  143. fsnotify_recalc_group_mask(audit_watch_group);
  144. return parent;
  145. }
  146. /* Initialize a watch entry. */
  147. static struct audit_watch *audit_init_watch(char *path)
  148. {
  149. struct audit_watch *watch;
  150. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  151. if (unlikely(!watch))
  152. return ERR_PTR(-ENOMEM);
  153. INIT_LIST_HEAD(&watch->rules);
  154. atomic_set(&watch->count, 1);
  155. watch->path = path;
  156. watch->dev = (dev_t)-1;
  157. watch->ino = (unsigned long)-1;
  158. return watch;
  159. }
  160. /* Translate a watch string to kernel respresentation. */
  161. int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  162. {
  163. struct audit_watch *watch;
  164. if (!audit_watch_group)
  165. return -EOPNOTSUPP;
  166. if (path[0] != '/' || path[len-1] == '/' ||
  167. krule->listnr != AUDIT_FILTER_EXIT ||
  168. op != Audit_equal ||
  169. krule->inode_f || krule->watch || krule->tree)
  170. return -EINVAL;
  171. watch = audit_init_watch(path);
  172. if (IS_ERR(watch))
  173. return PTR_ERR(watch);
  174. audit_get_watch(watch);
  175. krule->watch = watch;
  176. return 0;
  177. }
  178. /* Duplicate the given audit watch. The new watch's rules list is initialized
  179. * to an empty list and wlist is undefined. */
  180. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  181. {
  182. char *path;
  183. struct audit_watch *new;
  184. path = kstrdup(old->path, GFP_KERNEL);
  185. if (unlikely(!path))
  186. return ERR_PTR(-ENOMEM);
  187. new = audit_init_watch(path);
  188. if (IS_ERR(new)) {
  189. kfree(path);
  190. goto out;
  191. }
  192. new->dev = old->dev;
  193. new->ino = old->ino;
  194. audit_get_parent(old->parent);
  195. new->parent = old->parent;
  196. out:
  197. return new;
  198. }
  199. static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  200. {
  201. if (audit_enabled) {
  202. struct audit_buffer *ab;
  203. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  204. audit_log_format(ab, "auid=%u ses=%u op=",
  205. audit_get_loginuid(current),
  206. audit_get_sessionid(current));
  207. audit_log_string(ab, op);
  208. audit_log_format(ab, " path=");
  209. audit_log_untrustedstring(ab, w->path);
  210. audit_log_key(ab, r->filterkey);
  211. audit_log_format(ab, " list=%d res=1", r->listnr);
  212. audit_log_end(ab);
  213. }
  214. }
  215. /* Update inode info in audit rules based on filesystem event. */
  216. static void audit_update_watch(struct audit_parent *parent,
  217. const char *dname, dev_t dev,
  218. unsigned long ino, unsigned invalidating)
  219. {
  220. struct audit_watch *owatch, *nwatch, *nextw;
  221. struct audit_krule *r, *nextr;
  222. struct audit_entry *oentry, *nentry;
  223. mutex_lock(&audit_filter_mutex);
  224. /* Run all of the watches on this parent looking for the one that
  225. * matches the given dname */
  226. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  227. if (audit_compare_dname_path(dname, owatch->path, NULL))
  228. continue;
  229. /* If the update involves invalidating rules, do the inode-based
  230. * filtering now, so we don't omit records. */
  231. if (invalidating && !audit_dummy_context())
  232. audit_filter_inodes(current, current->audit_context);
  233. /* updating ino will likely change which audit_hash_list we
  234. * are on so we need a new watch for the new list */
  235. nwatch = audit_dupe_watch(owatch);
  236. if (IS_ERR(nwatch)) {
  237. mutex_unlock(&audit_filter_mutex);
  238. audit_panic("error updating watch, skipping");
  239. return;
  240. }
  241. nwatch->dev = dev;
  242. nwatch->ino = ino;
  243. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  244. oentry = container_of(r, struct audit_entry, rule);
  245. list_del(&oentry->rule.rlist);
  246. list_del_rcu(&oentry->list);
  247. nentry = audit_dupe_rule(&oentry->rule);
  248. if (IS_ERR(nentry)) {
  249. list_del(&oentry->rule.list);
  250. audit_panic("error updating watch, removing");
  251. } else {
  252. int h = audit_hash_ino((u32)ino);
  253. /*
  254. * nentry->rule.watch == oentry->rule.watch so
  255. * we must drop that reference and set it to our
  256. * new watch.
  257. */
  258. audit_put_watch(nentry->rule.watch);
  259. audit_get_watch(nwatch);
  260. nentry->rule.watch = nwatch;
  261. list_add(&nentry->rule.rlist, &nwatch->rules);
  262. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  263. list_replace(&oentry->rule.list,
  264. &nentry->rule.list);
  265. }
  266. audit_watch_log_rule_change(r, owatch, "updated rules");
  267. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  268. }
  269. audit_remove_watch(owatch);
  270. goto add_watch_to_parent; /* event applies to a single watch */
  271. }
  272. mutex_unlock(&audit_filter_mutex);
  273. return;
  274. add_watch_to_parent:
  275. list_add(&nwatch->wlist, &parent->watches);
  276. mutex_unlock(&audit_filter_mutex);
  277. return;
  278. }
  279. /* Remove all watches & rules associated with a parent that is going away. */
  280. static void audit_remove_parent_watches(struct audit_parent *parent)
  281. {
  282. struct audit_watch *w, *nextw;
  283. struct audit_krule *r, *nextr;
  284. struct audit_entry *e;
  285. mutex_lock(&audit_filter_mutex);
  286. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  287. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  288. e = container_of(r, struct audit_entry, rule);
  289. audit_watch_log_rule_change(r, w, "remove rule");
  290. list_del(&r->rlist);
  291. list_del(&r->list);
  292. list_del_rcu(&e->list);
  293. call_rcu(&e->rcu, audit_free_rule_rcu);
  294. }
  295. audit_remove_watch(w);
  296. }
  297. mutex_unlock(&audit_filter_mutex);
  298. fsnotify_destroy_mark(&parent->mark);
  299. fsnotify_recalc_group_mask(audit_watch_group);
  300. }
  301. /* Get path information necessary for adding watches. */
  302. static int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw)
  303. {
  304. struct nameidata *ndparent, *ndwatch;
  305. int err;
  306. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  307. if (unlikely(!ndparent))
  308. return -ENOMEM;
  309. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  310. if (unlikely(!ndwatch)) {
  311. kfree(ndparent);
  312. return -ENOMEM;
  313. }
  314. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  315. if (err) {
  316. kfree(ndparent);
  317. kfree(ndwatch);
  318. return err;
  319. }
  320. err = path_lookup(path, 0, ndwatch);
  321. if (err) {
  322. kfree(ndwatch);
  323. ndwatch = NULL;
  324. }
  325. *ndp = ndparent;
  326. *ndw = ndwatch;
  327. return 0;
  328. }
  329. /* Release resources used for watch path information. */
  330. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  331. {
  332. if (ndp) {
  333. path_put(&ndp->path);
  334. kfree(ndp);
  335. }
  336. if (ndw) {
  337. path_put(&ndw->path);
  338. kfree(ndw);
  339. }
  340. }
  341. /* Associate the given rule with an existing parent.
  342. * Caller must hold audit_filter_mutex. */
  343. static void audit_add_to_parent(struct audit_krule *krule,
  344. struct audit_parent *parent)
  345. {
  346. struct audit_watch *w, *watch = krule->watch;
  347. int watch_found = 0;
  348. BUG_ON(!mutex_is_locked(&audit_filter_mutex));
  349. list_for_each_entry(w, &parent->watches, wlist) {
  350. if (strcmp(watch->path, w->path))
  351. continue;
  352. watch_found = 1;
  353. /* put krule's and initial refs to temporary watch */
  354. audit_put_watch(watch);
  355. audit_put_watch(watch);
  356. audit_get_watch(w);
  357. krule->watch = watch = w;
  358. break;
  359. }
  360. if (!watch_found) {
  361. audit_get_parent(parent);
  362. watch->parent = parent;
  363. list_add(&watch->wlist, &parent->watches);
  364. }
  365. list_add(&krule->rlist, &watch->rules);
  366. }
  367. /* Find a matching watch entry, or add this one.
  368. * Caller must hold audit_filter_mutex. */
  369. int audit_add_watch(struct audit_krule *krule, struct list_head **list)
  370. {
  371. struct audit_watch *watch = krule->watch;
  372. struct audit_parent *parent;
  373. struct nameidata *ndp = NULL, *ndw = NULL;
  374. int h, ret = 0;
  375. mutex_unlock(&audit_filter_mutex);
  376. /* Avoid calling path_lookup under audit_filter_mutex. */
  377. ret = audit_get_nd(watch->path, &ndp, &ndw);
  378. if (ret) {
  379. /* caller expects mutex locked */
  380. mutex_lock(&audit_filter_mutex);
  381. goto error;
  382. }
  383. mutex_lock(&audit_filter_mutex);
  384. /* update watch filter fields */
  385. if (ndw) {
  386. watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
  387. watch->ino = ndw->path.dentry->d_inode->i_ino;
  388. }
  389. /* either find an old parent or attach a new one */
  390. parent = audit_find_parent(ndp->path.dentry->d_inode);
  391. if (!parent) {
  392. parent = audit_init_parent(ndp);
  393. if (IS_ERR(parent)) {
  394. ret = PTR_ERR(parent);
  395. goto error;
  396. }
  397. }
  398. audit_add_to_parent(krule, parent);
  399. /* match get in audit_find_parent or audit_init_parent */
  400. audit_put_parent(parent);
  401. h = audit_hash_ino((u32)watch->ino);
  402. *list = &audit_inode_hash[h];
  403. error:
  404. audit_put_nd(ndp, ndw); /* NULL args OK */
  405. return ret;
  406. }
  407. void audit_remove_watch_rule(struct audit_krule *krule)
  408. {
  409. struct audit_watch *watch = krule->watch;
  410. struct audit_parent *parent = watch->parent;
  411. list_del(&krule->rlist);
  412. if (list_empty(&watch->rules)) {
  413. audit_remove_watch(watch);
  414. if (list_empty(&parent->watches)) {
  415. audit_get_parent(parent);
  416. fsnotify_destroy_mark(&parent->mark);
  417. audit_put_parent(parent);
  418. }
  419. }
  420. fsnotify_recalc_group_mask(audit_watch_group);
  421. }
  422. static bool audit_watch_should_send_event(struct fsnotify_group *group, struct inode *inode,
  423. struct vfsmount *mnt, struct fsnotify_mark *mark,
  424. __u32 mask, void *data, int data_type)
  425. {
  426. struct fsnotify_mark *entry;
  427. bool send;
  428. entry = fsnotify_find_inode_mark(group, inode);
  429. if (!entry)
  430. return false;
  431. mask = (mask & ~FS_EVENT_ON_CHILD);
  432. send = (entry->mask & mask);
  433. /* find took a reference */
  434. fsnotify_put_mark(entry);
  435. return send;
  436. }
  437. /* Update watch data in audit rules based on fsnotify events. */
  438. static int audit_watch_handle_event(struct fsnotify_group *group,
  439. struct fsnotify_mark *mark,
  440. struct fsnotify_event *event)
  441. {
  442. struct inode *inode;
  443. __u32 mask = event->mask;
  444. const char *dname = event->file_name;
  445. struct audit_parent *parent;
  446. BUG_ON(group != audit_watch_group);
  447. parent = audit_find_parent(event->to_tell);
  448. if (unlikely(!parent))
  449. return 0;
  450. switch (event->data_type) {
  451. case (FSNOTIFY_EVENT_FILE):
  452. inode = event->file->f_path.dentry->d_inode;
  453. break;
  454. case (FSNOTIFY_EVENT_INODE):
  455. inode = event->inode;
  456. break;
  457. default:
  458. BUG();
  459. inode = NULL;
  460. break;
  461. };
  462. if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
  463. audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
  464. else if (mask & (FS_DELETE|FS_MOVED_FROM))
  465. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  466. else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  467. audit_remove_parent_watches(parent);
  468. /* moved put_inotify_watch to freeing mark */
  469. /* matched the ref taken by audit_find_parent */
  470. audit_put_parent(parent);
  471. return 0;
  472. }
  473. static const struct fsnotify_ops audit_watch_fsnotify_ops = {
  474. .should_send_event = audit_watch_should_send_event,
  475. .handle_event = audit_watch_handle_event,
  476. .free_group_priv = NULL,
  477. .freeing_mark = NULL,
  478. .free_event_priv = NULL,
  479. };
  480. static int __init audit_watch_init(void)
  481. {
  482. audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
  483. if (IS_ERR(audit_watch_group)) {
  484. audit_watch_group = NULL;
  485. audit_panic("cannot create audit fsnotify group");
  486. }
  487. return 0;
  488. }
  489. device_initcall(audit_watch_init);