fd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/sched/signal.h>
  3. #include <linux/errno.h>
  4. #include <linux/dcache.h>
  5. #include <linux/path.h>
  6. #include <linux/fdtable.h>
  7. #include <linux/namei.h>
  8. #include <linux/pid.h>
  9. #include <linux/security.h>
  10. #include <linux/file.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/fs.h>
  13. #include <linux/proc_fs.h>
  14. #include "../mount.h"
  15. #include "internal.h"
  16. #include "fd.h"
  17. static int seq_show(struct seq_file *m, void *v)
  18. {
  19. struct files_struct *files = NULL;
  20. int f_flags = 0, ret = -ENOENT;
  21. struct file *file = NULL;
  22. struct task_struct *task;
  23. task = get_proc_task(m->private);
  24. if (!task)
  25. return -ENOENT;
  26. files = get_files_struct(task);
  27. put_task_struct(task);
  28. if (files) {
  29. unsigned int fd = proc_fd(m->private);
  30. spin_lock(&files->file_lock);
  31. file = fcheck_files(files, fd);
  32. if (file) {
  33. struct fdtable *fdt = files_fdtable(files);
  34. f_flags = file->f_flags;
  35. if (close_on_exec(fd, fdt))
  36. f_flags |= O_CLOEXEC;
  37. get_file(file);
  38. ret = 0;
  39. }
  40. spin_unlock(&files->file_lock);
  41. put_files_struct(files);
  42. }
  43. if (ret)
  44. return ret;
  45. seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
  46. (long long)file->f_pos, f_flags,
  47. real_mount(file->f_path.mnt)->mnt_id);
  48. show_fd_locks(m, file, files);
  49. if (seq_has_overflowed(m))
  50. goto out;
  51. if (file->f_op->show_fdinfo)
  52. file->f_op->show_fdinfo(m, file);
  53. out:
  54. fput(file);
  55. return 0;
  56. }
  57. static int seq_fdinfo_open(struct inode *inode, struct file *file)
  58. {
  59. return single_open(file, seq_show, inode);
  60. }
  61. static const struct file_operations proc_fdinfo_file_operations = {
  62. .open = seq_fdinfo_open,
  63. .read = seq_read,
  64. .llseek = seq_lseek,
  65. .release = single_release,
  66. };
  67. static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
  68. {
  69. struct files_struct *files;
  70. struct task_struct *task;
  71. struct inode *inode;
  72. unsigned int fd;
  73. if (flags & LOOKUP_RCU)
  74. return -ECHILD;
  75. inode = d_inode(dentry);
  76. task = get_proc_task(inode);
  77. fd = proc_fd(inode);
  78. if (task) {
  79. files = get_files_struct(task);
  80. if (files) {
  81. struct file *file;
  82. rcu_read_lock();
  83. file = fcheck_files(files, fd);
  84. if (file) {
  85. unsigned f_mode = file->f_mode;
  86. rcu_read_unlock();
  87. put_files_struct(files);
  88. task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
  89. if (S_ISLNK(inode->i_mode)) {
  90. unsigned i_mode = S_IFLNK;
  91. if (f_mode & FMODE_READ)
  92. i_mode |= S_IRUSR | S_IXUSR;
  93. if (f_mode & FMODE_WRITE)
  94. i_mode |= S_IWUSR | S_IXUSR;
  95. inode->i_mode = i_mode;
  96. }
  97. security_task_to_inode(task, inode);
  98. put_task_struct(task);
  99. return 1;
  100. }
  101. rcu_read_unlock();
  102. put_files_struct(files);
  103. }
  104. put_task_struct(task);
  105. }
  106. return 0;
  107. }
  108. static const struct dentry_operations tid_fd_dentry_operations = {
  109. .d_revalidate = tid_fd_revalidate,
  110. .d_delete = pid_delete_dentry,
  111. };
  112. static int proc_fd_link(struct dentry *dentry, struct path *path)
  113. {
  114. struct files_struct *files = NULL;
  115. struct task_struct *task;
  116. int ret = -ENOENT;
  117. task = get_proc_task(d_inode(dentry));
  118. if (task) {
  119. files = get_files_struct(task);
  120. put_task_struct(task);
  121. }
  122. if (files) {
  123. unsigned int fd = proc_fd(d_inode(dentry));
  124. struct file *fd_file;
  125. spin_lock(&files->file_lock);
  126. fd_file = fcheck_files(files, fd);
  127. if (fd_file) {
  128. *path = fd_file->f_path;
  129. path_get(&fd_file->f_path);
  130. ret = 0;
  131. }
  132. spin_unlock(&files->file_lock);
  133. put_files_struct(files);
  134. }
  135. return ret;
  136. }
  137. static int
  138. proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
  139. struct task_struct *task, const void *ptr)
  140. {
  141. unsigned fd = (unsigned long)ptr;
  142. struct proc_inode *ei;
  143. struct inode *inode;
  144. inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK);
  145. if (!inode)
  146. goto out;
  147. ei = PROC_I(inode);
  148. ei->fd = fd;
  149. inode->i_op = &proc_pid_link_inode_operations;
  150. inode->i_size = 64;
  151. ei->op.proc_get_link = proc_fd_link;
  152. d_set_d_op(dentry, &tid_fd_dentry_operations);
  153. d_add(dentry, inode);
  154. /* Close the race of the process dying before we return the dentry */
  155. if (tid_fd_revalidate(dentry, 0))
  156. return 0;
  157. out:
  158. return -ENOENT;
  159. }
  160. static struct dentry *proc_lookupfd_common(struct inode *dir,
  161. struct dentry *dentry,
  162. instantiate_t instantiate)
  163. {
  164. struct task_struct *task = get_proc_task(dir);
  165. int result = -ENOENT;
  166. unsigned fd = name_to_int(&dentry->d_name);
  167. if (!task)
  168. goto out_no_task;
  169. if (fd == ~0U)
  170. goto out;
  171. result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
  172. out:
  173. put_task_struct(task);
  174. out_no_task:
  175. return ERR_PTR(result);
  176. }
  177. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  178. instantiate_t instantiate)
  179. {
  180. struct task_struct *p = get_proc_task(file_inode(file));
  181. struct files_struct *files;
  182. unsigned int fd;
  183. if (!p)
  184. return -ENOENT;
  185. if (!dir_emit_dots(file, ctx))
  186. goto out;
  187. files = get_files_struct(p);
  188. if (!files)
  189. goto out;
  190. rcu_read_lock();
  191. for (fd = ctx->pos - 2;
  192. fd < files_fdtable(files)->max_fds;
  193. fd++, ctx->pos++) {
  194. char name[10 + 1];
  195. int len;
  196. if (!fcheck_files(files, fd))
  197. continue;
  198. rcu_read_unlock();
  199. len = snprintf(name, sizeof(name), "%u", fd);
  200. if (!proc_fill_cache(file, ctx,
  201. name, len, instantiate, p,
  202. (void *)(unsigned long)fd))
  203. goto out_fd_loop;
  204. cond_resched();
  205. rcu_read_lock();
  206. }
  207. rcu_read_unlock();
  208. out_fd_loop:
  209. put_files_struct(files);
  210. out:
  211. put_task_struct(p);
  212. return 0;
  213. }
  214. static int proc_readfd(struct file *file, struct dir_context *ctx)
  215. {
  216. return proc_readfd_common(file, ctx, proc_fd_instantiate);
  217. }
  218. const struct file_operations proc_fd_operations = {
  219. .read = generic_read_dir,
  220. .iterate_shared = proc_readfd,
  221. .llseek = generic_file_llseek,
  222. };
  223. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  224. unsigned int flags)
  225. {
  226. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  227. }
  228. /*
  229. * /proc/pid/fd needs a special permission handler so that a process can still
  230. * access /proc/self/fd after it has executed a setuid().
  231. */
  232. int proc_fd_permission(struct inode *inode, int mask)
  233. {
  234. struct task_struct *p;
  235. int rv;
  236. rv = generic_permission(inode, mask);
  237. if (rv == 0)
  238. return rv;
  239. rcu_read_lock();
  240. p = pid_task(proc_pid(inode), PIDTYPE_PID);
  241. if (p && same_thread_group(p, current))
  242. rv = 0;
  243. rcu_read_unlock();
  244. return rv;
  245. }
  246. const struct inode_operations proc_fd_inode_operations = {
  247. .lookup = proc_lookupfd,
  248. .permission = proc_fd_permission,
  249. .setattr = proc_setattr,
  250. };
  251. static int
  252. proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
  253. struct task_struct *task, const void *ptr)
  254. {
  255. unsigned fd = (unsigned long)ptr;
  256. struct proc_inode *ei;
  257. struct inode *inode;
  258. inode = proc_pid_make_inode(dir->i_sb, task, S_IFREG | S_IRUSR);
  259. if (!inode)
  260. goto out;
  261. ei = PROC_I(inode);
  262. ei->fd = fd;
  263. inode->i_fop = &proc_fdinfo_file_operations;
  264. d_set_d_op(dentry, &tid_fd_dentry_operations);
  265. d_add(dentry, inode);
  266. /* Close the race of the process dying before we return the dentry */
  267. if (tid_fd_revalidate(dentry, 0))
  268. return 0;
  269. out:
  270. return -ENOENT;
  271. }
  272. static struct dentry *
  273. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  274. {
  275. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  276. }
  277. static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
  278. {
  279. return proc_readfd_common(file, ctx,
  280. proc_fdinfo_instantiate);
  281. }
  282. const struct inode_operations proc_fdinfo_inode_operations = {
  283. .lookup = proc_lookupfdinfo,
  284. .setattr = proc_setattr,
  285. };
  286. const struct file_operations proc_fdinfo_operations = {
  287. .read = generic_read_dir,
  288. .iterate_shared = proc_readfdinfo,
  289. .llseek = generic_file_llseek,
  290. };