sandboxed-api/oss-internship-2020/libarchive/ld_preload_example/README.md
Copybara-Service fbf3e84799 Merge pull request #65 from andreimedar:libarchive
PiperOrigin-RevId: 343290002
Change-Id: I1f29e4acfc7d423be63fd52e7a78ceb209d29115
2020-11-19 07:53:41 -08:00

2.0 KiB

Sandboxing the minitar example (only the extraction function) using LD_PRELOAD

Description

Using LD_PRELOAD we can create our own functions with the same signatures as the ones from the libarchive that are called in the original code and call those instead. This example only implements the extract part of the original tool.

Structure

  • minitar_main.cc - the original main function.
  • minitar.cc and minitar.h - original functions called from main, built into a shared library.
  • The three files mentioned above are taken from the original minitar example. The only difference to the code is that the main function was separated from the rest in order to build a shared library so that when LD_PRELOAD is used we can call our custom functions.
  • sapi_minitar.cc - libarchive functions with our own implementation, built into a shared library which will be used with LD_PRELOAD.

Most of the functions simply convert the arguments to sapi::v objects and calls the sandboxed version of the function. Also, there is a custom extract implementation that also created the sandbox object. If the sandbox could be created without special arguments then this would not be needed and could be done inside of the first function called. However, in our case, the sandbox requires the file path and so we do this in the create function where we have access to that path. Aftert this, we call the original function (the "next" function symbol).

Files changed from the original libarchive sandboxed version

The only changes are in the root CMakeLists.txt:

  • add_subdirectory(ld_preload_example) to add this current folder to the build process.

  • set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) to make sure that shared libraries are built with position independent code.

Usage

LD_PRELOAD= build/ld_preload_example/libminitar_preload.so build/ld_preload_example/minitar_original -xvf archive_file

Instead of the x option, t can be used as well only to print the archive entries.