linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Xu <jeffxu@chromium.org>
To: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Jeff Xu <jeffxu@chromium.org>,
	 akpm@linux-foundation.org, keescook@chromium.org,
	jannh@google.com,  torvalds@linux-foundation.org,
	adhemerval.zanella@linaro.org, oleg@redhat.com,
	 linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
	 linux-mm@kvack.org, jorgelo@chromium.org, sroettger@google.com,
	 ojeda@kernel.org, adobriyan@gmail.com, anna-maria@linutronix.de,
	 mark.rutland@arm.com, linus.walleij@linaro.org, Jason@zx2c4.com,
	 deller@gmx.de, rdunlap@infradead.org, davem@davemloft.net,
	hch@lst.de,  peterx@redhat.com, hca@linux.ibm.com,
	f.fainelli@gmail.com, gerg@kernel.org,
	 dave.hansen@linux.intel.com, mingo@kernel.org, ardb@kernel.org,
	 mhocko@suse.com, 42.hyeyoo@gmail.com, peterz@infradead.org,
	ardb@google.com,  enh@google.com, rientjes@google.com,
	groeck@chromium.org,  lorenzo.stoakes@oracle.com
Subject: Re: [RFC PATCH v2 1/1] exec: seal system mappings
Date: Mon, 11 Nov 2024 11:10:30 -0800	[thread overview]
Message-ID: <CABi2SkW-cWJQtT2E_vO40bFi7Qrr+At3Q0jFVVQt+WZP=jaHHg@mail.gmail.com> (raw)
In-Reply-To: <agkliam473nmhxirk76psryxh5qkrncdhwzyoyf4w4efkxnubw@vkeini5qa6xw>

Hi Liam

On Thu, Oct 17, 2024 at 9:01 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:

> > > Does it make sense for this to live in exec?  Couldn't you put it in the
> > > mm/mseal.c file?  It's vma flags for mappings and you've put it in
> > > fs/exec?
> > >
> > If you are referring to utilities related to kernel cmdline, they
> > should be in this file.
>
> You created a wrapper for the command line, but then included the user
> in this file as well.
>
> hugetlbfs reads the command line as well, in cmdline_parse_hugetlb_cma.
> That parser lives with the rest of the hugetlb code in hugetlb.c
>
> I think this has to do with your view as this is an exec thing, where I
> think it's an mm thing.  My arguments are that you are directly adding
> flags to vmas and it's dealing with mseal which has memory in the name
> with the file living in the mm/ directory.  If I wanted to know what's
> using mseal, I'd start there and totally miss what you are adding here.
>
> Besides applying a vma flag to exec mappings, why do you feel like it
> belongs in fs/ ?
>
The vdso/vvar/stack/heap alike are type of mappings belonging to
processes, and are created during execve() syscall which is in
fs/exec.c.

mm/mseal.c provides core memory sealing functionality and exec.c uses
it. IMO, it is better to keep the provider (mm/mseal.c) and consumer
(executable) separate.

To make modulization better, I can do below adjustment:
if (seal_system_mapping_enabled()) <-- implemented by fs/exec.c
   add_vm_sealed() <- keep in include/linux/mm.h

However, if you have a strong opinion on this, I could move the
parsing logic to mm/mseal.

> > > > +void update_seal_exec_system_mappings(unsigned long *
> > > The name is also very long and a bit odd, it could be used for other
> > > reasons, but you have _system_mappings on the end, and you use seal but
> > > it's mseal (or vm_seal)?  Would mseal_flag() work?
> > >
> > It could be longer :-)
> >  it means update_sealing_flag_for_executable_system_mappings.
> > mseal_flag is too short and not descriptive.
>
> mseal_exec_flags() ?
>
It needs to be more descriptive because there are also stacks and
heaps to be sealed. I suggest to use below name to make it shorter:

if (seal_system_mapping_enabled())
   add_vm_sealed()

> > > > diff --git a/mm/mmap.c b/mm/mmap.c
> > > > index 57fd5ab2abe7..d4717e34a60d 100644
> > > > --- a/mm/mmap.c
> > > > +++ b/mm/mmap.c
> > > > @@ -2133,6 +2133,7 @@ struct vm_area_struct *_install_special_mapping(
> > > >       unsigned long addr, unsigned long len,
> > > >       unsigned long vm_flags, const struct vm_special_mapping *spec)
> > > >  {
> > > > +     update_seal_exec_system_mappings(&vm_flags);
> > > >       return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
> > > >                                       &special_mapping_vmops);
> > >
> > > If you were to return a flag, you could change the vm_flags argument to
> > > vm_flags | mseal_flag()
> > >
> > passing pointer seems to be the most efficient way.
>
> I disagree.  Here is the godbolt.org output for gcc x86-64 14.2 of your
> code (with some added #defines to make it compile)
>
> seal_system_mappings:
>         .long   1
> seal_system_mappings_enabled:
>         push    rbp
>         mov     rbp, rsp
>         mov     eax, DWORD PTR seal_system_mappings[rip]
>         cmp     eax, 1
>         jne     .L2
>         mov     eax, 1
>         jmp     .L3
> .L2:
>         mov     eax, 0
> .L3:
>         pop     rbp
>         ret
> update_seal_exec_system_mappings:
>         push    rbp
>         mov     rbp, rsp
>         sub     rsp, 8
>         mov     QWORD PTR [rbp-8], rdi
>         mov     rax, QWORD PTR [rbp-8]
>         mov     rax, QWORD PTR [rax]
>         and     eax, 2
>         test    rax, rax
>         jne     .L6
>         call    seal_system_mappings_enabled
>         test    al, al
>         je      .L6
>         mov     rax, QWORD PTR [rbp-8]
>         mov     rax, QWORD PTR [rax]
>         or      rax, 2
>         mov     rdx, rax
>         mov     rax, QWORD PTR [rbp-8]
>         mov     QWORD PTR [rax], rdx
> .L6:
>         nop
>         leave
>         ret
> main:
>         push    rbp
>         mov     rbp, rsp
>         sub     rsp, 16
>         mov     QWORD PTR [rbp-8], 0
>         lea     rax, [rbp-8]
>         mov     rdi, rax
>         call    update_seal_exec_system_mappings
>         mov     rax, QWORD PTR [rbp-8]
>         leave
>         ret
>
> ----- 48 lines -----
> Here is what I am suggesting to do with replacing the passing of a
> pointer with a concise "vm_flags | mseal_exec_flags()" (with the same
> added #defines to make it compile)
>
> seal_system_mappings:
>         .long   1
> mseal_exec_flags:
>         push    rbp
>         mov     rbp, rsp
>         mov     eax, DWORD PTR seal_system_mappings[rip]
>         cmp     eax, 1
>         jne     .L2
>         mov     eax, 2
>         jmp     .L3
> .L2:
>         mov     eax, 0
> .L3:
>         pop     rbp
>         ret
> main:
>         push    rbp
>         mov     rbp, rsp
>         sub     rsp, 16
>         mov     QWORD PTR [rbp-8], 0
>         call    mseal_exec_flags
>         mov     edx, eax
>         mov     rax, QWORD PTR [rbp-8]
>         or      eax, edx
>         leave
>         ret
>
> ----- 26 lines -----
>
> So as you can see, there are less instructions in my version; there are
> 47.92% less lines of assembly.
>
vm_flags already  run out of space in 32 bit, sooner or later we will
need to change that to *** a struct ***,  passing address will be
becoming necessary with struct.  Since this is not a performance
sensitive code path, 3 or 4 times during execve(), I think it would be
good to start  here.

-Jeff


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

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14 21:50 [RFC PATCH v2 0/1] " jeffxu
2024-10-14 21:50 ` [RFC PATCH v2 1/1] exec: " jeffxu
2024-10-16 21:26   ` Kees Cook
2024-10-16 22:06     ` Jeff Xu
2024-10-17  3:56       ` Jeff Xu
2024-10-17  1:10   ` Liam R. Howlett
2024-10-17  3:43     ` Jeff Xu
2024-10-17  8:37       ` Oleg Nesterov
2024-10-17 16:12         ` Jeff Xu
2024-10-17 16:01       ` Liam R. Howlett
2024-11-11 19:10         ` Jeff Xu [this message]
2024-11-11 22:35           ` Liam R. Howlett
2024-11-13 21:38             ` Jeff Xu
2024-10-16 23:18 ` [RFC PATCH v2 0/1] " Liam R. Howlett
2024-10-17  0:58   ` Jeff Xu
2024-10-17  2:03     ` Liam R. Howlett
2024-11-11 18:25       ` Jeff Xu

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='CABi2SkW-cWJQtT2E_vO40bFi7Qrr+At3Q0jFVVQt+WZP=jaHHg@mail.gmail.com' \
    --to=jeffxu@chromium.org \
    --cc=42.hyeyoo@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=anna-maria@linutronix.de \
    --cc=ardb@google.com \
    --cc=ardb@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=deller@gmx.de \
    --cc=enh@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=gerg@kernel.org \
    --cc=groeck@chromium.org \
    --cc=hca@linux.ibm.com \
    --cc=hch@lst.de \
    --cc=jannh@google.com \
    --cc=jorgelo@chromium.org \
    --cc=keescook@chromium.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mark.rutland@arm.com \
    --cc=mhocko@suse.com \
    --cc=mingo@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterx@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=rientjes@google.com \
    --cc=sroettger@google.com \
    --cc=torvalds@linux-foundation.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