fdinfo.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/fsnotify_backend.h>
  5. #include <linux/idr.h>
  6. #include <linux/init.h>
  7. #include <linux/inotify.h>
  8. #include <linux/fanotify.h>
  9. #include <linux/kernel.h>
  10. #include <linux/namei.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/exportfs.h>
  15. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  16. #include <linux/susfs_def.h>
  17. #endif
  18. #include "inotify/inotify.h"
  19. #include "fanotify/fanotify.h"
  20. #include "fdinfo.h"
  21. #include "fsnotify.h"
  22. #if defined(CONFIG_PROC_FS)
  23. #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
  24. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  25. static void show_fdinfo(struct seq_file *m, struct file *f,
  26. void (*show)(struct seq_file *m,
  27. struct fsnotify_mark *mark,
  28. struct file *file))
  29. #else
  30. static void show_fdinfo(struct seq_file *m, struct file *f,
  31. void (*show)(struct seq_file *m,
  32. struct fsnotify_mark *mark))
  33. #endif
  34. {
  35. struct fsnotify_group *group = f->private_data;
  36. struct fsnotify_mark *mark;
  37. fsnotify_group_lock(group);
  38. list_for_each_entry(mark, &group->marks_list, g_list) {
  39. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  40. show(m, mark, f);
  41. #else
  42. show(m, mark);
  43. #endif
  44. if (seq_has_overflowed(m))
  45. break;
  46. }
  47. fsnotify_group_unlock(group);
  48. }
  49. #if defined(CONFIG_EXPORTFS)
  50. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  51. {
  52. struct {
  53. struct file_handle handle;
  54. u8 pad[MAX_HANDLE_SZ];
  55. } f;
  56. int size, ret, i;
  57. f.handle.handle_bytes = sizeof(f.pad);
  58. size = f.handle.handle_bytes >> 2;
  59. ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
  60. if ((ret == FILEID_INVALID) || (ret < 0)) {
  61. WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
  62. return;
  63. }
  64. f.handle.handle_type = ret;
  65. f.handle.handle_bytes = size * sizeof(u32);
  66. seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
  67. f.handle.handle_bytes, f.handle.handle_type);
  68. for (i = 0; i < f.handle.handle_bytes; i++)
  69. seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
  70. }
  71. #else
  72. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  73. {
  74. }
  75. #endif
  76. #ifdef CONFIG_INOTIFY_USER
  77. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  78. static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark, struct file *file)
  79. #else
  80. static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  81. #endif
  82. {
  83. struct inotify_inode_mark *inode_mark;
  84. struct inode *inode;
  85. if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
  86. return;
  87. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  88. inode = igrab(fsnotify_conn_inode(mark->connector));
  89. if (inode) {
  90. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  91. if (likely(current->susfs_task_state & TASK_STRUCT_NON_ROOT_USER_APP_PROC) &&
  92. unlikely(inode->i_state & INODE_STATE_SUS_KSTAT)) {
  93. struct path path;
  94. char *pathname = kmalloc(PAGE_SIZE, GFP_KERNEL);
  95. char *dpath;
  96. if (!pathname) {
  97. goto out_seq_printf;
  98. }
  99. dpath = d_path(&file->f_path, pathname, PAGE_SIZE);
  100. if (!dpath) {
  101. goto out_free_pathname;
  102. }
  103. if (kern_path(dpath, 0, &path)) {
  104. goto out_free_pathname;
  105. }
  106. seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ",
  107. inode_mark->wd, path.dentry->d_inode->i_ino, path.dentry->d_inode->i_sb->s_dev,
  108. inotify_mark_user_mask(mark));
  109. show_mark_fhandle(m, path.dentry->d_inode);
  110. seq_putc(m, '\n');
  111. iput(inode);
  112. path_put(&path);
  113. kfree(pathname);
  114. return;
  115. out_free_pathname:
  116. kfree(pathname);
  117. }
  118. out_seq_printf:
  119. #endif
  120. seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ",
  121. inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
  122. inotify_mark_user_mask(mark));
  123. show_mark_fhandle(m, inode);
  124. seq_putc(m, '\n');
  125. iput(inode);
  126. }
  127. }
  128. void inotify_show_fdinfo(struct seq_file *m, struct file *f)
  129. {
  130. show_fdinfo(m, f, inotify_fdinfo);
  131. }
  132. #endif /* CONFIG_INOTIFY_USER */
  133. #ifdef CONFIG_FANOTIFY
  134. static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  135. {
  136. unsigned int mflags = fanotify_mark_user_flags(mark);
  137. struct inode *inode;
  138. if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
  139. inode = igrab(fsnotify_conn_inode(mark->connector));
  140. if (!inode)
  141. return;
  142. seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
  143. inode->i_ino, inode->i_sb->s_dev,
  144. mflags, mark->mask, mark->ignore_mask);
  145. show_mark_fhandle(m, inode);
  146. seq_putc(m, '\n');
  147. iput(inode);
  148. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  149. struct mount *mnt = fsnotify_conn_mount(mark->connector);
  150. seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
  151. mnt->mnt_id, mflags, mark->mask, mark->ignore_mask);
  152. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
  153. struct super_block *sb = fsnotify_conn_sb(mark->connector);
  154. seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
  155. sb->s_dev, mflags, mark->mask, mark->ignore_mask);
  156. }
  157. }
  158. void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  159. {
  160. struct fsnotify_group *group = f->private_data;
  161. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  162. group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
  163. group->fanotify_data.f_flags);
  164. show_fdinfo(m, f, fanotify_fdinfo);
  165. }
  166. #endif /* CONFIG_FANOTIFY */
  167. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  168. #endif /* CONFIG_PROC_FS */