From owner-freebsd-hackers@freebsd.org Tue Jul 2 03:15:27 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 79F0015C2929 for ; Tue, 2 Jul 2019 03:15:27 +0000 (UTC) (envelope-from shawn.webb@hardenedbsd.org) Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id AE0656B531 for ; Tue, 2 Jul 2019 03:15:25 +0000 (UTC) (envelope-from shawn.webb@hardenedbsd.org) Received: by mail-qt1-x843.google.com with SMTP id p15so16996371qtl.3 for ; Mon, 01 Jul 2019 20:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hardenedbsd.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2oQuLhMB5aG8GALFsa86DKTSCVUBnDqCoLDdFCTWMf0=; b=R+7955zUVUBUHLAKOuoseK9NLg9pI8pzK5v/PoP8/3lbplT0cequNiz/UDr6hmLcnm jjIskCE4jpph8joeh2aE89jJVaQEalTdUYNZ0K+TMF4PWOrY0gYhMBlPgy1mjVJouoUe m9r53IBg6lo7848ApV5Ym0MXaxl3rJ7CnVYbAl0VE2MdXrrw5DX/bePfNhwWVagTqxzS DDGk14kZQAUdiBVpPLeoNDC8LL4SH8M7xq74jPcPSc5Oau/UdmsEmqBDqCIF2V44CeGh VJs9Uu2WPesAmO9VJC0IVWr2tnudpwrrnmkznFey3njftFuZ/SqFft+X2EcWmK78HC8z 4iEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2oQuLhMB5aG8GALFsa86DKTSCVUBnDqCoLDdFCTWMf0=; b=acKH+Z2aOTHgvUrad62FnB5gfqx3fvLhUBR246RLqkfQ05yle3YCC5D7C4utxCA5Au QZybKq+tBSCmg83eN+cGCnZLNLzyCCs9YnjwAyOifjgljuVrerJCkGCBDIimesLHITT9 t3CJVkGH9gL84smoCdS9pYylCe4WsrX2TaNBXdbpUf2iqKABIEeyHgved8HgGPvuBE4A CK9KsGl7w7rO/Wa+0ueQI7l80K0DNwOkQ9Xl7qrVV+ZKLjDmwJtRY3xx9sNXSQHfICU8 PiNW0iBOvaZaBpYw/Hr6+7FsH5O+PSJvjblGAp6Qwz31gaR8jn0RVFSQndoGlaZs3Tmi x54g== X-Gm-Message-State: APjAAAWkUdWrvmYEfIWa7Q6c9zAd2rEkVxmDPZNEkCfChpaJKxJK9NVE FKQwbJjw4QP18+XH9zJGe+l+U+5lwyg= X-Google-Smtp-Source: APXvYqw2HXDo8e0+1fRE1+Fknmyd4sOkTLuDmMiwqE5hPBegRtevdHQxzdpejYk9kKM9SdlysxnJ2w== X-Received: by 2002:ac8:5458:: with SMTP id d24mr22787976qtq.329.1562037324870; Mon, 01 Jul 2019 20:15:24 -0700 (PDT) Received: from mutt-hbsd ([151.196.118.239]) by smtp.gmail.com with ESMTPSA id a23sm5501797qtp.22.2019.07.01.20.15.23 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 01 Jul 2019 20:15:23 -0700 (PDT) Date: Mon, 1 Jul 2019 23:15:23 -0400 From: Shawn Webb To: Yuri Cc: Freebsd hackers list Subject: Re: Question: Is there a way to get the list of all dynamic libraries needed by a given elf through API? Message-ID: <20190702031523.lhlrqnlo7pxccl42@mutt-hbsd> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="3nmsf3huqkgjbmlp" Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD mutt-hbsd 13.0-CURRENT-HBSD FreeBSD 13.0-CURRENT-HBSD X-PGP-Key: http://pgp.mit.edu/pks/lookup?op=vindex&search=0xFF2E67A277F8E1FA User-Agent: NeoMutt/20180716 X-Rspamd-Queue-Id: AE0656B531 X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=hardenedbsd.org header.s=google header.b=R+7955zU; spf=pass (mx1.freebsd.org: domain of shawn.webb@hardenedbsd.org designates 2607:f8b0:4864:20::843 as permitted sender) smtp.mailfrom=shawn.webb@hardenedbsd.org X-Spamd-Result: default: False [-4.73 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[hardenedbsd.org:+]; RCPT_COUNT_TWO(0.00)[2]; MX_GOOD(-0.01)[alt1.aspmx.l.google.com,aspmx.l.google.com,aspmx2.googlemail.com,alt2.aspmx.l.google.com,aspmx3.googlemail.com]; SIGNED_PGP(-2.00)[]; NEURAL_HAM_SHORT(-0.85)[-0.852,0]; RECEIVED_SPAMHAUS_PBL(0.00)[239.118.196.151.zen.spamhaus.org : 127.0.0.10]; IP_SCORE(-0.77)[ip: (1.73), ipnet: 2607:f8b0::/32(-3.14), asn: 15169(-2.35), country: US(-0.06)]; SUBJECT_ENDS_QUESTION(1.00)[]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; MIME_TRACE(0.00)[0:+,1:+]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[hardenedbsd.org:s=google]; FROM_HAS_DN(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.20)[multipart/signed,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; DMARC_NA(0.00)[hardenedbsd.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[3.4.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; RCVD_TLS_LAST(0.00)[]; MID_RHS_NOT_FQDN(0.50)[] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Jul 2019 03:15:27 -0000 --3nmsf3huqkgjbmlp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jul 01, 2019 at 07:54:27PM -0700, Yuri wrote: > I want to get the same information that 'ldd ' returns, but fro= m a > C/C++ program without launching external processes. >=20 > It is possible to find the list of elf's shared library dependencies using > libelf. But I couldn't find how to map library names to full library path= s. >=20 >=20 > ldd(1) sets a certain environment variable, forks and calls execl(3), and > the "ld.so magic" does the rest. >=20 >=20 > Obviously, if one needs to do the same from their own C program, forking = is > a much less attractive option compared to the API way. >=20 >=20 > Is there an API to find such mappings? If no, why doesn't it exist? Hey Yuri, If you're not afraid to get your hands, feet, and pet rock named Steve dirty with the one true language of love, C, and you can link against libraries in base, take a look at libelf. If you want to use a scripting language, ldd is still the right tool for the job. Taking a look at the manpage for ldd(1), which will eventually point us to rtld(1), which will teach us that we can end up with something like: $ ldd -f '%p\n' /bin/ls /lib/libutil.so.9 /lib/libncursesw.so.8 /lib/libthr.so.3 /lib/librt.so.1 /lib/libm.so.5 /lib/libexecinfo.so.1 /lib/libc.so.7 (That is verbatim on my HardenedBSD dev laptop. The multitude of libraries is due to SafeStack and CFI.) So, what you're looking for is technically documented, but not in a straightforward manner. Thanks, --=20 Shawn Webb Cofounder / Security Engineer HardenedBSD Tor-ified Signal: +1 443-546-8752 Tor+XMPP+OTR: lattera@is.a.hacker.sx GPG Key ID: 0xFF2E67A277F8E1FA GPG Key Fingerprint: D206 BB45 15E0 9C49 0CF9 3633 C85B 0AF8 AB23 0FB2 --3nmsf3huqkgjbmlp Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEA6TL67gupaZ9nzhT/y5nonf44foFAl0azEUACgkQ/y5nonf4 4frJjA/+PiYNUyqaO4gfU78N1mO5R5vo4Y0oJ4WXu1+FMFC/ntWmuGStjJlM2iP8 1wJfzJV2bU+wEVGnpk07XkV19VPrVHDKlLc4gIqeiOrh6PMIQTMWiKo/JTUey4V+ sk+MRgCijgo7c0rvqpv1taW1TFSQQhuXK/UlhkAe9IQLxP/koJZTNI6QOi0aVsAt ZgaHSbOMgGRTbvhnu2cSHLuUkd1k2MHgNbHAty0Sqbw8RYI25KgQhKFC/NE6Srm7 c0z4U7HEiG+lgsaKUYxniRAjfwPdINr0dW8Auh1KdzRMjnDZKjl5+qiNOSzed8NN h7L6oYF5paadnHE25SUcy+nkwuapsHhM7lw2uW//le0JPdtebSdoZ2cGd/dskcFN y5gMX0Rr2mcs5Tc1fOTMAjGaLd0nHrMHRiL3p2PmuxKROc839vuExtqbAk+qaGaJ crlolDMv4khpcnE3sB4pM3DQ5yFcFvI2OvxRzGOcvFfAAf13fiD/X9qmLB3uEjKK JzaIF7SvOaxA54sm3WbeBo+imIESyga29HxRIAbtgb5V+RzCSHeH+4lBHCQ4FJos 1Xy/olJFt8L+Fb6XSMp4RutQ5xJhkmbB370wpeZfHKW1njY79Fc5soOmfpIgLktF 9/OySB6xg4eM755w11u8wai7I0MIdSJFy6i2iuXSr7ku+cKC7xE= =oA5P -----END PGP SIGNATURE----- --3nmsf3huqkgjbmlp--