fd.c 7.4 KB

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