pid_namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Pid namespaces
  3. *
  4. * Authors:
  5. * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
  6. * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
  7. * Many thanks to Oleg Nesterov for comments and help
  8. *
  9. */
  10. #include <linux/pid.h>
  11. #include <linux/pid_namespace.h>
  12. #include <linux/user_namespace.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/cred.h>
  15. #include <linux/err.h>
  16. #include <linux/acct.h>
  17. #include <linux/slab.h>
  18. #include <linux/proc_ns.h>
  19. #include <linux/reboot.h>
  20. #include <linux/export.h>
  21. #include <linux/sched/task.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/idr.h>
  24. static DEFINE_MUTEX(pid_caches_mutex);
  25. static struct kmem_cache *pid_ns_cachep;
  26. /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
  27. #define MAX_PID_NS_LEVEL 32
  28. /* Write once array, filled from the beginning. */
  29. static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL];
  30. /*
  31. * creates the kmem cache to allocate pids from.
  32. * @level: pid namespace level
  33. */
  34. static struct kmem_cache *create_pid_cachep(unsigned int level)
  35. {
  36. /* Level 0 is init_pid_ns.pid_cachep */
  37. struct kmem_cache **pkc = &pid_cache[level - 1];
  38. struct kmem_cache *kc;
  39. char name[4 + 10 + 1];
  40. unsigned int len;
  41. kc = READ_ONCE(*pkc);
  42. if (kc)
  43. return kc;
  44. snprintf(name, sizeof(name), "pid_%u", level + 1);
  45. len = sizeof(struct pid) + level * sizeof(struct upid);
  46. mutex_lock(&pid_caches_mutex);
  47. /* Name collision forces to do allocation under mutex. */
  48. if (!*pkc)
  49. *pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
  50. mutex_unlock(&pid_caches_mutex);
  51. /* current can fail, but someone else can succeed. */
  52. return READ_ONCE(*pkc);
  53. }
  54. static void proc_cleanup_work(struct work_struct *work)
  55. {
  56. struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work);
  57. pid_ns_release_proc(ns);
  58. }
  59. static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
  60. {
  61. return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
  62. }
  63. static void dec_pid_namespaces(struct ucounts *ucounts)
  64. {
  65. dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
  66. }
  67. static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
  68. struct pid_namespace *parent_pid_ns)
  69. {
  70. struct pid_namespace *ns;
  71. unsigned int level = parent_pid_ns->level + 1;
  72. struct ucounts *ucounts;
  73. int err;
  74. err = -EINVAL;
  75. if (!in_userns(parent_pid_ns->user_ns, user_ns))
  76. goto out;
  77. err = -ENOSPC;
  78. if (level > MAX_PID_NS_LEVEL)
  79. goto out;
  80. ucounts = inc_pid_namespaces(user_ns);
  81. if (!ucounts)
  82. goto out;
  83. err = -ENOMEM;
  84. ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
  85. if (ns == NULL)
  86. goto out_dec;
  87. idr_init(&ns->idr);
  88. ns->pid_cachep = create_pid_cachep(level);
  89. if (ns->pid_cachep == NULL)
  90. goto out_free_idr;
  91. err = ns_alloc_inum(&ns->ns);
  92. if (err)
  93. goto out_free_idr;
  94. ns->ns.ops = &pidns_operations;
  95. kref_init(&ns->kref);
  96. ns->level = level;
  97. ns->parent = get_pid_ns(parent_pid_ns);
  98. ns->user_ns = get_user_ns(user_ns);
  99. ns->ucounts = ucounts;
  100. ns->pid_allocated = PIDNS_ADDING;
  101. INIT_WORK(&ns->proc_work, proc_cleanup_work);
  102. return ns;
  103. out_free_idr:
  104. idr_destroy(&ns->idr);
  105. kmem_cache_free(pid_ns_cachep, ns);
  106. out_dec:
  107. dec_pid_namespaces(ucounts);
  108. out:
  109. return ERR_PTR(err);
  110. }
  111. static void delayed_free_pidns(struct rcu_head *p)
  112. {
  113. struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
  114. dec_pid_namespaces(ns->ucounts);
  115. put_user_ns(ns->user_ns);
  116. kmem_cache_free(pid_ns_cachep, ns);
  117. }
  118. static void destroy_pid_namespace(struct pid_namespace *ns)
  119. {
  120. ns_free_inum(&ns->ns);
  121. idr_destroy(&ns->idr);
  122. call_rcu(&ns->rcu, delayed_free_pidns);
  123. }
  124. struct pid_namespace *copy_pid_ns(unsigned long flags,
  125. struct user_namespace *user_ns, struct pid_namespace *old_ns)
  126. {
  127. if (!(flags & CLONE_NEWPID))
  128. return get_pid_ns(old_ns);
  129. if (task_active_pid_ns(current) != old_ns)
  130. return ERR_PTR(-EINVAL);
  131. return create_pid_namespace(user_ns, old_ns);
  132. }
  133. static void free_pid_ns(struct kref *kref)
  134. {
  135. struct pid_namespace *ns;
  136. ns = container_of(kref, struct pid_namespace, kref);
  137. destroy_pid_namespace(ns);
  138. }
  139. void put_pid_ns(struct pid_namespace *ns)
  140. {
  141. struct pid_namespace *parent;
  142. while (ns != &init_pid_ns) {
  143. parent = ns->parent;
  144. if (!kref_put(&ns->kref, free_pid_ns))
  145. break;
  146. ns = parent;
  147. }
  148. }
  149. EXPORT_SYMBOL_GPL(put_pid_ns);
  150. void zap_pid_ns_processes(struct pid_namespace *pid_ns)
  151. {
  152. int nr;
  153. int rc;
  154. struct task_struct *task, *me = current;
  155. int init_pids = thread_group_leader(me) ? 1 : 2;
  156. struct pid *pid;
  157. /* Don't allow any more processes into the pid namespace */
  158. disable_pid_allocation(pid_ns);
  159. /*
  160. * Ignore SIGCHLD causing any terminated children to autoreap.
  161. * This speeds up the namespace shutdown, plus see the comment
  162. * below.
  163. */
  164. spin_lock_irq(&me->sighand->siglock);
  165. me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
  166. spin_unlock_irq(&me->sighand->siglock);
  167. /*
  168. * The last thread in the cgroup-init thread group is terminating.
  169. * Find remaining pid_ts in the namespace, signal and wait for them
  170. * to exit.
  171. *
  172. * Note: This signals each threads in the namespace - even those that
  173. * belong to the same thread group, To avoid this, we would have
  174. * to walk the entire tasklist looking a processes in this
  175. * namespace, but that could be unnecessarily expensive if the
  176. * pid namespace has just a few processes. Or we need to
  177. * maintain a tasklist for each pid namespace.
  178. *
  179. */
  180. rcu_read_lock();
  181. read_lock(&tasklist_lock);
  182. nr = 2;
  183. idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
  184. task = pid_task(pid, PIDTYPE_PID);
  185. if (task && !__fatal_signal_pending(task))
  186. send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
  187. }
  188. read_unlock(&tasklist_lock);
  189. rcu_read_unlock();
  190. /*
  191. * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
  192. * kernel_wait4() will also block until our children traced from the
  193. * parent namespace are detached and become EXIT_DEAD.
  194. */
  195. do {
  196. clear_thread_flag(TIF_SIGPENDING);
  197. rc = kernel_wait4(-1, NULL, __WALL, NULL);
  198. } while (rc != -ECHILD);
  199. /*
  200. * kernel_wait4() above can't reap the EXIT_DEAD children but we do not
  201. * really care, we could reparent them to the global init. We could
  202. * exit and reap ->child_reaper even if it is not the last thread in
  203. * this pid_ns, free_pid(pid_allocated == 0) calls proc_cleanup_work(),
  204. * pid_ns can not go away until proc_kill_sb() drops the reference.
  205. *
  206. * But this ns can also have other tasks injected by setns()+fork().
  207. * Again, ignoring the user visible semantics we do not really need
  208. * to wait until they are all reaped, but they can be reparented to
  209. * us and thus we need to ensure that pid->child_reaper stays valid
  210. * until they all go away. See free_pid()->wake_up_process().
  211. *
  212. * We rely on ignored SIGCHLD, an injected zombie must be autoreaped
  213. * if reparented.
  214. */
  215. for (;;) {
  216. set_current_state(TASK_INTERRUPTIBLE);
  217. if (pid_ns->pid_allocated == init_pids)
  218. break;
  219. schedule();
  220. }
  221. __set_current_state(TASK_RUNNING);
  222. if (pid_ns->reboot)
  223. current->signal->group_exit_code = pid_ns->reboot;
  224. acct_exit_ns(pid_ns);
  225. return;
  226. }
  227. #ifdef CONFIG_CHECKPOINT_RESTORE
  228. static int pid_ns_ctl_handler(struct ctl_table *table, int write,
  229. void __user *buffer, size_t *lenp, loff_t *ppos)
  230. {
  231. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  232. struct ctl_table tmp = *table;
  233. int ret, next;
  234. if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
  235. return -EPERM;
  236. /*
  237. * Writing directly to ns' last_pid field is OK, since this field
  238. * is volatile in a living namespace anyway and a code writing to
  239. * it should synchronize its usage with external means.
  240. */
  241. next = idr_get_cursor(&pid_ns->idr) - 1;
  242. tmp.data = &next;
  243. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  244. if (!ret && write)
  245. idr_set_cursor(&pid_ns->idr, next + 1);
  246. return ret;
  247. }
  248. extern int pid_max;
  249. static int zero = 0;
  250. static struct ctl_table pid_ns_ctl_table[] = {
  251. {
  252. .procname = "ns_last_pid",
  253. .maxlen = sizeof(int),
  254. .mode = 0666, /* permissions are checked in the handler */
  255. .proc_handler = pid_ns_ctl_handler,
  256. .extra1 = &zero,
  257. .extra2 = &pid_max,
  258. },
  259. { }
  260. };
  261. static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
  262. #endif /* CONFIG_CHECKPOINT_RESTORE */
  263. int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
  264. {
  265. if (pid_ns == &init_pid_ns)
  266. return 0;
  267. switch (cmd) {
  268. case LINUX_REBOOT_CMD_RESTART2:
  269. case LINUX_REBOOT_CMD_RESTART:
  270. pid_ns->reboot = SIGHUP;
  271. break;
  272. case LINUX_REBOOT_CMD_POWER_OFF:
  273. case LINUX_REBOOT_CMD_HALT:
  274. pid_ns->reboot = SIGINT;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. read_lock(&tasklist_lock);
  280. send_sig(SIGKILL, pid_ns->child_reaper, 1);
  281. read_unlock(&tasklist_lock);
  282. do_exit(0);
  283. /* Not reached */
  284. return 0;
  285. }
  286. static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
  287. {
  288. return container_of(ns, struct pid_namespace, ns);
  289. }
  290. static struct ns_common *pidns_get(struct task_struct *task)
  291. {
  292. struct pid_namespace *ns;
  293. rcu_read_lock();
  294. ns = task_active_pid_ns(task);
  295. if (ns)
  296. get_pid_ns(ns);
  297. rcu_read_unlock();
  298. return ns ? &ns->ns : NULL;
  299. }
  300. static struct ns_common *pidns_for_children_get(struct task_struct *task)
  301. {
  302. struct pid_namespace *ns = NULL;
  303. task_lock(task);
  304. if (task->nsproxy) {
  305. ns = task->nsproxy->pid_ns_for_children;
  306. get_pid_ns(ns);
  307. }
  308. task_unlock(task);
  309. if (ns) {
  310. read_lock(&tasklist_lock);
  311. if (!ns->child_reaper) {
  312. put_pid_ns(ns);
  313. ns = NULL;
  314. }
  315. read_unlock(&tasklist_lock);
  316. }
  317. return ns ? &ns->ns : NULL;
  318. }
  319. static void pidns_put(struct ns_common *ns)
  320. {
  321. put_pid_ns(to_pid_ns(ns));
  322. }
  323. static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  324. {
  325. struct pid_namespace *active = task_active_pid_ns(current);
  326. struct pid_namespace *ancestor, *new = to_pid_ns(ns);
  327. if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
  328. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  329. return -EPERM;
  330. /*
  331. * Only allow entering the current active pid namespace
  332. * or a child of the current active pid namespace.
  333. *
  334. * This is required for fork to return a usable pid value and
  335. * this maintains the property that processes and their
  336. * children can not escape their current pid namespace.
  337. */
  338. if (new->level < active->level)
  339. return -EINVAL;
  340. ancestor = new;
  341. while (ancestor->level > active->level)
  342. ancestor = ancestor->parent;
  343. if (ancestor != active)
  344. return -EINVAL;
  345. put_pid_ns(nsproxy->pid_ns_for_children);
  346. nsproxy->pid_ns_for_children = get_pid_ns(new);
  347. return 0;
  348. }
  349. static struct ns_common *pidns_get_parent(struct ns_common *ns)
  350. {
  351. struct pid_namespace *active = task_active_pid_ns(current);
  352. struct pid_namespace *pid_ns, *p;
  353. /* See if the parent is in the current namespace */
  354. pid_ns = p = to_pid_ns(ns)->parent;
  355. for (;;) {
  356. if (!p)
  357. return ERR_PTR(-EPERM);
  358. if (p == active)
  359. break;
  360. p = p->parent;
  361. }
  362. return &get_pid_ns(pid_ns)->ns;
  363. }
  364. static struct user_namespace *pidns_owner(struct ns_common *ns)
  365. {
  366. return to_pid_ns(ns)->user_ns;
  367. }
  368. const struct proc_ns_operations pidns_operations = {
  369. .name = "pid",
  370. .type = CLONE_NEWPID,
  371. .get = pidns_get,
  372. .put = pidns_put,
  373. .install = pidns_install,
  374. .owner = pidns_owner,
  375. .get_parent = pidns_get_parent,
  376. };
  377. const struct proc_ns_operations pidns_for_children_operations = {
  378. .name = "pid_for_children",
  379. .real_ns_name = "pid",
  380. .type = CLONE_NEWPID,
  381. .get = pidns_for_children_get,
  382. .put = pidns_put,
  383. .install = pidns_install,
  384. .owner = pidns_owner,
  385. .get_parent = pidns_get_parent,
  386. };
  387. static __init int pid_namespaces_init(void)
  388. {
  389. pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
  390. #ifdef CONFIG_CHECKPOINT_RESTORE
  391. register_sysctl_paths(kern_path, pid_ns_ctl_table);
  392. #endif
  393. return 0;
  394. }
  395. __initcall(pid_namespaces_init);