Skip to content

Commit 4471a67

Browse files
Jiri Kosinatorvalds
authored andcommitted
brk: COMPAT_BRK: fix detection of randomized brk
5520e89 ("brk: fix min_brk lower bound computation for COMPAT_BRK") tried to get the whole logic of brk randomization for legacy (libc5-based) applications finally right. It turns out that the way to detect whether brk has actually been randomized in the end or not introduced by that patch still doesn't work for those binaries, as reported by Geert: : /sbin/init from my old m68k ramdisk exists prematurely. : : Before the patch: : : | brk(0x80005c8e) = 0x80006000 : : After the patch: : : | brk(0x80005c8e) = 0x80005c8e : : Old libc5 considers brk() to have failed if the return value is not : identical to the requested value. I don't like it, but currently see no better option than a bit flag in task_struct to catch the CONFIG_COMPAT_BRK && randomize_va_space == 2 case. Signed-off-by: Jiri Kosina <[email protected]> Tested-by: Geert Uytterhoeven <[email protected]> Reported-by: Geert Uytterhoeven <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5de1743 commit 4471a67

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

fs/binfmt_elf.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,9 +941,13 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
941941
current->mm->start_stack = bprm->p;
942942

943943
#ifdef arch_randomize_brk
944-
if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1))
944+
if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
945945
current->mm->brk = current->mm->start_brk =
946946
arch_randomize_brk(current->mm);
947+
#ifdef CONFIG_COMPAT_BRK
948+
current->brk_randomized = 1;
949+
#endif
950+
}
947951
#endif
948952

949953
if (current->personality & MMAP_PAGE_ZERO) {

include/linux/sched.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,9 @@ struct task_struct {
12541254
#endif
12551255

12561256
struct mm_struct *mm, *active_mm;
1257+
#ifdef CONFIG_COMPAT_BRK
1258+
unsigned brk_randomized:1;
1259+
#endif
12571260
#if defined(SPLIT_RSS_COUNTING)
12581261
struct task_rss_stat rss_stat;
12591262
#endif

mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
259259
* randomize_va_space to 2, which will still cause mm->start_brk
260260
* to be arbitrarily shifted
261261
*/
262-
if (mm->start_brk > PAGE_ALIGN(mm->end_data))
262+
if (current->brk_randomized)
263263
min_brk = mm->start_brk;
264264
else
265265
min_brk = mm->end_data;

0 commit comments

Comments
 (0)