teknoraver

mountbomb

Feb 25th, 2026 (edited)
6,505
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <sys/mount.h>
  9. #include <sys/stat.h>
  10. #include <sys/sysinfo.h>
  11.  
  12. #define DIR1 "/tmp/dir1_%d"
  13. #define DIR2 "/tmp/dir2_%d"
  14.  
  15. int main(void)
  16. {
  17.     /* unshare -rm: new mount + user namespace */
  18.     if (getuid() != 0 && unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1) {
  19.         perror("unshare");
  20.         return 1;
  21.     }
  22.  
  23.     int nprocs = get_nprocs();
  24.     int id = 0;
  25.  
  26.     for (int i = 1; i < nprocs; i++) {
  27.         pid_t pid = fork();
  28.         if (pid == -1) {
  29.             perror("fork");
  30.             return 1;
  31.         }
  32.         if (pid == 0) {
  33.             id = i;
  34.             break;
  35.         }
  36.     }
  37.  
  38.     char dir1[32], dir2[32];
  39.     snprintf(dir1, sizeof(dir1), DIR1, id);
  40.     snprintf(dir2, sizeof(dir2), DIR2, id);
  41.     mkdir(dir1, 0755);
  42.     mkdir(dir2, 0755);
  43.  
  44.     while (1) {
  45.         if (mount(dir1, dir2, NULL, MS_BIND, NULL) == -1) {
  46.             fprintf(stderr, "mount --bind %s %s: %s\n", dir1, dir2, strerror(errno));
  47.             return 1;
  48.         }
  49.  
  50.         if (umount(dir2) == -1) {
  51.             fprintf(stderr, "umount %s: %s\n", dir2, strerror(errno));
  52.             return 1;
  53.         }
  54.     }
  55.  
  56.     return 0;
  57. }
Advertisement