Skip to contentSkip to conversation

LD_PRELOAD an anonymous file?

DISCUSSION [email protected] 1 participant 2 messages Aug 20, 2002 – Sep 13, 2002 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

hi,

Q: - is there any way to do the same thing as ``LD_PRELOAD=file'', but using only a nameless file?

that is - I have a program like this:

  | /*launcher.c:*/
  | #include <unistd.h>
  | #include <stdlib.h>
  | #include <stdio.h>
  | main()
  | {
  |   int fd;
  |   char *av[] = { "file", "/etc/motd", 0, };
  |   putenv("LD_PRELOAD=./intercept.so"); /* I may want to unlink() this name */
  |   execv("/usr/bin/file", av); /* launch some target program to be influenced */
  |   perror("execv");
  | }
  | /*intercept.c*/
  | #include <dlfcn.h>
  | #include <stdio.h>
  | int open(const char *p, int mod)
  | {
  |   int fd;
  |   void *h = dlopen("/lib/libc.so.6", RTLD_LAZY);
  |   int (*f)() = dlsym(h, "open");
  |   dlclose(h);
  |   printf("open: %s\n", p);
  |   fflush(stdout);
  |   fd = (*f)(p, mod);
  |   return fd;
  | }
  | /*makefile*/
  | intercept.so: intercept.c
  | 	gcc -c intercept.c
  | 	ld -G -shared -o intercept.so intercept.o -l dl
  | 
  | launcher: launcher.c
  | 	gcc -o launcher launcher.c

this works fine. and calls to open() does go into the stub defined in intercept.c

now what I want to do now is:

1) within the "laucher" program, dynamically generate equivalent data(=program) that was supposed to go into file intercept.so .

2) keep this generated data to be (somewhat) invisible even in the face of program crash. (I might be generating (somewhat) sensitive data for this).

3) to do 2), I'm thinking about either
A) once store the data into file under the name "intercept.so" anyway, but immediately unlink(2) this, retaining only the filedescriptor to refer to the file. (but if the file no longer has a name how can I interact with the dynamic linker?)
B) otherwise if there's some private && transient (more or less) name space in some file system eg., /proc/self/... (?) this may be equally okay compared to A).
C) by any chance - is there any funky way of doing this without using filesystem at all? (eg., use only VM,... or use some dynamic linker interface I was not aware of)

regards,
kenji

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

hi - thanks much!

Quoted 3 lines

A (very brief) test suggested that the links in /proc/<parent pid>/fd should still work. e.g. if file descriptor 3 points to what used to be intercept.so, so will /proc/<parent pid>/fd/3.

brilliant! I've tried myself -- works fine :) (my program actually does exec() without doing fork(), so /proc/self/fd/<fd> was just fine. also:
/dev/fd/<fd> worked equally okay on all of solaris2, linux, netbsd :) )

Quoted 2 lines

In principle, it could probably be done with ptrace() and the rest of the debugging API, but it wouldn't be much fun.

hmm., never thought about that direction - I'll think about it.

thanks again!!
kenji :)