|
| 1 | +This folder contains example grate implementations, as well as an example runner for grate-cage configurations. |
| 2 | + |
| 3 | +### Compile |
| 4 | + |
| 5 | +`./tools/compile.sh <grate source files>` |
| 6 | + |
| 7 | +### Run |
| 8 | + |
| 9 | +- Compile all cages present in the `cage/` folder, and place them into `LIND_ROOT` using the `scripts/lind_compile.sh` command. |
| 10 | +- Compile all grates present in the `grates/` folder, and place them into `LIND_ROOT`. |
| 11 | +- Run an example grate configuration using `./tools/run.sh`. This will run the following configuration: |
| 12 | + |
| 13 | +``` |
| 14 | +geteuid_grate: |
| 15 | + getegid_grate.wasm: |
| 16 | + bash -c "etest & imfs_grate runopen & getgid_grate gidtest" |
| 17 | +``` |
| 18 | + |
| 19 | +### File Structure |
| 20 | + |
| 21 | +``` |
| 22 | +grates/ |
| 23 | + imfs_grate.c // Grate source code with wrappers for individual syscalls. |
| 24 | + imfs.* // IMFS source code. |
| 25 | + ... // Other Grate examples |
| 26 | +
|
| 27 | +cages/ |
| 28 | + mash.c // Barebones, minimal bash source code. |
| 29 | + etest.c |
| 30 | + runopen.c |
| 31 | + ... // Example cage source code. |
| 32 | +
|
| 33 | +syscalls // List of syscalls along with the arguments required. |
| 34 | +
|
| 35 | +tools/ |
| 36 | + - wand.py // Python script that generates bindings for syscall conversions. |
| 37 | + - magic.*tmpl // Template files for .h and .c files with the necessary bindings. |
| 38 | + - compile.sh // Compilation script. |
| 39 | + - run.sh // Run runopen.c |
| 40 | +``` |
| 41 | + |
| 42 | +### Writing a syscall wrapper |
| 43 | + |
| 44 | +Consider the example of the `xstat_grate` syscall wrapper written with the new API: |
| 45 | + |
| 46 | +```c |
| 47 | +int xstat_syscall(int cageid, char *pathname, struct stat *statbuf) { |
| 48 | + return imfs_stat(cageid, pathname, statbuf); |
| 49 | +} |
| 50 | +``` |
| 51 | +
|
| 52 | +The syscall wrapper only has to deal with parameters that a regular syscall declaration would take, along with the `cageid` parameter which points to the id of the cage that called this syscall. |
| 53 | +
|
| 54 | +Handling of copying data in or out of cages is handled internally. In this example, `pathname` already points to a valid memory address in this grate, and anything written to `*statbuf` is copied out to the appropriate location in the calling cage's memory. |
| 55 | +
|
| 56 | +For an example of a complete grate file, view `imfs_grate.c`. A short example is below: |
| 57 | +
|
| 58 | +```c |
| 59 | +#include <sys/types.h> |
| 60 | +#include <sys/stat.h> |
| 61 | +
|
| 62 | +#include <lind_syscall_num.h> |
| 63 | +#include "magic.h" // These are the headers required to use the bindings. |
| 64 | +
|
| 65 | +#include "imfs.h" |
| 66 | +
|
| 67 | +// grate_syscalls* are extern'd variables. These should be a list of syscall nums. |
| 68 | +int grate_syscalls[] = {XSTAT_SYSCALL}; |
| 69 | +int grate_syscalls_len = 1; |
| 70 | +
|
| 71 | +// Optional initialization and destroy logic or the grate. |
| 72 | +void grate_init() { |
| 73 | + imfs_init(); |
| 74 | +} |
| 75 | +
|
| 76 | +void grate_destroy() { |
| 77 | + printf("IMFS Exiting\n"); |
| 78 | +} |
| 79 | +
|
| 80 | +int xstat_syscall(int cageid, char *pathname, struct stat *statbuf) { |
| 81 | + return imfs_stat(cageid, pathname, statbuf); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Internals |
| 86 | + |
| 87 | +The full implementation of how these bindings are generated can be seen in `tools/wand.py`, and `tools/compile.sh`. |
| 88 | + |
| 89 | +We begin with a list of syscall declarations, which give us extra information about the parameters of a given syscall. Some examples are below: |
| 90 | + |
| 91 | +```js |
| 92 | +xstat = { |
| 93 | + IN char* pathname |
| 94 | + OUT struct stat* statbuf |
| 95 | +} |
| 96 | + |
| 97 | +read = { |
| 98 | + N int fd |
| 99 | + OUT void* buf[count] |
| 100 | + N size_t count |
| 101 | +} |
| 102 | + |
| 103 | +write = { |
| 104 | + N int fd |
| 105 | + IN void* buf[count] |
| 106 | + N size_t count |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The `IN/OUT/N` tags tell us when or if to call `copy_data_between_cages` for a particular argument. This is extrapolated using the type of the argument as listed on the man pages. For e.g. `const` pointers are not copied out. Reguar pointers are copied in before the syscall and copied out after it (useful for calls such as `recvmsg()`). Integer and integer-aliased types are not copied. |
| 111 | + |
| 112 | +These tagged structs allow us to generate the `<syscall>_grate` functions that eventually call the user-defined wrappers. Some examples are cited below, for an example generated binding, view `magic.c` and `magic.h`. |
| 113 | + |
| 114 | + |
| 115 | +```c |
| 116 | +// Close doesn't require any copying of data. |
| 117 | +int close_grate(uint64_t cageid, ..., uint64_t arg6cage) { |
| 118 | + if (!close_syscall) { |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + int fd = arg1; |
| 123 | + |
| 124 | + int ret = close_syscall(cageid, fd); |
| 125 | + |
| 126 | + return ret; |
| 127 | +} |
| 128 | +``` |
| 129 | +
|
| 130 | +```c |
| 131 | +// XSTAT requires us to copy over the "pathname" before we call our wrapper, and call copy on "statbuf" to return the result to the calling cage. |
| 132 | +int xstat_grate(uint64_t cageid, ..., uint64_t arg6cage) { |
| 133 | + if (!xstat_syscall) { |
| 134 | + return -1; |
| 135 | + } |
| 136 | +
|
| 137 | + struct stat *statbuf = malloc(sizeof(struct stat)); |
| 138 | +
|
| 139 | + if (statbuf == NULL) { |
| 140 | + perror("malloc failed"); |
| 141 | + exit(EXIT_FAILURE); |
| 142 | + } |
| 143 | +
|
| 144 | + copy_data_between_cages(thiscage, arg2cage, arg2, arg2cage, (uint64_t)statbuf, |
| 145 | + thiscage, sizeof(struct stat), 0); |
| 146 | +
|
| 147 | + char *pathname = malloc(256); |
| 148 | +
|
| 149 | + if (pathname == NULL) { |
| 150 | + perror("malloc failed"); |
| 151 | + exit(EXIT_FAILURE); |
| 152 | + } |
| 153 | +
|
| 154 | + copy_data_between_cages(thiscage, arg1cage, arg1, arg1cage, |
| 155 | + (uint64_t)pathname, thiscage, 256, 1); |
| 156 | +
|
| 157 | + int ret = xstat_syscall(cageid, pathname, statbuf); |
| 158 | +
|
| 159 | + if (arg2 != 0) { |
| 160 | + copy_data_between_cages(thiscage, arg2cage, (uint64_t)statbuf, thiscage, |
| 161 | + arg2, arg2cage, sizeof(struct stat), 0); |
| 162 | + } |
| 163 | +
|
| 164 | + free(statbuf); |
| 165 | + free(pathname); |
| 166 | +
|
| 167 | + return ret; |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +#### Notes on dealing with a `buffer` vs `char *`: |
| 172 | + |
| 173 | +When allocating memory for "strings", there are two distinct cases to handle. |
| 174 | + |
| 175 | +For a `char *`, for now we assign a constant `256` bytes of memory and copies are done using `StrCpy` mode (relying on `\0` as the terminating character). Example - `char * pathname` in `open`) |
| 176 | + |
| 177 | +For buffers, (`read/write` has `void buf[count]`), we allocate the mentioned size of memory, and use `RawCpy` to copy exactly this size. |
| 178 | + |
| 179 | +For non-string pointers (such as `struct stat *statbuf`), we allocate `sizeof(<type>)` bytes, and use `RawCpy`. |
| 180 | + |
| 181 | +The current bindings generator implicitly handles all these cases. |
0 commit comments