diff mbox series

binfmt_flat: Fix corruption when not offsetting data start

Message ID 20240326032037.2478816-1-sorear@fastmail.com (mailing list archive)
State Changes Requested, archived
Headers show
Series binfmt_flat: Fix corruption when not offsetting data start | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR fail PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 success .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 fail .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Stefan O'Rear March 26, 2024, 3:20 a.m. UTC
Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
introduced a RISC-V specific variant of the FLAT format which does not
allocate any space for the (obsolescent) array of shared library
pointers. However, it did not disable the code which initializes the
array, resulting in the corruption of sizeof(long) bytes before the DATA
segment, generally the end of the TEXT segment.

Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the
shared library pointer region so that it will only be initialized if
space is reserved for it.

Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
Tested-by: Waldemar Brodkorb <wbx@openadk.org>
---
 fs/binfmt_flat.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Damien Le Moal March 26, 2024, 5:53 a.m. UTC | #1
On 3/26/24 12:20, Stefan O'Rear wrote:
> Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> introduced a RISC-V specific variant of the FLAT format which does not
> allocate any space for the (obsolescent) array of shared library
> pointers. However, it did not disable the code which initializes the
> array, resulting in the corruption of sizeof(long) bytes before the DATA
> segment, generally the end of the TEXT segment.
> 
> Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the

s/initialization the/the initialization of the

> shared library pointer region so that it will only be initialized if
> space is reserved for it.
> 
> Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
> Tested-by: Waldemar Brodkorb <wbx@openadk.org>

It feels like CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is not a very good name...
Maybe something like CONFIG_BINFMT_FLAT_NO_SHARED_LIBRARY would be better. But
renaming can be done in a followup patch.

Reviewed-by: Damien Le Moal <dlemoal

> ---
>  fs/binfmt_flat.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
> index c26545d71d39..70c2b68988f4 100644
> --- a/fs/binfmt_flat.c
> +++ b/fs/binfmt_flat.c
> @@ -879,6 +879,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>  	if (res < 0)
>  		return res;
>  
> +#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>  	/* Update data segment pointers for all libraries */
>  	for (i = 0; i < MAX_SHARED_LIBS; i++) {
>  		if (!libinfo.lib_list[i].loaded)
> @@ -893,6 +894,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>  				return -EFAULT;
>  		}
>  	}
> +#endif
>  
>  	set_binfmt(&flat_format);
>
Greg Ungerer April 4, 2024, 7:40 a.m. UTC | #2
Hi Stefan,

On 26/3/24 13:20, Stefan O'Rear wrote:
> Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> introduced a RISC-V specific variant of the FLAT format which does not
> allocate any space for the (obsolescent) array of shared library
> pointers. However, it did not disable the code which initializes the
> array, resulting in the corruption of sizeof(long) bytes before the DATA
> segment, generally the end of the TEXT segment.
> 
> Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the
> shared library pointer region so that it will only be initialized if
> space is reserved for it.
> 
> Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
> Tested-by: Waldemar Brodkorb <wbx@openadk.org>
> ---
>   fs/binfmt_flat.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
> index c26545d71d39..70c2b68988f4 100644
> --- a/fs/binfmt_flat.c
> +++ b/fs/binfmt_flat.c
> @@ -879,6 +879,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>   	if (res < 0)
>   		return res;
>   
> +#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>   	/* Update data segment pointers for all libraries */
>   	for (i = 0; i < MAX_SHARED_LIBS; i++) {
                         ^^^^^^^^^^^^^^^

It may be cleaner (ie no # conditional required) if this was changed to
DATA_START_OFFSET_WORDS. So becoming:

         for (i = 0; i < DATA_START_OFFSET_WORDS; i++) {

My only concern is does this make it less clear what the code is doing?

Regards
Greg


>   		if (!libinfo.lib_list[i].loaded)
> @@ -893,6 +894,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>   				return -EFAULT;
>   		}
>   	}
> +#endif
>   
>   	set_binfmt(&flat_format);
>
Alexandre Ghiti April 24, 2024, 8:44 p.m. UTC | #3
Hi Stefan,

On 26/03/2024 04:20, Stefan O'Rear wrote:
> Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> introduced a RISC-V specific variant of the FLAT format which does not
> allocate any space for the (obsolescent) array of shared library
> pointers. However, it did not disable the code which initializes the
> array, resulting in the corruption of sizeof(long) bytes before the DATA
> segment, generally the end of the TEXT segment.
>
> Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the
> shared library pointer region so that it will only be initialized if
> space is reserved for it.
>
> Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
> Tested-by: Waldemar Brodkorb <wbx@openadk.org>
> ---
>   fs/binfmt_flat.c | 2 ++
>   1 file changed, 2 insertions(+)
>
> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
> index c26545d71d39..70c2b68988f4 100644
> --- a/fs/binfmt_flat.c
> +++ b/fs/binfmt_flat.c
> @@ -879,6 +879,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>   	if (res < 0)
>   		return res;
>   
> +#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>   	/* Update data segment pointers for all libraries */
>   	for (i = 0; i < MAX_SHARED_LIBS; i++) {
>   		if (!libinfo.lib_list[i].loaded)
> @@ -893,6 +894,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>   				return -EFAULT;
>   		}
>   	}
> +#endif
>   
>   	set_binfmt(&flat_format);
>   

I have this fix on my list for quite some time, will you respin a new 
version for 6.9?

Thanks,

Alex
Palmer Dabbelt Aug. 7, 2024, 2:05 p.m. UTC | #4
On Wed, 24 Apr 2024 13:44:31 PDT (-0700), alex@ghiti.fr wrote:
> Hi Stefan,
>
> On 26/03/2024 04:20, Stefan O'Rear wrote:
>> Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
>> introduced a RISC-V specific variant of the FLAT format which does not
>> allocate any space for the (obsolescent) array of shared library
>> pointers. However, it did not disable the code which initializes the
>> array, resulting in the corruption of sizeof(long) bytes before the DATA
>> segment, generally the end of the TEXT segment.
>>
>> Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the
>> shared library pointer region so that it will only be initialized if
>> space is reserved for it.
>>
>> Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
>> Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
>> Tested-by: Waldemar Brodkorb <wbx@openadk.org>
>> ---
>>   fs/binfmt_flat.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>> index c26545d71d39..70c2b68988f4 100644
>> --- a/fs/binfmt_flat.c
>> +++ b/fs/binfmt_flat.c
>> @@ -879,6 +879,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>>   	if (res < 0)
>>   		return res;
>>
>> +#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>   	/* Update data segment pointers for all libraries */
>>   	for (i = 0; i < MAX_SHARED_LIBS; i++) {
>>   		if (!libinfo.lib_list[i].loaded)
>> @@ -893,6 +894,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
>>   				return -EFAULT;
>>   		}
>>   	}
>> +#endif
>>
>>   	set_binfmt(&flat_format);
>>
>
> I have this fix on my list for quite some time, will you respin a new
> version for 6.9?

IIRC we asked on IRC and Sorear wasn't going to respin the patch, so 
unless someone else wants to pick it up I think it's just going to get 
lost.

>
> Thanks,
>
> Alex
Kees Cook Aug. 7, 2024, 7:52 p.m. UTC | #5
On Wed, Aug 07, 2024 at 07:05:11AM -0700, Palmer Dabbelt wrote:
> On Wed, 24 Apr 2024 13:44:31 PDT (-0700), alex@ghiti.fr wrote:
> > Hi Stefan,
> > 
> > On 26/03/2024 04:20, Stefan O'Rear wrote:
> > > Commit 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> > > introduced a RISC-V specific variant of the FLAT format which does not
> > > allocate any space for the (obsolescent) array of shared library
> > > pointers. However, it did not disable the code which initializes the
> > > array, resulting in the corruption of sizeof(long) bytes before the DATA
> > > segment, generally the end of the TEXT segment.
> > > 
> > > Use CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard initialization the
> > > shared library pointer region so that it will only be initialized if
> > > space is reserved for it.
> > > 
> > > Fixes: 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")
> > > Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
> > > Tested-by: Waldemar Brodkorb <wbx@openadk.org>
> > > ---
> > >   fs/binfmt_flat.c | 2 ++
> > >   1 file changed, 2 insertions(+)
> > > 
> > > diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
> > > index c26545d71d39..70c2b68988f4 100644
> > > --- a/fs/binfmt_flat.c
> > > +++ b/fs/binfmt_flat.c
> > > @@ -879,6 +879,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
> > >   	if (res < 0)
> > >   		return res;
> > > 
> > > +#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
> > >   	/* Update data segment pointers for all libraries */
> > >   	for (i = 0; i < MAX_SHARED_LIBS; i++) {
> > >   		if (!libinfo.lib_list[i].loaded)
> > > @@ -893,6 +894,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
> > >   				return -EFAULT;
> > >   		}
> > >   	}
> > > +#endif
> > > 
> > >   	set_binfmt(&flat_format);
> > > 
> > 
> > I have this fix on my list for quite some time, will you respin a new
> > version for 6.9?
> 
> IIRC we asked on IRC and Sorear wasn't going to respin the patch, so unless
> someone else wants to pick it up I think it's just going to get lost.

I took a look at this and sent a v2 just now.
diff mbox series

Patch

diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index c26545d71d39..70c2b68988f4 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -879,6 +879,7 @@  static int load_flat_binary(struct linux_binprm *bprm)
 	if (res < 0)
 		return res;
 
+#ifndef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
 	/* Update data segment pointers for all libraries */
 	for (i = 0; i < MAX_SHARED_LIBS; i++) {
 		if (!libinfo.lib_list[i].loaded)
@@ -893,6 +894,7 @@  static int load_flat_binary(struct linux_binprm *bprm)
 				return -EFAULT;
 		}
 	}
+#endif
 
 	set_binfmt(&flat_format);