Skip to contentSkip to conversation

binfmt_flat: Fix corruption when not offsetting data start

PATCH In review Kees Cook 3 participants 3 messages Aug 7, 2024

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

PATCH v2 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 (obsolete) 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.

Introduce MAX_SHARED_LIBS_UPDATE which depends on the state of CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard 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")

---
v2: update based on v1 feedback
v1: https://lore.kernel.org/linux-mm/[email protected]/
Cc: Palmer Dabbelt <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Greg Ungerer <[email protected]>
Cc: Damien Le Moal <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 fs/binfmt_flat.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/binfmt_flat.c +3 -1
index c26545d71d39..cd6d5bbb4b9d 100644
@@ -72,8 +72,10 @@
 
 #ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
 #define DATA_START_OFFSET_WORDS		(0)
+#define MAX_SHARED_LIBS_UPDATE		(0)
 #else
 #define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
+#define MAX_SHARED_LIBS_UPDATE		(MAX_SHARED_LIBS)
 #endif
 
 struct lib_info {
@@ -880,7 +882,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
 		return res;
 
 	/* Update data segment pointers for all libraries */
-	for (i = 0; i < MAX_SHARED_LIBS; i++) {
+	for (i = 0; i < MAX_SHARED_LIBS_UPDATE; i++) {
 		if (!libinfo.lib_list[i].loaded)
 			continue;
 		for (j = 0; j < MAX_SHARED_LIBS; j++) {
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 12 lines · Kees Cook

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 (obsolete) 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.

Introduce MAX_SHARED_LIBS_UPDATE which depends on the state of CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard the initialization of the shared library pointer region so that it will only be initialized if space is reserved for it.

Quoted 4 lines
Fixes 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")

Looks good to me.

Signature · 2 lines
Damien Le Moal
Western Digital Research
Acked-by on v2 1/1
#2 lore raw
In reply to Kees Cook

Hi Kees,

Quoted 12 lines · Kees Cook

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 (obsolete) 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.

Introduce MAX_SHARED_LIBS_UPDATE which depends on the state of CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to guard the initialization of the shared library pointer region so that it will only be initialized if space is reserved for it.

Quoted 4 lines
Fixes 04d82a6d0881 ("binfmt_flat: allow not offsetting data start")

Looks good.

Regards
Greg

Quoted 41 lines

---
v2: update based on v1 feedback
v1: https://lore.kernel.org/linux-mm/[email protected]/
Cc: Palmer Dabbelt <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Greg Ungerer <[email protected]>
Cc: Damien Le Moal <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
  fs/binfmt_flat.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fs/binfmt_flat.c +3 -1
index c26545d71d39..cd6d5bbb4b9d 100644
@@ -72,8 +72,10 @@
 
  #ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
  #define DATA_START_OFFSET_WORDS		(0)
+#define MAX_SHARED_LIBS_UPDATE		(0)
  #else
  #define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
+#define MAX_SHARED_LIBS_UPDATE		(MAX_SHARED_LIBS)
  #endif
 
  struct lib_info {
@@ -880,7 +882,7 @@ static int load_flat_binary(struct linux_binprm *bprm)
  		return res;
 
  	/* Update data segment pointers for all libraries */
-	for (i = 0; i < MAX_SHARED_LIBS; i++) {
+	for (i = 0; i < MAX_SHARED_LIBS_UPDATE; i++) {
  		if (!libinfo.lib_list[i].loaded)
  			continue;
  		for (j = 0; j < MAX_SHARED_LIBS; j++) {