Skip to content

Commit cde4abb

Browse files
0.18.3 --- a directory handle names a directory and does not read one (#42)
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent d33e5c4 commit cde4abb

5 files changed

Lines changed: 118 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ for Linux, written on the kernel's own system-call interface.
88
openkal = "0.17.0"
99

1010
[target.'cfg(os = "linux")'.dependencies]
11-
openkal-linux = "0.18.2"
11+
openkal-linux = "0.18.3"
1212
```
1313

1414
## Why it does not use a C library

‎mcpp.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
namespace = "mcpplibs"
33
name = "openkal-linux"
4-
version = "0.18.2"
4+
version = "0.18.3"
55
description = "The reference implementation of openkal for Linux, written on the kernel's own system-call interface so that it can be placed beneath a C library as well as above one."
66
license = "Apache-2.0"
77

‎src/fs.cpp‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ bool granted(preopen* t, kal_uintptr* n) {
8282
return true;
8383
}
8484

85+
// A DIRECTORY HANDLE NAMES A DIRECTORY; IT DOES NOT READ ONE (0.18.3).
86+
//
87+
// Every `kal_dir' is a base: names are resolved beneath it (`*at'), a program
88+
// is started in it (`fchdir'), and it is described (`fstat', `fstatfs'). None
89+
// of these reads the directory, and `O_PATH' is the open that asks for no
90+
// access but naming. Reading happens only where entries are listed, and an
91+
// enumeration already opens a descriptor of its own through the handle, with
92+
// read access, at that moment.
93+
//
94+
// Until 0.18.2 the handles were opened for reading, and a directory the
95+
// program may search but not read could not be a base at all. Measured on
96+
// Android 14 under `run-as' (SELinux domain `runas_app', mcpp#857): the
97+
// preopen of `/' failed with EACCES, so every name resolved beneath the root,
98+
// and every program started by absolute name, failed before anything was
99+
// started --- reported as "not found", because the base it would have been
100+
// found in was never opened. The same program, its directory and its exec
101+
// path all worked when each step was called directly.
102+
constexpr okl_long dir_handle_flags = okl::o_path | okl::o_directory | okl::o_cloexec;
103+
85104
preopen* table(kal_uintptr* count) {
86105
static preopen t[kMaxGrants];
87106
static kal_uintptr n = 2;
@@ -102,11 +121,9 @@ preopen* table(kal_uintptr* count) {
102121
else { g_cwd[0] = '.'; g_cwd[1] = '\0'; cwd_len = 1; }
103122

104123
const okl_long fd0 = okl::sys(okl::nr_openat, okl::at_fdcwd,
105-
reinterpret_cast<okl_long>("."),
106-
okl::o_rdonly | okl::o_directory | okl::o_cloexec, 0);
124+
reinterpret_cast<okl_long>("."), dir_handle_flags, 0);
107125
const okl_long fd1 = okl::sys(okl::nr_openat, okl::at_fdcwd,
108-
reinterpret_cast<okl_long>("/"),
109-
okl::o_rdonly | okl::o_directory | okl::o_cloexec, 0);
126+
reinterpret_cast<okl_long>("/"), dir_handle_flags, 0);
110127
t[0] = { g_cwd, cwd_len, okl::failed(fd0) ? 0u : okl::pack(static_cast<int>(fd0)) };
111128
t[1] = { "/", 1, okl::failed(fd1) ? 0u : okl::pack(static_cast<int>(fd1)) };
112129
}
@@ -398,7 +415,7 @@ int kal_fs_open_dir(kal_dir base, const char* name, kal_uintptr len, kal_dir* ou
398415
if (b < 0 || out == nullptr || !okl::acceptable(name, len)) return kal_err_invalid;
399416
okl::terminated t(name, len); if (!t.ok) return kal_err_invalid;
400417
const okl_long fd = okl::sys(okl::nr_openat, b, reinterpret_cast<okl_long>(t.buf),
401-
okl::o_rdonly | okl::o_directory | okl::o_cloexec, 0);
418+
dir_handle_flags, 0);
402419
if (okl::failed(fd)) return okl::translate(fd);
403420
*out = kal_dir{ okl::pack(static_cast<int>(fd)) };
404421
return kal_ok;

‎src/process.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ inline okl_long handle_pid(kal_process h) { return static_cast<okl_long>(h.h & 0
344344
// the duplicate still allocates nothing. It costs the property `execveat'
345345
// bought, that the name is resolved through the descriptor, and is paid only
346346
// where `execveat' is unusable: from the first start under a translator this
347-
// implementation recognises, and after the first ENOSYS anywhere else.
347+
// implementation recognises, and, elsewhere, by each start that meets ENOSYS,
348+
// which falls back in the started image itself. That fallback is learned by
349+
// no other start: the image is a copy made by `clone' without shared memory,
350+
// so nothing it learns reaches this program, and the price is one refused
351+
// system call per start.
348352
//
349353
// Ported from the copy mcppls carried (vendor/openkal-linux 0.15.1), which set
350354
// the flag itself under PRoot; recognising the translator here removes that.

‎tests/search_only_base.cpp‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)