1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Copyright (C) 2013-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(HAVE_LINUX_SECCOMP_H) && \
defined(HAVE_LINUX_AUDIT_H) && \
defined(HAVE_LINUX_FILTER_H) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_SECCOMP) && \
defined(SECCOMP_SET_MODE_FILTER)
#define SYSCALL_NR (offsetof(struct seccomp_data, nr))
#define ALLOW_SYSCALL(syscall) \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##syscall, 0, 1), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
static struct sock_filter filter_allow_all[] = {
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
};
static struct sock_filter filter_allow_write[] = {
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SYSCALL_NR),
#if defined(__NR_open)
ALLOW_SYSCALL(open),
#endif
#if defined(__NR_openat)
ALLOW_SYSCALL(openat),
#endif
#if defined(__NR_write)
ALLOW_SYSCALL(write),
#endif
#if defined(__NR_close)
ALLOW_SYSCALL(close),
#endif
#if defined(__NR_exit_group)
ALLOW_SYSCALL(exit_group),
#endif
#if defined(__NR_set_robust_list)
ALLOW_SYSCALL(set_robust_list),
#endif
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
};
static struct sock_filter filter[] = {
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SYSCALL_NR),
#if defined(__NR_open)
ALLOW_SYSCALL(open),
#endif
#if defined(__NR_openat)
ALLOW_SYSCALL(openat),
#endif
#if defined(__NR_close)
ALLOW_SYSCALL(close),
#endif
#if defined(__NR_exit_group)
ALLOW_SYSCALL(exit_group),
#endif
#if defined(__NR_set_robust_list)
ALLOW_SYSCALL(set_robust_list),
#endif
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
};
static struct sock_filter filter_random[64];
static struct sock_fprog prog_allow_all = {
.len = (unsigned short)SIZEOF_ARRAY(filter_allow_all),
.filter = filter_allow_all
};
static struct sock_fprog prog_allow_write = {
.len = (unsigned short)SIZEOF_ARRAY(filter_allow_write),
.filter = filter_allow_write
};
static struct sock_fprog prog = {
.len = (unsigned short)SIZEOF_ARRAY(filter),
.filter = filter
};
static struct sock_fprog prog_random = {
.len = (unsigned short)SIZEOF_ARRAY(filter_random),
.filter = filter_random
};
static int stress_seccomp_supported(void)
{
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
pr_inf("seccomp stressor will be skipped, the check for seccomp failed, fork failed: errno=%d (%s)\n",
errno, strerror(errno));
return -1;
}
if (pid == 0) {
if (shim_seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog_allow_all) == 0) {
_exit(0);
}
_exit(errno);
}
if (waitpid(pid, &status, 0) < 0) {
pr_inf("seccomp stressor will be skipped, the check for seccomp failed, wait failed: errno=%d (%s)\n",
errno, strerror(errno));
return -1;
}
if (WIFEXITED(status) && (WEXITSTATUS(status) != 0)) {
errno = WEXITSTATUS(status);
if (errno == EACCES) {
pr_inf("seccomp stressor will be skipped, SECCOMP_SET_MODE_FILTER requires CAP_SYS_ADMIN capability\n");
return -1;
} else {
pr_inf("seccomp stressor will be skipped, SECCOMP_SET_MODE_FILTER is not supported, errno=%d (%s)\n",
errno, strerror(errno));
}
return -1;
}
return 0;
}
/*
* stress_seccomp_set_filter()
* set up a seccomp filter, allow writes
* if allow_write is true.
*/
static inline int stress_seccomp_set_filter(
const args_t *args,
const bool allow_write,
bool do_random)
{
#if defined(__NR_seccomp)
static bool use_seccomp = true;
#endif
/*
* Depending on allow_write we either use the
* filter that allows writes or the filter
* that does not allow writes
*/
size_t i;
struct sock_fprog *p = allow_write ?
&prog_allow_write :
do_random ? &prog_random : &prog;
if (do_random) {
for (i = 0; i < SIZEOF_ARRAY(filter_random); i++) {
struct sock_filter bpf_stmt = BPF_STMT(mwc32(), SECCOMP_RET_KILL);
filter_random[i] = bpf_stmt;
}
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
pr_fail_err("prctl PR_SET_NEW_PRIVS");
return -1;
}
#if defined(__NR_seccomp)
/*
* Try using the newer seccomp syscall first
*/
if (use_seccomp) {
redo_seccomp:
if (shim_seccomp(SECCOMP_SET_MODE_FILTER, 0, p) == 0)
return 0;
if (errno != ENOSYS) {
if (do_random) {
do_random = false;
p = &prog;
goto redo_seccomp;
}
pr_fail_err("seccomp SECCOMP_SET_MODE_FILTER");
return -1;
}
use_seccomp = false;
}
#endif
redo_prctl:
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, p) < 0) {
if (do_random) {
do_random = false;
p = &prog;
goto redo_prctl;
}
pr_fail_err("prctl PR_SET_SECCOMP");
return -1;
}
return 0;
}
/*
* stress_seccomp()
* stress seccomp
*/
static int stress_seccomp(const args_t *args)
{
do {
pid_t pid;
const bool allow_write = (mwc32() % 50) != 0;
const bool do_random = (mwc32() % 20) != 0;
pid = fork();
if (pid == -1) {
pr_fail_err("fork");
break;
}
if (pid == 0) {
/*
* The child has a seccomp filter applied and
* 1 in 50 chance that write() is not allowed
* causing seccomp to kill it and the parent
* sees it die on a SIGSYS
*/
int fd, rc = EXIT_SUCCESS;
if (stress_seccomp_set_filter(args, allow_write, do_random) < 0)
_exit(EXIT_FAILURE);
if ((fd = open("/dev/null", O_WRONLY)) < 0) {
pr_err("%s: open failed on /dev/null, "
"errno=%d (%s)\n",
args->name, errno, strerror(errno));
_exit(EXIT_FAILURE);
}
if (write(fd, "TEST\n", 5) < 0) {
pr_err("%s: write to /dev/null failed, "
"errno=%d (%s)\n",
args->name, errno, strerror(errno));
rc = EXIT_FAILURE;
}
(void)close(fd);
_exit(rc);
}
if (pid > 0) {
int status;
/* Wait for child to exit or get killed by seccomp */
if (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR)
pr_dbg("%s: waitpid failed, errno = %d (%s)\n",
args->name, errno, strerror(errno));
} else {
/* Did the child hit a weird error? */
if (WIFEXITED(status) &&
(WEXITSTATUS(status) != EXIT_SUCCESS)) {
pr_fail("%s: aborting because of unexpected "
"failure in child process\n", args->name);
return EXIT_FAILURE;
}
/* ..exited OK but we expected SIGSYS death? */
if (WIFEXITED(status) && !allow_write) {
pr_fail("%s: expecting SIGSYS seccomp trap "
"but got a successful exit which "
"was not expected\n",
args->name);
}
/* ..exited with a SIGSYS but we expexted OK exit? */
if (WIFSIGNALED(status) && allow_write) {
if (WTERMSIG(status) == SIGSYS) {
pr_fail("%s: expecting a successful exit "
"but got a seccomp SIGSYS "
"which was not expected\n",
args->name);
}
}
}
}
inc_counter(args);
} while (keep_stressing());
return EXIT_SUCCESS;
}
stressor_info_t stress_seccomp_info = {
.stressor = stress_seccomp,
.supported = stress_seccomp_supported,
.class = CLASS_OS
};
#else
stressor_info_t stress_seccomp_info = {
.stressor = stress_not_implemented,
.class = CLASS_OS
};
#endif
|