|
| 1 | +#include <errno.h> |
| 2 | +#include <lind_syscall.h> |
| 3 | + |
| 4 | +#include <assert.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <sys/types.h> |
| 8 | +#include <sys/wait.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +// Dispatcher function |
| 12 | +int pass_fptr_to_wt(uint64_t fn_ptr_uint, uint64_t cageid, uint64_t arg1, |
| 13 | + uint64_t arg1cage, uint64_t arg2, uint64_t arg2cage, |
| 14 | + uint64_t arg3, uint64_t arg3cage, uint64_t arg4, |
| 15 | + uint64_t arg4cage, uint64_t arg5, uint64_t arg5cage, |
| 16 | + uint64_t arg6, uint64_t arg6cage) { |
| 17 | + if (fn_ptr_uint == 0) { |
| 18 | + fprintf(stderr, |
| 19 | + "[Grate|interpose-exec] Invalid function ptr\n"); |
| 20 | + assert(0); |
| 21 | + } |
| 22 | + |
| 23 | + printf("[Grate|interpose-exec] Handling function ptr: %llu from cage: " |
| 24 | + "%llu\n", |
| 25 | + fn_ptr_uint, cageid); |
| 26 | + |
| 27 | + int (*fn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, |
| 28 | + uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, |
| 29 | + uint64_t) = |
| 30 | + (int (*)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, |
| 31 | + uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, |
| 32 | + uint64_t))(uintptr_t)fn_ptr_uint; |
| 33 | + |
| 34 | + return fn(cageid, arg1, arg1cage, arg2, arg2cage, arg3, arg3cage, arg4, |
| 35 | + arg4cage, arg5, arg5cage, arg6, arg6cage); |
| 36 | +} |
| 37 | + |
| 38 | +int read_grate(uint64_t grateid, uint64_t arg1, uint64_t arg1cage, |
| 39 | + uint64_t arg2, uint64_t arg2cage, uint64_t arg3, |
| 40 | + uint64_t arg3cage, uint64_t arg4, uint64_t arg4cage, |
| 41 | + uint64_t arg5, uint64_t arg5cage, uint64_t arg6, |
| 42 | + uint64_t arg6cage) { |
| 43 | + int thiscage = getpid(); |
| 44 | + int cageid = arg1cage; |
| 45 | + |
| 46 | + int fd = (int)arg1; |
| 47 | + int count = (size_t)arg3; |
| 48 | + |
| 49 | + ssize_t ret = 4321; |
| 50 | + |
| 51 | + char buf[11] = "helloworld"; |
| 52 | + |
| 53 | + copy_data_between_cages(thiscage, arg2cage, (uint64_t)buf, thiscage, |
| 54 | + arg2, arg2cage, count, |
| 55 | + 0 // Use copytype 0 so read exactly count |
| 56 | + // bytes instead of stopping at '\0' |
| 57 | + ); |
| 58 | + |
| 59 | + return ret; |
| 60 | +} |
| 61 | + |
| 62 | +int open_grate(uint64_t cageid, uint64_t arg1, uint64_t arg1cage, uint64_t arg2, |
| 63 | + uint64_t arg2cage, uint64_t arg3, uint64_t arg3cage, |
| 64 | + uint64_t arg4, uint64_t arg4cage, uint64_t arg5, |
| 65 | + uint64_t arg5cage, uint64_t arg6, uint64_t arg6cage) { |
| 66 | + printf( |
| 67 | + "[Grate|interpose-exec] In exec_grate %d handler for cage: %llu\n", |
| 68 | + getpid(), cageid); |
| 69 | + |
| 70 | + int self_grate_id = getpid(); |
| 71 | + |
| 72 | + // Overwrite the path supplied to open with a different path. |
| 73 | + char new_path[20] = "/tmp/redirected.txt"; |
| 74 | + |
| 75 | + int ret = make_threei_call( |
| 76 | + 2, 0, self_grate_id, arg1cage, |
| 77 | + // We need to modify the cageid here to indicate that we want the |
| 78 | + // address translated. |
| 79 | + (uint64_t)&new_path, self_grate_id | (1ULL << 63), arg2, arg2cage, |
| 80 | + arg3, arg3cage, arg4, arg4cage, arg5, arg5cage, arg6, arg6cage, |
| 81 | + 0 // we will handle the errno in this grate instead of translating |
| 82 | + // it to |
| 83 | + ); |
| 84 | + |
| 85 | + return ret; |
| 86 | +} |
| 87 | + |
| 88 | +// Main function will always be same in all grates |
| 89 | +int main(int argc, char *argv[]) { |
| 90 | + // Should be at least one input (at least one grate file and one cage |
| 91 | + // file) |
| 92 | + if (argc < 2) { |
| 93 | + fprintf(stderr, "Usage: %s <cage_file> <grate_file>\n", |
| 94 | + argv[0]); |
| 95 | + assert(0); |
| 96 | + } |
| 97 | + |
| 98 | + int grateid = getpid(); |
| 99 | + |
| 100 | + pid_t pid = fork(); |
| 101 | + if (pid < 0) { |
| 102 | + perror("fork failed"); |
| 103 | + assert(0); |
| 104 | + } else if (pid == 0) { |
| 105 | + int cageid = getpid(); |
| 106 | + |
| 107 | + // This is to test whether we can use arg, argcage from |
| 108 | + // different cages. |
| 109 | + uint64_t fn_ptr_addr = (uint64_t)(uintptr_t)&open_grate; |
| 110 | + int ret = register_handler(cageid, 2, grateid, fn_ptr_addr); |
| 111 | + |
| 112 | + // This is to check copy_data for regression. |
| 113 | + fn_ptr_addr = (uint64_t)(uintptr_t)&read_grate; |
| 114 | + ret = register_handler(cageid, 0, grateid, fn_ptr_addr); |
| 115 | + |
| 116 | + if (execv(argv[1], &argv[1]) == -1) { |
| 117 | + perror("execv failed"); |
| 118 | + assert(0); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + int status; |
| 123 | + int failed = 0; |
| 124 | + while (wait(&status) > 0) { |
| 125 | + if (status != 0) { |
| 126 | + fprintf(stderr, |
| 127 | + "[Grate|interpose-exec] FAIL: child exited " |
| 128 | + "with status %d\n", |
| 129 | + status); |
| 130 | + assert(0); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + printf("[Grate|interpose-exec] PASS\n"); |
| 135 | + return 0; |
| 136 | +} |
0 commit comments