public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Stas Sergeev via Libc-alpha <libc-alpha@sourceware.org>
Cc: Stas Sergeev <stsp2@yandex.ru>
Subject: Re: [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767]
Date: Tue, 14 Feb 2023 10:51:30 +0100	[thread overview]
Message-ID: <87mt5ga82l.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <20230214084123.2056067-3-stsp2@yandex.ru> (Stas Sergeev via Libc-alpha's message of "Tue, 14 Feb 2023 13:41:23 +0500")

* Stas Sergeev via Libc-alpha:

> This patch adds the following function:
> void *dlmem(const unsigned char *buffer, size_t size, int flags);
>
> It is the same as dlopen() but allows to dynamic-link solibs from
> the memory buffer, rather than from a file as dlopen() does.
>
> "buffer" arg is the pointer to the solib image in memory.
> "size" is the solib image size. Must be smaller-or-equal to the
> actual buffer size.
> "flags" is the same flags argument used in dlopen().
>
> The idea behind the implementation is very simple: where the
> dlopen() would mmap() the file, dlmem() does anonymous
> mmap()+memcpy().

With the mandatory copy, I'm not sure if this is a substantial
improvement over the pedestrian implementation using memfd_create,
included below.  It should work with restrictive SELinux modes, for now
at least, while MAP_ANONYMOUS will most definitely fail.  (Currently,
memfd_create is an un-auditable trapdoor to the land of self-modifying
code because the underlying pseudo-file-system cannot be mounted noexec
by the system administrator.)

What's the debugger story with your dlmem variant?  It looks like GDB
gets confused even with the memfd_create mapping, which really isn't
great.

Thanks,
Florian

/* Copyright (C) 2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

void *
dlmem (const void *base, size_t length, int flags)
{
  /* Last used descriptor, to produce unique paths under
     /proc/PID/fd.  Avoid clashes with the standard file descriptors.
     A real implementation would need locking.  */
  static int last_fd = 2;

  int fd = memfd_create ("dlmem", MFD_CLOEXEC);
  if (fd < 0)
    return NULL;
  if (fd <= last_fd)
    {
      int new_fd = fcntl (fd, F_DUPFD_CLOEXEC, last_fd + 1);
      if (new_fd < 0)
        {
          close (fd);
          return NULL;
        }
      close (fd);
      fd = new_fd;
    }

  /* Write the ELF image to the memfd file.  */
  if (write (fd, base, length) != length)
    {
      close (fd);
      return NULL;
    }

  /* Construct the path for dlopen.  Do not use /proc/self to help
     GDB.  (Otherwise GDB would try to open its own descriptors.)  */
  char *path;
  if (asprintf (&path, "/proc/%ld/fd/%d", (long int) getpid (), fd) < 0)
    {
      close (fd);
      return NULL;
    }

  void *handle = dlopen (path, flags);

  free (path);
  close (fd);

  if (handle != NULL)
    last_fd = fd;
  return handle;
}

#include <err.h>
#include <sys/stat.h>

int
main (int argc, char **argv)
{
  if (argc != 2)
    {
      fprintf (stderr, "usage: %s PATH\n", argv[0]);
      return 1;
    }
  int fd = open (argv[1], O_RDONLY);
  if (fd < 0)
    err (1, "open");
  struct stat st;
  if (fstat (fd, &st) != 0)
    err (1, "stat");
  char *buf = malloc (st.st_size);
  if (buf == NULL)
    err (1, "malloc");
  if (read (fd, buf, st.st_size) != st.st_size)
    err (1, "read");
  close (fd);
  void *handle = dlmem (buf, st.st_size, RTLD_NOW);
  free (buf);
  if (handle == NULL)
    errx (1, "dlmem");
  double (*sin) (double) = dlsym (handle, "sin");
  printf ("%f\n", sin (1.5707963267948966));
  dlclose (handle);
}


  reply	other threads:[~2023-02-14  9:51 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-14  8:41 [PATCH v4 0/2] implement dlmem() function Stas Sergeev
2023-02-14  8:41 ` [PATCH 1/2] elf: strdup() l_name if no realname [BZ #30100] Stas Sergeev
2023-02-14  8:41 ` [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767] Stas Sergeev
2023-02-14  9:51   ` Florian Weimer [this message]
2023-02-14 13:13     ` stsp
2023-02-15 11:30     ` stsp
2023-03-18 16:58     ` stsp
  -- strict thread matches above, loose matches on Subject: below --
2023-02-15 11:21 [PATCH v5 0/2] implement dlmem() with audit extension Stas Sergeev
2023-02-15 11:21 ` [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767] Stas Sergeev
2023-02-13 13:23 [PATCH v3 0/2] implement dlmem() function Stas Sergeev
2023-02-13 13:23 ` [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767] Stas Sergeev
2023-02-13 13:45   ` Florian Weimer
2023-02-13 16:36     ` stsp
2023-02-14  8:43     ` stsp
2023-03-18 17:28     ` stsp
2023-02-10 14:07 [PATCH 1/2] elf: strdup() l_name if no realname [BZ #30100] Stas Sergeev
2023-02-10 14:07 ` [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767] Stas Sergeev
2023-02-10 21:51   ` Joseph Myers
2023-02-11 20:10     ` stsp
2023-02-13 21:46       ` Joseph Myers
2023-02-14  8:42         ` stsp

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=87mt5ga82l.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=stsp2@yandex.ru \
    /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).