audit_watch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/file.h>
  22. #include <linux/kernel.h>
  23. #include <linux/audit.h>
  24. #include <linux/kthread.h>
  25. #include <linux/mutex.h>
  26. #include <linux/fs.h>
  27. #include <linux/fsnotify_backend.h>
  28. #include <linux/namei.h>
  29. #include <linux/netlink.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/security.h>
  33. #include "audit.h"
  34. /*
  35. * Reference counting:
  36. *
  37. * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
  38. * event. Each audit_watch holds a reference to its associated parent.
  39. *
  40. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  41. * audit_remove_watch(). Additionally, an audit_watch may exist
  42. * temporarily to assist in searching existing filter data. Each
  43. * audit_krule holds a reference to its associated watch.
  44. */
  45. struct audit_watch {
  46. atomic_t count; /* reference count */
  47. dev_t dev; /* associated superblock device */
  48. char *path; /* insertion path */
  49. unsigned long ino; /* associated inode number */
  50. struct audit_parent *parent; /* associated parent */
  51. struct list_head wlist; /* entry in parent->watches list */
  52. struct list_head rules; /* anchor for krule->rlist */
  53. };
  54. struct audit_parent {
  55. struct list_head watches; /* anchor for audit_watch->wlist */
  56. struct fsnotify_mark mark; /* fsnotify mark on the inode */
  57. };
  58. /* fsnotify handle. */
  59. static struct fsnotify_group *audit_watch_group;
  60. /* fsnotify events we care about. */
  61. #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  62. FS_MOVE_SELF | FS_EVENT_ON_CHILD)
  63. static void audit_free_parent(struct audit_parent *parent)
  64. {
  65. WARN_ON(!list_empty(&parent->watches));
  66. kfree(parent);
  67. }
  68. static void audit_watch_free_mark(struct fsnotify_mark *entry)
  69. {
  70. struct audit_parent *parent;
  71. parent = container_of(entry, struct audit_parent, mark);
  72. audit_free_parent(parent);
  73. }
  74. static void audit_get_parent(struct audit_parent *parent)
  75. {
  76. if (likely(parent))
  77. fsnotify_get_mark(&parent->mark);
  78. }
  79. static void audit_put_parent(struct audit_parent *parent)
  80. {
  81. if (likely(parent))
  82. fsnotify_put_mark(&parent->mark);
  83. }
  84. /*
  85. * Find and return the audit_parent on the given inode. If found a reference
  86. * is taken on this parent.
  87. */
  88. static inline struct audit_parent *audit_find_parent(struct inode *inode)
  89. {
  90. struct audit_parent *parent = NULL;
  91. struct fsnotify_mark *entry;
  92. entry = fsnotify_find_inode_mark(audit_watch_group, inode);
  93. if (entry)
  94. parent = container_of(entry, struct audit_parent, mark);
  95. return parent;
  96. }
  97. void audit_get_watch(struct audit_watch *watch)
  98. {
  99. atomic_inc(&watch->count);
  100. }
  101. void audit_put_watch(struct audit_watch *watch)
  102. {
  103. if (atomic_dec_and_test(&watch->count)) {
  104. WARN_ON(watch->parent);
  105. WARN_ON(!list_empty(&watch->rules));
  106. kfree(watch->path);
  107. kfree(watch);
  108. }
  109. }
  110. static void audit_remove_watch(struct audit_watch *watch)
  111. {
  112. list_del(&watch->wlist);
  113. audit_put_parent(watch->parent);
  114. watch->parent = NULL;
  115. audit_put_watch(watch); /* match initial get */
  116. }
  117. char *audit_watch_path(struct audit_watch *watch)
  118. {
  119. return watch->path;
  120. }
  121. int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
  122. {
  123. return (watch->ino != AUDIT_INO_UNSET) &&
  124. (watch->ino == ino) &&
  125. (watch->dev == dev);
  126. }
  127. /* Initialize a parent watch entry. */
  128. static struct audit_parent *audit_init_parent(struct path *path)
  129. {
  130. struct inode *inode = d_backing_inode(path->dentry);
  131. struct audit_parent *parent;
  132. int ret;
  133. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  134. if (unlikely(!parent))
  135. return ERR_PTR(-ENOMEM);
  136. INIT_LIST_HEAD(&parent->watches);
  137. fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
  138. parent->mark.mask = AUDIT_FS_WATCH;
  139. ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
  140. if (ret < 0) {
  141. audit_free_parent(parent);
  142. return ERR_PTR(ret);
  143. }
  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 = AUDIT_DEV_UNSET;
  157. watch->ino = AUDIT_INO_UNSET;
  158. return watch;
  159. }
  160. /* Translate a watch string to kernel representation. */
  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. 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. if (unlikely(!ab))
  204. return;
  205. audit_log_format(ab, "auid=%u ses=%u op=%s",
  206. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  207. audit_get_sessionid(current), 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,
  228. AUDIT_NAME_FULL))
  229. continue;
  230. /* If the update involves invalidating rules, do the inode-based
  231. * filtering now, so we don't omit records. */
  232. if (invalidating && !audit_dummy_context())
  233. audit_filter_inodes(current, current->audit_context);
  234. /* updating ino will likely change which audit_hash_list we
  235. * are on so we need a new watch for the new list */
  236. nwatch = audit_dupe_watch(owatch);
  237. if (IS_ERR(nwatch)) {
  238. mutex_unlock(&audit_filter_mutex);
  239. audit_panic("error updating watch, skipping");
  240. return;
  241. }
  242. nwatch->dev = dev;
  243. nwatch->ino = ino;
  244. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  245. oentry = container_of(r, struct audit_entry, rule);
  246. list_del(&oentry->rule.rlist);
  247. list_del_rcu(&oentry->list);
  248. nentry = audit_dupe_rule(&oentry->rule);
  249. if (IS_ERR(nentry)) {
  250. list_del(&oentry->rule.list);
  251. audit_panic("error updating watch, removing");
  252. } else {
  253. int h = audit_hash_ino((u32)ino);
  254. /*
  255. * nentry->rule.watch == oentry->rule.watch so
  256. * we must drop that reference and set it to our
  257. * new watch.
  258. */
  259. audit_put_watch(nentry->rule.watch);
  260. audit_get_watch(nwatch);
  261. nentry->rule.watch = nwatch;
  262. list_add(&nentry->rule.rlist, &nwatch->rules);
  263. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  264. list_replace(&oentry->rule.list,
  265. &nentry->rule.list);
  266. }
  267. if (oentry->rule.exe)
  268. audit_remove_mark(oentry->rule.exe);
  269. audit_watch_log_rule_change(r, owatch, "updated_rules");
  270. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  271. }
  272. audit_remove_watch(owatch);
  273. goto add_watch_to_parent; /* event applies to a single watch */
  274. }
  275. mutex_unlock(&audit_filter_mutex);
  276. return;
  277. add_watch_to_parent:
  278. list_add(&nwatch->wlist, &parent->watches);
  279. mutex_unlock(&audit_filter_mutex);
  280. return;
  281. }
  282. /* Remove all watches & rules associated with a parent that is going away. */
  283. static void audit_remove_parent_watches(struct audit_parent *parent)
  284. {
  285. struct audit_watch *w, *nextw;
  286. struct audit_krule *r, *nextr;
  287. struct audit_entry *e;
  288. mutex_lock(&audit_filter_mutex);
  289. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  290. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  291. e = container_of(r, struct audit_entry, rule);
  292. audit_watch_log_rule_change(r, w, "remove_rule");
  293. if (e->rule.exe)
  294. audit_remove_mark(e->rule.exe);
  295. list_del(&r->rlist);
  296. list_del(&r->list);
  297. list_del_rcu(&e->list);
  298. call_rcu(&e->rcu, audit_free_rule_rcu);
  299. }
  300. audit_remove_watch(w);
  301. }
  302. mutex_unlock(&audit_filter_mutex);
  303. fsnotify_destroy_mark(&parent->mark, audit_watch_group);
  304. }
  305. /* Get path information necessary for adding watches. */
  306. static int audit_get_nd(struct audit_watch *watch, struct path *parent)
  307. {
  308. struct dentry *d = kern_path_locked(watch->path, parent);
  309. if (IS_ERR(d))
  310. return PTR_ERR(d);
  311. inode_unlock(d_backing_inode(parent->dentry));
  312. if (d_is_positive(d)) {
  313. /* update watch filter fields */
  314. watch->dev = d->d_sb->s_dev;
  315. watch->ino = d_backing_inode(d)->i_ino;
  316. }
  317. dput(d);
  318. return 0;
  319. }
  320. /* Associate the given rule with an existing parent.
  321. * Caller must hold audit_filter_mutex. */
  322. static void audit_add_to_parent(struct audit_krule *krule,
  323. struct audit_parent *parent)
  324. {
  325. struct audit_watch *w, *watch = krule->watch;
  326. int watch_found = 0;
  327. BUG_ON(!mutex_is_locked(&audit_filter_mutex));
  328. list_for_each_entry(w, &parent->watches, wlist) {
  329. if (strcmp(watch->path, w->path))
  330. continue;
  331. watch_found = 1;
  332. /* put krule's ref to temporary watch */
  333. audit_put_watch(watch);
  334. audit_get_watch(w);
  335. krule->watch = watch = w;
  336. audit_put_parent(parent);
  337. break;
  338. }
  339. if (!watch_found) {
  340. watch->parent = parent;
  341. audit_get_watch(watch);
  342. list_add(&watch->wlist, &parent->watches);
  343. }
  344. list_add(&krule->rlist, &watch->rules);
  345. }
  346. /* Find a matching watch entry, or add this one.
  347. * Caller must hold audit_filter_mutex. */
  348. int audit_add_watch(struct audit_krule *krule, struct list_head **list)
  349. {
  350. struct audit_watch *watch = krule->watch;
  351. struct audit_parent *parent;
  352. struct path parent_path;
  353. int h, ret = 0;
  354. mutex_unlock(&audit_filter_mutex);
  355. /* Avoid calling path_lookup under audit_filter_mutex. */
  356. ret = audit_get_nd(watch, &parent_path);
  357. /* caller expects mutex locked */
  358. mutex_lock(&audit_filter_mutex);
  359. if (ret)
  360. return ret;
  361. /* either find an old parent or attach a new one */
  362. parent = audit_find_parent(d_backing_inode(parent_path.dentry));
  363. if (!parent) {
  364. parent = audit_init_parent(&parent_path);
  365. if (IS_ERR(parent)) {
  366. ret = PTR_ERR(parent);
  367. goto error;
  368. }
  369. }
  370. audit_add_to_parent(krule, parent);
  371. h = audit_hash_ino((u32)watch->ino);
  372. *list = &audit_inode_hash[h];
  373. error:
  374. path_put(&parent_path);
  375. return ret;
  376. }
  377. void audit_remove_watch_rule(struct audit_krule *krule)
  378. {
  379. struct audit_watch *watch = krule->watch;
  380. struct audit_parent *parent = watch->parent;
  381. list_del(&krule->rlist);
  382. if (list_empty(&watch->rules)) {
  383. audit_remove_watch(watch);
  384. if (list_empty(&parent->watches)) {
  385. audit_get_parent(parent);
  386. fsnotify_destroy_mark(&parent->mark, audit_watch_group);
  387. audit_put_parent(parent);
  388. }
  389. }
  390. }
  391. /* Update watch data in audit rules based on fsnotify events. */
  392. static int audit_watch_handle_event(struct fsnotify_group *group,
  393. struct inode *to_tell,
  394. struct fsnotify_mark *inode_mark,
  395. struct fsnotify_mark *vfsmount_mark,
  396. u32 mask, const void *data, int data_type,
  397. const unsigned char *dname, u32 cookie)
  398. {
  399. const struct inode *inode;
  400. struct audit_parent *parent;
  401. parent = container_of(inode_mark, struct audit_parent, mark);
  402. BUG_ON(group != audit_watch_group);
  403. switch (data_type) {
  404. case (FSNOTIFY_EVENT_PATH):
  405. inode = d_backing_inode(((const struct path *)data)->dentry);
  406. break;
  407. case (FSNOTIFY_EVENT_INODE):
  408. inode = (const struct inode *)data;
  409. break;
  410. default:
  411. BUG();
  412. inode = NULL;
  413. break;
  414. };
  415. if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
  416. audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
  417. else if (mask & (FS_DELETE|FS_MOVED_FROM))
  418. audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
  419. else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  420. audit_remove_parent_watches(parent);
  421. return 0;
  422. }
  423. static const struct fsnotify_ops audit_watch_fsnotify_ops = {
  424. .handle_event = audit_watch_handle_event,
  425. };
  426. static int __init audit_watch_init(void)
  427. {
  428. audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
  429. if (IS_ERR(audit_watch_group)) {
  430. audit_watch_group = NULL;
  431. audit_panic("cannot create audit fsnotify group");
  432. }
  433. return 0;
  434. }
  435. device_initcall(audit_watch_init);
  436. int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
  437. {
  438. struct audit_fsnotify_mark *audit_mark;
  439. char *pathname;
  440. pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
  441. if (!pathname)
  442. return -ENOMEM;
  443. audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
  444. if (IS_ERR(audit_mark)) {
  445. kfree(pathname);
  446. return PTR_ERR(audit_mark);
  447. }
  448. new->exe = audit_mark;
  449. return 0;
  450. }
  451. int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
  452. {
  453. struct file *exe_file;
  454. unsigned long ino;
  455. dev_t dev;
  456. exe_file = get_task_exe_file(tsk);
  457. if (!exe_file)
  458. return 0;
  459. ino = file_inode(exe_file)->i_ino;
  460. dev = file_inode(exe_file)->i_sb->s_dev;
  461. fput(exe_file);
  462. return audit_mark_compare(mark, ino, dev);
  463. }