audit_fsnotify.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* audit_fsnotify.c -- tracking inodes
  3. *
  4. * Copyright 2003-2009,2014-2015 Red Hat, Inc.
  5. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  6. * Copyright 2005 IBM Corporation
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/audit.h>
  10. #include <linux/kthread.h>
  11. #include <linux/mutex.h>
  12. #include <linux/fs.h>
  13. #include <linux/fsnotify_backend.h>
  14. #include <linux/namei.h>
  15. #include <linux/netlink.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/security.h>
  19. #include "audit.h"
  20. /*
  21. * this mark lives on the parent directory of the inode in question.
  22. * but dev, ino, and path are about the child
  23. */
  24. struct audit_fsnotify_mark {
  25. dev_t dev; /* associated superblock device */
  26. unsigned long ino; /* associated inode number */
  27. char *path; /* insertion path */
  28. struct fsnotify_mark mark; /* fsnotify mark on the inode */
  29. struct audit_krule *rule;
  30. };
  31. /* fsnotify handle. */
  32. static struct fsnotify_group *audit_fsnotify_group;
  33. /* fsnotify events we care about. */
  34. #define AUDIT_FS_EVENTS (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
  35. FS_MOVE_SELF)
  36. static void audit_fsnotify_mark_free(struct audit_fsnotify_mark *audit_mark)
  37. {
  38. kfree(audit_mark->path);
  39. kfree(audit_mark);
  40. }
  41. static void audit_fsnotify_free_mark(struct fsnotify_mark *mark)
  42. {
  43. struct audit_fsnotify_mark *audit_mark;
  44. audit_mark = container_of(mark, struct audit_fsnotify_mark, mark);
  45. audit_fsnotify_mark_free(audit_mark);
  46. }
  47. char *audit_mark_path(struct audit_fsnotify_mark *mark)
  48. {
  49. return mark->path;
  50. }
  51. int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long ino, dev_t dev)
  52. {
  53. if (mark->ino == AUDIT_INO_UNSET)
  54. return 0;
  55. return (mark->ino == ino) && (mark->dev == dev);
  56. }
  57. static void audit_update_mark(struct audit_fsnotify_mark *audit_mark,
  58. const struct inode *inode)
  59. {
  60. audit_mark->dev = inode ? inode->i_sb->s_dev : AUDIT_DEV_UNSET;
  61. audit_mark->ino = inode ? inode->i_ino : AUDIT_INO_UNSET;
  62. }
  63. struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pathname, int len)
  64. {
  65. struct audit_fsnotify_mark *audit_mark;
  66. struct path path;
  67. struct dentry *dentry;
  68. struct inode *inode;
  69. int ret;
  70. if (pathname[0] != '/' || pathname[len-1] == '/')
  71. return ERR_PTR(-EINVAL);
  72. dentry = kern_path_locked(pathname, &path);
  73. if (IS_ERR(dentry))
  74. return (void *)dentry; /* returning an error */
  75. inode = path.dentry->d_inode;
  76. inode_unlock(inode);
  77. audit_mark = kzalloc(sizeof(*audit_mark), GFP_KERNEL);
  78. if (unlikely(!audit_mark)) {
  79. audit_mark = ERR_PTR(-ENOMEM);
  80. goto out;
  81. }
  82. fsnotify_init_mark(&audit_mark->mark, audit_fsnotify_group);
  83. audit_mark->mark.mask = AUDIT_FS_EVENTS;
  84. audit_mark->path = pathname;
  85. audit_update_mark(audit_mark, dentry->d_inode);
  86. audit_mark->rule = krule;
  87. ret = fsnotify_add_inode_mark(&audit_mark->mark, inode, true);
  88. if (ret < 0) {
  89. fsnotify_put_mark(&audit_mark->mark);
  90. audit_mark = ERR_PTR(ret);
  91. }
  92. out:
  93. dput(dentry);
  94. path_put(&path);
  95. return audit_mark;
  96. }
  97. static void audit_mark_log_rule_change(struct audit_fsnotify_mark *audit_mark, char *op)
  98. {
  99. struct audit_buffer *ab;
  100. struct audit_krule *rule = audit_mark->rule;
  101. if (!audit_enabled)
  102. return;
  103. ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
  104. if (unlikely(!ab))
  105. return;
  106. audit_log_session_info(ab);
  107. audit_log_format(ab, " op=%s path=", op);
  108. audit_log_untrustedstring(ab, audit_mark->path);
  109. audit_log_key(ab, rule->filterkey);
  110. audit_log_format(ab, " list=%d res=1", rule->listnr);
  111. audit_log_end(ab);
  112. }
  113. void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
  114. {
  115. fsnotify_destroy_mark(&audit_mark->mark, audit_fsnotify_group);
  116. fsnotify_put_mark(&audit_mark->mark);
  117. }
  118. void audit_remove_mark_rule(struct audit_krule *krule)
  119. {
  120. struct audit_fsnotify_mark *mark = krule->exe;
  121. audit_remove_mark(mark);
  122. }
  123. static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
  124. {
  125. struct audit_krule *rule = audit_mark->rule;
  126. struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
  127. audit_mark_log_rule_change(audit_mark, "autoremove_rule");
  128. audit_del_rule(entry);
  129. }
  130. /* Update mark data in audit rules based on fsnotify events. */
  131. static int audit_mark_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
  132. struct inode *inode, struct inode *dir,
  133. const struct qstr *dname, u32 cookie)
  134. {
  135. struct audit_fsnotify_mark *audit_mark;
  136. audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
  137. if (WARN_ON_ONCE(inode_mark->group != audit_fsnotify_group) ||
  138. WARN_ON_ONCE(!inode))
  139. return 0;
  140. if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
  141. if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
  142. return 0;
  143. audit_update_mark(audit_mark, inode);
  144. } else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF)) {
  145. audit_autoremove_mark_rule(audit_mark);
  146. }
  147. return 0;
  148. }
  149. static const struct fsnotify_ops audit_mark_fsnotify_ops = {
  150. .handle_inode_event = audit_mark_handle_event,
  151. .free_mark = audit_fsnotify_free_mark,
  152. };
  153. static int __init audit_fsnotify_init(void)
  154. {
  155. audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops);
  156. if (IS_ERR(audit_fsnotify_group)) {
  157. audit_fsnotify_group = NULL;
  158. audit_panic("cannot create audit fsnotify group");
  159. }
  160. return 0;
  161. }
  162. device_initcall(audit_fsnotify_init);