https://sourceware.org/bugzilla/show_bug.cgi?id=35076

            Bug ID: 35076
           Summary: x86 --rosegment -z relro: RW file bytes share the R E
                    segment's file page
           Product: binutils
           Version: 2.47
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: binutils
          Assignee: unassigned at sourceware dot org
          Reporter: mark at markesler dot com
  Target Milestone: ---

With -z separate-code --rosegment -z relro on x86-64, ld 2.47 places the RW
PT_LOAD's first file bytes in the file page that the R E PT_LOAD maps. The
loader maps that page r-x, so the initial on-disk bytes of .init_array,
.fini_array, .dynamic and .got, and the start of .got.plt, are mapped
executable. The segments' virtual pages stay disjoint; the overlap is one
file page mapped twice. ld.texi describes -z separate-code as keeping code
"in wholly disjoint pages from any other data".

Either --no-rosegment or -z norelro avoids it. Fedora 45's binutils is
configured with --enable-rosegment, and Fedora packages link with -z relro
(%build_ldflags in redhat-rpm-config), so this is the default layout there,
PIE included.

Environment:
  binutils-2.47-1.fc45 (GNU ld version 2.47.20260726), gcc-16.2.1-3.fc45,
  glibc-2.44-2.fc45, x86_64; default script "-z combreloc -z separate-code
  --rosegment".
  Master e38505b664 (GNU ld 2.47.50.20261130), built with default configure
  options in the same environment, gives the same result; the reproducer
  passes -z separate-code and --rosegment itself.
  Comparison: binutils-2.46.1-1.fc44.

Reproducer (readelf output trimmed to the LOAD, GNU_RELRO and mapping lines):

  $ echo 'int main(void){return 0;}' > m.c
  $ gcc -fno-pie -no-pie -Wl,-z,separate-code -Wl,--rosegment -Wl,-z,relro -o m
m.c
  $ readelf -lW m
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000570
0x000570 R   0x1000
  LOAD           0x001000 0x0000000000401000 0x0000000000401000 0x000131
0x000131 R E 0x1000
  LOAD           0x001e38 0x0000000000402e38 0x0000000000402e38 0x0001cc
0x0001d0 RW  0x1000
  GNU_RELRO      0x001e38 0x0000000000402e38 0x0000000000402e38 0x0001c8
0x0001c8 R   0x1
   03     .init .text .fini
   04     .init_array .fini_array .dynamic .got .got.plt .data .bss

The R E segment's file bytes are 0x1000-0x1131. The RW segment starts at
file offset 0x1e38, in the same file page.

At run time, a second program built with the same flags prints its own
mappings:

  $ cat maps.c
  #include <stdio.h>
  #include <string.h>
  extern char __executable_start[];
  extern void (*__init_array_start[])(void);
  int main(void) {
    char line[512]; FILE *f = fopen("/proc/self/maps", "r");
    while (f && fgets(line, sizeof line, f)) if (strstr(line, "/maps-"))
fputs(line, stdout);
    printf("__init_array_start is at %p (the RW mapping)\n", (void
*)__init_array_start);
    return 0;
  }
  $ gcc -fno-pie -no-pie -Wl,-z,separate-code -Wl,--rosegment -Wl,-z,relro -o
maps-rose maps.c
  $ ./maps-rose
  00400000-00401000 r--p 00000000 ...
  00401000-00402000 r-xp 00001000 ...
  00402000-00403000 r--p 00001000 ...   (RELRO page)
  00403000-00404000 rw-p 00002000 ...

File offset 0x1000 is mapped twice, once r-x. In that program .init_array
is at file offset 0x1df8, so its initial bytes lie in the r-x mapping at
0x401df8. With --no-rosegment the r-x mapping is offset 0x1000 and the
RELRO page is offset 0x2000.

RW p_offset, all with -z separate-code (* = in the R E segment's file page):

                       --rosegment          --no-rosegment
                       relro     norelro    relro     norelro
  2.47     -no-pie     0x1e38 *  0x2000     0x2e38    0x2120
  2.47     -pie        0x1dc8 *  0x2000     0x2dc8    0x2108
  master   -no-pie     0x1e38 *  0x2000     0x2e38    0x2120
  master   -pie        0x1df8 *  0x2000     0x2df8    0x2128

With -z max-page-size=0x10000 or 0x200000 the RW segment starts at 0x1fe38
or 0x3ffe38, again in the code segment's max-page-size page; whether that
page is mapped r-x as a whole depends on the run-time page size.

Expected: the RW segment's first file byte lies in a page the R E segment
does not map, as with --no-rosegment.

The bytes involved are the unrelocated initial values (offsets in a PIE),
in place of the zero padding that ended the code page before. That adds a
few hundred bytes of non-code to an executable mapping; it is not a
vulnerability.

Related: PR 34003 and 99600a47. With these flags 2.46.1 put the ELF headers
and code in an R E segment at offset 0 and the read-only data in an R
segment at 0x1000, whose file page the RW segment shared. 99600a47 moves
the headers out of the executable segment; the RW bytes now share the
code's file page instead. The "after" example in its commit message shows
the same layout (R E at 0x1000, filesize 0x5; RW at 0x1f78).

Not tested: other architectures, gold, lld. ALL_TEXT_AFTER_RO is set only
in the x86 emulparams. A search of Bugzilla found no report of this on
x86; PR 32221 shows a similar layout on arc-elf (RW at file offset 0x1cc,
in the R E segment's page).

Thanks for looking.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to