pidfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. struct task_struct *task;
  92. __poll_t poll_flags = 0;
  93. poll_wait(file, &pid->wait_pidfd, pts);
  94. /*
  95. * Don't wake waiters if the thread-group leader exited
  96. * prematurely. They either get notified when the last subthread
  97. * exits or not at all if one of the remaining subthreads execs
  98. * and assumes the struct pid of the old thread-group leader.
  99. */
  100. guard(rcu)();
  101. task = pid_task(pid, PIDTYPE_PID);
  102. if (!task)
  103. poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
  104. else if (task->exit_state && !delay_group_leader(task))
  105. poll_flags = EPOLLIN | EPOLLRDNORM;
  106. return poll_flags;
  107. }
  108. static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  109. {
  110. struct task_struct *task __free(put_task) = NULL;
  111. struct nsproxy *nsp __free(put_nsproxy) = NULL;
  112. struct pid *pid = pidfd_pid(file);
  113. struct ns_common *ns_common = NULL;
  114. struct pid_namespace *pid_ns;
  115. if (arg)
  116. return -EINVAL;
  117. task = get_pid_task(pid, PIDTYPE_PID);
  118. if (!task)
  119. return -ESRCH;
  120. scoped_guard(task_lock, task) {
  121. nsp = task->nsproxy;
  122. if (nsp)
  123. get_nsproxy(nsp);
  124. }
  125. if (!nsp)
  126. return -ESRCH; /* just pretend it didn't exist */
  127. /*
  128. * We're trying to open a file descriptor to the namespace so perform a
  129. * filesystem cred ptrace check. Also, we mirror nsfs behavior.
  130. */
  131. if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
  132. return -EACCES;
  133. switch (cmd) {
  134. /* Namespaces that hang of nsproxy. */
  135. case PIDFD_GET_CGROUP_NAMESPACE:
  136. if (IS_ENABLED(CONFIG_CGROUPS)) {
  137. get_cgroup_ns(nsp->cgroup_ns);
  138. ns_common = to_ns_common(nsp->cgroup_ns);
  139. }
  140. break;
  141. case PIDFD_GET_IPC_NAMESPACE:
  142. if (IS_ENABLED(CONFIG_IPC_NS)) {
  143. get_ipc_ns(nsp->ipc_ns);
  144. ns_common = to_ns_common(nsp->ipc_ns);
  145. }
  146. break;
  147. case PIDFD_GET_MNT_NAMESPACE:
  148. get_mnt_ns(nsp->mnt_ns);
  149. ns_common = to_ns_common(nsp->mnt_ns);
  150. break;
  151. case PIDFD_GET_NET_NAMESPACE:
  152. if (IS_ENABLED(CONFIG_NET_NS)) {
  153. ns_common = to_ns_common(nsp->net_ns);
  154. get_net_ns(ns_common);
  155. }
  156. break;
  157. case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
  158. if (IS_ENABLED(CONFIG_PID_NS)) {
  159. get_pid_ns(nsp->pid_ns_for_children);
  160. ns_common = to_ns_common(nsp->pid_ns_for_children);
  161. }
  162. break;
  163. case PIDFD_GET_TIME_NAMESPACE:
  164. if (IS_ENABLED(CONFIG_TIME_NS)) {
  165. get_time_ns(nsp->time_ns);
  166. ns_common = to_ns_common(nsp->time_ns);
  167. }
  168. break;
  169. case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
  170. if (IS_ENABLED(CONFIG_TIME_NS)) {
  171. get_time_ns(nsp->time_ns_for_children);
  172. ns_common = to_ns_common(nsp->time_ns_for_children);
  173. }
  174. break;
  175. case PIDFD_GET_UTS_NAMESPACE:
  176. if (IS_ENABLED(CONFIG_UTS_NS)) {
  177. get_uts_ns(nsp->uts_ns);
  178. ns_common = to_ns_common(nsp->uts_ns);
  179. }
  180. break;
  181. /* Namespaces that don't hang of nsproxy. */
  182. case PIDFD_GET_USER_NAMESPACE:
  183. if (IS_ENABLED(CONFIG_USER_NS)) {
  184. rcu_read_lock();
  185. ns_common = to_ns_common(get_user_ns(task_cred_xxx(task, user_ns)));
  186. rcu_read_unlock();
  187. }
  188. break;
  189. case PIDFD_GET_PID_NAMESPACE:
  190. if (IS_ENABLED(CONFIG_PID_NS)) {
  191. rcu_read_lock();
  192. pid_ns = task_active_pid_ns(task);
  193. if (pid_ns)
  194. ns_common = to_ns_common(get_pid_ns(pid_ns));
  195. rcu_read_unlock();
  196. }
  197. break;
  198. default:
  199. return -ENOIOCTLCMD;
  200. }
  201. if (!ns_common)
  202. return -EOPNOTSUPP;
  203. /* open_namespace() unconditionally consumes the reference */
  204. return open_namespace(ns_common);
  205. }
  206. static const struct file_operations pidfs_file_operations = {
  207. .poll = pidfd_poll,
  208. #ifdef CONFIG_PROC_FS
  209. .show_fdinfo = pidfd_show_fdinfo,
  210. #endif
  211. .unlocked_ioctl = pidfd_ioctl,
  212. .compat_ioctl = compat_ptr_ioctl,
  213. };
  214. struct pid *pidfd_pid(const struct file *file)
  215. {
  216. if (file->f_op != &pidfs_file_operations)
  217. return ERR_PTR(-EBADF);
  218. return file_inode(file)->i_private;
  219. }
  220. static struct vfsmount *pidfs_mnt __ro_after_init;
  221. #if BITS_PER_LONG == 32
  222. /*
  223. * Provide a fallback mechanism for 32-bit systems so processes remain
  224. * reliably comparable by inode number even on those systems.
  225. */
  226. static DEFINE_IDA(pidfd_inum_ida);
  227. static int pidfs_inum(struct pid *pid, unsigned long *ino)
  228. {
  229. int ret;
  230. ret = ida_alloc_range(&pidfd_inum_ida, RESERVED_PIDS + 1,
  231. UINT_MAX, GFP_ATOMIC);
  232. if (ret < 0)
  233. return -ENOSPC;
  234. *ino = ret;
  235. return 0;
  236. }
  237. static inline void pidfs_free_inum(unsigned long ino)
  238. {
  239. if (ino > 0)
  240. ida_free(&pidfd_inum_ida, ino);
  241. }
  242. #else
  243. static inline int pidfs_inum(struct pid *pid, unsigned long *ino)
  244. {
  245. *ino = pid->ino;
  246. return 0;
  247. }
  248. #define pidfs_free_inum(ino) ((void)(ino))
  249. #endif
  250. /*
  251. * The vfs falls back to simple_setattr() if i_op->setattr() isn't
  252. * implemented. Let's reject it completely until we have a clean
  253. * permission concept for pidfds.
  254. */
  255. static int pidfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  256. struct iattr *attr)
  257. {
  258. return -EOPNOTSUPP;
  259. }
  260. /*
  261. * User space expects pidfs inodes to have no file type in st_mode.
  262. *
  263. * In particular, 'lsof' has this legacy logic:
  264. *
  265. * type = s->st_mode & S_IFMT;
  266. * switch (type) {
  267. * ...
  268. * case 0:
  269. * if (!strcmp(p, "anon_inode"))
  270. * Lf->ntype = Ntype = N_ANON_INODE;
  271. *
  272. * to detect our old anon_inode logic.
  273. *
  274. * Rather than mess with our internal sane inode data, just fix it
  275. * up here in getattr() by masking off the format bits.
  276. */
  277. static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,
  278. struct kstat *stat, u32 request_mask,
  279. unsigned int query_flags)
  280. {
  281. struct inode *inode = d_inode(path->dentry);
  282. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  283. stat->mode &= ~S_IFMT;
  284. return 0;
  285. }
  286. static const struct inode_operations pidfs_inode_operations = {
  287. .getattr = pidfs_getattr,
  288. .setattr = pidfs_setattr,
  289. };
  290. static void pidfs_evict_inode(struct inode *inode)
  291. {
  292. struct pid *pid = inode->i_private;
  293. clear_inode(inode);
  294. put_pid(pid);
  295. pidfs_free_inum(inode->i_ino);
  296. }
  297. static const struct super_operations pidfs_sops = {
  298. .drop_inode = generic_delete_inode,
  299. .evict_inode = pidfs_evict_inode,
  300. .statfs = simple_statfs,
  301. };
  302. /*
  303. * 'lsof' has knowledge of out historical anon_inode use, and expects
  304. * the pidfs dentry name to start with 'anon_inode'.
  305. */
  306. static char *pidfs_dname(struct dentry *dentry, char *buffer, int buflen)
  307. {
  308. return dynamic_dname(buffer, buflen, "anon_inode:[pidfd]");
  309. }
  310. static const struct dentry_operations pidfs_dentry_operations = {
  311. .d_delete = always_delete_dentry,
  312. .d_dname = pidfs_dname,
  313. .d_prune = stashed_dentry_prune,
  314. };
  315. static int pidfs_init_inode(struct inode *inode, void *data)
  316. {
  317. inode->i_private = data;
  318. inode->i_flags |= S_PRIVATE;
  319. inode->i_mode |= S_IRWXU;
  320. inode->i_op = &pidfs_inode_operations;
  321. inode->i_fop = &pidfs_file_operations;
  322. /*
  323. * Inode numbering for pidfs start at RESERVED_PIDS + 1. This
  324. * avoids collisions with the root inode which is 1 for pseudo
  325. * filesystems.
  326. */
  327. return pidfs_inum(data, &inode->i_ino);
  328. }
  329. static void pidfs_put_data(void *data)
  330. {
  331. struct pid *pid = data;
  332. put_pid(pid);
  333. }
  334. static const struct stashed_operations pidfs_stashed_ops = {
  335. .init_inode = pidfs_init_inode,
  336. .put_data = pidfs_put_data,
  337. };
  338. static int pidfs_init_fs_context(struct fs_context *fc)
  339. {
  340. struct pseudo_fs_context *ctx;
  341. ctx = init_pseudo(fc, PID_FS_MAGIC);
  342. if (!ctx)
  343. return -ENOMEM;
  344. fc->s_iflags |= SB_I_NOEXEC;
  345. fc->s_iflags |= SB_I_NODEV;
  346. ctx->ops = &pidfs_sops;
  347. ctx->dops = &pidfs_dentry_operations;
  348. fc->s_fs_info = (void *)&pidfs_stashed_ops;
  349. return 0;
  350. }
  351. static struct file_system_type pidfs_type = {
  352. .name = "pidfs",
  353. .init_fs_context = pidfs_init_fs_context,
  354. .kill_sb = kill_anon_super,
  355. };
  356. struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
  357. {
  358. struct file *pidfd_file;
  359. struct path path;
  360. int ret;
  361. ret = path_from_stashed(&pid->stashed, pidfs_mnt, get_pid(pid), &path);
  362. if (ret < 0)
  363. return ERR_PTR(ret);
  364. pidfd_file = dentry_open(&path, flags, current_cred());
  365. path_put(&path);
  366. return pidfd_file;
  367. }
  368. void __init pidfs_init(void)
  369. {
  370. pidfs_mnt = kern_mount(&pidfs_type);
  371. if (IS_ERR(pidfs_mnt))
  372. panic("Failed to mount pidfs pseudo filesystem");
  373. }