LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-04-2012, 08:01 AM   #1
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Rep: Reputation: Disabled
ldd functionality in C++


There is given a problem:
I have to write a c++ program what is able to list its own dependencies (like ldd does). Moreover the binary of this c++ program should have suid flag.
I tried the following:
setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);
execl(binary, binary, (char *)NULL);

It works for binaries without suid flag. With suid flag I got exit status 5.
After tracing the functionality of the original ldd I figured out the following:
LD_TRACE_LOADED_OBJECTS=1 /lib64/ld-linux-x86-64.so.2 binary_name
But I'm not sure how to implement it in c++ and neither it will work from c++ too?

Any idea?
 
Old 07-04-2012, 01:20 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
Hi.

Code:
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	if(argc < 2) return 1;

	char ld_so[] = "/lib64/ld-linux-x86-64.so.2";
	setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);
	execl(ld_so, ld_so, argv[1], (char *)NULL);
	return 0;
}
ld-linux-x86-64.so.2 is an executable:
Code:
$ /lib64/ld-linux-x86-64.so.2
Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
                        object we can handle
  --library-path PATH   use given PATH instead of content of the environment
                        variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
                        in LIST
  --audit LIST          use objects named in LIST as auditors
 
1 members found this post helpful.
Old 07-05-2012, 03:14 AM   #3
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks.
 
Old 07-11-2012, 08:30 AM   #4
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi there again,

I missed a very important part. It should work for itself if it has suid flag :S But this solution gives:

exit status 5

error message. Is there any way to fix this?
 
Old 07-11-2012, 08:47 AM   #5
littleplane
LQ Newbie
 
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
To be more accurate: I got the same error message if the calling binary - with suid flag - tried to ldd another binaries. So the emphasis isn't on that we want to ldd ourself but that we are suid.
 
Old 07-12-2012, 08:18 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
For SUID applications some of the LD_* variables are ignored for safety reasons. Maybe this is an error because of this.

You can have a look at the source of readelf to get the information:
Code:
$ readelf -d demo

Dynamic section at offset 0xf18 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libfoobar.so.1]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
...
Libraries which are needed by the listed libraries you have to resolve on your own though.
 
  


Reply

Tags
c++, ldd, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LDD mistery vadiml Linux - General 2 07-17-2009 04:46 AM
Ubuntu 5.10 -> 6.06: diversion of /usr/bin/ldd to /usr/bin/ldd.amd64 by ia32-libs HellSpawn Linux - Software 2 06-04-2006 09:18 PM
about ldd -d jingle_mj Programming 4 09-10-2004 06:25 AM
about ldd -d jingle_mj Linux - General 2 08-25-2004 06:30 AM
ldd simon Linux - General 1 08-02-2001 06:15 AM


All times are GMT -5. The time now is 04:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration