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>
1517import openkal.types;
1618import openkal.env;
@@ -35,13 +37,14 @@ kal_dir root() {
3537char g_self[4096 ];
3638char 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