lxc_user_nic: rework device creation

Closes #3058. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 19bfd55a
...@@ -53,6 +53,14 @@ ...@@ -53,6 +53,14 @@
#define usernic_error(format, ...) usernic_debug_stream(stderr, format, __VA_ARGS__) #define usernic_error(format, ...) usernic_debug_stream(stderr, format, __VA_ARGS__)
#define cmd_error_errno(__ret__, __errno__, format, ...) \
({ \
typeof(__ret__) __internal_ret__ = (__ret__); \
errno = (__errno__); \
CMD_SYSERROR(format, ##__VA_ARGS__); \
__internal_ret__; \
})
__noreturn static void usage(bool fail) __noreturn static void usage(bool fail)
{ {
fprintf(stderr, "Description:\n"); fprintf(stderr, "Description:\n");
...@@ -493,19 +501,19 @@ static int instantiate_veth(char *veth1, char *veth2, pid_t pid, unsigned int mt ...@@ -493,19 +501,19 @@ static int instantiate_veth(char *veth1, char *veth2, pid_t pid, unsigned int mt
ret = lxc_veth_create(veth1, veth2, pid, mtu); ret = lxc_veth_create(veth1, veth2, pid, mtu);
if (ret < 0) { if (ret < 0) {
errno = -ret;
CMD_SYSERROR("Failed to create %s-%s\n", veth1, veth2); CMD_SYSERROR("Failed to create %s-%s\n", veth1, veth2);
return -1; return ret_errno(-ret);
} }
/* Changing the high byte of the mac address to 0xfe, the bridge /*
* Changing the high byte of the mac address to 0xfe, the bridge
* interface will always keep the host's mac address and not take the * interface will always keep the host's mac address and not take the
* mac address of a container. * mac address of a container.
*/ */
ret = setup_private_host_hw_addr(veth1); ret = setup_private_host_hw_addr(veth1);
if (ret < 0) { if (ret < 0) {
errno = -ret;
CMD_SYSERROR("Failed to change mac address of host interface %s\n", veth1); CMD_SYSERROR("Failed to change mac address of host interface %s\n", veth1);
return ret_errno(-ret);
} }
return netdev_set_flag(veth1, IFF_UP); return netdev_set_flag(veth1, IFF_UP);
...@@ -769,13 +777,25 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -769,13 +777,25 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
return NULL; return NULL;
} }
if (lxc_pwrite_nointr(fd, newline, slen, length) != slen) if (lxc_pwrite_nointr(fd, newline, slen, length) != slen) {
CMD_SYSERROR("Failed to append new entry \"%s\" to database file", newline); CMD_SYSERROR("Failed to append new entry \"%s\" to database file", newline);
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
return NULL;
}
ret = ftruncate(fd, length + slen); ret = ftruncate(fd, length + slen);
if (ret < 0) if (ret < 0) {
CMD_SYSERROR("Failed to truncate file\n"); CMD_SYSERROR("Failed to truncate file\n");
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
return NULL;
}
return strdup(nicname); return strdup(nicname);
} }
...@@ -814,113 +834,84 @@ again: ...@@ -814,113 +834,84 @@ again:
static char *lxc_secure_rename_in_ns(int pid, char *oldname, char *newname, static char *lxc_secure_rename_in_ns(int pid, char *oldname, char *newname,
int *container_veth_ifidx) int *container_veth_ifidx)
{ {
int ofd, ret; __do_close int fd = -EBADF, ofd = -EBADF;
int fret = -1;
int ifindex, ret;
pid_t pid_self; pid_t pid_self;
uid_t ruid, suid, euid; uid_t ruid, suid, euid;
char ifname[IFNAMSIZ]; char ifname[IFNAMSIZ];
char *string_ret = NULL, *name = NULL;
int fd = -1, ifindex = -1;
pid_self = lxc_raw_getpid(); pid_self = lxc_raw_getpid();
ofd = lxc_preserve_ns(pid_self, "net"); ofd = lxc_preserve_ns(pid_self, "net");
if (ofd < 0) { if (ofd < 0)
usernic_error("Failed opening network namespace path for %d", pid_self); return cmd_error_errno(NULL, errno, "Failed opening network namespace path for %d", pid_self);
return NULL;
}
fd = lxc_preserve_ns(pid, "net"); fd = lxc_preserve_ns(pid, "net");
if (fd < 0) { if (fd < 0)
usernic_error("Failed opening network namespace path for %d", pid); return cmd_error_errno(NULL, errno, "Failed opening network namespace path for %d", pid);
goto do_partial_cleanup;
}
ret = getresuid(&ruid, &euid, &suid); ret = getresuid(&ruid, &euid, &suid);
if (ret < 0) { if (ret < 0)
CMD_SYSERROR("Failed to retrieve real, effective, and saved user IDs\n"); return cmd_error_errno(NULL, errno, "Failed to retrieve real, effective, and saved user IDs\n");
goto do_partial_cleanup;
}
ret = setns(fd, CLONE_NEWNET); ret = setns(fd, CLONE_NEWNET);
close(fd); if (ret < 0)
fd = -1; return cmd_error_errno(NULL, errno, "Failed to setns() to the network namespace of the container with PID %d\n", pid);
if (ret < 0) {
CMD_SYSERROR("Failed to setns() to the network namespace of the container with PID %d\n",
pid);
goto do_partial_cleanup;
}
ret = setresuid(ruid, ruid, 0); ret = setresuid(ruid, ruid, 0);
if (ret < 0) { if (ret < 0) {
CMD_SYSERROR("Failed to drop privilege by setting effective user id and real user id to %d, and saved user ID to 0\n", CMD_SYSERROR("Failed to drop privilege by setting effective user id and real user id to %d, and saved user ID to 0\n", ruid);
ruid); /*
/* It's ok to jump to do_full_cleanup here since setresuid() * It's ok to jump to do_full_cleanup here since setresuid()
* will succeed when trying to set real, effective, and saved to * will succeed when trying to set real, effective, and saved
* values they currently have. * to values they currently have.
*/ */
goto do_full_cleanup; goto out_setns;
} }
/* Check if old interface exists. */ /* Check if old interface exists. */
ifindex = if_nametoindex(oldname); ifindex = if_nametoindex(oldname);
if (!ifindex) { if (!ifindex) {
CMD_SYSERROR("Failed to get netdev index\n"); CMD_SYSERROR("Failed to get netdev index\n");
goto do_full_cleanup; goto out_setresuid;
} }
/* When the IFLA_IFNAME attribute is passed something like "<prefix>%d" /*
* When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
* netlink will replace the format specifier with an appropriate index. * netlink will replace the format specifier with an appropriate index.
* So we pass "eth%d". * So we pass "eth%d".
*/ */
if (newname) ret = lxc_netdev_rename_by_name(oldname, newname ? newname : "eth%d");
name = newname;
else
name = "eth%d";
ret = lxc_netdev_rename_by_name(oldname, name);
name = NULL;
if (ret < 0) { if (ret < 0) {
usernic_error("Error %d renaming netdev %s to %s in container\n", CMD_SYSERROR("Error %d renaming netdev %s to %s in container\n", ret, oldname, newname ? newname : "eth%d");
ret, oldname, newname ? newname : "eth%d"); goto out_setresuid;
goto do_full_cleanup;
} }
/* Retrieve new name for interface. */ /* Retrieve new name for interface. */
if (!if_indextoname(ifindex, ifname)) { if (!if_indextoname(ifindex, ifname)) {
CMD_SYSERROR("Failed to get new netdev name\n"); CMD_SYSERROR("Failed to get new netdev name\n");
goto do_full_cleanup; goto out_setresuid;
} }
/* Allocation failure for strdup() is checked below. */ fret = 0;
name = strdup(ifname);
string_ret = name;
*container_veth_ifidx = ifindex;
do_full_cleanup: out_setresuid:
ret = setresuid(ruid, euid, suid); ret = setresuid(ruid, euid, suid);
if (ret < 0) { if (ret < 0)
CMD_SYSERROR("Failed to restore privilege by setting effective user id to %d, real user id to %d, and saved user ID to %d\n", return cmd_error_errno(NULL, errno, "Failed to restore privilege by setting effective user id to %d, real user id to %d, and saved user ID to %d\n",
ruid, euid, suid); ruid, euid, suid);
string_ret = NULL;
}
out_setns:
ret = setns(ofd, CLONE_NEWNET); ret = setns(ofd, CLONE_NEWNET);
if (ret < 0) { if (ret < 0)
CMD_SYSERROR("Failed to setns() to original network namespace of PID %d\n", ofd); return cmd_error_errno(NULL, errno, "Failed to setns() to original network namespace of PID %d\n", ofd);
string_ret = NULL;
}
do_partial_cleanup:
if (fd >= 0)
close(fd);
if (!string_ret && name)
free(name);
close(ofd); if (fret < 0)
return NULL;
return string_ret; *container_veth_ifidx = ifindex;
return strdup(ifname);
} }
/* If the caller (real uid, not effective uid) may read the /proc/[pid]/ns/net, /* If the caller (real uid, not effective uid) may read the /proc/[pid]/ns/net,
......
...@@ -456,13 +456,15 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ...@@ -456,13 +456,15 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
#endif #endif
#if HAVE_M_FORMAT #if HAVE_M_FORMAT
#define CMD_SYSERROR(format, ...) \ #define CMD_SYSERROR(format, ...) \
fprintf(stderr, "%m - " format, ##__VA_ARGS__) fprintf(stderr, "%m - %s: %d: %s: " format "\n", __FILE__, __LINE__, \
__func__, ##__VA_ARGS__);
#else #else
#define CMD_SYSERROR(format, ...) \ #define CMD_SYSERROR(format, ...) \
do { \ do { \
lxc_log_strerror_r; \ lxc_log_strerror_r; \
fprintf(stderr, "%s - " format, ptr, ##__VA_ARGS__); \ fprintf(stderr, "%s - %s: %d: %s: " format "\n", __FILE__, \
__LINE__, __func__, ##__VA_ARGS__);
} while (0) } while (0)
#endif #endif
......
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