]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: improve get_ns_uid() and add get_ns_gid()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 9 Sep 2018 10:46:00 +0000 (12:46 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Sep 2018 22:44:56 +0000 (00:44 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgfsng.c
src/lxc/macro.h
src/lxc/utils.c
src/lxc/utils.h

index 1fe561498bad95d3639fcb7ca26c0258c3516b7e..c37456b92521eab6316f15f719308de8cecad950 100644 (file)
@@ -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;
index d44e2f9b1cc19b5b07c44d10f166e67eac67e2b5..c0a50371d728005ea644feaa56ca54d4f645e624 100644 (file)
@@ -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 */
index 9795b51b668a1978c803c10c672b43218dc216e8..b85383a42ae78ef33f3470aca170b2c38cfca510 100644 (file)
@@ -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);
index f2d802991f8f6d5597175acbb188e21704f5aa11..51cfe4c85f03a4c34dadda0ee5ff546190694e03 100644 (file)
@@ -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);