|
| 1 | +#include <errno.h> |
| 2 | +// lind_syscall.h provides functions needed for cage interactions: register_handler, copy_data_between_cage, and make_threei_call |
| 3 | +#include <lind_syscall.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <sys/types.h> |
| 7 | +#include <sys/wait.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +// Dispatcher function |
| 11 | +// |
| 12 | +// Entry point into a grate when a child cage invokes a registered |
| 13 | +// syscall. This function is used to invoke the appropriate handler, |
| 14 | +// and value returned is the passed down to the calling cage. |
| 15 | +// |
| 16 | +// Args: |
| 17 | +// fn_ptr_uint Address of the registered syscall handler within the |
| 18 | +// grate's address space. |
| 19 | +// cageid Identifier of the calling cage. |
| 20 | +// arg[1-6] Syscall arguments. Numeric types are passed by value, pointers |
| 21 | +// are passed as addresses in the originating cage's address space. |
| 22 | +// arg[1-6]cage Cage IDs corresponding to each argument, indicating which cage's address |
| 23 | +// space the argument belongs to. |
| 24 | +int pass_fptr_to_wt(uint64_t fn_ptr_uint, uint64_t cageid, uint64_t arg1, |
| 25 | + uint64_t arg1cage, uint64_t arg2, uint64_t arg2cage, |
| 26 | + uint64_t arg3, uint64_t arg3cage, uint64_t arg4, |
| 27 | + uint64_t arg4cage, uint64_t arg5, uint64_t arg5cage, |
| 28 | + uint64_t arg6, uint64_t arg6cage) { |
| 29 | + if (fn_ptr_uint == 0) { |
| 30 | + fprintf(stderr, "[Grate|geteuid] Invalid function ptr\n"); |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + printf("[Grate|geteuid] Handling function ptr: %llu from cage: %llu\n", |
| 35 | + fn_ptr_uint, cageid); |
| 36 | + |
| 37 | + int (*fn)(uint64_t) = (int (*)(uint64_t))(uintptr_t)fn_ptr_uint; |
| 38 | + |
| 39 | + return fn(cageid); |
| 40 | +} |
| 41 | + |
| 42 | +// Function ptr and signatures of this grate |
| 43 | +int geteuid_grate(uint64_t); |
| 44 | + |
| 45 | +int geteuid_grate(uint64_t cageid) { |
| 46 | + printf("[Grate|geteuid] In geteuid_grate %d handler for cage: %llu\n", |
| 47 | + getpid(), cageid); |
| 48 | + return 10; |
| 49 | +} |
| 50 | + |
| 51 | +// Main function will always be same in all grates |
| 52 | +int main(int argc, char *argv[]) { |
| 53 | + // Should be at least two inputs (at least one grate file and one cage file) |
| 54 | + if (argc < 2) { |
| 55 | + fprintf(stderr, "Usage: %s <cage_file>\n", argv[0]); |
| 56 | + exit(EXIT_FAILURE); |
| 57 | + } |
| 58 | + |
| 59 | + int grateid = getpid(); |
| 60 | + |
| 61 | + // Because we assume that all cages are unaware of the existence of grate, |
| 62 | + // cages will not handle the logic of `exec`ing grate. |
| 63 | + // Instead, a grate instance is responsible for mananging this. |
| 64 | + // |
| 65 | + // It forks and execs exactly once: To execute the child binary provided |
| 66 | + // as argv[1], passing argv[1..] as that program's command-line arguments. |
| 67 | + // Any further process management is handled by this executed program, not |
| 68 | + // by the original grate. |
| 69 | + |
| 70 | + pid_t pid = fork(); |
| 71 | + if (pid < 0) { |
| 72 | + perror("fork failed"); |
| 73 | + exit(EXIT_FAILURE); |
| 74 | + } else if (pid == 0) { |
| 75 | + int cageid = getpid(); |
| 76 | + // Set the geteuid (syscallnum=107) of this cage to call this grate |
| 77 | + // function geteuid_grate (func index=0) |
| 78 | + // Syntax of register_handler: |
| 79 | + // register_handler( |
| 80 | + // int64_t targetcage, - Cage ID to be intercepted |
| 81 | + // uint64_t targetcallnum, - Syscall number to be intercepted |
| 82 | + // uint64_t handlefunc_flag, - 0 for deregister non-0 for register |
| 83 | + // uint64_t this_grate_id, - Grate ID to redirect call to |
| 84 | + // uint64_t optional_arg - Handler function pointer if registering |
| 85 | + // ) |
| 86 | + uint64_t fn_ptr_addr = (uint64_t)(uintptr_t)&geteuid_grate; |
| 87 | + printf("[Grate|geteuid] Registering geteuid handler for cage %d in " |
| 88 | + "grate %d with fn ptr addr: %llu\n", |
| 89 | + cageid, grateid, fn_ptr_addr); |
| 90 | + int ret = register_handler(cageid, 107, 1, grateid, fn_ptr_addr); |
| 91 | + |
| 92 | + if (execv(argv[1], &argv[1]) == -1) { |
| 93 | + perror("execv failed"); |
| 94 | + exit(EXIT_FAILURE); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + int status; |
| 99 | + while (wait(&status) > 0) { |
| 100 | + printf("[Grate|geteuid] terminated, status: %d\n", status); |
| 101 | + } |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments