mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Farid Zakaria <fmzakari@ucsc.edu>
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Getting access to section data during dynlink.c
Date: Mon, 16 Oct 2023 14:09:06 -0700	[thread overview]
Message-ID: <CAH4OOv7trtsu5tnV03Z0Ya0VXDBaLbR9iFwVpiOMKXUtp8QjYQ@mail.gmail.com> (raw)
In-Reply-To: <20231016142603.GL4163@brightrain.aerifal.cx>

Thanks for the advice.

I'm now looking at the following two options (seeing if they work):
1. I see that an auxiliary vector is the FD or the name of the file
given to dynlink.
Can I mmap this file again in it's entirety and read the section I care about?

2. Try to create a segment that maps to the sections I care about such
that they are loaded.
(Try to apply some heuristic to then identify it)

Early attempts at (1) have the mmap failing -- i need to debug further.


On Mon, Oct 16, 2023 at 7:25 AM Rich Felker <dalias@libc.org> wrote:
>
> On Sun, Oct 15, 2023 at 06:06:48PM -0700, Farid Zakaria wrote:
> > Hi!
> >
> > I'd like to read some section data during dynlink.c
> > Does anyone have any good suggestions on the best way to do so?
> > I believe most ELF files ask for the load to start from the start of the
> > ELF file.
> >
> > I see in dynlink.c the kernel sends AT_PHDR as an auxiliary vector --
> > Should I try applying a fixed offset from it to get to the start of the
> > ehdr ?
> >
> > Any advice is appreciated.
> >
> > Please include me in the CC for the reply.
> > I can't recall if I've subscribed.
>
> Neither the Ehdrs nor sections are "loadable" parts of an executable
> ELF file. They may happen to be present in the mapped pages due to
> page granularity of mappings, but that doesn't mean they're guaranteed
> to be there; the Ehdrs are for the program loader's use, and the
> sections are for the use of linker (non-dynamic), debugger, etc.
>
> In musl we use Ehdrs in a couple places: the dynamic linker finds its
> own program headers via assuming they're mapped, but this is rather
> reasonable since we built it and it's either going to always-succeed
> or always-fail and get caught before deployment if that build-time
> assumption somehow isn't met. It's not contingent on properties of a
> program encountered at runtime. We also use Ehdrs when loading a
> program (invoking ldso as a command) or shared library, but in that
> case we are the loaded and have access to them via the file being
> loaded.
>
> Depending on what you want to do, and whether you just need to be
> compatible with your own binaries or arbitrary ones, it may suffice to
> do some sort of hack like rounding down from the program header
> address to the start of the page and hoping the Ehdrs live there. But
> it might make sense to look for other ways to do what you're trying to
> do, without needing to access non-runtime data structures.
>
> Rich

  reply	other threads:[~2023-10-16 21:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-16  1:06 Farid Zakaria
2023-10-16 14:26 ` Rich Felker
2023-10-16 21:09   ` Farid Zakaria [this message]
2023-10-16 21:16     ` Farid Zakaria
2023-10-16 21:53   ` Szabolcs Nagy
2023-10-16 22:04     ` Rich Felker
2023-10-17  3:39       ` Farid Zakaria
2023-10-17  8:28       ` Szabolcs Nagy
2023-10-17 12:24         ` Rich Felker
2023-10-17 17:37         ` Farid Zakaria

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=CAH4OOv7trtsu5tnV03Z0Ya0VXDBaLbR9iFwVpiOMKXUtp8QjYQ@mail.gmail.com \
    --to=fmzakari@ucsc.edu \
    --cc=dalias@libc.org \
    --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).