fd.c 9.0 KB

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