2022-07-18 09:12:42 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/calls/calls.h"
|
2022-07-18 14:23:15 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-07-18 09:12:42 +00:00
|
|
|
#include "libc/calls/landlock.h"
|
|
|
|
#include "libc/calls/strace.internal.h"
|
|
|
|
#include "libc/calls/struct/stat.h"
|
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
|
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
|
|
|
#include "libc/errno.h"
|
2022-07-19 04:05:46 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-07-18 09:12:42 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-07-18 14:23:15 +00:00
|
|
|
#include "libc/nexgen32e/threaded.h"
|
|
|
|
#include "libc/runtime/internal.h"
|
2022-07-18 09:12:42 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-07-18 14:23:15 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
2022-07-18 09:11:06 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
2022-07-19 04:05:46 +00:00
|
|
|
#include "libc/sysv/consts/fd.h"
|
2022-07-18 09:12:42 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/pr.h"
|
|
|
|
#include "libc/sysv/consts/s.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-07-18 15:26:40 +00:00
|
|
|
#define UNVEIL_READ \
|
|
|
|
(LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | \
|
|
|
|
LANDLOCK_ACCESS_FS_REFER)
|
|
|
|
#define UNVEIL_WRITE (LANDLOCK_ACCESS_FS_WRITE_FILE)
|
|
|
|
#define UNVEIL_EXEC (LANDLOCK_ACCESS_FS_EXECUTE)
|
|
|
|
#define UNVEIL_CREATE \
|
|
|
|
(LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | \
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SOCK | \
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
|
|
|
|
LANDLOCK_ACCESS_FS_MAKE_SYM)
|
|
|
|
|
2022-07-18 09:12:42 +00:00
|
|
|
#define FILE_BITS \
|
|
|
|
(LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE | \
|
|
|
|
LANDLOCK_ACCESS_FS_EXECUTE)
|
|
|
|
|
2022-07-18 15:26:40 +00:00
|
|
|
/**
|
2022-07-18 09:12:42 +00:00
|
|
|
* Long living state for landlock calls.
|
2022-07-18 15:26:40 +00:00
|
|
|
* fs_mask is set to use all the access rights from the latest landlock ABI.
|
|
|
|
* On init, the current supported abi is checked and unavailable rights are
|
|
|
|
* masked off.
|
|
|
|
*
|
2022-07-18 09:12:42 +00:00
|
|
|
* As of 5.19, the latest abi is v2.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - Integrate with pledge and remove the file access?
|
|
|
|
* - Stuff state into the .protected section?
|
|
|
|
*/
|
2022-07-18 09:11:06 +00:00
|
|
|
_Thread_local static struct {
|
2022-07-18 15:26:40 +00:00
|
|
|
uint64_t fs_mask;
|
2022-07-18 09:12:42 +00:00
|
|
|
int fd;
|
|
|
|
} State = {
|
2022-07-18 15:26:40 +00:00
|
|
|
.fs_mask = UNVEIL_READ | UNVEIL_WRITE | UNVEIL_EXEC | UNVEIL_CREATE,
|
|
|
|
.fd = 0,
|
2022-07-18 09:12:42 +00:00
|
|
|
};
|
|
|
|
|
2022-07-18 09:11:06 +00:00
|
|
|
static int unveil_final(void) {
|
2022-07-18 09:12:42 +00:00
|
|
|
int rc;
|
2022-07-18 15:26:40 +00:00
|
|
|
if (State.fd == -1) return eperm();
|
2022-07-18 09:12:42 +00:00
|
|
|
if ((rc = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) != -1 &&
|
|
|
|
(rc = landlock_restrict_self(State.fd, 0)) != -1 &&
|
2022-07-18 09:11:06 +00:00
|
|
|
(rc = sys_close(State.fd)) != -1)
|
2022-07-18 09:12:42 +00:00
|
|
|
State.fd = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-07-18 09:11:06 +00:00
|
|
|
static int err_close(int rc, int fd) {
|
|
|
|
int serrno = errno;
|
|
|
|
sys_close(fd);
|
|
|
|
errno = serrno;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unveil_init(void) {
|
|
|
|
int rc, fd;
|
2022-07-19 04:05:46 +00:00
|
|
|
if ((rc = landlock_create_ruleset(0, 0, LANDLOCK_CREATE_RULESET_VERSION)) <
|
|
|
|
0) {
|
2022-07-18 15:26:40 +00:00
|
|
|
if (errno == EOPNOTSUPP) errno = ENOSYS;
|
2022-07-18 09:12:42 +00:00
|
|
|
return -1;
|
2022-07-18 15:26:40 +00:00
|
|
|
}
|
|
|
|
if (rc < 2) State.fs_mask &= ~LANDLOCK_ACCESS_FS_REFER;
|
2022-07-18 09:12:42 +00:00
|
|
|
const struct landlock_ruleset_attr attr = {
|
2022-07-18 15:26:40 +00:00
|
|
|
.handled_access_fs = State.fs_mask,
|
2022-07-18 09:12:42 +00:00
|
|
|
};
|
2022-07-19 04:05:46 +00:00
|
|
|
// [undocumented] landlock_create_ruleset() always returns o_cloexec
|
|
|
|
// assert(__sys_fcntl(rc, F_GETFD, 0) == FD_CLOEXEC);
|
2022-07-18 09:12:42 +00:00
|
|
|
if ((rc = landlock_create_ruleset(&attr, sizeof(attr), 0)) < 0) return -1;
|
2022-07-18 09:11:06 +00:00
|
|
|
// grant file descriptor a higher number that's less likely to interfere
|
2022-07-19 04:05:46 +00:00
|
|
|
if ((fd = __sys_fcntl(rc, F_DUPFD_CLOEXEC, 100)) == -1) {
|
|
|
|
return err_close(-1, rc);
|
|
|
|
}
|
|
|
|
if (sys_close(rc) == -1) {
|
|
|
|
return err_close(-1, fd);
|
|
|
|
}
|
2022-07-18 09:11:06 +00:00
|
|
|
State.fd = fd;
|
2022-07-18 09:12:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sys_unveil_linux(const char *path, const char *permissions) {
|
|
|
|
int rc;
|
2022-07-18 09:11:06 +00:00
|
|
|
if (!State.fd && (rc = unveil_init()) == -1) return rc;
|
2022-07-18 15:26:40 +00:00
|
|
|
if ((path && !permissions) || (!path && permissions)) return einval();
|
2022-07-18 09:11:06 +00:00
|
|
|
if (!path && !permissions) return unveil_final();
|
2022-07-18 09:12:42 +00:00
|
|
|
struct landlock_path_beneath_attr pb = {0};
|
|
|
|
for (const char *c = permissions; *c != '\0'; c++) {
|
|
|
|
switch (*c) {
|
2022-07-18 15:26:40 +00:00
|
|
|
// clang-format off
|
|
|
|
case 'r': pb.allowed_access |= UNVEIL_READ; break;
|
|
|
|
case 'w': pb.allowed_access |= UNVEIL_WRITE; break;
|
|
|
|
case 'x': pb.allowed_access |= UNVEIL_EXEC; break;
|
|
|
|
case 'c': pb.allowed_access |= UNVEIL_CREATE; break;
|
|
|
|
default: return einval();
|
|
|
|
// clang-format on
|
2022-07-18 09:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 15:26:40 +00:00
|
|
|
pb.allowed_access &= State.fs_mask;
|
2022-07-18 09:11:06 +00:00
|
|
|
if ((rc = sys_open(path, O_PATH | O_CLOEXEC, 0)) == -1) return rc;
|
2022-07-18 09:12:42 +00:00
|
|
|
pb.parent_fd = rc;
|
|
|
|
struct stat st;
|
2022-07-18 14:23:15 +00:00
|
|
|
if ((rc = sys_fstat(pb.parent_fd, &st)) == -1) {
|
|
|
|
return err_close(rc, pb.parent_fd);
|
|
|
|
}
|
2022-07-18 09:12:42 +00:00
|
|
|
if (!S_ISDIR(st.st_mode)) pb.allowed_access &= FILE_BITS;
|
2022-07-18 14:23:15 +00:00
|
|
|
if ((rc = landlock_add_rule(State.fd, LANDLOCK_RULE_PATH_BENEATH, &pb, 0))) {
|
2022-07-18 09:12:42 +00:00
|
|
|
return err_close(rc, pb.parent_fd);
|
2022-07-18 14:23:15 +00:00
|
|
|
}
|
2022-07-18 09:11:06 +00:00
|
|
|
sys_close(pb.parent_fd);
|
2022-07-18 09:12:42 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-07-18 09:11:06 +00:00
|
|
|
/**
|
2022-07-18 15:26:40 +00:00
|
|
|
* Restricts filesystem operations, e.g.
|
|
|
|
*
|
|
|
|
* unveil("/etc", "r");
|
|
|
|
*
|
|
|
|
* Unveiling restricts the visibility of the filesystem to a set of allowed
|
|
|
|
* paths with specific operations. This system call is supported natively on
|
|
|
|
* OpenBSD and polyfilled on Linux using the Landlock LSM[1].
|
|
|
|
*
|
|
|
|
* On OpenBSD, accessing paths outside of the allowed set raises ENOENT, and
|
|
|
|
* accessing ones with incorrect permissions raises EACCES. On Linux, both these
|
|
|
|
* cases raise EACCES.
|
|
|
|
*
|
|
|
|
* Using unveil is irreversible. On OpenBSD, the first call immediately enforces
|
|
|
|
* the filesystem visibilty, and existing paths can only be updated with equal
|
|
|
|
* or lesser permissions. Filesystem operations that try to access invisible
|
|
|
|
* paths will raise ENOENT, and operations without the correct permissions raise
|
|
|
|
* EACCES. Unveiling can be disabled by either passing two NULL arguments or by
|
|
|
|
* calling pledge() without the "unveil" promise.
|
|
|
|
*
|
|
|
|
* Landlock is more permissive than OpenBSD's unveil. Filesystem visibility is
|
|
|
|
* only enforced after disabling, and path permissions can be increased at any
|
|
|
|
* time. Finally, both accessing invisible paths or ones with incorrect
|
|
|
|
* permissions will raise EACCES.
|
|
|
|
*
|
|
|
|
* `permissions` is a string consisting of zero or more of the following
|
|
|
|
* characters:
|
|
|
|
*
|
|
|
|
* - 'r' makes `path` available for read-only path operations, corresponding to
|
|
|
|
* the pledge promise "rpath".
|
|
|
|
* - `w` makes `path` available for write operations, corresponding to the
|
|
|
|
* pledge promise "wpath".
|
|
|
|
* - `x` makes `path` available for execute operations, corresponding to the
|
|
|
|
* pledge promises "exec" and "execnative".
|
|
|
|
* - `c` allows `path` to be created and removed, corresponding to the pledge
|
|
|
|
* promise "cpath".
|
|
|
|
*
|
|
|
|
* [1] https://docs.kernel.org/userspace-api/landlock.html
|
|
|
|
*
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
* @raise ENOSYS if host os isn't Linux or OpenBSD
|
|
|
|
* @raise ENOSYS if Landlock isn't supported on this kernel
|
|
|
|
* @raise EINVAL if one argument is set and the other is not
|
|
|
|
* @raise EINVAL if an invalid character in `permissions` was found
|
2022-07-18 09:11:06 +00:00
|
|
|
*/
|
2022-07-18 09:12:42 +00:00
|
|
|
int unveil(const char *path, const char *permissions) {
|
|
|
|
int rc;
|
|
|
|
if (IsLinux()) {
|
|
|
|
rc = sys_unveil_linux(path, permissions);
|
|
|
|
} else {
|
|
|
|
rc = sys_unveil(path, permissions);
|
|
|
|
}
|
|
|
|
STRACE("unveil(%#s, %#s) → %d% m", path, permissions, rc);
|
|
|
|
return rc;
|
|
|
|
}
|