fd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 struct dentry *
  141. proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
  142. struct task_struct *task, const void *ptr)
  143. {
  144. struct dentry *error = ERR_PTR(-ENOENT);
  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. error = NULL;
  162. out:
  163. return error;
  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. struct dentry *result = ERR_PTR(-ENOENT);
  171. unsigned fd = name_to_int(dentry);
  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 result;
  181. }
  182. static int proc_readfd_common(struct file * filp, void * dirent,
  183. filldir_t filldir, instantiate_t instantiate)
  184. {
  185. struct dentry *dentry = filp->f_path.dentry;
  186. struct inode *inode = dentry->d_inode;
  187. struct task_struct *p = get_proc_task(inode);
  188. struct files_struct *files;
  189. unsigned int fd, ino;
  190. int retval;
  191. retval = -ENOENT;
  192. if (!p)
  193. goto out_no_task;
  194. retval = 0;
  195. fd = filp->f_pos;
  196. switch (fd) {
  197. case 0:
  198. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  199. goto out;
  200. filp->f_pos++;
  201. case 1:
  202. ino = parent_ino(dentry);
  203. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  204. goto out;
  205. filp->f_pos++;
  206. default:
  207. files = get_files_struct(p);
  208. if (!files)
  209. goto out;
  210. rcu_read_lock();
  211. for (fd = filp->f_pos - 2;
  212. fd < files_fdtable(files)->max_fds;
  213. fd++, filp->f_pos++) {
  214. char name[PROC_NUMBUF];
  215. int len;
  216. int rv;
  217. if (!fcheck_files(files, fd))
  218. continue;
  219. rcu_read_unlock();
  220. len = snprintf(name, sizeof(name), "%d", fd);
  221. rv = proc_fill_cache(filp, dirent, filldir,
  222. name, len, instantiate, p,
  223. (void *)(unsigned long)fd);
  224. if (rv < 0)
  225. goto out_fd_loop;
  226. rcu_read_lock();
  227. }
  228. rcu_read_unlock();
  229. out_fd_loop:
  230. put_files_struct(files);
  231. }
  232. out:
  233. put_task_struct(p);
  234. out_no_task:
  235. return retval;
  236. }
  237. static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
  238. {
  239. return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
  240. }
  241. const struct file_operations proc_fd_operations = {
  242. .read = generic_read_dir,
  243. .readdir = proc_readfd,
  244. .llseek = default_llseek,
  245. };
  246. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  247. unsigned int flags)
  248. {
  249. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  250. }
  251. /*
  252. * /proc/pid/fd needs a special permission handler so that a process can still
  253. * access /proc/self/fd after it has executed a setuid().
  254. */
  255. int proc_fd_permission(struct inode *inode, int mask)
  256. {
  257. int rv = generic_permission(inode, mask);
  258. if (rv == 0)
  259. return 0;
  260. if (task_pid(current) == proc_pid(inode))
  261. rv = 0;
  262. return rv;
  263. }
  264. const struct inode_operations proc_fd_inode_operations = {
  265. .lookup = proc_lookupfd,
  266. .permission = proc_fd_permission,
  267. .setattr = proc_setattr,
  268. };
  269. static struct dentry *
  270. proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
  271. struct task_struct *task, const void *ptr)
  272. {
  273. struct dentry *error = ERR_PTR(-ENOENT);
  274. unsigned fd = (unsigned long)ptr;
  275. struct proc_inode *ei;
  276. struct inode *inode;
  277. inode = proc_pid_make_inode(dir->i_sb, task);
  278. if (!inode)
  279. goto out;
  280. ei = PROC_I(inode);
  281. ei->fd = fd;
  282. inode->i_mode = S_IFREG | S_IRUSR;
  283. inode->i_fop = &proc_fdinfo_file_operations;
  284. d_set_d_op(dentry, &tid_fd_dentry_operations);
  285. d_add(dentry, inode);
  286. /* Close the race of the process dying before we return the dentry */
  287. if (tid_fd_revalidate(dentry, 0))
  288. error = NULL;
  289. out:
  290. return error;
  291. }
  292. static struct dentry *
  293. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  294. {
  295. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  296. }
  297. static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
  298. {
  299. return proc_readfd_common(filp, dirent, filldir,
  300. proc_fdinfo_instantiate);
  301. }
  302. const struct inode_operations proc_fdinfo_inode_operations = {
  303. .lookup = proc_lookupfdinfo,
  304. .setattr = proc_setattr,
  305. };
  306. const struct file_operations proc_fdinfo_operations = {
  307. .read = generic_read_dir,
  308. .readdir = proc_readfdinfo,
  309. .llseek = default_llseek,
  310. };