audit_watch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/namei.h>
  27. #include <linux/netlink.h>
  28. #include <linux/sched.h>
  29. #include <linux/inotify.h>
  30. #include <linux/security.h>
  31. #include "audit.h"
  32. /*
  33. * Reference counting:
  34. *
  35. * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
  36. * event. Each audit_watch holds a reference to its associated parent.
  37. *
  38. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  39. * audit_remove_watch(). Additionally, an audit_watch may exist
  40. * temporarily to assist in searching existing filter data. Each
  41. * audit_krule holds a reference to its associated watch.
  42. */
  43. struct audit_watch {
  44. atomic_t count; /* reference count */
  45. char *path; /* insertion path */
  46. dev_t dev; /* associated superblock device */
  47. unsigned long ino; /* associated inode number */
  48. struct audit_parent *parent; /* associated parent */
  49. struct list_head wlist; /* entry in parent->watches list */
  50. struct list_head rules; /* associated rules */
  51. };
  52. struct audit_parent {
  53. struct list_head ilist; /* entry in inotify registration list */
  54. struct list_head watches; /* associated watches */
  55. struct inotify_watch wdata; /* inotify watch data */
  56. unsigned flags; /* status flags */
  57. };
  58. /* Inotify handle. */
  59. struct inotify_handle *audit_ih;
  60. /*
  61. * audit_parent status flags:
  62. *
  63. * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
  64. * a filesystem event to ensure we're adding audit watches to a valid parent.
  65. * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
  66. * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
  67. * we can receive while holding nameidata.
  68. */
  69. #define AUDIT_PARENT_INVALID 0x001
  70. /* Inotify events we care about. */
  71. #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
  72. static void audit_free_parent(struct inotify_watch *i_watch)
  73. {
  74. struct audit_parent *parent;
  75. parent = container_of(i_watch, struct audit_parent, wdata);
  76. WARN_ON(!list_empty(&parent->watches));
  77. kfree(parent);
  78. }
  79. void audit_get_watch(struct audit_watch *watch)
  80. {
  81. atomic_inc(&watch->count);
  82. }
  83. void audit_put_watch(struct audit_watch *watch)
  84. {
  85. if (atomic_dec_and_test(&watch->count)) {
  86. WARN_ON(watch->parent);
  87. WARN_ON(!list_empty(&watch->rules));
  88. kfree(watch->path);
  89. kfree(watch);
  90. }
  91. }
  92. void audit_remove_watch(struct audit_watch *watch)
  93. {
  94. list_del(&watch->wlist);
  95. put_inotify_watch(&watch->parent->wdata);
  96. watch->parent = NULL;
  97. audit_put_watch(watch); /* match initial get */
  98. }
  99. char *audit_watch_path(struct audit_watch *watch)
  100. {
  101. return watch->path;
  102. }
  103. struct list_head *audit_watch_rules(struct audit_watch *watch)
  104. {
  105. return &watch->rules;
  106. }
  107. unsigned long audit_watch_inode(struct audit_watch *watch)
  108. {
  109. return watch->ino;
  110. }
  111. dev_t audit_watch_dev(struct audit_watch *watch)
  112. {
  113. return watch->dev;
  114. }
  115. /* Initialize a parent watch entry. */
  116. static struct audit_parent *audit_init_parent(struct nameidata *ndp)
  117. {
  118. struct audit_parent *parent;
  119. s32 wd;
  120. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  121. if (unlikely(!parent))
  122. return ERR_PTR(-ENOMEM);
  123. INIT_LIST_HEAD(&parent->watches);
  124. parent->flags = 0;
  125. inotify_init_watch(&parent->wdata);
  126. /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
  127. get_inotify_watch(&parent->wdata);
  128. wd = inotify_add_watch(audit_ih, &parent->wdata,
  129. ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
  130. if (wd < 0) {
  131. audit_free_parent(&parent->wdata);
  132. return ERR_PTR(wd);
  133. }
  134. return parent;
  135. }
  136. /* Initialize a watch entry. */
  137. static struct audit_watch *audit_init_watch(char *path)
  138. {
  139. struct audit_watch *watch;
  140. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  141. if (unlikely(!watch))
  142. return ERR_PTR(-ENOMEM);
  143. INIT_LIST_HEAD(&watch->rules);
  144. atomic_set(&watch->count, 1);
  145. watch->path = path;
  146. watch->dev = (dev_t)-1;
  147. watch->ino = (unsigned long)-1;
  148. return watch;
  149. }
  150. /* Translate a watch string to kernel respresentation. */
  151. int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  152. {
  153. struct audit_watch *watch;
  154. if (!audit_ih)
  155. return -EOPNOTSUPP;
  156. if (path[0] != '/' || path[len-1] == '/' ||
  157. krule->listnr != AUDIT_FILTER_EXIT ||
  158. op != Audit_equal ||
  159. krule->inode_f || krule->watch || krule->tree)
  160. return -EINVAL;
  161. watch = audit_init_watch(path);
  162. if (IS_ERR(watch))
  163. return PTR_ERR(watch);
  164. audit_get_watch(watch);
  165. krule->watch = watch;
  166. return 0;
  167. }
  168. /* Duplicate the given audit watch. The new watch's rules list is initialized
  169. * to an empty list and wlist is undefined. */
  170. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  171. {
  172. char *path;
  173. struct audit_watch *new;
  174. path = kstrdup(old->path, GFP_KERNEL);
  175. if (unlikely(!path))
  176. return ERR_PTR(-ENOMEM);
  177. new = audit_init_watch(path);
  178. if (IS_ERR(new)) {
  179. kfree(path);
  180. goto out;
  181. }
  182. new->dev = old->dev;
  183. new->ino = old->ino;
  184. get_inotify_watch(&old->parent->wdata);
  185. new->parent = old->parent;
  186. out:
  187. return new;
  188. }
  189. static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  190. {
  191. if (audit_enabled) {
  192. struct audit_buffer *ab;
  193. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  194. audit_log_format(ab, "auid=%u ses=%u op=",
  195. audit_get_loginuid(current),
  196. audit_get_sessionid(current));
  197. audit_log_string(ab, op);
  198. audit_log_format(ab, " path=");
  199. audit_log_untrustedstring(ab, w->path);
  200. audit_log_key(ab, r->filterkey);
  201. audit_log_format(ab, " list=%d res=1", r->listnr);
  202. audit_log_end(ab);
  203. }
  204. }
  205. /* Update inode info in audit rules based on filesystem event. */
  206. static void audit_update_watch(struct audit_parent *parent,
  207. const char *dname, dev_t dev,
  208. unsigned long ino, unsigned invalidating)
  209. {
  210. struct audit_watch *owatch, *nwatch, *nextw;
  211. struct audit_krule *r, *nextr;
  212. struct audit_entry *oentry, *nentry;
  213. mutex_lock(&audit_filter_mutex);
  214. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  215. if (audit_compare_dname_path(dname, owatch->path, NULL))
  216. continue;
  217. /* If the update involves invalidating rules, do the inode-based
  218. * filtering now, so we don't omit records. */
  219. if (invalidating && current->audit_context)
  220. audit_filter_inodes(current, current->audit_context);
  221. nwatch = audit_dupe_watch(owatch);
  222. if (IS_ERR(nwatch)) {
  223. mutex_unlock(&audit_filter_mutex);
  224. audit_panic("error updating watch, skipping");
  225. return;
  226. }
  227. nwatch->dev = dev;
  228. nwatch->ino = ino;
  229. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  230. oentry = container_of(r, struct audit_entry, rule);
  231. list_del(&oentry->rule.rlist);
  232. list_del_rcu(&oentry->list);
  233. nentry = audit_dupe_rule(&oentry->rule, nwatch);
  234. if (IS_ERR(nentry)) {
  235. list_del(&oentry->rule.list);
  236. audit_panic("error updating watch, removing");
  237. } else {
  238. int h = audit_hash_ino((u32)ino);
  239. list_add(&nentry->rule.rlist, &nwatch->rules);
  240. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  241. list_replace(&oentry->rule.list,
  242. &nentry->rule.list);
  243. }
  244. audit_watch_log_rule_change(r, owatch, "updated rules");
  245. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  246. }
  247. audit_remove_watch(owatch);
  248. goto add_watch_to_parent; /* event applies to a single watch */
  249. }
  250. mutex_unlock(&audit_filter_mutex);
  251. return;
  252. add_watch_to_parent:
  253. list_add(&nwatch->wlist, &parent->watches);
  254. mutex_unlock(&audit_filter_mutex);
  255. return;
  256. }
  257. /* Remove all watches & rules associated with a parent that is going away. */
  258. static void audit_remove_parent_watches(struct audit_parent *parent)
  259. {
  260. struct audit_watch *w, *nextw;
  261. struct audit_krule *r, *nextr;
  262. struct audit_entry *e;
  263. mutex_lock(&audit_filter_mutex);
  264. parent->flags |= AUDIT_PARENT_INVALID;
  265. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  266. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  267. e = container_of(r, struct audit_entry, rule);
  268. audit_watch_log_rule_change(r, w, "remove rule");
  269. list_del(&r->rlist);
  270. list_del(&r->list);
  271. list_del_rcu(&e->list);
  272. call_rcu(&e->rcu, audit_free_rule_rcu);
  273. }
  274. audit_remove_watch(w);
  275. }
  276. mutex_unlock(&audit_filter_mutex);
  277. }
  278. /* Unregister inotify watches for parents on in_list.
  279. * Generates an IN_IGNORED event. */
  280. void audit_inotify_unregister(struct list_head *in_list)
  281. {
  282. struct audit_parent *p, *n;
  283. list_for_each_entry_safe(p, n, in_list, ilist) {
  284. list_del(&p->ilist);
  285. inotify_rm_watch(audit_ih, &p->wdata);
  286. /* the unpin matching the pin in audit_do_del_rule() */
  287. unpin_inotify_watch(&p->wdata);
  288. }
  289. }
  290. /* Get path information necessary for adding watches. */
  291. static int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw)
  292. {
  293. struct nameidata *ndparent, *ndwatch;
  294. int err;
  295. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  296. if (unlikely(!ndparent))
  297. return -ENOMEM;
  298. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  299. if (unlikely(!ndwatch)) {
  300. kfree(ndparent);
  301. return -ENOMEM;
  302. }
  303. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  304. if (err) {
  305. kfree(ndparent);
  306. kfree(ndwatch);
  307. return err;
  308. }
  309. err = path_lookup(path, 0, ndwatch);
  310. if (err) {
  311. kfree(ndwatch);
  312. ndwatch = NULL;
  313. }
  314. *ndp = ndparent;
  315. *ndw = ndwatch;
  316. return 0;
  317. }
  318. /* Release resources used for watch path information. */
  319. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  320. {
  321. if (ndp) {
  322. path_put(&ndp->path);
  323. kfree(ndp);
  324. }
  325. if (ndw) {
  326. path_put(&ndw->path);
  327. kfree(ndw);
  328. }
  329. }
  330. /* Associate the given rule with an existing parent inotify_watch.
  331. * Caller must hold audit_filter_mutex. */
  332. static void audit_add_to_parent(struct audit_krule *krule,
  333. struct audit_parent *parent)
  334. {
  335. struct audit_watch *w, *watch = krule->watch;
  336. int watch_found = 0;
  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. get_inotify_watch(&parent->wdata);
  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)
  358. {
  359. struct audit_watch *watch = krule->watch;
  360. struct inotify_watch *i_watch;
  361. struct audit_parent *parent;
  362. struct nameidata *ndp = NULL, *ndw = NULL;
  363. int ret = 0;
  364. mutex_unlock(&audit_filter_mutex);
  365. /* Avoid calling path_lookup under audit_filter_mutex. */
  366. ret = audit_get_nd(watch->path, &ndp, &ndw);
  367. if (ret) {
  368. /* caller expects mutex locked */
  369. mutex_lock(&audit_filter_mutex);
  370. goto error;
  371. }
  372. /* update watch filter fields */
  373. if (ndw) {
  374. watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
  375. watch->ino = ndw->path.dentry->d_inode->i_ino;
  376. }
  377. /* The audit_filter_mutex must not be held during inotify calls because
  378. * we hold it during inotify event callback processing. If an existing
  379. * inotify watch is found, inotify_find_watch() grabs a reference before
  380. * returning.
  381. */
  382. if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
  383. &i_watch) < 0) {
  384. parent = audit_init_parent(ndp);
  385. if (IS_ERR(parent)) {
  386. /* caller expects mutex locked */
  387. mutex_lock(&audit_filter_mutex);
  388. ret = PTR_ERR(parent);
  389. goto error;
  390. }
  391. } else
  392. parent = container_of(i_watch, struct audit_parent, wdata);
  393. mutex_lock(&audit_filter_mutex);
  394. /* parent was moved before we took audit_filter_mutex */
  395. if (parent->flags & AUDIT_PARENT_INVALID)
  396. ret = -ENOENT;
  397. else
  398. audit_add_to_parent(krule, parent);
  399. /* match get in audit_init_parent or inotify_find_watch */
  400. put_inotify_watch(&parent->wdata);
  401. error:
  402. audit_put_nd(ndp, ndw); /* NULL args OK */
  403. return ret;
  404. }
  405. void audit_remove_watch_rule(struct audit_krule *krule, struct list_head *list)
  406. {
  407. struct audit_watch *watch = krule->watch;
  408. struct audit_parent *parent = watch->parent;
  409. list_del(&krule->rlist);
  410. if (list_empty(&watch->rules)) {
  411. audit_remove_watch(watch);
  412. if (list_empty(&parent->watches)) {
  413. /* Put parent on the inotify un-registration
  414. * list. Grab a reference before releasing
  415. * audit_filter_mutex, to be released in
  416. * audit_inotify_unregister().
  417. * If filesystem is going away, just leave
  418. * the sucker alone, eviction will take
  419. * care of it. */
  420. if (pin_inotify_watch(&parent->wdata))
  421. list_add(&parent->ilist, list);
  422. }
  423. }
  424. }
  425. /* Update watch data in audit rules based on inotify events. */
  426. static void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
  427. u32 cookie, const char *dname, struct inode *inode)
  428. {
  429. struct audit_parent *parent;
  430. parent = container_of(i_watch, struct audit_parent, wdata);
  431. if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
  432. audit_update_watch(parent, dname, inode->i_sb->s_dev,
  433. inode->i_ino, 0);
  434. else if (mask & (IN_DELETE|IN_MOVED_FROM))
  435. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  436. /* inotify automatically removes the watch and sends IN_IGNORED */
  437. else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
  438. audit_remove_parent_watches(parent);
  439. /* inotify does not remove the watch, so remove it manually */
  440. else if(mask & IN_MOVE_SELF) {
  441. audit_remove_parent_watches(parent);
  442. inotify_remove_watch_locked(audit_ih, i_watch);
  443. } else if (mask & IN_IGNORED)
  444. put_inotify_watch(i_watch);
  445. }
  446. static const struct inotify_operations audit_inotify_ops = {
  447. .handle_event = audit_handle_ievent,
  448. .destroy_watch = audit_free_parent,
  449. };
  450. static int __init audit_watch_init(void)
  451. {
  452. audit_ih = inotify_init(&audit_inotify_ops);
  453. if (IS_ERR(audit_ih))
  454. audit_panic("cannot initialize inotify handle");
  455. return 0;
  456. }
  457. subsys_initcall(audit_watch_init);