rlimits-per-userns.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Author: Alexey Gladkov <[email protected]>
  4. */
  5. #define _GNU_SOURCE
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <sys/time.h>
  9. #include <sys/resource.h>
  10. #include <sys/prctl.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sched.h>
  17. #include <signal.h>
  18. #include <limits.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. #include <err.h>
  22. #define NR_CHILDS 2
  23. static char *service_prog;
  24. static uid_t user = 60000;
  25. static uid_t group = 60000;
  26. static void setrlimit_nproc(rlim_t n)
  27. {
  28. pid_t pid = getpid();
  29. struct rlimit limit = {
  30. .rlim_cur = n,
  31. .rlim_max = n
  32. };
  33. warnx("(pid=%d): Setting RLIMIT_NPROC=%ld", pid, n);
  34. if (setrlimit(RLIMIT_NPROC, &limit) < 0)
  35. err(EXIT_FAILURE, "(pid=%d): setrlimit(RLIMIT_NPROC)", pid);
  36. }
  37. static pid_t fork_child(void)
  38. {
  39. pid_t pid = fork();
  40. if (pid < 0)
  41. err(EXIT_FAILURE, "fork");
  42. if (pid > 0)
  43. return pid;
  44. pid = getpid();
  45. warnx("(pid=%d): New process starting ...", pid);
  46. if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
  47. err(EXIT_FAILURE, "(pid=%d): prctl(PR_SET_PDEATHSIG)", pid);
  48. signal(SIGUSR1, SIG_DFL);
  49. warnx("(pid=%d): Changing to uid=%d, gid=%d", pid, user, group);
  50. if (setgid(group) < 0)
  51. err(EXIT_FAILURE, "(pid=%d): setgid(%d)", pid, group);
  52. if (setuid(user) < 0)
  53. err(EXIT_FAILURE, "(pid=%d): setuid(%d)", pid, user);
  54. warnx("(pid=%d): Service running ...", pid);
  55. warnx("(pid=%d): Unshare user namespace", pid);
  56. if (unshare(CLONE_NEWUSER) < 0)
  57. err(EXIT_FAILURE, "unshare(CLONE_NEWUSER)");
  58. char *const argv[] = { "service", NULL };
  59. char *const envp[] = { "I_AM_SERVICE=1", NULL };
  60. warnx("(pid=%d): Executing real service ...", pid);
  61. execve(service_prog, argv, envp);
  62. err(EXIT_FAILURE, "(pid=%d): execve", pid);
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. size_t i;
  67. pid_t child[NR_CHILDS];
  68. int wstatus[NR_CHILDS];
  69. int childs = NR_CHILDS;
  70. pid_t pid;
  71. if (getenv("I_AM_SERVICE")) {
  72. pause();
  73. exit(EXIT_SUCCESS);
  74. }
  75. service_prog = argv[0];
  76. pid = getpid();
  77. warnx("(pid=%d) Starting testcase", pid);
  78. /*
  79. * This rlimit is not a problem for root because it can be exceeded.
  80. */
  81. setrlimit_nproc(1);
  82. for (i = 0; i < NR_CHILDS; i++) {
  83. child[i] = fork_child();
  84. wstatus[i] = 0;
  85. usleep(250000);
  86. }
  87. while (1) {
  88. for (i = 0; i < NR_CHILDS; i++) {
  89. if (child[i] <= 0)
  90. continue;
  91. errno = 0;
  92. pid_t ret = waitpid(child[i], &wstatus[i], WNOHANG);
  93. if (!ret || (!WIFEXITED(wstatus[i]) && !WIFSIGNALED(wstatus[i])))
  94. continue;
  95. if (ret < 0 && errno != ECHILD)
  96. warn("(pid=%d): waitpid(%d)", pid, child[i]);
  97. child[i] *= -1;
  98. childs -= 1;
  99. }
  100. if (!childs)
  101. break;
  102. usleep(250000);
  103. for (i = 0; i < NR_CHILDS; i++) {
  104. if (child[i] <= 0)
  105. continue;
  106. kill(child[i], SIGUSR1);
  107. }
  108. }
  109. for (i = 0; i < NR_CHILDS; i++) {
  110. if (WIFEXITED(wstatus[i]))
  111. warnx("(pid=%d): pid %d exited, status=%d",
  112. pid, -child[i], WEXITSTATUS(wstatus[i]));
  113. else if (WIFSIGNALED(wstatus[i]))
  114. warnx("(pid=%d): pid %d killed by signal %d",
  115. pid, -child[i], WTERMSIG(wstatus[i]));
  116. if (WIFSIGNALED(wstatus[i]) && WTERMSIG(wstatus[i]) == SIGUSR1)
  117. continue;
  118. warnx("(pid=%d): Test failed", pid);
  119. exit(EXIT_FAILURE);
  120. }
  121. warnx("(pid=%d): Test passed", pid);
  122. exit(EXIT_SUCCESS);
  123. }