fd.c 7.5 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 "../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. d_drop(dentry);
  111. return 0;
  112. }
  113. static const struct dentry_operations tid_fd_dentry_operations = {
  114. .d_revalidate = tid_fd_revalidate,
  115. .d_delete = pid_delete_dentry,
  116. };
  117. static int proc_fd_link(struct dentry *dentry, struct path *path)
  118. {
  119. struct files_struct *files = NULL;
  120. struct task_struct *task;
  121. int ret = -ENOENT;
  122. task = get_proc_task(dentry->d_inode);
  123. if (task) {
  124. files = get_files_struct(task);
  125. put_task_struct(task);
  126. }
  127. if (files) {
  128. int fd = proc_fd(dentry->d_inode);
  129. struct file *fd_file;
  130. spin_lock(&files->file_lock);
  131. fd_file = fcheck_files(files, fd);
  132. if (fd_file) {
  133. *path = fd_file->f_path;
  134. path_get(&fd_file->f_path);
  135. ret = 0;
  136. }
  137. spin_unlock(&files->file_lock);
  138. put_files_struct(files);
  139. }
  140. return ret;
  141. }
  142. static int
  143. proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
  144. struct task_struct *task, const void *ptr)
  145. {
  146. unsigned fd = (unsigned long)ptr;
  147. struct proc_inode *ei;
  148. struct inode *inode;
  149. inode = proc_pid_make_inode(dir->i_sb, task);
  150. if (!inode)
  151. goto out;
  152. ei = PROC_I(inode);
  153. ei->fd = fd;
  154. inode->i_mode = S_IFLNK;
  155. inode->i_op = &proc_pid_link_inode_operations;
  156. inode->i_size = 64;
  157. ei->op.proc_get_link = proc_fd_link;
  158. d_set_d_op(dentry, &tid_fd_dentry_operations);
  159. d_add(dentry, inode);
  160. /* Close the race of the process dying before we return the dentry */
  161. if (tid_fd_revalidate(dentry, 0))
  162. return 0;
  163. out:
  164. return -ENOENT;
  165. }
  166. static struct dentry *proc_lookupfd_common(struct inode *dir,
  167. struct dentry *dentry,
  168. instantiate_t instantiate)
  169. {
  170. struct task_struct *task = get_proc_task(dir);
  171. int result = -ENOENT;
  172. unsigned fd = name_to_int(dentry);
  173. if (!task)
  174. goto out_no_task;
  175. if (fd == ~0U)
  176. goto out;
  177. result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
  178. out:
  179. put_task_struct(task);
  180. out_no_task:
  181. return ERR_PTR(result);
  182. }
  183. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  184. instantiate_t instantiate)
  185. {
  186. struct task_struct *p = get_proc_task(file_inode(file));
  187. struct files_struct *files;
  188. unsigned int fd;
  189. if (!p)
  190. return -ENOENT;
  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_tgid(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. };