audit_watch.c 14 KB

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