utils: improve get_ns_uid() and add get_ns_gid()

parent 0b26d75e
......@@ -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;
......
......@@ -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 */
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment