From 365d10dfcd4d088a0063361c883be0934fd08e44 Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Sat, 7 Nov 2026 05:10:18 +0900 Subject: [PATCH 1/2] 0.22.1: the confinement rules are as long as the grants make them The sandbox profile was a fixed 16 KiB; sixteen grants named near MAXPATHLEN exceeded it and the start was refused with kal_err_invalid. The profile is now sized to the bound of what the rules can hold for the grants of the start; an allocation failure reports kal_err_no_memory and an overflow, which the bound excludes, kal_err_no_space. The 0.23 test starts a confined program with sixteen grants, the last named by more than 950 bytes. OpenKal is the compatible range ^0.24.0. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com> --- README.md | 11 ++++++++++- mcpp.toml | 4 ++-- src/process.cpp | 44 ++++++++++++++++++++++++++++++++++-------- tests/openkal_0_23.cpp | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f1406af..ab40f71 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ written on the kernel's own calls. openkal = "0.24.0" [target.'cfg(os = "macos")'.dependencies] -openkal-macos = "0.22.0" +openkal-macos = "0.22.1" ``` Its purpose is as much to test the specification as to be used. A specification @@ -23,6 +23,15 @@ References retain the original process identity, birth and boot session. Retirement checks the live continuation before and after membership migration; ambiguity reports `unknown`. No process registry is installed. +**The confinement rules are as long as the grants make them (0.22.1).** The +sandbox profile names each grant in the write rule and in the read rule. Its +buffer was a fixed 16 KiB, and sixteen grants with names near `MAXPATHLEN` +exceeded it; the start was refused with `kal_err_invalid`. The buffer is now +sized to the bound of what the rules can contain for the grants of that start. +A start whose rules cannot be held reports `kal_err_no_memory`; overflowing the +bound, which the bound excludes, reports `kal_err_no_space`. The dependency on +OpenKal is the compatible range `^0.24.0`. + ## Interfaces provided Every interface of `SURFACE.txt` except `openkal.prof` and `openkal.debug`, which are a diff --git a/mcpp.toml b/mcpp.toml index 95b533d..4c74e1a 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "openkal-macos" -version = "0.22.0" +version = "0.22.1" description = "An implementation of openkal for macOS, written on the kernel's own calls. Its purpose is as much to test the specification as to be used." license = "Apache-2.0" @@ -61,7 +61,7 @@ provides-interfaces = [ ] [dependencies] -openkal = "0.24.0" +openkal = "^0.24.0" [build] # The flags are attached to this package's own sources rather than to the whole diff --git a/src/process.cpp b/src/process.cpp index aff53de..c437bc1 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -43,22 +43,47 @@ constexpr kal_uintptr kMaxEntries = 512; // are not read-only and the devices a program writes to. Asking whether a name // exists, and what it is, is not refused: it reveals what the name is, which // the rule above already allows a program to learn by trying. +// +// THE RULES ARE AS LONG AS THE GRANTS MAKE THEM (0.22.1). A fixed 16 KiB was +// exceeded by sixteen long grants, and the start was refused with +// kal_err_invalid, which says the request was malformed. The capacity is now +// the bound of what `build' can write --- the fixed rules, the program's name +// quoted, and each grant's name quoted in both rules --- and memory for it is +// obtained before the start. A start whose rules cannot be held fails with +// kal_err_no_memory; one whose rules outgrow the bound, which the bound +// excludes, with kal_err_no_space. struct profile { - char text[16384]; - okm_uptr n = 0; - bool put(const char* s) { while (*s) { if (n + 1 >= sizeof text) return false; text[n++] = *s++; } text[n] = '\0'; return true; } + static constexpr okm_uptr kFixed = 1024; // the literal rules, with room + static constexpr okm_uptr kName = 1024; // F_GETPATH's MAXPATHLEN + static constexpr okm_uptr kGrant = 2 * (16 + 2 * kName + 2); + char* text = nullptr; + okm_uptr cap = 0; + okm_uptr n = 0; + profile() = default; + profile(const profile&) = delete; + profile& operator=(const profile&) = delete; + ~profile() { if (text) kal_free(text, cap, 1); } + bool reserve(kal_uintptr grants, const char* program) { + okm_uptr p = 0; + while (program[p]) ++p; + cap = kFixed + 2 * p + 4 + grants * kGrant; + text = static_cast(kal_alloc(cap, 1)); + return text != nullptr; + } + bool put(const char* s) { while (*s) { if (n + 1 >= cap) return false; text[n++] = *s++; } text[n] = '\0'; return true; } bool quoted(const char* s) { if (!put("\"")) return false; for (; *s; ++s) { - if (n + 3 >= sizeof text) return false; + if (n + 3 >= cap) return false; if (*s == '"' || *s == '\\') text[n++] = '\\'; text[n++] = *s; } text[n] = '\0'; return put("\""); } - bool build(const okm_long* grants, const okm_u32* rights, kal_uintptr g, const char* program) { - char path[1024]; + int build(const okm_long* grants, const okm_u32* rights, kal_uintptr g, const char* program) { + if (!reserve(g, program)) return kal_err_no_memory; + char path[kName]; bool ok = put("(version 1)\n(allow default)\n(deny file-write* (require-not (require-any" " (literal \"/dev/null\") (literal \"/dev/tty\") (literal \"/dev/dtracehelper\")"); for (kal_uintptr i = 0; ok && i < g; ++i) { @@ -82,7 +107,7 @@ struct profile { if (okm::failed(okm::sys(okm::nr_fcntl, grants[i], okm::f_getpath, reinterpret_cast(path)))) continue; ok = put(" (subpath ") && quoted(path) && put(")"); } - return ok && put(")))\n"); + return ok && put(")))\n") ? kal_ok : kal_err_no_space; } }; @@ -907,7 +932,10 @@ int kal_process_spawn(const kal_spawn* how, // CONFINED (0.23): the rules are written here, before the duplication, and // applied in the duplicate before it is replaced. profile rules; - if (confine && !rules.build(granted, grant_rights, how->grant_count, whole)) return kal_err_invalid; + if (confine) { + const int built = rules.build(granted, grant_rights, how->grant_count, whole); + if (built != kal_ok) return built; + } const okm_long in = streams ? static_cast(streams->in.h) : 0; const okm_long ou = streams ? static_cast(streams->out.h) : 0; diff --git a/tests/openkal_0_23.cpp b/tests/openkal_0_23.cpp index 98c0e08..015ef07 100644 --- a/tests/openkal_0_23.cpp +++ b/tests/openkal_0_23.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include import openkal.types; @@ -162,6 +163,39 @@ int main(int argc, char** argv) { std::printf(" the confined program reported %d (ended by %d), granted:\n", s, g_ended_by); for (kal_uintptr k = 0; k < count; ++k) std::printf(" %s\n", names[k]); } + + // SIXTEEN LONG GRANTS (0.22.1): the rules name every grant twice, and + // a fixed 16 KiB could not hold sixteen names near MAXPATHLEN; the + // start was refused with kal_err_invalid. The grants above are kept + // (the program loads from them) and the rest are long directories. + char deep[1024]; + std::snprintf(deep, sizeof deep, "%s", g_dir); + while (std::strlen(deep) + 1 + 100 < 900) { + char part[101]; + std::memset(part, 'd', 100); + part[100] = 0; + std::strcat(deep, "/"); + std::strcat(deep, part); + mkdir(deep, 0755); + } + kal_uintptr full = count; + for (int i = 0; full < 16; ++i) { + std::snprintf(names[full], sizeof names[full], "%s/grant-%02d-%s", deep, i, "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); + mkdir(names[full], 0755); + kal_dir d{}, a{}; + if (kal_fs_open_dir(root(), names[full] + 1, std::strlen(names[full] + 1), &d) != kal_ok + || kal_fs_dir_attenuate(d, &ro, &a) != kal_ok) { + check(false, "a long directory is opened and attenuated"); + break; + } + grants[full] = kal_preopen{ a, names[full], std::strlen(names[full]) }; + ++full; + } + check(full == 16 && std::strlen(names[15]) > 950, "sixteen grants, the last named by more than 950 bytes"); + const int r = start("--resources", grants, full, + (kal::process::confine_flag | kal::process::economy_flag).bits); + check(r == 72, "a confined start with sixteen long grants runs"); + if (r != 72) std::printf(" the start reported %d (ended by %d)\n", r, g_ended_by); } else { std::printf(" confinement is not claimed here\n"); } From 69588984bff55f3b8b49a88ec00ad8f19c95168a Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Sat, 7 Nov 2026 05:26:13 +0900 Subject: [PATCH 2/2] test: the long grants reach more than 950 bytes Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com> --- tests/openkal_0_23.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/openkal_0_23.cpp b/tests/openkal_0_23.cpp index 015ef07..3c6894b 100644 --- a/tests/openkal_0_23.cpp +++ b/tests/openkal_0_23.cpp @@ -170,7 +170,7 @@ int main(int argc, char** argv) { // (the program loads from them) and the rest are long directories. char deep[1024]; std::snprintf(deep, sizeof deep, "%s", g_dir); - while (std::strlen(deep) + 1 + 100 < 900) { + while (std::strlen(deep) + 1 + 100 < 940) { char part[101]; std::memset(part, 'd', 100); part[100] = 0;