linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Roberto Sassu <roberto.sassu@huaweicloud.com>
To: "Kirill A. Shutemov" <kirill@shutemov.name>,
	Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Paul Moore <paul@paul-moore.com>,
	ebpqwerty472123@gmail.com,  kirill.shutemov@linux.intel.com,
	zohar@linux.ibm.com, dmitry.kasatkin@gmail.com,
	 eric.snowberg@oracle.com, jmorris@namei.org, serge@hallyn.com,
	 linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	 linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	Roberto Sassu <roberto.sassu@huawei.com>,
	linux-mm@kvack.org, akpm@linux-foundation.org,  vbabka@suse.cz,
	linux-fsdevel@vger.kernel.org,
	Liam Howlett <liam.howlett@oracle.com>,
	Jann Horn <jannh@google.com>
Subject: Re: [PATCH 1/3] ima: Remove inode lock
Date: Fri, 18 Oct 2024 13:22:35 +0200	[thread overview]
Message-ID: <b7155b2fa47f17e587b73620e86ef019a5efa7e1.camel@huaweicloud.com> (raw)
In-Reply-To: <gl4pf7gezpjtvnbp4lzyb65wqaiw3xzjjrs3476j5odxsfzvsj@oouue73v3cgr>

On Fri, 2024-10-18 at 14:05 +0300, Kirill A. Shutemov wrote:
> On Fri, Oct 18, 2024 at 12:00:22PM +0100, Lorenzo Stoakes wrote:
> > + Liam, Jann
> > 
> > On Fri, Oct 18, 2024 at 01:49:06PM +0300, Kirill A. Shutemov wrote:
> > > On Fri, Oct 18, 2024 at 11:24:06AM +0200, Roberto Sassu wrote:
> > > > Probably it is hard, @Kirill would there be any way to safely move
> > > > security_mmap_file() out of the mmap_lock lock?
> > > 
> > > What about something like this (untested):
> > > 
> > > diff --git a/mm/mmap.c b/mm/mmap.c
> > > index dd4b35a25aeb..03473e77d356 100644
> > > --- a/mm/mmap.c
> > > +++ b/mm/mmap.c
> > > @@ -1646,6 +1646,26 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
> > >  	if (pgoff + (size >> PAGE_SHIFT) < pgoff)
> > >  		return ret;
> > > 
> > > +	if (mmap_read_lock_killable(mm))
> > > +		return -EINTR;
> > > +
> > > +	vma = vma_lookup(mm, start);
> > > +
> > > +	if (!vma || !(vma->vm_flags & VM_SHARED)) {
> > > +		mmap_read_unlock(mm);
> > > +		return -EINVAL;
> > > +	}
> > > +
> > > +	file = get_file(vma->vm_file);
> > > +
> > > +	mmap_read_unlock(mm);
> > > +
> > > +	ret = security_mmap_file(vma->vm_file, prot, flags);
> > 
> > Accessing VMA fields without any kind of lock is... very much not advised.
> > 
> > I'm guessing you meant to say:
> > 
> > 	ret = security_mmap_file(file, prot, flags);
> > 
> > Here? :)
> 
> Sure. My bad.
> 
> Patch with all fixups:

Thanks a lot! Let's wait a bit until the others have a chance to
comment. Meanwhile, I will test it.

Do you want me to do the final patch, or will you be proposing it?

Roberto

> diff --git a/mm/mmap.c b/mm/mmap.c
> index dd4b35a25aeb..541787d526b6 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1646,14 +1646,41 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
>  	if (pgoff + (size >> PAGE_SHIFT) < pgoff)
>  		return ret;
>  
> -	if (mmap_write_lock_killable(mm))
> +	if (mmap_read_lock_killable(mm))
>  		return -EINTR;
>  
>  	vma = vma_lookup(mm, start);
>  
> +	if (!vma || !(vma->vm_flags & VM_SHARED)) {
> +		mmap_read_unlock(mm);
> +		return -EINVAL;
> +	}
> +
> +	file = get_file(vma->vm_file);
> +
> +	mmap_read_unlock(mm);
> +
> +	ret = security_mmap_file(file, prot, flags);
> +	if (ret) {
> +		fput(file);
> +		return ret;
> +	}
> +
> +	ret = -EINVAL;
> +
> +	if (mmap_write_lock_killable(mm)) {
> +		fput(file);
> +		return -EINTR;
> +	}
> +
> +	vma = vma_lookup(mm, start);
> +
>  	if (!vma || !(vma->vm_flags & VM_SHARED))
>  		goto out;
>  
> +	if (vma->vm_file != file)
> +		goto out;
> +
>  	if (start + size > vma->vm_end) {
>  		VMA_ITERATOR(vmi, mm, vma->vm_end);
>  		struct vm_area_struct *next, *prev = vma;
> @@ -1688,16 +1715,11 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
>  	if (vma->vm_flags & VM_LOCKED)
>  		flags |= MAP_LOCKED;
>  
> -	file = get_file(vma->vm_file);
> -	ret = security_mmap_file(vma->vm_file, prot, flags);
> -	if (ret)
> -		goto out_fput;
>  	ret = do_mmap(vma->vm_file, start, size,
>  			prot, flags, 0, pgoff, &populate, NULL);
> -out_fput:
> -	fput(file);
>  out:
>  	mmap_write_unlock(mm);
> +	fput(file);
>  	if (populate)
>  		mm_populate(ret, populate);
>  	if (!IS_ERR_VALUE(ret))



  reply	other threads:[~2024-10-18 11:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20241008165732.2603647-1-roberto.sassu@huaweicloud.com>
     [not found] ` <CAHC9VhSyWNKqustrTjA1uUaZa_jA-KjtzpKdJ4ikSUKoi7iV0Q@mail.gmail.com>
     [not found]   ` <CAHC9VhQR2JbB7ni2yX_U8TWE0PcQQkm_pBCuG3nYN7qO15nNjg@mail.gmail.com>
     [not found]     ` <7358f12d852964d9209492e337d33b8880234b74.camel@huaweicloud.com>
     [not found]       ` <593282dbc9f48673c8f3b8e0f28e100f34141115.camel@huaweicloud.com>
     [not found]         ` <15bb94a306d3432de55c0a12f29e7ed2b5fa3ba1.camel@huaweicloud.com>
2024-10-16  9:59           ` Roberto Sassu
2024-10-18  9:24             ` Roberto Sassu
2024-10-18 10:49               ` Kirill A. Shutemov
2024-10-18 10:52                 ` Kirill A. Shutemov
2024-10-18 10:55                   ` Kirill A. Shutemov
2024-10-18 11:00                 ` Lorenzo Stoakes
2024-10-18 11:05                   ` Kirill A. Shutemov
2024-10-18 11:22                     ` Roberto Sassu [this message]
2024-10-18 11:54                       ` Kirill A. Shutemov
2024-10-18 11:56                         ` Roberto Sassu
2024-10-18 13:29                     ` Roberto Sassu
2024-10-18 14:36                   ` Jann Horn
2024-10-18 14:48                     ` 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=b7155b2fa47f17e587b73620e86ef019a5efa7e1.camel@huaweicloud.com \
    --to=roberto.sassu@huaweicloud.com \
    --cc=akpm@linux-foundation.org \
    --cc=bpf@vger.kernel.org \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=ebpqwerty472123@gmail.com \
    --cc=eric.snowberg@oracle.com \
    --cc=jannh@google.com \
    --cc=jmorris@namei.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=kirill@shutemov.name \
    --cc=liam.howlett@oracle.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=paul@paul-moore.com \
    --cc=roberto.sassu@huawei.com \
    --cc=serge@hallyn.com \
    --cc=vbabka@suse.cz \
    --cc=zohar@linux.ibm.com \
    /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