From patchwork Wed Jun 17 14:00:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Dasmohapatra X-Patchwork-Id: 39661 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 44DA7389852D; Wed, 17 Jun 2020 14:00:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44DA7389852D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592402447; bh=YMuZkZsjZXAgRtNoBZ1gSz1RGNXb7lxObiCtDsujhdg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=KDcfHSssj/FKfx/QzG29zMJlKvj0LLTWKrbsX4mMZLMKHlS7lEgPu9w2njWti659a 3S++8b/0VzBAruw9d4dPcM9abloxlM1XwL9TykYE/FdsTyi0UdQ1xZ7c3tYqi+CSO3 0TLqDi/oJOw2xVt+B3JMosAEOHLNPtyZcvXZVOeY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by sourceware.org (Postfix) with ESMTPS id A04373893669 for ; Wed, 17 Jun 2020 14:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A04373893669 Received: from noise.collabora.co.uk (unknown [IPv6:2001:4d48:ad56:3000:88ff:22ff:81f3:30c8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: vivek) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 96A892A3FC7 for ; Wed, 17 Jun 2020 15:00:35 +0100 (BST) To: libc-alpha@sourceware.org Subject: [RFC][PATCH v5 12/16] When loading DSOs into alternate namespaces check for DT_GNU_UNIQUE Date: Wed, 17 Jun 2020 15:00:20 +0100 Message-Id: <20200617140024.12777-13-vivek@collabora.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200617140024.12777-1-vivek@collabora.com> References: <20200617140024.12777-1-vivek@collabora.com> X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Vivek_Das=C2=A0Mohapatra_via_Libc-alpha?= From: Vivek Dasmohapatra Reply-To: =?utf-8?q?Vivek_Das=C2=A0Mohapatra?= Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" If a DSO has not already been loaded and the target is not the main namespace then we must check to see if it's been DT_GNU_UNIQUE tagged and load it into the main namespace instead. dl_open_worker has alread been modified to notice the discrepancy between the request and the result in such cases, and will set up a proxy in the target namespace. --- elf/dl-load.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 66848f8434..e0eced6dd2 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -827,6 +827,62 @@ _dl_init_paths (const char *llp) env_path_list.dirs = (void *) -1; } +static ElfW(Word) +_has_dt_gnu_unique (int fd, const ElfW(Ehdr) *header, const ElfW(Phdr) *phdr) +{ + int unique = 0; + const ElfW(Phdr) *ph; + ElfW(Dyn) entry = {}; + off_t reset; + off_t pos; + off_t end; + + reset = __lseek (fd, 0, SEEK_CUR); + + for (ph = phdr; ph < &phdr[header->e_phnum]; ++ph) + { + switch (ph->p_type) + { + case PT_DYNAMIC: + pos = __lseek (fd, ph->p_offset, SEEK_SET); + end = pos + ph->p_filesz; + + while (pos < end) + { + ssize_t rb = 0; + do + { + ssize_t rretl = __read_nocancel (fd, &entry + rb, + sizeof (ElfW(Dyn)) - rb); + if (rretl <= 0) + goto cleanup; + + rb += rretl; + } + while (__glibc_unlikely (rb < sizeof (ElfW(Dyn)))); + + switch (entry.d_tag) + { + case DT_GNU_UNIQUE: + unique = entry.d_un.d_val; + case DT_NULL: + goto cleanup; + break; + default: + break; + } + pos += rb; + } + break; + } + } + + cleanup: + /* Put the file descriptor offset back where it was when we were called. */ + __lseek (fd, reset, SEEK_SET); + + return unique; +} static void __attribute__ ((noreturn, noinline)) @@ -941,7 +997,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, } #endif - /* DSOs in the main namespace which are flagged DF_1_UNIQUE should only + /* DSOs in the main namespace which are flagged DT_GNU_UNIQUE should only be opened into the main namespace. Other namespaces should only get proxies. */ if (__glibc_unlikely (nsid != LM_ID_BASE)) @@ -1019,6 +1075,32 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, else assert (r->r_state == RT_ADD); + /* Load the ELF header using preallocated struct space if it's big enough. */ + maplength = header->e_phnum * sizeof (ElfW(Phdr)); + if (header->e_phoff + maplength <= (size_t) fbp->len) + phdr = (void *) (fbp->buf + header->e_phoff); + else + { + phdr = alloca (maplength); + if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, + header->e_phoff) != maplength) + { + errstring = N_("cannot read file data"); + goto call_lose_errno; + } + } + + /* We need to check for DT_GNU_UNIQUE before we start + initialising any namespace dependent metatada. */ + if (nsid != LM_ID_BASE) + { + /* Target DSO is flagged as unique: Make sure it gets loaded into + the base namespace. It is up to our caller to generate a proxy in + the target nsid. */ + if (_has_dt_gnu_unique (fd, header, phdr)) + nsid = LM_ID_BASE; + } + /* Enter the new object in the list of loaded objects. */ l = _dl_new_object (realname, name, l_type, loader, mode, nsid); if (__glibc_unlikely (l == NULL)) @@ -1036,20 +1118,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, type = header->e_type; l->l_phnum = header->e_phnum; - maplength = header->e_phnum * sizeof (ElfW(Phdr)); - if (header->e_phoff + maplength <= (size_t) fbp->len) - phdr = (void *) (fbp->buf + header->e_phoff); - else - { - phdr = alloca (maplength); - if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, - header->e_phoff) != maplength) - { - errstring = N_("cannot read file data"); - goto call_lose_errno; - } - } - /* On most platforms presume that PT_GNU_STACK is absent and the stack is * executable. Other platforms default to a nonexecutable stack and don't * need PT_GNU_STACK to do so. */