Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp462987pxj; Tue, 18 May 2021 07:18:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyK2Gm8mhKVxfhko7ddnZe+XqJhoYsDQDw4P3+tun/2gfvR0xr+XoCDQhyG47syduFWbJ2s X-Received: by 2002:aa7:c745:: with SMTP id c5mr7549100eds.136.1621347483566; Tue, 18 May 2021 07:18:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621347483; cv=none; d=google.com; s=arc-20160816; b=c7bU5FXJ0+ougtXXYxMcFa87ok9GYpQ5KmKLbh1tGt0HJNQq/0lIyuQJqQz8kYwmYZ bygZU2gKv6F7SVn49H5KpyBzSUDm5C1kMhUhjnOJFO9iM8M+/EJ19W4iGM99PJppjlVP LQfGF4AhWIaWSFoD53SO+RoQpFdmTaJ6m6IklseuzFDjpMzEZ84Mq2QyaGeYilKzD8BP SOYTIzZz+LaZ4JCXZDdqoI9n6boWKzudMQUl8nDsKT1Li3vfzAOdRl2wUljsG0hG/DX3 wKEQDFieshAsKpr7eibXJowVB+/qQPN39gVYL34VjcjtU2cpdT5Sun5NYbhRAxYnqt/C R5zA== 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=rE5Kvjufk0Xem3w50wCFdScoqGogSx085n76kiM76jQ=; b=QwWbMQPBOa+10YLSsRch82Z7xkobk6zGzu7lpJKsWZm4NXavHmpc7kMCOF/Wls2DtO voV3F+xyyPCGMW4tu3RxgvCMrpZd2rDjqbS/XCAw3oLyPTY2OHlnZfjnO+TPf1oi8Bj2 LM+sFewNIlyX4wAAz3A73Z7yU+Pd4n2RTrgDelhgggbn/fGtyRoBbrC53na+wWI7ZXbE 0HvYs8n7i4JtwN68GUWuIv1O9I4PRXYCvPG+TteFlKSbZRDae2Gtj4z4Cyw2iKC8IHPa wVL/qW2Z9wY6Kpm1S1lmSR6XYdM0StvcY4tSO4RDZIZ03RU+qpu9UO5x04c62hf0k4xA vUaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=STomcj7Z; 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 t23si17293791ejj.290.2021.05.18.07.17.39; Tue, 18 May 2021 07:18:03 -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=STomcj7Z; 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 S1344482AbhEQPol (ORCPT + 99 others); Mon, 17 May 2021 11:44:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:52290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244213AbhEQP1V (ORCPT ); Mon, 17 May 2021 11:27:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C6B2461CB1; Mon, 17 May 2021 14:36:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621262208; bh=w1aeMm/kiwVrQe6PqJGVYcg9a6t6R3ZxK/2ZEZIDo5U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=STomcj7ZRpD/80Kf8Yhbg9P2FIFftEVu3xb58k5bbliKnNa16pGCfucF/MwOW6HKp QWOkF2HkLTJbLlxNy1d+yqwMzTyOf5jtXd+xWAewSXEMYeL+z4zkb2w51I8kaQomeo m0g6lrIuRV34KO5douxQRd+sy2WqPv/otZDRziXY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Xu , Hugh Dickins , Mike Kravetz , "Joel Fernandes (Google)" , Andrew Morton , Linus Torvalds Subject: [PATCH 5.11 239/329] mm/hugetlb: fix F_SEAL_FUTURE_WRITE Date: Mon, 17 May 2021 16:02:30 +0200 Message-Id: <20210517140310.201000694@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.043055203@linuxfoundation.org> References: <20210517140302.043055203@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: Peter Xu commit 22247efd822e6d263f3c8bd327f3f769aea9b1d9 upstream. Patch series "mm/hugetlb: Fix issues on file sealing and fork", v2. Hugh reported issue with F_SEAL_FUTURE_WRITE not applied correctly to hugetlbfs, which I can easily verify using the memfd_test program, which seems that the program is hardly run with hugetlbfs pages (as by default shmem). Meanwhile I found another probably even more severe issue on that hugetlb fork won't wr-protect child cow pages, so child can potentially write to parent private pages. Patch 2 addresses that. After this series applied, "memfd_test hugetlbfs" should start to pass. This patch (of 2): F_SEAL_FUTURE_WRITE is missing for hugetlb starting from the first day. There is a test program for that and it fails constantly. $ ./memfd_test hugetlbfs memfd-hugetlb: CREATE memfd-hugetlb: BASIC memfd-hugetlb: SEAL-WRITE memfd-hugetlb: SEAL-FUTURE-WRITE mmap() didn't fail as expected Aborted (core dumped) I think it's probably because no one is really running the hugetlbfs test. Fix it by checking FUTURE_WRITE also in hugetlbfs_file_mmap() as what we do in shmem_mmap(). Generalize a helper for that. Link: https://lkml.kernel.org/r/20210503234356.9097-1-peterx@redhat.com Link: https://lkml.kernel.org/r/20210503234356.9097-2-peterx@redhat.com Fixes: ab3948f58ff84 ("mm/memfd: add an F_SEAL_FUTURE_WRITE seal to memfd") Signed-off-by: Peter Xu Reported-by: Hugh Dickins Reviewed-by: Mike Kravetz Cc: Joel Fernandes (Google) Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/hugetlbfs/inode.c | 5 +++++ include/linux/mm.h | 32 ++++++++++++++++++++++++++++++++ mm/shmem.c | 22 ++++------------------ 3 files changed, 41 insertions(+), 18 deletions(-) --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -131,6 +131,7 @@ static void huge_pagevec_release(struct static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) { struct inode *inode = file_inode(file); + struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); loff_t len, vma_len; int ret; struct hstate *h = hstate_file(file); @@ -146,6 +147,10 @@ static int hugetlbfs_file_mmap(struct fi vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND; vma->vm_ops = &hugetlb_vm_ops; + ret = seal_check_future_write(info->seals, vma); + if (ret) + return ret; + /* * page based offset in vm_pgoff could be sufficiently large to * overflow a loff_t when converted to byte offset. This can --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3191,5 +3191,37 @@ unsigned long wp_shared_mapping_range(st extern int sysctl_nr_trim_pages; +/** + * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it + * @seals: the seals to check + * @vma: the vma to operate on + * + * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on + * the vma flags. Return 0 if check pass, or <0 for errors. + */ +static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) +{ + if (seals & F_SEAL_FUTURE_WRITE) { + /* + * New PROT_WRITE and MAP_SHARED mmaps are not allowed when + * "future write" seal active. + */ + if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) + return -EPERM; + + /* + * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as + * MAP_SHARED and read-only, take care to not allow mprotect to + * revert protections on such mappings. Do this only for shared + * mappings. For private mappings, don't need to mask + * VM_MAYWRITE as we still want them to be COW-writable. + */ + if (vma->vm_flags & VM_SHARED) + vma->vm_flags &= ~(VM_MAYWRITE); + } + + return 0; +} + #endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */ --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2256,25 +2256,11 @@ out_nomem: static int shmem_mmap(struct file *file, struct vm_area_struct *vma) { struct shmem_inode_info *info = SHMEM_I(file_inode(file)); + int ret; - if (info->seals & F_SEAL_FUTURE_WRITE) { - /* - * New PROT_WRITE and MAP_SHARED mmaps are not allowed when - * "future write" seal active. - */ - if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) - return -EPERM; - - /* - * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as - * MAP_SHARED and read-only, take care to not allow mprotect to - * revert protections on such mappings. Do this only for shared - * mappings. For private mappings, don't need to mask - * VM_MAYWRITE as we still want them to be COW-writable. - */ - if (vma->vm_flags & VM_SHARED) - vma->vm_flags &= ~(VM_MAYWRITE); - } + ret = seal_check_future_write(info->seals, vma); + if (ret) + return ret; /* arm64 - allow memory tagging on RAM-based files */ vma->vm_flags |= VM_MTE_ALLOWED;