chiark / gitweb /
nspawn: add new --personality= switch to make it easier to run 32bit containers on...
[elogind.git] / src / nspawn / nspawn.c
index 3a6d428cd5e682231079e1a8001d067e45a1ced8..98e90fe3c9207ab51dde3c3ece05bea4aca7a24e 100644 (file)
 #include <sys/un.h>
 #include <sys/socket.h>
 #include <linux/netlink.h>
-#include <linux/rtnetlink.h>
 #include <sys/eventfd.h>
 #include <net/if.h>
 #include <linux/veth.h>
+#include <sys/personality.h>
 
 #ifdef HAVE_SELINUX
 #include <selinux/selinux.h>
 #include "rtnl-util.h"
 #include "udev-util.h"
 
+#ifdef HAVE_SECCOMP
+#include "seccomp-util.h"
+#endif
+
 typedef enum LinkJournal {
         LINK_NO,
         LINK_AUTO,
@@ -134,6 +138,8 @@ static bool arg_register = true;
 static bool arg_keep_unit = false;
 static char **arg_network_interfaces = NULL;
 static bool arg_network_veth = false;
+static char *arg_network_bridge = NULL;
+static unsigned long arg_personality = 0xffffffffLU;
 
 static int help(void) {
 
@@ -152,8 +158,12 @@ static int help(void) {
                "     --network-interface=INTERFACE\n"
                "                            Assign an existing network interface to the\n"
                "                            container\n"
-               "     --network-veth         Add a virtual ethernet connection between host\n"
+               "     --network-veth         Add a virtual ethernet connection between host\n"
                "                            and container\n"
+               "     --network-bridge=INTERFACE\n"
+               "                            Add a virtual ethernet connection between host\n"
+               "                            and container and add it to an existing bridge on\n"
+               "                            the host\n"
                "  -Z --selinux-context=SECLABEL\n"
                "                            Set the SELinux security context to be used by\n"
                "                            processes in the container\n"
@@ -197,6 +207,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_KEEP_UNIT,
                 ARG_NETWORK_INTERFACE,
                 ARG_NETWORK_VETH,
+                ARG_NETWORK_BRIDGE,
+                ARG_PERSONALITY,
         };
 
         static const struct option options[] = {
@@ -223,7 +235,9 @@ static int parse_argv(int argc, char *argv[]) {
                 { "register",              required_argument, NULL, ARG_REGISTER          },
                 { "keep-unit",             no_argument,       NULL, ARG_KEEP_UNIT         },
                 { "network-interface",     required_argument, NULL, ARG_NETWORK_INTERFACE },
-                { "network-veth",          no_argument,       NULL, ARG_NETWORK_VETH   },
+                { "network-veth",          no_argument,       NULL, ARG_NETWORK_VETH      },
+                { "network-bridge",        required_argument, NULL, ARG_NETWORK_BRIDGE    },
+                { "personality",           required_argument, NULL, ARG_PERSONALITY       },
                 {}
         };
 
@@ -263,6 +277,13 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_NETWORK_BRIDGE:
+                        arg_network_bridge = strdup(optarg);
+                        if (!arg_network_bridge)
+                                return log_oom();
+
+                        /* fall through */
+
                 case ARG_NETWORK_VETH:
                         arg_network_veth = true;
                         arg_private_network = true;
@@ -457,6 +478,16 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_keep_unit = true;
                         break;
 
+                case ARG_PERSONALITY:
+
+                        arg_personality = parse_personality(optarg);
+                        if (arg_personality == 0xffffffffLU) {
+                                log_error("Unknown or unsupported personality '%s'.", optarg);
+                                return -EINVAL;
+                        }
+
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -1271,10 +1302,9 @@ static int reset_audit_loginuid(void) {
         return 0;
 }
 
-static int setup_veth(int netns_fd) {
+static int setup_veth(pid_t pid, char iface_name[]) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
-        char iface_name[IFNAMSIZ] = "ve-";
         int r;
 
         if (!arg_private_network)
@@ -1283,7 +1313,7 @@ static int setup_veth(int netns_fd) {
         if (!arg_network_veth)
                 return 0;
 
-        strncpy(iface_name+3, arg_machine, sizeof(iface_name) - 3);
+        strncpy(iface_name+3, arg_machine, IFNAMSIZ - 3);
 
         r = sd_rtnl_open(0, &rtnl);
         if (r < 0) {
@@ -1291,19 +1321,19 @@ static int setup_veth(int netns_fd) {
                 return r;
         }
 
-        r = sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m);
+        r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, 0, &m);
         if (r < 0) {
                 log_error("Failed to allocate netlink message: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, "host0");
+        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, iface_name);
         if (r < 0) {
-                log_error("Failed to append netlink kind: %s", strerror(-r));
+                log_error("Failed to add netlink interface name: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_rtnl_message_open_container(m, IFLA_LINKINFO, 0);
+        r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
         if (r < 0) {
                 log_error("Failed to open netlink container: %s", strerror(-r));
                 return r;
@@ -1315,25 +1345,25 @@ static int setup_veth(int netns_fd) {
                 return r;
         }
 
-        r = sd_rtnl_message_open_container(m, IFLA_INFO_DATA, 0);
+        r = sd_rtnl_message_open_container(m, IFLA_INFO_DATA);
         if (r < 0) {
                 log_error("Failed to open netlink container: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_rtnl_message_open_container(m, VETH_INFO_PEER, sizeof(struct ifinfomsg));
+        r = sd_rtnl_message_open_container(m, VETH_INFO_PEER);
         if (r < 0) {
-                log_error("Failed to open netlink container: %s", strerror(-r));
+                log_error("Failed to open netlink container: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, iface_name);
+        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, "host0");
         if (r < 0) {
-                log_error("Failed to append netlink kind: %s", strerror(-r));
+                log_error("Failed to add netlink interface name: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_rtnl_message_append_u32(m, IFLA_NET_NS_FD, netns_fd);
+        r = sd_rtnl_message_append_u32(m, IFLA_NET_NS_PID, pid);
         if (r < 0) {
                 log_error("Failed to add netlink namespace field: %s", strerror(-r));
                 return r;
@@ -1366,6 +1396,59 @@ static int setup_veth(int netns_fd) {
         return 0;
 }
 
+static int setup_bridge(const char veth_name[]) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+        int r, bridge;
+
+        if (!arg_private_network)
+                return 0;
+
+        if (!arg_network_veth)
+                return 0;
+
+        if (!arg_network_bridge)
+                return 0;
+
+        bridge = (int) if_nametoindex(arg_network_bridge);
+        if (bridge <= 0) {
+                log_error("Failed to resolve interface %s: %m", arg_network_bridge);
+                return -errno;
+        }
+
+        r = sd_rtnl_open(0, &rtnl);
+        if (r < 0) {
+                log_error("Failed to connect to netlink: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, 0, &m);
+        if (r < 0) {
+                log_error("Failed to allocate netlink message: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, veth_name);
+        if (r < 0) {
+                log_error("Failed to add netlink interface name field: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append_u32(m, IFLA_MASTER, bridge);
+        if (r < 0) {
+                log_error("Failed to add netlink master field: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call(rtnl, m, 0, NULL);
+        if (r < 0) {
+                log_error("Failed to add veth interface to bridge: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
 static int move_network_interfaces(pid_t pid) {
         _cleanup_udev_unref_ struct udev *udev = NULL;
         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
@@ -1414,7 +1497,7 @@ static int move_network_interfaces(pid_t pid) {
                         return -EBUSY;
                 }
 
-                r = sd_rtnl_message_new_link(RTM_NEWLINK, ifi, &m);
+                r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, ifi, &m);
                 if (r < 0) {
                         log_error("Failed to allocate netlink message: %s", strerror(-r));
                         return r;
@@ -1456,7 +1539,13 @@ static int audit_still_doesnt_work_in_containers(void) {
         if (!seccomp)
                 return log_oom();
 
-        r = seccomp_rule_add_exact(
+        r = seccomp_add_secondary_archs(seccomp);
+        if (r < 0 && r != -EEXIST) {
+                log_error("Failed to add secondary archs to seccomp filter: %s", strerror(-r));
+                goto finish;
+        }
+
+        r = seccomp_rule_add(
                         seccomp,
                         SCMP_ACT_ERRNO(EAFNOSUPPORT),
                         SCMP_SYS(socket),
@@ -1489,7 +1578,7 @@ finish:
 
 int main(int argc, char *argv[]) {
 
-        _cleanup_close_ int master = -1, kdbus_fd = -1, sync_fd = -1, netns_fd = -1;
+        _cleanup_close_ int master = -1, kdbus_fd = -1, sync_fd = -1;
         _cleanup_close_pipe_ int kmsg_socket_pair[2] = { -1, -1 };
         _cleanup_free_ char *kdbus_domain = NULL;
         _cleanup_fdset_free_ FDSet *fds = NULL;
@@ -1498,6 +1587,7 @@ int main(int argc, char *argv[]) {
         int n_fd_passed;
         pid_t pid = 0;
         sigset_t mask;
+        char veth_name[IFNAMSIZ] = "ve-";
 
         log_parse_environment();
         log_open();
@@ -1555,9 +1645,21 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (arg_boot && path_is_os_tree(arg_directory) <= 0) {
-                log_error("Directory %s doesn't look like an OS root directory (/etc/os-release is missing). Refusing.", arg_directory);
-                goto finish;
+        if (arg_boot) {
+                if (path_is_os_tree(arg_directory) <= 0) {
+                        log_error("Directory %s doesn't look like an OS root directory (/etc/os-release is missing). Refusing.", arg_directory);
+                        goto finish;
+                }
+        } else {
+                const char *p;
+
+                p = strappenda(arg_directory,
+                               argc > optind && path_is_absolute(argv[optind]) ? argv[optind] : "/usr/bin/");
+                if (access(p, F_OK) < 0) {
+                        log_error("Directory %s lacks the binary to execute or doesn't look like a binary tree. Refusing.", arg_directory);
+                        goto finish;
+
+                }
         }
 
         log_close();
@@ -1592,14 +1694,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (arg_network_veth) {
-                netns_fd = open("/proc/self/ns/net", O_RDWR|O_CLOEXEC);
-                if (netns_fd < 0) {
-                        log_error("Failed to open network namespace fd: %m");
-                        goto finish;
-                }
-        }
-
         if (access("/dev/kdbus/control", F_OK) >= 0) {
 
                 if (arg_share_system) {
@@ -1754,14 +1848,6 @@ int main(int argc, char *argv[]) {
 
                         dev_setup(arg_directory);
 
-                        if (setup_veth(netns_fd) < 0)
-                                goto child_fail;
-
-                        if (netns_fd >= 0) {
-                                close_nointr_nofail(netns_fd);
-                                netns_fd = -1;
-                        }
-
                         if (audit_still_doesnt_work_in_containers() < 0)
                                 goto child_fail;
 
@@ -1911,6 +1997,13 @@ int main(int argc, char *argv[]) {
 
                         setup_hostname();
 
+                        if (arg_personality != 0xffffffffLU) {
+                                if (personality(arg_personality) < 0) {
+                                        log_error("personality() failed: %m");
+                                        goto child_fail;
+                                }
+                        }
+
                         eventfd_read(sync_fd, &x);
                         close_nointr_nofail(sync_fd);
                         sync_fd = -1;
@@ -1956,6 +2049,7 @@ int main(int argc, char *argv[]) {
                         else {
                                 chdir(home ? home : "/root");
                                 execle("/bin/bash", "-bash", NULL, env_use);
+                                execle("/bin/sh", "-sh", NULL, env_use);
                         }
 
                         log_error("execv() failed: %m");
@@ -1975,6 +2069,14 @@ int main(int argc, char *argv[]) {
                 if (r < 0)
                         goto finish;
 
+                r = setup_veth(pid, veth_name);
+                if (r < 0)
+                        goto finish;
+
+                r = setup_bridge(veth_name);
+                if (r < 0)
+                        goto finish;
+
                 eventfd_write(sync_fd, 1);
                 close_nointr_nofail(sync_fd);
                 sync_fd = -1;