Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp768238pxy; Wed, 28 Apr 2021 13:53:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcVSLichen5GoNUxv59aOPkkVwXJL4nz/Oyb8qYAsIWaQmRXqclcQgXaTh19S0WDPeXHY1 X-Received: by 2002:a17:906:1444:: with SMTP id q4mr31345396ejc.343.1619643238549; Wed, 28 Apr 2021 13:53:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619643238; cv=none; d=google.com; s=arc-20160816; b=On/OTVFdVnOGmQrcF4L4ZxjYDB1SNkbHuuB18ibg6RHNrKz+FYDuGr/tn4ot9L391K jMArHO2pCZoHwLt4/gDPZS6LVK/lhjUj29mPvlBQvSUPnOZOvr9zQpyDsxm4rILAaXWg bLyp4uuP3xl2jQn0rtgG5RxdsTC0+zSv4aUZnt+JG0cJEfjqW1sAMlxJF1qbphLb1rCE 1k00hhOh5MzoUdJ7noKHtVSqN4uxnl9uZ3VxvOddy8mSOMQYM/Px+tm/L/fHPURJACj4 q1/zu/2tf6ZvdBIrT19NJWlu3uZKIYRvk8wjBg20A+GWjZkyQqdN2FT55NBAbSmj5K1d R2qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=W3wbIZp6Y1eJURenlypAKMNuDhi204EdAx8I9omgFTU=; b=KJ7Csfc3aRst37hQZ/WbrChYUuCKauuBB2OmxZKfhWFfeSwEzckV9YTx2KZsRuZUeI 4Aq43XT51teTYmjeNKPI93ogkCxMhr7fP1dmtQtPmn2fdgCAxG6EqLY6qwGYYPjssaHr ZXbzah6nP0G0wFjHhc5ZpVPAElvFZuwloT6LKr50/ioEtFswE4i1R70W/8C77Cn3phfh NDCX5Ul84MhlRIJOT6KhB0yk3xwYd5ILCNsqkG8rzX0zixq3+LfGloIehZzgifFTwBG8 /dDnbbP34cUJ6PTS76luvty30lilr6ay8F0pwnNedYe8xuPEWLMxUAGtgMLMY2DNW0ER 3Ofw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NjnVlpw8; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s15si838592edd.547.2021.04.28.13.53.35; Wed, 28 Apr 2021 13:53:58 -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=@redhat.com header.s=mimecast20190719 header.b=NjnVlpw8; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243664AbhD1Skg (ORCPT + 99 others); Wed, 28 Apr 2021 14:40:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:58528 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243508AbhD1Skf (ORCPT ); Wed, 28 Apr 2021 14:40:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619635190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=W3wbIZp6Y1eJURenlypAKMNuDhi204EdAx8I9omgFTU=; b=NjnVlpw8Zp4FpFYM7RFv5R3b7wO2A7bvybPwREIBBAMCpCNapjAXNYDnj10T1whDN1Qyvc pe8Vfs7o9PLY6UZ7zlNIrPi78ZMgLHotxsunuSff1ApbnXa8gPBbxI/5AhU0Wps2DabiJt KJ+fCS+ovQY/UJA9fOz3ge/xF+HyJuM= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-277-UYu39LRtOI-wgKlECk8pHg-1; Wed, 28 Apr 2021 14:39:46 -0400 X-MC-Unique: UYu39LRtOI-wgKlECk8pHg-1 Received: by mail-qk1-f199.google.com with SMTP id y9-20020ae9f4090000b02902e4caf24229so5569997qkl.4 for ; Wed, 28 Apr 2021 11:39:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=W3wbIZp6Y1eJURenlypAKMNuDhi204EdAx8I9omgFTU=; b=I3hN29qbDVZT3m2lsvwLyShy2JEUUvaY/g+3+1aQ087f0D3qIzPsEq0w/PdWZRcQI5 jQNhO/xTZA9j1L8cUw76kPPliLnc9MFRu9tPRL25uYr1WKZ5T5LLlUdCn2nkSFXllz9F OANoUw0sigGCuNwjjuP3LAtICimo5q4xlSRtWmO18RiA7S7Kt4KHJtcEMrKJBYfcyuAQ 4knfP0agoKnPT4Lb31dnVNqbhKb5tTCPNNvZV8mwc42Lpu6/rYu8sV6ttk4cNqHyh0Ju ahgvJ4KyhhFCT/N5UJD27Y3JeE6r3r4C59B0J4JunvFE6bmgwWO/5V85D5TWC1LIP7tj tKyA== X-Gm-Message-State: AOAM532UsUTtSzKH/4BdpXI8TS0I5/TFGVFuypBkqlY+rW6KF37quzCb qif75fPTEzqIoVJMrcSLyfwMwp3CBcUJgs1DVizm5iUGDtYjhGo4kszt6p7bs6152agGkDTqTkq uUGzw9J9CbrQ+Skjcg946aLth X-Received: by 2002:a05:6214:16c6:: with SMTP id d6mr15125099qvz.38.1619635184988; Wed, 28 Apr 2021 11:39:44 -0700 (PDT) X-Received: by 2002:a05:6214:16c6:: with SMTP id d6mr15125064qvz.38.1619635184705; Wed, 28 Apr 2021 11:39:44 -0700 (PDT) Received: from xz-x1 (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id j191sm393241qke.131.2021.04.28.11.39.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Apr 2021 11:39:44 -0700 (PDT) Date: Wed, 28 Apr 2021 14:39:43 -0400 From: Peter Xu To: Axel Rasmussen Cc: Andrea Arcangeli , Andrew Morton , Hugh Dickins , Lokesh Gidra , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] userfaultfd: release page in error path to avoid BUG_ON Message-ID: <20210428183943.GH6584@xz-x1> References: <20210428180109.293606-1-axelrasmussen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210428180109.293606-1-axelrasmussen@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 28, 2021 at 11:01:09AM -0700, Axel Rasmussen wrote: > Consider the following sequence of events (described from the point of > view of the commit that introduced the bug - see "Fixes:" below): > > 1. Userspace issues a UFFD ioctl, which ends up calling into > shmem_mcopy_atomic_pte(). We successfully account the blocks, we > shmem_alloc_page(), but then the copy_from_user() fails. We return > -EFAULT. We don't release the page we allocated. > 2. Our caller detects this error code, tries the copy_from_user() after > dropping the mmap_sem, and retries, calling back into > shmem_mcopy_atomic_pte(). > 3. Meanwhile, let's say another process filled up the tmpfs being used. > 4. So shmem_mcopy_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 -EFAULT is returned. > > (Later on in the commit history, -EFAULT became -ENOENT, mmap_sem became > mmap_lock, and shmem_inode_acct_block() was added.) I suggest you do s/EFAULT/ENOENT/ directly in above. > > A malicious user (even an unprivileged one) could trigger this > intentionally without too much trouble. > > To fix this, detect if we have a "dangling" page when accounting fails, > and if so, release it before returning. > > Fixes: cb658a453b93 ("userfaultfd: shmem: avoid leaking blocks and used blocks in UFFDIO_COPY") > Reported-by: Hugh Dickins > Signed-off-by: Axel Rasmussen > --- > mm/shmem.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/mm/shmem.c b/mm/shmem.c > index 26c76b13ad23..46766c9d7151 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -2375,8 +2375,19 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, > 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)) { > + unlock_page(*pagep); Not necessary? > + put_page(*pagep); > + *pagep = NULL; > + } > goto out; All "goto out" in this functions looks weird as it returns directly... so if you're touching this after all, I suggest we do "return -ENOMEM" directly and drop the "ret = -ENOMEM". Thanks, > + } > > if (!*pagep) { > page = shmem_alloc_page(gfp, info, pgoff); > -- > 2.31.1.498.g6c1eba8ee3d-goog > -- Peter Xu