blob: f4ee253ea89b247cb224b67b73c448a65e71530d [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "base.h"
#include "memory.h"
#include "supervisor.h"
#include "sysctl.h"
#define SYSCTL_NAME "max_cell_id_blocks"
#define SYSCTL_PATH "/proc/sys/user/" SYSCTL_NAME
#define SYSCTL_MAX_WRITE 32
static int create_memfd(void)
{
int fd = memfd_create(SYSCTL_NAME, MFD_CLOEXEC | MFD_ALLOW_SEALING);
return fd < 0 ? -errno : fd;
}
static int seal_memfd(int fd)
{
if (fcntl(fd, F_ADD_SEALS,
F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL) < 0)
return -errno;
return 0;
}
static int reopen_fd(int fd, int flags)
{
char path[sizeof("/proc/self/fd/") + 10];
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
fd = open(path, flags | O_CLOEXEC);
return fd < 0 ? -errno : fd;
}
static int write_all(int fd, const char *data, size_t len)
{
while (len) {
ssize_t n = write(fd, data, len);
if (n < 0) {
if (errno == EINTR)
continue;
return -errno;
}
if (!n)
return -EIO;
data += n;
len -= (size_t)n;
}
return 0;
}
static int require_root(uint32_t pid)
{
struct caller_cred cred;
int ret;
ret = caller_cred_from_pid(pid, &cred);
if (ret < 0)
return ret;
return cred.fsuid ? -EACCES : 0;
}
static bool is_sysctl_path(int listener, uint64_t id, uint32_t pid,
uint64_t addr)
{
char path[sizeof(SYSCTL_PATH)];
return !read_tracee_cstring(listener, id, pid, addr, path,
sizeof(path)) &&
!strcmp(path, SYSCTL_PATH);
}
int sysctl_create(struct sysctl *sysctl)
{
int fd, ret;
sysctl->writer_fd = -1;
fd = create_memfd();
if (fd < 0)
return err_ctx(fd, "memfd_create");
ret = file_identity_of_fd(fd, &sysctl->writer_id);
if (ret < 0) {
err_ctx(ret, "fstat handle");
goto close_writer;
}
if (fchmod(fd, 0644) < 0) {
ret = err_ctx(-errno, "fchmod");
goto close_writer;
}
ret = seal_memfd(fd);
if (ret < 0) {
err_ctx(ret, "F_ADD_SEALS");
goto close_writer;
}
sysctl->writer_fd = fd;
return 0;
close_writer:
close_fd(fd);
return ret;
}
void sysctl_destroy(struct sysctl *sysctl)
{
close_fd(take_fd(&sysctl->writer_fd));
}
static int open_snapshot(uint16_t quota)
{
char data[sizeof("65535\n")];
int snapshot, ret;
snapshot = create_memfd();
if (snapshot < 0)
return snapshot;
snprintf(data, sizeof(data), "%u\n", (unsigned int)quota);
ret = write_all(snapshot, data, strlen(data));
if (ret < 0)
goto close_snapshot;
ret = seal_memfd(snapshot);
if (ret < 0)
goto close_snapshot;
ret = reopen_fd(snapshot, O_RDONLY);
close_snapshot:
close_fd(snapshot);
return ret;
}
static int sysctl_open(const struct sysctl *sysctl, uint32_t pid, int flags,
uint16_t quota, struct dispatch_outcome *out)
{
int fd, ret;
if (flags & O_DIRECTORY)
return -ENOTDIR;
if ((flags & O_PATH) == O_PATH)
return -EOPNOTSUPP;
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
return -EEXIST;
switch (flags & O_ACCMODE) {
case O_RDONLY:
fd = open_snapshot(quota);
break;
case O_WRONLY:
ret = require_root(pid);
if (ret < 0)
return ret;
fd = reopen_fd(sysctl->writer_fd, O_WRONLY);
break;
default:
return -EOPNOTSUPP;
}
if (fd < 0)
return fd;
*out = (struct dispatch_outcome){
.type = DISPATCH_SEND_FD,
.fd = fd,
.cloexec = !!(flags & O_CLOEXEC),
};
return 0;
}
static int sysctl_stat(const struct sysctl *sysctl, int listener, uint64_t id,
uint32_t pid, uint64_t addr,
struct dispatch_outcome *out)
{
struct stat st;
int ret;
memset(&st, 0, sizeof(st));
if (fstat(sysctl->writer_fd, &st) < 0)
return -errno;
ret = write_tracee_bytes(listener, id, pid, addr, &st, sizeof(st));
if (ret < 0)
return ret;
*out = (struct dispatch_outcome){.type = DISPATCH_VALUE, .value = 0};
return 0;
}
static bool quota_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
}
static int parse_quota(const char *start, size_t len, uint16_t *quota)
{
const char *end = start + len;
unsigned int value = 0;
while (start < end && quota_whitespace(*start))
start++;
while (start < end && quota_whitespace(end[-1]))
end--;
if (start < end && *start == '+')
start++;
if (start == end)
return -EINVAL;
for (; start < end; start++) {
if (*start < '0' || *start > '9')
return -EINVAL;
value = value * 10 + (*start - '0');
if (value > ID_TOTAL_BLOCKS)
return -EINVAL;
}
*quota = (uint16_t)value;
return 0;
}
static int sysctl_write(const struct sysctl *sysctl, int listener,
const struct seccomp_notif *notif, uint16_t *quota,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
uint32_t raw_fd = (uint32_t)args[0];
struct file_identity file_id;
char bytes[SYSCTL_MAX_WRITE];
uint64_t count = args[2];
int fd, flags, ret;
if (raw_fd > INT32_MAX)
return 0;
fd = dup_tracee_fd(listener, notif->id, notif->pid, (int)raw_fd);
if (fd < 0)
return 0;
ret = file_identity_of_fd(fd, &file_id);
if (ret < 0 || !file_identity_equal(&file_id, &sysctl->writer_id))
goto close_file;
flags = fcntl(fd, F_GETFL);
if (flags < 0) {
ret = -errno;
goto close_file;
}
if ((flags & O_ACCMODE) != O_WRONLY) {
ret = -EBADF;
goto close_file;
}
ret = require_root(notif->pid);
if (ret < 0)
goto close_file;
if (count > sizeof(bytes)) {
ret = -EINVAL;
goto close_file;
}
if (!count)
goto close_file;
ret = read_tracee_bytes(listener, notif->id, notif->pid, args[1], bytes,
(size_t)count);
if (ret < 0)
goto close_file;
ret = parse_quota(bytes, (size_t)count, quota);
if (!ret)
*out = (struct dispatch_outcome){
.type = DISPATCH_VALUE,
.value = (int64_t)count,
};
close_file:
close_fd(fd);
return ret;
}
int sysctl_dispatch(const struct sysctl *sysctl, int listener,
const struct seccomp_notif *notif, uint16_t *quota,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
uint64_t id = notif->id;
uint32_t pid = notif->pid;
*out = (struct dispatch_outcome){.type = DISPATCH_CONTINUE};
switch (notif->data.nr) {
case __NR_openat:
if (is_sysctl_path(listener, id, pid, args[1]))
return sysctl_open(sysctl, pid, (int)args[2], *quota,
out);
break;
#if defined(__x86_64__)
case __NR_open:
if (is_sysctl_path(listener, id, pid, args[0]))
return sysctl_open(sysctl, pid, (int)args[1], *quota,
out);
break;
#endif
case __NR_newfstatat:
if (is_sysctl_path(listener, id, pid, args[1]))
return sysctl_stat(sysctl, listener, id, pid, args[2],
out);
break;
#if defined(__x86_64__)
case __NR_stat:
case __NR_lstat:
if (is_sysctl_path(listener, id, pid, args[0]))
return sysctl_stat(sysctl, listener, id, pid, args[1],
out);
break;
#endif
/* procps expects EINVAL for readlink on a regular file. */
case __NR_readlinkat:
if (is_sysctl_path(listener, id, pid, args[1]))
return -EINVAL;
break;
#if defined(__x86_64__)
case __NR_readlink:
if (is_sysctl_path(listener, id, pid, args[0]))
return -EINVAL;
break;
#endif
case __NR_write:
return sysctl_write(sysctl, listener, notif, quota, out);
}
return 0;
}