0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define _GNU_SOURCE
0011 #include <errno.h>
0012 #include <fcntl.h>
0013 #include <linux/landlock.h>
0014 #include <linux/prctl.h>
0015 #include <stddef.h>
0016 #include <stdio.h>
0017 #include <stdlib.h>
0018 #include <string.h>
0019 #include <sys/prctl.h>
0020 #include <sys/stat.h>
0021 #include <sys/syscall.h>
0022 #include <unistd.h>
0023
0024 #ifndef landlock_create_ruleset
0025 static inline int
0026 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
0027 const size_t size, const __u32 flags)
0028 {
0029 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
0030 }
0031 #endif
0032
0033 #ifndef landlock_add_rule
0034 static inline int landlock_add_rule(const int ruleset_fd,
0035 const enum landlock_rule_type rule_type,
0036 const void *const rule_attr,
0037 const __u32 flags)
0038 {
0039 return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
0040 flags);
0041 }
0042 #endif
0043
0044 #ifndef landlock_restrict_self
0045 static inline int landlock_restrict_self(const int ruleset_fd,
0046 const __u32 flags)
0047 {
0048 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
0049 }
0050 #endif
0051
0052 #define ENV_FS_RO_NAME "LL_FS_RO"
0053 #define ENV_FS_RW_NAME "LL_FS_RW"
0054 #define ENV_PATH_TOKEN ":"
0055
0056 static int parse_path(char *env_path, const char ***const path_list)
0057 {
0058 int i, num_paths = 0;
0059
0060 if (env_path) {
0061 num_paths++;
0062 for (i = 0; env_path[i]; i++) {
0063 if (env_path[i] == ENV_PATH_TOKEN[0])
0064 num_paths++;
0065 }
0066 }
0067 *path_list = malloc(num_paths * sizeof(**path_list));
0068 for (i = 0; i < num_paths; i++)
0069 (*path_list)[i] = strsep(&env_path, ENV_PATH_TOKEN);
0070
0071 return num_paths;
0072 }
0073
0074
0075
0076 #define ACCESS_FILE ( \
0077 LANDLOCK_ACCESS_FS_EXECUTE | \
0078 LANDLOCK_ACCESS_FS_WRITE_FILE | \
0079 LANDLOCK_ACCESS_FS_READ_FILE)
0080
0081
0082
0083 static int populate_ruleset(const char *const env_var, const int ruleset_fd,
0084 const __u64 allowed_access)
0085 {
0086 int num_paths, i, ret = 1;
0087 char *env_path_name;
0088 const char **path_list = NULL;
0089 struct landlock_path_beneath_attr path_beneath = {
0090 .parent_fd = -1,
0091 };
0092
0093 env_path_name = getenv(env_var);
0094 if (!env_path_name) {
0095
0096 fprintf(stderr, "Missing environment variable %s\n", env_var);
0097 return 1;
0098 }
0099 env_path_name = strdup(env_path_name);
0100 unsetenv(env_var);
0101 num_paths = parse_path(env_path_name, &path_list);
0102 if (num_paths == 1 && path_list[0][0] == '\0') {
0103
0104
0105
0106
0107 ret = 0;
0108 goto out_free_name;
0109 }
0110
0111 for (i = 0; i < num_paths; i++) {
0112 struct stat statbuf;
0113
0114 path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
0115 if (path_beneath.parent_fd < 0) {
0116 fprintf(stderr, "Failed to open \"%s\": %s\n",
0117 path_list[i], strerror(errno));
0118 goto out_free_name;
0119 }
0120 if (fstat(path_beneath.parent_fd, &statbuf)) {
0121 close(path_beneath.parent_fd);
0122 goto out_free_name;
0123 }
0124 path_beneath.allowed_access = allowed_access;
0125 if (!S_ISDIR(statbuf.st_mode))
0126 path_beneath.allowed_access &= ACCESS_FILE;
0127 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
0128 &path_beneath, 0)) {
0129 fprintf(stderr,
0130 "Failed to update the ruleset with \"%s\": %s\n",
0131 path_list[i], strerror(errno));
0132 close(path_beneath.parent_fd);
0133 goto out_free_name;
0134 }
0135 close(path_beneath.parent_fd);
0136 }
0137 ret = 0;
0138
0139 out_free_name:
0140 free(path_list);
0141 free(env_path_name);
0142 return ret;
0143 }
0144
0145
0146
0147 #define ACCESS_FS_ROUGHLY_READ ( \
0148 LANDLOCK_ACCESS_FS_EXECUTE | \
0149 LANDLOCK_ACCESS_FS_READ_FILE | \
0150 LANDLOCK_ACCESS_FS_READ_DIR)
0151
0152 #define ACCESS_FS_ROUGHLY_WRITE ( \
0153 LANDLOCK_ACCESS_FS_WRITE_FILE | \
0154 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
0155 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
0156 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
0157 LANDLOCK_ACCESS_FS_MAKE_DIR | \
0158 LANDLOCK_ACCESS_FS_MAKE_REG | \
0159 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
0160 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
0161 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
0162 LANDLOCK_ACCESS_FS_MAKE_SYM | \
0163 LANDLOCK_ACCESS_FS_REFER)
0164
0165 #define ACCESS_ABI_2 ( \
0166 LANDLOCK_ACCESS_FS_REFER)
0167
0168
0169
0170 int main(const int argc, char *const argv[], char *const *const envp)
0171 {
0172 const char *cmd_path;
0173 char *const *cmd_argv;
0174 int ruleset_fd, abi;
0175 __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
0176 access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
0177 struct landlock_ruleset_attr ruleset_attr = {
0178 .handled_access_fs = access_fs_rw,
0179 };
0180
0181 if (argc < 2) {
0182 fprintf(stderr,
0183 "usage: %s=\"...\" %s=\"...\" %s <cmd> [args]...\n\n",
0184 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
0185 fprintf(stderr,
0186 "Launch a command in a restricted environment.\n\n");
0187 fprintf(stderr, "Environment variables containing paths, "
0188 "each separated by a colon:\n");
0189 fprintf(stderr,
0190 "* %s: list of paths allowed to be used in a read-only way.\n",
0191 ENV_FS_RO_NAME);
0192 fprintf(stderr,
0193 "* %s: list of paths allowed to be used in a read-write way.\n",
0194 ENV_FS_RW_NAME);
0195 fprintf(stderr,
0196 "\nexample:\n"
0197 "%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" "
0198 "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
0199 "%s bash -i\n",
0200 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
0201 return 1;
0202 }
0203
0204 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
0205 if (abi < 0) {
0206 const int err = errno;
0207
0208 perror("Failed to check Landlock compatibility");
0209 switch (err) {
0210 case ENOSYS:
0211 fprintf(stderr,
0212 "Hint: Landlock is not supported by the current kernel. "
0213 "To support it, build the kernel with "
0214 "CONFIG_SECURITY_LANDLOCK=y and prepend "
0215 "\"landlock,\" to the content of CONFIG_LSM.\n");
0216 break;
0217 case EOPNOTSUPP:
0218 fprintf(stderr,
0219 "Hint: Landlock is currently disabled. "
0220 "It can be enabled in the kernel configuration by "
0221 "prepending \"landlock,\" to the content of CONFIG_LSM, "
0222 "or at boot time by setting the same content to the "
0223 "\"lsm\" kernel parameter.\n");
0224 break;
0225 }
0226 return 1;
0227 }
0228
0229 if (abi < 2) {
0230 ruleset_attr.handled_access_fs &= ~ACCESS_ABI_2;
0231 access_fs_ro &= ~ACCESS_ABI_2;
0232 access_fs_rw &= ~ACCESS_ABI_2;
0233 }
0234
0235 ruleset_fd =
0236 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
0237 if (ruleset_fd < 0) {
0238 perror("Failed to create a ruleset");
0239 return 1;
0240 }
0241 if (populate_ruleset(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
0242 goto err_close_ruleset;
0243 }
0244 if (populate_ruleset(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
0245 goto err_close_ruleset;
0246 }
0247 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
0248 perror("Failed to restrict privileges");
0249 goto err_close_ruleset;
0250 }
0251 if (landlock_restrict_self(ruleset_fd, 0)) {
0252 perror("Failed to enforce ruleset");
0253 goto err_close_ruleset;
0254 }
0255 close(ruleset_fd);
0256
0257 cmd_path = argv[1];
0258 cmd_argv = argv + 1;
0259 execvpe(cmd_path, cmd_argv, envp);
0260 fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
0261 strerror(errno));
0262 fprintf(stderr, "Hint: access to the binary, the interpreter or "
0263 "shared libraries may be denied.\n");
0264 return 1;
0265
0266 err_close_ruleset:
0267 close(ruleset_fd);
0268 return 1;
0269 }