Debian Codegraph @

src/landlock.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2025 Thomas Habets <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 #define _GNU_SOURCE
19 #if HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <dirent.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #if HAVE_LINUX_LANDLOCK_H
30 #if HAS_LANDLOCK
31 #include<linux/landlock.h>
32 #include<sys/syscall.h>
33 #define ACTUALLY_USE_LANDLOCK 1
34 #endif
35 #endif
36
37 #include "arping.h"
38 #include "cast.h"
39
40 #ifdef ACTUALLY_USE_LANDLOCK
41 static int ll_create_ruleset(const struct landlock_ruleset_attr *attr, size_t size, __u32 flags) {
42 return cast_long_int(syscall(SYS_landlock_create_ruleset, attr, size, flags), NULL);
43 }
44 /*
45 static int ll_add_rule(int ruleset_fd, enum landlock_rule_type type, const void *rule_attr, __u32 flags) {
46 return syscall(SYS_landlock_add_rule, ruleset_fd, type, rule_attr, flags);
47 }
48 */
49 static long ll_restrict_self(int ruleset_fd, __u32 flags) {
50 return syscall(SYS_landlock_restrict_self, ruleset_fd, flags);
51 }
52 #endif
53
54 void
55 drop_landlock()
56 {
57 #ifdef ACTUALLY_USE_LANDLOCK
58 struct landlock_ruleset_attr ruleset_attr = {
59 .handled_access_fs = 0
60 | LANDLOCK_ACCESS_FS_WRITE_FILE
61 | LANDLOCK_ACCESS_FS_READ_FILE
62 | LANDLOCK_ACCESS_FS_READ_DIR
63 | LANDLOCK_ACCESS_FS_REMOVE_DIR
64 | LANDLOCK_ACCESS_FS_REMOVE_FILE
65 | LANDLOCK_ACCESS_FS_MAKE_CHAR
66 | LANDLOCK_ACCESS_FS_MAKE_DIR
67 | LANDLOCK_ACCESS_FS_MAKE_REG
68 | LANDLOCK_ACCESS_FS_MAKE_SOCK
69 | LANDLOCK_ACCESS_FS_MAKE_FIFO
70 #ifdef LANDLOCK_ACCESS_FS_REFER
71 | LANDLOCK_ACCESS_FS_REFER
72 #endif
73 | LANDLOCK_ACCESS_FS_MAKE_BLOCK
74 | LANDLOCK_ACCESS_FS_MAKE_SYM
75 #ifdef LANDLOCK_ACCESS_FS_TRUNCATE
76 | LANDLOCK_ACCESS_FS_TRUNCATE
77 #endif
78 #ifdef LANDLOCK_ACCESS_FS_IOCTL_DEV
79 | LANDLOCK_ACCESS_FS_IOCTL_DEV
80 #endif
81 | LANDLOCK_ACCESS_FS_EXECUTE,
82 #ifdef LANDLOCK_ACCESS_NET_BIND_TCP
83 .handled_access_net =
84 LANDLOCK_ACCESS_NET_BIND_TCP
85 | LANDLOCK_ACCESS_NET_CONNECT_TCP,
86 #endif
87 #ifdef LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
88 .scoped =
89 LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
90 | LANDLOCK_SCOPE_SIGNAL,
91 #endif
92 };
93 int ruleset = ll_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
94 if (ruleset < 0) {
95 fprintf(stderr, "arping: Failed to create landlock ruleset: %s\n",
96 strerror(errno));
97 return;
98 }
99 if (ll_restrict_self(ruleset, 0)) {
100 fprintf(stderr, "arping: Failed to restrict with landlock: %s\n",
101 strerror(errno));
102 close(ruleset);
103 return;
104 }
105 close(ruleset);
106 DIR *de = opendir("/");
107 if (de) {
108 fprintf(stderr, "arping: landlock failed to take effect\n");
109 closedir(de);
110 } else if (errno != EACCES) {
111 fprintf(stderr, "arping: landlock caused error not EACCES: %s\n",
112 strerror(errno));
113 }
114 if (verbose > 0) {
115 printf("arping: Landlock enabled\n");
116 }
117 #endif
118 }
119
120 /* ---- Emacs Variables ----
121 * Local Variables:
122 * c-basic-offset: 8
123 * indent-tabs-mode: nil
124 * End:
125 *
126 * vim: ts=8 sw=8
127 */