linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Muchun Song <muchun.song@linux.dev>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	Matthew Wilcox <willy@infradead.org>,
	Hugh Dickins <hughd@google.com>,
	Andy Lutomirski <luto@kernel.org>, Jan Kara <jack@suse.cz>,
	linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH v4 2/3] mm: update memfd seal write check to include F_SEAL_WRITE
Date: Fri, 13 Oct 2023 12:32:08 +0200	[thread overview]
Message-ID: <20231013103208.kdffpyerufr4ygnw@quack3> (raw)
In-Reply-To: <913628168ce6cce77df7d13a63970bae06a526e0.1697116581.git.lstoakes@gmail.com>

On Thu 12-10-23 18:04:29, Lorenzo Stoakes wrote:
> The seal_check_future_write() function is called by shmem_mmap() or
> hugetlbfs_file_mmap() to disallow any future writable mappings of an memfd
> sealed this way.
> 
> The F_SEAL_WRITE flag is not checked here, as that is handled via the
> mapping->i_mmap_writable mechanism and so any attempt at a mapping would
> fail before this could be run.
> 
> However we intend to change this, meaning this check can be performed for
> F_SEAL_WRITE mappings also.
> 
> The logic here is equally applicable to both flags, so update this function
> to accommodate both and rename it accordingly.
> 
> Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>

For some reason only this one patch landed in my inbox but I've checked all
three on lore and they look good to me. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

to all of them. Thanks!

								Honza

> ---
>  fs/hugetlbfs/inode.c |  2 +-
>  include/linux/mm.h   | 15 ++++++++-------
>  mm/shmem.c           |  2 +-
>  3 files changed, 10 insertions(+), 9 deletions(-)
> 
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 06693bb1153d..5c333373dcc9 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -112,7 +112,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
>  	vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND);
>  	vma->vm_ops = &hugetlb_vm_ops;
>  
> -	ret = seal_check_future_write(info->seals, vma);
> +	ret = seal_check_write(info->seals, vma);
>  	if (ret)
>  		return ret;
>  
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index bae234d18d81..26d7dc3b342b 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -4078,25 +4078,26 @@ static inline void mem_dump_obj(void *object) {}
>  #endif
>  
>  /**
> - * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it
> + * seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and
> + *                    handle them.
>   * @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.
> + * Check whether F_SEAL_WRITE or F_SEAL_FUTURE_WRITE are 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)
> +static inline int seal_check_write(int seals, struct vm_area_struct *vma)
>  {
> -	if (seals & F_SEAL_FUTURE_WRITE) {
> +	if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
>  		/*
>  		 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
> -		 * "future write" seal active.
> +		 * write seals are 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
> +		 * 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
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 6503910b0f54..cab053831fea 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2405,7 +2405,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
>  	struct shmem_inode_info *info = SHMEM_I(inode);
>  	int ret;
>  
> -	ret = seal_check_future_write(info->seals, vma);
> +	ret = seal_check_write(info->seals, vma);
>  	if (ret)
>  		return ret;
>  
> -- 
> 2.42.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR


  reply	other threads:[~2023-10-13 10:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 17:04 [PATCH v4 0/3] permit write-sealed memfd read-only shared mappings Lorenzo Stoakes
2023-10-12 17:04 ` [PATCH v4 1/3] mm: drop the assumption that VM_SHARED always implies writable Lorenzo Stoakes
2023-10-12 17:04 ` [PATCH v4 2/3] mm: update memfd seal write check to include F_SEAL_WRITE Lorenzo Stoakes
2023-10-13 10:32   ` Jan Kara [this message]
2023-10-12 17:04 ` [PATCH v4 3/3] mm: perform the mapping_map_writable() check after call_mmap() Lorenzo Stoakes
2023-10-16 16:27   ` Lorenzo Stoakes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231013103208.kdffpyerufr4ygnw@quack3 \
    --to=jack@suse.cz \
    --cc=akpm@linux-foundation.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=hughd@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lstoakes@gmail.com \
    --cc=luto@kernel.org \
    --cc=mike.kravetz@oracle.com \
    --cc=muchun.song@linux.dev \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox