Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp150825pxv; Wed, 30 Jun 2021 02:13:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFM1+eu0YHRb4OnIi4mA9+RM6p9eTSfETGPTW3cBg6ns86Ps8uO8AtRFkVMI+aKzvtGSEI X-Received: by 2002:a05:6602:c1:: with SMTP id z1mr7021928ioe.93.1625044435146; Wed, 30 Jun 2021 02:13:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625044435; cv=none; d=google.com; s=arc-20160816; b=QF9WsA9RDjtJD3RRsRake8oaPMoxriarXDoFs7VDE5QEC0PJrtHaypBb5F2yjwrnw5 Ev/3hoKfhfQbcqyQlBLNCHHBL+K2DyValndASWuUhivILdXFb1Hud7OHbYELFHotbndg YQE7GLJTaGddU/80X6ulx3/yGcIorgqhFT+panFwUdQ+INPYAg2nAdpIivPr6b8Ghb0g kihmT8bT6Hz3VK68DSnMg0rWL0SfHNS5/AjLWWltIBnGYYX0dF7CTKtQ0a6uk1KbXEnO 9vjMGn0s2akQL5fYhHHXepZ8E1B6Oq7PBs9QZryjpmATHqRnUM8gv796vM6KmNcFwwHt momQ== 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=1H4R63oRVDHcDVqGB57j4YDUXpMV3Sy+qXVheOxI8hs=; b=Yh5scKFmvpCMBnQSUcBqfISR7SuDLNcD5XAxOq1j5DoyU+xiJ8fXnXD0J2CgNvRuRf HzSXg4jPRJvtWSgtXb+CX1vOAxiCCkW/kDXDC7qhPpUWZnMQcCUE+v6x2dCn4FoeHHGd Kh5KYZjDgCZgCnADImaFjCnWZawv+T9+9wRCONEz+699kJ2eHj2Kexx/iIWDGTqNfeM1 7coor0HojCRPXZArgiimQ/erYr8gKOCVU1Wo7h26uzEjqKtvqJnDyid+zxgy+9eGSF47 or7I3mPl2Qgnal8cXhxmZ5aW89o7636jjpyyqcjmaIVVdmoOFeSyJ6kTFbiV9Fv9j9+M 0KgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lW99uWA9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e6si2512719iov.1.2021.06.30.02.13.43; Wed, 30 Jun 2021 02:13:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lW99uWA9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233736AbhF3JPl (ORCPT + 99 others); Wed, 30 Jun 2021 05:15:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:54706 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233541AbhF3JPk (ORCPT ); Wed, 30 Jun 2021 05:15:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4797061D05 for ; Wed, 30 Jun 2021 09:13:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625044392; bh=J2MOPPIp5YIk+RoNqiEpM/27M2PC8jB9VCYZvKMRjKE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=lW99uWA911R48Z/wX80vMxUdNRoKC6SqFJmh8e4wVzaI+2qhYE/LRFP524heeh8DL zdyBrgCm4vFsnzvqykM2g5r5nXFq/grG/ka+t+NCaC5ILHuC8uvoBD/nVXH+knrrqx iymjrIyW43fe6ydeChoweDglWNQPnbLtNjkb/oBCwIvDOlmwSG6hzFio1fEOXj3n4T VDydykPEX1AA8vhc4buewgc7r55/I0KqtuU7tf3lIVicJr76nOH4AGFHDiaihSKikX mwW0KfkCihEjL7BgtYp1JNBN4z63AUg5cfJPsgTs5cuZjlj6sZ5G2lPhW0633+yWHE FfyqN/kngr/xA== Received: by mail-oo1-f48.google.com with SMTP id s10-20020a4aeaca0000b029024c2acf6eecso453264ooh.9 for ; Wed, 30 Jun 2021 02:13:12 -0700 (PDT) X-Gm-Message-State: AOAM531IijvAsKVLSjNLR0JSQsuB/FQoP6vrg9Ne5wudNGpQtqmE0RzL ETnaAHqaugbFEyBOPuNtfkUiv8hGaqm9QnUkwDY= X-Received: by 2002:a4a:2f87:: with SMTP id p129mr7656173oop.41.1625044391555; Wed, 30 Jun 2021 02:13:11 -0700 (PDT) MIME-Version: 1.0 References: <000000000000f3e94a05c5d8686f@google.com> <20210630080934.GA24186@gondor.apana.org.au> In-Reply-To: <20210630080934.GA24186@gondor.apana.org.au> From: Ard Biesheuvel Date: Wed, 30 Jun 2021 11:13:00 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [syzbot] BUG: sleeping function called from invalid context in __fdget_pos To: Herbert Xu Cc: Dave Hansen , syzbot , Borislav Petkov , "H. Peter Anvin" , jpa@git.mail.kapsi.fi, kan.liang@linux.intel.com, Linux Kernel Mailing List , Andy Lutomirski , Ingo Molnar , syzkaller-bugs , Thomas Gleixner , X86 ML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 30 Jun 2021 at 10:10, Herbert Xu wrote: > > Hi Ard: > > On Wed, Jun 30, 2021 at 09:42:14AM +0200, Ard Biesheuvel wrote: > > > > > There's one suspect-looking site in xts_crypt(): > > > > > > > kernel_fpu_begin(); > > > > > > > > /* calculate first value of T */ > > > > aesni_enc(aes_ctx(ctx->raw_tweak_ctx), walk.iv, walk.iv); > > > > > > > > while (walk.nbytes > 0) { > > > > int nbytes = walk.nbytes; > > > > > > > > ... > > > > > > > > err = skcipher_walk_done(&walk, walk.nbytes - nbytes); > > > > > > > > kernel_fpu_end(); > > > > > > > > if (walk.nbytes > 0) > > > > kernel_fpu_begin(); > > > > } > > > > > > I wonder if a slab allocation failure could leave us with walk.nbytes==0. > > > > The code is actually the other way around: kernel_fpu_end() comes > > before the call to skcipher_walk_done(). > > > > So IIUC, this code forces an allocation failure, and checks whether > > the code deals with this gracefully, right? > > > > The skcipher walk API guarantees that walk.nbytes == 0 if an error is > > returned, so the pairing of FPU begin/end looks correct to me. And > > skcipher_walk_next() should not invoke anything that might sleep from > > this particular context. > > > > Herbert, any ideas? > > xts_crypt looks buggy to me. In particular, if the second > skcipher_walk_virt call (the one in the if clause) fails, then > we will return without calling kernel_fpu_end. > > Another issue, we are not checking for errors on the first > skcipher_walk_virt call, this may cause a double-free with > the subsequent skcipher_walk_abort inside the if clause. > > With skcikpher_walk_virt, you must check for errors explicitly > *unless* you use it in a loop construct which exits on !walk->nbytes. > So something like this, I suppose? --- a/arch/x86/crypto/aesni-intel_glue.c +++ b/arch/x86/crypto/aesni-intel_glue.c @@ -849,6 +849,8 @@ return -EINVAL; err = skcipher_walk_virt(&walk, req, false); + if (err) + return err; if (unlikely(tail > 0 && walk.nbytes < walk.total)) { int blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2; @@ -862,7 +864,10 @@ skcipher_request_set_crypt(&subreq, req->src, req->dst, blocks * AES_BLOCK_SIZE, req->iv); req = &subreq; + err = skcipher_walk_virt(&walk, req, false); + if (err) + return err; } else { tail = 0; }