From patchwork Mon Mar 30 17:43:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Dasmohapatra X-Patchwork-Id: 38669 Return-Path: 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 EC0E2385E001 for ; Mon, 30 Mar 2020 17:43:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EC0E2385E001 Received: from noise.collabora.co.uk (unknown [IPv6:2a00:5f00:102:0:c0a7:51ff:feaf:e642]) (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 2626F2966DD; Mon, 30 Mar 2020 18:43:57 +0100 (BST) From: =?utf-8?q?Vivek_Das=C2=A0Mohapatra?= To: vivek@etla.org, libc-alpha@sourceware.org Subject: [RFC][PATCH v4 03/15] elf/dl-object.c: Implement a helper function to proxy link_map entries Date: Mon, 30 Mar 2020 18:43:37 +0100 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-Spam-Status: No, score=-27.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, 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-List-Received-Date: Mon, 30 Mar 2020 17:43:59 -0000 Provides the minimal functionality needed to take an existing link_map entry and create a proxy for it in the specified namespace. --- elf/dl-object.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ sysdeps/generic/ldsodefs.h | 5 +++ 2 files changed, 89 insertions(+) diff --git a/elf/dl-object.c b/elf/dl-object.c index d2cdf135cc..2ee5eee13c 100644 --- a/elf/dl-object.c +++ b/elf/dl-object.c @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -50,6 +51,89 @@ _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid) __rtld_lock_unlock_recursive (GL(dl_load_write_lock)); } +/* Proxy an existing link map entry into a new link map: + This is based on _dl_new_object, skipping the steps we know we won't need + because this is mostly just a shell for the l_real pointer holding the real + link map entry (normally l == l->l_real, but not for ld.so in non-main + link maps or RTLD_SHARED proxies). + It also flags the proxy by setting l_proxy, and sets the the no-delete + flag in the original if it is an lt_loaded. */ +struct link_map * +_dl_new_proxy (struct link_map *old, int mode, Lmid_t nsid) +{ + const char *name; + struct link_map *new; + struct libname_list *newname; +#ifdef SHARED + unsigned int na = GLRO(dl_naudit); + + if ((mode & __RTLD_OPENEXEC) != 0) + na = DL_NNS; + + size_t audit_space = na * sizeof (struct auditstate); +#else +# define audit_space 0 +#endif + + name = old->l_name; + + /* Find the original link map entry if `old' is itself a proxy. */ + while (old && old->l_proxy) + old = old->l_real; + + if (old == NULL) + _dl_signal_error (EINVAL, name, NULL, N_("cannot proxy NULL link_map")); + + /* Object already exists in the target namespace. This should get handled + by dl_open_worker but just in case we get this far, handle it: */ + if (__glibc_unlikely (old->l_ns == nsid)) + { + /* Not actually possible, given the sanity checks above. */ + if (old->l_proxy) + return old; + + _dl_signal_error (EEXIST, name, NULL, + N_("existing object cannot be demoted to a proxy")); + } + + /* Now duplicate as little of _dl_new_object as possible to get a + working proxied object in the target link map. */ + new = (struct link_map *) calloc (sizeof (*new) + audit_space + + sizeof (struct link_map *) + + sizeof (*newname) + PATH_MAX, 1); + + if (new == NULL) + _dl_signal_error (ENOMEM, name, NULL, + N_("cannot create shared object descriptor")); + + /* Specific to the proxy. */ + new->l_real = old; + new->l_proxy = 1; + new->l_ns = nsid; + + /* Copied from the origin. */ + new->l_libname = old->l_libname; + new->l_name = old->l_name; + /* Proxies are considered lt_loaded if the real entry type is lt_library. */ + new->l_type = (old->l_type == lt_library) ? lt_loaded : old->l_type; + + if (__glibc_unlikely (mode & RTLD_NODELETE)) + new->l_flags_1 |= DF_1_NODELETE; + + /* Specific to the origin. Ideally we'd do some accounting here but + for now it's easier to pin the original so the proxy remains valid. */ + if (old->l_type == lt_loaded) + old->l_flags_1 |= DF_1_NODELETE; + + /* Fix up the searchlist so that relocations work. */ + _dl_map_object_deps (new, NULL, 0, 0, + mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT)); + + /* And finally put the proxy in the target namespace. */ + _dl_add_to_namespace_list (new, nsid); + + return new; +} /* Allocate a `struct link_map' for a new object being loaded, and enter it into the _dl_loaded list. */ diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 497938ffa2..d3113ff4d8 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -945,6 +945,11 @@ extern lookup_t _dl_lookup_symbol_x (const char *undef, struct link_map *skip_map) attribute_hidden; +/* Proxy an existing link map entry into a new link map */ +extern struct link_map *_dl_new_proxy (struct link_map *old, + int mode, + Lmid_t nsid) + attribute_hidden; /* Add the new link_map NEW to the end of the namespace list. */ extern void _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid)