Skip to content

Commit

Permalink
utils: improve get_ns_uid() and add get_ns_gid()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Sep 30, 2018
1 parent 8f5dbd1 commit 2b3062a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lxc/cgroups/cgfsng.c
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/lxc/macro.h
Expand Up @@ -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 */
29 changes: 28 additions & 1 deletion src/lxc/utils.c
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/lxc/utils.h
Expand Up @@ -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);

Expand Down

0 comments on commit 2b3062a

Please sign in to comment.