]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxccontainer: fix enter_net_ns helper to work when netns is inherited 4497/head
authorJef Steelant <jef.steelant_ext@softathome.com>
Fri, 6 Dec 2024 10:20:20 +0000 (11:20 +0100)
committerJef Steelant <jef.steelant_ext@softathome.com>
Thu, 12 Dec 2024 20:01:06 +0000 (21:01 +0100)
If a network namespace is shared by setting lxc.namespace.share.net and
the container is unprivileged, then the network namespace should be
entered before entering the user namespace. However, if an unprivileged
user started a container, then the network namespace should be entered
after entering the user namespace. To solve this, we try to enter the
network namespace before entering the user namespace. If it did not
succeed, it will be tried again inside the uder namespace.

Signed-off-by: Jef Steelant <jef.steelant_ext@softathome.com>
src/lxc/lxccontainer.c

index 0d71cfad80a827ae52c401841e87b150bf3260b2..0bc446cdb9edd61385d359e7a6db4282ad1bda0a 100644 (file)
@@ -2219,17 +2219,23 @@ WRAP_API_1(bool, lxcapi_clear_config_item, const char *)
 
 static inline bool enter_net_ns(struct lxc_container *c)
 {
+       bool net_ns_entered;
        pid_t pid = do_lxcapi_init_pid(c);
 
        if (pid < 0)
                return false;
 
+       net_ns_entered = switch_to_ns(pid, "net");
+
        if ((geteuid() != 0 || (c->lxc_conf && !list_empty(&c->lxc_conf->id_map))) &&
            (access("/proc/self/ns/user", F_OK) == 0))
                if (!switch_to_ns(pid, "user"))
                        return false;
 
-       return switch_to_ns(pid, "net");
+       if (!net_ns_entered)
+               return switch_to_ns(pid, "net");
+
+       return true;
 }
 
 /* Used by qsort and bsearch functions for comparing names. */