Skip to content

Commit a53589e

Browse files
committed
lxc/lxccontainer: stop printing misleading errors in enter_net_ns()
In enter_net_ns() we try to enter network namespace at first, before entering a user namespace to support inherited netns case properly. It is expected to get EPERM for unprivileged container with non-shared network namespace at first try. Let's take this into account and stop misleading users with these error messages. Link: https://discuss.linuxcontainers.org/t/lxc-ls-fancy-command-shows-operation-not-permitted/24080 Fixes: 3011e79 ("lxccontainer: fix enter_net_ns helper to work when netns is inherited") Fixes: lxc#4560 Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
1 parent e2434a2 commit a53589e

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/lxc/lxccontainer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ static inline bool enter_net_ns(struct lxc_container *c)
22202220
if (pid < 0)
22212221
return false;
22222222

2223-
net_ns_entered = switch_to_ns(pid, "net");
2223+
net_ns_entered = try_switch_to_ns(pid, "net", true);
22242224

22252225
if ((geteuid() != 0 || (c->lxc_conf && !list_empty(&c->lxc_conf->id_map))) &&
22262226
(access("/proc/self/ns/user", F_OK) == 0))

src/lxc/utils.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ int detect_shared_rootfs(void)
878878
return 0;
879879
}
880880

881-
bool switch_to_ns(pid_t pid, const char *ns)
881+
bool try_switch_to_ns(pid_t pid, const char *ns, bool optional)
882882
{
883883
__do_close int fd = -EBADF;
884884
int ret;
@@ -896,8 +896,12 @@ bool switch_to_ns(pid_t pid, const char *ns)
896896
return log_error_errno(false, errno, "Failed to open \"%s\"", nspath);
897897

898898
ret = setns(fd, 0);
899-
if (ret)
900-
return log_error_errno(false, errno, "Failed to set process %d to \"%s\" of %d", pid, ns, fd);
899+
if (ret) {
900+
if (optional)
901+
return log_trace_errno(false, errno, "Failed to set process %d to \"%s\" of %d", pid, ns, fd);
902+
else
903+
return log_error_errno(false, errno, "Failed to set process %d to \"%s\" of %d", pid, ns, fd);
904+
}
901905

902906
return true;
903907
}

src/lxc/utils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ __hidden extern bool is_shared_mountpoint(const char *path);
134134
__hidden extern int detect_shared_rootfs(void);
135135
__hidden extern bool detect_ramfs_rootfs(void);
136136
__hidden extern char *on_path(const char *cmd, const char *rootfs);
137-
__hidden extern bool switch_to_ns(pid_t pid, const char *ns);
137+
138+
__hidden extern bool try_switch_to_ns(pid_t pid, const char *ns, bool optional);
139+
inline static bool switch_to_ns(pid_t pid, const char *ns)
140+
{
141+
return try_switch_to_ns(pid, ns, false);
142+
}
143+
138144
__hidden extern char *get_template_path(const char *t);
139145
__hidden extern int safe_mount(const char *src, const char *dest, const char *fstype,
140146
unsigned long flags, const void *data, const char *rootfs);

0 commit comments

Comments
 (0)