pwntools icon indicating copy to clipboard operation
pwntools copied to clipboard

modified options in make_elf to disable newer GNU linker warnings

Open tj-oconnor opened this issue 3 years ago • 2 comments

linker options

the stable/dev repositories of asm/make_elf throw when building ELFs due to an error with a newly implemented GNU linker warning that warns about LOAD segment with RWX permissions. Adding the --no-warn-rwx-segments and --no-warn-execstack linker options remediates this issue

See kernel.org mailing list for explanation of these new gnu linker warnings

error replication

from pwn import * p = run_assembly('push 0xbad') [ERROR] There was an error running ['/usr/bin/x86_64-linux-gnu-ld', '--oformat=elf32-i386', '-EL', '-m', 'elf_i386', '-z', 'execstack', '-o', '/tmp/pwn-asm-phbp53vv/step3', '/tmp/pwn-asm-phbp53vv/step2', '--section-start=.shellcode=0x10000000', '--entry=0x10000000', '-z', 'max-page-size=4096', '-z', 'common-page-size=4096']: It had this on stdout: /usr/bin/x86_64-linux-gnu-ld: warning: /tmp/pwn-asm-phbp53vv/step3 has a LOAD segment with RWX permissions

[ERROR] An error occurred while assembling: 1: .section .shellcode,"awx" 2: .global _start 3: .global __start 4: .p2align 2 5: _start: 6: __start: 7: .intel_syntax noprefix 8: push 0xbad Traceback (most recent call last): File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 710, in asm _run(linker + ldflags) File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 404, in _run log.error(msg, *args) File "/usr/local/lib/python3.10/dist-packages/pwnlib/log.py", line 424, in error raise PwnlibException(message % args) pwnlib.exception.PwnlibException: There was an error running ['/usr/bin/x86_64-linux-gnu-ld', '--oformat=elf32-i386', '-EL', '-m', 'elf_i386', '-z', 'execstack', '-o', '/tmp/pwn-asm-phbp53vv/step3', '/tmp/pwn-asm-phbp53vv/step2', '--section-start=.shellcode=0x10000000', '--entry=0x10000000', '-z', 'max-page-size=4096', '-z', 'common-page-size=4096']: It had this on stdout: /usr/bin/x86_64-linux-gnu-ld: warning: /tmp/pwn-asm-phbp53vv/step3 has a LOAD segment with RWX permissions

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.10/dist-packages/pwnlib/context/init.py", line 1577, in setter return function(*a, **kw) File "/usr/local/lib/python3.10/dist-packages/pwnlib/runner.py", line 34, in run_assembly return ELF.from_assembly(assembly).process() File "/usr/local/lib/python3.10/dist-packages/pwnlib/context/init.py", line 1577, in setter return function(*a, **kw) File "/usr/local/lib/python3.10/dist-packages/pwnlib/elf/elf.py", line 402, in from_assembly return ELF(make_elf_from_assembly(assembly, *a, **kw)) File "/usr/local/lib/python3.10/dist-packages/pwnlib/context/init.py", line 1577, in setter return function(*a, **kw) File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 524, in make_elf_from_assembly result = asm(assembly, vma = vma, shared = shared, extract = False, **kwargs) File "/usr/local/lib/python3.10/dist-packages/pwnlib/context/init.py", line 1577, in setter return function(*a, **kw) File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 733, in asm log.exception("An error occurred while assembling:\n%s" % lines) File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 710, in asm _run(linker + ldflags) File "/usr/local/lib/python3.10/dist-packages/pwnlib/asm.py", line 404, in _run log.error(msg, *args) File "/usr/local/lib/python3.10/dist-packages/pwnlib/log.py", line 424, in error raise PwnlibException(message % args) pwnlib.exception.PwnlibException: There was an error running ['/usr/bin/x86_64-linux-gnu-ld', '--oformat=elf32-i386', '-EL', '-m', 'elf_i386', '-z', 'execstack', '-o', '/tmp/pwn-asm-phbp53vv/step3', '/tmp/pwn-asm-phbp53vv/step2', '--section-start=.shellcode=0x10000000', '--entry=0x10000000', '-z', 'max-page-size=4096', '-z', 'common-page-size=4096']: It had this on stdout: /usr/bin/x86_64-linux-gnu-ld: warning: /tmp/pwn-asm-phbp53vv/step3 has a LOAD segment with RWX permissions

tj-oconnor avatar Aug 19 '22 20:08 tj-oconnor

This needs some guard around the linker version, since unkown arguments don't appear to be ignored on older versions.

$ ld --version
GNU ld (GNU Binutils for Ubuntu) 2.34
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
$ ld --no-warn-rwx-segments
ld: unrecognized option '--no-warn-rwx-segments'
ld: use the --help option for usage information
$ ld --no-warn-execstack
ld: unrecognized option '--no-warn-execstack'
ld: use the --help option for usage information

peace-maker avatar Aug 20 '22 09:08 peace-maker

Thank you. Updated to include a guard. Additionally, updated another location where it was necessary to add the new linker options.

tj-oconnor avatar Aug 26 '22 21:08 tj-oconnor

Sadly, it seems that your method of checking ld versioning does not account for all types of output.

re.search(r' (\d+\.\d+.\d+)', result) doesn't pick up 2.39.

The regex does not pickup Kali Linux's and Garuda's output of ld --version:

Here is the example of such output

GNU ld (GNU Binutils for Debian) 2.39
Copyright (C) 2022 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

CmdP1rx avatar Oct 24 '22 20:10 CmdP1rx

I'd suggest changing the regex to (\d+(?:\.\d+)+).

peace-maker avatar Nov 15 '22 13:11 peace-maker

Another option would be to execute ld --no-warn-rwx-segments --help and check the return status.

Diff-fusion avatar Nov 15 '22 14:11 Diff-fusion