From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8B5C13858D1E for ; Tue, 14 Feb 2023 09:51:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B5C13858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676368297; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AgBWQExpXZd3WVjFmWXCEKNQR3wXTIqsJjz8OalDryk=; b=QZ1W+QY7MG4mhR3Qwl91Z/o/PMShayz8+J/no9+O/ndyM4H10iA2ErS7AbeKuRx1PzlA7R 46wAt4V/VqEEg9kt0DhIWZLoDk1FTuLYiq/0Khn0NN50zgAaw5ow1N2Kym//YceIYB2zHx me5RVyunjGkB1fPBETgOGGjvYPRIUI4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-379-5QXMb--kNneig0QStTiNJg-1; Tue, 14 Feb 2023 04:51:33 -0500 X-MC-Unique: 5QXMb--kNneig0QStTiNJg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9088C18E0921; Tue, 14 Feb 2023 09:51:33 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A0471492B03; Tue, 14 Feb 2023 09:51:32 +0000 (UTC) From: Florian Weimer To: Stas Sergeev via Libc-alpha Cc: Stas Sergeev Subject: Re: [PATCH 2/2] dlfcn,elf: implement dlmem() function [BZ #11767] References: <20230214084123.2056067-1-stsp2@yandex.ru> <20230214084123.2056067-3-stsp2@yandex.ru> Date: Tue, 14 Feb 2023 10:51:30 +0100 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") Message-ID: <87mt5ga82l.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * 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 . */ #define _GNU_SOURCE #include #include #include #include #include #include #include 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 #include 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); }