binfmt_elf: seal address zero
Subsystems (5)
cover binfmt_elf: seal address zero
From: Jeff Xu <[email protected]>
In load_elf_binary as part of the execve(), when the current task’s personality has MMAP_PAGE_ZERO set, the kernel allocates one page at address 0. According to the comment:
/* Why this, you ask??? Well SVr4 maps page 0 as read-only,
and some applications "depend" upon this behavior. Since we do not have the power to recompile these, we
emulate the SVr4 behavior. Sigh. */
At one point, Linus suggested removing this [1].
Sealing this is probably safe, the comment doesn’t say the app ever wanting to change the mapping to rwx. Sealing also ensures that never happens.
[1] https://lore.kernel.org/lkml/CAHk-=whVa=nm_GW=NVfPHqcxDbWt4JjjK1YWb0cLjO4ZSGyiDA@mail.gmail.com/
Jeff Xu (1):
binfmt_elf: mseal address zero
fs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
RFC PATCH 1/1 binfmt_elf: mseal address zero
From: Jeff Xu <[email protected]>
Some legacy SVr4 apps might depend on page on address zero to be readable, however I can't find a reason that the page ever becomes writeable, so seal it.
If there is a compain, we can make this configurable.
--- fs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +4 -0
@@ -1314,6 +1314,10 @@ static int load_elf_binary(struct linux_binprm *bprm)emulate the SVr4 behavior. Sigh. */ error =vm_mmap (NULL ,0 ,PAGE_SIZE ,PROT_READ |PROT_EXEC ,MAP_FIXED |MAP_PRIVATE ,0 ); + +#ifdef CONFIG_64BIT +do_mseal (0 ,PAGE_SIZE ,0 ); +#endif } regs =current_pt_regs ();
include/linux/mm.h +4 -0
@@ -4201,4 +4201,8 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma);int reserve_mem_find_by_name (const char *name,phys_addr_t *start,phys_addr_t *size); +#ifdef CONFIG_64BIT +int do_mseal (unsigned long start,size_t len_in,unsigned long flags); +#endif +#endif /* _LINUX_MM_H */
mm/mseal.c +1 -1
@@ -248,7 +248,7 @@ static int apply_mm_seal(unsigned long start, unsigned long end)* * unseal() is not supported. */ -static int do_mseal (unsigned long start,size_t len_in,unsigned long flags) +int do_mseal (unsigned long start,size_t len_in,unsigned long flags) {size_t len;int ret =0 ;
Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
Kees Cook
maintainerRe: [RFC PATCH v1 1/1] binfmt_elf: mseal address zero
Quoted 20 lines · On Thu, Aug 01, 2024 at 05:08:33PM +0000, [email protected] wrote:
From: Jeff Xu <[email protected]>
Some legacy SVr4 apps might depend on page on address zero to be readable, however I can't find a reason that the page ever becomes writeable, so seal it.
If there is a compain, we can make this configurable.
Signed-off-by Jeff Xu <[email protected]>--- fs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)fs/binfmt_elf.c +0
-0
fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm) · line 1320
Quoted 7 lines
fs/binfmt_elf.c +4
-0@@ -1314,6 +1314,10 @@ static int load_elf_binary(struct linux_binprm *bprm)emulate the SVr4 behavior. Sigh. */ error =vm_mmap (NULL ,0 ,PAGE_SIZE ,PROT_READ |PROT_EXEC ,MAP_FIXED |MAP_PRIVATE ,0 ); + +#ifdef CONFIG_64BIT +do_mseal (0 ,PAGE_SIZE ,0 ); +#endif
Instead of wrapping this in #ifdefs, does it make more sense to adjust the mm.h declaration instead, like this below...
include/linux/mm.h void vma_pgtable_walk_end(struct vm_area_struct *vma); · line 4205
Quoted 13 lines
fs/binfmt_elf.c +0
-0} regs =current_pt_regs ();include/linux/mm.h +2
-0@@ -4201,4 +4201,8 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma);int reserve_mem_find_by_name (const char *name,phys_addr_t *start,phys_addr_t *size); +#ifdef CONFIG_64BIT +int do_mseal (unsigned long start,size_t len_in,unsigned long flags);
#else
static inline int do_mseal(unsigned long start, size_t len_in, unsigned long flags)
{
return -ENOTSUPP;
}Quoted 19 lines
include/linux/mm.h +2
-0+#endif +#endif /* _LINUX_MM_H */ mm/mseal.c +1
-1@@ -248,7 +248,7 @@ static int apply_mm_seal(unsigned long start, unsigned long end)* * unseal() is not supported. */ -static int do_mseal (unsigned long start,size_t len_in,unsigned long flags) +int do_mseal (unsigned long start,size_t len_in,unsigned long flags) {size_t len;int ret =0 ;Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
And if it returns an error code, should we check it when used in load_elf_binary()? (And if so, should the mm.h return 0 for non-64bit?)
Signature · 1 line
Kees Cook
Re: [RFC PATCH v1 1/1] binfmt_elf: mseal address zero
Quoted 17 lines · Kees Cook
Quoted 15 lines · On Thu, Aug 01, 2024 at 05:08:33PM +0000, [email protected] wrote:
From: Jeff Xu <[email protected]>
Some legacy SVr4 apps might depend on page on address zero to be readable, however I can't find a reason that the page ever becomes writeable, so seal it.
If there is a compain, we can make this configurable.
Signed-off-by Jeff Xu <[email protected]>--- fs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)
Quoted 16 lines
fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm) · line 1320
Quoted 12 lines
fs/binfmt_elf.c +4
-0@@ -1314,6 +1314,10 @@ static int load_elf_binary(struct linux_binprm *bprm)emulate the SVr4 behavior. Sigh. */ error =vm_mmap (NULL ,0 ,PAGE_SIZE ,PROT_READ |PROT_EXEC ,MAP_FIXED |MAP_PRIVATE ,0 ); + +#ifdef CONFIG_64BIT +do_mseal (0 ,PAGE_SIZE ,0 ); +#endif Instead of wrapping this in #ifdefs, does it make more sense to adjust the
mm.hdeclaration instead, like this below...
Sure.
Quoted 11 lines
Quoted 11 lines
fs/binfmt_elf.c +0
-0} regs =current_pt_regs ();include/linux/mm.h +0
-0@@ -4201,4 +4201,8 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma);int reserve_mem_find_by_name (const char *name,phys_addr_t *start,phys_addr_t *size);
Quoted 9 lines
include/linux/mm.h void vma_pgtable_walk_end(struct vm_area_struct *vma); · line 4205
Quoted 2 lines
include/linux/mm.h +2
-0+#ifdef CONFIG_64BIT +int do_mseal (unsigned long start,size_t len_in,unsigned long flags);#else
static inline intdo_mseal(unsigned long start, size_t len_in, unsigned long flags){ return -ENOTSUPP; }
OK.
Quoted 14 lines
Quoted 14 lines
include/linux/mm.h +2
-0+#endif +#endif /* _LINUX_MM_H */ mm/mseal.c +1
-1@@ -248,7 +248,7 @@ static int apply_mm_seal(unsigned long start, unsigned long end)* * unseal() is not supported. */ -static int do_mseal (unsigned long start,size_t len_in,unsigned long flags) +int do_mseal (unsigned long start,size_t len_in,unsigned long flags) {
Quoted 9 lines
Quoted 5 lines
mm/mseal.c +0
-1size_t len;int ret =0 ; --2.46.0.rc1.232.g9752f9e123-goog
And if it returns an error code, should we check it when used in
load_elf_binary()? (And if so, should themm.hreturn 0 for non-64bit?)
It shouldn't fail. I can add pr_warning to handle the error case:pr_warning("pid=%d, couldn't seal the page on address 0.\n", task_pid_nr(current));
Thanks!
Best regards,
-Jeff
Quoted 2 lines
--
Kees Cook
Kees Cook
maintainerQuoted 6 lines · On Thu, Aug 01, 2024 at 05:08:32PM +0000, [email protected] wrote:
From: Jeff Xu <[email protected]>
In
load_elf_binaryas part of theexecve(), when the current task’s personality hasMMAP_PAGE_ZEROset, the kernel allocates one page at address 0. According to the comment:
Quoted 6 lines
/* Why this, you ask??? Well SVr4 maps page 0 as read-only,
and some applications "depend" upon this behavior. Since we do not have the power to recompile these, we
emulate the SVr4 behavior. Sigh. */At one point, Linus suggested removing this [1].
For users, I didn't find much in a Debian Code Search:
https://codesearch.debian.net/search?q=MMAP_PAGE_ZERO&literal=1&perpkg=1
I see rr uses it in testing, and some utils have it as an option, so I think maybe just leave it supported.
Quoted 4 lines
Sealing this is probably safe, the comment doesn’t say the app ever wanting to change the mapping to rwx. Sealing also ensures that never happens.
Yeah, this seems fine to me.
Quoted 14 lines
[1] https://lore.kernel.org/lkml/CAHk-=whVa=nm_GW=NVfPHqcxDbWt4JjjK1YWb0cLjO4ZSGyiDA@mail.gmail.com/
Jeff Xu (1):
binfmt_elf: mseal address zerofs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
Signature · 1 line
Kees Cook
Quoted 13 lines · Kees Cook
Quoted 11 lines · On Thu, Aug 01, 2024 at 05:08:32PM +0000, [email protected] wrote:
From: Jeff Xu <[email protected]>
In
load_elf_binaryas part of theexecve(), when the current task’s personality hasMMAP_PAGE_ZEROset, the kernel allocates one page at address 0. According to the comment:/* Why this, you ask??? Well SVr4 maps page 0 as read-only,
and some applications "depend" upon this behavior. Since we do not have the power to recompile these, we
emulate the SVr4 behavior. Sigh. */
Quoted 8 lines
Quoted 1 line
At one point, Linus suggested removing this [1].
For users, I didn't find much in a Debian Code Search:
https://codesearch.debian.net/search?q=MMAP_PAGE_ZERO&literal=1&perpkg=1I see rr uses it in testing, and some utils have it as an option, so I think maybe just leave it supported.
Thanks for checking, I will add those in the V2 cover letter.
Quoted 24 lines
Quoted 4 lines
Sealing this is probably safe, the comment doesn’t say the app ever wanting to change the mapping to rwx. Sealing also ensures that never happens.
Yeah, this seems fine to me.
Quoted 6 lines
[1] https://lore.kernel.org/lkml/CAHk-=whVa=nm_GW=NVfPHqcxDbWt4JjjK1YWb0cLjO4ZSGyiDA@mail.gmail.com/
Jeff Xu (1):
binfmt_elf: mseal address zeroQuoted 8 lines
fs/binfmt_elf.c | 4 ++++ include/linux/mm.h | 4 ++++ mm/mseal.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-)--
2.46.0.rc1.232.g9752f9e123-goog--
Kees Cook