Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3128551pxj; Mon, 17 May 2021 18:31:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwy/QioSqL9RLFdEmmy2V16X8uPqUs/HygTEThmB6EPwIkjpQ5eb+UtQ2lezfFpmvS7Wtg6 X-Received: by 2002:a02:c85b:: with SMTP id r27mr2817594jao.43.1621301516547; Mon, 17 May 2021 18:31:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621301516; cv=none; d=google.com; s=arc-20160816; b=BK5JmtfJRXW5zCZW6KH8TEbRJaXjfc7inNah1Sf1y4AylmzNqEumKeSUyjI3Bk7QX4 3rxIBge/F35WfAVlPfp0nIxBCLLNLeH9w77Rc1fj3+28cS14d038RVYNVmMb+ska8MNx qjtY75FZn4//1EgjHHAMIWPDpADqVAz7RKw+joz8Qj3FsLjZvTfgmuRKFHdcWjnnImY4 aN+YTcV7AeWTtoI68wDuvqQXLkmwaXpNdkLClh0ZAKM6SGd47ur/nJsfdLGvE8xCW30H QX1a+E7khVCTTxxWJRzG+sAq0w/H2KVDUs5JwlGqubr2pbsQSHsMkQboK+naiILfngOi nc9g== 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=4X7u+VGz4IpYt8BZe1L2ysVpF6Gbv3vNGqktT0XsPPA=; b=ajYthmXvfJDogA6FY79q2rzFpBTtcMRkpA1GY2vfYygIL9pH8oNhaofZhIr1ss1kPM 7WQXmeI12DQLOfjhVFRUIGDG2zeze09d/Zfs+muWaDBykirhxq+bO74f8ZzDI8eMm5lS cnqxHqK1fiZIYSr66iKPnTOeomW8LTgv29f3JeyvortV7sNoIZO1yyC15GMHk/Ckuy+d HpQAYl8gsC4v8VIzqLKdkyo6bvGxeDkFnU7zr2vT1sSXVBOYa01mTm8lq5Ec51Gzifpb bs4qRvUvFoJ5HnyFbkapkjGZ8rQRW90ZA2ZdNA5D45JgBG0LwAq0Xr44g+g/010FZm9g fosQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JbukH8U2; 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 o8si20900040ilu.133.2021.05.17.18.31.43; Mon, 17 May 2021 18:31:56 -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=JbukH8U2; 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 S240436AbhEQOeR (ORCPT + 99 others); Mon, 17 May 2021 10:34:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:40160 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239109AbhEQO2j (ORCPT ); Mon, 17 May 2021 10:28:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EA4D961629; Mon, 17 May 2021 14:14:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621260867; bh=jirZkGEe5wWgnF+tmpHDzHT+ftUUibXCIsygCdyUXZU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JbukH8U2Vy5Gm1BgZuwa6v/uIxUYlW1rCb51iX3S3lNtJsNSO3vDP1wzPepYuFEGH BmbziLpOgYDeKfxvFgYAO4YPreJwelni+DCgABfqDoDMqaQuptZQ/WJ07Lo7BiE0so gSqrBKGMfuLah8r4z1EZsZJlWFiP/pa+qtFS1aH0= 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.12 256/363] userfaultfd: release page in error path to avoid BUG_ON Date: Mon, 17 May 2021 16:02:02 +0200 Message-Id: <20210517140311.257482045@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.508966430@linuxfoundation.org> References: <20210517140302.508966430@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 @@ -2375,8 +2375,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);