fd.c 7.4 KB

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