pidfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/anon_inodes.h>
  3. #include <linux/file.h>
  4. #include <linux/fs.h>
  5. #include <linux/magic.h>
  6. #include <linux/mount.h>
  7. #include <linux/pid.h>
  8. #include <linux/pidfs.h>
  9. #include <linux/pid_namespace.h>
  10. #include <linux/poll.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/proc_ns.h>
  13. #include <linux/pseudo_fs.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/seq_file.h>
  16. #include <uapi/linux/pidfd.h>
  17. #include <linux/ipc_namespace.h>
  18. #include <linux/time_namespace.h>
  19. #include <linux/utsname.h>
  20. #include <net/net_namespace.h>
  21. #include "internal.h"
  22. #include "mount.h"
  23. #ifdef CONFIG_PROC_FS
  24. /**
  25. * pidfd_show_fdinfo - print information about a pidfd
  26. * @m: proc fdinfo file
  27. * @f: file referencing a pidfd
  28. *
  29. * Pid:
  30. * This function will print the pid that a given pidfd refers to in the
  31. * pid namespace of the procfs instance.
  32. * If the pid namespace of the process is not a descendant of the pid
  33. * namespace of the procfs instance 0 will be shown as its pid. This is
  34. * similar to calling getppid() on a process whose parent is outside of
  35. * its pid namespace.
  36. *
  37. * NSpid:
  38. * If pid namespaces are supported then this function will also print
  39. * the pid of a given pidfd refers to for all descendant pid namespaces
  40. * starting from the current pid namespace of the instance, i.e. the
  41. * Pid field and the first entry in the NSpid field will be identical.
  42. * If the pid namespace of the process is not a descendant of the pid
  43. * namespace of the procfs instance 0 will be shown as its first NSpid
  44. * entry and no others will be shown.
  45. * Note that this differs from the Pid and NSpid fields in
  46. * /proc/<pid>/status where Pid and NSpid are always shown relative to
  47. * the pid namespace of the procfs instance. The difference becomes
  48. * obvious when sending around a pidfd between pid namespaces from a
  49. * different branch of the tree, i.e. where no ancestral relation is
  50. * present between the pid namespaces:
  51. * - create two new pid namespaces ns1 and ns2 in the initial pid
  52. * namespace (also take care to create new mount namespaces in the
  53. * new pid namespace and mount procfs)
  54. * - create a process with a pidfd in ns1
  55. * - send pidfd from ns1 to ns2
  56. * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid
  57. * have exactly one entry, which is 0
  58. */
  59. static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
  60. {
  61. struct pid *pid = pidfd_pid(f);
  62. struct pid_namespace *ns;
  63. pid_t nr = -1;
  64. if (likely(pid_has_task(pid, PIDTYPE_PID))) {
  65. ns = proc_pid_ns(file_inode(m->file)->i_sb);
  66. nr = pid_nr_ns(pid, ns);
  67. }
  68. seq_put_decimal_ll(m, "Pid:\t", nr);
  69. #ifdef CONFIG_PID_NS
  70. seq_put_decimal_ll(m, "\nNSpid:\t", nr);
  71. if (nr > 0) {
  72. int i;
  73. /* If nr is non-zero it means that 'pid' is valid and that
  74. * ns, i.e. the pid namespace associated with the procfs
  75. * instance, is in the pid namespace hierarchy of pid.
  76. * Start at one below the already printed level.
  77. */
  78. for (i = ns->level + 1; i <= pid->level; i++)
  79. seq_put_decimal_ll(m, "\t", pid->numbers[i].nr);
  80. }
  81. #endif
  82. seq_putc(m, '\n');
  83. }
  84. #endif
  85. /*
  86. * Poll support for process exit notification.
  87. */
  88. static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
  89. {
  90. struct pid *pid = pidfd_pid(file);
  91. bool thread = file->f_flags & PIDFD_THREAD;
  92. struct task_struct *task;
  93. __poll_t poll_flags = 0;
  94. poll_wait(file, &pid->wait_pidfd, pts);
  95. /*
  96. * Depending on PIDFD_THREAD, inform pollers when the thread
  97. * or the whole thread-group exits.
  98. */
  99. guard(rcu)();
  100. task = pid_task(pid, PIDTYPE_PID);
  101. if (!task)
  102. poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
  103. else if (task->exit_state && (thread || thread_group_empty(task)))
  104. poll_flags = EPOLLIN | EPOLLRDNORM;
  105. return poll_flags;
  106. }
  107. static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  108. {
  109. struct task_struct *task __free(put_task) = NULL;
  110. struct nsproxy *nsp __free(put_nsproxy) = NULL;
  111. struct pid *pid = pidfd_pid(file);
  112. struct ns_common *ns_common = NULL;
  113. struct pid_namespace *pid_ns;
  114. if (arg)
  115. return -EINVAL;
  116. task = get_pid_task(pid, PIDTYPE_PID);
  117. if (!task)
  118. return -ESRCH;
  119. scoped_guard(task_lock, task) {
  120. nsp = task->nsproxy;
  121. if (nsp)
  122. get_nsproxy(nsp);
  123. }
  124. if (!nsp)
  125. return -ESRCH; /* just pretend it didn't exist */
  126. /*
  127. * We're trying to open a file descriptor to the namespace so perform a
  128. * filesystem cred ptrace check. Also, we mirror nsfs behavior.
  129. */
  130. if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
  131. return -EACCES;
  132. switch (cmd) {
  133. /* Namespaces that hang of nsproxy. */
  134. case PIDFD_GET_CGROUP_NAMESPACE:
  135. if (IS_ENABLED(CONFIG_CGROUPS)) {
  136. get_cgroup_ns(nsp->cgroup_ns);
  137. ns_common = to_ns_common(nsp->cgroup_ns);
  138. }
  139. break;
  140. case PIDFD_GET_IPC_NAMESPACE:
  141. if (IS_ENABLED(CONFIG_IPC_NS)) {
  142. get_ipc_ns(nsp->ipc_ns);
  143. ns_common = to_ns_common(nsp->ipc_ns);
  144. }
  145. break;
  146. case PIDFD_GET_MNT_NAMESPACE:
  147. get_mnt_ns(nsp->mnt_ns);
  148. ns_common = to_ns_common(nsp->mnt_ns);
  149. break;
  150. case PIDFD_GET_NET_NAMESPACE:
  151. if (IS_ENABLED(CONFIG_NET_NS)) {
  152. ns_common = to_ns_common(nsp->net_ns);
  153. get_net_ns(ns_common);
  154. }
  155. break;
  156. case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
  157. if (IS_ENABLED(CONFIG_PID_NS)) {
  158. get_pid_ns(nsp->pid_ns_for_children);
  159. ns_common = to_ns_common(nsp->pid_ns_for_children);
  160. }
  161. break;
  162. case PIDFD_GET_TIME_NAMESPACE:
  163. if (IS_ENABLED(CONFIG_TIME_NS)) {
  164. get_time_ns(nsp->time_ns);
  165. ns_common = to_ns_common(nsp->time_ns);
  166. }
  167. break;
  168. case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
  169. if (IS_ENABLED(CONFIG_TIME_NS)) {
  170. get_time_ns(nsp->time_ns_for_children);
  171. ns_common = to_ns_common(nsp->time_ns_for_children);
  172. }
  173. break;
  174. case PIDFD_GET_UTS_NAMESPACE:
  175. if (IS_ENABLED(CONFIG_UTS_NS)) {
  176. get_uts_ns(nsp->uts_ns);
  177. ns_common = to_ns_common(nsp->uts_ns);
  178. }
  179. break;
  180. /* Namespaces that don't hang of nsproxy. */
  181. case PIDFD_GET_USER_NAMESPACE:
  182. if (IS_ENABLED(CONFIG_USER_NS)) {
  183. rcu_read_lock();
  184. ns_common = to_ns_common(get_user_ns(task_cred_xxx(task, user_ns)));
  185. rcu_read_unlock();
  186. }
  187. break;
  188. case PIDFD_GET_PID_NAMESPACE:
  189. if (IS_ENABLED(CONFIG_PID_NS)) {
  190. rcu_read_lock();
  191. pid_ns = task_active_pid_ns(task);
  192. if (pid_ns)
  193. ns_common = to_ns_common(get_pid_ns(pid_ns));
  194. rcu_read_unlock();
  195. }
  196. break;
  197. default:
  198. return -ENOIOCTLCMD;
  199. }
  200. if (!ns_common)
  201. return -EOPNOTSUPP;
  202. /* open_namespace() unconditionally consumes the reference */
  203. return open_namespace(ns_common);
  204. }
  205. static const struct file_operations pidfs_file_operations = {
  206. .poll = pidfd_poll,
  207. #ifdef CONFIG_PROC_FS
  208. .show_fdinfo = pidfd_show_fdinfo,
  209. #endif
  210. .unlocked_ioctl = pidfd_ioctl,
  211. .compat_ioctl = compat_ptr_ioctl,
  212. };
  213. struct pid *pidfd_pid(const struct file *file)
  214. {
  215. if (file->f_op != &pidfs_file_operations)
  216. return ERR_PTR(-EBADF);
  217. return file_inode(file)->i_private;
  218. }
  219. static struct vfsmount *pidfs_mnt __ro_after_init;
  220. #if BITS_PER_LONG == 32
  221. /*
  222. * Provide a fallback mechanism for 32-bit systems so processes remain
  223. * reliably comparable by inode number even on those systems.
  224. */
  225. static DEFINE_IDA(pidfd_inum_ida);
  226. static int pidfs_inum(struct pid *pid, unsigned long *ino)
  227. {
  228. int ret;
  229. ret = ida_alloc_range(&pidfd_inum_ida, RESERVED_PIDS + 1,
  230. UINT_MAX, GFP_ATOMIC);
  231. if (ret < 0)
  232. return -ENOSPC;
  233. *ino = ret;
  234. return 0;
  235. }
  236. static inline void pidfs_free_inum(unsigned long ino)
  237. {
  238. if (ino > 0)
  239. ida_free(&pidfd_inum_ida, ino);
  240. }
  241. #else
  242. static inline int pidfs_inum(struct pid *pid, unsigned long *ino)
  243. {
  244. *ino = pid->ino;
  245. return 0;
  246. }
  247. #define pidfs_free_inum(ino) ((void)(ino))
  248. #endif
  249. /*
  250. * The vfs falls back to simple_setattr() if i_op->setattr() isn't
  251. * implemented. Let's reject it completely until we have a clean
  252. * permission concept for pidfds.
  253. */
  254. static int pidfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  255. struct iattr *attr)
  256. {
  257. return -EOPNOTSUPP;
  258. }
  259. /*
  260. * User space expects pidfs inodes to have no file type in st_mode.
  261. *
  262. * In particular, 'lsof' has this legacy logic:
  263. *
  264. * type = s->st_mode & S_IFMT;
  265. * switch (type) {
  266. * ...
  267. * case 0:
  268. * if (!strcmp(p, "anon_inode"))
  269. * Lf->ntype = Ntype = N_ANON_INODE;
  270. *
  271. * to detect our old anon_inode logic.
  272. *
  273. * Rather than mess with our internal sane inode data, just fix it
  274. * up here in getattr() by masking off the format bits.
  275. */
  276. static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,
  277. struct kstat *stat, u32 request_mask,
  278. unsigned int query_flags)
  279. {
  280. struct inode *inode = d_inode(path->dentry);
  281. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  282. stat->mode &= ~S_IFMT;
  283. return 0;
  284. }
  285. static const struct inode_operations pidfs_inode_operations = {
  286. .getattr = pidfs_getattr,
  287. .setattr = pidfs_setattr,
  288. };
  289. static void pidfs_evict_inode(struct inode *inode)
  290. {
  291. struct pid *pid = inode->i_private;
  292. clear_inode(inode);
  293. put_pid(pid);
  294. pidfs_free_inum(inode->i_ino);
  295. }
  296. static const struct super_operations pidfs_sops = {
  297. .drop_inode = generic_delete_inode,
  298. .evict_inode = pidfs_evict_inode,
  299. .statfs = simple_statfs,
  300. };
  301. /*
  302. * 'lsof' has knowledge of out historical anon_inode use, and expects
  303. * the pidfs dentry name to start with 'anon_inode'.
  304. */
  305. static char *pidfs_dname(struct dentry *dentry, char *buffer, int buflen)
  306. {
  307. return dynamic_dname(buffer, buflen, "anon_inode:[pidfd]");
  308. }
  309. static const struct dentry_operations pidfs_dentry_operations = {
  310. .d_delete = always_delete_dentry,
  311. .d_dname = pidfs_dname,
  312. .d_prune = stashed_dentry_prune,
  313. };
  314. static int pidfs_init_inode(struct inode *inode, void *data)
  315. {
  316. inode->i_private = data;
  317. inode->i_flags |= S_PRIVATE;
  318. inode->i_mode |= S_IRWXU;
  319. inode->i_op = &pidfs_inode_operations;
  320. inode->i_fop = &pidfs_file_operations;
  321. /*
  322. * Inode numbering for pidfs start at RESERVED_PIDS + 1. This
  323. * avoids collisions with the root inode which is 1 for pseudo
  324. * filesystems.
  325. */
  326. return pidfs_inum(data, &inode->i_ino);
  327. }
  328. static void pidfs_put_data(void *data)
  329. {
  330. struct pid *pid = data;
  331. put_pid(pid);
  332. }
  333. static const struct stashed_operations pidfs_stashed_ops = {
  334. .init_inode = pidfs_init_inode,
  335. .put_data = pidfs_put_data,
  336. };
  337. static int pidfs_init_fs_context(struct fs_context *fc)
  338. {
  339. struct pseudo_fs_context *ctx;
  340. ctx = init_pseudo(fc, PID_FS_MAGIC);
  341. if (!ctx)
  342. return -ENOMEM;
  343. ctx->ops = &pidfs_sops;
  344. ctx->dops = &pidfs_dentry_operations;
  345. fc->s_fs_info = (void *)&pidfs_stashed_ops;
  346. return 0;
  347. }
  348. static struct file_system_type pidfs_type = {
  349. .name = "pidfs",
  350. .init_fs_context = pidfs_init_fs_context,
  351. .kill_sb = kill_anon_super,
  352. };
  353. struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
  354. {
  355. struct file *pidfd_file;
  356. struct path path;
  357. int ret;
  358. ret = path_from_stashed(&pid->stashed, pidfs_mnt, get_pid(pid), &path);
  359. if (ret < 0)
  360. return ERR_PTR(ret);
  361. pidfd_file = dentry_open(&path, flags, current_cred());
  362. path_put(&path);
  363. return pidfd_file;
  364. }
  365. void __init pidfs_init(void)
  366. {
  367. pidfs_mnt = kern_mount(&pidfs_type);
  368. if (IS_ERR(pidfs_mnt))
  369. panic("Failed to mount pidfs pseudo filesystem");
  370. }