1*ec63e07aSXin Li# Sandboxing PFFFT library 2*ec63e07aSXin Li 3*ec63e07aSXin LiThis library was sandboxed as part of Google's summer 2020 internship program 4*ec63e07aSXin Li([blog post](https://security.googleblog.com/2020/12/improving-open-source-security-during.html)). 5*ec63e07aSXin Li 6*ec63e07aSXin LiBuild System: CMake 7*ec63e07aSXin LiOS: Linux 8*ec63e07aSXin Li 9*ec63e07aSXin Li### How to use from an existing Project 10*ec63e07aSXin Li 11*ec63e07aSXin LiIf your project does not include Sandboxed API as a dependency yet, add the 12*ec63e07aSXin Lifollowing lines to the main `CMakeLists.txt`: 13*ec63e07aSXin Li 14*ec63e07aSXin Li```cmake 15*ec63e07aSXin Liinclude(FetchContent) 16*ec63e07aSXin Li 17*ec63e07aSXin LiFetchContent_Declare(sandboxed-api 18*ec63e07aSXin Li GIT_REPOSITORY https://github.com/google/sandboxed-api 19*ec63e07aSXin Li GIT_TAG main # Or pin a specific commit/tag 20*ec63e07aSXin Li) 21*ec63e07aSXin LiFetchContent_MakeAvailable(sandboxed-api) # CMake 3.14 or higher 22*ec63e07aSXin Li 23*ec63e07aSXin Liadd_sapi_subdirectory(contrib/pffft) 24*ec63e07aSXin Li``` 25*ec63e07aSXin Li 26*ec63e07aSXin LiThe `add_sapi_subdirectory()` macro sets up the source and binary directories 27*ec63e07aSXin Lifor the sandboxed jsonnet targets. 28*ec63e07aSXin Li 29*ec63e07aSXin LiAfterwards your project's code can link to `sapi_contrib::pffft` and use the 30*ec63e07aSXin Ligenerated header `pffft_sapi.sapi.h`. An example sandbox policy can be found 31*ec63e07aSXin Liin `main_pffft_sandboxed.cc`. 32*ec63e07aSXin Li 33*ec63e07aSXin Li### For testing: 34*ec63e07aSXin Li`cd build`, then `./pffft_sandboxed` 35*ec63e07aSXin Li 36*ec63e07aSXin Li### For debug: 37*ec63e07aSXin Lidisplay custom info with 38*ec63e07aSXin Li`./pffft_sandboxed --logtostderr` 39*ec63e07aSXin Li 40*ec63e07aSXin Li## ***About the project*** 41*ec63e07aSXin Li 42*ec63e07aSXin LiPFFFT library is concerned with 1D Fast-Fourier Transformations finding a 43*ec63e07aSXin Licompromise between accuracy and speed. It deals with real and complex 44*ec63e07aSXin Livectors, both cases being illustrated in the testing part (`test_pffft.c` 45*ec63e07aSXin Lifor initially and original version, `main_pffft_sandboxed.cc` for our 46*ec63e07aSXin Licurrently implemented sandboxed version). 47*ec63e07aSXin LiThe original files can be found at: https://bitbucket.org/jpommier/pffft/src.* 48*ec63e07aSXin Li 49*ec63e07aSXin LiThe purpose of sandboxing is to limit the permissions and capabilities of 50*ec63e07aSXin Lilibrary’s methods, in order to secure the usage of them. 51*ec63e07aSXin LiAfter obtaining the sandbox, the functions will be called through an 52*ec63e07aSXin LiSandbox API (being called `api` in the current test) and so, the 53*ec63e07aSXin Lioperations, system calls or namspaces access may be controlled. 54*ec63e07aSXin LiFrom both `pffft.h` and `fftpack.h` headers, useful methods are added to 55*ec63e07aSXin Lisapi library builded with CMake. There is also a need to link math library 56*ec63e07aSXin Lias the transformations made require mathematical operators. 57*ec63e07aSXin LiRegarding the testing of the methods, one main is doing this job by 58*ec63e07aSXin Liiterating through a set of values, that represents the accuracy of 59*ec63e07aSXin Litransformations and print the speed for each value and type of 60*ec63e07aSXin Litransformation. More specifically, the input length is the target for 61*ec63e07aSXin Liaccuracy (named as `n`) and it stands for the number of data points from 62*ec63e07aSXin Lithe series that calculate the result of transformation. It is also 63*ec63e07aSXin Liimportant to mention that the `complex` variable stands for a boolean value 64*ec63e07aSXin Lithat tells the type of transformation (0 for REAL and 1 for COMPLEX) and 65*ec63e07aSXin Liit is taken into account while testing. 66*ec63e07aSXin LiIn the end, the performance of PFFFT library it is outlined by the output. 67*ec63e07aSXin LiThere are two output formats available, from which you can choose through 68*ec63e07aSXin Li`--output_format=` command-line flag. 69*ec63e07aSXin LiWithout using this type of argument when running, the output format is set 70*ec63e07aSXin Liby default.* 71*ec63e07aSXin Li 72*ec63e07aSXin Li#### CMake observations resume: 73*ec63e07aSXin Li 74*ec63e07aSXin Li* linking pffft and fftpack (which contains necessary functions for pffft) 75*ec63e07aSXin Li* set math library 76*ec63e07aSXin Li 77*ec63e07aSXin Li#### Sandboxed main observations resume: 78*ec63e07aSXin Li 79*ec63e07aSXin Li* containing two testing parts (fft / pffft benchmarks) 80*ec63e07aSXin Li* showing the performance of the transformations implies 81*ec63e07aSXin Li testing them through various FFT dimenstions. 82*ec63e07aSXin Li Variable n, the input length, will take specific values 83*ec63e07aSXin Li meaning the number of points to which it is set the calculus 84*ec63e07aSXin Li (more details of mathematical purpose of n - https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm). 85*ec63e07aSXin Li* output shows speed depending on the input length 86*ec63e07aSXin Li* use `--output_format=0` or `--output_format=1` arguments to choose between output formats. 87*ec63e07aSXin Li `0` is for a detailed output, while `1` is only displaying each transformation process speed. 88*ec63e07aSXin Li 89*ec63e07aSXin Li### Bugs history 90*ec63e07aSXin Li1. [Solved] pffft benchmark bug: "Sandbox not active" 91*ec63e07aSXin Li 92*ec63e07aSXin Li n = 64, status OK, `pffft_transform` generates error 93*ec63e07aSXin Li n > 64, status not OK 94*ec63e07aSXin Li Problem on initialising `absl::StatusOr<PFFFT_Setup *> s;` the memory that stays 95*ec63e07aSXin Li for s is not the same with the address passed in `pffft_transform` function. 96*ec63e07aSXin Li (`sapi::v::GenericPtr` - to be changed) 97*ec63e07aSXin Li 98*ec63e07aSXin Li Temporary solution: change the generated files to accept 99*ec63e07aSXin Li `uintptr_t` instead of `PFFFT_Setup` 100*ec63e07aSXin Li 101*ec63e07aSXin Li Solution: using `sapi::v::RemotePtr` instead of `sapi::v::GenericPtr` 102*ec63e07aSXin Li to access the memory of object `s` 103*ec63e07aSXin Li 104*ec63e07aSXin Li2. [Unresolved] compiling bug: "No space left on device" 105*ec63e07aSXin Li 106*ec63e07aSXin Li The building process creates some `embed` files that use lots of 107*ec63e07aSXin Li memory, trying to write them on `/tmp`. 108*ec63e07aSXin Li 109*ec63e07aSXin Li Temporary solution: clean /tmp directory by `sudo rm -rf /tmp/*` 110