2015-01-15 03:20:45 +01:00
|
|
|
#
|
2015-02-05 15:46:40 +01:00
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2020-06-06 03:40:10 +02:00
|
|
|
# Copyright (C) 2015-2020
|
2015-02-05 15:46:40 +01:00
|
|
|
# Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# 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.
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-15 03:20:45 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2015-03-22 01:40:19 +01:00
|
|
|
from distutils import spawn
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
import asyncio
|
2015-01-15 03:20:45 +01:00
|
|
|
import subprocess
|
2018-04-24 18:30:55 +02:00
|
|
|
import sys
|
2015-01-15 03:20:45 +01:00
|
|
|
import time
|
2018-04-24 18:30:55 +02:00
|
|
|
import unittest
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
import qubes.tests
|
2017-09-28 02:58:45 +02:00
|
|
|
import qubes.firewall
|
2018-10-15 00:38:38 +02:00
|
|
|
import qubes.vm.qubesvm
|
2016-03-02 02:46:15 +01:00
|
|
|
import qubes.vm.appvm
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2018-10-15 00:38:38 +02:00
|
|
|
|
|
|
|
# noinspection PyAttributeOutsideInit,PyPep8Naming
|
2017-07-12 19:01:15 +02:00
|
|
|
class VmNetworkingMixin(object):
|
2015-02-05 15:46:40 +01:00
|
|
|
test_ip = '192.168.123.45'
|
|
|
|
test_name = 'test.example.com'
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
ping_cmd = 'ping -W 1 -n -c 1 {target}'
|
|
|
|
ping_ip = ping_cmd.format(target=test_ip)
|
|
|
|
ping_name = ping_cmd.format(target=test_name)
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2016-03-07 01:25:34 +01:00
|
|
|
# filled by load_tests
|
|
|
|
template = None
|
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
def run_cmd(self, vm, cmd, user="root"):
|
2018-10-15 00:38:38 +02:00
|
|
|
'''Run a command *cmd* in a *vm* as *user*. Return its exit code.
|
2018-10-15 00:30:17 +02:00
|
|
|
:type self: qubes.tests.SystemTestCase | VmNetworkingMixin
|
2018-10-15 00:38:38 +02:00
|
|
|
:param qubes.vm.qubesvm.QubesVM vm: VM object to run command in
|
|
|
|
:param str cmd: command to execute
|
|
|
|
:param std user: user to execute command as
|
|
|
|
:return int: command exit code
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
2017-09-28 02:58:45 +02:00
|
|
|
self.loop.run_until_complete(vm.run_for_stdio(cmd, user=user))
|
2017-04-18 10:16:14 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
return e.returncode
|
|
|
|
return 0
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2015-03-22 01:40:50 +01:00
|
|
|
super(VmNetworkingMixin, self).setUp()
|
2017-12-05 03:57:30 +01:00
|
|
|
if self.template.startswith('whonix-'):
|
2016-06-24 04:36:37 +02:00
|
|
|
self.skipTest("Test not supported here - Whonix uses its own "
|
|
|
|
"firewall settings")
|
2019-10-28 03:30:49 +01:00
|
|
|
if self.template.endswith('-minimal'):
|
|
|
|
self.skipTest(
|
|
|
|
"Test not supported here - minimal template don't have "
|
|
|
|
"networking packages by default")
|
2016-03-02 02:46:15 +01:00
|
|
|
self.init_default_template(self.template)
|
|
|
|
self.testnetvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('netvm1'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(self.testnetvm.create_on_disk())
|
2016-03-02 02:46:15 +01:00
|
|
|
self.testnetvm.provides_network = True
|
2018-10-15 00:39:32 +02:00
|
|
|
self.testnetvm.netvm = None
|
2016-03-02 02:46:15 +01:00
|
|
|
self.testvm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2017-09-28 03:40:45 +02:00
|
|
|
name=self.make_vm_name('vm1'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.create_on_disk())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm1.netvm = self.testnetvm
|
2016-03-02 02:46:15 +01:00
|
|
|
self.app.save()
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
self.configure_netvm()
|
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
def configure_netvm(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2015-01-15 03:20:45 +01:00
|
|
|
def run_netvm_cmd(cmd):
|
2019-12-03 13:24:58 +01:00
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.testnetvm.run_for_stdio(cmd, user='root'))
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self.fail("Command '%s' failed: %s%s" %
|
|
|
|
(cmd, e.stdout.decode(), e.stderr.decode()))
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
if not self.testnetvm.is_running():
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testnetvm.start())
|
2015-01-15 03:20:45 +01:00
|
|
|
# Ensure that dnsmasq is installed:
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(self.testnetvm.run_for_stdio(
|
|
|
|
'dnsmasq --version', user='root'))
|
|
|
|
except subprocess.CalledProcessError:
|
2015-01-15 03:20:45 +01:00
|
|
|
self.skipTest("dnsmasq not installed")
|
|
|
|
|
|
|
|
run_netvm_cmd("ip link add test0 type dummy")
|
|
|
|
run_netvm_cmd("ip link set test0 up")
|
2015-02-05 15:46:40 +01:00
|
|
|
run_netvm_cmd("ip addr add {}/24 dev test0".format(self.test_ip))
|
2018-10-24 14:15:18 +02:00
|
|
|
run_netvm_cmd("iptables -I INPUT -d {} -j ACCEPT --wait".format(
|
|
|
|
self.test_ip))
|
2016-06-24 04:37:06 +02:00
|
|
|
# ignore failure
|
2019-04-13 16:42:37 +02:00
|
|
|
self.run_cmd(self.testnetvm, "pkill dnsmasq")
|
2015-02-05 15:46:40 +01:00
|
|
|
run_netvm_cmd("dnsmasq -a {ip} -A /{name}/{ip} -i test0 -z".format(
|
|
|
|
ip=self.test_ip, name=self.test_name))
|
|
|
|
run_netvm_cmd("echo nameserver {} > /etc/resolv.conf".format(
|
|
|
|
self.test_ip))
|
2015-01-15 03:20:45 +01:00
|
|
|
run_netvm_cmd("/usr/lib/qubes/qubes-setup-dnat-to-ns")
|
|
|
|
|
|
|
|
|
|
|
|
def test_000_simple_networking(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2015-02-05 15:46:40 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
def test_010_simple_proxyvm(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-03-02 02:46:15 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm1.netvm = self.proxy
|
2016-03-07 01:25:34 +01:00
|
|
|
self.app.save()
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
|
|
|
|
"Ping by IP from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
|
|
|
|
"Ping by name from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP from AppVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by IP from AppVM failed")
|
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2016-02-28 03:44:33 +01:00
|
|
|
@qubes.tests.expectedFailureIfTemplate('debian-7')
|
2015-03-22 01:40:19 +01:00
|
|
|
@unittest.skipUnless(spawn.find_executable('xdotool'),
|
|
|
|
"xdotool not installed")
|
2015-01-15 03:20:45 +01:00
|
|
|
def test_020_simple_proxyvm_nm(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-03-07 01:25:34 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
2017-09-28 03:41:37 +02:00
|
|
|
self.proxy.features['service.network-manager'] = True
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm1.netvm = self.proxy
|
2016-03-02 02:46:15 +01:00
|
|
|
self.app.save()
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name failed")
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
# reconnect to make sure that device was configured by NM
|
|
|
|
self.assertEqual(
|
|
|
|
self.run_cmd(self.proxy, "nmcli device disconnect eth0",
|
2015-02-05 15:46:40 +01:00
|
|
|
user="user"),
|
2015-01-15 03:20:45 +01:00
|
|
|
0, "Failed to disconnect eth0 using nmcli")
|
|
|
|
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
2015-02-05 15:46:40 +01:00
|
|
|
"Network should be disabled, but apparently it isn't")
|
2015-01-15 03:20:45 +01:00
|
|
|
self.assertEqual(
|
2015-02-05 15:46:40 +01:00
|
|
|
self.run_cmd(self.proxy,
|
|
|
|
'nmcli connection up "VM uplink eth0" ifname eth0',
|
|
|
|
user="user"),
|
2015-01-15 03:20:45 +01:00
|
|
|
0, "Failed to connect eth0 using nmcli")
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, "nm-online", user="user"), 0,
|
|
|
|
"Failed to wait for NM connection")
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
# check for nm-applet presence
|
|
|
|
self.assertEqual(subprocess.call([
|
2016-07-19 02:11:55 +02:00
|
|
|
'xdotool', 'search', '--class', '{}:nm-applet'.format(
|
|
|
|
self.proxy.name)],
|
2017-09-28 03:41:37 +02:00
|
|
|
stdout=subprocess.DEVNULL), 0, "nm-applet window not found")
|
2015-01-15 03:20:45 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP failed (after NM reconnection")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name failed (after NM reconnection)")
|
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
def test_030_firewallvm_firewall(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-03-02 02:46:15 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
2016-03-02 02:46:15 +01:00
|
|
|
self.app.save()
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
# block all for first
|
|
|
|
|
2017-09-28 02:58:45 +02:00
|
|
|
self.testvm1.firewall.rules = [qubes.firewall.Rule(action='drop')]
|
2016-09-12 06:04:23 +02:00
|
|
|
self.testvm1.firewall.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
|
2018-10-21 16:26:39 +02:00
|
|
|
server = self.loop.run_until_complete(self.testnetvm.run(
|
2018-10-21 19:38:21 +02:00
|
|
|
'socat TCP-LISTEN:1234,fork EXEC:/bin/uname'))
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
try:
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
|
|
|
|
"Ping by IP from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
|
|
|
|
"Ping by name from ProxyVM failed")
|
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP should be blocked")
|
|
|
|
|
2018-10-21 16:26:39 +02:00
|
|
|
client_cmd = "socat TCP:{}:1234 -".format(self.test_ip)
|
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
|
|
|
|
# block all except ICMP
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [(
|
|
|
|
qubes.firewall.Rule(None, action='accept', proto='icmp')
|
|
|
|
)]
|
|
|
|
self.testvm1.firewall.save()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually
|
|
|
|
# applied
|
|
|
|
time.sleep(3)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP failed (should be allowed now)")
|
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name should be blocked")
|
|
|
|
|
|
|
|
# all TCP still blocked
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [
|
|
|
|
qubes.firewall.Rule(None, action='accept', proto='icmp'),
|
|
|
|
qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
|
|
|
|
]
|
|
|
|
self.testvm1.firewall.save()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually
|
|
|
|
# applied
|
|
|
|
time.sleep(3)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name failed (should be allowed now)")
|
2018-10-21 16:26:39 +02:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
|
|
|
|
# block all except target
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [
|
|
|
|
qubes.firewall.Rule(None, action='accept', dsthost=self.test_ip,
|
|
|
|
proto='tcp', dstports=1234),
|
|
|
|
]
|
|
|
|
self.testvm1.firewall.save()
|
|
|
|
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually
|
|
|
|
# applied
|
|
|
|
time.sleep(3)
|
2018-10-21 16:26:39 +02:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection failed (should be allowed now)")
|
|
|
|
|
|
|
|
# allow all except target
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [
|
|
|
|
qubes.firewall.Rule(None, action='drop', dsthost=self.test_ip,
|
|
|
|
proto='tcp', dstports=1234),
|
2017-09-28 02:58:45 +02:00
|
|
|
qubes.firewall.Rule(action='accept'),
|
2017-06-01 00:33:53 +02:00
|
|
|
]
|
|
|
|
self.testvm1.firewall.save()
|
|
|
|
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually
|
|
|
|
# applied
|
|
|
|
time.sleep(3)
|
2018-10-21 16:26:39 +02:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
finally:
|
2018-10-21 16:26:39 +02:00
|
|
|
server.terminate()
|
|
|
|
self.loop.run_until_complete(server.wait())
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_040_inter_vm(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-03-02 02:46:15 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-03-07 01:25:34 +01:00
|
|
|
self.proxy.provides_network = True
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
|
2016-03-02 02:46:15 +01:00
|
|
|
self.testvm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2017-09-28 03:40:45 +02:00
|
|
|
name=self.make_vm_name('vm2'),
|
2016-03-02 02:46:15 +01:00
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm2.create_on_disk())
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm2.netvm = self.proxy
|
2016-03-02 02:46:15 +01:00
|
|
|
self.app.save()
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(asyncio.wait([
|
|
|
|
self.testvm1.start(),
|
|
|
|
self.testvm2.start()]))
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1,
|
2015-02-05 15:46:40 +01:00
|
|
|
self.ping_cmd.format(target=self.testvm2.ip)), 0)
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
self.testvm2.netvm = self.testnetvm
|
|
|
|
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1,
|
2015-02-05 15:46:40 +01:00
|
|
|
self.ping_cmd.format(target=self.testvm2.ip)), 0)
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm2,
|
2015-02-05 15:46:40 +01:00
|
|
|
self.ping_cmd.format(target=self.testvm1.ip)), 0)
|
2015-01-15 03:20:45 +01:00
|
|
|
|
|
|
|
self.testvm1.netvm = self.testnetvm
|
|
|
|
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1,
|
2015-02-05 15:46:40 +01:00
|
|
|
self.ping_cmd.format(target=self.testvm2.ip)), 0)
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm2,
|
2015-02-05 15:46:40 +01:00
|
|
|
self.ping_cmd.format(target=self.testvm1.ip)), 0)
|
2015-03-22 01:40:50 +01:00
|
|
|
|
2015-06-27 04:46:17 +02:00
|
|
|
def test_050_spoof_ip(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Test if VM IP spoofing is blocked
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2015-06-27 04:46:17 +02:00
|
|
|
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
2017-12-03 03:15:14 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testnetvm,
|
|
|
|
'iptables -I INPUT -i vif+ ! -s {} -p icmp -j LOG'.format(
|
|
|
|
self.testvm1.ip)), 0)
|
2017-09-28 03:41:37 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.run_for_stdio(
|
|
|
|
'ip addr flush dev eth0 && '
|
|
|
|
'ip addr add 10.137.1.128/24 dev eth0 && '
|
|
|
|
'ip route add default dev eth0',
|
|
|
|
user='root'))
|
2015-06-27 04:46:17 +02:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Spoofed ping should be blocked")
|
2017-12-03 03:15:14 +01:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testnetvm.run_for_stdio('iptables -nxvL INPUT',
|
|
|
|
user='root'))
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.fail('iptables -nxvL INPUT failed')
|
|
|
|
|
|
|
|
output = output.decode().splitlines()
|
|
|
|
packets = output[2].lstrip().split()[0]
|
|
|
|
self.assertEquals(packets, '0', 'Some packet hit the INPUT rule')
|
2015-03-22 01:40:50 +01:00
|
|
|
|
2016-07-12 06:28:01 +02:00
|
|
|
def test_100_late_xldevd_startup(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Regression test for #1990
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-07-12 06:28:01 +02:00
|
|
|
# Simulater late xl devd startup
|
|
|
|
cmd = "systemctl stop xendriverdomain"
|
|
|
|
if self.run_cmd(self.testnetvm, cmd) != 0:
|
|
|
|
self.fail("Command '%s' failed" % cmd)
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-07-12 06:28:01 +02:00
|
|
|
|
|
|
|
cmd = "systemctl start xendriverdomain"
|
|
|
|
if self.run_cmd(self.testnetvm, cmd) != 0:
|
|
|
|
self.fail("Command '%s' failed" % cmd)
|
|
|
|
|
2019-12-03 13:43:19 +01:00
|
|
|
# let it initialize the interface(s)
|
|
|
|
time.sleep(1)
|
|
|
|
|
2016-07-12 06:28:01 +02:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
2016-03-07 01:25:34 +01:00
|
|
|
|
2019-02-23 15:42:48 +01:00
|
|
|
def test_110_dynamic_attach(self):
|
|
|
|
self.testvm1.netvm = None
|
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
self.testvm1.netvm = self.testnetvm
|
|
|
|
# wait for it to settle down
|
|
|
|
self.loop.run_until_complete(self.testvm1.run_for_stdio(
|
|
|
|
'udevadm settle'))
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
|
|
|
|
def test_111_dynamic_detach_attach(self):
|
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
self.testvm1.netvm = None
|
|
|
|
# wait for it to settle down
|
|
|
|
self.loop.run_until_complete(self.testvm1.run_for_stdio(
|
|
|
|
'udevadm settle'))
|
|
|
|
self.testvm1.netvm = self.testnetvm
|
|
|
|
# wait for it to settle down
|
|
|
|
self.loop.run_until_complete(self.testvm1.run_for_stdio(
|
|
|
|
'udevadm settle'))
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
|
|
|
|
def test_112_reattach_after_provider_shutdown(self):
|
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
|
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
self.loop.run_until_complete(self.proxy.shutdown(force=True, wait=True))
|
|
|
|
self.loop.run_until_complete(self.proxy.start())
|
|
|
|
# wait for it to settle down
|
2019-10-13 05:58:50 +02:00
|
|
|
self.loop.run_until_complete(self.wait_for_session(self.proxy))
|
2019-02-23 15:42:48 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
|
|
|
|
def test_113_reattach_after_provider_kill(self):
|
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
|
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
self.loop.run_until_complete(self.proxy.kill())
|
|
|
|
self.loop.run_until_complete(self.proxy.start())
|
|
|
|
# wait for it to settle down
|
2019-10-13 05:58:50 +02:00
|
|
|
self.loop.run_until_complete(self.wait_for_session(self.proxy))
|
2019-02-23 15:42:48 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
|
|
|
|
def test_114_reattach_after_provider_crash(self):
|
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
|
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
p = self.loop.run_until_complete(self.proxy.run(
|
|
|
|
'echo c > /proc/sysrq-trigger', user='root'))
|
|
|
|
self.loop.run_until_complete(p.wait())
|
|
|
|
timeout = 10
|
|
|
|
while self.proxy.is_running():
|
|
|
|
self.loop.run_until_complete(asyncio.sleep(1))
|
|
|
|
timeout -= 1
|
|
|
|
self.assertGreater(timeout, 0,
|
|
|
|
'timeout waiting for crash cleanup')
|
|
|
|
self.loop.run_until_complete(self.proxy.start())
|
|
|
|
# wait for it to settle down
|
2019-10-13 05:58:50 +02:00
|
|
|
self.loop.run_until_complete(self.wait_for_session(self.proxy))
|
2019-02-23 15:42:48 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
|
2016-10-31 02:17:21 +01:00
|
|
|
def test_200_fake_ip_simple(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Test hiding VM real IP
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2017-07-18 03:50:02 +02:00
|
|
|
self.testvm1.features['net.fake-ip'] = '192.168.1.128'
|
|
|
|
self.testvm1.features['net.fake-gateway'] = '192.168.1.1'
|
|
|
|
self.testvm1.features['net.fake-netmask'] = '255.255.255.0'
|
2016-11-01 00:28:37 +01:00
|
|
|
self.app.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
2017-04-18 10:16:14 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm1.run_for_stdio(
|
|
|
|
'ip addr show dev eth0', user='root'))
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.fail('ip addr show dev eth0 failed')
|
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertIn('192.168.1.128', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.testvm1.ip), output)
|
2016-10-31 02:17:21 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm1.run_for_stdio('ip route show', user='root'))
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.fail('ip route show failed')
|
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertIn('192.168.1.1', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.testvm1.netvm.ip), output)
|
2016-10-31 02:17:21 +01:00
|
|
|
|
|
|
|
def test_201_fake_ip_without_gw(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Test hiding VM real IP
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2017-07-18 03:50:02 +02:00
|
|
|
self.testvm1.features['net.fake-ip'] = '192.168.1.128'
|
2016-11-01 00:28:37 +01:00
|
|
|
self.app.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
2017-04-18 10:16:14 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm1.run_for_stdio('ip addr show dev eth0',
|
|
|
|
user='root'))
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.fail('ip addr show dev eth0 failed')
|
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertIn('192.168.1.128', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.testvm1.ip), output)
|
2016-10-31 02:17:21 +01:00
|
|
|
|
|
|
|
def test_202_fake_ip_firewall(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Test hiding VM real IP, firewall
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2017-07-18 03:50:02 +02:00
|
|
|
self.testvm1.features['net.fake-ip'] = '192.168.1.128'
|
|
|
|
self.testvm1.features['net.fake-gateway'] = '192.168.1.1'
|
|
|
|
self.testvm1.features['net.fake-netmask'] = '255.255.255.0'
|
2016-10-31 02:17:21 +01:00
|
|
|
|
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-10-31 02:17:21 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
self.app.save()
|
|
|
|
|
|
|
|
# block all but ICMP and DNS
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [
|
|
|
|
qubes.firewall.Rule(None, action='accept', proto='icmp'),
|
|
|
|
qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
|
|
|
|
]
|
|
|
|
self.testvm1.firewall.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 02:17:21 +01:00
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
|
2018-10-21 16:26:39 +02:00
|
|
|
server = self.loop.run_until_complete(self.testnetvm.run(
|
2018-10-21 19:38:21 +02:00
|
|
|
'socat TCP-LISTEN:1234,fork EXEC:/bin/uname'))
|
2016-10-31 03:09:45 +01:00
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
try:
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
|
|
|
|
"Ping by IP from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
|
|
|
|
"Ping by name from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP should be allowed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name should be allowed")
|
2018-10-21 16:26:39 +02:00
|
|
|
client_cmd = "socat TCP:{}:1234 -".format(self.test_ip)
|
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
finally:
|
2018-10-21 16:26:39 +02:00
|
|
|
server.terminate()
|
|
|
|
self.loop.run_until_complete(server.wait())
|
2016-10-31 03:09:45 +01:00
|
|
|
|
2016-10-31 03:39:46 +01:00
|
|
|
def test_203_fake_ip_inter_vm_allow(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Access VM with "fake IP" from other VM (when firewall allows)
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-10-31 03:39:46 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-10-31 03:39:46 +01:00
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
2017-07-18 03:50:02 +02:00
|
|
|
self.testvm1.features['net.fake-ip'] = '192.168.1.128'
|
|
|
|
self.testvm1.features['net.fake-gateway'] = '192.168.1.1'
|
|
|
|
self.testvm1.features['net.fake-netmask'] = '255.255.255.0'
|
2016-10-31 03:39:46 +01:00
|
|
|
|
|
|
|
self.testvm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2017-09-28 03:40:45 +02:00
|
|
|
name=self.make_vm_name('vm2'),
|
2016-10-31 03:39:46 +01:00
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm2.create_on_disk())
|
2016-10-31 03:39:46 +01:00
|
|
|
self.testvm2.netvm = self.proxy
|
|
|
|
self.app.save()
|
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
|
|
|
self.loop.run_until_complete(self.testvm2.start())
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2018-10-15 00:38:38 +02:00
|
|
|
cmd = 'iptables -I FORWARD -s {} -d {} -j ACCEPT'.format(
|
|
|
|
self.testvm2.ip, self.testvm1.ip)
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(self.proxy.run_for_stdio(
|
2017-09-28 03:41:37 +02:00
|
|
|
cmd, user='root'))
|
2017-04-18 10:16:14 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
2017-09-29 03:39:22 +02:00
|
|
|
raise AssertionError(
|
|
|
|
'{} failed with: {}'.format(cmd, e.returncode)) from None
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
2017-09-28 03:41:37 +02:00
|
|
|
cmd = 'iptables -I INPUT -s {} -j ACCEPT'.format(self.testvm2.ip)
|
2017-10-03 11:58:39 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.run_for_stdio(
|
2017-09-28 03:41:37 +02:00
|
|
|
cmd, user='root'))
|
2017-04-18 10:16:14 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
2017-09-29 03:39:22 +02:00
|
|
|
raise AssertionError(
|
|
|
|
'{} failed with: {}'.format(cmd, e.returncode)) from None
|
2016-10-31 03:39:46 +01:00
|
|
|
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm2,
|
|
|
|
self.ping_cmd.format(target=self.testvm1.ip)), 0)
|
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
2017-09-28 03:41:37 +02:00
|
|
|
cmd = 'iptables -nvxL INPUT | grep {}'.format(self.testvm2.ip)
|
2017-06-01 00:33:53 +02:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
2017-09-28 03:41:37 +02:00
|
|
|
self.testvm1.run_for_stdio(cmd, user='root'))
|
2017-04-18 10:16:14 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
2017-09-29 03:39:22 +02:00
|
|
|
raise AssertionError(
|
|
|
|
'{} failed with {}'.format(cmd, e.returncode)) from None
|
2017-02-19 00:07:16 +01:00
|
|
|
self.assertNotEqual(stdout.decode().split()[0], '0',
|
2016-10-31 03:39:46 +01:00
|
|
|
'Packets didn\'t managed to the VM')
|
|
|
|
|
|
|
|
def test_204_fake_ip_proxy(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Test hiding VM real IP
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-10-31 03:39:46 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-10-31 03:39:46 +01:00
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
2017-07-18 03:50:02 +02:00
|
|
|
self.proxy.features['net.fake-ip'] = '192.168.1.128'
|
|
|
|
self.proxy.features['net.fake-gateway'] = '192.168.1.1'
|
|
|
|
self.proxy.features['net.fake-netmask'] = '255.255.255.0'
|
2016-10-31 03:39:46 +01:00
|
|
|
self.testvm1.netvm = self.proxy
|
2016-11-01 00:28:37 +01:00
|
|
|
self.app.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 03:39:46 +01:00
|
|
|
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0)
|
|
|
|
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.proxy.run_for_stdio(
|
|
|
|
'ip addr show dev eth0', user='root'))
|
2018-10-15 00:38:38 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2017-04-18 10:16:14 +02:00
|
|
|
self.fail('ip addr show dev eth0 failed')
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 03:39:46 +01:00
|
|
|
self.assertIn('192.168.1.128', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.testvm1.ip), output)
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.proxy.run_for_stdio(
|
|
|
|
'ip route show', user='root'))
|
2018-10-15 00:38:38 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2017-04-18 10:16:14 +02:00
|
|
|
self.fail('ip route show failed')
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 03:39:46 +01:00
|
|
|
self.assertIn('192.168.1.1', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.testvm1.netvm.ip), output)
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm1.run_for_stdio(
|
|
|
|
'ip addr show dev eth0', user='root'))
|
2018-10-15 00:38:38 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2017-04-18 10:16:14 +02:00
|
|
|
self.fail('ip addr show dev eth0 failed')
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 03:39:46 +01:00
|
|
|
self.assertNotIn('192.168.1.128', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertIn(str(self.testvm1.ip), output)
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
try:
|
|
|
|
(output, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm1.run_for_stdio(
|
|
|
|
'ip route show', user='root'))
|
2018-10-15 00:38:38 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2017-04-18 10:16:14 +02:00
|
|
|
self.fail('ip route show failed')
|
2017-06-01 00:33:53 +02:00
|
|
|
output = output.decode()
|
2016-10-31 03:39:46 +01:00
|
|
|
self.assertIn('192.168.1.128', output)
|
2017-12-23 15:35:46 +01:00
|
|
|
self.assertNotIn(str(self.proxy.ip), output)
|
2016-10-31 03:39:46 +01:00
|
|
|
|
2016-10-31 03:09:45 +01:00
|
|
|
def test_210_custom_ip_simple(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Custom AppVM IP
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-10-31 03:09:45 +01:00
|
|
|
self.testvm1.ip = '192.168.1.1'
|
2016-11-01 00:28:37 +01:00
|
|
|
self.app.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 03:09:45 +01:00
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
|
|
|
|
|
|
|
def test_211_custom_ip_proxy(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Custom ProxyVM IP
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-10-31 03:09:45 +01:00
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-10-31 03:09:45 +01:00
|
|
|
self.proxy.provides_network = True
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.proxy.ip = '192.168.1.1'
|
|
|
|
self.testvm1.netvm = self.proxy
|
2016-11-01 00:28:37 +01:00
|
|
|
self.app.save()
|
2016-10-31 03:09:45 +01:00
|
|
|
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 03:09:45 +01:00
|
|
|
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
|
|
|
|
|
|
|
|
def test_212_custom_ip_firewall(self):
|
2018-10-15 00:30:17 +02:00
|
|
|
'''Custom VM IP and firewall
|
|
|
|
|
|
|
|
:type self: qubes.tests.SystemTestCase | VMNetworkingMixin
|
|
|
|
'''
|
2016-10-31 03:09:45 +01:00
|
|
|
self.testvm1.ip = '192.168.1.1'
|
|
|
|
|
|
|
|
self.proxy = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('proxy'),
|
|
|
|
label='red')
|
|
|
|
self.proxy.provides_network = True
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.proxy.create_on_disk())
|
2016-10-31 03:09:45 +01:00
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
self.app.save()
|
|
|
|
|
|
|
|
# block all but ICMP and DNS
|
|
|
|
|
|
|
|
self.testvm1.firewall.rules = [
|
|
|
|
qubes.firewall.Rule(None, action='accept', proto='icmp'),
|
|
|
|
qubes.firewall.Rule(None, action='accept', specialtarget='dns'),
|
|
|
|
]
|
|
|
|
self.testvm1.firewall.save()
|
2017-04-18 10:16:14 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2016-10-31 03:09:45 +01:00
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
|
2018-10-21 16:26:39 +02:00
|
|
|
server = self.loop.run_until_complete(self.testnetvm.run(
|
2018-10-21 19:38:21 +02:00
|
|
|
'socat TCP-LISTEN:1234,fork EXEC:/bin/uname'))
|
2016-10-31 02:17:21 +01:00
|
|
|
|
2017-06-01 00:33:53 +02:00
|
|
|
try:
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
|
|
|
|
"Ping by IP from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
|
|
|
|
"Ping by name from ProxyVM failed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP should be allowed")
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name should be allowed")
|
2018-10-21 16:26:39 +02:00
|
|
|
client_cmd = "socat TCP:{}:1234 -".format(self.test_ip)
|
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, client_cmd), 0,
|
2017-06-01 00:33:53 +02:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
finally:
|
2018-10-21 16:26:39 +02:00
|
|
|
server.terminate()
|
|
|
|
self.loop.run_until_complete(server.wait())
|
2016-10-31 02:17:21 +01:00
|
|
|
|
2019-02-23 15:42:48 +01:00
|
|
|
|
2018-12-05 03:51:27 +01:00
|
|
|
|
2018-10-07 19:44:48 +02:00
|
|
|
def create_testcases_for_templates():
|
|
|
|
yield from qubes.tests.create_testcases_for_templates('VmNetworking',
|
|
|
|
VmNetworkingMixin, qubes.tests.SystemTestCase,
|
|
|
|
module=sys.modules[__name__])
|
|
|
|
|
2015-03-22 01:40:50 +01:00
|
|
|
def load_tests(loader, tests, pattern):
|
2018-03-30 03:04:15 +02:00
|
|
|
tests.addTests(loader.loadTestsFromNames(
|
2018-10-07 19:44:48 +02:00
|
|
|
create_testcases_for_templates()))
|
2015-03-30 21:33:50 +02:00
|
|
|
return tests
|
2018-10-07 19:44:48 +02:00
|
|
|
|
|
|
|
qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)
|