Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp490901pxj; Thu, 20 May 2021 14:18:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDfa5lpTW94co1pdkUNFlI3DF4wLtJhSVrjsstuYPSjhqNHIgkxs/QeIvY1I1jB5sDe+oW X-Received: by 2002:a17:907:20d1:: with SMTP id qq17mr6421460ejb.329.1621545490398; Thu, 20 May 2021 14:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621545490; cv=none; d=google.com; s=arc-20160816; b=Snz8vcu6hXAByNfS1emtakjaBy+VCn3K67S472xiEoZrUrIdYytNmYZxBJ/05g71XE fBH7XE2kewJteb8rv+g3YhsnzVKTT0sTUTQUNXRN5nSXU0O2oJr//OU1vbXWUH20KPaM 9xLgsW8CBAOd0o7ZwOFmlewx2WcI5DU7lJ6RrQ4836Cx1wQTkdoa2+gZkeiMaHK29rbi HS/Yspf5RFAFGm0rtn6hqdTFKDmgT/0O1gbpG7csdzRJQJt8SoopUlve7fBbegnH5TkB s/qYpcvYf9r1md2MGdvKNaNmC7ri0FTLaBwtqaOV5WzxQNRlFBytoqp3wdnZnq6UJWdS JNqQ== 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=dOi99NPCJBXsKQhQ0IXITnIExdpwdz+n+cOp+Dt89oA=; b=J3gglTZz4GvS1nyglsbFOzFoIcC/3ml2lAVVBibcWq1fAYElCUTqzRZRDO8vbf2UIt W/ea4G3vqVzgt0p56Ni8qtbdo71+fQw9JDyKqnBNReerD0J90SEGN+iqxb2epmZx9Hoi IbY8IbVfh0bowssjBUvnuqeCcv96jBDVNpsbf8/mcjQexj2xT4XbYUdB+2iJQFmdNXxb fHmWdCV8w1VxE0b4b0zhSxgM+9j0AD9weZ2gZenpVw8gPQnsZXJ5w1lq5Ov1QT1JoJDd caluh3EyE+eGMt6VJJDTx4V+mCyJfBI0RFDtLt2NLqCCnB08mwk43BLw9JBwjTsSjlpS oDFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cDgIvaKl; 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 kg7si828446ejc.458.2021.05.20.14.17.45; Thu, 20 May 2021 14:18:10 -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=cDgIvaKl; 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 S238955AbhETLBc (ORCPT + 99 others); Thu, 20 May 2021 07:01:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:43874 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237230AbhETKmn (ORCPT ); Thu, 20 May 2021 06:42:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id F2DAF6140B; Thu, 20 May 2021 09:56:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504589; bh=6WwRAP4UY1++4XNIWcIHlvMJAG/oqBguxaJ0Ev1RCpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cDgIvaKl1TCK8hbl1t1jr64uUt2r5kR1JMsmZFXB66hDn24Oz9t+uG98vmL116fix qckx9jFtyyouO4uHeaOSHKUEBev0mfoaMVH8sYfT6hzsPeOKVZxNvYNQZzliGeXfMa gjQg7J7Hb1UmOivlNeei6PwxJyMv9ZkvhnQPcXOk= 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 4.14 280/323] userfaultfd: release page in error path to avoid BUG_ON Date: Thu, 20 May 2021 11:22:52 +0200 Message-Id: <20210520092129.813709969@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@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 @@ -2251,8 +2251,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);