diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/src/utils.c b/src/utils.c index 69fcb0e..945f8a5 100644 --- a/src/utils.c +++ b/src/utils.c @@ -387,20 +387,55 @@ int create_device_files(const char *mount_path) return s; } -int update_uid_map(pid_t pid, unsigned int uid_map[3]) +int update_guid_map(pid_t pid, unsigned int map[3], int update_uidmap) { - int s; - const char *const path_pid = "/proc/%d/uid_map"; - const char *const path_self = "/proc/self/uid_map"; - char path[32]; + int s, fd; + ssize_t written; + const char *const path_pid = "/proc/%d/%s"; + const char *const path_self = "/proc/self/%s"; + char buf[64]; if (pid < 0) { - s = snprintf(path, sizeof path, "%s", path_self); + s = snprintf(buf, sizeof buf, path_self, + (update_uidmap ? "uid_map" : "gid_map")); } else { - s = snprintf(path, sizeof path, path_pid, pid); + s = snprintf(buf, sizeof buf, path_pid, pid, + (update_uidmap ? "uid_map" : "gid_map")); } if (s <= 0) return 1; + fd = open(buf, O_WRONLY); + if (fd < 0) + return 1; + + s = snprintf(buf, sizeof buf, "%u %u %u\n", map[0], map[1], map[2]); + written = write(fd, buf, s); + if (written <= 0) + return 1; + + return 0; +} + +int update_setgroups_self(int allow) +{ + int fd; + ssize_t written; + const char *const path_self = "/proc/self/setgroups"; + const char *const str_allow = "allow"; + const char *const str_deny = "deny"; + + fd = open(path_self, O_WRONLY); + if (fd < 0) + return 1; + + if (allow) { + written = write(fd, str_allow, sizeof str_allow); + } else { + written = write(fd, str_deny, sizeof str_deny); + } + if (written <= 0) + return 1; + return 0; } |