Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp453625pxj; Tue, 18 May 2021 07:08:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGvmp5hSJjgEOdtZ3rGp5UbRMl2idknRVoEUYt7ypNOpZ/qZIYcbmJ8dG8z0Y/m+Yo0vAD X-Received: by 2002:a6b:5015:: with SMTP id e21mr4359922iob.104.1621346889105; Tue, 18 May 2021 07:08:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621346889; cv=none; d=google.com; s=arc-20160816; b=gGsjWYzbptB0hYSLte4r2O3uewQUi3vU12RQ4oaXNcyw3ZJAdf+0v1i+/VvxvywneN kZKAii4T10epYy+FTJGnXbEuLEq9jzuEUuKnVNYsOm5jsW7EkwTf99mZL/+VFmYh4Wsq mDe5C2b8w4KykYYevp0p8qQWoEoSU1GjAWBwGvNK+5mlchl2I5SV2PdJDAgq+p4dr9HK +8Vx+U5DljKla94D6aEguSA1NtjjfS06TUzMlDYDUjOfgN4dpcoS7Dl0l3YKGpr+3zpW niQNU3yWFInOMZmSVn9jT2h7kbVbL9IIIUQvpYQdlQ/eCxM954RkkawcrQkuCiS35I2v xp6A== 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=Byrg9jdza2PRLu90DXGNN0XAUZYxJbbBQcd0I0cCNNU=; b=FSEXQr6vPbXG9LBj6Fuy3warjyLy49xKfZBdoW0k1g90Kvbmmg9HkqEqxipLqf9NiU nYEvkJhctxeLZ03KJnswLuUu+G8JsbXmFEGY9opokS532Wa6O/GBxWk77Q6xFyiP6Fu7 CKINm4+ODkog/ewiIU8bQTl4K8pv8GFmk0bXB4xy3rHzvq4MbrvzEzxTqg00CBlTsaN+ yEdnvOgaCpVmZoar/dCEux/HeK1T77GDq+zK0aM9K7rX79Grg+LQVIhYPlZZHJxpuRgr nBX+bCzsl2bSeDIrxsBKOQKJI3QXf47JdgidENDuiIvOT+V8zk3WA2Kwdcd+QOVLEDS0 EBKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1PyEVc3t; 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 b18si6982454ils.16.2021.05.18.07.07.56; Tue, 18 May 2021 07:08:09 -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=1PyEVc3t; 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 S239059AbhEQPlr (ORCPT + 99 others); Mon, 17 May 2021 11:41:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:41358 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243995AbhEQP0j (ORCPT ); Mon, 17 May 2021 11:26:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3744D61108; Mon, 17 May 2021 14:36:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621262199; bh=kC7FQNzElUN2/JaEJqPMFhEHrMIh5d+NALf67BGqDAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1PyEVc3t5434czenVMAm4jdSv3/V7Fi9p8NheLW6yvmV3STqIJC/Y9DkYO2GyOhmy ztTUnGYHkxQj2Q7yqa+9JjylP4Stwh7V43AOQiAMLrEmDcV8EeNQ53rGTB3fP08BuD qqy1ZvbEQKINx0S7+CAabXcWfd8W7gdPQVAS6lpk= 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.11 237/329] userfaultfd: release page in error path to avoid BUG_ON Date: Mon, 17 May 2021 16:02:28 +0200 Message-Id: <20210517140310.130976446@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.043055203@linuxfoundation.org> References: <20210517140302.043055203@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 @@ -2373,8 +2373,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);