summaryrefslogtreecommitdiff
path: root/landloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'landloc.h')
-rw-r--r--landloc.h30
1 files changed, 26 insertions, 4 deletions
diff --git a/landloc.h b/landloc.h
index c5b849a..e1ade20 100644
--- a/landloc.h
+++ b/landloc.h
@@ -18,9 +18,19 @@
18 */ 18 */
19 19
20/** 20/**
21 * Usage example: 21 * Repository: https://git.sr.ht/~shtrophic/landloc.h
22 * 22 */
23 23
24/**
25 * Usage:
26 *
27 * Define a sandboxing function using the LL_BEGIN(...) and LL_END macros.
28 * the arguments of LL_BEGIN are the function's signature.
29 * Between those macros, implement your sandbox using LL_PATH() and LL_PORT() macros.
30 * Calling LL_PATH() and LL_PORT() anywhere else will not work.
31 * You may prepend `static` before LL_BEGIN to make the function static.
32 * You need (should) wrap your sandboxing code in another set of braces:
33 *
24LL_BEGIN(my_sandbox_function, const char *rw_path) { 34LL_BEGIN(my_sandbox_function, const char *rw_path) {
25 35
26 LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE); 36 LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
@@ -28,6 +38,10 @@ LL_BEGIN(my_sandbox_function, const char *rw_path) {
28 38
29} LL_END 39} LL_END
30 40
41 *
42 * Then, call it in your application's code.
43 *
44
31int main(void) { 45int main(void) {
32 46
33 int status = my_sandbox_function("some/path"); 47 int status = my_sandbox_function("some/path");
@@ -37,6 +51,14 @@ int main(void) {
37 } 51 }
38 52
39} 53}
54
55 *
56 * You may define LL_PRINTERR(fmt, ...) before including this header to enable debug output:
57 *
58
59#define LL_PRINTERR(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
60#include "landloc.h"
61
40 */ 62 */
41 63
42#ifndef __LANDLOC_H__ 64#ifndef __LANDLOC_H__
@@ -138,9 +160,9 @@ int main(void) {
138} while (0) 160} while (0)
139 161
140#define LL_PORT(p, rules) do {\ 162#define LL_PORT(p, rules) do {\
163 unsigned short __port = (p);\
164 __nattr.allowed_access = (rules);\
141 if (ll_abi > 3) {\ 165 if (ll_abi > 3) {\
142 unsigned short __port = (p);\
143 __nattr.allowed_access = (rules);\
144 __nattr.port = __port;\ 166 __nattr.port = __port;\
145 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\ 167 __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
146 if (__err) {\ 168 if (__err) {\