suricata
util-landlock.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Eric Leblond <el@stamus-networks.com>
22  */
23 
24 #include "suricata.h"
25 #include "feature.h"
26 #include "util-conf.h"
27 #include "util-file.h"
28 #include "util-landlock.h"
29 #include "util-mem.h"
30 #include "util-path.h"
31 
32 #ifndef HAVE_LINUX_LANDLOCK_H
33 
35 {
36 }
37 
38 #else /* HAVE_LINUX_LANDLOCK_H */
39 
40 #include <linux/landlock.h>
41 
42 #ifndef landlock_create_ruleset
43 static inline int landlock_create_ruleset(
44  const struct landlock_ruleset_attr *const attr, const size_t size, const __u32 flags)
45 {
46  return syscall(__NR_landlock_create_ruleset, attr, size, flags);
47 }
48 #endif
49 
50 #ifndef landlock_add_rule
51 static inline int landlock_add_rule(const int ruleset_fd, const enum landlock_rule_type rule_type,
52  const void *const rule_attr, const __u32 flags)
53 {
54  return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr, flags);
55 }
56 #endif
57 
58 #ifndef landlock_restrict_self
59 static inline int landlock_restrict_self(const int ruleset_fd, const __u32 flags)
60 {
61  return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
62 }
63 #endif
64 
65 #ifndef LANDLOCK_ACCESS_FS_REFER
66 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
67 #endif
68 
69 #define _LANDLOCK_ACCESS_FS_WRITE \
70  (LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | \
71  LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_MAKE_CHAR | \
72  LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | \
73  LANDLOCK_ACCESS_FS_MAKE_SOCK | LANDLOCK_ACCESS_FS_MAKE_FIFO | \
74  LANDLOCK_ACCESS_FS_MAKE_BLOCK | LANDLOCK_ACCESS_FS_MAKE_SYM | \
75  LANDLOCK_ACCESS_FS_REFER)
76 
77 #define _LANDLOCK_ACCESS_FS_READ (LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR)
78 
79 #define _LANDLOCK_SURI_ACCESS_FS_WRITE \
80  (LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | \
81  LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_MAKE_SOCK)
82 
83 struct landlock_ruleset {
84  int fd;
85  struct landlock_ruleset_attr attr;
86 };
87 
88 static inline struct landlock_ruleset *LandlockCreateRuleset(void)
89 {
90  struct landlock_ruleset *ruleset = SCCalloc(1, sizeof(struct landlock_ruleset));
91  if (ruleset == NULL) {
92  SCLogError("Can't alloc landlock ruleset");
93  return NULL;
94  }
95 
96  ruleset->attr.handled_access_fs =
97  _LANDLOCK_ACCESS_FS_READ | _LANDLOCK_ACCESS_FS_WRITE | LANDLOCK_ACCESS_FS_EXECUTE;
98 
99  int abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
100  if (abi < 0) {
101  SCFree(ruleset);
102  return NULL;
103  }
104  if (abi < 2) {
106  SCLogError("Landlock disabled: need Linux 5.19+ for file store support");
107  SCFree(ruleset);
108  return NULL;
109  } else {
110  ruleset->attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
111  }
112  }
113 
114  ruleset->fd = landlock_create_ruleset(&ruleset->attr, sizeof(ruleset->attr), 0);
115  if (ruleset->fd < 0) {
116  SCFree(ruleset);
117  SCLogError("Can't create landlock ruleset");
118  return NULL;
119  }
120  return ruleset;
121 }
122 
123 static inline void LandlockEnforceRuleset(struct landlock_ruleset *ruleset)
124 {
125  if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
126  SCLogError("Can't self restrict (prctl phase): %s", strerror(errno));
127  return;
128  }
129  if (landlock_restrict_self(ruleset->fd, 0)) {
130  SCLogError("Can't self restrict (landlock phase): %s", strerror(errno));
131  }
132 }
133 
134 static int LandlockSandboxingAddRule(
135  struct landlock_ruleset *ruleset, const char *directory, uint64_t permission)
136 {
137  struct landlock_path_beneath_attr path_beneath = {
138  .allowed_access = permission & ruleset->attr.handled_access_fs,
139  };
140 
141  int dir_fd = open(directory, O_PATH | O_CLOEXEC | O_DIRECTORY);
142  if (dir_fd == -1) {
143  SCLogError("Can't open %s", directory);
144  return -1;
145  }
146  path_beneath.parent_fd = dir_fd;
147 
148  if (landlock_add_rule(ruleset->fd, LANDLOCK_RULE_PATH_BENEATH, &path_beneath, 0)) {
149  SCLogError("Can't add write rule: %s", strerror(errno));
150  close(dir_fd);
151  return -1;
152  }
153 
154  close(dir_fd);
155  return 0;
156 }
157 
158 static inline void LandlockSandboxingWritePath(
159  struct landlock_ruleset *ruleset, const char *directory)
160 {
161  if (LandlockSandboxingAddRule(ruleset, directory, _LANDLOCK_SURI_ACCESS_FS_WRITE) == 0) {
162  SCLogConfig("Added write permission to '%s'", directory);
163  }
164 }
165 
166 static inline void LandlockSandboxingReadPath(
167  struct landlock_ruleset *ruleset, const char *directory)
168 {
169  if (LandlockSandboxingAddRule(ruleset, directory, _LANDLOCK_ACCESS_FS_READ) == 0) {
170  SCLogConfig("Added read permission to '%s'", directory);
171  }
172 }
173 
174 void LandlockSandboxing(SCInstance *suri)
175 {
176  /* Read configuration variable and exit if no enforcement */
177  int conf_status;
178  if (ConfGetBool("security.landlock.enabled", &conf_status) == 0) {
179  conf_status = 0;
180  }
181  if (!conf_status) {
182  SCLogConfig("Landlock is not enabled in configuration");
183  return;
184  }
185  struct landlock_ruleset *ruleset = LandlockCreateRuleset();
186  if (ruleset == NULL) {
187  SCLogError("Kernel does not support Landlock");
188  return;
189  }
190 
191  LandlockSandboxingWritePath(ruleset, ConfigGetLogDirectory());
192  struct stat sb;
193  if (stat(ConfigGetDataDirectory(), &sb) == 0) {
194  LandlockSandboxingAddRule(ruleset, ConfigGetDataDirectory(),
195  _LANDLOCK_SURI_ACCESS_FS_WRITE | _LANDLOCK_ACCESS_FS_READ);
196  }
197  if (suri->run_mode == RUNMODE_PCAP_FILE) {
198  const char *pcap_file;
199  if (ConfGet("pcap-file.file", &pcap_file) == 1) {
200  char *file_name = SCStrdup(pcap_file);
201  if (file_name != NULL) {
202  struct stat statbuf;
203  if (stat(file_name, &statbuf) != -1) {
204  if (S_ISDIR(statbuf.st_mode)) {
205  LandlockSandboxingReadPath(ruleset, file_name);
206  } else {
207  LandlockSandboxingReadPath(ruleset, dirname(file_name));
208  }
209  } else {
210  SCLogError("Can't open pcap file");
211  }
212  SCFree(file_name);
213  }
214  }
215  }
216  if (suri->sig_file) {
217  char *file_name = SCStrdup(suri->sig_file);
218  if (file_name != NULL) {
219  LandlockSandboxingReadPath(ruleset, dirname(file_name));
220  SCFree(file_name);
221  }
222  }
223  if (suri->pid_filename) {
224  char *file_name = SCStrdup(suri->pid_filename);
225  if (file_name != NULL) {
226  LandlockSandboxingWritePath(ruleset, dirname(file_name));
227  SCFree(file_name);
228  }
229  }
230  if (ConfUnixSocketIsEnable()) {
231  const char *socketname;
232  if (ConfGet("unix-command.filename", &socketname) == 1) {
233  if (PathIsAbsolute(socketname)) {
234  char *file_name = SCStrdup(socketname);
235  if (file_name != NULL) {
236  LandlockSandboxingWritePath(ruleset, dirname(file_name));
237  SCFree(file_name);
238  }
239  } else {
240  LandlockSandboxingWritePath(ruleset, LOCAL_STATE_DIR "/run/suricata/");
241  }
242  } else {
243  LandlockSandboxingWritePath(ruleset, LOCAL_STATE_DIR "/run/suricata/");
244  }
245  }
246  if (!suri->sig_file_exclusive) {
247  const char *rule_path;
248  if (ConfGet("default-rule-path", &rule_path) == 1 && rule_path) {
249  LandlockSandboxingReadPath(ruleset, rule_path);
250  }
251  }
252 
253  ConfNode *read_dirs = ConfGetNode("security.landlock.directories.read");
254  if (read_dirs) {
255  if (!ConfNodeIsSequence(read_dirs)) {
256  SCLogWarning("Invalid security.landlock.directories.read configuration section: "
257  "expected a list of directory names.");
258  } else {
259  ConfNode *directory;
260  TAILQ_FOREACH (directory, &read_dirs->head, next) {
261  LandlockSandboxingReadPath(ruleset, directory->val);
262  }
263  }
264  }
265  ConfNode *write_dirs = ConfGetNode("security.landlock.directories.write");
266  if (write_dirs) {
267  if (!ConfNodeIsSequence(write_dirs)) {
268  SCLogWarning("Invalid security.landlock.directories.write configuration section: "
269  "expected a list of directory names.");
270  } else {
271  ConfNode *directory;
272  TAILQ_FOREACH (directory, &write_dirs->head, next) {
273  LandlockSandboxingWritePath(ruleset, directory->val);
274  }
275  }
276  }
277  LandlockEnforceRuleset(ruleset);
278  SCFree(ruleset);
279 }
280 
281 #endif /* HAVE_LINUX_LANDLOCK_H */
SCInstance_::run_mode
enum RunModes run_mode
Definition: suricata.h:124
ConfNode_::val
char * val
Definition: conf.h:34
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:482
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
ConfigGetDataDirectory
const char * ConfigGetDataDirectory(void)
Definition: util-conf.c:80
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
RequiresFeature
bool RequiresFeature(const char *feature_name)
Definition: feature.c:126
feature.h
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
util-landlock.h
util-mem.h
util-file.h
util-conf.h
FEATURE_OUTPUT_FILESTORE
#define FEATURE_OUTPUT_FILESTORE
Definition: feature.h:28
flags
uint8_t flags
Definition: decode-gre.h:0
util-path.h
PathIsAbsolute
int PathIsAbsolute(const char *path)
Check if a path is absolute.
Definition: util-path.c:44
ConfNodeIsSequence
int ConfNodeIsSequence(const ConfNode *node)
Check if a node is a sequence or node.
Definition: conf.c:911
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
SCInstance_::sig_file
char * sig_file
Definition: suricata.h:128
ConfUnixSocketIsEnable
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:136
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
ConfigGetLogDirectory
const char * ConfigGetLogDirectory(void)
Definition: util-conf.c:38
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
SCInstance_::pid_filename
char * pid_filename
Definition: suricata.h:130
suricata.h
SCInstance_
Definition: suricata.h:123
SCInstance_::sig_file_exclusive
bool sig_file_exclusive
Definition: suricata.h:129
LandlockSandboxing
void LandlockSandboxing(SCInstance *suri)
Definition: util-landlock.c:34
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30