Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp406116rwb; Thu, 18 Aug 2022 06:05:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR6dPNKuLhYLwONlL4Fbdylh6QAOgh91gUxileF3MDJFR34f4hqrE0dWRzRxb1josOHu4qKC X-Received: by 2002:a17:903:248:b0:172:7520:db04 with SMTP id j8-20020a170903024800b001727520db04mr2586834plh.99.1660827932702; Thu, 18 Aug 2022 06:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660827932; cv=none; d=google.com; s=arc-20160816; b=jTmi/GNb0mdkQPjX4kJKVralarygD7n+nRh4aqwN51ezSE4vPGDmB8feirkVAGGxUa 3Og0sCB1526TVu7cW+N0oJXE5q98UbBba7b2pFBneQ5eYe78b3jNlXAgffXE1uD8HK1V xDBKUZnb48y09bWgmxaHlxqrQdYFcYAmVv2CRO98eZFnVkFnBpaTCoa2C3Cmy5u7Tbme GzrjYUnviuNIo4uMNxdetdDlSuTiah3ZUE7Jkm2umZGwsGYHb0yFtn8Z85EtUyHJpinY zRk/B0dygn0u+dW6nmuLXNNSiBLT+8GpC2LVfyTquH3EtsF7Q1uw0WXbCvxM2VTp9d2i tg1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=iyJ8a0+Q11wpj6Xm1lfvhar/WsSf14ZnhDpis/hRpGc=; b=O2hCDhlb44E6UFc29S1HKvwTNBV7iMW9zg/+4G4PKOTInoPDgf75xOWdTN0q6788mP /gEt94GFxol64b/vs/XY5afFL0NDNPaM2SspGQ3vCu620AsB6im9C+hgD0qA6XE76eb+ V57uugzjLE9Tl+6cdAh4NoOqOPJIj0DaErQM2vtbQoZVFn7d0vT0LgaKkwL1oz6e6VON cXrIm0kLx9tCoVZQhsNZAC6n1JxOwYjHLUhFuPAURdlRwgUmjRk9gTr4cI5awcuBtRc7 A8pYEnt6AEedX4QytqWpbQg1izoC2Txq4G7XluzX7X4zVDzZEU47V0E0rOJ82Yh9xd2n 2n3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nUN1J9Tl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c5-20020a170902c1c500b0016f063c3ea1si1227140plc.30.2022.08.18.06.05.20; Thu, 18 Aug 2022 06:05:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nUN1J9Tl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244924AbiHRNCn (ORCPT + 99 others); Thu, 18 Aug 2022 09:02:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244217AbiHRNC2 (ORCPT ); Thu, 18 Aug 2022 09:02:28 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FAB3B4422 for ; Thu, 18 Aug 2022 06:02:16 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id s31-20020a17090a2f2200b001faaf9d92easo4655047pjd.3 for ; Thu, 18 Aug 2022 06:02:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=iyJ8a0+Q11wpj6Xm1lfvhar/WsSf14ZnhDpis/hRpGc=; b=nUN1J9TlzwwbgCx+cs97MtgVnPoAfhc7Z6wLYnhf9XlftasErYgstZqCOOP+poaERd 15bfVK3uM8nx83r/3B4gJjAs7YLniAF1qcg7dLYDEBufJS6HtoaW42J5tvHxTwINnFoq Hpj7PnDXHI216Uf7pAH35zGiMX765S/x6bNgMegD+xFS6I0xVHNQAKQphgKVDZiNj1bl 6UvSb/c3XTKBzqsqmu2M/lrasRG1+1rvSTkIU24BnKsDITp1HfQE98auGeUTyLzFuifJ vnLw4PV9A4zKHNY9bqDWk24yw67Hc5hZSN8vMw/oKKSnWmx9Pzw0Fm3CcnYwzec0KiwS kZsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=iyJ8a0+Q11wpj6Xm1lfvhar/WsSf14ZnhDpis/hRpGc=; b=S0Lt16MRmJOPMTnRmFSkOpeAmiosql59W8j35KWLoI3c0tDPVxeAUYQzslcX456a+q 3gP8InSNKiY5EQYVNP42HRvmbOtuNfGlCRZ8dw7DAQV3dzZYik6eIX3dFHW7vnzMUmI2 qUWg1EGYj6zCs3KvJWiH+1+W3+e90yzPqREkp9B/bk1OYZqLhTKwnyYxfItqArbViiqw r9siiVT82l63lI2ABsnrIH0aMYThRJR/Gci6so99ywCwBoBtfcEkzCuhR5dfJCpiyjU/ 7tKD6byRVqukrzNNRLlDKN/Gh/lH+DCA6cZSagd4Q/gXGhvnsr0IxXehT2C0G0RRCC/x mQ0w== X-Gm-Message-State: ACgBeo37qJeLtFuflKzhXE5NQm6CeZTWYabZDRf9hurbwdX+qL31bKps 2ZxIZXyJpH3budzBdFl4X7C/YrrAjFFNUhKibHH2ww== X-Received: by 2002:a17:902:d50b:b0:172:a41f:b204 with SMTP id b11-20020a170902d50b00b00172a41fb204mr2543037plg.70.1660827735062; Thu, 18 Aug 2022 06:02:15 -0700 (PDT) MIME-Version: 1.0 References: <20220818110859.1918035-1-jens.wiklander@linaro.org> In-Reply-To: From: Jens Wiklander Date: Thu, 18 Aug 2022 15:02:04 +0200 Message-ID: Subject: Re: [PATCH v2] tee: add overflow check in register_shm_helper() To: Sumit Garg Cc: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org, Linus Torvalds , stable@vger.kernel.org, Nimish Mishra , Anirban Chakraborty , Debdeep Mukhopadhyay , Jerome Forissier Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Sumit, On Thu, Aug 18, 2022 at 2:41 PM Sumit Garg wrote: > > Hi Jens, > > On Thu, 18 Aug 2022 at 16:39, Jens Wiklander wrote: > > > > With special lengths supplied by user space, register_shm_helper() has > > an integer overflow when calculating the number of pages covered by a > > supplied user space memory region. This causes > > internal_get_user_pages_fast() a helper function of > > pin_user_pages_fast() to do a NULL pointer dereference. > > > > [ 14.141620] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000010 > > [ 14.142556] Mem abort info: > > [ 14.142829] ESR = 0x0000000096000044 > > [ 14.143237] EC = 0x25: DABT (current EL), IL = 32 bits > > [ 14.143742] SET = 0, FnV = 0 > > [ 14.144052] EA = 0, S1PTW = 0 > > [ 14.144348] FSC = 0x04: level 0 translation fault > > [ 14.144767] Data abort info: > > [ 14.145053] ISV = 0, ISS = 0x00000044 > > [ 14.145394] CM = 0, WnR = 1 > > [ 14.145766] user pgtable: 4k pages, 48-bit VAs, pgdp=000000004278e000 > > [ 14.146279] [0000000000000010] pgd=0000000000000000, p4d=0000000000000000 > > [ 14.147435] Internal error: Oops: 96000044 [#1] PREEMPT SMP > > [ 14.148026] Modules linked in: > > [ 14.148595] CPU: 1 PID: 173 Comm: optee_example_a Not tainted 5.19.0 #11 > > [ 14.149204] Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 > > [ 14.149832] pstate: 604000c5 (nZCv daIF +PAN -UAO -TCO -DIT -SSBS BTYPE=--) > > [ 14.150481] pc : internal_get_user_pages_fast+0x474/0xa80 > > [ 14.151640] lr : internal_get_user_pages_fast+0x404/0xa80 > > [ 14.152408] sp : ffff80000a88bb30 > > [ 14.152711] x29: ffff80000a88bb30 x28: 0000fffff836d000 x27: 0000fffff836e000 > > [ 14.153580] x26: fffffc0000000000 x25: fffffc0000f4a1c0 x24: ffff00000289fb70 > > [ 14.154634] x23: ffff000002702e08 x22: 0000000000040001 x21: ffff8000097eec60 > > [ 14.155378] x20: 0000000000f4a1c0 x19: 00e800007d287f43 x18: 0000000000000000 > > [ 14.156215] x17: 0000000000000000 x16: 0000000000000000 x15: 0000fffff836cfb0 > > [ 14.157068] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 > > [ 14.157747] x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000 > > [ 14.158576] x8 : ffff00000276ec80 x7 : 0000000000000000 x6 : 000000000000003f > > [ 14.159243] x5 : 0000000000000000 x4 : ffff000041ec4eac x3 : ffff000002774cb8 > > [ 14.159977] x2 : 0000000000000004 x1 : 0000000000000010 x0 : 0000000000000000 > > [ 14.160883] Call trace: > > [ 14.161166] internal_get_user_pages_fast+0x474/0xa80 > > [ 14.161763] pin_user_pages_fast+0x24/0x4c > > [ 14.162227] register_shm_helper+0x194/0x330 > > [ 14.162734] tee_shm_register_user_buf+0x78/0x120 > > [ 14.163290] tee_ioctl+0xd0/0x11a0 > > [ 14.163739] __arm64_sys_ioctl+0xa8/0xec > > [ 14.164227] invoke_syscall+0x48/0x114 > > [ 14.164653] el0_svc_common.constprop.0+0x44/0xec > > [ 14.165130] do_el0_svc+0x2c/0xc0 > > [ 14.165498] el0_svc+0x2c/0x84 > > [ 14.165847] el0t_64_sync_handler+0x1ac/0x1b0 > > [ 14.166258] el0t_64_sync+0x18c/0x190 > > [ 14.166878] Code: 91002318 11000401 b900f7e1 f9403be1 (f820d839) > > [ 14.167666] ---[ end trace 0000000000000000 ]--- > > > > Fix this by adding an overflow check when calculating the end of the > > memory range. Also add an explicit call to access_ok() in > > tee_shm_register_user_buf() to catch an invalid user space address > > early. > > > > Fixes: 033ddf12bcf5 ("tee: add register user memory") > > Cc: stable@vger.kernel.org > > Reported-by: Nimish Mishra > > Reported-by: Anirban Chakraborty > > Reported-by: Debdeep Mukhopadhyay > > Suggested-by: Jerome Forissier > > Signed-off-by: Jens Wiklander > > --- > > drivers/tee/tee_shm.c | 23 +++++++++++++++++++---- > > 1 file changed, 19 insertions(+), 4 deletions(-) > > > > I can't see the v1 and neither a changelog for v2, so my comments > below may be duplicate. Fair point. The original patch wasn't posted publicly, but in order to avoid confusion with that patch I chose to publish this as V2. > > > diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c > > index f2b1bcefcadd..f71651021c8d 100644 > > --- a/drivers/tee/tee_shm.c > > +++ b/drivers/tee/tee_shm.c > > @@ -231,15 +231,30 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); > > > > static struct tee_shm * > > register_shm_helper(struct tee_context *ctx, unsigned long addr, > > - size_t length, u32 flags, int id) > > + unsigned long length, u32 flags, int id) > > { > > struct tee_device *teedev = ctx->teedev; > > + unsigned long end_addr; > > struct tee_shm *shm; > > unsigned long start; > > size_t num_pages; > > void *ret; > > int rc; > > > > + /* Check for overflows, this may be input from user space */ > > IMO, this bound checking should be part of the parent function (like > tee_shm_register_user_buf() in this case). I don't see any harm in checking it here even if it will then check input from tee_shm_register_kernel_buf() too. Then I'm also reusing the result in the roundup() and that should be done in this function. > > > + addr = untagged_addr(addr); > > + start = rounddown(addr, PAGE_SIZE); > > + if (check_add_overflow(addr, length, &end_addr)) > > + return ERR_PTR(-EINVAL); > > Isn't this check redundant after access_ok()? AFAICS, access_ok() > should limit the upper bound to TASK_SIZE_MAX which should detect any > overflows. It may be redundant, depending on the configuration. It's likely redundant on all platforms we care about at the moment, but who knows where this will be used in the future. > > > + end_addr = roundup(end_addr, PAGE_SIZE); > > + if (end_addr < start) > > + return ERR_PTR(-EINVAL); > > Ditto? Yeah, same argument. Thanks, Jens > > -Sumit > > > + num_pages = (end_addr - start) / PAGE_SIZE; > > + > > + /* Error out early if no pages are to be registered */ > > + if (!num_pages) > > + return ERR_PTR(-EINVAL); > > + > > if (!tee_device_get(teedev)) > > return ERR_PTR(-EINVAL); > > > > @@ -261,11 +276,8 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr, > > shm->flags = flags; > > shm->ctx = ctx; > > shm->id = id; > > - addr = untagged_addr(addr); > > - start = rounddown(addr, PAGE_SIZE); > > shm->offset = addr - start; > > shm->size = length; > > - num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE; > > shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); > > if (!shm->pages) { > > ret = ERR_PTR(-ENOMEM); > > @@ -326,6 +338,9 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, > > void *ret; > > int id; > > > > + if (!access_ok((void __user *)addr, length)) > > + return ERR_PTR(-EFAULT); > > + > > mutex_lock(&teedev->mutex); > > id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL); > > mutex_unlock(&teedev->mutex); > > -- > > 2.31.1 > >