From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 2504F38515F6 for ; Fri, 23 Jul 2021 21:00:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2504F38515F6 Received: by mail-pj1-x102a.google.com with SMTP id a9so3908749pjw.5 for ; Fri, 23 Jul 2021 14:00:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=BK8bdyfqjD4vNjdfO9BBYh50c7GlqnXmPxgF8poTUeU=; b=q5SyD2WooYnlOU5N8g9zDEKE6F2md7XjjTIPz+t6OW6/DP044Ad8kM7PfkU4zHogOU 6fC9z7iXLCO/+u4pP2dgaRiLQ8bPl2xPMg4eLomGzCUUsxKr/onXb/0dzNkf6pLACk2j wTXA7p3TUvFTeM/fmh57hzZqNpfTvf7k205yKwXV7y4VRMBUsJQBpwTqifunuypWo95I F6LSgBUx8+dnO0QregFdIH3qDuUAbhdFzbP6bFA/qJbMb39TSksQ7hwA9LUJY0KeriVR TQFcMmmSOhXAuil4pYK9+EpujIJc2Bv6lD69Q9DjpSo7mDHW8EVe9Hgz+1asMjl5UhSI Ye1A== X-Gm-Message-State: AOAM533wXWVBk3/JfV2EMBNakbSuWZQtQfURtoeiehjXnr3xWCyNOeCn HUu3YF1vSoxHnyBrVaCwCVlWZtY876xFsw== X-Google-Smtp-Source: ABdhPJzngR3Uli0jGKnL8m1wikt7cZ/fsFEOzdrcOY0KAnMSZBmyNfZsIHSD1j4cg+pCnD6kBdHo3A== X-Received: by 2002:a63:fc02:: with SMTP id j2mr6608584pgi.235.1627074040933; Fri, 23 Jul 2021 14:00:40 -0700 (PDT) Received: from ?IPv6:2804:431:c7ca:1133:2a2b:ddea:41fe:ab82? ([2804:431:c7ca:1133:2a2b:ddea:41fe:ab82]) by smtp.gmail.com with ESMTPSA id c4sm28278455pfo.45.2021.07.23.14.00.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Jul 2021 14:00:40 -0700 (PDT) Subject: Re: Enable preloading in dlopen-ed shared libraries? To: Fengkai Sun , Libc-help References: From: Adhemerval Zanella Message-ID: <81d3637d-8ea4-4048-98de-584e813ddeaf@linaro.org> Date: Fri, 23 Jul 2021 18:00:38 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Jul 2021 21:00:43 -0000 On 14/07/2021 05:44, Fengkai Sun via Libc-help wrote: > Hi list, > > As I dive a little deeper into the source code, I found that > `_dl_map_object_deps' is called in both rtld.c and dl-open.c. > > The semantics of two invocations are both clear: rtld.c makes a call like > this: > _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0); > because ld.so needs to preload the libraries specified by LD_PRELOAD into > the global scope for interposing the symbols. > > dl-open.c makes a call like this: > _dl_map_object_deps (new, NULL, 0, 0, > mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT)); > because no dlopen-ed library needs to preload anything. > > However, I think it might be useful to allow users to preload some > libraries in the local scope after the map of the dlopen-ed library, just > like how ld.so treats preloaded libs in global scope. Do you mean by preloading the shared library list using dlmopen in a new namespace? Or do you mean to use RTLD_DEEPBIND with the preload libraries? > > By doing so, the user can easily provide a different definition of a symbol > from the one of the main executable, by enabling RTLD_DEEPBIND. > This is useful under some circumstances. For example, a dlopen-ed library > may want to use a separate heap from the main heap, and the user can > provide another malloc implementation for that library. But how is this different than the malloc() interposition already supported with LD_PRELOAD? > > The auditing interface can do the similar thing, but after doing some > experiments, I found that `la_symbind64' cannot catch the bindings of > global variables, and it cannot hook all of the function bindings. The rtld-audit currently only works for symbols which requires a PLT call, the global variables either done with GOT access directly or through copy relocations. I am working on extending la_symbind() to work with bind-now binaries, so it would be called at loading time in symbol resolution instead on the lazy binding resolution. > > Would it be a good idea to add an interface to enable preloading in the > local scope of dlopen-ed shared libraries? I am trying to understand better what you are trying to do here, because you are mixing two different usercases here. The RTLD_DEEPBIND is usually used for shared libraries to use its direct dependencies over the global list, the rtld-audit interfaces are loaded in a different namespace. It means that symbol interposition for heap functions you described does not fit with the interfaces: LD_PRELOAD are already taking precedence over the global scope and using a difference namespace meaning a different scope.