fd.c 7.9 KB

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