Commit ae22a220 by S.Çağlar Onur Committed by Stéphane Graber

make lxcapi_get_interfaces and lxcapi_get_ips unprivileged container aware

Based on Stéphane's suggestion, those two API methods now; * fork a new process, * switch to appropriate namespace(s), * do what we want, * return the data over a pipe to the parent which returns the result to the original caller. For the whole thread please see; https://lists.linuxcontainers.org/pipermail/lxc-devel/2014-January/007362.html This patch also makes lxc-ls and lxc-info call those functions. I'm adding Stéphane as an author here since both the idea as well as the initial setns code come from him. Author: S.Çağlar Onur <caglar@10ur.org> Author: Stéphane Graber <stgraber@ubuntu.com> Signed-off-by: 's avatarS.Çağlar Onur <caglar@10ur.org> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent adf4b408
...@@ -265,7 +265,7 @@ for container_name in lxc.list_containers(config_path=nest_lxcpath): ...@@ -265,7 +265,7 @@ for container_name in lxc.list_containers(config_path=nest_lxcpath):
# FIXME: We should get get_ips working as non-root # FIXME: We should get get_ips working as non-root
if container.running: if container.running:
if not SUPPORT_SETNS_NET or os.geteuid() != 0: if not SUPPORT_SETNS_NET:
entry[protocol] = 'UNKNOWN' entry[protocol] = 'UNKNOWN'
continue continue
......
...@@ -310,7 +310,6 @@ static int print_info(const char *name, const char *lxcpath) ...@@ -310,7 +310,6 @@ static int print_info(const char *name, const char *lxcpath)
} }
if (ips) { if (ips) {
if (geteuid() == 0) {
char **addresses = c->get_ips(c, NULL, NULL, 0); char **addresses = c->get_ips(c, NULL, NULL, 0);
if (addresses) { if (addresses) {
char *address; char *address;
...@@ -321,9 +320,6 @@ static int print_info(const char *name, const char *lxcpath) ...@@ -321,9 +320,6 @@ static int print_info(const char *name, const char *lxcpath)
i++; i++;
} }
} }
} else {
print_info_msg_str("IP:", "UNKNOWN");
}
} }
if (stats) { if (stats) {
......
...@@ -1372,48 +1372,51 @@ static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key) ...@@ -1372,48 +1372,51 @@ static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
return ret == 0; return ret == 0;
} }
static inline void exit_from_ns(struct lxc_container *c, int *old_netns, int *new_netns) { static inline bool enter_to_ns(struct lxc_container *c) {
/* Switch back to original netns */ int netns, userns, ret = 0, init_pid = 0;;
if (*old_netns >= 0 && setns(*old_netns, CLONE_NEWNET))
SYSERROR("failed to setns");
if (*new_netns >= 0)
close(*new_netns);
if (*old_netns >= 0)
close(*old_netns);
}
static inline bool enter_to_ns(struct lxc_container *c, int *old_netns, int *new_netns) {
int ret = 0;
char new_netns_path[MAXPATHLEN]; char new_netns_path[MAXPATHLEN];
char new_userns_path[MAXPATHLEN];
if (!c->is_running(c)) if (!c->is_running(c))
goto out; goto out;
/* Save reference to old netns */ init_pid = c->init_pid(c);
*old_netns = open("/proc/self/ns/net", O_RDONLY);
if (*old_netns < 0) { /* Switch to new userns */
SYSERROR("failed to open /proc/self/ns/net"); if (geteuid() && access("/proc/self/ns/user", F_OK) == 0) {
ret = snprintf(new_userns_path, MAXPATHLEN, "/proc/%d/ns/user", init_pid);
if (ret < 0 || ret >= MAXPATHLEN)
goto out;
userns = open(new_userns_path, O_RDONLY);
if (userns < 0) {
SYSERROR("failed to open %s", new_userns_path);
goto out;
}
if (setns(userns, CLONE_NEWUSER)) {
SYSERROR("failed to setns for CLONE_NEWUSER");
goto out; goto out;
} }
}
/* Switch to new netns */ /* Switch to new netns */
ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c)); ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", init_pid);
if (ret < 0 || ret >= MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
goto out; goto out;
*new_netns = open(new_netns_path, O_RDONLY); netns = open(new_netns_path, O_RDONLY);
if (*new_netns < 0) { if (netns < 0) {
SYSERROR("failed to open %s", new_netns_path); SYSERROR("failed to open %s", new_netns_path);
goto out; goto out;
} }
if (setns(*new_netns, CLONE_NEWNET)) { if (setns(netns, CLONE_NEWNET)) {
SYSERROR("failed to setns"); SYSERROR("failed to setns for CLONE_NEWNET");
goto out; goto out;
} }
return true; return true;
out: out:
exit_from_ns(c, old_netns, new_netns);
return false; return false;
} }
...@@ -1490,18 +1493,35 @@ static bool remove_from_array(char ***names, char *cname, int size) ...@@ -1490,18 +1493,35 @@ static bool remove_from_array(char ***names, char *cname, int size)
static char** lxcapi_get_interfaces(struct lxc_container *c) static char** lxcapi_get_interfaces(struct lxc_container *c)
{ {
int i, count = 0; pid_t pid;
struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; int i, count = 0, pipefd[2];
char **interfaces = NULL; char **interfaces = NULL;
int old_netns = -1, new_netns = -1; char interface[IFNAMSIZ];
if (am_unpriv()) { if(pipe(pipefd) < 0) {
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); SYSERROR("pipe failed");
goto out; return NULL;
}
pid = fork();
if (pid < 0) {
SYSERROR("failed to fork task to get interfaces information\n");
close(pipefd[0]);
close(pipefd[1]);
return NULL;
} }
if (!enter_to_ns(c, &old_netns, &new_netns)) if (pid == 0) { // child
int ret = 1, nbytes;
struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
/* close the read-end of the pipe */
close(pipefd[0]);
if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
goto out; goto out;
}
/* Grab the list of interfaces */ /* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) { if (getifaddrs(&interfaceArray)) {
...@@ -1511,51 +1531,87 @@ static char** lxcapi_get_interfaces(struct lxc_container *c) ...@@ -1511,51 +1531,87 @@ static char** lxcapi_get_interfaces(struct lxc_container *c)
/* Iterate through the interfaces */ /* Iterate through the interfaces */
for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) { for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
if (array_contains(&interfaces, tempIfAddr->ifa_name, count)) nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ);
continue; if (nbytes < 0) {
ERROR("write failed");
if(!add_to_array(&interfaces, tempIfAddr->ifa_name, count)) goto out;
goto err; }
count++; count++;
} }
ret = 0;
out: out:
if (interfaceArray) if (interfaceArray)
freeifaddrs(interfaceArray); freeifaddrs(interfaceArray);
exit_from_ns(c, &old_netns, &new_netns); /* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]);
exit(ret);
}
/* Append NULL to the array */ /* close the write-end of the pipe */
if(interfaces) close(pipefd[1]);
interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);
return interfaces; while (read(pipefd[0], &interface, IFNAMSIZ) > 0) {
if (array_contains(&interfaces, interface, count))
continue;
err: if(!add_to_array(&interfaces, interface, count))
ERROR("PARENT: add_to_array failed");
count++;
}
if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++) for(i=0;i<count;i++)
free(interfaces[i]); free(interfaces[i]);
free(interfaces); free(interfaces);
interfaces = NULL; interfaces = NULL;
goto out; }
/* close the read-end of the pipe */
close(pipefd[0]);
/* Append NULL to the array */
if(interfaces)
interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);
return interfaces;
} }
static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope) static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope)
{ {
int i, count = 0; pid_t pid;
int i, count = 0, pipefd[2];
char **addresses = NULL;
char address[INET6_ADDRSTRLEN];
if(pipe(pipefd) < 0) {
SYSERROR("pipe failed");
return NULL;
}
pid = fork();
if (pid < 0) {
SYSERROR("failed to fork task to get container ips\n");
close(pipefd[0]);
close(pipefd[1]);
return NULL;
}
if (pid == 0) { // child
int ret = 1, nbytes;
struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
char addressOutputBuffer[INET6_ADDRSTRLEN]; char addressOutputBuffer[INET6_ADDRSTRLEN];
void *tempAddrPtr = NULL; void *tempAddrPtr = NULL;
char **addresses = NULL;
char *address = NULL; char *address = NULL;
int old_netns = -1, new_netns = -1;
if (am_unpriv()) { /* close the read-end of the pipe */
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__); close(pipefd[0]);
goto out;
}
if (!enter_to_ns(c, &old_netns, &new_netns)) if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
goto out; goto out;
}
/* Grab the list of interfaces */ /* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) { if (getifaddrs(&interfaceArray)) {
...@@ -1595,30 +1651,48 @@ static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, con ...@@ -1595,30 +1651,48 @@ static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, con
if (!address) if (!address)
continue; continue;
if(!add_to_array(&addresses, address, count)) nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN);
goto err; if (nbytes < 0) {
ERROR("write failed");
goto out;
}
count++; count++;
} }
ret = 0;
out: out:
if(interfaceArray) if(interfaceArray)
freeifaddrs(interfaceArray); freeifaddrs(interfaceArray);
exit_from_ns(c, &old_netns, &new_netns); /* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]);
exit(ret);
}
/* Append NULL to the array */ /* close the write-end of the pipe */
if(addresses) close(pipefd[1]);
addresses = (char **)lxc_append_null_to_array((void **)addresses, count);
return addresses; while (read(pipefd[0], &address, INET6_ADDRSTRLEN) > 0) {
if(!add_to_array(&addresses, address, count))
ERROR("PARENT: add_to_array failed");
count++;
}
err: if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++) for(i=0;i<count;i++)
free(addresses[i]); free(addresses[i]);
free(addresses); free(addresses);
addresses = NULL; addresses = NULL;
}
goto out; /* close the read-end of the pipe */
close(pipefd[0]);
/* Append NULL to the array */
if(addresses)
addresses = (char **)lxc_append_null_to_array((void **)addresses, count);
return addresses;
} }
static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen) static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment