network.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/python
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2015
  7. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import multiprocessing
  25. import os
  26. import subprocess
  27. import unittest
  28. import time
  29. from qubes.qubes import QubesVmCollection, defaults
  30. import qubes.tests
  31. class VmNetworkingTests(qubes.tests.SystemTestsMixin, unittest.TestCase):
  32. test_ip = '192.168.123.45'
  33. test_name = 'test.example.com'
  34. ping_cmd = 'ping -W 1 -n -c 1 {target}'
  35. ping_ip = ping_cmd.format(target=test_ip)
  36. ping_name = ping_cmd.format(target=test_name)
  37. def run_cmd(self, vm, cmd, user="root"):
  38. p = vm.run(cmd, user=user, passio_popen=True, ignore_stderr=True)
  39. p.stdin.close()
  40. p.stdout.read()
  41. return p.wait()
  42. def setUp(self):
  43. super(VmNetworkingTests, self).setUp()
  44. self.testnetvm = self.qc.add_new_vm("QubesNetVm",
  45. name=self.make_vm_name('netvm1'),
  46. template=self.qc.get_default_template())
  47. self.testnetvm.create_on_disk(verbose=False)
  48. self.testvm1 = self.qc.add_new_vm("QubesAppVm",
  49. name=self.make_vm_name('vm2'),
  50. template=self.qc.get_default_template())
  51. self.testvm1.create_on_disk(verbose=False)
  52. self.testvm1.netvm = self.testnetvm
  53. self.qc.save()
  54. self.configure_netvm()
  55. def configure_netvm(self):
  56. def run_netvm_cmd(cmd):
  57. if self.run_cmd(self.testnetvm, cmd) != 0:
  58. self.fail("Command '%s' failed" % cmd)
  59. if not self.testnetvm.is_running():
  60. self.testnetvm.start()
  61. # Ensure that dnsmasq is installed:
  62. p = self.testnetvm.run("dnsmasq --version", user="root",
  63. passio_popen=True)
  64. if p.wait() != 0:
  65. self.skipTest("dnsmasq not installed")
  66. run_netvm_cmd("ip link add test0 type dummy")
  67. run_netvm_cmd("ip link set test0 up")
  68. run_netvm_cmd("ip addr add {}/24 dev test0".format(self.test_ip))
  69. run_netvm_cmd("iptables -I INPUT -d {} -j ACCEPT".format(self.test_ip))
  70. run_netvm_cmd("dnsmasq -a {ip} -A /{name}/{ip} -i test0 -z".format(
  71. ip=self.test_ip, name=self.test_name))
  72. run_netvm_cmd("echo nameserver {} > /etc/resolv.conf".format(
  73. self.test_ip))
  74. run_netvm_cmd("/usr/lib/qubes/qubes-setup-dnat-to-ns")
  75. def test_000_simple_networking(self):
  76. self.testvm1.start()
  77. self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0)
  78. self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0)
  79. def test_010_simple_proxyvm(self):
  80. self.proxy = self.qc.add_new_vm("QubesProxyVm",
  81. name=self.make_vm_name('proxy'),
  82. template=self.qc.get_default_template())
  83. self.proxy.create_on_disk(verbose=False)
  84. self.proxy.netvm = self.testnetvm
  85. self.testvm1.netvm = self.proxy
  86. self.qc.save()
  87. self.testvm1.start()
  88. self.assertTrue(self.proxy.is_running())
  89. self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
  90. "Ping by IP from ProxyVM failed")
  91. self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
  92. "Ping by name from ProxyVM failed")
  93. self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  94. "Ping by IP from AppVM failed")
  95. self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
  96. "Ping by IP from AppVM failed")
  97. def test_020_simple_proxyvm_nm(self):
  98. self.proxy = self.qc.add_new_vm("QubesProxyVm",
  99. name=self.make_vm_name('proxy'),
  100. template=self.qc.get_default_template())
  101. self.proxy.create_on_disk(verbose=False)
  102. self.proxy.netvm = self.testnetvm
  103. self.proxy.services['network-manager'] = True
  104. self.testvm1.netvm = self.proxy
  105. self.qc.save()
  106. self.testvm1.start()
  107. self.assertTrue(self.proxy.is_running())
  108. self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  109. "Ping by IP failed")
  110. self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
  111. "Ping by name failed")
  112. # reconnect to make sure that device was configured by NM
  113. self.assertEqual(
  114. self.run_cmd(self.proxy, "nmcli device disconnect eth0",
  115. user="user"),
  116. 0, "Failed to disconnect eth0 using nmcli")
  117. self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  118. "Network should be disabled, but apparently it isn't")
  119. self.assertEqual(
  120. self.run_cmd(self.proxy,
  121. 'nmcli connection up "VM uplink eth0" ifname eth0',
  122. user="user"),
  123. 0, "Failed to connect eth0 using nmcli")
  124. self.assertEqual(self.run_cmd(self.proxy, "nm-online", user="user"), 0,
  125. "Failed to wait for NM connection")
  126. # check for nm-applet presence
  127. self.assertEqual(subprocess.call([
  128. 'xdotool', 'search', '--all', '--name',
  129. '--class', '^(NetworkManager Applet|{})$'.format(self.proxy.name)],
  130. stdout=open('/dev/null', 'w')), 0, "nm-applet window not found")
  131. self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  132. "Ping by IP failed (after NM reconnection")
  133. self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
  134. "Ping by name failed (after NM reconnection)")
  135. def test_030_firewallvm_firewall(self):
  136. self.proxy = self.qc.add_new_vm("QubesProxyVm",
  137. name=self.make_vm_name('proxy'),
  138. template=self.qc.get_default_template())
  139. self.proxy.create_on_disk(verbose=False)
  140. self.proxy.netvm = self.testnetvm
  141. self.testvm1.netvm = self.proxy
  142. self.qc.save()
  143. # block all for first
  144. self.testvm1.write_firewall_conf({
  145. 'allow': False,
  146. 'allowDns': False,
  147. 'allowIcmp': False,
  148. })
  149. self.testvm1.start()
  150. self.assertTrue(self.proxy.is_running())
  151. self.testnetvm.run("nc -l --send-only -e /bin/hostname -k 1234")
  152. self.assertEqual(self.run_cmd(self.proxy, self.ping_ip), 0,
  153. "Ping by IP from ProxyVM failed")
  154. self.assertEqual(self.run_cmd(self.proxy, self.ping_name), 0,
  155. "Ping by name from ProxyVM failed")
  156. self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  157. "Ping by IP should be blocked")
  158. nc_cmd = "nc -w 1 --recv-only 192.168.123.45 1234"
  159. self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
  160. "TCP connection should be blocked")
  161. # block all except ICMP
  162. self.testvm1.write_firewall_conf({
  163. 'allow': False,
  164. 'allowDns': False,
  165. 'allowIcmp': True,
  166. })
  167. self.proxy.write_iptables_xenstore_entry()
  168. # Ugly hack b/c there is no feedback when the rules are actually applied
  169. time.sleep(1)
  170. self.assertEqual(self.run_cmd(self.testvm1, self.ping_ip), 0,
  171. "Ping by IP failed (should be allowed now)")
  172. self.assertNotEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
  173. "Ping by name should be blocked")
  174. # all TCP still blocked
  175. self.testvm1.write_firewall_conf({
  176. 'allow': False,
  177. 'allowDns': True,
  178. 'allowIcmp': True,
  179. })
  180. self.proxy.write_iptables_xenstore_entry()
  181. # Ugly hack b/c there is no feedback when the rules are actually applied
  182. time.sleep(1)
  183. self.assertEqual(self.run_cmd(self.testvm1, self.ping_name), 0,
  184. "Ping by name failed (should be allowed now)")
  185. self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
  186. "TCP connection should be blocked")
  187. # block all except target
  188. self.testvm1.write_firewall_conf({
  189. 'allow': False,
  190. 'allowDns': True,
  191. 'allowIcmp': True,
  192. 'rules': [{'address': self.ping_ip,
  193. 'netmask': 32,
  194. 'proto': 'tcp',
  195. 'portBegin': 1234
  196. }] })
  197. self.proxy.write_iptables_xenstore_entry()
  198. # Ugly hack b/c there is no feedback when the rules are actually applied
  199. time.sleep(1)
  200. self.assertEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
  201. "TCP connection failed (should be allowed now)")
  202. # allow all except target
  203. self.testvm1.write_firewall_conf({
  204. 'allow': True,
  205. 'allowDns': True,
  206. 'allowIcmp': True,
  207. 'rules': [{'address': self.ping_ip,
  208. 'netmask': 32,
  209. 'proto': 'tcp',
  210. 'portBegin': 1234
  211. }]
  212. })
  213. self.proxy.write_iptables_xenstore_entry()
  214. # Ugly hack b/c there is no feedback when the rules are actually applied
  215. time.sleep(1)
  216. self.assertNotEqual(self.run_cmd(self.testvm1, nc_cmd), 0,
  217. "TCP connection should be blocked")
  218. def test_040_inter_vm(self):
  219. self.proxy = self.qc.add_new_vm("QubesProxyVm",
  220. name=self.make_vm_name('proxy'),
  221. template=self.qc.get_default_template())
  222. self.proxy.create_on_disk(verbose=False)
  223. self.proxy.netvm = self.testnetvm
  224. self.testvm1.netvm = self.proxy
  225. self.testvm2 = self.qc.add_new_vm("QubesAppVm",
  226. name=self.make_vm_name('vm3'),
  227. template=self.qc.get_default_template())
  228. self.testvm2.create_on_disk(verbose=False)
  229. self.testvm2.netvm = self.proxy
  230. self.qc.save()
  231. self.testvm1.start()
  232. self.testvm2.start()
  233. self.assertNotEqual(self.run_cmd(self.testvm1,
  234. self.ping_cmd.format(target=self.testvm2.ip)), 0)
  235. self.testvm2.netvm = self.testnetvm
  236. self.assertNotEqual(self.run_cmd(self.testvm1,
  237. self.ping_cmd.format(target=self.testvm2.ip)), 0)
  238. self.assertNotEqual(self.run_cmd(self.testvm2,
  239. self.ping_cmd.format(target=self.testvm1.ip)), 0)
  240. self.testvm1.netvm = self.testnetvm
  241. self.assertNotEqual(self.run_cmd(self.testvm1,
  242. self.ping_cmd.format(target=self.testvm2.ip)), 0)
  243. self.assertNotEqual(self.run_cmd(self.testvm2,
  244. self.ping_cmd.format(target=self.testvm1.ip)), 0)