From: Christian Brauner Date: Sun, 9 Sep 2018 10:46:00 +0000 (+0200) Subject: utils: improve get_ns_uid() and add get_ns_gid() X-Git-Tag: lxc-3.1.0~115^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b962868f4ca36b80dcc7bc4681586da0178eae0d;p=thirdparty%2Flxc.git utils: improve get_ns_uid() and add get_ns_gid() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 1fe561498..c37456b92 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1388,6 +1388,8 @@ static int chown_cgroup_wrapper(void *data) } destuid = get_ns_uid(arg->origuid); + if (destuid == LXC_INVALID_UID) + destuid = 0; for (i = 0; arg->hierarchies[i]; i++) { char *fullpath; diff --git a/src/lxc/macro.h b/src/lxc/macro.h index d44e2f9b1..c0a50371d 100644 --- a/src/lxc/macro.h +++ b/src/lxc/macro.h @@ -340,4 +340,7 @@ extern int __build_bug_on_failed; #define PTR_TO_INTMAX(p) ((intmax_t)((intptr_t)(p))) #define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u))) +#define LXC_INVALID_UID ((uid_t)-1) +#define LXC_INVALID_GID ((gid_t)-1) + #endif /* __LXC_MACRO_H */ diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 9795b51b6..b85383a42 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -544,7 +544,34 @@ uid_t get_ns_uid(uid_t orig) } } - nsid = 0; + nsid = LXC_INVALID_UID; + +found: + fclose(f); + free(line); + return nsid; +} + +gid_t get_ns_gid(gid_t orig) +{ + char *line = NULL; + size_t sz = 0; + gid_t nsid, hostid, range; + FILE *f = fopen("/proc/self/gid_map", "r"); + if (!f) + return 0; + + while (getline(&line, &sz, f) != -1) { + if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3) + continue; + + if (hostid <= orig && hostid + range > orig) { + nsid += orig - hostid; + goto found; + } + } + + nsid = LXC_INVALID_GID; found: fclose(f); diff --git a/src/lxc/utils.h b/src/lxc/utils.h index f2d802991..51cfe4c85 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -328,6 +328,10 @@ inline static bool am_host_unpriv(void) * parse /proc/self/uid_map to find what @orig maps to */ extern uid_t get_ns_uid(uid_t orig); +/* + * parse /proc/self/gid_map to find what @orig maps to + */ +extern gid_t get_ns_gid(gid_t orig); extern bool dir_exists(const char *path);