Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp547743pxj; Tue, 18 May 2021 09:02:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRjgWjaKuvgQrVtuth2W4rQ9ODLsTM1vN7/hewZl4jjigjP77L7MS7AcTl2etX19jre0NM X-Received: by 2002:a17:906:bc86:: with SMTP id lv6mr6764323ejb.333.1621353726648; Tue, 18 May 2021 09:02:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621353726; cv=none; d=google.com; s=arc-20160816; b=u+coazNl8WhkjjatxS8kfFICR8cGjTemL9U+vpuoBtFYnpz5zhNmTv05FYhRtjk4HC 2BXUBQKsj6/Y5h1PxYM6WD9B6qvk3HAKeNVGFCuFM5NIKQn0Dk16qzrPCqM3bOlMTkdM tvoAkwrtZxiq7ap3qO1SgYS19Mw7mluAPcwbPYSGGLsO6hQub5Zy0dUCQjpBseC1Mb6X PHoMd5uU1zfplJL5kmUIT4IYE7NNYseBHzDsVqLsyc3s90uwxNa8fmgDr4c3Nz0TDMu/ oN+mIsaxGvNR9AaBL7tnHLkoKHBym6QGZdrPOsPevigUgN/nsSNGzGTZ9pX4QAIFhZYn u7Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/U35RPTQxJ3VPU4et4RspscyjjBH8gB8qg6gIn/kx2g=; b=d2kRAt8waiwhPjJS2hox1KACC8mSjsS1MibzgMls4/e6dcU3p9+Eydws5ZaUdaljM/ IHpI5zryUZXOMFdEgePQiN+AOK3g6vrb4OkeFrZojs2xcXnasRJVKHsmE4r8/6zhjOdA uRX9wWOWVAflayE4U2RwET3Ur7wULahG7caqm/cmKK/dSByoD1arbV6ClJmEq8Rtd5eu P22KU6BB4Ebet4CxkI/HBf2pkRDTXjw8ofBCjta37+0qTxG1uhNcDRafK2xcIkIUyYIF 3vWhewtuneWQxRFmy4yQwV1X++AHUUKuh5hek+d/WdzQEqkWQPqCjE2wZDlkFo+WCJds klRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pOIG8vnz; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cw1si17402329ejb.544.2021.05.18.09.01.41; Tue, 18 May 2021 09:02:06 -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=@linuxfoundation.org header.s=korg header.b=pOIG8vnz; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344618AbhEQP7s (ORCPT + 99 others); Mon, 17 May 2021 11:59:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:52066 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343955AbhEQPkE (ORCPT ); Mon, 17 May 2021 11:40:04 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4D34D60E09; Mon, 17 May 2021 14:41:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621262496; bh=f2OF7iqAYPnz+i8TMq4e2ncFDmzmxlCAFW+zBxs/dDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pOIG8vnz9in+TORLP4xhA2Sl1P37M0MIH4RBKTUOo4z04hs4J0rxF0CsfUqTffiWE AFeZPZwpVe/60V8nQoYDmORcKv6YcqtWdMrQ7AfIW/ZDKRQWwIvZOjq2AKvYOFzCsx sKYge3H3LOfSdg7QGCSu1q14B5ZpvmdP9jd512AI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Axel Rasmussen , Hugh Dickins , Peter Xu , Andrew Morton , Linus Torvalds Subject: [PATCH 5.10 201/289] userfaultfd: release page in error path to avoid BUG_ON Date: Mon, 17 May 2021 16:02:06 +0200 Message-Id: <20210517140311.885708418@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140305.140529752@linuxfoundation.org> References: <20210517140305.140529752@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Axel Rasmussen commit 7ed9d238c7dbb1fdb63ad96a6184985151b0171c upstream. Consider the following sequence of events: 1. Userspace issues a UFFD ioctl, which ends up calling into shmem_mfill_atomic_pte(). We successfully account the blocks, we shmem_alloc_page(), but then the copy_from_user() fails. We return -ENOENT. We don't release the page we allocated. 2. Our caller detects this error code, tries the copy_from_user() after dropping the mmap_lock, and retries, calling back into shmem_mfill_atomic_pte(). 3. Meanwhile, let's say another process filled up the tmpfs being used. 4. So shmem_mfill_atomic_pte() fails to account blocks this time, and immediately returns - without releasing the page. This triggers a BUG_ON in our caller, which asserts that the page should always be consumed, unless -ENOENT is returned. To fix this, detect if we have such a "dangling" page when accounting fails, and if so, release it before returning. Link: https://lkml.kernel.org/r/20210428230858.348400-1-axelrasmussen@google.com Fixes: cb658a453b93 ("userfaultfd: shmem: avoid leaking blocks and used blocks in UFFDIO_COPY") Signed-off-by: Axel Rasmussen Reported-by: Hugh Dickins Acked-by: Hugh Dickins Reviewed-by: Peter Xu Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/shmem.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2378,8 +2378,18 @@ static int shmem_mfill_atomic_pte(struct pgoff_t offset, max_off; ret = -ENOMEM; - if (!shmem_inode_acct_block(inode, 1)) + if (!shmem_inode_acct_block(inode, 1)) { + /* + * We may have got a page, returned -ENOENT triggering a retry, + * and now we find ourselves with -ENOMEM. Release the page, to + * avoid a BUG_ON in our caller. + */ + if (unlikely(*pagep)) { + put_page(*pagep); + *pagep = NULL; + } goto out; + } if (!*pagep) { page = shmem_alloc_page(gfp, info, pgoff);