|
| 1 | +// This is a bash-static initializer for Conty |
| 2 | + |
| 3 | +#define _GNU_SOURCE |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | +#include <sys/mman.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +// Replace all 0 below before compilation |
| 10 | + |
| 11 | +// The size of our statically compiled bash binary |
| 12 | +#define BASH_SIZE 0 |
| 13 | + |
| 14 | +// The size of conty-start.sh script |
| 15 | +// It can be bigger than the actual size of the script |
| 16 | +#define SCRIPT_SIZE 0 |
| 17 | + |
| 18 | +// The size of this program itself after compilation |
| 19 | +// It can be bigger than the actual size of the program |
| 20 | +#define PROGRAM_SIZE 0 |
| 21 | + |
| 22 | +// Bubblewrap can handle up to 9000 arguments |
| 23 | +// And we reserve 1000 for internal use in Conty |
| 24 | +#define MAX_ARGS_NUMBER 8000 |
| 25 | + |
| 26 | +int main(int argc, char* argv[]) |
| 27 | +{ |
| 28 | + if (argc > MAX_ARGS_NUMBER) { |
| 29 | + printf("Too many arguments"); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + char program_path[8192] = { 0 }; |
| 34 | + int binary_code[BASH_SIZE + 1]; |
| 35 | + char bash_script[SCRIPT_SIZE + 1]; |
| 36 | + |
| 37 | + readlink("/proc/self/exe", program_path, sizeof program_path); |
| 38 | + FILE *current_program = fopen(program_path, "rb"); |
| 39 | + int bash_binary = memfd_create("bash-static", 0); |
| 40 | + |
| 41 | + fseek(current_program, PROGRAM_SIZE, 0); |
| 42 | + fread(binary_code, BASH_SIZE, 1, current_program); |
| 43 | + write(bash_binary, binary_code, BASH_SIZE); |
| 44 | + |
| 45 | + fseek(current_program, PROGRAM_SIZE + BASH_SIZE, 0); |
| 46 | + fread(bash_script, SCRIPT_SIZE, 1, current_program); |
| 47 | + fclose(current_program); |
| 48 | + |
| 49 | + char * bash_args[MAX_ARGS_NUMBER + 5] = {program_path, "-c", "--", bash_script, argv[0]}; |
| 50 | + |
| 51 | + int k = 5; |
| 52 | + for (int i = 1; i < argc; i++, k++) { |
| 53 | + bash_args[k] = argv[i]; |
| 54 | + } |
| 55 | + bash_args[k] = NULL; |
| 56 | + |
| 57 | + fexecve(bash_binary, bash_args, environ); |
| 58 | + printf("Failed to execute builtin bash-static"); |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
0 commit comments