1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2006 IBM Corporation 4 * 5 * Author: Serge Hallyn <serue@us.ibm.com> 6 * 7 * Jun 2006 - namespaces support 8 * OpenVZ, SWsoft Inc. 9 * Pavel Emelianov <xemul@openvz.org> 10 */ 11 12 #include <linux/slab.h> 13 #include <linux/export.h> 14 #include <linux/nsproxy.h> 15 #include <linux/ns/ns_common_types.h> 16 #include <linux/init_task.h> 17 #include <linux/mnt_namespace.h> 18 #include <linux/utsname.h> 19 #include <linux/pid_namespace.h> 20 #include <net/net_namespace.h> 21 #include <linux/ipc_namespace.h> 22 #include <linux/time_namespace.h> 23 #include <linux/fs_struct.h> 24 #include <linux/proc_fs.h> 25 #include <linux/proc_ns.h> 26 #include <linux/file.h> 27 #include <linux/syscalls.h> 28 #include <linux/cgroup.h> 29 #include <linux/perf_event.h> 30 #include <linux/nstree.h> 31 32 static struct kmem_cache *nsproxy_cachep; 33 34 struct nsproxy init_nsproxy = { 35 .count = REFCOUNT_INIT(1), 36 .uts_ns = &init_uts_ns, 37 #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC) 38 .ipc_ns = &init_ipc_ns, 39 #endif 40 .mnt_ns = NULL, 41 .pid_ns_for_children = &init_pid_ns, 42 #ifdef CONFIG_NET 43 .net_ns = &init_net, 44 #endif 45 #ifdef CONFIG_CGROUPS 46 .cgroup_ns = &init_cgroup_ns, 47 #endif 48 #ifdef CONFIG_TIME_NS 49 .time_ns = &init_time_ns, 50 .time_ns_for_children = &init_time_ns, 51 #endif 52 }; 53 54 static inline struct nsproxy *create_nsproxy(void) 55 { 56 struct nsproxy *nsproxy; 57 58 nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL); 59 if (nsproxy) 60 refcount_set(&nsproxy->count, 1); 61 return nsproxy; 62 } 63 64 void nsproxy_free(struct nsproxy *ns) 65 { 66 put_mnt_ns(ns->mnt_ns); 67 put_uts_ns(ns->uts_ns); 68 put_ipc_ns(ns->ipc_ns); 69 put_pid_ns(ns->pid_ns_for_children); 70 put_time_ns(ns->time_ns); 71 put_time_ns(ns->time_ns_for_children); 72 put_cgroup_ns(ns->cgroup_ns); 73 put_net(ns->net_ns); 74 kmem_cache_free(nsproxy_cachep, ns); 75 } 76 77 void deactivate_nsproxy(struct nsproxy *ns) 78 { 79 nsproxy_ns_active_put(ns); 80 nsproxy_free(ns); 81 } 82 83 /* 84 * Create new nsproxy and all of its the associated namespaces. 85 * Return the newly created nsproxy. Do not attach this to the task, 86 * leave it to the caller to do proper locking and attach it to task. 87 */ 88 static struct nsproxy *create_new_namespaces(u64 flags, 89 struct task_struct *tsk, struct user_namespace *user_ns, 90 struct fs_struct *new_fs) 91 { 92 struct nsproxy *new_nsp; 93 int err; 94 95 new_nsp = create_nsproxy(); 96 if (!new_nsp) 97 return ERR_PTR(-ENOMEM); 98 99 new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, 100 user_ns, new_fs); 101 if (IS_ERR(new_nsp->mnt_ns)) { 102 err = PTR_ERR(new_nsp->mnt_ns); 103 goto out_ns; 104 } 105 106 new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns); 107 if (IS_ERR(new_nsp->uts_ns)) { 108 err = PTR_ERR(new_nsp->uts_ns); 109 goto out_uts; 110 } 111 112 new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns); 113 if (IS_ERR(new_nsp->ipc_ns)) { 114 err = PTR_ERR(new_nsp->ipc_ns); 115 goto out_ipc; 116 } 117 118 new_nsp->pid_ns_for_children = 119 copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children); 120 if (IS_ERR(new_nsp->pid_ns_for_children)) { 121 err = PTR_ERR(new_nsp->pid_ns_for_children); 122 goto out_pid; 123 } 124 125 new_nsp->cgroup_ns = copy_cgroup_ns(flags, user_ns, 126 tsk->nsproxy->cgroup_ns); 127 if (IS_ERR(new_nsp->cgroup_ns)) { 128 err = PTR_ERR(new_nsp->cgroup_ns); 129 goto out_cgroup; 130 } 131 132 new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns); 133 if (IS_ERR(new_nsp->net_ns)) { 134 err = PTR_ERR(new_nsp->net_ns); 135 goto out_net; 136 } 137 138 new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns, 139 tsk->nsproxy->time_ns_for_children); 140 if (IS_ERR(new_nsp->time_ns_for_children)) { 141 err = PTR_ERR(new_nsp->time_ns_for_children); 142 goto out_time; 143 } 144 new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns); 145 146 return new_nsp; 147 148 out_time: 149 put_net(new_nsp->net_ns); 150 out_net: 151 put_cgroup_ns(new_nsp->cgroup_ns); 152 out_cgroup: 153 put_pid_ns(new_nsp->pid_ns_for_children); 154 out_pid: 155 put_ipc_ns(new_nsp->ipc_ns); 156 out_ipc: 157 put_uts_ns(new_nsp->uts_ns); 158 out_uts: 159 put_mnt_ns(new_nsp->mnt_ns); 160 out_ns: 161 kmem_cache_free(nsproxy_cachep, new_nsp); 162 return ERR_PTR(err); 163 } 164 165 /* 166 * called from clone. This now handles copy for nsproxy and all 167 * namespaces therein. 168 */ 169 int copy_namespaces(u64 flags, struct task_struct *tsk) 170 { 171 struct nsproxy *old_ns = tsk->nsproxy; 172 struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns); 173 struct nsproxy *new_ns; 174 175 if (likely(!(flags & (CLONE_NS_ALL & ~CLONE_NEWUSER)))) { 176 if ((flags & CLONE_VM) || 177 likely(old_ns->time_ns_for_children == old_ns->time_ns)) { 178 get_nsproxy(old_ns); 179 return 0; 180 } 181 } else if (!ns_capable(user_ns, CAP_SYS_ADMIN)) 182 return -EPERM; 183 184 /* 185 * CLONE_NEWIPC must detach from the undolist: after switching 186 * to a new ipc namespace, the semaphore arrays from the old 187 * namespace are unreachable. In clone parlance, CLONE_SYSVSEM 188 * means share undolist with parent, so we must forbid using 189 * it along with CLONE_NEWIPC. 190 */ 191 if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) == 192 (CLONE_NEWIPC | CLONE_SYSVSEM)) 193 return -EINVAL; 194 195 new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs); 196 if (IS_ERR(new_ns)) 197 return PTR_ERR(new_ns); 198 199 if ((flags & CLONE_VM) == 0) 200 timens_on_fork(new_ns, tsk); 201 202 nsproxy_ns_active_get(new_ns); 203 tsk->nsproxy = new_ns; 204 return 0; 205 } 206 207 /* 208 * Called from unshare. Unshare all the namespaces part of nsproxy. 209 * On success, returns the new nsproxy. 210 */ 211 int unshare_nsproxy_namespaces(unsigned long unshare_flags, 212 struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs) 213 { 214 struct user_namespace *user_ns; 215 u64 flags = unshare_flags; 216 int err = 0; 217 218 if (!(flags & (CLONE_NS_ALL & ~CLONE_NEWUSER))) 219 return 0; 220 221 user_ns = new_cred ? new_cred->user_ns : current_user_ns(); 222 if (!ns_capable(user_ns, CAP_SYS_ADMIN)) 223 return -EPERM; 224 225 /* 226 * Convert the 32-bit UNSHARE_EMPTY_MNTNS (which aliases 227 * CLONE_PARENT_SETTID) to the unique 64-bit CLONE_EMPTY_MNTNS. 228 */ 229 if (flags & UNSHARE_EMPTY_MNTNS) { 230 flags &= ~(u64)UNSHARE_EMPTY_MNTNS; 231 flags |= CLONE_EMPTY_MNTNS; 232 } 233 234 *new_nsp = create_new_namespaces(flags, current, user_ns, 235 new_fs ? new_fs : current->fs); 236 if (IS_ERR(*new_nsp)) { 237 err = PTR_ERR(*new_nsp); 238 goto out; 239 } 240 241 out: 242 return err; 243 } 244 245 void switch_task_namespaces(struct task_struct *p, struct nsproxy *new) 246 { 247 struct nsproxy *ns; 248 249 might_sleep(); 250 251 if (new) 252 nsproxy_ns_active_get(new); 253 254 task_lock(p); 255 ns = p->nsproxy; 256 p->nsproxy = new; 257 task_unlock(p); 258 259 if (ns) 260 put_nsproxy(ns); 261 } 262 263 void exit_nsproxy_namespaces(struct task_struct *p) 264 { 265 switch_task_namespaces(p, NULL); 266 } 267 268 void switch_cred_namespaces(const struct cred *old, const struct cred *new) 269 { 270 ns_ref_active_get(new->user_ns); 271 ns_ref_active_put(old->user_ns); 272 } 273 274 void get_cred_namespaces(struct task_struct *tsk) 275 { 276 ns_ref_active_get(tsk->real_cred->user_ns); 277 } 278 279 void exit_cred_namespaces(struct task_struct *tsk) 280 { 281 ns_ref_active_put(tsk->real_cred->user_ns); 282 } 283 284 int exec_task_namespaces(void) 285 { 286 struct task_struct *tsk = current; 287 struct nsproxy *new; 288 289 if (tsk->nsproxy->time_ns_for_children == tsk->nsproxy->time_ns) 290 return 0; 291 292 new = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs); 293 if (IS_ERR(new)) 294 return PTR_ERR(new); 295 296 timens_on_fork(new, tsk); 297 switch_task_namespaces(tsk, new); 298 return 0; 299 } 300 301 static int check_setns_flags(unsigned long flags) 302 { 303 if (!flags || (flags & ~CLONE_NS_ALL)) 304 return -EINVAL; 305 306 #ifndef CONFIG_USER_NS 307 if (flags & CLONE_NEWUSER) 308 return -EINVAL; 309 #endif 310 #ifndef CONFIG_PID_NS 311 if (flags & CLONE_NEWPID) 312 return -EINVAL; 313 #endif 314 #ifndef CONFIG_UTS_NS 315 if (flags & CLONE_NEWUTS) 316 return -EINVAL; 317 #endif 318 #ifndef CONFIG_IPC_NS 319 if (flags & CLONE_NEWIPC) 320 return -EINVAL; 321 #endif 322 #ifndef CONFIG_CGROUPS 323 if (flags & CLONE_NEWCGROUP) 324 return -EINVAL; 325 #endif 326 #ifndef CONFIG_NET_NS 327 if (flags & CLONE_NEWNET) 328 return -EINVAL; 329 #endif 330 #ifndef CONFIG_TIME_NS 331 if (flags & CLONE_NEWTIME) 332 return -EINVAL; 333 #endif 334 335 return 0; 336 } 337 338 static void put_nsset(struct nsset *nsset) 339 { 340 unsigned flags = nsset->flags; 341 342 if (flags & CLONE_NEWUSER) 343 put_cred(nsset_cred(nsset)); 344 /* 345 * We only created a temporary copy if we attached to more than just 346 * the mount namespace. 347 */ 348 if (nsset->fs && (flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS)) 349 free_fs_struct(nsset->fs); 350 if (nsset->nsproxy) 351 nsproxy_free(nsset->nsproxy); 352 } 353 354 static int prepare_nsset(unsigned flags, struct nsset *nsset) 355 { 356 struct task_struct *me = current; 357 358 nsset->nsproxy = create_new_namespaces(0, me, current_user_ns(), me->fs); 359 if (IS_ERR(nsset->nsproxy)) 360 return PTR_ERR(nsset->nsproxy); 361 362 if (flags & CLONE_NEWUSER) 363 nsset->cred = prepare_creds(); 364 else 365 nsset->cred = current_cred(); 366 if (!nsset->cred) 367 goto out; 368 369 /* Only create a temporary copy of fs_struct if we really need to. */ 370 if (flags == CLONE_NEWNS) { 371 nsset->fs = me->fs; 372 } else if (flags & CLONE_NEWNS) { 373 nsset->fs = copy_fs_struct(me->fs); 374 if (!nsset->fs) 375 goto out; 376 } 377 378 nsset->flags = flags; 379 return 0; 380 381 out: 382 put_nsset(nsset); 383 return -ENOMEM; 384 } 385 386 static inline int validate_ns(struct nsset *nsset, struct ns_common *ns) 387 { 388 int ret; 389 390 ret = security_namespace_install(nsset, ns); 391 if (ret) 392 return ret; 393 394 return ns->ops->install(nsset, ns); 395 } 396 397 /* 398 * This is the inverse operation to unshare(). 399 * Ordering is equivalent to the standard ordering used everywhere else 400 * during unshare and process creation. The switch to the new set of 401 * namespaces occurs at the point of no return after installation of 402 * all requested namespaces was successful in commit_nsset(). 403 */ 404 static int validate_nsset(struct nsset *nsset, struct pid *pid) 405 { 406 int ret = 0; 407 unsigned flags = nsset->flags; 408 struct user_namespace *user_ns = NULL; 409 struct pid_namespace *pid_ns = NULL; 410 struct nsproxy *nsp; 411 struct task_struct *tsk; 412 413 /* Take a "snapshot" of the target task's namespaces. */ 414 rcu_read_lock(); 415 tsk = pid_task(pid, PIDTYPE_PID); 416 if (!tsk) { 417 rcu_read_unlock(); 418 return -ESRCH; 419 } 420 421 if (!ptrace_may_access(tsk, PTRACE_MODE_READ_REALCREDS)) { 422 rcu_read_unlock(); 423 return -EPERM; 424 } 425 426 task_lock(tsk); 427 nsp = tsk->nsproxy; 428 if (nsp) 429 get_nsproxy(nsp); 430 task_unlock(tsk); 431 if (!nsp) { 432 rcu_read_unlock(); 433 return -ESRCH; 434 } 435 436 #ifdef CONFIG_PID_NS 437 if (flags & CLONE_NEWPID) { 438 pid_ns = task_active_pid_ns(tsk); 439 if (unlikely(!pid_ns)) { 440 rcu_read_unlock(); 441 ret = -ESRCH; 442 goto out; 443 } 444 get_pid_ns(pid_ns); 445 } 446 #endif 447 448 #ifdef CONFIG_USER_NS 449 if (flags & CLONE_NEWUSER) 450 user_ns = get_user_ns(__task_cred(tsk)->user_ns); 451 #endif 452 rcu_read_unlock(); 453 454 /* 455 * Install requested namespaces. The caller will have 456 * verified earlier that the requested namespaces are 457 * supported on this kernel. We don't report errors here 458 * if a namespace is requested that isn't supported. 459 */ 460 #ifdef CONFIG_USER_NS 461 if (flags & CLONE_NEWUSER) { 462 ret = validate_ns(nsset, &user_ns->ns); 463 if (ret) 464 goto out; 465 } 466 #endif 467 468 if (flags & CLONE_NEWNS) { 469 ret = validate_ns(nsset, from_mnt_ns(nsp->mnt_ns)); 470 if (ret) 471 goto out; 472 } 473 474 #ifdef CONFIG_UTS_NS 475 if (flags & CLONE_NEWUTS) { 476 ret = validate_ns(nsset, &nsp->uts_ns->ns); 477 if (ret) 478 goto out; 479 } 480 #endif 481 482 #ifdef CONFIG_IPC_NS 483 if (flags & CLONE_NEWIPC) { 484 ret = validate_ns(nsset, &nsp->ipc_ns->ns); 485 if (ret) 486 goto out; 487 } 488 #endif 489 490 #ifdef CONFIG_PID_NS 491 if (flags & CLONE_NEWPID) { 492 ret = validate_ns(nsset, &pid_ns->ns); 493 if (ret) 494 goto out; 495 } 496 #endif 497 498 #ifdef CONFIG_CGROUPS 499 if (flags & CLONE_NEWCGROUP) { 500 ret = validate_ns(nsset, &nsp->cgroup_ns->ns); 501 if (ret) 502 goto out; 503 } 504 #endif 505 506 #ifdef CONFIG_NET_NS 507 if (flags & CLONE_NEWNET) { 508 ret = validate_ns(nsset, &nsp->net_ns->ns); 509 if (ret) 510 goto out; 511 } 512 #endif 513 514 #ifdef CONFIG_TIME_NS 515 if (flags & CLONE_NEWTIME) { 516 ret = validate_ns(nsset, &nsp->time_ns->ns); 517 if (ret) 518 goto out; 519 } 520 #endif 521 522 out: 523 if (pid_ns) 524 put_pid_ns(pid_ns); 525 if (nsp) 526 put_nsproxy(nsp); 527 put_user_ns(user_ns); 528 529 return ret; 530 } 531 532 /* 533 * This is the point of no return. There are just a few namespaces 534 * that do some actual work here and it's sufficiently minimal that 535 * a separate ns_common operation seems unnecessary for now. 536 * Unshare is doing the same thing. If we'll end up needing to do 537 * more in a given namespace or a helper here is ultimately not 538 * exported anymore a simple commit handler for each namespace 539 * should be added to ns_common. 540 */ 541 static void commit_nsset(struct nsset *nsset) 542 { 543 unsigned flags = nsset->flags; 544 struct task_struct *me = current; 545 546 #ifdef CONFIG_USER_NS 547 if (flags & CLONE_NEWUSER) { 548 /* transfer ownership */ 549 commit_creds(nsset_cred(nsset)); 550 nsset->cred = NULL; 551 } 552 #endif 553 554 /* We only need to commit if we have used a temporary fs_struct. */ 555 if ((flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS)) { 556 set_fs_root(me->fs, &nsset->fs->root); 557 set_fs_pwd(me->fs, &nsset->fs->pwd); 558 } 559 560 #ifdef CONFIG_IPC_NS 561 if (flags & CLONE_NEWIPC) 562 exit_sem(me); 563 #endif 564 565 #ifdef CONFIG_TIME_NS 566 if (flags & CLONE_NEWTIME) 567 timens_commit(me, nsset->nsproxy->time_ns); 568 #endif 569 570 /* transfer ownership */ 571 switch_task_namespaces(me, nsset->nsproxy); 572 nsset->nsproxy = NULL; 573 } 574 575 SYSCALL_DEFINE2(setns, int, fd, int, flags) 576 { 577 CLASS(fd, f)(fd); 578 struct ns_common *ns = NULL; 579 struct nsset nsset = {}; 580 int err = 0; 581 582 if (fd_empty(f)) 583 return -EBADF; 584 585 if (proc_ns_file(fd_file(f))) { 586 ns = get_proc_ns(file_inode(fd_file(f))); 587 if (flags && (ns->ns_type != flags)) 588 err = -EINVAL; 589 flags = ns->ns_type; 590 } else if (!IS_ERR(pidfd_pid(fd_file(f)))) { 591 err = check_setns_flags(flags); 592 } else { 593 err = -EINVAL; 594 } 595 if (err) 596 goto out; 597 598 err = prepare_nsset(flags, &nsset); 599 if (err) 600 goto out; 601 602 if (proc_ns_file(fd_file(f))) 603 err = validate_ns(&nsset, ns); 604 else 605 err = validate_nsset(&nsset, pidfd_pid(fd_file(f))); 606 if (!err) { 607 commit_nsset(&nsset); 608 perf_event_namespaces(current); 609 } 610 put_nsset(&nsset); 611 out: 612 return err; 613 } 614 615 int __init nsproxy_cache_init(void) 616 { 617 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT); 618 return 0; 619 } 620