commit 43947cc

shtrophic  ·  2024-12-07 16:17:18 +0000 UTC
parent e52b4bf
update landloc.h
1 files changed,  +26, -4
+26, -4
 1@@ -18,9 +18,19 @@
 2  */
 3 
 4 /**
 5- * Usage example:
 6- *
 7+ * Repository: https://git.sr.ht/~shtrophic/landloc.h
 8+ */
 9 
10+/**
11+ * Usage:
12+ *
13+ * Define a sandboxing function using the LL_BEGIN(...) and LL_END macros.
14+ * the arguments of LL_BEGIN are the function's signature.
15+ * Between those macros, implement your sandbox using LL_PATH() and LL_PORT() macros.
16+ * Calling LL_PATH() and LL_PORT() anywhere else will not work.
17+ * You may prepend `static` before LL_BEGIN to make the function static.
18+ * You need (should) wrap your sandboxing code in another set of braces:
19+ *
20 LL_BEGIN(my_sandbox_function, const char *rw_path) {
21 
22     LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
23@@ -28,6 +38,10 @@ LL_BEGIN(my_sandbox_function, const char *rw_path) {
24 
25 } LL_END
26 
27+ *
28+ * Then, call it in your application's code.
29+ *
30+
31 int main(void) {
32 
33     int status = my_sandbox_function("some/path");
34@@ -37,6 +51,14 @@ int main(void) {
35     }
36 
37 }
38+
39+ *
40+ * You may define LL_PRINTERR(fmt, ...) before including this header to enable debug output:
41+ *
42+
43+#define LL_PRINTERR(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
44+#include "landloc.h"
45+
46  */
47 
48 #ifndef __LANDLOC_H__
49@@ -138,9 +160,9 @@ int main(void) {
50 } while (0)
51 
52 #define LL_PORT(p, rules) do {\
53+    unsigned short __port = (p);\
54+    __nattr.allowed_access = (rules);\
55     if (ll_abi > 3) {\
56-        unsigned short __port = (p);\
57-        __nattr.allowed_access = (rules);\
58         __nattr.port = __port;\
59         __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
60         if (__err) {\