3. Magic revealed

 

It looks worse than you can imagine!

I can imagine some pretty bad things!

That's why I said *worse*!

 Terry Pratchett, Moving Pictures

The fancy output format of The address of main was chosen for a reason. It is valid input for /bin/sh. Let's see whether the program from The magic of the Elf has main at the same offset.

While the simplicity of ndisasm is charming, objdump requires heavy machinery. The output includes function labels. Filtering the complete disassembly can yield the desired code without prior knowledge of the function address. We use --start-address for symmetry with ndisasm, however. That option accepts only numeric values, not symbol names.

Command: pre/sparc-debian2.2-linux/magic_elf/objdump.sh
#!/bin/sh
. out/sparc-debian2.2-linux/magic_elf/addr_of_main
/usr/bin/objdump -d --start-address=0x${main_l} \
	tmp/sparc-debian2.2-linux/magic_elf/magic_elf \
| pre/sparc-debian2.2-linux/magic_elf/objdump_format.sh ${main_l}

Command: pre/sparc-debian2.2-linux/magic_elf/objdump_format.sh
#!/bin/sh
xdigit='[[:xdigit:]]\{1,\}'
start_address=${1:-$xdigit}

# white space is tab-stop, not just spaces
tab='[^	]\{1,\}	'
space='[[:space:]]\{1,\}'
nospace='[^[:space:]]\{1,\}'

/bin/sed -n -e "/^ *${start_address}:/,$ p" \
| /bin/sed \
	-e 's/ *	/	/g' \
	-e "s/${space}\(!\)/	\1/" \
	-e "s/^\(${tab}${tab}${nospace}\)${space}/\1	/" \
	-e "/^[[:space:]]*\.*[[:space:]]*$/q" \
	-e "/\<\(restore\|unimp\)\>/q" \
| /usr/bin/expand -t 12,32,40,60

Output: out/sparc-debian2.2-linux/magic_elf/objdump.asm
   1073c:   9d e3 bf 98         save    %sp, -104, %sp
   10740:   90 10 20 01         mov     1, %o0
   10744:   13 00 00 40         sethi   %hi(0x10000), %o1
   10748:   92 12 60 01         or      %o1, 1, %o1         ! 10001 <*ABS*+0x10001>
   1074c:   40 00 44 5a         call    218b4 <_PROCEDURE_LINKAGE_TABLE_+0x30>
   10750:   94 10 20 03         mov     3, %o2
   10754:   81 c7 e0 08         ret     
   10758:   91 e8 20 00         restore %g0, 0, %o0

This looks like a real main. So both programs indeed have main at the same offset. Unfortunately a brief look through /bin proves this to be pure chance. And instead of a real system call for write(2) we see something strange. It resolves to a location in a shared library. But what function in what library?

3.1. GDB to the rescue

Output: out/sparc-debian2.2-linux/magic_elf/gdb
[func=main]
0x1073c <main>:                 save    %sp, -104, %sp
0x10740 <main+4>:               mov     1, %o0
0x10744 <main+8>:               sethi   %hi(0x10000), %o1
0x10748 <main+12>:              or      %o1, 1, %o1 ! 0x10001
0x1074c <main+16>:              call    0x218b4 <write>
0x10750 <main+20>:              mov     3, %o2
0x10758 <main+28>:              restore %g0, 0, %o0

Looks better. We need a way to retrieve the function name, write, from this output. Then we can feed gdb this argument for disassembly.

Command: pre/sparc-debian2.2-linux/evil_magic/first_gdb_func.sed
#!/bin/sed -nf
/.*<\(.*\)>$/ {
	s//\1/
	p
	q
}

Command: pre/sparc-debian2.2-linux/evil_magic/gdb_write.sh
#!/bin/sh
file=${1:-tmp/sparc-debian2.2-linux/magic_elf/magic_elf}
func=$( pre/sparc-debian2.2-linux/evil_magic/first_gdb_func.sed \
	< out/sparc-debian2.2-linux/magic_elf/gdb )

/bin/echo "[func=${func}]"
pre/sparc-debian2.2-linux/magic_elf/gdb_core.sh ${file} ${func} \
| pre/sparc-debian2.2-linux/magic_elf/gdb_format.sh

Output: out/sparc-debian2.2-linux/evil_magic/write.gdb
[func=write]
0x218b4 <write>:                sethi   %hi(0xc000), %g1
0x218b8 <write+4>:              b,a     0x21884 <_PROCEDURE_LINKAGE_TABLE_>

Oops. Shared libraries don't share their secrets with everyone.

3.2. In doubt use force

We can now search for a fine manual explaining how to debug shared libraries. Or just compile the bugger static.

Seems we found an easy way to fill up the hard disk. Anyway, what has gdb(1) to say about it?

The function was called write before, it is called write now. Let's look what is behind the name.

3.3. Write your name

Above disassembly is not guaranteed to work. The names of symbols imported by libraries differ from one platform to the other, and from one compiler to the other. A more rational approach is to search the listing of all symbols for similar names and identical addresses.

I suspect there is actually order behind the chaos. The symbol __write, with a varying number of leading underscores, seems to be "the real thing" on all platforms. The aliases for the value, 0x184e8, differ a lot.

There are two man pages giving some overview of system calls, intro(2) and syscalls(2). /usr/include/unistd.h declares a traditional general purpose interface called syscall. Not all Linux system have man page syscall(2), though. Anyway, the statement mov 4,%g1 corresponds to the value of __NR_write in /usr/include/asm/unistd.h. Note that Linux uses ta 0x10 while Solaris uses ta 8. There are other differences, but they are beyond the scope of this document.