Skip to contentSkip to conversation

Elf loader crash while zero-filling .bss

PATCH In review Abel Bernabeu 5 participants 8 messages Feb 11, 2008 1 message not in the archive

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

PATCH 1/1 Elf loader crash while zero-filling .bss

I've finally found a solution for the crash in load_binary_elf I reported last week:

http://lkml.org/lkml/2008/1/30/171

The attached patch solves my problem, but please test it yourself...

set_brk(start, end) allocs just page aligned regions (by "colapsing" both extremes to the start of the page in which they lay)... That means than even if both pointers are not equal there are still some chances that set_brk has allocated no space at all because ELF_PAGEALIGN(elf_bss) != ELF_PAGEALIGN(elf_brk).

So the condition was not correct.

fs/binfmt_elf.c +1 -1
@@ -939,3 +939,3 @@ static int load_elf_binary(struct linux_
 	}
-	if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
+	if (likely(ELF_PAGEALIGN(elf_bss) != ELF_PAGEALIGN(elf_brk)) &&

unlikely(padzero(elf_bss))) {
send_sig(SIGSEGV, current, 0);

#1 lore raw

2008/2/11, Abel Bernabeu <[email protected]>:

Quoted 5 lines

I've finally found a solution for the crash in load_binary_elf I reported last week:

http://lkml.org/lkml/2008/1/30/171

Quoted 7 lines

The attached patch solves my problem, but please test it yourself...

set_brk(start, end) allocs just page aligned regions (by "colapsing" both extremes to the start of the page in which they lay)... That means than even if both pointers are not equal there are still some chances that set_brk has allocated no space at all because ELF_PAGEALIGN(elf_bss) != ELF_PAGEALIGN(elf_brk).

Sorry this was an errata in my comment: no space is allocated at all because ELF_PAGEALIGN(elf_bss) == ELF_PAGEALIGN(elf_brk)

#2 lore raw
Quoted 7 lines · Abel Bernabeu
Quoted 5 lines

set_brk(start, end) allocs just page aligned regions (by "colapsing" both extremes to the start of the page in which they lay)... That means than even if both pointers are not equal there are still some chances that set_brk has allocated no space at all because ELF_PAGEALIGN(elf_bss) != ELF_PAGEALIGN(elf_brk).

Sorry this was an errata in my comment: no space is allocated at all because ELF_PAGEALIGN(elf_bss) == ELF_PAGEALIGN(elf_brk)

I also don't fully understand the "colapsing" part ... ELF_PAGEALIGN() really aligns the address to the nearest page boundary at the higher address ... so I don't understand the comment about colapsing to the start of the page that contains the address, it's seems to me that the real behavior is quite the opposite.

Signature · 2 lines
Jiri Kosina
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 9 lines · Abel Bernabeu

In such a way that set_brk(0x0, 0x100) does not alloc any space at all. There are just more ways to get no memory allocation than set_brk(elf_bss, elf_bss) (the equalness condition i've changed). Sorry, the correct description for the patch may be:
set_brk(start, end) allocs just page aligned regions (by "colapsing" both extremes to the END of the page in which they lay)... That means than even if both pointers are not equal there are still some chances that set_brk has allocated no space at all because because ELF_PAGEALIGN(elf_bss) == ELF_PAGEALIGN(elf_brk)

Now, the question is whether it is valid for ELF binary to not have the end of .bss section (if present at all) not page-aligned.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#5 lore raw
Quoted 10 lines · Jiri Kosina
Quoted 7 lines · Abel Bernabeu

In such a way that set_brk(0x0, 0x100) does not alloc any space at all. There are just more ways to get no memory allocation than set_brk(elf_bss, elf_bss) (the equalness condition i've changed). Sorry, the correct description for the patch may be:
set_brk(start, end) allocs just page aligned regions (by "colapsing"
both extremes to the END of the page in which they lay)... That means
than even if both pointers are not equal there are still some chances
that set_brk has allocated no space at all because because
ELF_PAGEALIGN(elf_bss) == ELF_PAGEALIGN(elf_brk)

Quoted 2 lines

Now, the question is whether it is valid for ELF binary to not have the end of .bss section (if present at all) not page-aligned.

Why wouldn't it be? It would, however, be valid to the kernel to round it up to the next boundary.

-hpa

#6 lore raw
Quoted 4 lines · H. Peter Anvin
Quoted 2 lines

Now, the question is whether it is valid for ELF binary to not have the end of .bss section (if present at all) not page-aligned.

Why wouldn't it be? It would, however, be valid to the kernel to round it up to the next boundary.

I wasn't immediately sure if there is nothing in ELF specification that would forbid that.

Then I think that Abel's patch is correct.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#7 lore raw
Quoted 10 lines · Jiri Kosina
Quoted 4 lines · H. Peter Anvin
Quoted 2 lines

Now, the question is whether it is valid for ELF binary to not have the end of .bss section (if present at all) not page-aligned.

Why wouldn't it be? It would, however, be valid to the kernel to round it up to the next boundary.

I wasn't immediately sure if there is nothing in ELF specification that would forbid that.

The only requirement for a loadable segment is that its address is congruent modulo alignment with the file offset.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#8 lore raw
In reply to Abel Bernabeu
Quoted 5 lines · Abel Bernabeu

I've finally found a solution for the crash in load_binary_elf I reported last week:

http://lkml.org/lkml/2008/1/30/171

Quoted 7 lines

The attached patch solves my problem, but please test it yourself...

set_brk(start, end) allocs just page aligned regions (by "colapsing" both extremes to the start of the page in which they lay)... That means than even if both pointers are not equal there are still some chances that set_brk has allocated no space at all because ELF_PAGEALIGN(elf_bss) != ELF_PAGEALIGN(elf_brk).

What architecture was this?
Most architectures align .bss properly but it seems arm does not and I guess this is needed.

As .bss was empty? in your case you did not trigger
any alignmnet by linker due to largest member in section => boom.

I do think your patch paper over the real bug.

Sam