From patchwork Fri Aug 23 18:48:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 11112219 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 056C814DE for ; Fri, 23 Aug 2019 18:50:25 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D034321874 for ; Fri, 23 Aug 2019 18:50:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=epam.com header.i=@epam.com header.b="aY71xuS4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D034321874 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=epam.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i1EcF-0001i8-M7; Fri, 23 Aug 2019 18:48:51 +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 1i1EcE-0001hy-5Z for xen-devel@lists.xenproject.org; Fri, 23 Aug 2019 18:48:50 +0000 X-Inumbo-ID: a4076792-c5d6-11e9-adef-12813bfff9fa Received: from EUR01-VE1-obe.outbound.protection.outlook.com (unknown [40.107.14.44]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id a4076792-c5d6-11e9-adef-12813bfff9fa; Fri, 23 Aug 2019 18:48:48 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lq30uAqT7TttHlpmVWq+uKvkeXhuywLVeFBp0FhzCDazdCOey80K3iDxeP9RfXbHcPIqwL32ZcN+iyi5He1fe6EWJ5aqUrSb8WOPwcD64DGGv5W9eldgeWRphqh7NuSZjmWLMCl0VZTw3biyIcDRacSPZxxjPfqX3LvOnHl5g65hSjS63KnrRAtvayloNGEJ1YbDkbzwRu6HE/LZmhT+37qBvxpDKrNkOcHztBv3fww4ilDbTHxjm/OmoY4FBy0DZ2TNNazYIpoqjydjY7KaPKVhOMMsak4y0IfgAer5aiStgL6NYDyjlyS6S7BkZATX6hYFzCpb/Sq8oJ+3YxxSCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6wcxbcpo/okkTVzuNPy2P6JFd/kTE20LYJk6ij1ASiA=; b=BaP732LkfSptowFZe15cMDkYNbboJb9cgjPQtAJRzGzyirn8Yegy5L5BYIJP8Eq5hW6h7oH3wR8BwE/jrSsktM0RcgWw436X9xJO7Kd555RBa642ZAYHItJ2W0s6zWd5aKVVpcLW9J9wVvI8ZFRReBH84Kysuok/1o8kVK2Sk9gvBbawr8Yuj/Sv2GWt4XDAry5tjM0VBSOW5U0L6SrqJS7ps0tY4BCyGhmGsGawl9HrOa9Z+havTQOCUiVsFGe9uNtk/kUrfnp/0VJgeuB8cDvoPZPPiI4bI2GiQ0j3rN2Kr5o5TUG0mSrpW0vMcaOda7rjp0ywLPWa0ZTI4VmPOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6wcxbcpo/okkTVzuNPy2P6JFd/kTE20LYJk6ij1ASiA=; b=aY71xuS46n6jlaCShBuXvM3mP//FqPmLEayKPcdvLCvfFbsR28ty6sLQ5ArmmCtZMvDcuad3Cd5VE3nYsmltHvMHwUH0nEJo8t9TmSCrSaV+lNcQdXf8jeluP4/24i3O9Ge6WgNLfXp4c1xnD98p5JAsbYrmpBQg0Z/pTxrYcSUVEK0/F09qLygBdoJnT6+0DHGN27aaEYV0gTMdD/Nlay1Ge4XM5cN2q58vH7OZZIhO+TwtwCsJkW/4n8u/7IlwOUGhCphLmJlTUuBpU90t+0Trl6vpkUIB12GADBx52loJ6uYnE54+RxMPA6RRL0VKy6ufelbqTfZuYb8eQHcIRw== Received: from AM0PR03MB4148.eurprd03.prod.outlook.com (20.177.40.10) by AM0PR03MB4690.eurprd03.prod.outlook.com (20.177.41.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2178.18; Fri, 23 Aug 2019 18:48:46 +0000 Received: from AM0PR03MB4148.eurprd03.prod.outlook.com ([fe80::71e3:834d:5708:5a0a]) by AM0PR03MB4148.eurprd03.prod.outlook.com ([fe80::71e3:834d:5708:5a0a%5]) with mapi id 15.20.2199.015; Fri, 23 Aug 2019 18:48:46 +0000 From: Volodymyr Babchuk To: "xen-devel@lists.xenproject.org" Thread-Topic: [PATCH 1/5] xen/arm: optee: impose limit on shared buffer size Thread-Index: AQHVWeNlCY0CqDi7zE2giEuaPhvOCg== Date: Fri, 23 Aug 2019 18:48:46 +0000 Message-ID: <20190823184826.14525-2-volodymyr_babchuk@epam.com> References: <20190823184826.14525-1-volodymyr_babchuk@epam.com> In-Reply-To: <20190823184826.14525-1-volodymyr_babchuk@epam.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; x-originating-ip: [85.223.209.22] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: a8ddb2ad-9b70-4622-4ef3-08d727fa87ad x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(2017052603328)(7193020); SRVR:AM0PR03MB4690; x-ms-traffictypediagnostic: AM0PR03MB4690:|AM0PR03MB4690: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6790; x-forefront-prvs: 0138CD935C x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(4636009)(346002)(136003)(376002)(39860400002)(366004)(396003)(189003)(199004)(52084003)(6436002)(305945005)(25786009)(76176011)(81166006)(71190400001)(478600001)(7736002)(81156014)(5660300002)(8676002)(71200400001)(4326008)(186003)(6506007)(99286004)(2351001)(6486002)(1076003)(2501003)(26005)(2906002)(6916009)(66446008)(55236004)(66946007)(91956017)(14454004)(76116006)(66476007)(66556008)(36756003)(64756008)(6116002)(6512007)(446003)(11346002)(3846002)(476003)(2616005)(316002)(54906003)(86362001)(80792005)(102836004)(5640700003)(8936002)(66066001)(14444005)(486006)(256004)(53936002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM0PR03MB4690; H:AM0PR03MB4148.eurprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: epam.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: pljiRIPf4TvJOrfmikttJeu7cen9KdZLvrf4ZdH7TM9LDm9mbp/sykKctRenPu2P38S2DsObNvY9N7xSDSbqzjycBwchv7KUsxGi5CkXP1VG5gJV4jOK68+BM46H6l99lfeM/08z5+6SaAvNK2wWqgUu/b3GsTwR4unbn1zIySJ90HrvEro95frMH5dP2Qg5GyUNMc1i/qrzN+6MT9JjMbr2X0QJVsIEc+tp+qd/7PvVEbnX3ZX5v0lmH8XL1xUuSechz6Z71rwM/ubCdHbWryvnUC1YP3wRBkJJFM9vEtQhbYWbInzL4nu1Y/u3tV3Tx4wLoyhgR/rrtJtRhBsgh93LBmAoAt2wwU1I7qqlU+f3CdKPue3bmpbImBSr+etwfWZyI9qf29FZ6Y+cMjv1aLpEITBKwezZQL+B6XXaeUM= MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-Network-Message-Id: a8ddb2ad-9b70-4622-4ef3-08d727fa87ad X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Aug 2019 18:48:46.7004 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: fIVLU6+ahhQAguozHQDWqpiPDgJFgdLAiAAoCFphzDhYAnQAj3WQm0CKi3Fmz845bEZW5N0ZkvZX7rluIGtk/ygGqg0kAXMO8J7Jd3mqz8k= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB4690 Subject: [Xen-devel] [PATCH 1/5] xen/arm: optee: impose limit on shared buffer size 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: "tee-dev@lists.linaro.org" , Julien Grall , Stefano Stabellini , Volodymyr Babchuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" We want to limit number of calls to lookup_and_pin_guest_ram_addr() per one request. There are two ways to do this: either preempt translate_noncontig() or to limit size of one shared buffer size. It is quite hard to preempt translate_noncontig(), because it is deep nested. So we chose second option. We will allow 512 pages per one shared buffer. This does not interfere with GP standard, as it requires that size limit for shared buffer should be at lest 512kB. Also, with this limitation OP-TEE still passes own "xtest" test suite, so this is okay for now. Signed-off-by: Volodymyr Babchuk --- xen/arch/arm/tee/optee.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/xen/arch/arm/tee/optee.c b/xen/arch/arm/tee/optee.c index ec5402e89b..f4fa8a7758 100644 --- a/xen/arch/arm/tee/optee.c +++ b/xen/arch/arm/tee/optee.c @@ -72,6 +72,17 @@ */ #define MAX_TOTAL_SMH_BUF_PG 16384 +/* + * Arbitrary value that limits maximum shared buffer size. It is + * merely coincidence that it equals to both default OP-TEE SHM buffer + * size limit and to (1 << CONFIG_DOMU_MAX_ORDER). Please note that + * this define limits number of pages. But user buffer can be not + * aligned to a page boundary. So it is possible that user would not + * be able to share exactly MAX_SHM_BUFFER_PG * PAGE_SIZE bytes with + * OP-TEE. + */ +#define MAX_SHM_BUFFER_PG 512 + #define OPTEE_KNOWN_NSEC_CAPS OPTEE_SMC_NSEC_CAP_UNIPROCESSOR #define OPTEE_KNOWN_SEC_CAPS (OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM | \ OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM | \ @@ -697,15 +708,17 @@ static int translate_noncontig(struct optee_domain *ctx, size = ROUNDUP(param->u.tmem.size + offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE); pg_count = DIV_ROUND_UP(size, OPTEE_MSG_NONCONTIG_PAGE_SIZE); + if ( pg_count > MAX_SHM_BUFFER_PG ) + return -ENOMEM; + order = get_order_from_bytes(get_pages_list_size(pg_count)); /* - * In the worst case we will want to allocate 33 pages, which is - * MAX_TOTAL_SMH_BUF_PG/511 rounded up. This gives order 6 or at - * most 64 pages allocated. This buffer will be freed right after - * the end of the call and there can be no more than + * In the worst case we will want to allocate 2 pages, which is + * MAX_SHM_BUFFER_PG/511 rounded up. This buffer will be freed + * right after the end of the call and there can be no more than * max_optee_threads calls simultaneously. So in the worst case - * guest can trick us to allocate 64 * max_optee_threads pages in + * guest can trick us to allocate 2 * max_optee_threads pages in * total. */ xen_pgs = alloc_domheap_pages(current->domain, order, 0); @@ -747,13 +760,6 @@ static int translate_noncontig(struct optee_domain *ctx, xen_data = __map_domain_page(xen_pgs); } - /* - * TODO: That function can pin up to 64MB of guest memory by - * calling lookup_and_pin_guest_ram_addr() 16384 times - * (assuming that PAGE_SIZE equals to 4096). - * This should be addressed before declaring OP-TEE security - * supported. - */ BUILD_BUG_ON(PAGE_SIZE != 4096); page = get_domain_ram_page(gaddr_to_gfn(guest_data->pages_list[idx])); if ( !page )