fd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. 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. const struct cred *cred;
  71. struct inode *inode;
  72. int fd;
  73. if (flags & LOOKUP_RCU)
  74. return -ECHILD;
  75. inode = d_inode(dentry);
  76. task = get_proc_task(inode);
  77. fd = proc_fd(inode);
  78. if (task) {
  79. files = get_files_struct(task);
  80. if (files) {
  81. struct file *file;
  82. rcu_read_lock();
  83. file = fcheck_files(files, fd);
  84. if (file) {
  85. unsigned f_mode = file->f_mode;
  86. rcu_read_unlock();
  87. put_files_struct(files);
  88. if (task_dumpable(task)) {
  89. rcu_read_lock();
  90. cred = __task_cred(task);
  91. inode->i_uid = cred->euid;
  92. inode->i_gid = cred->egid;
  93. rcu_read_unlock();
  94. } else {
  95. inode->i_uid = GLOBAL_ROOT_UID;
  96. inode->i_gid = GLOBAL_ROOT_GID;
  97. }
  98. if (S_ISLNK(inode->i_mode)) {
  99. unsigned i_mode = S_IFLNK;
  100. if (f_mode & FMODE_READ)
  101. i_mode |= S_IRUSR | S_IXUSR;
  102. if (f_mode & FMODE_WRITE)
  103. i_mode |= S_IWUSR | S_IXUSR;
  104. inode->i_mode = i_mode;
  105. }
  106. security_task_to_inode(task, inode);
  107. put_task_struct(task);
  108. return 1;
  109. }
  110. rcu_read_unlock();
  111. put_files_struct(files);
  112. }
  113. put_task_struct(task);
  114. }
  115. return 0;
  116. }
  117. static const struct dentry_operations tid_fd_dentry_operations = {
  118. .d_revalidate = tid_fd_revalidate,
  119. .d_delete = pid_delete_dentry,
  120. };
  121. static int proc_fd_link(struct dentry *dentry, struct path *path)
  122. {
  123. struct files_struct *files = NULL;
  124. struct task_struct *task;
  125. int ret = -ENOENT;
  126. task = get_proc_task(d_inode(dentry));
  127. if (task) {
  128. files = get_files_struct(task);
  129. put_task_struct(task);
  130. }
  131. if (files) {
  132. int fd = proc_fd(d_inode(dentry));
  133. struct file *fd_file;
  134. spin_lock(&files->file_lock);
  135. fd_file = fcheck_files(files, fd);
  136. if (fd_file) {
  137. *path = fd_file->f_path;
  138. path_get(&fd_file->f_path);
  139. ret = 0;
  140. }
  141. spin_unlock(&files->file_lock);
  142. put_files_struct(files);
  143. }
  144. return ret;
  145. }
  146. static int
  147. proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
  148. struct task_struct *task, const void *ptr)
  149. {
  150. unsigned fd = (unsigned long)ptr;
  151. struct proc_inode *ei;
  152. struct inode *inode;
  153. inode = proc_pid_make_inode(dir->i_sb, task);
  154. if (!inode)
  155. goto out;
  156. ei = PROC_I(inode);
  157. ei->fd = fd;
  158. inode->i_mode = S_IFLNK;
  159. inode->i_op = &proc_pid_link_inode_operations;
  160. inode->i_size = 64;
  161. ei->op.proc_get_link = proc_fd_link;
  162. d_set_d_op(dentry, &tid_fd_dentry_operations);
  163. d_add(dentry, inode);
  164. /* Close the race of the process dying before we return the dentry */
  165. if (tid_fd_revalidate(dentry, 0))
  166. return 0;
  167. out:
  168. return -ENOENT;
  169. }
  170. static struct dentry *proc_lookupfd_common(struct inode *dir,
  171. struct dentry *dentry,
  172. instantiate_t instantiate)
  173. {
  174. struct task_struct *task = get_proc_task(dir);
  175. int result = -ENOENT;
  176. unsigned fd = name_to_int(&dentry->d_name);
  177. if (!task)
  178. goto out_no_task;
  179. if (fd == ~0U)
  180. goto out;
  181. result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
  182. out:
  183. put_task_struct(task);
  184. out_no_task:
  185. return ERR_PTR(result);
  186. }
  187. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  188. instantiate_t instantiate)
  189. {
  190. struct task_struct *p = get_proc_task(file_inode(file));
  191. struct files_struct *files;
  192. unsigned int fd;
  193. if (!p)
  194. return -ENOENT;
  195. if (!dir_emit_dots(file, ctx))
  196. goto out;
  197. files = get_files_struct(p);
  198. if (!files)
  199. goto out;
  200. rcu_read_lock();
  201. for (fd = ctx->pos - 2;
  202. fd < files_fdtable(files)->max_fds;
  203. fd++, ctx->pos++) {
  204. char name[PROC_NUMBUF];
  205. int len;
  206. if (!fcheck_files(files, fd))
  207. continue;
  208. rcu_read_unlock();
  209. len = snprintf(name, sizeof(name), "%d", fd);
  210. if (!proc_fill_cache(file, ctx,
  211. name, len, instantiate, p,
  212. (void *)(unsigned long)fd))
  213. goto out_fd_loop;
  214. rcu_read_lock();
  215. }
  216. rcu_read_unlock();
  217. out_fd_loop:
  218. put_files_struct(files);
  219. out:
  220. put_task_struct(p);
  221. return 0;
  222. }
  223. static int proc_readfd(struct file *file, struct dir_context *ctx)
  224. {
  225. return proc_readfd_common(file, ctx, proc_fd_instantiate);
  226. }
  227. const struct file_operations proc_fd_operations = {
  228. .read = generic_read_dir,
  229. .iterate = proc_readfd,
  230. .llseek = default_llseek,
  231. };
  232. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  233. unsigned int flags)
  234. {
  235. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  236. }
  237. /*
  238. * /proc/pid/fd needs a special permission handler so that a process can still
  239. * access /proc/self/fd after it has executed a setuid().
  240. */
  241. int proc_fd_permission(struct inode *inode, int mask)
  242. {
  243. struct task_struct *p;
  244. int rv;
  245. rv = generic_permission(inode, mask);
  246. if (rv == 0)
  247. return rv;
  248. rcu_read_lock();
  249. p = pid_task(proc_pid(inode), PIDTYPE_PID);
  250. if (p && same_thread_group(p, current))
  251. rv = 0;
  252. rcu_read_unlock();
  253. return rv;
  254. }
  255. const struct inode_operations proc_fd_inode_operations = {
  256. .lookup = proc_lookupfd,
  257. .permission = proc_fd_permission,
  258. .setattr = proc_setattr,
  259. };
  260. static int
  261. proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
  262. struct task_struct *task, const void *ptr)
  263. {
  264. unsigned fd = (unsigned long)ptr;
  265. struct proc_inode *ei;
  266. struct inode *inode;
  267. inode = proc_pid_make_inode(dir->i_sb, task);
  268. if (!inode)
  269. goto out;
  270. ei = PROC_I(inode);
  271. ei->fd = fd;
  272. inode->i_mode = S_IFREG | S_IRUSR;
  273. inode->i_fop = &proc_fdinfo_file_operations;
  274. d_set_d_op(dentry, &tid_fd_dentry_operations);
  275. d_add(dentry, inode);
  276. /* Close the race of the process dying before we return the dentry */
  277. if (tid_fd_revalidate(dentry, 0))
  278. return 0;
  279. out:
  280. return -ENOENT;
  281. }
  282. static struct dentry *
  283. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  284. {
  285. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  286. }
  287. static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
  288. {
  289. return proc_readfd_common(file, ctx,
  290. proc_fdinfo_instantiate);
  291. }
  292. const struct inode_operations proc_fdinfo_inode_operations = {
  293. .lookup = proc_lookupfdinfo,
  294. .setattr = proc_setattr,
  295. };
  296. const struct file_operations proc_fdinfo_operations = {
  297. .read = generic_read_dir,
  298. .iterate = proc_readfdinfo,
  299. .llseek = default_llseek,
  300. };