From patchwork Mon Oct 24 19:33:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13018050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CF47FA373F for ; Mon, 24 Oct 2022 19:33:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AD1194000A; Mon, 24 Oct 2022 15:33:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75D9C940007; Mon, 24 Oct 2022 15:33:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5621594000A; Mon, 24 Oct 2022 15:33:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 45404940007 for ; Mon, 24 Oct 2022 15:33:43 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 13E9740607 for ; Mon, 24 Oct 2022 19:33:43 +0000 (UTC) X-FDA: 80056842726.24.4C6D654 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id A8CBA80004 for ; Mon, 24 Oct 2022 19:33:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666640022; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sZ8v+EwY2bNX8aSl1hAi5ZLb9PPb9KwOp7fvuUkZC2g=; b=Yf1vVdEDmiwLON8NcTd8HrxG+ZU020mTcoAMgcjzNFp8YPdjWXckcTnQTJYWmLQZIqFYrX XfJ+W568s3Lk2HdWyMZU2KwWTht0DSr7lMMxKH4UKAJXVHyl/9gtCQo9y+Dbbvp3BZRHQW mZAHDsaovXAXLmMU3zML4MYARdpFIX0= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-183-vytJy8u-OqqbVhO2cYyY7A-1; Mon, 24 Oct 2022 15:33:41 -0400 X-MC-Unique: vytJy8u-OqqbVhO2cYyY7A-1 Received: by mail-qk1-f197.google.com with SMTP id h9-20020a05620a244900b006ee944ec451so9770996qkn.13 for ; Mon, 24 Oct 2022 12:33:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sZ8v+EwY2bNX8aSl1hAi5ZLb9PPb9KwOp7fvuUkZC2g=; b=MCnTQmp6bVW5QGBUA6xeDEaIti3r6Eixe/PK3458E6d4dwPQ8quTUA5/Y8SNOj6J9I vHTNPVV3TPZdyUtDnll/XSC/DzUbFTBRvCqx6h/VBGKvM+QvELTlIg2VS86P3q3GN1nw Qt4omkf9npD4mnlUQ7zVPkaWMcT2btj+Pam6nOsEjHJIK3iuUZiiGXuwvmW2OTON+wsr NEEmG+z67TYj6j1VF+vJB7p4E/Gewpkf5rXV2tA8RjoUkmlCDixJ+aoyZXpX50dVbrHS qdybb/qnMOQHS1scevENurFuZUyeW9zO/GI2gtB9KdBQoMNluX9bhD3pMgDrntqsnnob zxAA== X-Gm-Message-State: ACrzQf33wK9kb7G6X27HZhI0wlfe4cgxcuXX3XwCJvc4oj8AV4AXC6vK xici+BexOO7GGUNgpUVqU5xWQdXl2zYLQc5kyBUNZq3+dT3IoQLdkcdf0giKBzz99u7st95Qs5i TM+Kq8+EeWPdUlJRE7ds2fYGIDa9/tHQs5sS/1VErcPsmxe0QsWopaxQ8fGQh X-Received: by 2002:ac8:5dd3:0:b0:39d:1847:872a with SMTP id e19-20020ac85dd3000000b0039d1847872amr18544514qtx.645.1666640020436; Mon, 24 Oct 2022 12:33:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6yZixs/k9ipGQk+YxOWJ1akEpX25bTl/zf3AiVYxllzKHCwgi54KXtb6GwKtr88nUataLr5Q== X-Received: by 2002:ac8:5dd3:0:b0:39d:1847:872a with SMTP id e19-20020ac85dd3000000b0039d1847872amr18544482qtx.645.1666640020135; Mon, 24 Oct 2022 12:33:40 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id t15-20020a05620a450f00b006cddf59a600sm545164qkp.34.2022.10.24.12.33.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 12:33:39 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Axel Rasmussen , peterx@redhat.com, Andrew Morton , Andrea Arcangeli , Nadav Amit Subject: [PATCH 1/2] mm/uffd: Fix vma check on userfault for wp Date: Mon, 24 Oct 2022 15:33:35 -0400 Message-Id: <20221024193336.1233616-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221024193336.1233616-1-peterx@redhat.com> References: <20221024193336.1233616-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666640022; a=rsa-sha256; cv=none; b=aXWSlMDj1qhwFVobM5jDn5R45qP3ZifWsW6OQ1O+PRd41UXcMpb8CFhJs5+GCe37BjVl03 xbB+1uBwtobGjVN2bJSc7t2v+YUnXlBywDaRJZvcw9/gpVGjxKN/F5lAp28+nXqbynKG80 mD9GZSlB815U7EO1eO+gTllPbrrMku4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Yf1vVdED; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666640022; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sZ8v+EwY2bNX8aSl1hAi5ZLb9PPb9KwOp7fvuUkZC2g=; b=NZmiXfV31/nZXMlPlFPKSX+/YnTsHfI3lIFJuoNpbnCMcWOK0f3lJ8Qn2/fgCnE28oyyoD ewwfsdh+45dtkuUoL5sZKVhFPnRUBL5i78DR8GSvlQQXP8YZyByElkjLq+q473U2Ip8khZ 3LQCfqD45RZt6F1q+9Jh+rCRoCOzJtc= X-Rspamd-Queue-Id: A8CBA80004 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Yf1vVdED; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: p91ondnx955bnun1j77a3sgzhrb69u5s X-HE-Tag: 1666640022-340021 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We used to have a report that pte-marker code can be reached even when uffd-wp is not compiled in for file memories, here: https://lore.kernel.org/all/YzeR+R6b4bwBlBHh@x1n/T/#u I just got time to revisit this and found that the root cause is we simply messed up with the vma check, so that for !PTE_MARKER_UFFD_WP system, we will allow UFFDIO_REGISTER of MINOR & WP upon shmem as the check was wrong: if (vm_flags & VM_UFFD_MINOR) return is_vm_hugetlb_page(vma) || vma_is_shmem(vma); Where we'll allow anything to pass on shmem as long as minor mode is requested. Axel did it right when introducing minor mode but I messed it up in b1f9e876862d when moving code around. Fix it. Cc: Axel Rasmussen Fixes: b1f9e876862d ("mm/uffd: enable write protection for shmem & hugetlbfs") Signed-off-by: Peter Xu --- include/linux/userfaultfd_k.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index f07e6998bb68..9df0b9a762cc 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -146,9 +146,9 @@ static inline bool userfaultfd_armed(struct vm_area_struct *vma) static inline bool vma_can_userfault(struct vm_area_struct *vma, unsigned long vm_flags) { - if (vm_flags & VM_UFFD_MINOR) - return is_vm_hugetlb_page(vma) || vma_is_shmem(vma); - + if ((vm_flags & VM_UFFD_MINOR) && + (!is_vm_hugetlb_page(vma) && !vma_is_shmem(vma))) + return false; #ifndef CONFIG_PTE_MARKER_UFFD_WP /* * If user requested uffd-wp but not enabled pte markers for