LD_PRELOAD non è troppo difficile e non è necessario essere root. Interponi la tua routine C che viene chiamata al posto del vero open()
nella libreria C. La tua routine controlla se il file da aprire è "/tmp/adb.log" e chiama il vero open con un nome file diverso. Ecco il tuo shim_open.c:
/*
* capture calls to a routine and replace with your code
* gcc -Wall -O2 -fpic -shared -ldl -o shim_open.so shim_open.c
* LD_PRELOAD=/.../shim_open.so cat /tmp/adb.log
*/
#define _FCNTL_H 1 /* hack for open() prototype */
#define _GNU_SOURCE /* needed to get RTLD_NEXT defined in dlfcn.h */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#define OLDNAME "/tmp/adb.log"
#define NEWNAME "/tmp/myadb.log"
int open(const char *pathname, int flags, mode_t mode){
static int (*real_open)(const char *pathname, int flags, mode_t mode) = NULL;
if (!real_open) {
real_open = dlsym(RTLD_NEXT, "open");
char *error = dlerror();
if (error != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}
}
if (strcmp(pathname,OLDNAME)==0) pathname = NEWNAME;
fprintf(stderr, "opening: %s\n", pathname);
return real_open(pathname, flags, mode);
}
Compilalo con gcc -Wall -O2 -fpic -shared -ldl -o shim_open.so shim_open.c
e testalo inserendo qualcosa in /tmp/myadb.log
e in esecuzioneLD_PRELOAD=/.../shim_open.so cat /tmp/adb.log
. Quindi prova LD_PRELOAD su adb.
Ecco un esempio molto semplice di utilizzo di util-linux
è unshare
per mettere un processo in uno spazio dei nomi di montaggio privato e dargli una visione diversa dello stesso filesystem che ha attualmente il suo genitore:
{ cd /tmp #usually a safe place for this stuff
echo hey >file #some
echo there >file2 #evidence
sudo unshare -m sh -c ' #unshare requires root by default
mount -B file2 file #bind mount there over hey
cat file #show it
kill -TSTP "$$" #suspend root shell and switch back to parent
umount file #unbind there
cat file' #show it
cat file #root shell just suspended
fg #bring it back
cat file2 #round it off
}
there #root shell
hey #root shell suspended
hey #root shell restored
there #rounded
Puoi dare a un processo una vista privata del suo filesystem con il unshare
utility sui sistemi Linux aggiornati, sebbene la stessa funzione di mount namespace sia stata abbastanza matura per l'intera serie di kernel 3.x. Puoi inserire spazi dei nomi preesistenti di tutti i tipi con nsenter
utility dallo stesso pacchetto, e puoi saperne di più con man
.