-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
273 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ | ||
/* | ||
* Landlock - User space API | ||
* | ||
* Copyright © 2017-2020 Mickaël Salaün <[email protected]> | ||
* Copyright © 2018-2020 ANSSI | ||
*/ | ||
#if defined(_LINUX_LANDLOCK_H) | ||
#elif __has_include(<linux/landlock.h>) | ||
#include <linux/landlock.h> | ||
#else | ||
#include <linux/types.h> | ||
struct landlock_ruleset_attr { | ||
__u64 handled_access_fs; | ||
}; | ||
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0) | ||
enum landlock_rule_type { | ||
LANDLOCK_RULE_PATH_BENEATH = 1, | ||
}; | ||
struct landlock_path_beneath_attr { | ||
__u64 allowed_access; | ||
__s32 parent_fd; | ||
} __attribute__((packed)); | ||
#define LANDLOCK_ACCESS_FS_EXECUTE (1ULL << 0) | ||
#define LANDLOCK_ACCESS_FS_WRITE_FILE (1ULL << 1) | ||
#define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2) | ||
#define LANDLOCK_ACCESS_FS_READ_DIR (1ULL << 3) | ||
#define LANDLOCK_ACCESS_FS_REMOVE_DIR (1ULL << 4) | ||
#define LANDLOCK_ACCESS_FS_REMOVE_FILE (1ULL << 5) | ||
#define LANDLOCK_ACCESS_FS_MAKE_CHAR (1ULL << 6) | ||
#define LANDLOCK_ACCESS_FS_MAKE_DIR (1ULL << 7) | ||
#define LANDLOCK_ACCESS_FS_MAKE_REG (1ULL << 8) | ||
#define LANDLOCK_ACCESS_FS_MAKE_SOCK (1ULL << 9) | ||
#define LANDLOCK_ACCESS_FS_MAKE_FIFO (1ULL << 10) | ||
#define LANDLOCK_ACCESS_FS_MAKE_BLOCK (1ULL << 11) | ||
#define LANDLOCK_ACCESS_FS_MAKE_SYM (1ULL << 12) | ||
#endif /* _LINUX_LANDLOCK_H */ | ||
|
||
#include <sys/syscall.h> | ||
#ifndef __NR_landlock_create_ruleset | ||
#define __NR_landlock_create_ruleset 444 | ||
#endif | ||
#ifndef __NR_landlock_add_rule | ||
#define __NR_landlock_add_rule 445 | ||
#endif | ||
#ifndef __NR_landlock_restrict_self | ||
#define __NR_landlock_restrict_self 446 | ||
#endif | ||
|
||
#include <cstddef> | ||
#include <unistd.h> | ||
#ifndef landlock_create_ruleset | ||
static inline int | ||
landlock_create_ruleset(const struct landlock_ruleset_attr *const attr, | ||
const size_t size, const __u32 flags) { | ||
return syscall(__NR_landlock_create_ruleset, attr, size, flags); | ||
} | ||
#endif | ||
#ifndef landlock_add_rule | ||
static inline int landlock_add_rule(const int ruleset_fd, | ||
const enum landlock_rule_type rule_type, | ||
const void *const rule_attr, | ||
const __u32 flags) { | ||
return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr, | ||
flags); | ||
} | ||
#endif | ||
#ifndef landlock_restrict_self | ||
static inline int landlock_restrict_self(const int ruleset_fd, | ||
const __u32 flags) { | ||
return syscall(__NR_landlock_restrict_self, ruleset_fd, flags); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef __FreeBSD__ | ||
|
||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "landlock_header.h" | ||
#include "landlock_helpers.h" | ||
|
||
int landlock_add_rules(const int ruleset_fd, const char *const *const paths, | ||
__u64 access_rule) { | ||
struct landlock_path_beneath_attr path_beneath = { | ||
.allowed_access = access_rule, | ||
.parent_fd = -1, | ||
}; | ||
for (const char *const *pathptr = paths; *pathptr; pathptr++) { | ||
path_beneath.parent_fd = open(*pathptr, O_PATH | O_CLOEXEC); | ||
if (path_beneath.parent_fd < 0) { | ||
if (errno == ENOENT) | ||
goto close_fd; // missing files are ignored | ||
fprintf(stderr, "Failed to open path '%s' for rule: %s\n", *pathptr, | ||
strerror(errno)); | ||
return -1; | ||
} | ||
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, | ||
&path_beneath, 0)) { | ||
fprintf(stderr, "Failed to add rule '%s' to ruleset: %s\n", | ||
*pathptr, strerror(errno)); | ||
return -1; | ||
} | ||
close_fd: | ||
close(path_beneath.parent_fd); | ||
} | ||
return 0; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <linux/types.h> | ||
|
||
int landlock_add_rules(const int ruleset_fd, const char *const *const paths, | ||
__u64 access_rule); |
Oops, something went wrong.