Skip to contentSkip to conversation

binfmt_flat: Fix corruption when not offsetting data start

PATCH In review Stefan O'Rear 6 participants 6 messages Mar 26, 2024 – Aug 7, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 binfmt_flat: Fix corruption when not offsetting data start

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")
---
 fs/binfmt_flat.c | 2 ++
 1 file changed, 2 insertions(+)
fs/binfmt_flat.c +2 -0
index c26545d71d39..70c2b68988f4 100644
@@ -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);
 
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Stefan O'Rear

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

Quoted 6 lines

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")

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
Quoted 24 lines
---
 fs/binfmt_flat.c | 2 ++
 1 file changed, 2 insertions(+)
fs/binfmt_flat.c +2 -0
index c26545d71d39..70c2b68988f4 100644
@@ -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);
 
Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
In reply to Stefan O'Rear

Hi Stefan,

Quoted 23 lines · Stefan O'Rear

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")
---
  fs/binfmt_flat.c | 2 ++
  1 file changed, 2 insertions(+)
fs/binfmt_flat.c +0 -0
index c26545d71d39..70c2b68988f4 100644

fs/binfmt_flat.c static int load_flat_binary(struct linux_binprm *bprm) · line 884

Quoted 6 lines
fs/binfmt_flat.c +1 -0
@@ -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

Quoted 9 lines
fs/binfmt_flat.c +1 -0
  		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);
 
#3 lore raw
In reply to Stefan O'Rear

Hi Stefan,

Quoted 31 lines · Stefan O'Rear

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")
---
  fs/binfmt_flat.c | 2 ++
  1 file changed, 2 insertions(+)
fs/binfmt_flat.c +1 -0
index c26545d71d39..70c2b68988f4 100644
@@ -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)

fs/binfmt_flat.c static int load_flat_binary(struct linux_binprm *bprm) · line 900

Quoted 7 lines
fs/binfmt_flat.c +1 -0
@@ -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

maintainer
#4 lore raw
Quoted 39 lines · On Wed, 24 Apr 2024 13:44:31 PDT (-0700), [email protected] wrote:

Hi Stefan,

Quoted 36 lines · Stefan O'Rear

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")
---
  fs/binfmt_flat.c | 2 ++
  1 file changed, 2 insertions(+)
fs/binfmt_flat.c +2 -0
index c26545d71d39..70c2b68988f4 100644
@@ -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
 
Quoted 5 lines

fs/binfmt_flat.c static int load_flat_binary(struct linux_binprm *bprm) · line 900

Quoted 2 lines
fs/binfmt_flat.c +0 -0
  	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.

Quoted 4 lines

Thanks,

Alex

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#5 lore raw
Quoted 40 lines · Palmer Dabbelt
Quoted 39 lines · On Wed, 24 Apr 2024 13:44:31 PDT (-0700), [email protected] wrote:

Hi Stefan,

Quoted 36 lines · Stefan O'Rear

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")
---
  fs/binfmt_flat.c | 2 ++
  1 file changed, 2 insertions(+)
fs/binfmt_flat.c +2 -0
index c26545d71d39..70c2b68988f4 100644
@@ -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
 
Quoted 8 lines
Quoted 5 lines

fs/binfmt_flat.c static int load_flat_binary(struct linux_binprm *bprm) · line 900

Quoted 2 lines
fs/binfmt_flat.c +0 -0
  	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.

Signature · 6 lines
Kees Cook

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv