Message ID | 20240222080815.46291-1-zhengqi.arch@bytedance.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:aa16:b0:108:e6aa:91d0 with SMTP id by22csp101283dyb; Thu, 22 Feb 2024 00:10:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXCZv0Pz8njxpdC+1DTpXVHghgLPLQgxW0miL30BxcgqDfRHV7IAkwdTkwz3r4jCwix+mnBVhclv+eTRRAaJsJFL60m9Q== X-Google-Smtp-Source: AGHT+IESCIN3PnTrsv/+9aWvW9bhpzFuD+VgHeOfb/BB/JdVQTboKHTCTjTbUUm+maWkvlNn5DM+ X-Received: by 2002:a17:90b:88f:b0:299:760b:6702 with SMTP id bj15-20020a17090b088f00b00299760b6702mr10441483pjb.44.1708589427763; Thu, 22 Feb 2024 00:10:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708589427; cv=pass; d=google.com; s=arc-20160816; b=qLKLTIPGNEgQi4wWsaoh0pvHnTI+BqZmv1EtcpQVTWNMOXVp6WMjxVVOj8syCfyV+V Wj4shjDATUDVh7uvbSAMvu5jDzEjgQpkEKdvtf4BheGuOaIHq4fB5ljQELoIkWfbPbiu X66E0NBgpbvSLaYSX4aBXkkxMEfAVRqdaJQWlSzRk6dqGsEwe2afUsT2hZB0Amw1dmWY 4NNjaON0n83vHKjk0/j9X2Q3B6DToVnXDtUg2ru2L/mSUmX9BrG3ZmSKXOm3mN9TnejM rMptOUlRDXKU46JRyPy5qI1lg7ye7Qb1Mzk2QiYnFEY4oeLi4si1bz5kX9cQWtrYlZKK +7+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; fh=IJsme3EfJvhugRP7uFQR+LzCryhR6PSG/4m9zE7ZKWg=; b=jQmsNIVM1YwP0nV6crLKtAEHNUTJBbpW3dZ5OYPszzeNj7kvZquNu33LlqIXJC8jE4 Mrc8yCQP8AVMSIBNF79LUlI2aISiz3vj9g6+uBB1af2n402v6owXWPZRaXJotYJPaOHh pPWxqXCbY6XEZ5AMQ0OIuN4yjxbWW2BM92vQ2ykV/ZdrYs7sbrIIHV+ygLMzKZ51eX7A llJfUccsI8b8s7j4VJ5m51XKszGGzqDEzK7NnzcyeO+o4FRMLzIRvqytOncunFWoNZkb qVf1diGtiP5Bz4IsPxah1Tg6gfAHTPwzl+A89vcxd1midt8Zy1qZ3fp/fj0htsxElrc2 kA0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=KCdZEJyW; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id rm6-20020a17090b3ec600b002994bab98a4si10023215pjb.170.2024.02.22.00.10.27 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 00:10:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=KCdZEJyW; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76062-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7CBEBB24D9B for <ouuuleilei@gmail.com>; Thu, 22 Feb 2024 08:09:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 19E1C17C60; Thu, 22 Feb 2024 08:08:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="KCdZEJyW" Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 823AB290A for <linux-kernel@vger.kernel.org>; Thu, 22 Feb 2024 08:08:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708589329; cv=none; b=fByArtnps0UU8Gs6vwKQHSy0x34hcKn2S45FPCL+GnwYeEVaCrr6VjRBXWUAE6UB6zY0SFnOIU1ZbyB+lUmQM34q0LJhmT4Xpjwq5q4KNIIGUDBX7rhEfo2vPS6/6W3xDMkybZ19SWt2+F72xLR1qMDBhgLopXTttWGyGMl3C2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708589329; c=relaxed/simple; bh=6uQZyNQBDfK/Q2MJsvb89cQpkDZn71u/ixkGYNhY+lk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=CY7gXDmDe9MwZ+wvTwV/BKuQllpERRHwgisaoEu3VUDk2c9voeGLiiPvmqjBJ/1MU8GS513UEcTbuGcW6ckOwL7sDlHztY4QATY1ufsxbfuNv/FrbkTc2Ws50fI3u8JhaWuR7q4dO7Uy8jhENi9YHogsvV/XSzP9+FlnIWA6uNI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=KCdZEJyW; arc=none smtp.client-ip=209.85.210.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-6e46cfe4696so280364a34.0 for <linux-kernel@vger.kernel.org>; Thu, 22 Feb 2024 00:08:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1708589326; x=1709194126; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; b=KCdZEJyWkaFWzGjmhmiWx1DvEk3Ew7X4vvWZbX81+lfZMPmAyHRMRNU8Je2tNJ7tlw tYSYLDGyJoHHTHTESPxo9nmDL+n8o373TCFcBS+n4F7MvXviwDf1bVP6Qfcm8woHukMq 7OLNszY8csQgYiaE7WBgOvlLF0Sp92BE0gRBWRGb7mVRWYyD2UZE+FD9ETUtvyyjsWS2 Ea4SftqtRRoNlazeWENErYuLBe4zawyZvXsKzW0ZVSgCjv7TEreWp6x53zqclFJLwTMZ 221gYOQ/8Y6AUHzLOE5TFhic30bvE0XPqeAQbO2wmDB+hmOFAserXQu46IjV9VrBahDM c3fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708589326; x=1709194126; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; b=uWG28tMh5bi5sCuAQneUmeMa6VksXAdKSPhtvh0JfN1NTxJ4A2FQu8xubA5qJpyVHM PKiUq1om8E6j+eKdsFDrHT3yVGKV5Liu5wqWBX7ZSCRmv2O5FQ9tgL48qnQcvfbF3eff NHKv6WcQxefPAcSRbTYuZJ/s4KdF5OEPhI2eB26ossQkIvcsENxBOj+HyOQqvc+R/ITQ 0iUqjLvcUzNxlBZ+rvmrUoGV2LsTE1xqRZQMkUx/cncaNUUiMfhBjWMNRvNp4B+nkZyu a3R7NuLw96Gg2oca/cqpwbpYDjqJ1ZlANK1SYgVeRAJPk+f/Cag/sz6Aa/FJ99U8sa/C c/rw== X-Forwarded-Encrypted: i=1; AJvYcCWL5n5evrvKn/oWAh43N20vAt4SkPhRr/keZ5gqvuk1AtDyBx68ThIdubkgXV2giGhBp1539uKwlQ6v5ta+ow3H5XwuTpU4UCphgNfr X-Gm-Message-State: AOJu0YwJAzKPMCo0IgMJQ2w9WFBcBRj3uOo6mj/J9Rv/CKE1dEuuFY3x LPWF3BRNa6vjsIfbt49KQL2itb2kO0LBiifOQAXEn4zq+pbhG/EYOH+A4HSDSmk= X-Received: by 2002:a05:6358:e49a:b0:178:9f1d:65e3 with SMTP id by26-20020a056358e49a00b001789f1d65e3mr19766252rwb.0.1708589326343; Thu, 22 Feb 2024 00:08:46 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.155]) by smtp.gmail.com with ESMTPSA id a3-20020a056a000c8300b006e4cf04e501sm659058pfv.13.2024.02.22.00.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 00:08:45 -0800 (PST) From: Qi Zheng <zhengqi.arch@bytedance.com> To: akpm@linux-foundation.org, aarcange@redhat.com, surenb@google.com, david@redhat.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng <zhengqi.arch@bytedance.com> Subject: [PATCH] mm: userfaultfd: fix unexpected change to src_folio when UFFDIO_MOVE fails Date: Thu, 22 Feb 2024 16:08:15 +0800 Message-Id: <20240222080815.46291-1-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791585867884989258 X-GMAIL-MSGID: 1791585867884989258 |
Series |
mm: userfaultfd: fix unexpected change to src_folio when UFFDIO_MOVE fails
|
|
Commit Message
Qi Zheng
Feb. 22, 2024, 8:08 a.m. UTC
After ptep_clear_flush(), if we find that src_folio is pinned we will fail
UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to
src_folio->{mapping,index} is not restored in this process. This is not
what we expected, so fix it.
Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
mm/userfaultfd.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
Comments
On 22.02.24 09:08, Qi Zheng wrote: > After ptep_clear_flush(), if we find that src_folio is pinned we will fail > UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to > src_folio->{mapping,index} is not restored in this process. This is not > what we expected, so fix it. > > Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") > Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> > --- > mm/userfaultfd.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index 4744d6a96f96..503ea77c81aa 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -1008,9 +1008,6 @@ static int move_present_pte(struct mm_struct *mm, > goto out; > } > > - folio_move_anon_rmap(src_folio, dst_vma); > - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); > - > orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); > /* Folio got pinned from under us. Put it back and fail the move. */ > if (folio_maybe_dma_pinned(src_folio)) { > @@ -1019,6 +1016,9 @@ static int move_present_pte(struct mm_struct *mm, > goto out; > } > > + folio_move_anon_rmap(src_folio, dst_vma); > + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); > + > orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); > /* Follow mremap() behavior and treat the entry dirty after the move */ > orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma); Indeed, LGTM. Reviewed-by: David Hildenbrand <david@redhat.com>
On Thu, 22 Feb 2024 16:08:15 +0800 Qi Zheng <zhengqi.arch@bytedance.com> wrote: > After ptep_clear_flush(), if we find that src_folio is pinned we will fail > UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to > src_folio->{mapping,index} is not restored in this process. This is not > what we expected, so fix it. > > Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") What are the expected worst-case userspace-visible runtime effects of this flaw?
On Thu, Feb 22, 2024 at 12:43 AM David Hildenbrand <david@redhat.com> wrote: > > On 22.02.24 09:08, Qi Zheng wrote: > > After ptep_clear_flush(), if we find that src_folio is pinned we will fail > > UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to > > src_folio->{mapping,index} is not restored in this process. This is not > > what we expected, so fix it. > > > > Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") > > Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> > > --- > > mm/userfaultfd.c | 6 +++--- > > 1 file changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > > index 4744d6a96f96..503ea77c81aa 100644 > > --- a/mm/userfaultfd.c > > +++ b/mm/userfaultfd.c > > @@ -1008,9 +1008,6 @@ static int move_present_pte(struct mm_struct *mm, > > goto out; > > } > > > > - folio_move_anon_rmap(src_folio, dst_vma); > > - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); > > - > > orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); > > /* Folio got pinned from under us. Put it back and fail the move. */ > > if (folio_maybe_dma_pinned(src_folio)) { > > @@ -1019,6 +1016,9 @@ static int move_present_pte(struct mm_struct *mm, > > goto out; > > } > > > > + folio_move_anon_rmap(src_folio, dst_vma); > > + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); > > + > > orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); > > /* Follow mremap() behavior and treat the entry dirty after the move */ > > orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma); > > Indeed, LGTM. > > Reviewed-by: David Hildenbrand <david@redhat.com> Thanks for catching this! Makes total sense to check before modification. Reviewed-by: Suren Baghdasaryan <surenb@google.com> > > -- > Cheers, > > David / dhildenb >
On Thu, Feb 22, 2024 at 1:00 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > On Thu, 22 Feb 2024 16:08:15 +0800 Qi Zheng <zhengqi.arch@bytedance.com> wrote: > > > After ptep_clear_flush(), if we find that src_folio is pinned we will fail > > UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to > > src_folio->{mapping,index} is not restored in this process. This is not > > what we expected, so fix it. > > > > Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") > > What are the expected worst-case userspace-visible runtime effects of > this flaw? It can cause rmap for that page to be invalid. I guess memory corruption might be the visible effect?
On 22.02.24 22:56, Suren Baghdasaryan wrote: > On Thu, Feb 22, 2024 at 1:00 PM Andrew Morton <akpm@linux-foundation.org> wrote: >> >> On Thu, 22 Feb 2024 16:08:15 +0800 Qi Zheng <zhengqi.arch@bytedance.com> wrote: >> >>> After ptep_clear_flush(), if we find that src_folio is pinned we will fail >>> UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to >>> src_folio->{mapping,index} is not restored in this process. This is not >>> what we expected, so fix it. >>> >>> Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") >> >> What are the expected worst-case userspace-visible runtime effects of >> this flaw? > > It can cause rmap for that page to be invalid. I guess memory > corruption might be the visible effect? At least swapout+migration would no longer work, because we might fail to locate the mappings of that folio. Memory corruption, not sure.
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4744d6a96f96..503ea77c81aa 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1008,9 +1008,6 @@ static int move_present_pte(struct mm_struct *mm, goto out; } - folio_move_anon_rmap(src_folio, dst_vma); - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); - orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); /* Folio got pinned from under us. Put it back and fail the move. */ if (folio_maybe_dma_pinned(src_folio)) { @@ -1019,6 +1016,9 @@ static int move_present_pte(struct mm_struct *mm, goto out; } + folio_move_anon_rmap(src_folio, dst_vma); + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); + orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); /* Follow mremap() behavior and treat the entry dirty after the move */ orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma);