Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp347458pxj; Tue, 18 May 2021 04:55:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUxxFI1OG4dmChgpVarqmxKdjDWvCjbn1fs7TZwY1ZJo7e5ALc959f4+26DWLtSp9KvwUu X-Received: by 2002:a17:906:38c5:: with SMTP id r5mr5433232ejd.230.1621338920325; Tue, 18 May 2021 04:55:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621338920; cv=none; d=google.com; s=arc-20160816; b=SKjQMnARLuKD3YFfyw3j/zxq5xKDdcj29R8ohLh5M7MMlCstFmiHMFrL+lW6gZryq8 rnLVbs3Tu2/zlFuqQ3ETV0qC1kFu/H8kQuFlcCxWb/V/5YAmqEOeeZRmkYGYatVA6UNp 95JY7oLvSENSwxYlgaVbK/CFCzd4DJSMMzjyvQ9TDbf9xS9GJwvisHVmZMpF+eQieEHn ebXPy/ImJUvHQC/u6qu5Ctw3ge0/t7yHag2wfFl4a3GjvDQUJxjWDP7QNcVBtevGSInH CVzvAv66CgtPwoWTQWPhoZH78ZuoBvGxmMzvwYk2rwmnLmPqxEfgf3BtzNiVxFVVB0a5 zjAw== 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=0z9/b8xBrJ+OKW1bUi9uCw9RD+MWSMlnAz2bFGOW+BI=; b=GzmWBUMk4ErqXKr8uvZD49BFwJiluCQrsHHnGArtsNUiLHnf+i9zhyvkEEqhy4nFwM iHPqpDV/WA8O8H+y53ag1+VeEb+i5uuX43ngsFmNH6PG6a0bRGDSGe0ux5bHRxSN44x6 HgZnd2CDM9T09VmpJeW7ka/UDMKzydyDseGUxb6ip9t9dZcEk1j6QvaodqOOBxByYz/y zBb2dpRktrKCEtFqdy1bvgQdcgxsc70FIkBioB6vz8EuTnIYVr7QZNvbEN0lk5anrp5C EXuE7NnTtyFQoffrZshwOMIWnagvw5V+Ku0dz3VFBfPp5Qas5MOkBxiqhNd/HHw+FSBy 3+Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vI95srwe; 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 m19si3923127edd.490.2021.05.18.04.54.57; Tue, 18 May 2021 04:55:20 -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=vI95srwe; 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 S245216AbhEQPZF (ORCPT + 99 others); Mon, 17 May 2021 11:25:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:34366 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243655AbhEQPLb (ORCPT ); Mon, 17 May 2021 11:11:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 75030616EC; Mon, 17 May 2021 14:30:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621261859; bh=DRF2xxxVxTcryOxi5DlOs877XO+fhe8mU3b1Cd34yGY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vI95srweTHwdRE8B8FKMy6hD9fh1BZyN7R7V4g8ZES1GjCRq4QcCdFRhMnTPP46le QNrRIKReKmo9+eioTc1+UIR93Ad0qNCy13dA/rc0t6v/GVBVjNlusinfsKE/iFcXis I2dxvLYbXJMWvvYedBWdDccsXLevWX1nqZcJGQvs= 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.4 101/141] userfaultfd: release page in error path to avoid BUG_ON Date: Mon, 17 May 2021 16:02:33 +0200 Message-Id: <20210517140246.180508390@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140242.729269392@linuxfoundation.org> References: <20210517140242.729269392@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 @@ -2327,8 +2327,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);