Untar and load to LD_PRELOAD

191 views Asked by At

assume that I have a tar.gz archive that contains 1 shared library. My intention, is to untar it "on-the-fly" and the .so (that is extracted), put it on LD_PRELOAD and the run my code.

So, I made a script:

#!/bin/bash

myTarLib=$1
tar -zxf $myTarLib --to-command "export LD_PRELOAD="

./run_the_func

The execution of the run_the_exec didn't use the .so from the tar. I have the impression that the "--to-command" option creates another shell; is it correct?

Do you have any suggestion on how I could do it? The important part, is that i don't want to have the .so on the disk.

Thanks in advance!

1

There are 1 answers

3
Vangelis On

I found a solution to the problem... The use of memfd_create

The memfd_create creates a file descriptor. Then this can be used to store any data in it. The manpage is here.

In order to use it, you need to create a C-Wrapper that takes care of the untar (in my case). The code is:

#include <linux/memfd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main()
{
    int fd = memfd_create("my_test", MFD_CLOEXEC);
    if (fd == -1)
    {
        fprintf(stderr, "Creation failed\n");
    }


    char command_1[128];
    char *path="hiddenLibrary/libmy_func_real.tgz";
    //feel free to modify it to the path of your encrypted library
    sprintf(command_1, "tar -zxf %s --to-stdout > /proc/%ld/fd/%d", path, (long) getpid(), fd);

    printf("Running decrypt command\n");
    system(command_1);

    printf("The untar-ed library is located at:/proc/%ld/fd/%d\nOnce you finished type a number and hit enter\n",(long) getpid(), fd);
    float temp;
    scanf("%f", &temp);

return 0;
}

Now the idea is that the C code above, will run the untar and will store the result to the fd. Once you have finished using it, you simply hit a number and the C code exits. During the exit, all the fds are released, so the untar-ed library is "gone".