audit_watch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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_entry 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 *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 *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. return parent;
  146. }
  147. /* Initialize a watch entry. */
  148. static struct audit_watch *audit_init_watch(char *path)
  149. {
  150. struct audit_watch *watch;
  151. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  152. if (unlikely(!watch))
  153. return ERR_PTR(-ENOMEM);
  154. INIT_LIST_HEAD(&watch->rules);
  155. atomic_set(&watch->count, 1);
  156. watch->path = path;
  157. watch->dev = (dev_t)-1;
  158. watch->ino = (unsigned long)-1;
  159. return watch;
  160. }
  161. /* Translate a watch string to kernel respresentation. */
  162. int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  163. {
  164. struct audit_watch *watch;
  165. if (!audit_watch_group)
  166. return -EOPNOTSUPP;
  167. if (path[0] != '/' || path[len-1] == '/' ||
  168. krule->listnr != AUDIT_FILTER_EXIT ||
  169. op != Audit_equal ||
  170. krule->inode_f || krule->watch || krule->tree)
  171. return -EINVAL;
  172. watch = audit_init_watch(path);
  173. if (IS_ERR(watch))
  174. return PTR_ERR(watch);
  175. audit_get_watch(watch);
  176. krule->watch = watch;
  177. return 0;
  178. }
  179. /* Duplicate the given audit watch. The new watch's rules list is initialized
  180. * to an empty list and wlist is undefined. */
  181. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  182. {
  183. char *path;
  184. struct audit_watch *new;
  185. path = kstrdup(old->path, GFP_KERNEL);
  186. if (unlikely(!path))
  187. return ERR_PTR(-ENOMEM);
  188. new = audit_init_watch(path);
  189. if (IS_ERR(new)) {
  190. kfree(path);
  191. goto out;
  192. }
  193. new->dev = old->dev;
  194. new->ino = old->ino;
  195. audit_get_parent(old->parent);
  196. new->parent = old->parent;
  197. out:
  198. return new;
  199. }
  200. static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  201. {
  202. if (audit_enabled) {
  203. struct audit_buffer *ab;
  204. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  205. audit_log_format(ab, "auid=%u ses=%u op=",
  206. audit_get_loginuid(current),
  207. audit_get_sessionid(current));
  208. audit_log_string(ab, op);
  209. audit_log_format(ab, " path=");
  210. audit_log_untrustedstring(ab, w->path);
  211. audit_log_key(ab, r->filterkey);
  212. audit_log_format(ab, " list=%d res=1", r->listnr);
  213. audit_log_end(ab);
  214. }
  215. }
  216. /* Update inode info in audit rules based on filesystem event. */
  217. static void audit_update_watch(struct audit_parent *parent,
  218. const char *dname, dev_t dev,
  219. unsigned long ino, unsigned invalidating)
  220. {
  221. struct audit_watch *owatch, *nwatch, *nextw;
  222. struct audit_krule *r, *nextr;
  223. struct audit_entry *oentry, *nentry;
  224. mutex_lock(&audit_filter_mutex);
  225. /* Run all of the watches on this parent looking for the one that
  226. * matches the given dname */
  227. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  228. if (audit_compare_dname_path(dname, owatch->path, NULL))
  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. audit_watch_log_rule_change(r, owatch, "updated rules");
  268. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  269. }
  270. audit_remove_watch(owatch);
  271. goto add_watch_to_parent; /* event applies to a single watch */
  272. }
  273. mutex_unlock(&audit_filter_mutex);
  274. return;
  275. add_watch_to_parent:
  276. list_add(&nwatch->wlist, &parent->watches);
  277. mutex_unlock(&audit_filter_mutex);
  278. return;
  279. }
  280. /* Remove all watches & rules associated with a parent that is going away. */
  281. static void audit_remove_parent_watches(struct audit_parent *parent)
  282. {
  283. struct audit_watch *w, *nextw;
  284. struct audit_krule *r, *nextr;
  285. struct audit_entry *e;
  286. mutex_lock(&audit_filter_mutex);
  287. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  288. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  289. e = container_of(r, struct audit_entry, rule);
  290. audit_watch_log_rule_change(r, w, "remove rule");
  291. list_del(&r->rlist);
  292. list_del(&r->list);
  293. list_del_rcu(&e->list);
  294. call_rcu(&e->rcu, audit_free_rule_rcu);
  295. }
  296. audit_remove_watch(w);
  297. }
  298. mutex_unlock(&audit_filter_mutex);
  299. fsnotify_destroy_mark_by_entry(&parent->mark);
  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_by_entry(&parent->mark);
  417. audit_put_parent(parent);
  418. }
  419. }
  420. }
  421. static bool audit_watch_should_send_event(struct fsnotify_group *group, struct inode *inode, __u32 mask)
  422. {
  423. struct fsnotify_mark_entry *entry;
  424. bool send;
  425. spin_lock(&inode->i_lock);
  426. entry = fsnotify_find_mark_entry(group, inode);
  427. spin_unlock(&inode->i_lock);
  428. if (!entry)
  429. return false;
  430. mask = (mask & ~FS_EVENT_ON_CHILD);
  431. send = (entry->mask & mask);
  432. /* find took a reference */
  433. fsnotify_put_mark(entry);
  434. return send;
  435. }
  436. /* Update watch data in audit rules based on fsnotify events. */
  437. static int audit_watch_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  438. {
  439. struct inode *inode;
  440. __u32 mask = event->mask;
  441. const char *dname = event->file_name;
  442. struct audit_parent *parent;
  443. BUG_ON(group != audit_watch_group);
  444. parent = audit_find_parent(event->to_tell);
  445. if (unlikely(!parent))
  446. return 0;
  447. switch (event->data_type) {
  448. case (FSNOTIFY_EVENT_PATH):
  449. inode = event->path.dentry->d_inode;
  450. break;
  451. case (FSNOTIFY_EVENT_INODE):
  452. inode = event->inode;
  453. break;
  454. default:
  455. BUG();
  456. inode = NULL;
  457. break;
  458. };
  459. if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
  460. audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
  461. else if (mask & (FS_DELETE|FS_MOVED_FROM))
  462. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  463. else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  464. audit_remove_parent_watches(parent);
  465. /* moved put_inotify_watch to freeing mark */
  466. /* matched the ref taken by audit_find_parent */
  467. audit_put_parent(parent);
  468. return 0;
  469. }
  470. static const struct fsnotify_ops audit_watch_fsnotify_ops = {
  471. .should_send_event = audit_watch_should_send_event,
  472. .handle_event = audit_watch_handle_event,
  473. .free_group_priv = NULL,
  474. .freeing_mark = NULL,
  475. .free_event_priv = NULL,
  476. };
  477. static int __init audit_watch_init(void)
  478. {
  479. audit_watch_group = fsnotify_obtain_group(AUDIT_WATCH_GROUP_NUM, AUDIT_FS_WATCH,
  480. &audit_watch_fsnotify_ops);
  481. if (IS_ERR(audit_watch_group)) {
  482. audit_watch_group = NULL;
  483. audit_panic("cannot create audit fsnotify group");
  484. }
  485. return 0;
  486. }
  487. subsys_initcall(audit_watch_init);