123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include <support/namespace.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sched.h>
- #include <stdio.h>
- #include <string.h>
- #include <support/check.h>
- #include <support/xunistd.h>
- #include <unistd.h>
- #ifdef CLONE_NEWUSER
- static void
- setup_uid_gid_mapping (uid_t original_uid, gid_t original_gid)
- {
- int fd = open64 ("/proc/self/uid_map", O_WRONLY);
- if (fd < 0)
- {
- printf ("warning: could not open /proc/self/uid_map: %m\n"
- "warning: file creation may not be possible\n");
- return;
- }
-
- char buf[100];
- int ret = snprintf (buf, sizeof (buf), "%llu %llu 1\n",
- (unsigned long long) original_uid,
- (unsigned long long) original_uid);
- TEST_VERIFY_EXIT (ret < sizeof (buf));
- xwrite (fd, buf, ret);
- xclose (fd);
-
- fd = open64 ("/proc/self/setgroups", O_WRONLY, 0);
- if (fd < 0)
- {
- if (errno != ENOENT)
- FAIL_EXIT1 ("open64 (\"/proc/self/setgroups\", 0x%x, 0%o): %m",
- O_WRONLY, 0);
-
- }
- else
- {
- xwrite (fd, "deny\n", strlen ("deny\n"));
- xclose (fd);
- }
-
- fd = xopen ("/proc/self/gid_map", O_WRONLY, 0);
- ret = snprintf (buf, sizeof (buf), "%llu %llu 1\n",
- (unsigned long long) original_gid,
- (unsigned long long) original_gid);
- TEST_VERIFY_EXIT (ret < sizeof (buf));
- xwrite (fd, buf, ret);
- xclose (fd);
- }
- #endif
- bool
- support_become_root (void)
- {
- #ifdef CLONE_NEWUSER
- uid_t original_uid = getuid ();
- gid_t original_gid = getgid ();
- if (unshare (CLONE_NEWUSER | CLONE_NEWNS) == 0)
- {
- setup_uid_gid_mapping (original_uid, original_gid);
-
- return true;
- }
- #endif
- if (setuid (0) != 0)
- {
- printf ("warning: could not become root outside namespace (%m)\n");
- return false;
- }
- return true;
- }
|