mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-08 02:03:56 -06:00
This misc series of changes:
- Improves documentation of SSH fingerprint checking - Fixes SHA256 fingerprints with non-blockdev usage - Blocks the clone3, setns, unshare & execveat syscalls with seccomp - Blocks process spawning via clone syscall, but allows threads, with seccomp - Takes over seccomp maintainer role - Expands firmware descriptor spec to allow flash without NVRAM -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEE2vOm/bJrYpEtDo4/vobrtBUQT98FAmIOOBkACgkQvobrtBUQ T9/ruhAAr8jkAH8FN5ftx2/L7q8SHpjPupue1CJ0Nl/ykmYhTGc+SqC3R2nZWOk2 Ws8hHVcDVT1lhrGxPtU7o+JPC1TebJTsloimJoKQY3qfdvZadJeR/4KsOUzi2ruu VZ6HiYvZc1c9T+NPf3QRhBo7yyascKWKWHDseUNIt/2DiefCox4QFUDDMG86HiQF KK30xWTvwJdcPxRlbfZbWRoqA0v4OoSDK6Ftp94FQSNBkExO85kstDq3xVaApf8H DE1QD7gf+dvz11wVuFhrf4d1EH032nU0p0kMxhABc4/kZXo5iWXohhzML3/MUEVT pe5/9pzUdWpfXQd/2r7x2PyPgySAG7lGbkgltowY52qnRPaNw9ukwkFCFAj8wiD8 FT2ghvkYD3zLfnZ3nuuzJVjf3pXgCc5VcfXaoffT72a7gpI1LTuEqPFwo04imV4l 21fYFx26mYTGCLH1CwVw8MQ2z/dg6uorT/NHdmRA/KrYJ1Elay2K7DV3Z5jOM5MI 0Ll5HkfsUut+1rioUjNgmlQ+96k/G0P0hVUoTUIcgl3U/GDx2+ypcrNTfmEcaCLV bOhsjtrcg/KAXsCSbvnfDe3bWf0txnscyqoilEzDahLvciWG3d6qlhczLy29LGb4 /w7iqnUcSygXc+a9/ckVo1h5fo0i9qb3W8Pw9klapvz6SGJ83g4= =PeCY -----END PGP SIGNATURE----- Merge remote-tracking branch 'remotes/berrange-gitlab/tags/misc-next-pull-request' into staging This misc series of changes: - Improves documentation of SSH fingerprint checking - Fixes SHA256 fingerprints with non-blockdev usage - Blocks the clone3, setns, unshare & execveat syscalls with seccomp - Blocks process spawning via clone syscall, but allows threads, with seccomp - Takes over seccomp maintainer role - Expands firmware descriptor spec to allow flash without NVRAM # gpg: Signature made Thu 17 Feb 2022 11:57:13 GMT # gpg: using RSA key DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF # gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" [full] # gpg: aka "Daniel P. Berrange <berrange@redhat.com>" [full] # Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E 8E3F BE86 EBB4 1510 4FDF * remotes/berrange-gitlab/tags/misc-next-pull-request: docs: expand firmware descriptor to allow flash without NVRAM MAINTAINERS: take over seccomp from Eduardo Otubo seccomp: block setns, unshare and execveat syscalls seccomp: block use of clone3 syscall seccomp: fix blocking of process spawning seccomp: add unit test for seccomp filtering seccomp: allow action to be customized per syscall block: print the server key type and fingerprint on failure block: support sha256 fingerprint with pre-blockdev options block: better document SSH host key fingerprint checking Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
commit
4aa2e497a9
7 changed files with 599 additions and 87 deletions
|
@ -53,6 +53,10 @@ if have_system or have_tools
|
|||
tests += {
|
||||
'test-qmp-event': [testqapi],
|
||||
}
|
||||
|
||||
if seccomp.found()
|
||||
tests += {'test-seccomp': ['../../softmmu/qemu-seccomp.c', seccomp]}
|
||||
endif
|
||||
endif
|
||||
|
||||
if have_block
|
||||
|
|
269
tests/unit/test-seccomp.c
Normal file
269
tests/unit/test-seccomp.c
Normal file
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
* QEMU seccomp test suite
|
||||
*
|
||||
* Copyright (c) 2021 Red Hat, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/config-file.h"
|
||||
#include "qemu/option.h"
|
||||
#include "sysemu/seccomp.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qemu/module.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
static void test_seccomp_helper(const char *args, bool killed,
|
||||
int errnum, int (*doit)(void))
|
||||
{
|
||||
if (g_test_subprocess()) {
|
||||
QemuOptsList *olist;
|
||||
QemuOpts *opts;
|
||||
int ret;
|
||||
|
||||
module_call_init(MODULE_INIT_OPTS);
|
||||
olist = qemu_find_opts("sandbox");
|
||||
g_assert(olist != NULL);
|
||||
|
||||
opts = qemu_opts_parse_noisily(olist, args, true);
|
||||
g_assert(opts != NULL);
|
||||
|
||||
parse_sandbox(NULL, opts, &error_abort);
|
||||
|
||||
/* Running in a child process */
|
||||
ret = doit();
|
||||
|
||||
if (errnum != 0) {
|
||||
g_assert(ret != 0);
|
||||
g_assert(errno == errnum);
|
||||
} else {
|
||||
g_assert(ret == 0);
|
||||
}
|
||||
|
||||
_exit(0);
|
||||
} else {
|
||||
/* Running in main test process, spawning the child */
|
||||
g_test_trap_subprocess(NULL, 0, 0);
|
||||
if (killed) {
|
||||
g_test_trap_assert_failed();
|
||||
} else {
|
||||
g_test_trap_assert_passed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void test_seccomp_killed(const char *args, int (*doit)(void))
|
||||
{
|
||||
test_seccomp_helper(args, true, 0, doit);
|
||||
}
|
||||
|
||||
static void test_seccomp_errno(const char *args, int errnum, int (*doit)(void))
|
||||
{
|
||||
test_seccomp_helper(args, false, errnum, doit);
|
||||
}
|
||||
|
||||
static void test_seccomp_passed(const char *args, int (*doit)(void))
|
||||
{
|
||||
test_seccomp_helper(args, false, 0, doit);
|
||||
}
|
||||
|
||||
#ifdef SYS_fork
|
||||
static int doit_sys_fork(void)
|
||||
{
|
||||
int ret = syscall(SYS_fork);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if (ret == 0) {
|
||||
_exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_seccomp_sys_fork_on_nospawn(void)
|
||||
{
|
||||
test_seccomp_killed("on,spawn=deny", doit_sys_fork);
|
||||
}
|
||||
|
||||
static void test_seccomp_sys_fork_on(void)
|
||||
{
|
||||
test_seccomp_passed("on", doit_sys_fork);
|
||||
}
|
||||
|
||||
static void test_seccomp_sys_fork_off(void)
|
||||
{
|
||||
test_seccomp_passed("off", doit_sys_fork);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int doit_fork(void)
|
||||
{
|
||||
int ret = fork();
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if (ret == 0) {
|
||||
_exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_seccomp_fork_on_nospawn(void)
|
||||
{
|
||||
test_seccomp_killed("on,spawn=deny", doit_fork);
|
||||
}
|
||||
|
||||
static void test_seccomp_fork_on(void)
|
||||
{
|
||||
test_seccomp_passed("on", doit_fork);
|
||||
}
|
||||
|
||||
static void test_seccomp_fork_off(void)
|
||||
{
|
||||
test_seccomp_passed("off", doit_fork);
|
||||
}
|
||||
|
||||
static void *noop(void *arg)
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
|
||||
static int doit_thread(void)
|
||||
{
|
||||
pthread_t th;
|
||||
int ret = pthread_create(&th, NULL, noop, NULL);
|
||||
if (ret != 0) {
|
||||
errno = ret;
|
||||
return -1;
|
||||
} else {
|
||||
pthread_join(th, NULL);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_seccomp_thread_on(void)
|
||||
{
|
||||
test_seccomp_passed("on", doit_thread);
|
||||
}
|
||||
|
||||
static void test_seccomp_thread_on_nospawn(void)
|
||||
{
|
||||
test_seccomp_passed("on,spawn=deny", doit_thread);
|
||||
}
|
||||
|
||||
static void test_seccomp_thread_off(void)
|
||||
{
|
||||
test_seccomp_passed("off", doit_thread);
|
||||
}
|
||||
|
||||
static int doit_sched(void)
|
||||
{
|
||||
struct sched_param param = { .sched_priority = 0 };
|
||||
return sched_setscheduler(getpid(), SCHED_OTHER, ¶m);
|
||||
}
|
||||
|
||||
static void test_seccomp_sched_on_nores(void)
|
||||
{
|
||||
test_seccomp_errno("on,resourcecontrol=deny", EPERM, doit_sched);
|
||||
}
|
||||
|
||||
static void test_seccomp_sched_on(void)
|
||||
{
|
||||
test_seccomp_passed("on", doit_sched);
|
||||
}
|
||||
|
||||
static void test_seccomp_sched_off(void)
|
||||
{
|
||||
test_seccomp_passed("off", doit_sched);
|
||||
}
|
||||
|
||||
static bool can_play_with_seccomp(void)
|
||||
{
|
||||
g_autofree char *status = NULL;
|
||||
g_auto(GStrv) lines = NULL;
|
||||
size_t i;
|
||||
|
||||
if (!g_file_get_contents("/proc/self/status", &status, NULL, NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lines = g_strsplit(status, "\n", 0);
|
||||
|
||||
for (i = 0; lines[i] != NULL; i++) {
|
||||
if (g_str_has_prefix(lines[i], "Seccomp:")) {
|
||||
/*
|
||||
* "Seccomp: 1" or "Seccomp: 2" indicate we're already
|
||||
* confined, probably as we're inside a container. In
|
||||
* this case our tests might get unexpected results,
|
||||
* so we can't run reliably
|
||||
*/
|
||||
if (!strchr(lines[i], '0')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Doesn't look like seccomp is enabled in the kernel */
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
g_test_init(&argc, &argv, NULL);
|
||||
if (can_play_with_seccomp()) {
|
||||
#ifdef SYS_fork
|
||||
g_test_add_func("/softmmu/seccomp/sys-fork/on",
|
||||
test_seccomp_sys_fork_on);
|
||||
g_test_add_func("/softmmu/seccomp/sys-fork/on-nospawn",
|
||||
test_seccomp_sys_fork_on_nospawn);
|
||||
g_test_add_func("/softmmu/seccomp/sys-fork/off",
|
||||
test_seccomp_sys_fork_off);
|
||||
#endif
|
||||
|
||||
g_test_add_func("/softmmu/seccomp/fork/on",
|
||||
test_seccomp_fork_on);
|
||||
g_test_add_func("/softmmu/seccomp/fork/on-nospawn",
|
||||
test_seccomp_fork_on_nospawn);
|
||||
g_test_add_func("/softmmu/seccomp/fork/off",
|
||||
test_seccomp_fork_off);
|
||||
|
||||
g_test_add_func("/softmmu/seccomp/thread/on",
|
||||
test_seccomp_thread_on);
|
||||
g_test_add_func("/softmmu/seccomp/thread/on-nospawn",
|
||||
test_seccomp_thread_on_nospawn);
|
||||
g_test_add_func("/softmmu/seccomp/thread/off",
|
||||
test_seccomp_thread_off);
|
||||
|
||||
if (doit_sched() == 0) {
|
||||
/*
|
||||
* musl doesn't impl sched_setscheduler, hence
|
||||
* we check above if it works first
|
||||
*/
|
||||
g_test_add_func("/softmmu/seccomp/sched/on",
|
||||
test_seccomp_sched_on);
|
||||
g_test_add_func("/softmmu/seccomp/sched/on-nores",
|
||||
test_seccomp_sched_on_nores);
|
||||
g_test_add_func("/softmmu/seccomp/sched/off",
|
||||
test_seccomp_sched_off);
|
||||
}
|
||||
}
|
||||
return g_test_run();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue