fd.c 8.1 KB

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