audit_watch.c 15 KB

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