Skip to content

Commit 04d82a6

Browse files
damien-lemoalGreg Ungerer
authored andcommitted
binfmt_flat: allow not offsetting data start
Commit 2217b98 ("binfmt_flat: revert "binfmt_flat: don't offset the data start"") restored offsetting the start of the data section by a number of words defined by MAX_SHARED_LIBS. As a result, since MAX_SHARED_LIBS is never 0, a gap between the text and data sections always exists. For architectures which cannot support a such gap between the text and data sections (e.g. riscv nommu), flat binary programs cannot be executed. To allow an architecture to request no data start offset to allow for contiguous text and data sections for binaries flagged with FLAT_FLAG_RAM, introduce the new config option CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c to MAX_SHARED_LIBS for architectures tolerating or needing the data start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case) and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled. DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the data section length and start position. Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com> Signed-off-by: Greg Ungerer <gerg@linux-m68k.org>
1 parent bf05bf1 commit 04d82a6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

fs/Kconfig.binfmt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
112112
config BINFMT_FLAT_OLD_ALWAYS_RAM
113113
bool
114114

115+
config BINFMT_FLAT_NO_DATA_START_OFFSET
116+
bool
117+
115118
config BINFMT_FLAT_OLD
116119
bool "Enable support for very old legacy flat binaries"
117120
depends on BINFMT_FLAT

fs/binfmt_flat.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
#define MAX_SHARED_LIBS (1)
7575
#endif
7676

77+
#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
78+
#define DATA_START_OFFSET_WORDS (0)
79+
#else
80+
#define DATA_START_OFFSET_WORDS (MAX_SHARED_LIBS)
81+
#endif
82+
7783
struct lib_info {
7884
struct {
7985
unsigned long start_code; /* Start of text segment */
@@ -576,7 +582,8 @@ static int load_flat_file(struct linux_binprm *bprm,
576582
goto err;
577583
}
578584

579-
len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
585+
len = data_len + extra +
586+
DATA_START_OFFSET_WORDS * sizeof(unsigned long);
580587
len = PAGE_ALIGN(len);
581588
realdatastart = vm_mmap(NULL, 0, len,
582589
PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
@@ -591,7 +598,7 @@ static int load_flat_file(struct linux_binprm *bprm,
591598
goto err;
592599
}
593600
datapos = ALIGN(realdatastart +
594-
MAX_SHARED_LIBS * sizeof(unsigned long),
601+
DATA_START_OFFSET_WORDS * sizeof(unsigned long),
595602
FLAT_DATA_ALIGN);
596603

597604
pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
@@ -622,7 +629,8 @@ static int load_flat_file(struct linux_binprm *bprm,
622629
memp_size = len;
623630
} else {
624631

625-
len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
632+
len = text_len + data_len + extra +
633+
DATA_START_OFFSET_WORDS * sizeof(u32);
626634
len = PAGE_ALIGN(len);
627635
textpos = vm_mmap(NULL, 0, len,
628636
PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
@@ -638,7 +646,7 @@ static int load_flat_file(struct linux_binprm *bprm,
638646

639647
realdatastart = textpos + ntohl(hdr->data_start);
640648
datapos = ALIGN(realdatastart +
641-
MAX_SHARED_LIBS * sizeof(u32),
649+
DATA_START_OFFSET_WORDS * sizeof(u32),
642650
FLAT_DATA_ALIGN);
643651

644652
reloc = (__be32 __user *)
@@ -714,7 +722,7 @@ static int load_flat_file(struct linux_binprm *bprm,
714722
ret = result;
715723
pr_err("Unable to read code+data+bss, errno %d\n", ret);
716724
vm_munmap(textpos, text_len + data_len + extra +
717-
MAX_SHARED_LIBS * sizeof(u32));
725+
DATA_START_OFFSET_WORDS * sizeof(u32));
718726
goto err;
719727
}
720728
}

0 commit comments

Comments
 (0)