2012-04-04 01:27:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Will Drewry <wad@dataspill.org>
|
2023-02-03 05:33:09 +00:00
|
|
|
* Copyright (c) 2015,2017,2019,2020,2023 Damien Miller <djm@mindrot.org>
|
2012-04-04 01:27:54 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uncomment the SANDBOX_SECCOMP_FILTER_DEBUG macro below to help diagnose
|
|
|
|
* filter breakage during development. *Do not* use this in production,
|
|
|
|
* as it relies on making library calls that are unsafe in signal context.
|
|
|
|
*
|
|
|
|
* Instead, live systems the auditctl(8) may be used to monitor failures.
|
|
|
|
* E.g.
|
|
|
|
* auditctl -a task,always -F uid=<privsep uid>
|
|
|
|
*/
|
2022-12-07 07:38:25 +00:00
|
|
|
/* #define SANDBOX_SECCOMP_FILTER_DEBUG 1 */
|
2014-03-17 03:45:56 +00:00
|
|
|
|
2022-08-10 22:58:28 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* For older toolchains, it may be necessary to use the kernel
|
|
|
|
* headers directly.
|
|
|
|
*/
|
2012-04-04 01:27:54 +00:00
|
|
|
#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
|
|
|
|
# include <asm/siginfo.h>
|
|
|
|
# define __have_siginfo_t 1
|
|
|
|
# define __have_sigval_t 1
|
|
|
|
# define __have_sigevent_t 1
|
|
|
|
#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
|
2022-08-10 22:58:28 +00:00
|
|
|
#endif
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#ifdef SANDBOX_SECCOMP_FILTER
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/prctl.h>
|
2019-08-23 00:08:48 +00:00
|
|
|
#include <sys/mman.h>
|
2020-04-15 00:58:02 +00:00
|
|
|
#include <sys/syscall.h>
|
2012-04-04 01:27:54 +00:00
|
|
|
|
2023-02-03 05:33:09 +00:00
|
|
|
#include <linux/futex.h>
|
2015-06-17 00:50:51 +00:00
|
|
|
#include <linux/net.h>
|
2012-04-04 01:27:54 +00:00
|
|
|
#include <linux/audit.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/seccomp.h>
|
2013-02-22 00:37:00 +00:00
|
|
|
#include <elf.h>
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
#include <asm/unistd.h>
|
2017-03-22 01:43:02 +00:00
|
|
|
#ifdef __s390__
|
|
|
|
#include <asm/zcrypt.h>
|
|
|
|
#endif
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h> /* for offsetof */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "ssh-sandbox.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
|
|
|
|
/* Linux seccomp_filter sandbox */
|
|
|
|
#define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL
|
|
|
|
|
|
|
|
/* Use a signal handler to emit violations when debugging */
|
|
|
|
#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
|
|
|
|
# undef SECCOMP_FILTER_FAIL
|
|
|
|
# define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
|
|
|
|
#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
|
|
|
|
|
2017-03-14 01:24:47 +00:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
# define ARG_LO_OFFSET 0
|
|
|
|
# define ARG_HI_OFFSET sizeof(uint32_t)
|
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
# define ARG_LO_OFFSET sizeof(uint32_t)
|
|
|
|
# define ARG_HI_OFFSET 0
|
|
|
|
#else
|
|
|
|
#error "Unknown endianness"
|
|
|
|
#endif
|
|
|
|
|
2012-04-04 01:27:54 +00:00
|
|
|
/* Simple helpers to avoid manual errors (but larger BPF programs). */
|
|
|
|
#define SC_DENY(_nr, _errno) \
|
2017-03-14 06:48:43 +00:00
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
|
2012-04-04 01:27:54 +00:00
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
|
|
|
|
#define SC_ALLOW(_nr) \
|
2017-03-14 06:48:43 +00:00
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
|
2012-04-04 01:27:54 +00:00
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
|
2015-06-17 00:50:51 +00:00
|
|
|
#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
|
2017-03-14 06:48:43 +00:00
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 6), \
|
2019-08-23 00:08:48 +00:00
|
|
|
/* load and test syscall argument, low word */ \
|
2017-03-14 01:24:47 +00:00
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
|
|
|
|
((_arg_val) & 0xFFFFFFFF), 0, 3), \
|
2019-08-23 00:08:48 +00:00
|
|
|
/* load and test syscall argument, high word */ \
|
2015-06-17 00:50:51 +00:00
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
2017-03-14 01:24:47 +00:00
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
|
|
|
|
(((uint32_t)((uint64_t)(_arg_val) >> 32)) & 0xFFFFFFFF), 0, 1), \
|
2015-06-17 00:50:51 +00:00
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
|
|
|
|
/* reload syscall number; all rules expect it in accumulator */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, nr))
|
2019-08-23 00:08:48 +00:00
|
|
|
/* Allow if syscall argument contains only values in mask */
|
|
|
|
#define SC_ALLOW_ARG_MASK(_nr, _arg_nr, _arg_mask) \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 8), \
|
|
|
|
/* load, mask and test syscall argument, low word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, ~((_arg_mask) & 0xFFFFFFFF)), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 4), \
|
|
|
|
/* load, mask and test syscall argument, high word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, \
|
|
|
|
~(((uint32_t)((uint64_t)(_arg_mask) >> 32)) & 0xFFFFFFFF)), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 1), \
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
|
|
|
|
/* reload syscall number; all rules expect it in accumulator */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, nr))
|
2023-02-03 05:33:09 +00:00
|
|
|
/* Deny unless syscall argument contains only values in mask */
|
|
|
|
#define SC_DENY_UNLESS_ARG_MASK(_nr, _arg_nr, _arg_mask, _errno) \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 8), \
|
|
|
|
/* load, mask and test syscall argument, low word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, ~((_arg_mask) & 0xFFFFFFFF)), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 3), \
|
|
|
|
/* load, mask and test syscall argument, high word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, \
|
|
|
|
~(((uint32_t)((uint64_t)(_arg_mask) >> 32)) & 0xFFFFFFFF)), \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 1, 0), \
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno)), \
|
|
|
|
/* reload syscall number; all rules expect it in accumulator */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, nr))
|
|
|
|
#define SC_DENY_UNLESS_MASK(_nr, _arg_nr, _arg_val, _errno) \
|
|
|
|
/* Special handling for futex(2) that combines a bitmap and operation number */
|
|
|
|
#if defined(__NR_futex) || defined(__NR_futex_time64)
|
|
|
|
#define SC_FUTEX_MASK (FUTEX_PRIVATE_FLAG|FUTEX_CLOCK_REALTIME)
|
|
|
|
#define SC_ALLOW_FUTEX_OP(_nr, _op) \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 8), \
|
|
|
|
/* load syscall argument, low word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[1]) + ARG_LO_OFFSET), \
|
|
|
|
/* mask off allowed bitmap values, low word */ \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, ~(SC_FUTEX_MASK & 0xFFFFFFFF)), \
|
|
|
|
/* test operation number, low word */ \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ((_op) & 0xFFFFFFFF), 0, 4), \
|
|
|
|
/* load syscall argument, high word */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
|
|
|
|
offsetof(struct seccomp_data, args[1]) + ARG_HI_OFFSET), \
|
|
|
|
/* mask off allowed bitmap values, high word */ \
|
|
|
|
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, \
|
|
|
|
~(((uint32_t)((uint64_t)SC_FUTEX_MASK >> 32)) & 0xFFFFFFFF)), \
|
|
|
|
/* test operation number, high word */ \
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
|
|
|
|
(((uint32_t)((uint64_t)(_op) >> 32)) & 0xFFFFFFFF), 0, 1), \
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
|
|
|
|
/* reload syscall number; all rules expect it in accumulator */ \
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr))
|
|
|
|
|
|
|
|
/* Use this for both __NR_futex and __NR_futex_time64 */
|
|
|
|
# define SC_FUTEX(_nr) \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_WAIT), \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_WAIT_BITSET), \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_WAKE), \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_WAKE_BITSET), \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_REQUEUE), \
|
|
|
|
SC_ALLOW_FUTEX_OP(__NR_futex, FUTEX_CMP_REQUEUE)
|
|
|
|
#endif /* __NR_futex || __NR_futex_time64 */
|
|
|
|
|
|
|
|
#if defined(__NR_mmap) || defined(__NR_mmap2)
|
2023-02-11 01:32:19 +00:00
|
|
|
# ifdef MAP_FIXED_NOREPLACE
|
|
|
|
# define SC_MMAP_FLAGS MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_FIXED_NOREPLACE
|
|
|
|
# else
|
|
|
|
# define SC_MMAP_FLAGS MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED
|
|
|
|
# endif /* MAP_FIXED_NOREPLACE */
|
2023-02-03 05:33:09 +00:00
|
|
|
/* Use this for both __NR_mmap and __NR_mmap2 variants */
|
|
|
|
# define SC_MMAP(_nr) \
|
2023-02-11 01:32:19 +00:00
|
|
|
SC_DENY_UNLESS_ARG_MASK(_nr, 3, SC_MMAP_FLAGS, EINVAL), \
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG_MASK(_nr, 2, PROT_READ|PROT_WRITE|PROT_NONE)
|
|
|
|
#endif /* __NR_mmap || __NR_mmap2 */
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
/* Syscall filtering set for preauth. */
|
|
|
|
static const struct sock_filter preauth_insns[] = {
|
|
|
|
/* Ensure the syscall arch convention is as expected. */
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
|
|
|
|
offsetof(struct seccomp_data, arch)),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
|
|
|
|
/* Load the syscall number for checking. */
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
|
|
|
|
offsetof(struct seccomp_data, nr)),
|
2015-06-17 00:50:51 +00:00
|
|
|
|
|
|
|
/* Syscalls to non-fatally deny */
|
2016-05-19 23:56:53 +00:00
|
|
|
#ifdef __NR_lstat
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_lstat, EACCES),
|
2016-05-19 23:56:53 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_lstat64
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_lstat64, EACCES),
|
2016-05-19 23:56:53 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_fstat
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_fstat, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstat64
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_fstat64, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2021-03-12 04:58:57 +00:00
|
|
|
#ifdef __NR_fstatat64
|
|
|
|
SC_DENY(__NR_fstatat64, EACCES),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_open
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_open, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_openat
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_openat, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_newfstatat
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_newfstatat, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_stat
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_stat, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_stat64
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_DENY(__NR_stat64, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2019-10-01 14:05:09 +00:00
|
|
|
#ifdef __NR_shmget
|
|
|
|
SC_DENY(__NR_shmget, EACCES),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_shmat
|
|
|
|
SC_DENY(__NR_shmat, EACCES),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_shmdt
|
|
|
|
SC_DENY(__NR_shmdt, EACCES),
|
|
|
|
#endif
|
2019-10-12 01:31:05 +00:00
|
|
|
#ifdef __NR_ipc
|
|
|
|
SC_DENY(__NR_ipc, EACCES),
|
|
|
|
#endif
|
2020-11-08 13:19:23 +00:00
|
|
|
#ifdef __NR_statx
|
|
|
|
SC_DENY(__NR_statx, EACCES),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
|
|
|
|
/* Syscalls to permit */
|
|
|
|
#ifdef __NR_brk
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_brk),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_clock_gettime
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_clock_gettime),
|
2013-02-22 00:37:00 +00:00
|
|
|
#endif
|
2020-02-03 08:40:12 +00:00
|
|
|
#ifdef __NR_clock_gettime64
|
|
|
|
SC_ALLOW(__NR_clock_gettime64),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_close
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_close),
|
2014-02-06 00:17:50 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_exit
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_exit),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_exit_group
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_exit_group),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2018-09-15 09:37:48 +00:00
|
|
|
#ifdef __NR_futex
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_FUTEX(__NR_futex),
|
2018-09-15 09:37:48 +00:00
|
|
|
#endif
|
2021-02-10 23:18:05 +00:00
|
|
|
#ifdef __NR_futex_time64
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_FUTEX(__NR_futex_time64),
|
2021-02-10 23:18:05 +00:00
|
|
|
#endif
|
2018-05-25 03:36:58 +00:00
|
|
|
#ifdef __NR_geteuid
|
|
|
|
SC_ALLOW(__NR_geteuid),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_geteuid32
|
|
|
|
SC_ALLOW(__NR_geteuid32),
|
|
|
|
#endif
|
2015-06-29 22:35:12 +00:00
|
|
|
#ifdef __NR_getpgid
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_getpgid),
|
2015-06-29 22:35:12 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_getpid
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_getpid),
|
2012-04-04 01:27:54 +00:00
|
|
|
#endif
|
2015-09-10 00:57:41 +00:00
|
|
|
#ifdef __NR_getrandom
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_getrandom),
|
2015-09-10 00:57:41 +00:00
|
|
|
#endif
|
2022-01-30 22:13:36 +00:00
|
|
|
#ifdef __NR_gettid
|
|
|
|
SC_ALLOW(__NR_gettid),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_gettimeofday
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_gettimeofday),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2018-05-25 03:36:58 +00:00
|
|
|
#ifdef __NR_getuid
|
|
|
|
SC_ALLOW(__NR_getuid),
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_getuid32
|
|
|
|
SC_ALLOW(__NR_getuid32),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_madvise
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_NORMAL),
|
2023-02-11 01:32:19 +00:00
|
|
|
# ifdef MADV_FREE
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_FREE),
|
2023-02-11 01:32:19 +00:00
|
|
|
# endif
|
|
|
|
# ifdef MADV_DONTNEED
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_DONTNEED),
|
2023-02-11 01:32:19 +00:00
|
|
|
# endif
|
|
|
|
# ifdef MADV_DONTFORK
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_DONTFORK),
|
2023-02-11 01:32:19 +00:00
|
|
|
# endif
|
|
|
|
# ifdef MADV_DONTDUMP
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_DONTDUMP),
|
2023-02-11 01:32:19 +00:00
|
|
|
# endif
|
|
|
|
# ifdef MADV_WIPEONFORK
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_ALLOW_ARG(__NR_madvise, 2, MADV_WIPEONFORK),
|
2023-02-11 01:32:19 +00:00
|
|
|
# endif
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_DENY(__NR_madvise, EINVAL),
|
2013-02-22 00:37:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_mmap
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_MMAP(__NR_mmap),
|
2014-09-09 22:15:34 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_mmap2
|
2023-02-03 05:33:09 +00:00
|
|
|
SC_MMAP(__NR_mmap2),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2019-08-23 00:08:48 +00:00
|
|
|
#ifdef __NR_mprotect
|
|
|
|
SC_ALLOW_ARG_MASK(__NR_mprotect, 2, PROT_READ|PROT_WRITE|PROT_NONE),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_mremap
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_mremap),
|
2013-02-22 00:37:00 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_munmap
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_munmap),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2018-04-13 06:06:29 +00:00
|
|
|
#ifdef __NR_nanosleep
|
|
|
|
SC_ALLOW(__NR_nanosleep),
|
|
|
|
#endif
|
2019-11-13 12:19:35 +00:00
|
|
|
#ifdef __NR_clock_nanosleep
|
|
|
|
SC_ALLOW(__NR_clock_nanosleep),
|
|
|
|
#endif
|
2019-12-16 02:55:56 +00:00
|
|
|
#ifdef __NR_clock_nanosleep_time64
|
|
|
|
SC_ALLOW(__NR_clock_nanosleep_time64),
|
|
|
|
#endif
|
2020-01-08 00:26:45 +00:00
|
|
|
#ifdef __NR_clock_gettime64
|
|
|
|
SC_ALLOW(__NR_clock_gettime64),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR__newselect
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR__newselect),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2021-11-17 23:16:55 +00:00
|
|
|
#ifdef __NR_ppoll
|
|
|
|
SC_ALLOW(__NR_ppoll),
|
|
|
|
#endif
|
2022-02-26 03:06:14 +00:00
|
|
|
#ifdef __NR_ppoll_time64
|
|
|
|
SC_ALLOW(__NR_ppoll_time64),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_poll
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_poll),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2015-06-24 23:51:39 +00:00
|
|
|
#ifdef __NR_pselect6
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_pselect6),
|
2015-06-24 23:51:39 +00:00
|
|
|
#endif
|
2020-11-20 02:37:54 +00:00
|
|
|
#ifdef __NR_pselect6_time64
|
|
|
|
SC_ALLOW(__NR_pselect6_time64),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_read
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_read),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2012-04-04 01:27:54 +00:00
|
|
|
#ifdef __NR_rt_sigprocmask
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_rt_sigprocmask),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_select
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_select),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_shutdown
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_shutdown),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_sigprocmask
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_sigprocmask),
|
2012-04-04 01:27:54 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_time
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_time),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_write
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW(__NR_write),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2023-01-14 11:02:44 +00:00
|
|
|
#ifdef __NR_writev
|
|
|
|
SC_ALLOW(__NR_writev),
|
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
#ifdef __NR_socketcall
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW_ARG(__NR_socketcall, 0, SYS_SHUTDOWN),
|
2017-04-24 22:32:27 +00:00
|
|
|
SC_DENY(__NR_socketcall, EACCES),
|
2015-06-17 00:50:51 +00:00
|
|
|
#endif
|
2017-03-14 02:15:18 +00:00
|
|
|
#if defined(__NR_ioctl) && defined(__s390__)
|
|
|
|
/* Allow ioctls for ICA crypto card on s390 */
|
2017-03-14 06:48:43 +00:00
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, Z90STAT_STATUS_MASK),
|
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, ICARSAMODEXPO),
|
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, ICARSACRT),
|
2019-05-24 08:11:15 +00:00
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, ZSECSENDCPRB),
|
2017-05-09 16:33:30 +00:00
|
|
|
/* Allow ioctls for EP11 crypto card on s390 */
|
|
|
|
SC_ALLOW_ARG(__NR_ioctl, 1, ZSENDEP11CPRB),
|
2017-03-14 07:26:29 +00:00
|
|
|
#endif
|
|
|
|
#if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
|
|
|
|
/*
|
|
|
|
* On Linux x32, the clock_gettime VDSO falls back to the
|
|
|
|
* x86-64 syscall under some circumstances, e.g.
|
|
|
|
* https://bugs.debian.org/849923
|
|
|
|
*/
|
2017-03-20 21:47:55 +00:00
|
|
|
SC_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
|
2017-03-14 07:26:29 +00:00
|
|
|
#endif
|
2015-06-17 00:50:51 +00:00
|
|
|
|
|
|
|
/* Default deny */
|
2012-04-04 01:27:54 +00:00
|
|
|
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct sock_fprog preauth_program = {
|
|
|
|
.len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
|
|
|
|
.filter = (struct sock_filter *)preauth_insns,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ssh_sandbox {
|
2024-10-14 01:57:50 +00:00
|
|
|
int junk;
|
2012-04-04 01:27:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ssh_sandbox *
|
2014-01-17 05:47:04 +00:00
|
|
|
ssh_sandbox_init(struct monitor *monitor)
|
2012-04-04 01:27:54 +00:00
|
|
|
{
|
|
|
|
struct ssh_sandbox *box;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strictly, we don't need to maintain any state here but we need
|
|
|
|
* to return non-NULL to satisfy the API.
|
|
|
|
*/
|
|
|
|
debug3("%s: preparing seccomp filter sandbox", __func__);
|
|
|
|
box = xcalloc(1, sizeof(*box));
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
|
|
|
|
extern struct monitor *pmonitor;
|
2022-08-10 22:58:28 +00:00
|
|
|
void mm_log_handler(LogLevel level, int forced, const char *msg, void *ctx);
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
|
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
|
|
|
|
snprintf(msg, sizeof(msg),
|
|
|
|
"%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
|
|
|
|
__func__, info->si_arch, info->si_syscall, info->si_call_addr);
|
2022-08-10 22:58:28 +00:00
|
|
|
mm_log_handler(SYSLOG_LEVEL_FATAL, 0, msg, pmonitor);
|
2012-04-04 01:27:54 +00:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ssh_sandbox_child_debugging(void)
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
sigset_t mask;
|
|
|
|
|
|
|
|
debug3("%s: installing SIGSYS handler", __func__);
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGSYS);
|
|
|
|
|
|
|
|
act.sa_sigaction = &ssh_sandbox_violation;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
if (sigaction(SIGSYS, &act, NULL) == -1)
|
|
|
|
fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
|
|
|
|
fatal("%s: sigprocmask(SIGSYS): %s",
|
2021-04-03 06:47:37 +00:00
|
|
|
__func__, strerror(errno));
|
2012-04-04 01:27:54 +00:00
|
|
|
}
|
|
|
|
#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
|
|
|
|
|
|
|
|
void
|
|
|
|
ssh_sandbox_child(struct ssh_sandbox *box)
|
|
|
|
{
|
2021-11-17 23:16:55 +00:00
|
|
|
struct rlimit rl_zero, rl_one = {.rlim_cur = 1, .rlim_max = 1};
|
2012-07-06 00:27:10 +00:00
|
|
|
int nnp_failed = 0;
|
2012-04-04 01:27:54 +00:00
|
|
|
|
|
|
|
/* Set rlimits for completeness if possible. */
|
|
|
|
rl_zero.rlim_cur = rl_zero.rlim_max = 0;
|
|
|
|
if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
|
|
|
|
fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
|
|
|
|
__func__, strerror(errno));
|
2021-11-17 23:16:55 +00:00
|
|
|
/*
|
|
|
|
* Cannot use zero for nfds, because poll(2) will fail with
|
|
|
|
* errno=EINVAL if npfds>RLIMIT_NOFILE.
|
|
|
|
*/
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl_one) == -1)
|
2012-04-04 01:27:54 +00:00
|
|
|
fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
|
|
|
|
fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
|
|
|
|
#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
|
|
|
|
ssh_sandbox_child_debugging();
|
|
|
|
#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
|
|
|
|
|
|
|
|
debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
|
2012-07-06 00:27:10 +00:00
|
|
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
|
|
|
|
debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
|
2021-04-03 06:47:37 +00:00
|
|
|
__func__, strerror(errno));
|
2012-07-06 00:27:10 +00:00
|
|
|
nnp_failed = 1;
|
|
|
|
}
|
2012-04-04 01:27:54 +00:00
|
|
|
debug3("%s: attaching seccomp filter program", __func__);
|
|
|
|
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
|
2012-07-06 00:27:10 +00:00
|
|
|
debug("%s: prctl(PR_SET_SECCOMP): %s",
|
2021-04-03 06:47:37 +00:00
|
|
|
__func__, strerror(errno));
|
2012-07-06 00:27:10 +00:00
|
|
|
else if (nnp_failed)
|
|
|
|
fatal("%s: SECCOMP_MODE_FILTER activated but "
|
|
|
|
"PR_SET_NO_NEW_PRIVS failed", __func__);
|
2012-04-04 01:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SANDBOX_SECCOMP_FILTER */
|