2015-01-15 03:20:45 +01:00
|
|
|
#!/usr/bin/python
|
2015-02-05 15:46:40 +01:00
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
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
|
|
|
#
|
2015-02-05 15:46:40 +01:00
|
|
|
# Copyright (C) 2015
|
|
|
|
# Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2015-02-05 15:46:40 +01:00
|
|
|
# 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.
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-02-05 15:46:40 +01:00
|
|
|
# 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.
|
2015-01-15 03:20:45 +01:00
|
|
|
#
|
2015-03-22 01:40:19 +01:00
|
|
|
from distutils import spawn
|
2015-02-05 15:46:40 +01:00
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import unittest
|
|
|
|
import time
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection, defaults
|
|
|
|
|
2015-02-05 15:46:40 +01:00
|
|
|
import qubes.tests
|
|
|
|
|
2015-01-15 03:20:45 +01:00
|
|
|
|
2015-03-22 01:40:50 +01:00
|
|
|
class VmNetworkingMixin(qubes.tests.SystemTestsMixin):
|
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
|
|
|
|
|
|
|
def run_cmd(self, vm, cmd, user="root"):
|
|
|
|
p = vm.run(cmd, user=user, passio_popen=True, ignore_stderr=True)
|
|
|
|
p.stdin.close()
|
|
|
|
p.stdout.read()
|
|
|
|
return p.wait()
|
|
|
|
|
|
|
|
def setUp(self):
|
2015-03-22 01:40:50 +01:00
|
|
|
super(VmNetworkingMixin, self).setUp()
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testnetvm = self.qc.add_new_vm("QubesNetVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('netvm1'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testnetvm.create_on_disk(verbose=False)
|
|
|
|
self.testvm1 = self.qc.add_new_vm("QubesAppVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('vm2'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm1.create_on_disk(verbose=False)
|
|
|
|
self.testvm1.netvm = self.testnetvm
|
|
|
|
self.qc.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):
|
|
|
|
def run_netvm_cmd(cmd):
|
|
|
|
if self.run_cmd(self.testnetvm, cmd) != 0:
|
|
|
|
self.fail("Command '%s' failed" % cmd)
|
|
|
|
|
|
|
|
if not self.testnetvm.is_running():
|
|
|
|
self.testnetvm.start()
|
|
|
|
# Ensure that dnsmasq is installed:
|
|
|
|
p = self.testnetvm.run("dnsmasq --version", user="root",
|
|
|
|
passio_popen=True)
|
|
|
|
if p.wait() != 0:
|
|
|
|
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))
|
|
|
|
run_netvm_cmd("iptables -I INPUT -d {} -j ACCEPT".format(self.test_ip))
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
self.proxy = self.qc.add_new_vm("QubesProxyVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.create_on_disk(verbose=False)
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
self.qc.save()
|
|
|
|
|
|
|
|
self.testvm1.start()
|
|
|
|
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
|
|
|
|
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):
|
|
|
|
self.proxy = self.qc.add_new_vm("QubesProxyVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.create_on_disk(verbose=False)
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.proxy.services['network-manager'] = True
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
self.qc.save()
|
|
|
|
|
|
|
|
self.testvm1.start()
|
|
|
|
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([
|
|
|
|
'xdotool', 'search', '--all', '--name',
|
2015-02-05 15:46:40 +01:00
|
|
|
'--class', '^(NetworkManager Applet|{})$'.format(self.proxy.name)],
|
|
|
|
stdout=open('/dev/null', 'w')), 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):
|
|
|
|
self.proxy = self.qc.add_new_vm("QubesProxyVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.create_on_disk(verbose=False)
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
self.qc.save()
|
|
|
|
|
|
|
|
# block all for first
|
|
|
|
|
|
|
|
self.testvm1.write_firewall_conf({
|
|
|
|
'allow': False,
|
|
|
|
'allowDns': False,
|
|
|
|
'allowIcmp': False,
|
|
|
|
})
|
|
|
|
self.testvm1.start()
|
|
|
|
self.assertTrue(self.proxy.is_running())
|
|
|
|
|
|
|
|
self.testnetvm.run("nc -l --send-only -e /bin/hostname -k 1234")
|
|
|
|
|
|
|
|
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")
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
2015-01-15 03:20:45 +01:00
|
|
|
"Ping by IP should be blocked")
|
|
|
|
nc_cmd = "nc -w 1 --recv-only 192.168.123.45 1234"
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
|
2015-01-15 03:20:45 +01:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
|
|
|
|
# block all except ICMP
|
|
|
|
|
|
|
|
self.testvm1.write_firewall_conf({
|
|
|
|
'allow': False,
|
|
|
|
'allowDns': False,
|
|
|
|
'allowIcmp': True,
|
|
|
|
})
|
|
|
|
self.proxy.write_iptables_xenstore_entry()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually applied
|
|
|
|
time.sleep(1)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
|
|
|
|
"Ping by IP failed (should be allowed now)")
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
2015-01-15 03:20:45 +01:00
|
|
|
"Ping by name should be blocked")
|
|
|
|
|
|
|
|
# all TCP still blocked
|
|
|
|
|
|
|
|
self.testvm1.write_firewall_conf({
|
|
|
|
'allow': False,
|
|
|
|
'allowDns': True,
|
|
|
|
'allowIcmp': True,
|
|
|
|
})
|
|
|
|
self.proxy.write_iptables_xenstore_entry()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually applied
|
|
|
|
time.sleep(1)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
|
|
|
|
"Ping by name failed (should be allowed now)")
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
|
2015-01-15 03:20:45 +01:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
|
|
|
|
# block all except target
|
|
|
|
|
|
|
|
self.testvm1.write_firewall_conf({
|
|
|
|
'allow': False,
|
|
|
|
'allowDns': True,
|
|
|
|
'allowIcmp': True,
|
2015-02-05 15:46:40 +01:00
|
|
|
'rules': [{'address': self.ping_ip,
|
2015-01-15 03:20:45 +01:00
|
|
|
'netmask': 32,
|
|
|
|
'proto': 'tcp',
|
|
|
|
'portBegin': 1234
|
|
|
|
}] })
|
|
|
|
self.proxy.write_iptables_xenstore_entry()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually applied
|
|
|
|
time.sleep(1)
|
|
|
|
self.assertEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
|
|
|
|
"TCP connection failed (should be allowed now)")
|
|
|
|
|
|
|
|
# allow all except target
|
|
|
|
|
|
|
|
self.testvm1.write_firewall_conf({
|
|
|
|
'allow': True,
|
|
|
|
'allowDns': True,
|
|
|
|
'allowIcmp': True,
|
2015-02-05 15:46:40 +01:00
|
|
|
'rules': [{'address': self.ping_ip,
|
2015-01-15 03:20:45 +01:00
|
|
|
'netmask': 32,
|
|
|
|
'proto': 'tcp',
|
|
|
|
'portBegin': 1234
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
self.proxy.write_iptables_xenstore_entry()
|
|
|
|
# Ugly hack b/c there is no feedback when the rules are actually applied
|
|
|
|
time.sleep(1)
|
2015-02-02 04:50:37 +01:00
|
|
|
self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
|
2015-01-15 03:20:45 +01:00
|
|
|
"TCP connection should be blocked")
|
|
|
|
|
|
|
|
|
|
|
|
def test_040_inter_vm(self):
|
|
|
|
self.proxy = self.qc.add_new_vm("QubesProxyVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('proxy'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.proxy.create_on_disk(verbose=False)
|
|
|
|
self.proxy.netvm = self.testnetvm
|
|
|
|
self.testvm1.netvm = self.proxy
|
|
|
|
|
|
|
|
self.testvm2 = self.qc.add_new_vm("QubesAppVm",
|
2015-02-05 15:46:40 +01:00
|
|
|
name=self.make_vm_name('vm3'),
|
2015-03-22 01:40:50 +01:00
|
|
|
template=self.qc.get_vm_by_name(self.template))
|
2015-01-15 03:20:45 +01:00
|
|
|
self.testvm2.create_on_disk(verbose=False)
|
|
|
|
self.testvm2.netvm = self.proxy
|
|
|
|
self.qc.save()
|
|
|
|
|
|
|
|
self.testvm1.start()
|
|
|
|
self.testvm2.start()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
try:
|
|
|
|
qc = qubes.qubes.QubesVmCollection()
|
|
|
|
qc.lock_db_for_reading()
|
|
|
|
qc.load()
|
|
|
|
qc.unlock_db()
|
|
|
|
templates = [vm.name for vm in qc.values() if
|
|
|
|
isinstance(vm, qubes.qubes.QubesTemplateVm)]
|
|
|
|
except OSError:
|
|
|
|
templates = []
|
|
|
|
for template in templates:
|
|
|
|
tests.addTests(loader.loadTestsFromTestCase(
|
|
|
|
type(
|
|
|
|
'VmNetworking_' + template,
|
|
|
|
(VmNetworkingMixin, qubes.tests.QubesTestCase),
|
|
|
|
{'template': template})))
|
|
|
|
return tests
|