|
| 1 | +// A directory the program may search but not read is still a base (0.18.3). |
| 2 | +// |
| 3 | +// A `kal_dir' names a directory; resolving beneath it, starting a program in |
| 4 | +// it and describing it read nothing, so none of them needs the right to read |
| 5 | +// it. Only listing does. Until 0.18.2 every handle was opened for reading, and |
| 6 | +// on Android 14 under `run-as' (SELinux `runas_app') the root preopen itself |
| 7 | +// failed with EACCES: every program started by absolute name was reported as |
| 8 | +// not found (mcpp#857). |
| 9 | +// |
| 10 | +// Here the same situation is made with a mode instead of a policy: a directory |
| 11 | +// of mode 0111. A privileged user reads it regardless, so the one assertion |
| 12 | +// that it cannot be listed is made only for an unprivileged one. |
| 13 | +#include <cstdio> |
| 14 | +#include <cstring> |
| 15 | +#include <sys/stat.h> |
| 16 | +#include <unistd.h> |
| 17 | +import openkal.fs; |
| 18 | +import openkal.stream; |
| 19 | +import openkal.types; |
| 20 | +import openkal.process; |
| 21 | + |
| 22 | +namespace { |
| 23 | +int failures = 0; |
| 24 | +void check(bool held, const char* what) { |
| 25 | + if (!held) { std::printf("FAIL: %s\n", what); ++failures; } |
| 26 | +} |
| 27 | + |
| 28 | +kal_dir root() { |
| 29 | + for (kal_uintptr i = 0; i < kal::fs::preopen_count(); ++i) { |
| 30 | + kal_dir d{}; char name[512]; kal_uintptr len = 0; |
| 31 | + if (kal_fs_preopen(i, &d, name, sizeof name, &len) == kal_ok && len == 1 && name[0] == '/') |
| 32 | + return d; |
| 33 | + } |
| 34 | + return kal_dir{}; |
| 35 | +} |
| 36 | +} |
| 37 | + |
| 38 | +int main() { |
| 39 | + const auto work = kal::fs::working(); |
| 40 | + check(kal_fs_mkdir(work, "okl_search_only", 15) == kal_ok, "a directory is created"); |
| 41 | + kal_dir d{}; |
| 42 | + check(kal_fs_open_dir(work, "okl_search_only", 15, &d) == kal_ok, "it opens"); |
| 43 | + kal_file f{}; |
| 44 | + check(kal::fs::open_file(d, "inside", 6, kal::fs::open::write | kal::fs::open::create, &f) == kal_ok, |
| 45 | + "a file is created in it"); |
| 46 | + kal_stream_write(kal_fs_stream(f), "x", 1); |
| 47 | + kal_fs_close_file(f); |
| 48 | + kal_fs_close_dir(d); |
| 49 | + |
| 50 | + check(::chmod("okl_search_only", 0111) == 0, "the directory loses its read right"); |
| 51 | + |
| 52 | + kal_dir s{}; |
| 53 | + check(kal_fs_open_dir(work, "okl_search_only", 15, &s) == kal_ok, |
| 54 | + "a searchable directory without the read right opens as a base"); |
| 55 | + kal_file g{}; |
| 56 | + check(kal::fs::open_file(s, "inside", 6, kal::fs::open::read, &g) == kal_ok, |
| 57 | + "a name beneath it resolves"); |
| 58 | + char c = 0; |
| 59 | + check(kal_stream_read(kal_fs_stream(g), &c, 1) == 1 && c == 'x', "the file beneath it reads"); |
| 60 | + kal_fs_close_file(g); |
| 61 | + |
| 62 | + // A program is started IN it: the base it is resolved under is the root |
| 63 | + // preopen, and its working directory is the search-only directory. |
| 64 | + const char* argv[] = { "true" }; |
| 65 | + const kal_uintptr lens[] = { 4 }; |
| 66 | + const kal_spawn how{ root(), s, nullptr, nullptr, 0, 0 }; |
| 67 | + kal_process p{}; |
| 68 | + const int rc = kal_process_spawn(&how, "bin/true", 8, argv, lens, 1, nullptr, nullptr, 0, nullptr, &p); |
| 69 | + check(rc == kal_ok, "a program starts in a search-only directory"); |
| 70 | + if (rc == kal_ok) { |
| 71 | + int status = -1, terminated = -1; |
| 72 | + kal_process_wait(p, &status, &terminated); |
| 73 | + kal_process_close(p); |
| 74 | + check(terminated == 0 && status == 0, "the program ran to completion"); |
| 75 | + } |
| 76 | + |
| 77 | + if (::geteuid() != 0) { |
| 78 | + kal_uintptr iter = 0; |
| 79 | + check(kal_fs_list_begin(s, &iter) == kal_err_permission, |
| 80 | + "listing it is what the missing read right refuses"); |
| 81 | + } |
| 82 | + kal_fs_close_dir(s); |
| 83 | + ::chmod("okl_search_only", 0755); |
| 84 | + ::unlink("okl_search_only/inside"); |
| 85 | + ::rmdir("okl_search_only"); |
| 86 | + |
| 87 | + if (failures == 0) std::printf("search_only_base: ok\n"); |
| 88 | + return failures == 0 ? 0 : 1; |
| 89 | +} |
0 commit comments