fd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/sched/signal.h>
  3. #include <linux/errno.h>
  4. #include <linux/dcache.h>
  5. #include <linux/path.h>
  6. #include <linux/fdtable.h>
  7. #include <linux/namei.h>
  8. #include <linux/pid.h>
  9. #include <linux/ptrace.h>
  10. #include <linux/security.h>
  11. #include <linux/file.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/fs.h>
  14. #include <linux/proc_fs.h>
  15. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  16. #include <linux/susfs_def.h>
  17. #endif
  18. #include "../mount.h"
  19. #include "internal.h"
  20. #include "fd.h"
  21. static int seq_show(struct seq_file *m, void *v)
  22. {
  23. struct files_struct *files = NULL;
  24. int f_flags = 0, ret = -ENOENT;
  25. struct file *file = NULL;
  26. struct task_struct *task;
  27. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  28. struct mount *mnt = NULL;
  29. #endif
  30. task = get_proc_task(m->private);
  31. if (!task)
  32. return -ENOENT;
  33. task_lock(task);
  34. files = task->files;
  35. if (files) {
  36. unsigned int fd = proc_fd(m->private);
  37. spin_lock(&files->file_lock);
  38. file = files_lookup_fd_locked(files, fd);
  39. if (file) {
  40. struct fdtable *fdt = files_fdtable(files);
  41. f_flags = file->f_flags;
  42. if (close_on_exec(fd, fdt))
  43. f_flags |= O_CLOEXEC;
  44. get_file(file);
  45. ret = 0;
  46. }
  47. spin_unlock(&files->file_lock);
  48. }
  49. task_unlock(task);
  50. put_task_struct(task);
  51. if (ret)
  52. return ret;
  53. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  54. mnt = real_mount(file->f_path.mnt);
  55. if (likely(current->susfs_task_state & TASK_STRUCT_NON_ROOT_USER_APP_PROC) &&
  56. mnt->mnt_id >= DEFAULT_SUS_MNT_ID) {
  57. struct path path;
  58. char *pathname = kmalloc(PAGE_SIZE, GFP_KERNEL);
  59. char *dpath;
  60. for (; mnt->mnt_id >= DEFAULT_SUS_MNT_ID; mnt = mnt->mnt_parent) { }
  61. if (!pathname) {
  62. goto out_seq_printf;
  63. }
  64. dpath = d_path(&file->f_path, pathname, PAGE_SIZE);
  65. if (!dpath) {
  66. goto out_free_pathname;
  67. }
  68. if (kern_path(dpath, 0, &path)) {
  69. goto out_free_pathname;
  70. }
  71. seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
  72. (long long)file->f_pos, f_flags,
  73. mnt->mnt_id,
  74. path.dentry->d_inode->i_ino);
  75. path_put(&path);
  76. kfree(pathname);
  77. goto bypass_orig_flow;
  78. out_free_pathname:
  79. kfree(pathname);
  80. }
  81. out_seq_printf:
  82. seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
  83. (long long)file->f_pos, f_flags,
  84. mnt->mnt_id,
  85. file_inode(file)->i_ino);
  86. bypass_orig_flow:
  87. #else
  88. seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
  89. (long long)file->f_pos, f_flags,
  90. real_mount(file->f_path.mnt)->mnt_id,
  91. file_inode(file)->i_ino);
  92. #endif
  93. /* show_fd_locks() never deferences files so a stale value is safe */
  94. show_fd_locks(m, file, files);
  95. if (seq_has_overflowed(m))
  96. goto out;
  97. if (file->f_op->show_fdinfo)
  98. file->f_op->show_fdinfo(m, file);
  99. out:
  100. fput(file);
  101. return 0;
  102. }
  103. static int proc_fdinfo_access_allowed(struct inode *inode)
  104. {
  105. bool allowed = false;
  106. struct task_struct *task = get_proc_task(inode);
  107. if (!task)
  108. return -ESRCH;
  109. allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
  110. put_task_struct(task);
  111. if (!allowed)
  112. return -EACCES;
  113. return 0;
  114. }
  115. static int seq_fdinfo_open(struct inode *inode, struct file *file)
  116. {
  117. int ret = proc_fdinfo_access_allowed(inode);
  118. if (ret)
  119. return ret;
  120. return single_open(file, seq_show, inode);
  121. }
  122. static const struct file_operations proc_fdinfo_file_operations = {
  123. .open = seq_fdinfo_open,
  124. .read = seq_read,
  125. .llseek = seq_lseek,
  126. .release = single_release,
  127. };
  128. static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
  129. {
  130. struct file *file;
  131. rcu_read_lock();
  132. file = task_lookup_fd_rcu(task, fd);
  133. if (file)
  134. *mode = file->f_mode;
  135. rcu_read_unlock();
  136. return !!file;
  137. }
  138. static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
  139. fmode_t f_mode)
  140. {
  141. task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
  142. if (S_ISLNK(inode->i_mode)) {
  143. unsigned i_mode = S_IFLNK;
  144. if (f_mode & FMODE_READ)
  145. i_mode |= S_IRUSR | S_IXUSR;
  146. if (f_mode & FMODE_WRITE)
  147. i_mode |= S_IWUSR | S_IXUSR;
  148. inode->i_mode = i_mode;
  149. }
  150. security_task_to_inode(task, inode);
  151. }
  152. static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
  153. {
  154. struct task_struct *task;
  155. struct inode *inode;
  156. unsigned int fd;
  157. if (flags & LOOKUP_RCU)
  158. return -ECHILD;
  159. inode = d_inode(dentry);
  160. task = get_proc_task(inode);
  161. fd = proc_fd(inode);
  162. if (task) {
  163. fmode_t f_mode;
  164. if (tid_fd_mode(task, fd, &f_mode)) {
  165. tid_fd_update_inode(task, inode, f_mode);
  166. put_task_struct(task);
  167. return 1;
  168. }
  169. put_task_struct(task);
  170. }
  171. return 0;
  172. }
  173. static const struct dentry_operations tid_fd_dentry_operations = {
  174. .d_revalidate = tid_fd_revalidate,
  175. .d_delete = pid_delete_dentry,
  176. };
  177. static int proc_fd_link(struct dentry *dentry, struct path *path)
  178. {
  179. struct task_struct *task;
  180. int ret = -ENOENT;
  181. task = get_proc_task(d_inode(dentry));
  182. if (task) {
  183. unsigned int fd = proc_fd(d_inode(dentry));
  184. struct file *fd_file;
  185. fd_file = fget_task(task, fd);
  186. if (fd_file) {
  187. *path = fd_file->f_path;
  188. path_get(&fd_file->f_path);
  189. ret = 0;
  190. fput(fd_file);
  191. }
  192. put_task_struct(task);
  193. }
  194. return ret;
  195. }
  196. struct fd_data {
  197. fmode_t mode;
  198. unsigned fd;
  199. };
  200. static struct dentry *proc_fd_instantiate(struct dentry *dentry,
  201. struct task_struct *task, const void *ptr)
  202. {
  203. const struct fd_data *data = ptr;
  204. struct proc_inode *ei;
  205. struct inode *inode;
  206. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
  207. if (!inode)
  208. return ERR_PTR(-ENOENT);
  209. ei = PROC_I(inode);
  210. ei->fd = data->fd;
  211. inode->i_op = &proc_pid_link_inode_operations;
  212. inode->i_size = 64;
  213. ei->op.proc_get_link = proc_fd_link;
  214. tid_fd_update_inode(task, inode, data->mode);
  215. d_set_d_op(dentry, &tid_fd_dentry_operations);
  216. return d_splice_alias(inode, dentry);
  217. }
  218. static struct dentry *proc_lookupfd_common(struct inode *dir,
  219. struct dentry *dentry,
  220. instantiate_t instantiate)
  221. {
  222. struct task_struct *task = get_proc_task(dir);
  223. struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
  224. struct dentry *result = ERR_PTR(-ENOENT);
  225. if (!task)
  226. goto out_no_task;
  227. if (data.fd == ~0U)
  228. goto out;
  229. if (!tid_fd_mode(task, data.fd, &data.mode))
  230. goto out;
  231. result = instantiate(dentry, task, &data);
  232. out:
  233. put_task_struct(task);
  234. out_no_task:
  235. return result;
  236. }
  237. static int proc_readfd_common(struct file *file, struct dir_context *ctx,
  238. instantiate_t instantiate)
  239. {
  240. struct task_struct *p = get_proc_task(file_inode(file));
  241. unsigned int fd;
  242. if (!p)
  243. return -ENOENT;
  244. if (!dir_emit_dots(file, ctx))
  245. goto out;
  246. rcu_read_lock();
  247. for (fd = ctx->pos - 2;; fd++) {
  248. struct file *f;
  249. struct fd_data data;
  250. char name[10 + 1];
  251. unsigned int len;
  252. f = task_lookup_next_fd_rcu(p, &fd);
  253. ctx->pos = fd + 2LL;
  254. if (!f)
  255. break;
  256. data.mode = f->f_mode;
  257. rcu_read_unlock();
  258. data.fd = fd;
  259. len = snprintf(name, sizeof(name), "%u", fd);
  260. if (!proc_fill_cache(file, ctx,
  261. name, len, instantiate, p,
  262. &data))
  263. goto out;
  264. cond_resched();
  265. rcu_read_lock();
  266. }
  267. rcu_read_unlock();
  268. out:
  269. put_task_struct(p);
  270. return 0;
  271. }
  272. static int proc_readfd(struct file *file, struct dir_context *ctx)
  273. {
  274. return proc_readfd_common(file, ctx, proc_fd_instantiate);
  275. }
  276. const struct file_operations proc_fd_operations = {
  277. .read = generic_read_dir,
  278. .iterate_shared = proc_readfd,
  279. .llseek = generic_file_llseek,
  280. };
  281. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  282. unsigned int flags)
  283. {
  284. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  285. }
  286. /*
  287. * /proc/pid/fd needs a special permission handler so that a process can still
  288. * access /proc/self/fd after it has executed a setuid().
  289. */
  290. int proc_fd_permission(struct user_namespace *mnt_userns,
  291. struct inode *inode, int mask)
  292. {
  293. struct task_struct *p;
  294. int rv;
  295. rv = generic_permission(&init_user_ns, inode, mask);
  296. if (rv == 0)
  297. return rv;
  298. rcu_read_lock();
  299. p = pid_task(proc_pid(inode), PIDTYPE_PID);
  300. if (p && same_thread_group(p, current))
  301. rv = 0;
  302. rcu_read_unlock();
  303. return rv;
  304. }
  305. const struct inode_operations proc_fd_inode_operations = {
  306. .lookup = proc_lookupfd,
  307. .permission = proc_fd_permission,
  308. .setattr = proc_setattr,
  309. };
  310. static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
  311. struct task_struct *task, const void *ptr)
  312. {
  313. const struct fd_data *data = ptr;
  314. struct proc_inode *ei;
  315. struct inode *inode;
  316. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
  317. if (!inode)
  318. return ERR_PTR(-ENOENT);
  319. ei = PROC_I(inode);
  320. ei->fd = data->fd;
  321. inode->i_fop = &proc_fdinfo_file_operations;
  322. tid_fd_update_inode(task, inode, 0);
  323. d_set_d_op(dentry, &tid_fd_dentry_operations);
  324. return d_splice_alias(inode, dentry);
  325. }
  326. static struct dentry *
  327. proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
  328. {
  329. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  330. }
  331. static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
  332. {
  333. return proc_readfd_common(file, ctx,
  334. proc_fdinfo_instantiate);
  335. }
  336. static int proc_open_fdinfo(struct inode *inode, struct file *file)
  337. {
  338. int ret = proc_fdinfo_access_allowed(inode);
  339. if (ret)
  340. return ret;
  341. return 0;
  342. }
  343. const struct inode_operations proc_fdinfo_inode_operations = {
  344. .lookup = proc_lookupfdinfo,
  345. .setattr = proc_setattr,
  346. };
  347. const struct file_operations proc_fdinfo_operations = {
  348. .open = proc_open_fdinfo,
  349. .read = generic_read_dir,
  350. .iterate_shared = proc_readfdinfo,
  351. .llseek = generic_file_llseek,
  352. };