Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mcpp.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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
Expand Down
44 changes: 36 additions & 8 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(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) {
Expand All @@ -82,7 +107,7 @@ struct profile {
if (okm::failed(okm::sys(okm::nr_fcntl, grants[i], okm::f_getpath, reinterpret_cast<okm_long>(path)))) continue;
ok = put(" (subpath ") && quoted(path) && put(")");
}
return ok && put(")))\n");
return ok && put(")))\n") ? kal_ok : kal_err_no_space;
}
};

Expand Down Expand Up @@ -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<okm_long>(streams->in.h) : 0;
const okm_long ou = streams ? static_cast<okm_long>(streams->out.h) : 0;
Expand Down
34 changes: 34 additions & 0 deletions tests/openkal_0_23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstring>
#include <cstdlib>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <mach-o/dyld.h>
import openkal.types;
Expand Down Expand Up @@ -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 < 940) {
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");
}
Expand Down
Loading