public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "fweimer at redhat dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug dynamic-link/30424] New: Calling dlopen from preinit function inhibits proper libc initialization
Date: Fri, 05 May 2023 19:59:20 +0000	[thread overview]
Message-ID: <bug-30424-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=30424

            Bug ID: 30424
           Summary: Calling dlopen from preinit function inhibits proper
                    libc initialization
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: dynamic-link
          Assignee: unassigned at sourceware dot org
          Reporter: fweimer at redhat dot com
  Target Milestone: ---

Reproducer:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <gnu/lib-names.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
preinit (int argc, char **argv, char **envp)
{
  printf ("environ: %p\n", environ);
  printf ("USER: %s\n", getenv ("USER"));
  if (argc != 1)
    dlopen (LIBC_SO, RTLD_NOW);
}

static void (*const preinit_array []) (int, char **, char **)
     __attribute__ ((section (".preinit_array"), aligned (sizeof (void *)),
                     used)) =
{
  &preinit,
};

int
main (void)
{
  printf ("environ: %p\n", environ);
  printf ("USER: %s\n", getenv ("USER"));
}

Running it without any arguments:

environ: (nil)
USER: (null)
environ: 0x7ffe9b2dcc88
USER: fweimer

This is sort-of expected because at preinit time, libc is not fully initialized
yet because the ELF constructors have not run.

Running with an argument triggers the dlopen:

environ: (nil)
USER: (null)
environ: (nil)
USER: (null)

This is unexpected.  The reason is that _dl_open (in ld.so) does not use the
original data from the kernel, but receives the values to use from dlopen (in
libc.so). But as libc.so is not fully initialized at preinit time, it cannot
initialize itself. The fact that ELF constructors have run is recorded, so the
full initialization is never performed.

I think we should keep passing down at least environ from libc.so to ld.so
because environ can change legitimately. Nowadays, we have __libc_early_init,
we can use it to get rid of ELF constructors and initialize libc.so fully
before application code runs. This should resolve the issue because
__libc_early_init runs before the preinit functions.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2023-05-05 19:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-05 19:59 fweimer at redhat dot com [this message]
2023-05-05 20:03 ` [Bug dynamic-link/30424] " asn at samba dot org
2023-11-02  7:46 ` asn at samba dot org
2023-11-02 10:13 ` sam at gentoo dot org

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=bug-30424-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).