fd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. rcu_read_lock();
  98. file = task_lookup_fdget_rcu(task, fd);
  99. rcu_read_unlock();
  100. if (file) {
  101. *mode = file->f_mode;
  102. fput(file);
  103. }
  104. return !!file;
  105. }
  106. static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
  107. fmode_t f_mode)
  108. {
  109. task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
  110. if (S_ISLNK(inode->i_mode)) {
  111. unsigned i_mode = S_IFLNK;
  112. if (f_mode & FMODE_READ)
  113. i_mode |= S_IRUSR | S_IXUSR;
  114. if (f_mode & FMODE_WRITE)
  115. i_mode |= S_IWUSR | S_IXUSR;
  116. inode->i_mode = i_mode;
  117. }
  118. security_task_to_inode(task, inode);
  119. }
  120. static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
  121. {
  122. struct task_struct *task;
  123. struct inode *inode;
  124. unsigned int fd;
  125. if (flags & LOOKUP_RCU)
  126. return -ECHILD;
  127. inode = d_inode(dentry);
  128. task = get_proc_task(inode);
  129. fd = proc_fd(inode);
  130. if (task) {
  131. fmode_t f_mode;
  132. if (tid_fd_mode(task, fd, &f_mode)) {
  133. tid_fd_update_inode(task, inode, f_mode);
  134. put_task_struct(task);
  135. return 1;
  136. }
  137. put_task_struct(task);
  138. }
  139. return 0;
  140. }
  141. static const struct dentry_operations tid_fd_dentry_operations = {
  142. .d_revalidate = tid_fd_revalidate,
  143. .d_delete = pid_delete_dentry,
  144. };
  145. static int proc_fd_link(struct dentry *dentry, struct path *path)
  146. {
  147. struct task_struct *task;
  148. int ret = -ENOENT;
  149. task = get_proc_task(d_inode(dentry));
  150. if (task) {
  151. unsigned int fd = proc_fd(d_inode(dentry));
  152. struct file *fd_file;
  153. fd_file = fget_task(task, fd);
  154. if (fd_file) {
  155. *path = fd_file->f_path;
  156. path_get(&fd_file->f_path);
  157. ret = 0;
  158. fput(fd_file);
  159. }
  160. put_task_struct(task);
  161. }
  162. return ret;
  163. }
  164. struct fd_data {
  165. fmode_t mode;
  166. unsigned fd;
  167. };
  168. static struct dentry *proc_fd_instantiate(struct dentry *dentry,
  169. struct task_struct *task, const void *ptr)
  170. {
  171. const struct fd_data *data = ptr;
  172. struct proc_inode *ei;
  173. struct inode *inode;
  174. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
  175. if (!inode)
  176. return ERR_PTR(-ENOENT);
  177. ei = PROC_I(inode);
  178. ei->fd = data->fd;
  179. inode->i_op = &proc_pid_link_inode_operations;
  180. inode->i_size = 64;
  181. ei->op.proc_get_link = proc_fd_link;
  182. tid_fd_update_inode(task, inode, data->mode);
  183. return proc_splice_unmountable(inode, dentry,
  184. &tid_fd_dentry_operations);
  185. }
  186. static struct dentry *proc_lookupfd_common(struct inode *dir,
  187. struct dentry *dentry,
  188. instantiate_t instantiate)
  189. {
  190. struct task_struct *task = get_proc_task(dir);
  191. struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
  192. struct dentry *result = ERR_PTR(-ENOENT);
  193. if (!task)
  194. goto out_no_task;
  195. if (data.fd == ~0U)
  196. goto out;
  197. if (!tid_fd_mode(task, data.fd, &data.mode))
  198. goto out;
  199. result = instantiate(dentry, task, &data);
  200. out:
  201. put_task_struct(task);
  202. out_no_task:
  203. return result;
  204. }
  205. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  206. instantiate_t instantiate)
  207. {
  208. struct task_struct *p = get_proc_task(file_inode(file));
  209. unsigned int fd;
  210. if (!p)
  211. return -ENOENT;
  212. if (!dir_emit_dots(file, ctx))
  213. goto out;
  214. rcu_read_lock();
  215. for (fd = ctx->pos - 2;; fd++) {
  216. struct file *f;
  217. struct fd_data data;
  218. char name[10 + 1];
  219. unsigned int len;
  220. f = task_lookup_next_fdget_rcu(p, &fd);
  221. ctx->pos = fd + 2LL;
  222. if (!f)
  223. break;
  224. data.mode = f->f_mode;
  225. rcu_read_unlock();
  226. fput(f);
  227. data.fd = fd;
  228. len = snprintf(name, sizeof(name), "%u", fd);
  229. if (!proc_fill_cache(file, ctx,
  230. name, len, instantiate, p,
  231. &data))
  232. goto out;
  233. cond_resched();
  234. rcu_read_lock();
  235. }
  236. rcu_read_unlock();
  237. out:
  238. put_task_struct(p);
  239. return 0;
  240. }
  241. static int proc_readfd_count(struct inode *inode, loff_t *count)
  242. {
  243. struct task_struct *p = get_proc_task(inode);
  244. struct fdtable *fdt;
  245. if (!p)
  246. return -ENOENT;
  247. task_lock(p);
  248. if (p->files) {
  249. rcu_read_lock();
  250. fdt = files_fdtable(p->files);
  251. *count = bitmap_weight(fdt->open_fds, fdt->max_fds);
  252. rcu_read_unlock();
  253. }
  254. task_unlock(p);
  255. put_task_struct(p);
  256. return 0;
  257. }
  258. static int proc_fd_iterate(struct file *file, struct dir_context *ctx)
  259. {
  260. return proc_readfd_common(file, ctx, proc_fd_instantiate);
  261. }
  262. const struct file_operations proc_fd_operations = {
  263. .read = generic_read_dir,
  264. .iterate_shared = proc_fd_iterate,
  265. .llseek = generic_file_llseek,
  266. };
  267. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  268. unsigned int flags)
  269. {
  270. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  271. }
  272. /*
  273. * /proc/pid/fd needs a special permission handler so that a process can still
  274. * access /proc/self/fd after it has executed a setuid().
  275. */
  276. int proc_fd_permission(struct mnt_idmap *idmap,
  277. struct inode *inode, int mask)
  278. {
  279. struct task_struct *p;
  280. int rv;
  281. rv = generic_permission(&nop_mnt_idmap, inode, mask);
  282. if (rv == 0)
  283. return rv;
  284. rcu_read_lock();
  285. p = pid_task(proc_pid(inode), PIDTYPE_PID);
  286. if (p && same_thread_group(p, current))
  287. rv = 0;
  288. rcu_read_unlock();
  289. return rv;
  290. }
  291. static int proc_fd_getattr(struct mnt_idmap *idmap,
  292. const struct path *path, struct kstat *stat,
  293. u32 request_mask, unsigned int query_flags)
  294. {
  295. struct inode *inode = d_inode(path->dentry);
  296. int rv = 0;
  297. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  298. /* If it's a directory, put the number of open fds there */
  299. if (S_ISDIR(inode->i_mode)) {
  300. rv = proc_readfd_count(inode, &stat->size);
  301. if (rv < 0)
  302. return rv;
  303. }
  304. return rv;
  305. }
  306. const struct inode_operations proc_fd_inode_operations = {
  307. .lookup = proc_lookupfd,
  308. .permission = proc_fd_permission,
  309. .getattr = proc_fd_getattr,
  310. .setattr = proc_setattr,
  311. };
  312. static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
  313. struct task_struct *task, const void *ptr)
  314. {
  315. const struct fd_data *data = ptr;
  316. struct proc_inode *ei;
  317. struct inode *inode;
  318. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
  319. if (!inode)
  320. return ERR_PTR(-ENOENT);
  321. ei = PROC_I(inode);
  322. ei->fd = data->fd;
  323. inode->i_op = &proc_fdinfo_file_inode_operations;
  324. inode->i_fop = &proc_fdinfo_file_operations;
  325. tid_fd_update_inode(task, inode, 0);
  326. return proc_splice_unmountable(inode, dentry,
  327. &tid_fd_dentry_operations);
  328. }
  329. static struct dentry *
  330. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  331. {
  332. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  333. }
  334. static int proc_fdinfo_iterate(struct file *file, struct dir_context *ctx)
  335. {
  336. return proc_readfd_common(file, ctx,
  337. proc_fdinfo_instantiate);
  338. }
  339. const struct inode_operations proc_fdinfo_inode_operations = {
  340. .lookup = proc_lookupfdinfo,
  341. .permission = proc_fdinfo_permission,
  342. .setattr = proc_setattr,
  343. };
  344. const struct file_operations proc_fdinfo_operations = {
  345. .read = generic_read_dir,
  346. .iterate_shared = proc_fdinfo_iterate,
  347. .llseek = generic_file_llseek,
  348. };