mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Андрей Аладьев" <aladjev.andrew@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [musl] Static linking is broken after creation of DT_TEXTREL segment
Date: Wed, 29 Jan 2020 23:08:46 +0300	[thread overview]
Message-ID: <CAMw0sz+ONDY=44tyE+wosEzgtjGc-z0nK3RcymRN8yCUn73GRg@mail.gmail.com> (raw)
In-Reply-To: <20200129191946.GI2020@voyager>

[-- Attachment #1: Type: text/plain, Size: 3671 bytes --]

> Ooh boy, why would you do this? When there's a perfectly good -lgmp just
waiting for you.

Usage of "/usr/lib/libgmp.a" directly is not forbidden by any toolchain or
build system. Moreover it is recommended by cmake. You can google for
"target_link_libraries( site:github.com" and found millions of software
that uses something like:

find_library(EXTERNAL_LIB)
target_link_libraries(something ${EXTERNAL_LIB_FOUND})

> The warning is justified, you usually do not want to do this. With a
TEXTREL, the code has to be mapped as writable, so now programming errors
and exploits can change the executable code.

This is warning from ld, not from musl. Segfault is not an acceptable
message from libc. Libc should not try to write into readonly pointer.

> Well, the remedy is obvious: Get rid of the TEXTREL.

Yes, I've found a workaround: "USE='-asm' emerge -v1 gmp", assembly is
broken, will report it to gmp upstream. But this is not a fix for the issue.

> Iterate over the apps PHDRs and remove write protection from all RO
segments?

So libc knows that file is mapped as readonly and should not try to write
into readonly pointers. Libc can do the following:

1. Ignore impossible relocations.
2. Add a warning to stderr and still ignore impossible relocations.
3. do abort, user will receive SIGABRT and understand that he uses libc in
a wrong way.

Segfault is not an acceptable answer.

Thank you.

ср, 29 янв. 2020 г. в 22:20, Markus Wichmann <nullplan@gmx.net>:

> On Wed, Jan 29, 2020 at 09:41:46PM +0300, Андрей Аладьев wrote:
> > gcc main.c /usr/lib/libgmp.a -o main && ./main
>
> Ooh boy, why would you do this? When there's a perfectly good -lgmp just
> waiting for you.
>
> > warning: creating a DT_TEXTREL in object
>
> The warning is justified, you usually do not want to do this. With a
> TEXTREL, the code has to be mapped as writable, so now programming
> errors and exploits can change the executable code.
>
> > We can see that "laddr" provided pointer to "dso->base + rel[0]", than
> > switch tried to override it's value and segfault appeared. Pointer is
> > wrong, most likely readonly.
> >
>
> Pointer is correct, but memory should have been mapped writable. More on
> this in a bit.
>
> > You can replace "gcc" with "clang" and everything will be the same.
> Updated
> > binutils to most recent version 2.33.1 and rebuilt toolchain - nothing
> > changed. Pointer is still invalid and SEGV_ACCERR appears.
> >
>
> Of course. The issue is a relocation inside a read-only segment.
> Binutils and compiler are not malfunctioning.
>
> > So I think that bug is inside musl itself. Glibc container is the same
> > situation works fine. I see no way to create a workaround for this issue.
>
> Well, the remedy is obvious: Get rid of the TEXTREL. It is usually
> caused by bad assembly source code, and fixable with only minor
> knowledge of the details. Consult the search engine of your least
> distrust for more information on this.
>
> Regarding the actual problem though, the problem here is that DT_TEXTREL
> is handled only in map_library(), so it is not handled for kernel mapped
> DSOs. Which isn't a problem for libc or the VDSO, but the app itself
> might be TEXTREL (and is, in this case), and that isn't handled. How,
> though? Iterate over the apps PHDRs and remove write protection from all
> RO segments?
>
> And one more question: Since musl resolves all relocations immediately,
> couldn't we write-protect TEXTREL modules after relocating them?
> Provided no unresolved relocations remain, of course.
>
> Ciao,
> Markus
>

[-- Attachment #2: Type: text/html, Size: 4516 bytes --]

  parent reply	other threads:[~2020-01-29 20:09 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-29 18:41 Андрей Аладьев
2020-01-29 19:19 ` Markus Wichmann
2020-01-29 19:38   ` Markus Wichmann
2020-01-29 20:48     ` Rich Felker
2020-01-29 20:08   ` Андрей Аладьев [this message]
2020-01-30 17:02     ` Markus Wichmann
2020-01-31  4:24       ` Rich Felker
2020-01-31 14:47         ` Markus Wichmann
2020-01-31 16:35           ` Rich Felker
2020-01-31 15:16       ` Андрей Аладьев
2020-01-31 16:40         ` Rich Felker
2020-01-31 17:51           ` Андрей Аладьев
2020-01-31 18:01             ` Rich Felker
2020-01-31 19:11               ` Андрей Аладьев
2020-02-03  3:10       ` Rich Felker
2020-02-03  4:05         ` Rich Felker
2020-02-03  4:32         ` Markus Wichmann
2020-02-03  4:40           ` Rich Felker
2020-01-29 20:53 ` Rich Felker
2020-01-29 21:10   ` Szabolcs Nagy
2020-01-29 21:35     ` Андрей Аладьев
2020-01-29 21:46       ` Rich Felker
2020-01-29 23:10         ` Андрей Аладьев
2020-01-29 23:20       ` Szabolcs Nagy
2020-01-29 21:14   ` Андрей Аладьев
2020-01-29 21:43     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMw0sz+ONDY=44tyE+wosEzgtjGc-z0nK3RcymRN8yCUn73GRg@mail.gmail.com' \
    --to=aladjev.andrew@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).