fd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
  68. {
  69. struct files_struct *files = get_files_struct(task);
  70. struct file *file;
  71. if (!files)
  72. return false;
  73. rcu_read_lock();
  74. file = fcheck_files(files, fd);
  75. if (file)
  76. *mode = file->f_mode;
  77. rcu_read_unlock();
  78. put_files_struct(files);
  79. return !!file;
  80. }
  81. static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
  82. fmode_t f_mode)
  83. {
  84. task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
  85. if (S_ISLNK(inode->i_mode)) {
  86. unsigned i_mode = S_IFLNK;
  87. if (f_mode & FMODE_READ)
  88. i_mode |= S_IRUSR | S_IXUSR;
  89. if (f_mode & FMODE_WRITE)
  90. i_mode |= S_IWUSR | S_IXUSR;
  91. inode->i_mode = i_mode;
  92. }
  93. security_task_to_inode(task, inode);
  94. }
  95. static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
  96. {
  97. struct task_struct *task;
  98. struct inode *inode;
  99. unsigned int fd;
  100. if (flags & LOOKUP_RCU)
  101. return -ECHILD;
  102. inode = d_inode(dentry);
  103. task = get_proc_task(inode);
  104. fd = proc_fd(inode);
  105. if (task) {
  106. fmode_t f_mode;
  107. if (tid_fd_mode(task, fd, &f_mode)) {
  108. tid_fd_update_inode(task, inode, f_mode);
  109. put_task_struct(task);
  110. return 1;
  111. }
  112. put_task_struct(task);
  113. }
  114. return 0;
  115. }
  116. static const struct dentry_operations tid_fd_dentry_operations = {
  117. .d_revalidate = tid_fd_revalidate,
  118. .d_delete = pid_delete_dentry,
  119. };
  120. static int proc_fd_link(struct dentry *dentry, struct path *path)
  121. {
  122. struct files_struct *files = NULL;
  123. struct task_struct *task;
  124. int ret = -ENOENT;
  125. task = get_proc_task(d_inode(dentry));
  126. if (task) {
  127. files = get_files_struct(task);
  128. put_task_struct(task);
  129. }
  130. if (files) {
  131. unsigned int fd = proc_fd(d_inode(dentry));
  132. struct file *fd_file;
  133. spin_lock(&files->file_lock);
  134. fd_file = fcheck_files(files, fd);
  135. if (fd_file) {
  136. *path = fd_file->f_path;
  137. path_get(&fd_file->f_path);
  138. ret = 0;
  139. }
  140. spin_unlock(&files->file_lock);
  141. put_files_struct(files);
  142. }
  143. return ret;
  144. }
  145. struct fd_data {
  146. fmode_t mode;
  147. unsigned fd;
  148. };
  149. static struct dentry *proc_fd_instantiate(struct dentry *dentry,
  150. struct task_struct *task, const void *ptr)
  151. {
  152. const struct fd_data *data = ptr;
  153. struct proc_inode *ei;
  154. struct inode *inode;
  155. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
  156. if (!inode)
  157. return ERR_PTR(-ENOENT);
  158. ei = PROC_I(inode);
  159. ei->fd = data->fd;
  160. inode->i_op = &proc_pid_link_inode_operations;
  161. inode->i_size = 64;
  162. ei->op.proc_get_link = proc_fd_link;
  163. tid_fd_update_inode(task, inode, data->mode);
  164. d_set_d_op(dentry, &tid_fd_dentry_operations);
  165. return d_splice_alias(inode, dentry);
  166. }
  167. static struct dentry *proc_lookupfd_common(struct inode *dir,
  168. struct dentry *dentry,
  169. instantiate_t instantiate)
  170. {
  171. struct task_struct *task = get_proc_task(dir);
  172. struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
  173. struct dentry *result = ERR_PTR(-ENOENT);
  174. if (!task)
  175. goto out_no_task;
  176. if (data.fd == ~0U)
  177. goto out;
  178. if (!tid_fd_mode(task, data.fd, &data.mode))
  179. goto out;
  180. result = instantiate(dentry, task, &data);
  181. out:
  182. put_task_struct(task);
  183. out_no_task:
  184. return result;
  185. }
  186. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  187. instantiate_t instantiate)
  188. {
  189. struct task_struct *p = get_proc_task(file_inode(file));
  190. struct files_struct *files;
  191. unsigned int fd;
  192. if (!p)
  193. return -ENOENT;
  194. if (!dir_emit_dots(file, ctx))
  195. goto out;
  196. files = get_files_struct(p);
  197. if (!files)
  198. goto out;
  199. rcu_read_lock();
  200. for (fd = ctx->pos - 2;
  201. fd < files_fdtable(files)->max_fds;
  202. fd++, ctx->pos++) {
  203. struct file *f;
  204. struct fd_data data;
  205. char name[10 + 1];
  206. unsigned int len;
  207. f = fcheck_files(files, fd);
  208. if (!f)
  209. continue;
  210. data.mode = f->f_mode;
  211. rcu_read_unlock();
  212. data.fd = fd;
  213. len = snprintf(name, sizeof(name), "%u", fd);
  214. if (!proc_fill_cache(file, ctx,
  215. name, len, instantiate, p,
  216. &data))
  217. goto out_fd_loop;
  218. cond_resched();
  219. rcu_read_lock();
  220. }
  221. rcu_read_unlock();
  222. out_fd_loop:
  223. put_files_struct(files);
  224. out:
  225. put_task_struct(p);
  226. return 0;
  227. }
  228. static int proc_readfd(struct file *file, struct dir_context *ctx)
  229. {
  230. return proc_readfd_common(file, ctx, proc_fd_instantiate);
  231. }
  232. const struct file_operations proc_fd_operations = {
  233. .read = generic_read_dir,
  234. .iterate_shared = proc_readfd,
  235. .llseek = generic_file_llseek,
  236. };
  237. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  238. unsigned int flags)
  239. {
  240. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  241. }
  242. /*
  243. * /proc/pid/fd needs a special permission handler so that a process can still
  244. * access /proc/self/fd after it has executed a setuid().
  245. */
  246. int proc_fd_permission(struct inode *inode, int mask)
  247. {
  248. struct task_struct *p;
  249. int rv;
  250. rv = generic_permission(inode, mask);
  251. if (rv == 0)
  252. return rv;
  253. rcu_read_lock();
  254. p = pid_task(proc_pid(inode), PIDTYPE_PID);
  255. if (p && same_thread_group(p, current))
  256. rv = 0;
  257. rcu_read_unlock();
  258. return rv;
  259. }
  260. const struct inode_operations proc_fd_inode_operations = {
  261. .lookup = proc_lookupfd,
  262. .permission = proc_fd_permission,
  263. .setattr = proc_setattr,
  264. };
  265. static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
  266. struct task_struct *task, const void *ptr)
  267. {
  268. const struct fd_data *data = ptr;
  269. struct proc_inode *ei;
  270. struct inode *inode;
  271. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
  272. if (!inode)
  273. return ERR_PTR(-ENOENT);
  274. ei = PROC_I(inode);
  275. ei->fd = data->fd;
  276. inode->i_fop = &proc_fdinfo_file_operations;
  277. tid_fd_update_inode(task, inode, 0);
  278. d_set_d_op(dentry, &tid_fd_dentry_operations);
  279. return d_splice_alias(inode, dentry);
  280. }
  281. static struct dentry *
  282. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  283. {
  284. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  285. }
  286. static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
  287. {
  288. return proc_readfd_common(file, ctx,
  289. proc_fdinfo_instantiate);
  290. }
  291. const struct inode_operations proc_fdinfo_inode_operations = {
  292. .lookup = proc_lookupfdinfo,
  293. .setattr = proc_setattr,
  294. };
  295. const struct file_operations proc_fdinfo_operations = {
  296. .read = generic_read_dir,
  297. .iterate_shared = proc_readfdinfo,
  298. .llseek = generic_file_llseek,
  299. };