X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=service%2Finstance.c;h=b8602b8c49d0b0bf720def627865ad773f30e20d;hp=29fb834e03cdbea9cdd0f1cc60956cb711dbc847;hb=f355e233714e6ff38a38ccfe74f2f42d65a519c8;hpb=46954a519259e1818117adee133c119dedbd4523 diff --git a/service/instance.c b/service/instance.c index 29fb834..b8602b8 100644 --- a/service/instance.c +++ b/service/instance.c @@ -12,6 +12,7 @@ * GNU General Public License for more details. */ +#define _GNU_SOURCE #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +49,7 @@ enum { INSTANCE_ATTR_USER, INSTANCE_ATTR_STDOUT, INSTANCE_ATTR_STDERR, + INSTANCE_ATTR_NO_NEW_PRIVS, INSTANCE_ATTR_JAIL, INSTANCE_ATTR_TRACE, INSTANCE_ATTR_SECCOMP, @@ -68,6 +71,7 @@ static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = { [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING }, [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL }, [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL }, + [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL }, [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE }, [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL }, [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING }, @@ -75,22 +79,24 @@ static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = { enum { JAIL_ATTR_NAME, - JAIL_ATTR_ROOT, + JAIL_ATTR_HOSTNAME, JAIL_ATTR_PROCFS, JAIL_ATTR_SYSFS, JAIL_ATTR_UBUS, JAIL_ATTR_LOG, + JAIL_ATTR_RONLY, JAIL_ATTR_MOUNT, __JAIL_ATTR_MAX, }; static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = { [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, - [JAIL_ATTR_ROOT] = { "root", BLOBMSG_TYPE_STRING }, + [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING }, [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL }, [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL }, [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL }, [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL }, + [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL }, [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE }, }; @@ -181,9 +187,9 @@ jail_run(struct service_instance *in, char **argv) argv[argc++] = jail->name; } - if (jail->root) { - argv[argc++] = "-P"; - argv[argc++] = jail->root; + if (jail->hostname) { + argv[argc++] = "-h"; + argv[argc++] = jail->hostname; } if (in->seccomp) { @@ -191,6 +197,9 @@ jail_run(struct service_instance *in, char **argv) argv[argc++] = in->seccomp; } + if (in->no_new_privs) + argv[argc++] = "-c"; + if (jail->procfs) argv[argc++] = "-p"; @@ -203,6 +212,9 @@ jail_run(struct service_instance *in, char **argv) if (jail->log) argv[argc++] = "-l"; + if (jail->ronly) + argv[argc++] = "-o"; + blobmsg_list_for_each(&jail->mount, var) { const char *type = blobmsg_data(var->data); @@ -224,8 +236,11 @@ instance_run(struct service_instance *in, int _stdout, int _stderr) struct blobmsg_list_node *var; struct blob_attr *cur; char **argv; + char *ld_preload; int argc = 1; /* NULL terminated */ int rem, _stdin; + bool seccomp = !in->trace && !in->has_jail && in->seccomp; + bool setlbf = _stdout >= 0; if (in->nice) setpriority(PRIO_PROCESS, 0, in->nice); @@ -236,10 +251,14 @@ instance_run(struct service_instance *in, int _stdout, int _stderr) blobmsg_list_for_each(&in->env, var) setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1); - if (!in->trace && !in->has_jail && in->seccomp) { + if (seccomp) setenv("SECCOMP_FILE", in->seccomp, 1); - setenv("LD_PRELOAD", "/lib/libpreload-seccomp.so", 1); - } + + if ((seccomp || setlbf) && asprintf(&ld_preload, "LD_PRELOAD=%s%s%s", + seccomp ? "/lib/libpreload-seccomp.so" : "", + seccomp && setlbf ? ":" : "", + setlbf ? "/lib/libsetlbf.so" : "") > 0) + putenv(ld_preload); blobmsg_list_for_each(&in->limits, var) instance_limits(blobmsg_name(var->data), blobmsg_data(var->data)); @@ -295,7 +314,21 @@ instance_run(struct service_instance *in, int _stdout, int _stderr) exit(127); } -static void instance_free_stdio(struct service_instance *in); +static void +instance_free_stdio(struct service_instance *in) +{ + if (in->_stdout.fd.fd > -1) { + ustream_free(&in->_stdout.stream); + close(in->_stdout.fd.fd); + in->_stdout.fd.fd = -1; + } + + if (in->_stderr.fd.fd > -1) { + ustream_free(&in->_stderr.stream); + close(in->_stderr.fd.fd); + in->_stderr.fd.fd = -1; + } +} void instance_start(struct service_instance *in) @@ -623,8 +656,8 @@ instance_jail_parse(struct service_instance *in, struct blob_attr *attr) jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]); jail->argc += 2; } - if (tb[JAIL_ATTR_ROOT]) { - jail->root = blobmsg_get_string(tb[JAIL_ATTR_ROOT]); + if (tb[JAIL_ATTR_HOSTNAME]) { + jail->hostname = blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]); jail->argc += 2; } if (tb[JAIL_ATTR_PROCFS]) { @@ -643,6 +676,10 @@ instance_jail_parse(struct service_instance *in, struct blob_attr *attr) jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]); jail->argc++; } + if (tb[JAIL_ATTR_RONLY]) { + jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]); + jail->argc++; + } if (tb[JAIL_ATTR_MOUNT]) { struct blob_attr *cur; int rem; @@ -731,6 +768,9 @@ instance_config_parse(struct service_instance *in) if (tb[INSTANCE_ATTR_TRACE]) in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]); + if (tb[INSTANCE_ATTR_NO_NEW_PRIVS]) + in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]); + if (!in->trace && tb[INSTANCE_ATTR_SECCOMP]) { char *seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]); struct stat s; @@ -823,22 +863,6 @@ instance_update(struct service_instance *in, struct service_instance *in_new) return true; } -static void -instance_free_stdio(struct service_instance *in) -{ - if (in->_stdout.fd.fd > -1) { - ustream_free(&in->_stdout.stream); - close(in->_stdout.fd.fd); - in->_stdout.fd.fd = -1; - } - - if (in->_stderr.fd.fd > -1) { - ustream_free(&in->_stderr.stream); - close(in->_stderr.fd.fd); - in->_stderr.fd.fd = -1; - } -} - void instance_free(struct service_instance *in) { @@ -936,6 +960,9 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) if (in->trace) blobmsg_add_u8(b, "trace", true); + if (in->no_new_privs) + blobmsg_add_u8(b, "no_new_privs", true); + if (in->seccomp) blobmsg_add_string(b, "seccomp", in->seccomp); @@ -943,12 +970,13 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) void *r = blobmsg_open_table(b, "jail"); if (in->jail.name) blobmsg_add_string(b, "name", in->jail.name); - if (in->jail.root) - blobmsg_add_string(b, "root", in->jail.root); + if (in->jail.hostname) + blobmsg_add_string(b, "hostname", in->jail.hostname); blobmsg_add_u8(b, "procfs", in->jail.procfs); blobmsg_add_u8(b, "sysfs", in->jail.sysfs); blobmsg_add_u8(b, "ubus", in->jail.ubus); blobmsg_add_u8(b, "log", in->jail.log); + blobmsg_add_u8(b, "ronly", in->jail.ronly); blobmsg_close_table(b, r); if (!avl_is_empty(&in->jail.mount.avl)) { struct blobmsg_list_node *var;