From patchwork Sun Dec 1 01:53:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 180535 Delivered-To: patch@linaro.org Received: by 2002:a92:3001:0:0:0:0:0 with SMTP id x1csp3512293ile; Sat, 30 Nov 2019 17:53:31 -0800 (PST) X-Google-Smtp-Source: APXvYqwi3/7o/lrEPXjo8J+QWGWwx4gwHBD8roKLRQLCiOgGOQeTHNqUkYai6aMROzSrSeM4CHOu X-Received: by 2002:a17:906:16d1:: with SMTP id t17mr23887121ejd.41.1575165211425; Sat, 30 Nov 2019 17:53:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575165211; cv=none; d=google.com; s=arc-20160816; b=KzxTlpcNMqLoUx1ep1sajI8qT4DfsYKoX1CeVH9LFBYKL8zHvYBpE4jHyKJEe9zbIL A8rQv4zNnlzNrjosbwvf7B9H/9UMAo6uH+gYNbdTl5Zhiy+/VTl9alBfUe8BoXcFzQiG dlnW79ucmqPRfI/X2YOg4DHtzzhN8+nzeW18Po/25HTcuJpeleYrxc7WB+s8x1k28S0o PuZA/Xu4E3xi84pQMG8tQc4JCKSiyQLs+/2gwFNdNaVjbCuVtfQ4c87Fc2PChloHB+eR BXId7MbadkqLkxysPHDZYIM/IEXVAUSNFg4mA73lLZ8XH9HEXlPB8YvE/8L0ddTxycGo /CTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:message-id:subject:to:from :date:dkim-signature; bh=nrPpKrO8ecytjhYQE6uZKSWdV7jy+H3yuIfLbC/YHLE=; b=OWDCdugYqdCi5XrBVYMlnsbzPgA/K9gSH7SA5XINR4wKfOtlCUpj9bNI+UcnwxVjoJ PQJkggS5oxlK+yQtI/EboQ3x9ASIt44teTPeJ/EiVNpTT84vuZq+Nh97SvXFq8U7CKlY npIrsphMZQWW2DP4Cyko33XkuQGNEh4En+eUghYwWWqPwSLx0G5hLzggQreZHEOhlUo2 sknPmDFvY87bLhMjHSxgzKOBtgpkvzBsN+vHtQLqVvL/Nwzbn+xOENZnoIb/zOJ4zsAU Lj6kC6haonOoGfANDTHmfzyyEbC1ujhcXhQVE0CG3b6xbyX79wU/kCQA9DYT7fJVqc3H /uYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GEIv0jan; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v11si17659362eja.168.2019.11.30.17.53.31; Sat, 30 Nov 2019 17:53:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GEIv0jan; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727026AbfLABxa (ORCPT + 15 others); Sat, 30 Nov 2019 20:53:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:55696 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726878AbfLABxa (ORCPT ); Sat, 30 Nov 2019 20:53:30 -0500 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 311D6215A5; Sun, 1 Dec 2019 01:53:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575165209; bh=FPXma5KIlv3ycKGzXzXGQlKa9YuYJaaq8lBfD5AmEG4=; h=Date:From:To:Subject:From; b=GEIv0janIduchxVrBP9GSBJvrPzosKs7JhBH9KnSd38/ReYVR0SpJ9nreaRXVwqhK f6scz8KwTHCvAM3dDxJ/JAWAuvLlnDA1xSD9yNcO7+StJ5ePCIPllyiKQnJShmG4ci n9vPn3cIJAWN++Caj6PYP9aV4bfmjejqYpiY0RgE= Date: Sat, 30 Nov 2019 17:53:28 -0800 From: akpm@linux-foundation.org To: akpm@linux-foundation.org, hughd@google.com, joel@joelfernandes.org, linux-mm@kvack.org, mm-commits@vger.kernel.org, ngeoffray@google.com, shuah@kernel.org, stable@vger.kernel.org, torvalds@linux-foundation.org Subject: [patch 069/158] mm, memfd: fix COW issue on MAP_PRIVATE and F_SEAL_FUTURE_WRITE mappings Message-ID: <20191201015328.wunf9qxIi%akpm@linux-foundation.org> User-Agent: s-nail v14.8.16 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Nicolas Geoffray Subject: mm, memfd: fix COW issue on MAP_PRIVATE and F_SEAL_FUTURE_WRITE mappings F_SEAL_FUTURE_WRITE has unexpected behavior when used with MAP_PRIVATE: A private mapping created after the memfd file that gets sealed with F_SEAL_FUTURE_WRITE loses the copy-on-write at fork behavior, meaning children and parent share the same memory, even though the mapping is private. The reason for this is due to the code below: static int shmem_mmap(struct file *file, struct vm_area_struct *vma) { struct shmem_inode_info *info = SHMEM_I(file_inode(file)); 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 the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED * read-only mapping, take care to not allow mprotect to revert * protections. */ vma->vm_flags &= ~(VM_MAYWRITE); } ... } And for the mm to know if a mapping is copy-on-write: static inline bool is_cow_mapping(vm_flags_t flags) { return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; } The patch fixes the issue by making the mprotect revert protection happen only for shared mappings. For private mappings, using mprotect will have no effect on the seal behavior. The F_SEAL_FUTURE_WRITE feature was introduced in v5.1 so v5.3.x stable kernels would need a backport. [akpm@linux-foundation.org: reflow comment, per Christoph] Link: http://lkml.kernel.org/r/20191107195355.80608-1-joel@joelfernandes.org Fixes: ab3948f58ff84 ("mm/memfd: add an F_SEAL_FUTURE_WRITE seal to memfd") Signed-off-by: Nicolas Geoffray Signed-off-by: Joel Fernandes (Google) Cc: Hugh Dickins Cc: Shuah Khan Cc: Signed-off-by: Andrew Morton --- mm/shmem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/mm/shmem.c~memfd-fix-cow-issue-on-map_private-and-f_seal_future_write-mappings +++ a/mm/shmem.c @@ -2214,11 +2214,14 @@ static int shmem_mmap(struct file *file, return -EPERM; /* - * Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED - * read-only mapping, take care to not allow mprotect to revert - * protections. + * 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. */ - vma->vm_flags &= ~(VM_MAYWRITE); + if (vma->vm_flags & VM_SHARED) + vma->vm_flags &= ~(VM_MAYWRITE); } file_accessed(file);