Skip to content

Commit 81cab91

Browse files
0.24.1: preserve read-only grants under Linux confinement (#49)
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 8d35678 commit 81cab91

4 files changed

Lines changed: 69 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 4 deletions
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.23.0"
99

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

1414
## Why it does not use a C library
@@ -124,11 +124,22 @@ to its record in batches, and `e\0` at `kal_exit` or the C library's
124124
destructors. A grant carries its rights in `KAL_GRANT_RIGHTS`, and its record
125125
as a descriptor after the grants.
126126

127-
**Confinement** (`KAL_SPAWN_CONFINE`, `KAL_PROCESS_PROP_CONFINE`): Landlock, at
128-
the ABI version the kernel reports, set in the started image before it is
129-
replaced. The grants are the rules, by their rights; the program's own file,
127+
**Confinement** (`KAL_SPAWN_CONFINE`, `KAL_PROCESS_PROP_CONFINE`): Landlock ABI 3
128+
or later, set in the started image before it is replaced. Earlier ABIs cannot
129+
restrict file truncation, so confinement is not claimed and a request is
130+
refused before starting a program. The grants are the rules, by their rights; the program's own file,
130131
`/proc` and `/dev/null` are added, and `PR_SET_NO_NEW_PRIVS` is set.
131132

133+
## What 0.24.1 fixes
134+
135+
Confinement requires Landlock ABI 3, which can restrict both `truncate(2)` and
136+
`open(2)` with `O_RDONLY | O_TRUNC`. ABI 1 and 2 restrict opening a file for
137+
writing but cannot prevent these changes to a file in a read-only grant.
138+
The ABI cache uses atomic accesses when several threads first query process
139+
properties or start a program concurrently. The regression test opens an
140+
existing granted file for reading, attempts both raw truncation operations,
141+
and checks that its length remains unchanged.
142+
132143
**Observation.** A fault is reported on an alternate stack from
133144
`/proc/self/maps` and the ELF symbol tables, and the program is then ended as
134145
the kernel ends it. Samples are `ITIMER_PROF` and `SIGPROF` into a ring a

‎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.24.0"
4+
version = "0.24.1"
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/process.cpp‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,19 @@ namespace {
536536
// else. A ruleset of the grants, each with what its rights allow; the program
537537
// itself, to execute it; /proc, which the program's own implementation reads
538538
// (`kal_fs_resolved_name'); and /dev/null, which a null position is.
539+
// ABI 1/2 cannot restrict truncate(2), including O_RDONLY | O_TRUNC.
540+
// A read-only grant therefore requires ABI 3 before CONFINE can be claimed.
541+
constexpr int kConfinementAbi = 3;
542+
539543
int landlock_abi() {
540544
static int abi = -2;
541-
if (abi == -2) {
545+
int value = __atomic_load_n(&abi, __ATOMIC_ACQUIRE);
546+
if (value == -2) {
542547
const okl_long r = okl::sys(okl::nr_landlock_create_ruleset, 0, 0, 1 /* _VERSION */);
543-
abi = okl::failed(r) ? 0 : static_cast<int>(r);
548+
value = okl::failed(r) ? 0 : static_cast<int>(r);
549+
__atomic_store_n(&abi, value, __ATOMIC_RELEASE);
544550
}
545-
return abi;
551+
return value;
546552
}
547553

548554
okl_u64 handled_for(int abi) {
@@ -557,7 +563,7 @@ okl_u64 handled_for(int abi) {
557563
// replaced. Returns zero, or the kernel's negated reason.
558564
okl_long confine_to(const okl_u32* rights, kal_uintptr g, int base, const char* program) {
559565
const int abi = landlock_abi();
560-
if (abi < 1) return -okl::e_nosys;
566+
if (abi < kConfinementAbi) return -okl::e_nosys;
561567
struct { okl_u64 fs; } attr{ handled_for(abi) };
562568
const okl_long rs = okl::sys(okl::nr_landlock_create_ruleset, reinterpret_cast<okl_long>(&attr),
563569
sizeof attr, 0);
@@ -915,7 +921,7 @@ int kal_process_spawn(const kal_spawn* how,
915921
const kal_uintptr preference = how->flags & preferences;
916922
if ((preference & (preference - 1)) != 0) return kal_err_invalid;
917923
const bool confine = (how->flags & KAL_SPAWN_CONFINE) != 0;
918-
if (confine && landlock_abi() < 1) return kal_err_not_supported;
924+
if (confine && landlock_abi() < kConfinementAbi) return kal_err_not_supported;
919925
// THE NULL DEVICE BESIDE A STREAM AT ONE POSITION is two answers to one
920926
// question (openkal 0.22, clause 7.15).
921927
if (streams != nullptr
@@ -1608,7 +1614,7 @@ kal_uintptr kal_process_props(void) {
16081614
| KAL_PROCESS_PROP_JOB | KAL_PROCESS_PROP_JOB_USAGE
16091615
| KAL_PROCESS_PROP_JOB_LIMIT_SUPERVISED
16101616
| KAL_PROCESS_PROP_NULL_STREAMS
1611-
| (landlock_abi() >= 1 ? KAL_PROCESS_PROP_CONFINE : 0u)
1617+
| (landlock_abi() >= kConfinementAbi ? KAL_PROCESS_PROP_CONFINE : 0u)
16121618
| (open_id_available() || stat_state(0) != 0 ? KAL_PROCESS_PROP_OPEN_ID : 0u)
16131619
// AGREES WITH `kal_process_stop_requested', because the header
16141620
// defines null there as the absence this position reports. Read and

‎tests/openkal_0_23.cpp‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
// is refused by Landlock where the kernel has it. A PREFERENCE FOR ECONOMY
77
// turns the program's transparent huge pages off.
88
#include <cstdio>
9+
#include <cerrno>
910
#include <cstring>
1011
#include <cstdlib>
1112
#include <fcntl.h>
1213
#include <sys/prctl.h>
1314
#include <sys/syscall.h>
15+
#include <sys/stat.h>
1416
#include <unistd.h>
1517
import openkal.types;
1618
import openkal.env;
@@ -35,13 +37,14 @@ kal_dir root() {
3537
char g_self[4096];
3638
char g_dir[] = "/tmp/okl-0.24-XXXXXX";
3739

38-
int start(const char* errand, const kal_preopen* grants, kal_uintptr n, kal_uintptr flags) {
40+
int start(const char* errand, const kal_preopen* grants, kal_uintptr n, kal_uintptr flags,
41+
const char* operand = nullptr) {
3942
const kal_dir r = root();
40-
const char* args[] = { g_self, errand };
41-
const kal_uintptr lens[] = { std::strlen(g_self), std::strlen(errand) };
43+
const char* args[] = { g_self, errand, operand };
44+
const kal_uintptr lens[] = { std::strlen(g_self), std::strlen(errand), operand ? std::strlen(operand) : 0 };
4245
const kal_spawn how{ r, r, nullptr, grants, n, flags };
4346
kal_process p{};
44-
if (kal_process_spawn(&how, g_self + 1, std::strlen(g_self + 1), args, lens, 2, nullptr, nullptr, 0,
47+
if (kal_process_spawn(&how, g_self + 1, std::strlen(g_self + 1), args, lens, operand ? 3 : 2, nullptr, nullptr, 0,
4548
nullptr, &p) != kal_ok)
4649
return -1;
4750
int status = -1, terminated = 0;
@@ -75,6 +78,30 @@ int main(int argc, char** argv) {
7578
}
7679
if (argc > 1 && std::strcmp(argv[1], "--thp") == 0)
7780
return static_cast<int>(::prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0)) == 1 ? 70 : 71;
81+
if (argc == 3 && (std::strcmp(argv[1], "--truncate-raw") == 0 ||
82+
std::strcmp(argv[1], "--open-truncate-raw") == 0)) {
83+
// Prove the existing file is readable before asking the kernel to
84+
// truncate it. Missing files and unreachable grants are not proof.
85+
const int readable = static_cast<int>(::syscall(SYS_openat, AT_FDCWD, argv[2], O_RDONLY));
86+
if (readable < 0) return 66;
87+
::close(readable);
88+
errno = 0;
89+
const long result = std::strcmp(argv[1], "--truncate-raw") == 0
90+
? ::syscall(SYS_truncate, argv[2], 0)
91+
: ::syscall(SYS_openat, AT_FDCWD, argv[2], O_RDONLY | O_TRUNC);
92+
const int why = errno;
93+
if (result >= 0) {
94+
if (std::strcmp(argv[1], "--open-truncate-raw") == 0) ::close(static_cast<int>(result));
95+
return 64;
96+
}
97+
return why == EACCES ? 63 : 65;
98+
}
99+
100+
// The pinned compiler's Linux headers can predate Landlock. This syscall
101+
// has number 444 on both architectures this implementation builds for.
102+
const long landlock = ::syscall(444, nullptr, 0, 1 /* _VERSION */);
103+
check(kal::process::has(kal::process::confine) == (landlock >= 3),
104+
"confinement is claimed only where Landlock can restrict file truncation");
78105

79106
const kal_intptr n = kal_env_image(g_self, sizeof g_self - 1);
80107
if (n <= 0 || g_self[0] != '/') { std::printf("FAIL: the program's own image is named\n"); return 1; }
@@ -138,8 +165,17 @@ int main(int argc, char** argv) {
138165
const int s = start("--write-raw", grants, count, kal::process::confine_flag.bits);
139166
check(s == 60, "a confined program is refused by the kernel what its grants do not allow, without openkal");
140167
if (s != 60) std::printf(" the confined program reported %d\n", s);
168+
std::snprintf(path, sizeof path, "%s/present.txt", g_dir);
169+
const char* truncations[] = { "--truncate-raw", "--open-truncate-raw" };
170+
for (const char* errand : truncations) {
171+
check(start(errand, grants, count, kal::process::confine_flag.bits, path) == 63,
172+
"a readable file in a read-only grant cannot be truncated without openkal");
173+
struct stat info{};
174+
check(::stat(path, &info) == 0 && info.st_size == 1,
175+
"the read-only file retains its contents after the raw truncation attempt");
176+
}
141177
} else {
142-
std::printf(" this kernel has no Landlock; confinement is refused, which the conformance suite observes\n");
178+
std::printf(" this kernel has no Landlock ABI 3; confinement is refused, which the conformance suite observes\n");
143179
}
144180

145181
// Economy.

0 commit comments

Comments
 (0)