From patchwork Mon Jun 16 16:17:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 31992 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f69.google.com (mail-pa0-f69.google.com [209.85.220.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9B3F321379 for ; Mon, 16 Jun 2014 16:20:12 +0000 (UTC) Received: by mail-pa0-f69.google.com with SMTP id kq14sf6220700pab.0 for ; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe:list-post :list-help:list-subscribe:mime-version:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list :list-archive:content-type:content-transfer-encoding; bh=HttljikU3zazQQr6HzNs4nR7y63PvzJrnffJwmZwdIU=; b=Zc6Ey/AW4XdU91sD0QGK1CJK9EF7VIZPQFmjbghuhUWF8OkFOljLKWDWkN7+3i4T4r Cyc/zarpqs8AGwojWf43cHF6jW+JLd5BK4e6g0uuMocatsY1mqyQ9rqxFao3MQJvLgP0 b9nfwljZrr6gf5wmB91GE+3RWKeTaNlQ19ydmAvis7oygUcH+D53HKl5yzYr984RR21I ll8mQlfidyMFHFPzwMWXsxWIvAJTUoi68AZr3PucQY1hdw93UPY/rkrvVquvhWfHY3tN dM+ph+FDv7moz4q7A+K5arffbREv+YfqNfwqWydbzIjYg3z59vPnzC7rdAm0msWnInie 71XQ== X-Gm-Message-State: ALoCoQkFLn4J4DbkXLnoKxdCcPKgLt6JkgGUDjZHqR5z9jq3hSq0gjxJH91WSiEchUfXmSKn9qXv X-Received: by 10.66.164.167 with SMTP id yr7mr1399893pab.15.1402935611981; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.19.83 with SMTP id 77ls4557380qgg.28.gmail; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-Received: by 10.220.85.1 with SMTP id m1mr1345092vcl.42.1402935611754; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) Received: from mail-vc0-f181.google.com (mail-vc0-f181.google.com [209.85.220.181]) by mx.google.com with ESMTPS id r6si4286263vcy.51.2014.06.16.09.20.11 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 16 Jun 2014 09:20:11 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.181 as permitted sender) client-ip=209.85.220.181; Received: by mail-vc0-f181.google.com with SMTP id il7so5225873vcb.12 for ; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-Received: by 10.58.186.207 with SMTP id fm15mr16859699vec.4.1402935611665; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.54.6 with SMTP id vs6csp146187vcb; Mon, 16 Jun 2014 09:20:11 -0700 (PDT) X-Received: by 10.52.254.34 with SMTP id af2mr735202vdd.69.1402935609506; Mon, 16 Jun 2014 09:20:09 -0700 (PDT) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id uc3si1437326vec.97.2014.06.16.09.20.09 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 16 Jun 2014 09:20:09 -0700 (PDT) Received-SPF: none (google.com: xen-devel-bounces@lists.xen.org does not designate permitted sender hosts) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WwZbv-0006hP-Pk; Mon, 16 Jun 2014 16:18:19 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WwZbu-0006hB-RB for xen-devel@lists.xenproject.org; Mon, 16 Jun 2014 16:18:19 +0000 Received: from [193.109.254.147:7896] by server-6.bemta-14.messagelabs.com id 55/48-13066-AC81F935; Mon, 16 Jun 2014 16:18:18 +0000 X-Env-Sender: julien.grall@linaro.org X-Msg-Ref: server-2.tower-27.messagelabs.com!1402935497!11592106!1 X-Originating-IP: [209.85.212.181] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 6.11.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 27901 invoked from network); 16 Jun 2014 16:18:17 -0000 Received: from mail-wi0-f181.google.com (HELO mail-wi0-f181.google.com) (209.85.212.181) by server-2.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 16 Jun 2014 16:18:17 -0000 Received: by mail-wi0-f181.google.com with SMTP id n3so4369995wiv.14 for ; Mon, 16 Jun 2014 09:18:17 -0700 (PDT) X-Received: by 10.194.79.36 with SMTP id g4mr13035616wjx.71.1402935496861; Mon, 16 Jun 2014 09:18:16 -0700 (PDT) Received: from belegaer.uk.xensource.com ([185.25.64.249]) by mx.google.com with ESMTPSA id l45sm34836161eep.25.2014.06.16.09.18.15 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Jun 2014 09:18:16 -0700 (PDT) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Mon, 16 Jun 2014 17:17:49 +0100 Message-Id: <1402935486-29136-3-git-send-email-julien.grall@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1402935486-29136-1-git-send-email-julien.grall@linaro.org> References: <1402935486-29136-1-git-send-email-julien.grall@linaro.org> Cc: Keir Fraser , ian.campbell@citrix.com, tim@xen.org, Julien Grall , Ian Jackson , stefano.stabellini@citrix.com, Jan Beulich , Daniel De Graaf Subject: [Xen-devel] [RFC 02/19] xen: guestcopy: Provide an helper to copy string from guest X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: julien.grall@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.181 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: Flask code already provides an helper to copy a string from guest. In a later patch, the new DT hypercalls will need a similar function. To avoid code duplication, copy the flask helper (flask_copying_string) to common code: - Rename into copy_string_from_guest - Update arguments. The size provided by the hypercall is unsigned not signed - Add comment to explain the extra +1 Signed-off-by: Julien Grall Cc: Daniel De Graaf Cc: Ian Campbell Cc: Ian Jackson Cc: Jan Beulich Cc: Keir Fraser Cc: Tim Deegan --- xen/common/Makefile | 1 + xen/common/guestcopy.c | 28 ++++++++++++++++++++++++++++ xen/include/xen/guest_access.h | 5 +++++ xen/xsm/flask/flask_op.c | 29 +++-------------------------- 4 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 xen/common/guestcopy.c diff --git a/xen/common/Makefile b/xen/common/Makefile index 3683ae3..627b6e3 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -9,6 +9,7 @@ obj-y += event_2l.o obj-y += event_channel.o obj-y += event_fifo.o obj-y += grant_table.o +obj-y += guestcopy.o obj-y += irq.o obj-y += kernel.o obj-y += keyhandler.o diff --git a/xen/common/guestcopy.c b/xen/common/guestcopy.c new file mode 100644 index 0000000..13bde81 --- /dev/null +++ b/xen/common/guestcopy.c @@ -0,0 +1,28 @@ +#include +#include +#include + +int copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf, char **buf, + unsigned long size, unsigned long max_size) +{ + char *tmp; + + if ( size > max_size ) + return -ENOENT; + + /* Add an extra +1 to append \0. We can't assume the guest will + * provide a valid string */ + tmp = xmalloc_array(char, size + 1); + if ( !tmp ) + return -ENOMEM; + + if ( copy_from_guest(tmp, u_buf, size) ) + { + xfree(tmp); + return -EFAULT; + } + tmp[size] = 0; + + *buf = tmp; + return 0; +} diff --git a/xen/include/xen/guest_access.h b/xen/include/xen/guest_access.h index 373454e..61756ae 100644 --- a/xen/include/xen/guest_access.h +++ b/xen/include/xen/guest_access.h @@ -8,6 +8,8 @@ #define __XEN_GUEST_ACCESS_H__ #include +#include +#include #define copy_to_guest(hnd, ptr, nr) \ copy_to_guest_offset(hnd, 0, ptr, nr) @@ -27,4 +29,7 @@ #define __clear_guest(hnd, nr) \ __clear_guest_offset(hnd, 0, nr) +int copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf, char **buf, + unsigned long size, unsigned long max_size); + #endif /* __XEN_GUEST_ACCESS_H__ */ diff --git a/xen/xsm/flask/flask_op.c b/xen/xsm/flask/flask_op.c index 7743aac..6847108 100644 --- a/xen/xsm/flask/flask_op.c +++ b/xen/xsm/flask/flask_op.c @@ -76,29 +76,6 @@ static int domain_has_security(struct domain *d, u32 perms) perms, NULL); } -static int flask_copyin_string(XEN_GUEST_HANDLE(char) u_buf, char **buf, - size_t size, size_t max_size) -{ - char *tmp; - - if ( size > max_size ) - return -ENOENT; - - tmp = xmalloc_array(char, size + 1); - if ( !tmp ) - return -ENOMEM; - - if ( copy_from_guest(tmp, u_buf, size) ) - { - xfree(tmp); - return -EFAULT; - } - tmp[size] = 0; - - *buf = tmp; - return 0; -} - #endif /* COMPAT */ static int flask_security_user(struct xen_flask_userlist *arg) @@ -112,7 +89,7 @@ static int flask_security_user(struct xen_flask_userlist *arg) if ( rv ) return rv; - rv = flask_copyin_string(arg->u.user, &user, arg->size, PAGE_SIZE); + rv = copy_string_from_guest(arg->u.user, &user, arg->size, PAGE_SIZE); if ( rv ) return rv; @@ -227,7 +204,7 @@ static int flask_security_context(struct xen_flask_sid_context *arg) if ( rv ) return rv; - rv = flask_copyin_string(arg->context, &buf, arg->size, PAGE_SIZE); + rv = copy_string_from_guest(arg->context, &buf, arg->size, PAGE_SIZE); if ( rv ) return rv; @@ -324,7 +301,7 @@ static int flask_security_resolve_bool(struct xen_flask_boolean *arg) if ( arg->bool_id != -1 ) return 0; - rv = flask_copyin_string(arg->name, &name, arg->size, bool_maxstr); + rv = copy_string_from_guest(arg->name, &name, arg->size, bool_maxstr); if ( rv ) return rv;