[PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767]

Florian Weimer fweimer@redhat.com
Tue Feb 14 09:51:30 GMT 2023


* 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);
}



More information about the Libc-alpha mailing list