From patchwork Wed Jun 17 14:00:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Vivek Dasmohapatra X-Patchwork-Id: 39655 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 367343898516; Wed, 17 Jun 2020 14:00:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 367343898516 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592402443; bh=0GWChrmy8Tc5uEHX01NljeLdTXcPyXXMWpYnsnfjHxA=; 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=DK44+nx9Av5KJklOtAcFysF23AF4FOUdn1ydH7A3ZVf/kANAx9Qe/n2EiY4BYtj3M FsD/vP4NiBtEUapNpqqvy/x2es+7Frmv8UcyKcp6piNaOYFcOBjTm5o0ypwVjiQVM3 RAMrxFddcMSjVCIJdpwiGX4gGk9hFxlBDbAoWlHk= 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 7C80F3893668 for ; Wed, 17 Jun 2020 14:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7C80F3893668 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 6473B2A3FC4 for ; Wed, 17 Jun 2020 15:00:35 +0100 (BST) To: libc-alpha@sourceware.org Subject: [RFC][PATCH v5 09/16] Compare loaded DSOs by file ID and check for DF_1_UNIQUE Date: Wed, 17 Jun 2020 15:00:17 +0100 Message-Id: <20200617140024.12777-10-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> MIME-Version: 1.0 X-Spam-Status: No, score=-14.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 _dl_map_object_from_fd finds that a DSO it was asked to load into a non-base namespace is already loaded (into the main namespace) and is flagged DF_1_UNIQUE then it should return that DSO's link map entry. In such cases _dl_open_worker must notice that this has happened and dontinue down the link map proxy generation path instead of normal link map entry preparation. --- elf/dl-load.c | 26 ++++++++++++++++++++++++++ elf/dl-open.c | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/elf/dl-load.c b/elf/dl-load.c index d4d6a6318b..76970f79de 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -941,6 +941,32 @@ _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 + be opened into the main namespace. Other namespaces should only get + proxies. */ + if (__glibc_unlikely (nsid != LM_ID_BASE)) + { + /* Check base ns to see if the name matched another already loaded. */ + for (l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL; l = l->l_next) + if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id)) + { + if (!(l->l_flags_1 & DF_1_UNIQUE)) + continue; + + /* Already loaded. Bump its reference count and return it. */ + __close_nocancel (fd); + + /* If the name is not listed for this object add it. */ + free (realname); + add_name_to_object (l, name); + + /* NOTE: It is important that our caller picks up on the fact + that we have NOT returned an object in the requested namespace + and handles the proxying correctly */ + return l; + } + } + if (mode & RTLD_NOLOAD) { /* We are not supposed to load the object unless it is already diff --git a/elf/dl-open.c b/elf/dl-open.c index aca0384e5b..75dc3893d3 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -548,6 +548,16 @@ dl_open_worker (void *a) return; } + /* If we were trying to load a DF_1_UNIQUE flagged DSO which was NOT + ALREADY LOADED (or not loaded with the name we are using) then + _dl_map_object will return an instance from the main namespace. + We need to detect this and set up the RTLD_SHARED flags. */ + if (__glibc_unlikely(args->nsid != LM_ID_BASE && new->l_ns == LM_ID_BASE)) + { + want_proxy = RTLD_SHARED; + mode |= RTLD_SHARED; + } + /* If we want proxy and we get this far then the entry in ‘new’ will be in the main namespace: we should revert to the main namespace code path(s), but remember the namespace we want the proxy to be in. */