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. const 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_group);
  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_inode_mark(&audit_mark->mark, inode, true);
  97. if (ret < 0) {
  98. fsnotify_put_mark(&audit_mark->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=%s",
  116. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  117. audit_get_sessionid(current), op);
  118. audit_log_format(ab, " path=");
  119. audit_log_untrustedstring(ab, audit_mark->path);
  120. audit_log_key(ab, rule->filterkey);
  121. audit_log_format(ab, " list=%d res=1", rule->listnr);
  122. audit_log_end(ab);
  123. }
  124. void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
  125. {
  126. fsnotify_destroy_mark(&audit_mark->mark, audit_fsnotify_group);
  127. fsnotify_put_mark(&audit_mark->mark);
  128. }
  129. void audit_remove_mark_rule(struct audit_krule *krule)
  130. {
  131. struct audit_fsnotify_mark *mark = krule->exe;
  132. audit_remove_mark(mark);
  133. }
  134. static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
  135. {
  136. struct audit_krule *rule = audit_mark->rule;
  137. struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
  138. audit_mark_log_rule_change(audit_mark, "autoremove_rule");
  139. audit_del_rule(entry);
  140. }
  141. /* Update mark data in audit rules based on fsnotify events. */
  142. static int audit_mark_handle_event(struct fsnotify_group *group,
  143. struct inode *to_tell,
  144. u32 mask, const void *data, int data_type,
  145. const unsigned char *dname, u32 cookie,
  146. struct fsnotify_iter_info *iter_info)
  147. {
  148. struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
  149. struct audit_fsnotify_mark *audit_mark;
  150. const struct inode *inode = NULL;
  151. audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
  152. BUG_ON(group != audit_fsnotify_group);
  153. switch (data_type) {
  154. case (FSNOTIFY_EVENT_PATH):
  155. inode = ((const struct path *)data)->dentry->d_inode;
  156. break;
  157. case (FSNOTIFY_EVENT_INODE):
  158. inode = (const struct inode *)data;
  159. break;
  160. default:
  161. BUG();
  162. return 0;
  163. }
  164. if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
  165. if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
  166. return 0;
  167. audit_update_mark(audit_mark, inode);
  168. } else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
  169. audit_autoremove_mark_rule(audit_mark);
  170. return 0;
  171. }
  172. static const struct fsnotify_ops audit_mark_fsnotify_ops = {
  173. .handle_event = audit_mark_handle_event,
  174. .free_mark = audit_fsnotify_free_mark,
  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);