fd.c 7.5 KB

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