From patchwork Tue Feb 9 17:18:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Dasmohapatra X-Patchwork-Id: 41997 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 C1A7E398C013; Tue, 9 Feb 2021 17:18:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1A7E398C013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1612891138; bh=ou+Aiq65rIRvARoZUV+LJanP2DPLOr0C02M2AKbFo5Y=; 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=NsxRVF8UPJXkzUxFXMxIpj3q9Dj7BVqwEHH4Sun5+wEYoK05JXzkK/YLtA7eRnZTY mZY07ZdqpbsRpMz15zd64FGPirM5cXkS7Ip4dyAg/E2el3vWBoqLJyStMOLi49MsEq hdUjBU6X3fBvreo6pi+cOnaGEAetcYXAmGLX09t0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by sourceware.org (Postfix) with ESMTPS id 6FA54396AC26 for ; Tue, 9 Feb 2021 17:18:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6FA54396AC26 Received: from noise.lan (unknown [IPv6:2001:4d48:ad5c:ef00:8e70:5aff:fe59:c29c]) (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 96CF31F451E4 for ; Tue, 9 Feb 2021 17:18:46 +0000 (GMT) To: libc-alpha@sourceware.org Subject: [RFC][PATCH v8 14/20] When loading DSOs into alternate namespaces check for DT_GNU_UNIQUE Date: Tue, 9 Feb 2021 17:18:33 +0000 Message-Id: <20210209171839.7911-15-vivek@collabora.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210209171839.7911-1-vivek@collabora.com> References: <20210209171839.7911-1-vivek@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 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 | 96 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 82 insertions(+), 14 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 13ac2053b8..13879af82c 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -837,6 +837,62 @@ _dl_init_paths (const char *llp, const char *source, __rtld_env_path_list.dirs = (void *) -1; } +static ElfW(Word) +_has_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_FLAGS_1: + unique = entry.d_un.d_val & DF_GNU_1_UNIQUE; + 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; +} /* Process PT_GNU_PROPERTY program header PH in module L after PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0 @@ -1098,6 +1154,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 lose_errno; + } + } + + /* We need to check for DT_GNU_FLAGS_1/DF_GNU_1_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_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)) @@ -1115,20 +1197,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 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. */