1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/export.h> 4 #include <linux/nsproxy.h> 5 #include <linux/slab.h> 6 #include <linux/sched/signal.h> 7 #include <linux/user_namespace.h> 8 #include <linux/proc_ns.h> 9 #include <linux/highuid.h> 10 #include <linux/cred.h> 11 #include <linux/securebits.h> 12 #include <linux/keyctl.h> 13 #include <linux/key-type.h> 14 #include <keys/user-type.h> 15 #include <linux/seq_file.h> 16 #include <linux/fs.h> 17 #include <linux/uaccess.h> 18 #include <linux/ctype.h> 19 #include <linux/projid.h> 20 #include <linux/fs_struct.h> 21 #include <linux/bsearch.h> 22 #include <linux/sort.h> 23 24 static struct kmem_cache *user_ns_cachep __read_mostly; 25 static DEFINE_MUTEX(userns_state_mutex); 26 27 static bool new_idmap_permitted(const struct file *file, 28 struct user_namespace *ns, int cap_setid, 29 struct uid_gid_map *map); 30 static void free_user_ns(struct work_struct *work); 31 32 static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid) 33 { 34 return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES); 35 } 36 37 static void dec_user_namespaces(struct ucounts *ucounts) 38 { 39 return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES); 40 } 41 42 static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns) 43 { 44 /* Start with the same capabilities as init but useless for doing 45 * anything as the capabilities are bound to the new user namespace. 46 */ 47 cred->securebits = SECUREBITS_DEFAULT; 48 cred->cap_inheritable = CAP_EMPTY_SET; 49 cred->cap_permitted = CAP_FULL_SET; 50 cred->cap_effective = CAP_FULL_SET; 51 cred->cap_ambient = CAP_EMPTY_SET; 52 cred->cap_bset = CAP_FULL_SET; 53 #ifdef CONFIG_KEYS 54 key_put(cred->request_key_auth); 55 cred->request_key_auth = NULL; 56 #endif 57 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ 58 cred->user_ns = user_ns; 59 } 60 61 /* 62 * Create a new user namespace, deriving the creator from the user in the 63 * passed credentials, and replacing that user with the new root user for the 64 * new namespace. 65 * 66 * This is called by copy_creds(), which will finish setting the target task's 67 * credentials. 68 */ 69 int create_user_ns(struct cred *new) 70 { 71 struct user_namespace *ns, *parent_ns = new->user_ns; 72 kuid_t owner = new->euid; 73 kgid_t group = new->egid; 74 struct ucounts *ucounts; 75 int ret, i; 76 77 ret = -ENOSPC; 78 if (parent_ns->level > 32) 79 goto fail; 80 81 ucounts = inc_user_namespaces(parent_ns, owner); 82 if (!ucounts) 83 goto fail; 84 85 /* 86 * Verify that we can not violate the policy of which files 87 * may be accessed that is specified by the root directory, 88 * by verifing that the root directory is at the root of the 89 * mount namespace which allows all files to be accessed. 90 */ 91 ret = -EPERM; 92 if (current_chrooted()) 93 goto fail_dec; 94 95 /* The creator needs a mapping in the parent user namespace 96 * or else we won't be able to reasonably tell userspace who 97 * created a user_namespace. 98 */ 99 ret = -EPERM; 100 if (!kuid_has_mapping(parent_ns, owner) || 101 !kgid_has_mapping(parent_ns, group)) 102 goto fail_dec; 103 104 ret = -ENOMEM; 105 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL); 106 if (!ns) 107 goto fail_dec; 108 109 ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP); 110 ret = ns_alloc_inum(&ns->ns); 111 if (ret) 112 goto fail_free; 113 ns->ns.ops = &userns_operations; 114 115 refcount_set(&ns->ns.count, 1); 116 /* Leave the new->user_ns reference with the new user namespace. */ 117 ns->parent = parent_ns; 118 ns->level = parent_ns->level + 1; 119 ns->owner = owner; 120 ns->group = group; 121 INIT_WORK(&ns->work, free_user_ns); 122 for (i = 0; i < MAX_PER_NAMESPACE_UCOUNTS; i++) { 123 ns->ucount_max[i] = INT_MAX; 124 } 125 ns->ucount_max[UCOUNT_RLIMIT_NPROC] = rlimit(RLIMIT_NPROC); 126 ns->ucount_max[UCOUNT_RLIMIT_MSGQUEUE] = rlimit(RLIMIT_MSGQUEUE); 127 ns->ucount_max[UCOUNT_RLIMIT_SIGPENDING] = rlimit(RLIMIT_SIGPENDING); 128 ns->ucounts = ucounts; 129 130 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ 131 mutex_lock(&userns_state_mutex); 132 ns->flags = parent_ns->flags; 133 mutex_unlock(&userns_state_mutex); 134 135 #ifdef CONFIG_KEYS 136 INIT_LIST_HEAD(&ns->keyring_name_list); 137 init_rwsem(&ns->keyring_sem); 138 #endif 139 ret = -ENOMEM; 140 if (!setup_userns_sysctls(ns)) 141 goto fail_keyring; 142 143 set_cred_user_ns(new, ns); 144 return 0; 145 fail_keyring: 146 #ifdef CONFIG_PERSISTENT_KEYRINGS 147 key_put(ns->persistent_keyring_register); 148 #endif 149 ns_free_inum(&ns->ns); 150 fail_free: 151 kmem_cache_free(user_ns_cachep, ns); 152 fail_dec: 153 dec_user_namespaces(ucounts); 154 fail: 155 return ret; 156 } 157 158 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) 159 { 160 struct cred *cred; 161 int err = -ENOMEM; 162 163 if (!(unshare_flags & CLONE_NEWUSER)) 164 return 0; 165 166 cred = prepare_creds(); 167 if (cred) { 168 err = create_user_ns(cred); 169 if (err) 170 put_cred(cred); 171 else 172 *new_cred = cred; 173 } 174 175 return err; 176 } 177 178 static void free_user_ns(struct work_struct *work) 179 { 180 struct user_namespace *parent, *ns = 181 container_of(work, struct user_namespace, work); 182 183 do { 184 struct ucounts *ucounts = ns->ucounts; 185 parent = ns->parent; 186 if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { 187 kfree(ns->gid_map.forward); 188 kfree(ns->gid_map.reverse); 189 } 190 if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { 191 kfree(ns->uid_map.forward); 192 kfree(ns->uid_map.reverse); 193 } 194 if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { 195 kfree(ns->projid_map.forward); 196 kfree(ns->projid_map.reverse); 197 } 198 retire_userns_sysctls(ns); 199 key_free_user_ns(ns); 200 ns_free_inum(&ns->ns); 201 kmem_cache_free(user_ns_cachep, ns); 202 dec_user_namespaces(ucounts); 203 ns = parent; 204 } while (refcount_dec_and_test(&parent->ns.count)); 205 } 206 207 void __put_user_ns(struct user_namespace *ns) 208 { 209 schedule_work(&ns->work); 210 } 211 EXPORT_SYMBOL(__put_user_ns); 212 213 /** 214 * idmap_key struct holds the information necessary to find an idmapping in a 215 * sorted idmap array. It is passed to cmp_map_id() as first argument. 216 */ 217 struct idmap_key { 218 bool map_up; /* true -> id from kid; false -> kid from id */ 219 u32 id; /* id to find */ 220 u32 count; /* == 0 unless used with map_id_range_down() */ 221 }; 222 223 /** 224 * cmp_map_id - Function to be passed to bsearch() to find the requested 225 * idmapping. Expects struct idmap_key to be passed via @k. 226 */ 227 static int cmp_map_id(const void *k, const void *e) 228 { 229 u32 first, last, id2; 230 const struct idmap_key *key = k; 231 const struct uid_gid_extent *el = e; 232 233 id2 = key->id + key->count - 1; 234 235 /* handle map_id_{down,up}() */ 236 if (key->map_up) 237 first = el->lower_first; 238 else 239 first = el->first; 240 241 last = first + el->count - 1; 242 243 if (key->id >= first && key->id <= last && 244 (id2 >= first && id2 <= last)) 245 return 0; 246 247 if (key->id < first || id2 < first) 248 return -1; 249 250 return 1; 251 } 252 253 /** 254 * map_id_range_down_max - Find idmap via binary search in ordered idmap array. 255 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. 256 */ 257 static struct uid_gid_extent * 258 map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count) 259 { 260 struct idmap_key key; 261 262 key.map_up = false; 263 key.count = count; 264 key.id = id; 265 266 return bsearch(&key, map->forward, extents, 267 sizeof(struct uid_gid_extent), cmp_map_id); 268 } 269 270 /** 271 * map_id_range_down_base - Find idmap via binary search in static extent array. 272 * Can only be called if number of mappings is equal or less than 273 * UID_GID_MAP_MAX_BASE_EXTENTS. 274 */ 275 static struct uid_gid_extent * 276 map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count) 277 { 278 unsigned idx; 279 u32 first, last, id2; 280 281 id2 = id + count - 1; 282 283 /* Find the matching extent */ 284 for (idx = 0; idx < extents; idx++) { 285 first = map->extent[idx].first; 286 last = first + map->extent[idx].count - 1; 287 if (id >= first && id <= last && 288 (id2 >= first && id2 <= last)) 289 return &map->extent[idx]; 290 } 291 return NULL; 292 } 293 294 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count) 295 { 296 struct uid_gid_extent *extent; 297 unsigned extents = map->nr_extents; 298 smp_rmb(); 299 300 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 301 extent = map_id_range_down_base(extents, map, id, count); 302 else 303 extent = map_id_range_down_max(extents, map, id, count); 304 305 /* Map the id or note failure */ 306 if (extent) 307 id = (id - extent->first) + extent->lower_first; 308 else 309 id = (u32) -1; 310 311 return id; 312 } 313 314 static u32 map_id_down(struct uid_gid_map *map, u32 id) 315 { 316 return map_id_range_down(map, id, 1); 317 } 318 319 /** 320 * map_id_up_base - Find idmap via binary search in static extent array. 321 * Can only be called if number of mappings is equal or less than 322 * UID_GID_MAP_MAX_BASE_EXTENTS. 323 */ 324 static struct uid_gid_extent * 325 map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id) 326 { 327 unsigned idx; 328 u32 first, last; 329 330 /* Find the matching extent */ 331 for (idx = 0; idx < extents; idx++) { 332 first = map->extent[idx].lower_first; 333 last = first + map->extent[idx].count - 1; 334 if (id >= first && id <= last) 335 return &map->extent[idx]; 336 } 337 return NULL; 338 } 339 340 /** 341 * map_id_up_max - Find idmap via binary search in ordered idmap array. 342 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. 343 */ 344 static struct uid_gid_extent * 345 map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id) 346 { 347 struct idmap_key key; 348 349 key.map_up = true; 350 key.count = 1; 351 key.id = id; 352 353 return bsearch(&key, map->reverse, extents, 354 sizeof(struct uid_gid_extent), cmp_map_id); 355 } 356 357 static u32 map_id_up(struct uid_gid_map *map, u32 id) 358 { 359 struct uid_gid_extent *extent; 360 unsigned extents = map->nr_extents; 361 smp_rmb(); 362 363 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 364 extent = map_id_up_base(extents, map, id); 365 else 366 extent = map_id_up_max(extents, map, id); 367 368 /* Map the id or note failure */ 369 if (extent) 370 id = (id - extent->lower_first) + extent->first; 371 else 372 id = (u32) -1; 373 374 return id; 375 } 376 377 /** 378 * make_kuid - Map a user-namespace uid pair into a kuid. 379 * @ns: User namespace that the uid is in 380 * @uid: User identifier 381 * 382 * Maps a user-namespace uid pair into a kernel internal kuid, 383 * and returns that kuid. 384 * 385 * When there is no mapping defined for the user-namespace uid 386 * pair INVALID_UID is returned. Callers are expected to test 387 * for and handle INVALID_UID being returned. INVALID_UID 388 * may be tested for using uid_valid(). 389 */ 390 kuid_t make_kuid(struct user_namespace *ns, uid_t uid) 391 { 392 /* Map the uid to a global kernel uid */ 393 return KUIDT_INIT(map_id_down(&ns->uid_map, uid)); 394 } 395 EXPORT_SYMBOL(make_kuid); 396 397 /** 398 * from_kuid - Create a uid from a kuid user-namespace pair. 399 * @targ: The user namespace we want a uid in. 400 * @kuid: The kernel internal uid to start with. 401 * 402 * Map @kuid into the user-namespace specified by @targ and 403 * return the resulting uid. 404 * 405 * There is always a mapping into the initial user_namespace. 406 * 407 * If @kuid has no mapping in @targ (uid_t)-1 is returned. 408 */ 409 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid) 410 { 411 /* Map the uid from a global kernel uid */ 412 return map_id_up(&targ->uid_map, __kuid_val(kuid)); 413 } 414 EXPORT_SYMBOL(from_kuid); 415 416 /** 417 * from_kuid_munged - Create a uid from a kuid user-namespace pair. 418 * @targ: The user namespace we want a uid in. 419 * @kuid: The kernel internal uid to start with. 420 * 421 * Map @kuid into the user-namespace specified by @targ and 422 * return the resulting uid. 423 * 424 * There is always a mapping into the initial user_namespace. 425 * 426 * Unlike from_kuid from_kuid_munged never fails and always 427 * returns a valid uid. This makes from_kuid_munged appropriate 428 * for use in syscalls like stat and getuid where failing the 429 * system call and failing to provide a valid uid are not an 430 * options. 431 * 432 * If @kuid has no mapping in @targ overflowuid is returned. 433 */ 434 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid) 435 { 436 uid_t uid; 437 uid = from_kuid(targ, kuid); 438 439 if (uid == (uid_t) -1) 440 uid = overflowuid; 441 return uid; 442 } 443 EXPORT_SYMBOL(from_kuid_munged); 444 445 /** 446 * make_kgid - Map a user-namespace gid pair into a kgid. 447 * @ns: User namespace that the gid is in 448 * @gid: group identifier 449 * 450 * Maps a user-namespace gid pair into a kernel internal kgid, 451 * and returns that kgid. 452 * 453 * When there is no mapping defined for the user-namespace gid 454 * pair INVALID_GID is returned. Callers are expected to test 455 * for and handle INVALID_GID being returned. INVALID_GID may be 456 * tested for using gid_valid(). 457 */ 458 kgid_t make_kgid(struct user_namespace *ns, gid_t gid) 459 { 460 /* Map the gid to a global kernel gid */ 461 return KGIDT_INIT(map_id_down(&ns->gid_map, gid)); 462 } 463 EXPORT_SYMBOL(make_kgid); 464 465 /** 466 * from_kgid - Create a gid from a kgid user-namespace pair. 467 * @targ: The user namespace we want a gid in. 468 * @kgid: The kernel internal gid to start with. 469 * 470 * Map @kgid into the user-namespace specified by @targ and 471 * return the resulting gid. 472 * 473 * There is always a mapping into the initial user_namespace. 474 * 475 * If @kgid has no mapping in @targ (gid_t)-1 is returned. 476 */ 477 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid) 478 { 479 /* Map the gid from a global kernel gid */ 480 return map_id_up(&targ->gid_map, __kgid_val(kgid)); 481 } 482 EXPORT_SYMBOL(from_kgid); 483 484 /** 485 * from_kgid_munged - Create a gid from a kgid user-namespace pair. 486 * @targ: The user namespace we want a gid in. 487 * @kgid: The kernel internal gid to start with. 488 * 489 * Map @kgid into the user-namespace specified by @targ and 490 * return the resulting gid. 491 * 492 * There is always a mapping into the initial user_namespace. 493 * 494 * Unlike from_kgid from_kgid_munged never fails and always 495 * returns a valid gid. This makes from_kgid_munged appropriate 496 * for use in syscalls like stat and getgid where failing the 497 * system call and failing to provide a valid gid are not options. 498 * 499 * If @kgid has no mapping in @targ overflowgid is returned. 500 */ 501 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid) 502 { 503 gid_t gid; 504 gid = from_kgid(targ, kgid); 505 506 if (gid == (gid_t) -1) 507 gid = overflowgid; 508 return gid; 509 } 510 EXPORT_SYMBOL(from_kgid_munged); 511 512 /** 513 * make_kprojid - Map a user-namespace projid pair into a kprojid. 514 * @ns: User namespace that the projid is in 515 * @projid: Project identifier 516 * 517 * Maps a user-namespace uid pair into a kernel internal kuid, 518 * and returns that kuid. 519 * 520 * When there is no mapping defined for the user-namespace projid 521 * pair INVALID_PROJID is returned. Callers are expected to test 522 * for and handle INVALID_PROJID being returned. INVALID_PROJID 523 * may be tested for using projid_valid(). 524 */ 525 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid) 526 { 527 /* Map the uid to a global kernel uid */ 528 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid)); 529 } 530 EXPORT_SYMBOL(make_kprojid); 531 532 /** 533 * from_kprojid - Create a projid from a kprojid user-namespace pair. 534 * @targ: The user namespace we want a projid in. 535 * @kprojid: The kernel internal project identifier to start with. 536 * 537 * Map @kprojid into the user-namespace specified by @targ and 538 * return the resulting projid. 539 * 540 * There is always a mapping into the initial user_namespace. 541 * 542 * If @kprojid has no mapping in @targ (projid_t)-1 is returned. 543 */ 544 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid) 545 { 546 /* Map the uid from a global kernel uid */ 547 return map_id_up(&targ->projid_map, __kprojid_val(kprojid)); 548 } 549 EXPORT_SYMBOL(from_kprojid); 550 551 /** 552 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair. 553 * @targ: The user namespace we want a projid in. 554 * @kprojid: The kernel internal projid to start with. 555 * 556 * Map @kprojid into the user-namespace specified by @targ and 557 * return the resulting projid. 558 * 559 * There is always a mapping into the initial user_namespace. 560 * 561 * Unlike from_kprojid from_kprojid_munged never fails and always 562 * returns a valid projid. This makes from_kprojid_munged 563 * appropriate for use in syscalls like stat and where 564 * failing the system call and failing to provide a valid projid are 565 * not an options. 566 * 567 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned. 568 */ 569 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid) 570 { 571 projid_t projid; 572 projid = from_kprojid(targ, kprojid); 573 574 if (projid == (projid_t) -1) 575 projid = OVERFLOW_PROJID; 576 return projid; 577 } 578 EXPORT_SYMBOL(from_kprojid_munged); 579 580 581 static int uid_m_show(struct seq_file *seq, void *v) 582 { 583 struct user_namespace *ns = seq->private; 584 struct uid_gid_extent *extent = v; 585 struct user_namespace *lower_ns; 586 uid_t lower; 587 588 lower_ns = seq_user_ns(seq); 589 if ((lower_ns == ns) && lower_ns->parent) 590 lower_ns = lower_ns->parent; 591 592 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first)); 593 594 seq_printf(seq, "%10u %10u %10u\n", 595 extent->first, 596 lower, 597 extent->count); 598 599 return 0; 600 } 601 602 static int gid_m_show(struct seq_file *seq, void *v) 603 { 604 struct user_namespace *ns = seq->private; 605 struct uid_gid_extent *extent = v; 606 struct user_namespace *lower_ns; 607 gid_t lower; 608 609 lower_ns = seq_user_ns(seq); 610 if ((lower_ns == ns) && lower_ns->parent) 611 lower_ns = lower_ns->parent; 612 613 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first)); 614 615 seq_printf(seq, "%10u %10u %10u\n", 616 extent->first, 617 lower, 618 extent->count); 619 620 return 0; 621 } 622 623 static int projid_m_show(struct seq_file *seq, void *v) 624 { 625 struct user_namespace *ns = seq->private; 626 struct uid_gid_extent *extent = v; 627 struct user_namespace *lower_ns; 628 projid_t lower; 629 630 lower_ns = seq_user_ns(seq); 631 if ((lower_ns == ns) && lower_ns->parent) 632 lower_ns = lower_ns->parent; 633 634 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first)); 635 636 seq_printf(seq, "%10u %10u %10u\n", 637 extent->first, 638 lower, 639 extent->count); 640 641 return 0; 642 } 643 644 static void *m_start(struct seq_file *seq, loff_t *ppos, 645 struct uid_gid_map *map) 646 { 647 loff_t pos = *ppos; 648 unsigned extents = map->nr_extents; 649 smp_rmb(); 650 651 if (pos >= extents) 652 return NULL; 653 654 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 655 return &map->extent[pos]; 656 657 return &map->forward[pos]; 658 } 659 660 static void *uid_m_start(struct seq_file *seq, loff_t *ppos) 661 { 662 struct user_namespace *ns = seq->private; 663 664 return m_start(seq, ppos, &ns->uid_map); 665 } 666 667 static void *gid_m_start(struct seq_file *seq, loff_t *ppos) 668 { 669 struct user_namespace *ns = seq->private; 670 671 return m_start(seq, ppos, &ns->gid_map); 672 } 673 674 static void *projid_m_start(struct seq_file *seq, loff_t *ppos) 675 { 676 struct user_namespace *ns = seq->private; 677 678 return m_start(seq, ppos, &ns->projid_map); 679 } 680 681 static void *m_next(struct seq_file *seq, void *v, loff_t *pos) 682 { 683 (*pos)++; 684 return seq->op->start(seq, pos); 685 } 686 687 static void m_stop(struct seq_file *seq, void *v) 688 { 689 return; 690 } 691 692 const struct seq_operations proc_uid_seq_operations = { 693 .start = uid_m_start, 694 .stop = m_stop, 695 .next = m_next, 696 .show = uid_m_show, 697 }; 698 699 const struct seq_operations proc_gid_seq_operations = { 700 .start = gid_m_start, 701 .stop = m_stop, 702 .next = m_next, 703 .show = gid_m_show, 704 }; 705 706 const struct seq_operations proc_projid_seq_operations = { 707 .start = projid_m_start, 708 .stop = m_stop, 709 .next = m_next, 710 .show = projid_m_show, 711 }; 712 713 static bool mappings_overlap(struct uid_gid_map *new_map, 714 struct uid_gid_extent *extent) 715 { 716 u32 upper_first, lower_first, upper_last, lower_last; 717 unsigned idx; 718 719 upper_first = extent->first; 720 lower_first = extent->lower_first; 721 upper_last = upper_first + extent->count - 1; 722 lower_last = lower_first + extent->count - 1; 723 724 for (idx = 0; idx < new_map->nr_extents; idx++) { 725 u32 prev_upper_first, prev_lower_first; 726 u32 prev_upper_last, prev_lower_last; 727 struct uid_gid_extent *prev; 728 729 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 730 prev = &new_map->extent[idx]; 731 else 732 prev = &new_map->forward[idx]; 733 734 prev_upper_first = prev->first; 735 prev_lower_first = prev->lower_first; 736 prev_upper_last = prev_upper_first + prev->count - 1; 737 prev_lower_last = prev_lower_first + prev->count - 1; 738 739 /* Does the upper range intersect a previous extent? */ 740 if ((prev_upper_first <= upper_last) && 741 (prev_upper_last >= upper_first)) 742 return true; 743 744 /* Does the lower range intersect a previous extent? */ 745 if ((prev_lower_first <= lower_last) && 746 (prev_lower_last >= lower_first)) 747 return true; 748 } 749 return false; 750 } 751 752 /** 753 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map. 754 * Takes care to allocate a 4K block of memory if the number of mappings exceeds 755 * UID_GID_MAP_MAX_BASE_EXTENTS. 756 */ 757 static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent) 758 { 759 struct uid_gid_extent *dest; 760 761 if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) { 762 struct uid_gid_extent *forward; 763 764 /* Allocate memory for 340 mappings. */ 765 forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS, 766 sizeof(struct uid_gid_extent), 767 GFP_KERNEL); 768 if (!forward) 769 return -ENOMEM; 770 771 /* Copy over memory. Only set up memory for the forward pointer. 772 * Defer the memory setup for the reverse pointer. 773 */ 774 memcpy(forward, map->extent, 775 map->nr_extents * sizeof(map->extent[0])); 776 777 map->forward = forward; 778 map->reverse = NULL; 779 } 780 781 if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS) 782 dest = &map->extent[map->nr_extents]; 783 else 784 dest = &map->forward[map->nr_extents]; 785 786 *dest = *extent; 787 map->nr_extents++; 788 return 0; 789 } 790 791 /* cmp function to sort() forward mappings */ 792 static int cmp_extents_forward(const void *a, const void *b) 793 { 794 const struct uid_gid_extent *e1 = a; 795 const struct uid_gid_extent *e2 = b; 796 797 if (e1->first < e2->first) 798 return -1; 799 800 if (e1->first > e2->first) 801 return 1; 802 803 return 0; 804 } 805 806 /* cmp function to sort() reverse mappings */ 807 static int cmp_extents_reverse(const void *a, const void *b) 808 { 809 const struct uid_gid_extent *e1 = a; 810 const struct uid_gid_extent *e2 = b; 811 812 if (e1->lower_first < e2->lower_first) 813 return -1; 814 815 if (e1->lower_first > e2->lower_first) 816 return 1; 817 818 return 0; 819 } 820 821 /** 822 * sort_idmaps - Sorts an array of idmap entries. 823 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. 824 */ 825 static int sort_idmaps(struct uid_gid_map *map) 826 { 827 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 828 return 0; 829 830 /* Sort forward array. */ 831 sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent), 832 cmp_extents_forward, NULL); 833 834 /* Only copy the memory from forward we actually need. */ 835 map->reverse = kmemdup(map->forward, 836 map->nr_extents * sizeof(struct uid_gid_extent), 837 GFP_KERNEL); 838 if (!map->reverse) 839 return -ENOMEM; 840 841 /* Sort reverse array. */ 842 sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent), 843 cmp_extents_reverse, NULL); 844 845 return 0; 846 } 847 848 /** 849 * verify_root_map() - check the uid 0 mapping 850 * @file: idmapping file 851 * @map_ns: user namespace of the target process 852 * @new_map: requested idmap 853 * 854 * If a process requests mapping parent uid 0 into the new ns, verify that the 855 * process writing the map had the CAP_SETFCAP capability as the target process 856 * will be able to write fscaps that are valid in ancestor user namespaces. 857 * 858 * Return: true if the mapping is allowed, false if not. 859 */ 860 static bool verify_root_map(const struct file *file, 861 struct user_namespace *map_ns, 862 struct uid_gid_map *new_map) 863 { 864 int idx; 865 const struct user_namespace *file_ns = file->f_cred->user_ns; 866 struct uid_gid_extent *extent0 = NULL; 867 868 for (idx = 0; idx < new_map->nr_extents; idx++) { 869 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 870 extent0 = &new_map->extent[idx]; 871 else 872 extent0 = &new_map->forward[idx]; 873 if (extent0->lower_first == 0) 874 break; 875 876 extent0 = NULL; 877 } 878 879 if (!extent0) 880 return true; 881 882 if (map_ns == file_ns) { 883 /* The process unshared its ns and is writing to its own 884 * /proc/self/uid_map. User already has full capabilites in 885 * the new namespace. Verify that the parent had CAP_SETFCAP 886 * when it unshared. 887 * */ 888 if (!file_ns->parent_could_setfcap) 889 return false; 890 } else { 891 /* Process p1 is writing to uid_map of p2, who is in a child 892 * user namespace to p1's. Verify that the opener of the map 893 * file has CAP_SETFCAP against the parent of the new map 894 * namespace */ 895 if (!file_ns_capable(file, map_ns->parent, CAP_SETFCAP)) 896 return false; 897 } 898 899 return true; 900 } 901 902 static ssize_t map_write(struct file *file, const char __user *buf, 903 size_t count, loff_t *ppos, 904 int cap_setid, 905 struct uid_gid_map *map, 906 struct uid_gid_map *parent_map) 907 { 908 struct seq_file *seq = file->private_data; 909 struct user_namespace *map_ns = seq->private; 910 struct uid_gid_map new_map; 911 unsigned idx; 912 struct uid_gid_extent extent; 913 char *kbuf = NULL, *pos, *next_line; 914 ssize_t ret; 915 916 /* Only allow < page size writes at the beginning of the file */ 917 if ((*ppos != 0) || (count >= PAGE_SIZE)) 918 return -EINVAL; 919 920 /* Slurp in the user data */ 921 kbuf = memdup_user_nul(buf, count); 922 if (IS_ERR(kbuf)) 923 return PTR_ERR(kbuf); 924 925 /* 926 * The userns_state_mutex serializes all writes to any given map. 927 * 928 * Any map is only ever written once. 929 * 930 * An id map fits within 1 cache line on most architectures. 931 * 932 * On read nothing needs to be done unless you are on an 933 * architecture with a crazy cache coherency model like alpha. 934 * 935 * There is a one time data dependency between reading the 936 * count of the extents and the values of the extents. The 937 * desired behavior is to see the values of the extents that 938 * were written before the count of the extents. 939 * 940 * To achieve this smp_wmb() is used on guarantee the write 941 * order and smp_rmb() is guaranteed that we don't have crazy 942 * architectures returning stale data. 943 */ 944 mutex_lock(&userns_state_mutex); 945 946 memset(&new_map, 0, sizeof(struct uid_gid_map)); 947 948 ret = -EPERM; 949 /* Only allow one successful write to the map */ 950 if (map->nr_extents != 0) 951 goto out; 952 953 /* 954 * Adjusting namespace settings requires capabilities on the target. 955 */ 956 if (cap_valid(cap_setid) && !file_ns_capable(file, map_ns, CAP_SYS_ADMIN)) 957 goto out; 958 959 /* Parse the user data */ 960 ret = -EINVAL; 961 pos = kbuf; 962 for (; pos; pos = next_line) { 963 964 /* Find the end of line and ensure I don't look past it */ 965 next_line = strchr(pos, '\n'); 966 if (next_line) { 967 *next_line = '\0'; 968 next_line++; 969 if (*next_line == '\0') 970 next_line = NULL; 971 } 972 973 pos = skip_spaces(pos); 974 extent.first = simple_strtoul(pos, &pos, 10); 975 if (!isspace(*pos)) 976 goto out; 977 978 pos = skip_spaces(pos); 979 extent.lower_first = simple_strtoul(pos, &pos, 10); 980 if (!isspace(*pos)) 981 goto out; 982 983 pos = skip_spaces(pos); 984 extent.count = simple_strtoul(pos, &pos, 10); 985 if (*pos && !isspace(*pos)) 986 goto out; 987 988 /* Verify there is not trailing junk on the line */ 989 pos = skip_spaces(pos); 990 if (*pos != '\0') 991 goto out; 992 993 /* Verify we have been given valid starting values */ 994 if ((extent.first == (u32) -1) || 995 (extent.lower_first == (u32) -1)) 996 goto out; 997 998 /* Verify count is not zero and does not cause the 999 * extent to wrap 1000 */ 1001 if ((extent.first + extent.count) <= extent.first) 1002 goto out; 1003 if ((extent.lower_first + extent.count) <= 1004 extent.lower_first) 1005 goto out; 1006 1007 /* Do the ranges in extent overlap any previous extents? */ 1008 if (mappings_overlap(&new_map, &extent)) 1009 goto out; 1010 1011 if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS && 1012 (next_line != NULL)) 1013 goto out; 1014 1015 ret = insert_extent(&new_map, &extent); 1016 if (ret < 0) 1017 goto out; 1018 ret = -EINVAL; 1019 } 1020 /* Be very certaint the new map actually exists */ 1021 if (new_map.nr_extents == 0) 1022 goto out; 1023 1024 ret = -EPERM; 1025 /* Validate the user is allowed to use user id's mapped to. */ 1026 if (!new_idmap_permitted(file, map_ns, cap_setid, &new_map)) 1027 goto out; 1028 1029 ret = -EPERM; 1030 /* Map the lower ids from the parent user namespace to the 1031 * kernel global id space. 1032 */ 1033 for (idx = 0; idx < new_map.nr_extents; idx++) { 1034 struct uid_gid_extent *e; 1035 u32 lower_first; 1036 1037 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) 1038 e = &new_map.extent[idx]; 1039 else 1040 e = &new_map.forward[idx]; 1041 1042 lower_first = map_id_range_down(parent_map, 1043 e->lower_first, 1044 e->count); 1045 1046 /* Fail if we can not map the specified extent to 1047 * the kernel global id space. 1048 */ 1049 if (lower_first == (u32) -1) 1050 goto out; 1051 1052 e->lower_first = lower_first; 1053 } 1054 1055 /* 1056 * If we want to use binary search for lookup, this clones the extent 1057 * array and sorts both copies. 1058 */ 1059 ret = sort_idmaps(&new_map); 1060 if (ret < 0) 1061 goto out; 1062 1063 /* Install the map */ 1064 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) { 1065 memcpy(map->extent, new_map.extent, 1066 new_map.nr_extents * sizeof(new_map.extent[0])); 1067 } else { 1068 map->forward = new_map.forward; 1069 map->reverse = new_map.reverse; 1070 } 1071 smp_wmb(); 1072 map->nr_extents = new_map.nr_extents; 1073 1074 *ppos = count; 1075 ret = count; 1076 out: 1077 if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { 1078 kfree(new_map.forward); 1079 kfree(new_map.reverse); 1080 map->forward = NULL; 1081 map->reverse = NULL; 1082 map->nr_extents = 0; 1083 } 1084 1085 mutex_unlock(&userns_state_mutex); 1086 kfree(kbuf); 1087 return ret; 1088 } 1089 1090 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, 1091 size_t size, loff_t *ppos) 1092 { 1093 struct seq_file *seq = file->private_data; 1094 struct user_namespace *ns = seq->private; 1095 struct user_namespace *seq_ns = seq_user_ns(seq); 1096 1097 if (!ns->parent) 1098 return -EPERM; 1099 1100 if ((seq_ns != ns) && (seq_ns != ns->parent)) 1101 return -EPERM; 1102 1103 return map_write(file, buf, size, ppos, CAP_SETUID, 1104 &ns->uid_map, &ns->parent->uid_map); 1105 } 1106 1107 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, 1108 size_t size, loff_t *ppos) 1109 { 1110 struct seq_file *seq = file->private_data; 1111 struct user_namespace *ns = seq->private; 1112 struct user_namespace *seq_ns = seq_user_ns(seq); 1113 1114 if (!ns->parent) 1115 return -EPERM; 1116 1117 if ((seq_ns != ns) && (seq_ns != ns->parent)) 1118 return -EPERM; 1119 1120 return map_write(file, buf, size, ppos, CAP_SETGID, 1121 &ns->gid_map, &ns->parent->gid_map); 1122 } 1123 1124 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, 1125 size_t size, loff_t *ppos) 1126 { 1127 struct seq_file *seq = file->private_data; 1128 struct user_namespace *ns = seq->private; 1129 struct user_namespace *seq_ns = seq_user_ns(seq); 1130 1131 if (!ns->parent) 1132 return -EPERM; 1133 1134 if ((seq_ns != ns) && (seq_ns != ns->parent)) 1135 return -EPERM; 1136 1137 /* Anyone can set any valid project id no capability needed */ 1138 return map_write(file, buf, size, ppos, -1, 1139 &ns->projid_map, &ns->parent->projid_map); 1140 } 1141 1142 static bool new_idmap_permitted(const struct file *file, 1143 struct user_namespace *ns, int cap_setid, 1144 struct uid_gid_map *new_map) 1145 { 1146 const struct cred *cred = file->f_cred; 1147 1148 if (cap_setid == CAP_SETUID && !verify_root_map(file, ns, new_map)) 1149 return false; 1150 1151 /* Don't allow mappings that would allow anything that wouldn't 1152 * be allowed without the establishment of unprivileged mappings. 1153 */ 1154 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) && 1155 uid_eq(ns->owner, cred->euid)) { 1156 u32 id = new_map->extent[0].lower_first; 1157 if (cap_setid == CAP_SETUID) { 1158 kuid_t uid = make_kuid(ns->parent, id); 1159 if (uid_eq(uid, cred->euid)) 1160 return true; 1161 } else if (cap_setid == CAP_SETGID) { 1162 kgid_t gid = make_kgid(ns->parent, id); 1163 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) && 1164 gid_eq(gid, cred->egid)) 1165 return true; 1166 } 1167 } 1168 1169 /* Allow anyone to set a mapping that doesn't require privilege */ 1170 if (!cap_valid(cap_setid)) 1171 return true; 1172 1173 /* Allow the specified ids if we have the appropriate capability 1174 * (CAP_SETUID or CAP_SETGID) over the parent user namespace. 1175 * And the opener of the id file also had the approprpiate capability. 1176 */ 1177 if (ns_capable(ns->parent, cap_setid) && 1178 file_ns_capable(file, ns->parent, cap_setid)) 1179 return true; 1180 1181 return false; 1182 } 1183 1184 int proc_setgroups_show(struct seq_file *seq, void *v) 1185 { 1186 struct user_namespace *ns = seq->private; 1187 unsigned long userns_flags = READ_ONCE(ns->flags); 1188 1189 seq_printf(seq, "%s\n", 1190 (userns_flags & USERNS_SETGROUPS_ALLOWED) ? 1191 "allow" : "deny"); 1192 return 0; 1193 } 1194 1195 ssize_t proc_setgroups_write(struct file *file, const char __user *buf, 1196 size_t count, loff_t *ppos) 1197 { 1198 struct seq_file *seq = file->private_data; 1199 struct user_namespace *ns = seq->private; 1200 char kbuf[8], *pos; 1201 bool setgroups_allowed; 1202 ssize_t ret; 1203 1204 /* Only allow a very narrow range of strings to be written */ 1205 ret = -EINVAL; 1206 if ((*ppos != 0) || (count >= sizeof(kbuf))) 1207 goto out; 1208 1209 /* What was written? */ 1210 ret = -EFAULT; 1211 if (copy_from_user(kbuf, buf, count)) 1212 goto out; 1213 kbuf[count] = '\0'; 1214 pos = kbuf; 1215 1216 /* What is being requested? */ 1217 ret = -EINVAL; 1218 if (strncmp(pos, "allow", 5) == 0) { 1219 pos += 5; 1220 setgroups_allowed = true; 1221 } 1222 else if (strncmp(pos, "deny", 4) == 0) { 1223 pos += 4; 1224 setgroups_allowed = false; 1225 } 1226 else 1227 goto out; 1228 1229 /* Verify there is not trailing junk on the line */ 1230 pos = skip_spaces(pos); 1231 if (*pos != '\0') 1232 goto out; 1233 1234 ret = -EPERM; 1235 mutex_lock(&userns_state_mutex); 1236 if (setgroups_allowed) { 1237 /* Enabling setgroups after setgroups has been disabled 1238 * is not allowed. 1239 */ 1240 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED)) 1241 goto out_unlock; 1242 } else { 1243 /* Permanently disabling setgroups after setgroups has 1244 * been enabled by writing the gid_map is not allowed. 1245 */ 1246 if (ns->gid_map.nr_extents != 0) 1247 goto out_unlock; 1248 ns->flags &= ~USERNS_SETGROUPS_ALLOWED; 1249 } 1250 mutex_unlock(&userns_state_mutex); 1251 1252 /* Report a successful write */ 1253 *ppos = count; 1254 ret = count; 1255 out: 1256 return ret; 1257 out_unlock: 1258 mutex_unlock(&userns_state_mutex); 1259 goto out; 1260 } 1261 1262 bool userns_may_setgroups(const struct user_namespace *ns) 1263 { 1264 bool allowed; 1265 1266 mutex_lock(&userns_state_mutex); 1267 /* It is not safe to use setgroups until a gid mapping in 1268 * the user namespace has been established. 1269 */ 1270 allowed = ns->gid_map.nr_extents != 0; 1271 /* Is setgroups allowed? */ 1272 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); 1273 mutex_unlock(&userns_state_mutex); 1274 1275 return allowed; 1276 } 1277 1278 /* 1279 * Returns true if @child is the same namespace or a descendant of 1280 * @ancestor. 1281 */ 1282 bool in_userns(const struct user_namespace *ancestor, 1283 const struct user_namespace *child) 1284 { 1285 const struct user_namespace *ns; 1286 for (ns = child; ns->level > ancestor->level; ns = ns->parent) 1287 ; 1288 return (ns == ancestor); 1289 } 1290 1291 bool current_in_userns(const struct user_namespace *target_ns) 1292 { 1293 return in_userns(target_ns, current_user_ns()); 1294 } 1295 EXPORT_SYMBOL(current_in_userns); 1296 1297 static inline struct user_namespace *to_user_ns(struct ns_common *ns) 1298 { 1299 return container_of(ns, struct user_namespace, ns); 1300 } 1301 1302 static struct ns_common *userns_get(struct task_struct *task) 1303 { 1304 struct user_namespace *user_ns; 1305 1306 rcu_read_lock(); 1307 user_ns = get_user_ns(__task_cred(task)->user_ns); 1308 rcu_read_unlock(); 1309 1310 return user_ns ? &user_ns->ns : NULL; 1311 } 1312 1313 static void userns_put(struct ns_common *ns) 1314 { 1315 put_user_ns(to_user_ns(ns)); 1316 } 1317 1318 static int userns_install(struct nsset *nsset, struct ns_common *ns) 1319 { 1320 struct user_namespace *user_ns = to_user_ns(ns); 1321 struct cred *cred; 1322 1323 /* Don't allow gaining capabilities by reentering 1324 * the same user namespace. 1325 */ 1326 if (user_ns == current_user_ns()) 1327 return -EINVAL; 1328 1329 /* Tasks that share a thread group must share a user namespace */ 1330 if (!thread_group_empty(current)) 1331 return -EINVAL; 1332 1333 if (current->fs->users != 1) 1334 return -EINVAL; 1335 1336 if (!ns_capable(user_ns, CAP_SYS_ADMIN)) 1337 return -EPERM; 1338 1339 cred = nsset_cred(nsset); 1340 if (!cred) 1341 return -EINVAL; 1342 1343 put_user_ns(cred->user_ns); 1344 set_cred_user_ns(cred, get_user_ns(user_ns)); 1345 1346 if (set_cred_ucounts(cred) < 0) 1347 return -EINVAL; 1348 1349 return 0; 1350 } 1351 1352 struct ns_common *ns_get_owner(struct ns_common *ns) 1353 { 1354 struct user_namespace *my_user_ns = current_user_ns(); 1355 struct user_namespace *owner, *p; 1356 1357 /* See if the owner is in the current user namespace */ 1358 owner = p = ns->ops->owner(ns); 1359 for (;;) { 1360 if (!p) 1361 return ERR_PTR(-EPERM); 1362 if (p == my_user_ns) 1363 break; 1364 p = p->parent; 1365 } 1366 1367 return &get_user_ns(owner)->ns; 1368 } 1369 1370 static struct user_namespace *userns_owner(struct ns_common *ns) 1371 { 1372 return to_user_ns(ns)->parent; 1373 } 1374 1375 const struct proc_ns_operations userns_operations = { 1376 .name = "user", 1377 .type = CLONE_NEWUSER, 1378 .get = userns_get, 1379 .put = userns_put, 1380 .install = userns_install, 1381 .owner = userns_owner, 1382 .get_parent = ns_get_owner, 1383 }; 1384 1385 static __init int user_namespaces_init(void) 1386 { 1387 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); 1388 return 0; 1389 } 1390 subsys_initcall(user_namespaces_init); 1391