From patchwork Thu May 9 15:41:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasilis LIaskovitis X-Patchwork-Id: 10937301 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 22337933 for ; Thu, 9 May 2019 15:43:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10B1A283C3 for ; Thu, 9 May 2019 15:43:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0447B28B08; Thu, 9 May 2019 15:43:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 46941283C3 for ; Thu, 9 May 2019 15:43:50 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hOlB4-0007ej-HQ; Thu, 09 May 2019 15:41:46 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hOlB3-0007eJ-1u for xen-devel@lists.xenproject.org; Thu, 09 May 2019 15:41:45 +0000 X-Inumbo-ID: f0a90d6c-7270-11e9-8f98-a30bf37ff598 Received: from smtp.nue.novell.com (unknown [195.135.221.5]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f0a90d6c-7270-11e9-8f98-a30bf37ff598; Thu, 09 May 2019 15:41:41 +0000 (UTC) Received: from emea4-mta.ukb.novell.com ([10.120.13.87]) by smtp.nue.novell.com with ESMTP (TLS encrypted); Thu, 09 May 2019 17:41:40 +0200 Received: from linux-nq5u.suse.de (nwb-a10-snat.microfocus.com [10.120.13.202]) by emea4-mta.ukb.novell.com with ESMTP (NOT encrypted); Thu, 09 May 2019 16:41:33 +0100 From: Vasilis Liaskovitis To: xen-devel@lists.xenproject.org Date: Thu, 9 May 2019 17:41:25 +0200 Message-Id: <20190509154128.9196-2-vliaskovitis@suse.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190509154128.9196-1-vliaskovitis@suse.com> References: <20190509154128.9196-1-vliaskovitis@suse.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v3 1/4] xen: add hypercall for reading runtime parameters X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: jgross@suse.com, sstabellini@kernel.org, wei.liu2@citrix.com, vliaskovitis@suse.com, George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com, ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, dgdegra@tycho.nsa.gov Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a sysctl hypercall to support reading hypervisor runtime parameters. Limitations: - Custom runtime parameters (OPT_CUSTOM) are not supported yet. - For integer parameters (OPT_UINT), only unsigned parameters are printed correctly. - The implementation only reads runtime parameters, but it can be changed to read all hypervisor parameters if needed. Signed-off-by: Vasilis Liaskovitis --- tools/flask/policy/modules/dom0.te | 2 +- xen/common/kernel.c | 118 ++++++++++++++++++++++++++++ xen/common/sysctl.c | 52 +++++++++++- xen/include/public/sysctl.h | 18 +++++ xen/include/xen/lib.h | 1 + xen/xsm/flask/hooks.c | 3 + xen/xsm/flask/policy/access_vectors | 2 + 7 files changed, 193 insertions(+), 3 deletions(-) diff --git a/tools/flask/policy/modules/dom0.te b/tools/flask/policy/modules/dom0.te index a347d664f8..681d1a101b 100644 --- a/tools/flask/policy/modules/dom0.te +++ b/tools/flask/policy/modules/dom0.te @@ -16,7 +16,7 @@ allow dom0_t xen_t:xen { allow dom0_t xen_t:xen2 { resource_op psr_cmt_op psr_alloc pmu_ctrl get_symbol get_cpu_levelling_caps get_cpu_featureset livepatch_op - coverage_op set_parameter + coverage_op set_parameter get_parameter }; # Allow dom0 to use all XENVER_ subops that have checks. diff --git a/xen/common/kernel.c b/xen/common/kernel.c index 612575430f..6695ffc372 100644 --- a/xen/common/kernel.c +++ b/xen/common/kernel.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,123 @@ static int assign_integer_param(const struct kernel_param *param, uint64_t val) return 0; } +static int get_integer_param(const struct kernel_param *param, uint64_t *val) +{ + switch ( param->len ) + { + case sizeof(uint8_t): + *val = *(uint8_t *)param->par.var; + break; + + case sizeof(uint16_t): + *val = *(uint16_t *)param->par.var; + break; + + case sizeof(uint32_t): + *val = *(uint32_t *)param->par.var; + break; + + case sizeof(uint64_t): + *val = *(uint64_t *)param->par.var; + break; + + default: + BUG(); + break; + } + + return 0; +} + +int runtime_get_params(const char *cmdline, char *values, + size_t maxlen) +{ + char opt[128], *optkey, *q, *val = values; + const char *p = cmdline; + const struct kernel_param *param; + int rc = 0, len = 0; + size_t bufpos = 0; + uint64_t param_int; + + while ( !rc ) + { + /* Skip whitespace. */ + while ( isspace(*p) ) + p++; + if ( *p == '\0' ) + break; + + /* Grab the next whitespace-delimited option. */ + q = optkey = opt; + while ( !isspace(*p) && (*p != '\0') ) + { + if ( (q - opt) < (sizeof(opt) - 1) ) /* avoid overflow */ + *q++ = *p; + else return -ENOMEM; + p++; + } + *q = '\0'; + + for ( param = __param_start; param < __param_end; param++ ) + { + if ( strcmp(param->name, optkey) ) + continue; + + switch ( param->type ) + { + case OPT_STR: + len = snprintf(val + bufpos, maxlen - bufpos, "%s ", + (char*)param->par.var); + break; + + case OPT_UINT: + case OPT_SIZE: + /* Signed integer parameters are not supported yet. + * While there are no runtime signed integer parameters + * at the moment, adding one and trying to get its value + * with the current implementation will output the wrong + * value. + */ + get_integer_param(param, ¶m_int); + len = snprintf(val + bufpos, maxlen - bufpos, + "%"PRIu64" ", param_int); + break; + + case OPT_BOOL: + get_integer_param(param, ¶m_int); + len = snprintf(val + bufpos, maxlen - bufpos, "%s ", + param_int ? "true" : "false"); + break; + + case OPT_CUSTOM: + /* Custom parameters are not supported yet. */ + rc = -EINVAL; + break; + + default: + BUG(); + break; + } + + if ( len < 0 ) + rc = len; + else if ( len < maxlen - bufpos ) + /* if output was not truncated update buffer position. */ + bufpos += len; + else if ( len > 0 ) + rc = -ENOMEM; + + break; + } + + /* no parameter was matched */ + if ( param >= __param_end ) + rc = -EINVAL; + } + + return rc; +} + static int parse_params(const char *cmdline, const struct kernel_param *start, const struct kernel_param *end) { diff --git a/xen/common/sysctl.c b/xen/common/sysctl.c index c0aa6bde4e..20be6a6d8d 100644 --- a/xen/common/sysctl.c +++ b/xen/common/sysctl.c @@ -466,9 +466,9 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) copyback = 1; break; +#define XEN_PARAMETER_MAX_SIZE 1023 case XEN_SYSCTL_set_parameter: { -#define XEN_SET_PARAMETER_MAX_SIZE 1023 char *params; if ( op->u.set_parameter.pad[0] || op->u.set_parameter.pad[1] || @@ -477,7 +477,7 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret = -EINVAL; break; } - if ( op->u.set_parameter.size > XEN_SET_PARAMETER_MAX_SIZE ) + if ( op->u.set_parameter.size > XEN_PARAMETER_MAX_SIZE ) { ret = -E2BIG; break; @@ -501,6 +501,54 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) break; } + case XEN_SYSCTL_get_parameter: + { + char *params, *values; + + if ( op->u.get_parameter.pad[0] || op->u.get_parameter.pad[1] || + op->u.get_parameter.pad[2] ) + { + ret = -EINVAL; + break; + } + if ( op->u.get_parameter.size > XEN_PARAMETER_MAX_SIZE ) + { + ret = -E2BIG; + break; + } + params = xmalloc_bytes(op->u.get_parameter.size + 1); + if ( !params ) + { + ret = -ENOMEM; + break; + } + + values = xmalloc_bytes(XEN_PARAMETER_MAX_SIZE); + if ( !values ) + { + xfree(params); + ret = -ENOMEM; + break; + } + + if ( copy_from_guest(params, op->u.get_parameter.params, + op->u.get_parameter.size) ) + ret = -EFAULT; + else + { + params[op->u.set_parameter.size] = 0; + ret = runtime_get_params(params, values, XEN_PARAMETER_MAX_SIZE); + + if ( !ret && copy_to_guest(op->u.get_parameter.values, values, + strlen(values)) ) + ret = -EFAULT; + } + + xfree(params); + xfree(values); + + break; + } default: ret = arch_do_sysctl(op, u_sysctl); diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index c49b4dcc99..7d77d57115 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -1100,6 +1100,22 @@ typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t); #endif +/* + * XEN_SYSCTL_get_parameter + * + * Read hypervisor parameters at runtime. + * Parameters are a single string terminated by a NUL byte of max. size + * characters. Multiple settings can be specified by separating them + * with blanks. + */ + +struct xen_sysctl_get_parameter { + XEN_GUEST_HANDLE_64(char) params; /* IN: pointer to parameters. */ + XEN_GUEST_HANDLE_64(char) values; /* OUT: pointer to output values. */ + uint16_t size; /* IN: size of parameters. */ + uint16_t pad[3]; /* IN: MUST be zero. */ +}; + struct xen_sysctl { uint32_t cmd; #define XEN_SYSCTL_readconsole 1 @@ -1130,6 +1146,7 @@ struct xen_sysctl { #define XEN_SYSCTL_livepatch_op 27 #define XEN_SYSCTL_set_parameter 28 #define XEN_SYSCTL_get_cpu_policy 29 +#define XEN_SYSCTL_get_parameter 30 uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */ union { struct xen_sysctl_readconsole readconsole; @@ -1162,6 +1179,7 @@ struct xen_sysctl { #if defined(__i386__) || defined(__x86_64__) struct xen_sysctl_cpu_policy cpu_policy; #endif + struct xen_sysctl_get_parameter get_parameter; uint8_t pad[128]; } u; }; diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index e0b7bcb6b7..6e6367bb7a 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -71,6 +71,7 @@ struct domain; void cmdline_parse(const char *cmdline); int runtime_parse(const char *line); int parse_bool(const char *s, const char *e); +int runtime_get_params(const char *cmdline, char *values, size_t maxlen); /** * Given a specific name, parses a string of the form: diff --git a/xen/xsm/flask/hooks.c b/xen/xsm/flask/hooks.c index 3d00c747f6..1b832e9a4c 100644 --- a/xen/xsm/flask/hooks.c +++ b/xen/xsm/flask/hooks.c @@ -830,6 +830,9 @@ static int flask_sysctl(int cmd) case XEN_SYSCTL_set_parameter: return avc_current_has_perm(SECINITSID_XEN, SECCLASS_XEN2, XEN2__SET_PARAMETER, NULL); + case XEN_SYSCTL_get_parameter: + return avc_current_has_perm(SECINITSID_XEN, SECCLASS_XEN2, + XEN2__GET_PARAMETER, NULL); default: return avc_unknown_permission("sysctl", cmd); diff --git a/xen/xsm/flask/policy/access_vectors b/xen/xsm/flask/policy/access_vectors index e00448b776..c5ee21d852 100644 --- a/xen/xsm/flask/policy/access_vectors +++ b/xen/xsm/flask/policy/access_vectors @@ -103,6 +103,8 @@ class xen2 coverage_op # XEN_SYSCTL_set_parameter set_parameter +# XEN_SYSCTL_get_parameter + get_parameter } # Classes domain and domain2 consist of operations that a domain performs on