audit_fsnotify.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* audit_fsnotify.c -- tracking inodes
  2. *
  3. * Copyright 2003-2009,2014-2015 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. #include <linux/kernel.h>
  18. #include <linux/audit.h>
  19. #include <linux/kthread.h>
  20. #include <linux/mutex.h>
  21. #include <linux/fs.h>
  22. #include <linux/fsnotify_backend.h>
  23. #include <linux/namei.h>
  24. #include <linux/netlink.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/security.h>
  28. #include "audit.h"
  29. /*
  30. * this mark lives on the parent directory of the inode in question.
  31. * but dev, ino, and path are about the child
  32. */
  33. struct audit_fsnotify_mark {
  34. dev_t dev; /* associated superblock device */
  35. unsigned long ino; /* associated inode number */
  36. char *path; /* insertion path */
  37. struct fsnotify_mark mark; /* fsnotify mark on the inode */
  38. struct audit_krule *rule;
  39. };
  40. /* fsnotify handle. */
  41. static struct fsnotify_group *audit_fsnotify_group;
  42. /* fsnotify events we care about. */
  43. #define AUDIT_FS_EVENTS (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  44. FS_MOVE_SELF | FS_EVENT_ON_CHILD)
  45. static void audit_fsnotify_mark_free(struct audit_fsnotify_mark *audit_mark)
  46. {
  47. kfree(audit_mark->path);
  48. kfree(audit_mark);
  49. }
  50. static void audit_fsnotify_free_mark(struct fsnotify_mark *mark)
  51. {
  52. struct audit_fsnotify_mark *audit_mark;
  53. audit_mark = container_of(mark, struct audit_fsnotify_mark, mark);
  54. audit_fsnotify_mark_free(audit_mark);
  55. }
  56. char *audit_mark_path(struct audit_fsnotify_mark *mark)
  57. {
  58. return mark->path;
  59. }
  60. int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long ino, dev_t dev)
  61. {
  62. if (mark->ino == AUDIT_INO_UNSET)
  63. return 0;
  64. return (mark->ino == ino) && (mark->dev == dev);
  65. }
  66. static void audit_update_mark(struct audit_fsnotify_mark *audit_mark,
  67. struct inode *inode)
  68. {
  69. audit_mark->dev = inode ? inode->i_sb->s_dev : AUDIT_DEV_UNSET;
  70. audit_mark->ino = inode ? inode->i_ino : AUDIT_INO_UNSET;
  71. }
  72. struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pathname, int len)
  73. {
  74. struct audit_fsnotify_mark *audit_mark;
  75. struct path path;
  76. struct dentry *dentry;
  77. struct inode *inode;
  78. int ret;
  79. if (pathname[0] != '/' || pathname[len-1] == '/')
  80. return ERR_PTR(-EINVAL);
  81. dentry = kern_path_locked(pathname, &path);
  82. if (IS_ERR(dentry))
  83. return (void *)dentry; /* returning an error */
  84. inode = path.dentry->d_inode;
  85. inode_unlock(inode);
  86. audit_mark = kzalloc(sizeof(*audit_mark), GFP_KERNEL);
  87. if (unlikely(!audit_mark)) {
  88. audit_mark = ERR_PTR(-ENOMEM);
  89. goto out;
  90. }
  91. fsnotify_init_mark(&audit_mark->mark, audit_fsnotify_free_mark);
  92. audit_mark->mark.mask = AUDIT_FS_EVENTS;
  93. audit_mark->path = pathname;
  94. audit_update_mark(audit_mark, dentry->d_inode);
  95. audit_mark->rule = krule;
  96. ret = fsnotify_add_mark(&audit_mark->mark, audit_fsnotify_group, inode, NULL, true);
  97. if (ret < 0) {
  98. audit_fsnotify_mark_free(audit_mark);
  99. audit_mark = ERR_PTR(ret);
  100. }
  101. out:
  102. dput(dentry);
  103. path_put(&path);
  104. return audit_mark;
  105. }
  106. static void audit_mark_log_rule_change(struct audit_fsnotify_mark *audit_mark, char *op)
  107. {
  108. struct audit_buffer *ab;
  109. struct audit_krule *rule = audit_mark->rule;
  110. if (!audit_enabled)
  111. return;
  112. ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
  113. if (unlikely(!ab))
  114. return;
  115. audit_log_format(ab, "auid=%u ses=%u op=",
  116. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  117. audit_get_sessionid(current));
  118. audit_log_string(ab, op);
  119. audit_log_format(ab, " path=");
  120. audit_log_untrustedstring(ab, audit_mark->path);
  121. audit_log_key(ab, rule->filterkey);
  122. audit_log_format(ab, " list=%d res=1", rule->listnr);
  123. audit_log_end(ab);
  124. }
  125. void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
  126. {
  127. fsnotify_destroy_mark(&audit_mark->mark, audit_fsnotify_group);
  128. fsnotify_put_mark(&audit_mark->mark);
  129. }
  130. void audit_remove_mark_rule(struct audit_krule *krule)
  131. {
  132. struct audit_fsnotify_mark *mark = krule->exe;
  133. audit_remove_mark(mark);
  134. }
  135. static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
  136. {
  137. struct audit_krule *rule = audit_mark->rule;
  138. struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
  139. audit_mark_log_rule_change(audit_mark, "autoremove_rule");
  140. audit_del_rule(entry);
  141. }
  142. /* Update mark data in audit rules based on fsnotify events. */
  143. static int audit_mark_handle_event(struct fsnotify_group *group,
  144. struct inode *to_tell,
  145. struct fsnotify_mark *inode_mark,
  146. struct fsnotify_mark *vfsmount_mark,
  147. u32 mask, void *data, int data_type,
  148. const unsigned char *dname, u32 cookie)
  149. {
  150. struct audit_fsnotify_mark *audit_mark;
  151. struct inode *inode = NULL;
  152. audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
  153. BUG_ON(group != audit_fsnotify_group);
  154. switch (data_type) {
  155. case (FSNOTIFY_EVENT_PATH):
  156. inode = ((struct path *)data)->dentry->d_inode;
  157. break;
  158. case (FSNOTIFY_EVENT_INODE):
  159. inode = (struct inode *)data;
  160. break;
  161. default:
  162. BUG();
  163. return 0;
  164. };
  165. if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
  166. if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
  167. return 0;
  168. audit_update_mark(audit_mark, inode);
  169. } else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  170. audit_autoremove_mark_rule(audit_mark);
  171. return 0;
  172. }
  173. static const struct fsnotify_ops audit_mark_fsnotify_ops = {
  174. .handle_event = audit_mark_handle_event,
  175. };
  176. static int __init audit_fsnotify_init(void)
  177. {
  178. audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops);
  179. if (IS_ERR(audit_fsnotify_group)) {
  180. audit_fsnotify_group = NULL;
  181. audit_panic("cannot create audit fsnotify group");
  182. }
  183. return 0;
  184. }
  185. device_initcall(audit_fsnotify_init);