2021-09-27 13:10:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
#
|
|
|
|
# PASST - Plug A Simple Socket Transport
|
|
|
|
# for qemu/UNIX domain socket mode
|
|
|
|
#
|
|
|
|
# PASTA - Pack A Subtle Tap Abstraction
|
|
|
|
# for network namespace/tap device mode
|
|
|
|
#
|
|
|
|
# test/lib/setup - Set up and tear down passt and pasta environments
|
|
|
|
#
|
|
|
|
# Copyright (c) 2021 Red Hat GmbH
|
|
|
|
# Author: Stefano Brivio <sbrivio@redhat.com>
|
|
|
|
|
2022-07-06 07:29:00 +00:00
|
|
|
INITRAMFS="${BASEPATH}/mbuto.img"
|
2021-10-21 02:54:05 +00:00
|
|
|
VCPUS="$( [ $(nproc) -ge 8 ] && echo 6 || echo $(( $(nproc) / 2 + 1 )) )"
|
2021-09-27 13:10:35 +00:00
|
|
|
__mem_kib="$(sed -n 's/MemTotal:[ ]*\([0-9]*\) kB/\1/p' /proc/meminfo)"
|
2021-10-21 02:54:05 +00:00
|
|
|
VMEM="$((${__mem_kib} / 1024 / 4))"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
# setup_build() - Set up pane layout for build tests
|
|
|
|
setup_build() {
|
|
|
|
layout_host
|
|
|
|
}
|
|
|
|
|
2022-08-18 06:13:53 +00:00
|
|
|
# setup_distro() - Set up pane layout for distro tests
|
|
|
|
setup_distro() {
|
|
|
|
layout_host
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:29:00 +00:00
|
|
|
# setup_passt() - Start qemu and passt
|
2021-09-27 13:10:35 +00:00
|
|
|
setup_passt() {
|
|
|
|
layout_passt
|
|
|
|
|
|
|
|
# Ports:
|
|
|
|
#
|
|
|
|
# guest | host
|
|
|
|
# --------------|---------------------
|
|
|
|
# 10001 as server | forwarded to guest
|
|
|
|
# 10003 | as server
|
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/passt.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
|
|
|
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "make clean"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "make valgrind"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "valgrind --max-stackframe=$((4 * 1024 * 1024)) --trace-children=yes --vgdb=no --error-exitcode=1 --suppressions=test/valgrind.supp ./passt ${__opts} -f -t 10001 -u 10001 -P passt.pid"
|
|
|
|
sleep 5
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-07-06 07:28:58 +00:00
|
|
|
pane_run GUEST './qrap 5 qemu-system-$(uname -m)' \
|
|
|
|
' -machine accel=kvm' \
|
|
|
|
' -m '${VMEM}' -cpu host -smp '${VCPUS} \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -kernel ' "/boot/vmlinuz-$(uname -r)" \
|
2022-07-06 07:29:00 +00:00
|
|
|
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -nodefaults' \
|
|
|
|
' -append "console=ttyS0 mitigations=off apparmor=0 ' \
|
|
|
|
'virtio-net.napi_tx=1"' \
|
|
|
|
" -device virtio-net-pci,netdev=hostnet0,x-txburst=16384" \
|
2022-08-18 06:13:58 +00:00
|
|
|
" -netdev socket,fd=5,id=hostnet0" \
|
|
|
|
' -pidfile passt_qemu.pid'
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST
|
2022-07-15 05:21:31 +00:00
|
|
|
|
|
|
|
# Set things up to reduce problems parsing host command output
|
|
|
|
pane_run HOST "PS1='$ '"
|
|
|
|
pane_wait HOST
|
|
|
|
# Non-bash shells will fail this, but also don't need it, so ignore errors
|
|
|
|
pane_run HOST "bind 'set enable-bracketed-paste off'"
|
|
|
|
pane_wait HOST
|
2021-09-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# setup_pasta() - Create a network and user namespace, connect pasta to it
|
|
|
|
setup_pasta() {
|
|
|
|
layout_pasta
|
|
|
|
|
2022-03-02 04:00:21 +00:00
|
|
|
pane_run NS 'echo $$'
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait NS
|
2022-03-02 04:00:21 +00:00
|
|
|
__tty_pid="$(pane_parse NS)"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-03-02 04:00:21 +00:00
|
|
|
pane_run NS "unshare -rUnpf /bin/sh"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status NS
|
2022-03-02 04:00:21 +00:00
|
|
|
|
|
|
|
pane_run PASST "pstree -p ${__tty_pid} | sed -n 's/.*(\([0-9].*\))$/\1/p'"
|
|
|
|
pane_wait PASST
|
|
|
|
__target_pid="$(pane_parse PASST)"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
# Ports:
|
|
|
|
#
|
|
|
|
# ns | host
|
|
|
|
# ------------------|---------------------
|
|
|
|
# 10002 as server | spliced to ns
|
|
|
|
# 10003 spliced to init | as server
|
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/pasta.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-08-18 06:13:56 +00:00
|
|
|
pane_run PASST "./pasta ${__opts} -f -t 10002 -T 10003 -u 10002 -U 10003 -P passt.pid ${__target_pid}"
|
2021-09-27 13:10:35 +00:00
|
|
|
sleep 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup_passt_in_ns() - Set up namespace (with pasta), run qemu and passt into it
|
|
|
|
setup_passt_in_ns() {
|
|
|
|
layout_passt_in_pasta
|
|
|
|
|
|
|
|
# Ports:
|
|
|
|
#
|
|
|
|
# guest | ns | host
|
|
|
|
# -------------|--------------------|-----------------
|
|
|
|
# 10001 as server | forwarded to guest | spliced to ns
|
|
|
|
# 10002 | as server | spliced to ns
|
|
|
|
# 10003 | spliced to init | as server
|
|
|
|
# 10011 as server | forwarded to guest | spliced to ns
|
|
|
|
# 10012 | as server | spliced to ns
|
|
|
|
# 10013 | spliced to init | as server
|
2021-10-21 02:54:05 +00:00
|
|
|
#
|
|
|
|
# 10021 as server | forwarded to guest |
|
|
|
|
# 10031 as server | forwarded to guest |
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/pasta_with_passt.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
__pid_file="$(mktemp)"
|
|
|
|
pane_run PASST "./pasta ${__opts} -t 10001,10002,10011,10012 -T 10003,10013 -u 10001,10002,10011,10012 -U 10003,10013 -P ${__pid_file}"
|
2021-09-27 13:10:35 +00:00
|
|
|
sleep 1
|
|
|
|
pane_run PASST ''
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
__pasta_pid="$(cat "${__pid_file}")"
|
|
|
|
__ns_pid="$(cat /proc/${__pasta_pid}/task/${__pasta_pid}/children | cut -f1 -d' ')"
|
|
|
|
rm "${__pid_file}"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
pane_run GUEST "nsenter -t ${__ns_pid} -U -n --preserve-credentials"
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run NS "nsenter -t ${__ns_pid} -U -n -p --preserve-credentials"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST
|
|
|
|
pane_status NS
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-06-10 02:32:42 +00:00
|
|
|
pane_run NS "ip -j link show | jq -rM '.[] | select(.link_type == \"ether\").ifname'"
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait NS
|
|
|
|
__ifname="$(pane_parse NS)"
|
2022-06-10 02:32:44 +00:00
|
|
|
pane_run NS "/sbin/dhclient -4 --no-pid ${__ifname}"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status NS
|
2021-09-27 13:10:35 +00:00
|
|
|
sleep 2
|
2022-06-10 02:32:43 +00:00
|
|
|
pane_run NS "/sbin/dhclient -6 --no-pid ${__ifname}"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status NS
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/passt_in_pasta.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-03-15 19:16:13 +00:00
|
|
|
if [ ${VALGRIND} -eq 1 ]; then
|
|
|
|
pane_run PASST "make clean"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "make valgrind"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "valgrind --max-stackframe=$((4 * 1024 * 1024)) --trace-children=yes --vgdb=no --error-exitcode=1 --suppressions=test/valgrind.supp ./passt -f ${__opts} -t 10001,10011,10021,10031 -u 10001,10011,10021,10031 -P passt.pid"
|
|
|
|
else
|
|
|
|
pane_run PASST "make clean"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "make"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
pane_run PASST "./passt -f ${__opts} -t 10001,10011,10021,10031 -u 10001,10011,10021,10031 -P passt.pid"
|
|
|
|
fi
|
|
|
|
sleep 5
|
2021-09-27 13:10:35 +00:00
|
|
|
|
2022-07-06 07:28:58 +00:00
|
|
|
pane_run GUEST './qrap 5 qemu-system-$(uname -m)' \
|
|
|
|
' -machine accel=kvm' \
|
|
|
|
' -M accel=kvm:tcg' \
|
|
|
|
' -m '${VMEM}' -cpu host -smp '${VCPUS} \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -kernel ' "/boot/vmlinuz-$(uname -r)" \
|
2022-07-06 07:29:00 +00:00
|
|
|
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -nodefaults' \
|
|
|
|
' -append "console=ttyS0 mitigations=off apparmor=0 ' \
|
|
|
|
'virtio-net.napi_tx=1"' \
|
|
|
|
" -device virtio-net-pci,netdev=hostnet0,x-txburst=524288" \
|
2022-08-18 06:13:58 +00:00
|
|
|
" -netdev socket,fd=5,id=hostnet0" \
|
|
|
|
' -pidfile passt_in_ns_qemu.pid'
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST
|
2022-07-15 05:21:31 +00:00
|
|
|
|
|
|
|
# Set things up to reduce problems parsing host command output
|
|
|
|
pane_run HOST "PS1='$ '"
|
|
|
|
pane_wait HOST
|
|
|
|
# Non-bash shells will fail this, but also don't need it, so ignore errors
|
|
|
|
pane_run HOST "bind 'set enable-bracketed-paste off'"
|
|
|
|
pane_wait HOST
|
2021-09-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# setup_two_guests() - Set up two namespace, run qemu and passt in both of them
|
|
|
|
setup_two_guests() {
|
|
|
|
layout_two_guests
|
|
|
|
|
|
|
|
# Ports:
|
|
|
|
#
|
|
|
|
# guest #1 | guest #2 | ns #1 | ns #2 | host
|
|
|
|
# --------- |-----------|-----------|------------|------------
|
|
|
|
# 10001 as server | | to guest | to init | to ns #1
|
|
|
|
# 10002 | | as server | | to ns #1
|
|
|
|
# 10003 | | to init | to init | as server
|
|
|
|
# 10004 | as server | to init | to guest | to ns #2
|
|
|
|
# 10005 | | | as server | to ns #2
|
|
|
|
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
__pid1_file="$(mktemp)"
|
|
|
|
__pid2_file="$(mktemp)"
|
|
|
|
|
2021-09-27 13:10:35 +00:00
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/pasta_1.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
pane_run PASST_1 "./pasta ${__opts} -P ${__pid1_file} -t 10001,10002 -T 10003,10004 -u 10001,10002 -U 10003,10004"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/pasta_2.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
pane_run PASST_2 "./pasta ${__opts} -P ${__pid2_file} -t 10004,10005 -T 10003,10001 -u 10004,10005 -U 10003,10001"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
sleep 1
|
|
|
|
pane_run PASST_1 ''
|
|
|
|
pane_run PASST_2 ''
|
|
|
|
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status PASST_1
|
|
|
|
pane_status PASST_2
|
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as
implemented by --enable-sandbox in slirp4netns, we need to create a
new mount namespace and pivot_root() into a new (empty) mountpoint, so
that passt and pasta can't access any filesystem resource after
initialisation.
While at it, also detach IPC, PID (only for passt, to prevent
vulnerabilities based on the knowledge of a target PID), and UTS
namespaces.
With this approach, if we apply the seccomp filters right after the
configuration step, the number of allowed syscalls grows further. To
prevent this, defer the application of seccomp policies after the
initialisation phase, before the main loop, that's where we expect bad
things to happen, potentially. This way, we get back to 22 allowed
syscalls for passt and 34 for pasta, on x86_64.
While at it, move #syscalls notes to specific code paths wherever it
conceptually makes sense.
We have to open all the file handles we'll ever need before
sandboxing:
- the packet capture file can only be opened once, drop instance
numbers from the default path and use the (pre-sandbox) PID instead
- /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection
of bound ports in pasta mode, are now opened only once, before
sandboxing, and their handles are stored in the execution context
- the UNIX domain socket for passt is also bound only once, before
sandboxing: to reject clients after the first one, instead of
closing the listening socket, keep it open, accept and immediately
discard new connection if we already have a valid one
Clarify the (unchanged) behaviour for --netns-only in the man page.
To actually make passt and pasta processes run in a separate PID
namespace, we need to unshare(CLONE_NEWPID) before forking to
background (if configured to do so). Introduce a small daemon()
implementation, __daemon(), that additionally saves the PID file
before forking. While running in foreground, the process itself can't
move to a new PID namespace (a process can't change the notion of its
own PID): mention that in the man page.
For some reason, fork() in a detached PID namespace causes SIGTERM
and SIGQUIT to be ignored, even if the handler is still reported as
SIG_DFL: add a signal handler that just exits.
We can now drop most of the pasta_child_handler() implementation,
that took care of terminating all processes running in the same
namespace, if pasta started a shell: the shell itself is now the
init process in that namespace, and all children will terminate
once the init process exits.
Issuing 'echo $$' in a detached PID namespace won't return the
actual namespace PID as seen from the init namespace: adapt
demo and test setup scripts to reflect that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2022-02-07 20:11:37 +00:00
|
|
|
__pasta1_pid="$(cat "${__pid1_file}")"
|
|
|
|
__ns1_pid="$(cat /proc/${__pasta1_pid}/task/${__pasta1_pid}/children | cut -f1 -d' ')"
|
|
|
|
rm "${__pid1_file}"
|
|
|
|
__pasta2_pid="$(cat "${__pid2_file}")"
|
|
|
|
__ns2_pid="$(cat /proc/${__pasta2_pid}/task/${__pasta2_pid}/children | cut -f1 -d' ')"
|
|
|
|
rm "${__pid2_file}"
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
pane_run GUEST_1 "nsenter -t ${__ns1_pid} -U -n --preserve-credentials"
|
|
|
|
pane_run GUEST_2 "nsenter -t ${__ns2_pid} -U -n --preserve-credentials"
|
|
|
|
|
2022-06-10 02:32:42 +00:00
|
|
|
pane_run PASST_1 "ip -j link show | jq -rM '.[] | select(.link_type == \"ether\").ifname'"
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait PASST_1
|
|
|
|
__ifname="$(pane_parse PASST_1)"
|
|
|
|
|
2022-07-15 05:21:37 +00:00
|
|
|
pane_run GUEST_1 "/sbin/dhclient -4 --no-pid ${__ifname}"
|
|
|
|
pane_run GUEST_2 "/sbin/dhclient -4 --no-pid ${__ifname}"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST_1
|
|
|
|
pane_status GUEST_2
|
2021-09-27 13:10:35 +00:00
|
|
|
sleep 2
|
2022-07-15 05:21:37 +00:00
|
|
|
pane_run GUEST_1 "/sbin/dhclient -6 --no-pid ${__ifname}"
|
|
|
|
pane_run GUEST_2 "/sbin/dhclient -6 --no-pid ${__ifname}"
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST_1
|
|
|
|
pane_status GUEST_2
|
2021-09-27 13:10:35 +00:00
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/passt_1.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
|
|
|
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_run PASST_1 "./passt -f ${__opts} -t 10001 -u 10001"
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
__opts=
|
|
|
|
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p /tmp/passt_2.pcap"
|
|
|
|
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
|
2022-03-14 23:59:09 +00:00
|
|
|
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_run PASST_2 "./passt -f ${__opts} -t 10004 -u 10004"
|
|
|
|
|
2022-07-06 07:28:58 +00:00
|
|
|
pane_run GUEST_1 './qrap 5 qemu-system-$(uname -m)' \
|
|
|
|
' -M accel=kvm:tcg' \
|
|
|
|
' -m '${VMEM}' -cpu host -smp '${VCPUS} \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -kernel ' "/boot/vmlinuz-$(uname -r)" \
|
2022-07-06 07:29:00 +00:00
|
|
|
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -nodefaults' \
|
|
|
|
' -append "console=ttyS0 mitigations=off apparmor=0 ' \
|
|
|
|
'virtio-net.napi_tx=1"' \
|
|
|
|
" -device virtio-net-pci,netdev=hostnet0,x-txburst=16384" \
|
2022-08-18 06:13:58 +00:00
|
|
|
" -netdev socket,fd=5,id=hostnet0" \
|
|
|
|
' -pidfile two_guests_qemu_1.pid'
|
2022-07-06 07:28:58 +00:00
|
|
|
pane_run GUEST_2 './qrap 5 qemu-system-$(uname -m)' \
|
|
|
|
' -M accel=kvm:tcg' \
|
|
|
|
' -m '${VMEM}' -cpu host -smp '${VCPUS} \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -kernel ' "/boot/vmlinuz-$(uname -r)" \
|
2022-07-06 07:29:00 +00:00
|
|
|
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
|
2022-01-26 06:32:56 +00:00
|
|
|
' -nodefaults' \
|
|
|
|
' -append "console=ttyS0 mitigations=off apparmor=0 ' \
|
|
|
|
'virtio-net.napi_tx=1"' \
|
|
|
|
" -device virtio-net-pci,netdev=hostnet0,x-txburst=16384" \
|
2022-08-18 06:13:58 +00:00
|
|
|
" -netdev socket,fd=5,id=hostnet0" \
|
|
|
|
' -pidfile two_guests_qemu_2.pid'
|
2022-05-12 03:36:21 +00:00
|
|
|
pane_status GUEST_1
|
|
|
|
pane_status GUEST_2
|
2021-09-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 06:13:53 +00:00
|
|
|
# teardown_build() - Nothing to do, yet
|
|
|
|
teardown_build() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
|
|
|
# teardown_distro() - Nothing to do, yet
|
|
|
|
teardown_distro() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:16:13 +00:00
|
|
|
# teardown_passt() - Kill qemu, remove passt PID file
|
2021-09-27 13:10:35 +00:00
|
|
|
teardown_passt() {
|
2022-08-18 06:13:58 +00:00
|
|
|
kill $(cat passt_qemu.pid)
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait GUEST
|
2022-03-15 19:16:13 +00:00
|
|
|
rm passt.pid
|
2021-09-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# teardown_passt() - Exit namespace, kill pasta process
|
|
|
|
teardown_pasta() {
|
|
|
|
tmux send-keys -t ${PANE_NS} "C-d"
|
|
|
|
pane_wait NS
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:16:13 +00:00
|
|
|
# teardown_passt_in_ns() - Exit namespace, kill qemu and pasta, remove pid file
|
2021-09-27 13:10:35 +00:00
|
|
|
teardown_passt_in_ns() {
|
2022-08-18 06:13:58 +00:00
|
|
|
kill $(cat passt_in_ns_qemu.pid)
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait GUEST
|
|
|
|
tmux send-keys -t ${PANE_GUEST} "C-d"
|
|
|
|
|
|
|
|
tmux send-keys -t ${PANE_PASST} "C-d"
|
|
|
|
|
|
|
|
pane_wait GUEST
|
|
|
|
pane_wait NS
|
|
|
|
pane_wait PASST
|
2022-03-15 19:16:13 +00:00
|
|
|
|
|
|
|
rm passt.pid
|
2021-09-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# teardown_two_guests() - Exit namespaces, kill qemu processes, passt and pasta
|
|
|
|
teardown_two_guests() {
|
2022-08-18 06:13:58 +00:00
|
|
|
kill $(cat two_guests_qemu_1.pid)
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait GUEST_1
|
|
|
|
tmux send-keys -t ${PANE_GUEST_1} "C-d"
|
|
|
|
|
2022-08-18 06:13:58 +00:00
|
|
|
kill $(cat two_guests_qemu_2.pid)
|
2021-09-27 13:10:35 +00:00
|
|
|
pane_wait GUEST_2
|
|
|
|
tmux send-keys -t ${PANE_GUEST_2} "C-d"
|
|
|
|
|
|
|
|
tmux send-keys -t ${PANE_PASST_1} "C-c"
|
|
|
|
pane_wait PASST_1
|
|
|
|
tmux send-keys -t ${PANE_PASST_1} "C-d"
|
|
|
|
|
|
|
|
tmux send-keys -t ${PANE_PASST_2} "C-c"
|
|
|
|
pane_wait PASST_2
|
|
|
|
tmux send-keys -t ${PANE_PASST_2} "C-d"
|
|
|
|
|
|
|
|
pane_wait GUEST_1
|
|
|
|
pane_wait GUEST_2
|
|
|
|
pane_wait PASST_1
|
|
|
|
pane_wait PASST_2
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:45:26 +00:00
|
|
|
# teardown_demo_passt() - Exit namespace, kill qemu, passt and pasta
|
|
|
|
teardown_demo_passt() {
|
|
|
|
tmux send-keys -t ${PANE_GUEST} "C-c"
|
|
|
|
pane_wait GUEST
|
|
|
|
|
2022-07-21 12:01:28 +00:00
|
|
|
tmux send-keys -t ${PANE_GUEST} "C-d"
|
2021-09-29 14:45:26 +00:00
|
|
|
tmux send-keys -t ${PANE_PASST} "C-c"
|
|
|
|
|
|
|
|
pane_wait GUEST
|
|
|
|
pane_wait HOST
|
|
|
|
pane_wait PASST
|
2022-02-21 12:35:45 +00:00
|
|
|
|
|
|
|
tmux kill-pane -a -t 0
|
|
|
|
tmux send-keys -t 0 "C-c"
|
2021-09-29 14:45:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:35:45 +00:00
|
|
|
# teardown_demo_pasta() - Exit perf and namespace from remaining pane
|
2021-09-29 14:45:26 +00:00
|
|
|
teardown_demo_pasta() {
|
2022-02-21 12:35:45 +00:00
|
|
|
tmux send-keys -t ${PANE_NS} "q"
|
|
|
|
pane_wait NS
|
2021-09-29 14:45:26 +00:00
|
|
|
tmux send-keys -t ${PANE_NS} "C-d"
|
|
|
|
pane_wait NS
|
2022-02-21 12:35:45 +00:00
|
|
|
|
|
|
|
tmux kill-pane -a -t 0
|
|
|
|
tmux send-keys -t 0 "C-c"
|
|
|
|
}
|
|
|
|
|
|
|
|
# teardown_demo_podman() - Exit namespaces
|
|
|
|
teardown_demo_podman() {
|
|
|
|
tmux send-keys -t ${PANE_NS1} "C-d"
|
|
|
|
tmux send-keys -t ${PANE_NS2} "C-d"
|
|
|
|
pane_wait NS1
|
|
|
|
pane_wait NS2
|
|
|
|
|
|
|
|
tmux kill-pane -a -t 0
|
|
|
|
tmux send-keys -t 0 "C-c"
|
2021-09-29 14:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:10:35 +00:00
|
|
|
# setup() - Run setup_*() functions
|
|
|
|
# $*: Suffix list of setup_*() functions to be called
|
|
|
|
setup() {
|
|
|
|
for arg do
|
|
|
|
eval setup_${arg}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# teardown() - Run teardown_*() functions
|
|
|
|
# $*: Suffix list of teardown_*() functions to be called
|
|
|
|
teardown() {
|
|
|
|
for arg do
|
|
|
|
eval teardown_${arg}
|
|
|
|
done
|
|
|
|
}
|