5. Segments

 

Well, my terminal's locked up, and I ain't got any Mail,
And I can't recall the last time that my program didn't fail;
I've got stacks in my structs, I've got arrays in my queues,
I've got the : Segmentation violation -- Core dumped blues.

ELF provides two parallel views of a file's contents. The linking view is defined by the section header table, an array of Elf32_Shdr. The execution view is defined by the program header table, an array of Elf32_Phdr.

Theoretically the ELF specification is quite liberal. Position, contents and order of sections and segments are not restricted. But in real life an operating system is to used to just one program loader, one linker and few compilers. This makes the work of virus writers easier. We can reverse engineer the de-facto standard, a tiny subset of what the ELF standard allows. On a typical system only a minority of programs violates this subset, so ignoring them does not lower chances of survival.

All headers necessary to execute a program are stuffed into the first page (0x1000 bytes on sparc), probably to simplify the design of an OS' executable format detector. Dynamically linked executables require a few program headers more than static executables, but that's about all the variation there is. In any case the program header of the code segments is followed by the program header of the data segment. Static executables have the code segment at index 0, dynamically linked executables at index 2.

Let's get a bit more serious and examine the assembly program from The language of evil. A standalone executable built from assembler source is probably the most trivial example we can find.

5.1. objdump -fp

5.2. readelf -l

objdump's output is butt-ugly. On to readelf. The line starting with "There are 2 program headers" shows the value of e_phnum and e_phoff.

5.3. Observations

Nice to see the entry point (0x10074) again. Program layout is a simplified variation of Sort of an answer. The value of FileSiz includes ELF header and program header.

Overhead:

overhead = Entry point - VirtAddr =

0x10074 - 0x10000 = 0x74 = 116 bytes

Effective code size:

code size = FileSiz - overhead =

0x98 - 0x74 = 0x24 = 36 bytes

This matches with the disassembly listing. However, the ratio of file size to effective code deserves the title "Bloat", with capital B. Only 8 percent of the file actually do something useful!

Bloat factor:

code size / file size = 36 / 444 = 0.081

5.4. Segments of /bin/sh

Anyway, we see that even for trivial examples the code is surrounded by lots of other stuff. Let's zoom in on our target.

Looks intimidating. But then the ELF specification says that only segments of type "LOAD" are considered for execution. Since the flags of the first one include "execute" but not "write" it must be the code segment. The other one has the "write" flags set, so it must be the data segment. There is one possible deviation: On sparc-sunos most executables built by Sun feature a data segment with "execute" flag.

MemSiz (0x9070) is larger than FileSiz (0x57cc) in the data segment. Just like with mmap(2) excessive bytes are defined to be initialized with 0. The linker takes advantages of that by grouping all variables that should be initialized to zero at the end. Note that the last section of segment 3 (counting starts with 0) is called .bss, the traditional name for this kind of area.

The mapping for segment 2 looks even more complex. But I would guess that .rodata means "read-only data" and .text contains productive code, as opposed to the administrative stuff in the other sections.

Some executables of Red Hat 8.0 have an additional program header of type GNU_EH_FRAME.

5.5. Self modifying code

Previous examples in The language of evil used an __attribute__ clause to put the code into section .text. Without that it would end up in section .rodata. Both are members of the code segment which is executable in it its entireness; in this regard that would make no difference.

But what about putting the code a write enabled data segment? These settings can probably be changed by mprotect(2). But what are the default settings?

Output = Source: out/sparc-debian2.2-linux/evil_magic/func.inc
const unsigned char in_code[]
__attribute__ (( aligned(8), section(".text") )) =
{
  0x82,0x10,0x20,0x04,           /* 0: mov 4, %g1                    */
  0x90,0x10,0x20,0x01,           /* 4: mov 1, %o0                    */
  0x13,0x00,0x00,0x40,           /* 8: sethi %hi(0x10000), %o1       */
  0x92,0x12,0x60,0x01,           /* c: or %o1, 1, %o159              */
  0x94,0x10,0x20,0x03,           /* 10: mov 3, %o2                   */
  0x91,0xd0,0x20,0x10,           /* 14: ta 0x10                      */
  0x81,0xc3,0xe0,0x08,           /* 18: retl                         */
  0x01,0x00,0x00,0x00            /* 1c: nop                          */
}; /* 32 bytes (0x20) */

Source: pre/sparc-debian2.2-linux/evil_magic/self_modify.c
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "func.inc"

typedef void (*PfnVoid)(void);

#define TEST(where) \
	printf("%8p is " #where " ... ", in_##where); fflush(stdout); \
	received_sigill = 0; \
	if (0 == setjmp(env)) { (*(PfnVoid)in_##where)(); } \
	printf(" sigill=%d\n", received_sigill);
#define MEMCPY_TEST(where) \
	memcpy(in_##where, in_code, sizeof(in_code)); \
	TEST(where)

static jmp_buf env;
static int received_sigill = 0;
static void on_sigill(int sig)
{
  signal(SIGILL, on_sigill);
  printf(" on_sigill=%d ", sig);
  received_sigill = 1;
  longjmp(env, 1);
}

static char in_data[sizeof(in_code)];

int main()
{
  char* in_heap = malloc(sizeof(in_code));
  char in_stack[sizeof(in_code)];

  signal(SIGILL, on_sigill);
  TEST(code);
  MEMCPY_TEST(data);
  MEMCPY_TEST(heap);
  MEMCPY_TEST(stack);
  return 0;
}

Output: out/sparc-debian2.2-linux/evil_magic/self_modify
 0x10818 is code ... ELF sigill=0
 0x21e58 is data ... ELF sigill=0
 0x21e88 is heap ... ELF sigill=0
0xefffeb68 is stack ... ELF sigill=0