]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
better unprivileged detection
authorTycho Andersen <tycho@tycho.ws>
Fri, 26 Jan 2018 21:21:51 +0000 (21:21 +0000)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 6 Feb 2018 12:34:40 +0000 (13:34 +0100)
In particular, if we are already in a user namespace we are unprivileged,
and doing things like moving the physical nics back to the host netns won't
work. Let's do the same thing LXD does if euid == 0: inspect
/proc/self/uid_map and see what that says.

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
src/lxc/utils.h

index 5eeb5b8feb7eca40974932ffdad2b80b3dcc33a8..87c5152961481f36d1647abae568e9b4a814b43b 100644 (file)
@@ -418,8 +418,32 @@ extern int lxc_strmunmap(void *addr, size_t length);
 //initialize rand with urandom
 extern int randseed(bool);
 
-inline static bool am_unpriv(void) {
-       return geteuid() != 0;
+inline static bool am_unpriv(void)
+{
+       FILE *f;
+       uid_t user, host, count;
+       int ret;
+
+       if (geteuid() != 0)
+               return true;
+
+       /* Now: are we in a user namespace? Because then we're also
+        * unprivileged.
+        */
+       f = fopen("/proc/self/uid_map", "r");
+       if (!f) {
+               return false;
+       }
+
+       ret = fscanf(f, "%u %u %u", &user, &host, &count);
+       fclose(f);
+       if (ret != 3) {
+               return false;
+       }
+
+       if (user != 0 || host != 0 || count != UINT32_MAX)
+               return true;
+       return false;
 }
 
 /*