1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/nsfs.h>
#include "../kselftest_harness.h"
#include "../filesystems/utils.h"
#include "wrappers.h"
/*
* Stress tests for namespace active reference counting.
*
* These tests validate that the active reference counting system can handle
* high load scenarios including rapid namespace creation/destruction, large
* numbers of concurrent namespaces, and various edge cases under stress.
*/
/*
* Test rapid creation and destruction of user namespaces.
* Create and destroy namespaces in quick succession to stress the
* active reference tracking and ensure no leaks occur.
*/
TEST(rapid_namespace_creation_destruction)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = CLONE_NEWUSER,
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[256], ns_ids_after[256];
ssize_t ret_before, ret_after;
int i;
/* Get baseline count of active user namespaces */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active user namespaces", ret_before);
/* Rapidly create and destroy 100 user namespaces */
for (i = 0; i < 100; i++) {
pid_t pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child: create user namespace and immediately exit */
if (setup_userns() < 0)
exit(1);
exit(0);
}
/* Parent: wait for child */
int status;
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
}
/* Verify we're back to baseline (no leaked namespaces) */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After 100 rapid create/destroy cycles: %zd active user namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
/*
* Test creating many concurrent namespaces.
* Verify that listns() correctly tracks all of them and that they all
* become inactive after processes exit.
*/
TEST(many_concurrent_namespaces)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = CLONE_NEWUSER,
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[512], ns_ids_during[512], ns_ids_after[512];
ssize_t ret_before, ret_during, ret_after;
pid_t pids[50];
int num_children = 50;
int i;
int sv[2];
/* Get baseline */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active user namespaces", ret_before);
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
/* Create many children, each with their own user namespace */
for (i = 0; i < num_children; i++) {
pids[i] = fork();
ASSERT_GE(pids[i], 0);
if (pids[i] == 0) {
/* Child: create user namespace and wait for parent signal */
char c;
close(sv[0]);
if (setup_userns() < 0) {
close(sv[1]);
exit(1);
}
/* Signal parent we're ready */
if (write(sv[1], &c, 1) != 1) {
close(sv[1]);
exit(1);
}
/* Wait for parent signal to exit */
if (read(sv[1], &c, 1) != 1) {
close(sv[1]);
exit(1);
}
close(sv[1]);
exit(0);
}
}
close(sv[1]);
/* Wait for all children to signal ready */
for (i = 0; i < num_children; i++) {
char c;
if (read(sv[0], &c, 1) != 1) {
/* If we fail to read, kill all children and exit */
close(sv[0]);
for (int j = 0; j < num_children; j++)
kill(pids[j], SIGKILL);
for (int j = 0; j < num_children; j++)
waitpid(pids[j], NULL, 0);
ASSERT_TRUE(false);
}
}
/* List namespaces while all children are running */
ret_during = sys_listns(&req, ns_ids_during, ARRAY_SIZE(ns_ids_during), 0);
ASSERT_GE(ret_during, 0);
TH_LOG("With %d children running: %zd active user namespaces", num_children, ret_during);
/* Should have at least num_children more namespaces than baseline */
ASSERT_GE(ret_during, ret_before + num_children);
/* Signal all children to exit */
for (i = 0; i < num_children; i++) {
char c = 'X';
if (write(sv[0], &c, 1) != 1) {
/* If we fail to write, kill remaining children */
close(sv[0]);
for (int j = i; j < num_children; j++)
kill(pids[j], SIGKILL);
for (int j = 0; j < num_children; j++)
waitpid(pids[j], NULL, 0);
ASSERT_TRUE(false);
}
}
close(sv[0]);
/* Wait for all children */
for (i = 0; i < num_children; i++) {
int status;
waitpid(pids[i], &status, 0);
ASSERT_TRUE(WIFEXITED(status));
}
/* Verify we're back to baseline */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After all children exit: %zd active user namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
/*
* Test rapid namespace creation with different namespace types.
* Create multiple types of namespaces rapidly to stress the tracking system.
*/
TEST(rapid_mixed_namespace_creation)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = 0, /* All types */
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[512], ns_ids_after[512];
ssize_t ret_before, ret_after;
int i;
/* Get baseline count */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active namespaces (all types)", ret_before);
/* Rapidly create and destroy namespaces with multiple types */
for (i = 0; i < 50; i++) {
pid_t pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child: create multiple namespace types */
if (setup_userns() < 0)
exit(1);
/* Create additional namespace types */
if (unshare(CLONE_NEWNET) < 0)
exit(1);
if (unshare(CLONE_NEWUTS) < 0)
exit(1);
if (unshare(CLONE_NEWIPC) < 0)
exit(1);
exit(0);
}
/* Parent: wait for child */
int status;
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
}
/* Verify we're back to baseline */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After 50 rapid mixed namespace cycles: %zd active namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
/*
* Test nested namespace creation under stress.
* Create deeply nested namespace hierarchies and verify proper cleanup.
*/
TEST(nested_namespace_stress)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = CLONE_NEWUSER,
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[512], ns_ids_after[512];
ssize_t ret_before, ret_after;
int i;
/* Get baseline */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active user namespaces", ret_before);
/* Create 20 processes, each with nested user namespaces */
for (i = 0; i < 20; i++) {
pid_t pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
int userns_fd;
uid_t orig_uid = getuid();
int depth;
/* Create nested user namespaces (up to 5 levels) */
for (depth = 0; depth < 5; depth++) {
userns_fd = get_userns_fd(0, (depth == 0) ? orig_uid : 0, 1);
if (userns_fd < 0)
exit(1);
if (setns(userns_fd, CLONE_NEWUSER) < 0) {
close(userns_fd);
exit(1);
}
close(userns_fd);
}
exit(0);
}
/* Parent: wait for child */
int status;
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
}
/* Verify we're back to baseline */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After 20 nested namespace hierarchies: %zd active user namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
/*
* Test listns() pagination under stress.
* Create many namespaces and verify pagination works correctly.
*/
TEST(listns_pagination_stress)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = CLONE_NEWUSER,
.spare2 = 0,
.user_ns_id = 0,
};
pid_t pids[30];
int num_children = 30;
int i;
int sv[2];
__u64 all_ns_ids[512];
int total_found = 0;
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
/* Create many children with user namespaces */
for (i = 0; i < num_children; i++) {
pids[i] = fork();
ASSERT_GE(pids[i], 0);
if (pids[i] == 0) {
char c;
close(sv[0]);
if (setup_userns() < 0) {
close(sv[1]);
exit(1);
}
/* Signal parent we're ready */
if (write(sv[1], &c, 1) != 1) {
close(sv[1]);
exit(1);
}
/* Wait for parent signal to exit */
if (read(sv[1], &c, 1) != 1) {
close(sv[1]);
exit(1);
}
close(sv[1]);
exit(0);
}
}
close(sv[1]);
/* Wait for all children to signal ready */
for (i = 0; i < num_children; i++) {
char c;
if (read(sv[0], &c, 1) != 1) {
/* If we fail to read, kill all children and exit */
close(sv[0]);
for (int j = 0; j < num_children; j++)
kill(pids[j], SIGKILL);
for (int j = 0; j < num_children; j++)
waitpid(pids[j], NULL, 0);
ASSERT_TRUE(false);
}
}
/* Paginate through all namespaces using small batch sizes */
req.ns_id = 0;
while (1) {
__u64 batch[5]; /* Small batch size to force pagination */
ssize_t ret;
ret = sys_listns(&req, batch, ARRAY_SIZE(batch), 0);
if (ret < 0) {
if (errno == ENOSYS) {
close(sv[0]);
for (i = 0; i < num_children; i++)
kill(pids[i], SIGKILL);
for (i = 0; i < num_children; i++)
waitpid(pids[i], NULL, 0);
SKIP(return, "listns() not supported");
}
ASSERT_GE(ret, 0);
}
if (ret == 0)
break;
/* Store results */
for (i = 0; i < ret && total_found < 512; i++) {
all_ns_ids[total_found++] = batch[i];
}
/* Update cursor for next batch */
if (ret == ARRAY_SIZE(batch))
req.ns_id = batch[ret - 1];
else
break;
}
TH_LOG("Paginated through %d user namespaces", total_found);
/* Verify no duplicates in pagination */
for (i = 0; i < total_found; i++) {
for (int j = i + 1; j < total_found; j++) {
if (all_ns_ids[i] == all_ns_ids[j]) {
TH_LOG("Found duplicate ns_id: %llu at positions %d and %d",
(unsigned long long)all_ns_ids[i], i, j);
ASSERT_TRUE(false);
}
}
}
/* Signal all children to exit */
for (i = 0; i < num_children; i++) {
char c = 'X';
if (write(sv[0], &c, 1) != 1) {
close(sv[0]);
for (int j = i; j < num_children; j++)
kill(pids[j], SIGKILL);
for (int j = 0; j < num_children; j++)
waitpid(pids[j], NULL, 0);
ASSERT_TRUE(false);
}
}
close(sv[0]);
/* Wait for all children */
for (i = 0; i < num_children; i++) {
int status;
waitpid(pids[i], &status, 0);
}
}
/*
* Test concurrent namespace operations.
* Multiple processes creating, querying, and destroying namespaces concurrently.
*/
TEST(concurrent_namespace_operations)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = 0,
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[512], ns_ids_after[512];
ssize_t ret_before, ret_after;
pid_t pids[20];
int num_workers = 20;
int i;
/* Get baseline */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active namespaces", ret_before);
/* Create worker processes that do concurrent operations */
for (i = 0; i < num_workers; i++) {
pids[i] = fork();
ASSERT_GE(pids[i], 0);
if (pids[i] == 0) {
/* Each worker: create namespaces, list them, repeat */
int iterations;
for (iterations = 0; iterations < 10; iterations++) {
int userns_fd;
__u64 temp_ns_ids[100];
ssize_t ret;
/* Create a user namespace */
userns_fd = get_userns_fd(0, getuid(), 1);
if (userns_fd < 0)
continue;
/* List namespaces */
ret = sys_listns(&req, temp_ns_ids, ARRAY_SIZE(temp_ns_ids), 0);
(void)ret;
close(userns_fd);
/* Small delay */
usleep(1000);
}
exit(0);
}
}
/* Wait for all workers */
for (i = 0; i < num_workers; i++) {
int status;
waitpid(pids[i], &status, 0);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
}
/* Verify we're back to baseline */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After concurrent operations: %zd active namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
/*
* Test namespace churn - continuous creation and destruction.
* Simulates high-churn scenarios like container orchestration.
*/
TEST(namespace_churn)
{
struct ns_id_req req = {
.size = sizeof(req),
.spare = 0,
.ns_id = 0,
.ns_type = CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS,
.spare2 = 0,
.user_ns_id = 0,
};
__u64 ns_ids_before[512], ns_ids_after[512];
ssize_t ret_before, ret_after;
int cycle;
/* Get baseline */
ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
if (ret_before < 0) {
if (errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_GE(ret_before, 0);
}
TH_LOG("Baseline: %zd active namespaces", ret_before);
/* Simulate churn: batches of namespaces created and destroyed */
for (cycle = 0; cycle < 10; cycle++) {
pid_t batch_pids[10];
int i;
/* Create batch */
for (i = 0; i < 10; i++) {
batch_pids[i] = fork();
ASSERT_GE(batch_pids[i], 0);
if (batch_pids[i] == 0) {
/* Create multiple namespace types */
if (setup_userns() < 0)
exit(1);
if (unshare(CLONE_NEWNET) < 0)
exit(1);
if (unshare(CLONE_NEWUTS) < 0)
exit(1);
/* Keep namespaces alive briefly */
usleep(10000);
exit(0);
}
}
/* Wait for batch to complete */
for (i = 0; i < 10; i++) {
int status;
waitpid(batch_pids[i], &status, 0);
}
}
/* Verify we're back to baseline */
ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
ASSERT_GE(ret_after, 0);
TH_LOG("After 10 churn cycles (100 namespace sets): %zd active namespaces", ret_after);
ASSERT_EQ(ret_before, ret_after);
}
TEST_HARNESS_MAIN
|