audit_watch.c 14 KB

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