vm_qrexec_gui.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015
  5. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import asyncio
  22. import multiprocessing
  23. import os
  24. import subprocess
  25. import sys
  26. import tempfile
  27. import unittest
  28. from distutils import spawn
  29. import grp
  30. import qubes.config
  31. import qubes.devices
  32. import qubes.tests
  33. import qubes.vm.appvm
  34. import qubes.vm.templatevm
  35. TEST_DATA = b"0123456789" * 1024
  36. class TC_00_AppVMMixin(object):
  37. def setUp(self):
  38. super(TC_00_AppVMMixin, self).setUp()
  39. self.init_default_template(self.template)
  40. if self._testMethodName == 'test_210_time_sync':
  41. self.init_networking()
  42. self.testvm1 = self.app.add_new_vm(
  43. qubes.vm.appvm.AppVM,
  44. label='red',
  45. name=self.make_vm_name('vm1'),
  46. template=self.app.domains[self.template])
  47. self.loop.run_until_complete(self.testvm1.create_on_disk())
  48. self.testvm2 = self.app.add_new_vm(
  49. qubes.vm.appvm.AppVM,
  50. label='red',
  51. name=self.make_vm_name('vm2'),
  52. template=self.app.domains[self.template])
  53. self.loop.run_until_complete(self.testvm2.create_on_disk())
  54. self.app.save()
  55. def test_000_start_shutdown(self):
  56. # TODO: wait_for, timeout
  57. self.loop.run_until_complete(self.testvm1.start())
  58. self.assertEqual(self.testvm1.get_power_state(), "Running")
  59. self.loop.run_until_complete(self.testvm1.shutdown(wait=True))
  60. self.assertEqual(self.testvm1.get_power_state(), "Halted")
  61. @unittest.skipUnless(spawn.find_executable('xdotool'),
  62. "xdotool not installed")
  63. def test_010_run_xterm(self):
  64. self.loop.run_until_complete(self.testvm1.start())
  65. self.assertEqual(self.testvm1.get_power_state(), "Running")
  66. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  67. p = self.loop.run_until_complete(self.testvm1.run('xterm'))
  68. try:
  69. wait_count = 0
  70. title = 'user@{}'.format(self.testvm1.name)
  71. if self.template.count("whonix"):
  72. title = 'user@host'
  73. while subprocess.call(
  74. ['xdotool', 'search', '--name', title],
  75. stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) > 0:
  76. wait_count += 1
  77. if wait_count > 100:
  78. self.fail("Timeout while waiting for xterm window")
  79. self.loop.run_until_complete(asyncio.sleep(0.1))
  80. self.loop.run_until_complete(asyncio.sleep(0.5))
  81. subprocess.check_call(
  82. ['xdotool', 'search', '--name', title,
  83. 'windowactivate', 'type', 'exit\n'])
  84. wait_count = 0
  85. while subprocess.call(['xdotool', 'search', '--name', title],
  86. stdout=open(os.path.devnull, 'w'),
  87. stderr=subprocess.STDOUT) == 0:
  88. wait_count += 1
  89. if wait_count > 100:
  90. self.fail("Timeout while waiting for xterm "
  91. "termination")
  92. self.loop.run_until_complete(asyncio.sleep(0.1))
  93. finally:
  94. try:
  95. p.terminate()
  96. self.loop.run_until_complete(p.wait())
  97. except ProcessLookupError: # already dead
  98. pass
  99. @unittest.skipUnless(spawn.find_executable('xdotool'),
  100. "xdotool not installed")
  101. def test_011_run_gnome_terminal(self):
  102. if "minimal" in self.template:
  103. self.skipTest("Minimal template doesn't have 'gnome-terminal'")
  104. if 'whonix' in self.template:
  105. self.skipTest("Whonix template doesn't have 'gnome-terminal'")
  106. self.loop.run_until_complete(self.testvm1.start())
  107. self.assertEqual(self.testvm1.get_power_state(), "Running")
  108. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  109. p = self.loop.run_until_complete(self.testvm1.run('gnome-terminal'))
  110. try:
  111. title = 'user@{}'.format(self.testvm1.name)
  112. if self.template.count("whonix"):
  113. title = 'user@host'
  114. wait_count = 0
  115. while subprocess.call(
  116. ['xdotool', 'search', '--name', title],
  117. stdout=open(os.path.devnull, 'w'),
  118. stderr=subprocess.STDOUT) > 0:
  119. wait_count += 1
  120. if wait_count > 100:
  121. self.fail("Timeout while waiting for gnome-terminal window")
  122. self.loop.run_until_complete(asyncio.sleep(0.1))
  123. self.loop.run_until_complete(asyncio.sleep(0.5))
  124. subprocess.check_call(
  125. ['xdotool', 'search', '--name', title,
  126. 'windowactivate', '--sync', 'type', 'exit\n'])
  127. wait_count = 0
  128. while subprocess.call(['xdotool', 'search', '--name', title],
  129. stdout=open(os.path.devnull, 'w'),
  130. stderr=subprocess.STDOUT) == 0:
  131. wait_count += 1
  132. if wait_count > 100:
  133. self.fail("Timeout while waiting for gnome-terminal "
  134. "termination")
  135. self.loop.run_until_complete(asyncio.sleep(0.1))
  136. finally:
  137. try:
  138. p.terminate()
  139. self.loop.run_until_complete(p.wait())
  140. except ProcessLookupError: # already dead
  141. pass
  142. @unittest.skipUnless(spawn.find_executable('xdotool'),
  143. "xdotool not installed")
  144. @unittest.expectedFailure
  145. def test_012_qubes_desktop_run(self):
  146. self.loop.run_until_complete(self.testvm1.start())
  147. self.assertEqual(self.testvm1.get_power_state(), "Running")
  148. xterm_desktop_path = "/usr/share/applications/xterm.desktop"
  149. # Debian has it different...
  150. xterm_desktop_path_debian = \
  151. "/usr/share/applications/debian-xterm.desktop"
  152. try:
  153. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  154. 'test -r {}'.format(xterm_desktop_path_debian)))
  155. except subprocess.CalledProcessError:
  156. pass
  157. else:
  158. xterm_desktop_path = xterm_desktop_path_debian
  159. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  160. self.loop.run_until_complete(
  161. self.testvm1.run('qubes-desktop-run {}'.format(xterm_desktop_path)))
  162. title = 'user@{}'.format(self.testvm1.name)
  163. if self.template.count("whonix"):
  164. title = 'user@host'
  165. wait_count = 0
  166. while subprocess.call(
  167. ['xdotool', 'search', '--name', title],
  168. stdout=open(os.path.devnull, 'w'),
  169. stderr=subprocess.STDOUT) > 0:
  170. wait_count += 1
  171. if wait_count > 100:
  172. self.fail("Timeout while waiting for xterm window")
  173. self.loop.run_until_complete(asyncio.sleep(0.1))
  174. self.loop.run_until_complete(asyncio.sleep(0.5))
  175. subprocess.check_call(
  176. ['xdotool', 'search', '--name', title,
  177. 'windowactivate', '--sync', 'type', 'exit\n'])
  178. wait_count = 0
  179. while subprocess.call(['xdotool', 'search', '--name', title],
  180. stdout=open(os.path.devnull, 'w'),
  181. stderr=subprocess.STDOUT) == 0:
  182. wait_count += 1
  183. if wait_count > 100:
  184. self.fail("Timeout while waiting for xterm "
  185. "termination")
  186. self.loop.run_until_complete(asyncio.sleep(0.1))
  187. def test_050_qrexec_simple_eof(self):
  188. """Test for data and EOF transmission dom0->VM"""
  189. # XXX is this still correct? this is no longer simple qrexec,
  190. # but qubes.VMShell
  191. self.loop.run_until_complete(self.testvm1.start())
  192. try:
  193. (stdout, stderr) = self.loop.run_until_complete(asyncio.wait_for(
  194. self.testvm1.run_for_stdio('cat', input=TEST_DATA),
  195. timeout=10))
  196. except asyncio.TimeoutError:
  197. self.fail(
  198. "Timeout, probably EOF wasn't transferred to the VM process")
  199. self.assertEqual(stdout, TEST_DATA,
  200. 'Received data differs from what was sent')
  201. self.assertFalse(stderr,
  202. 'Some data was printed to stderr')
  203. def test_051_qrexec_simple_eof_reverse(self):
  204. """Test for EOF transmission VM->dom0"""
  205. @asyncio.coroutine
  206. def run(self):
  207. p = yield from self.testvm1.run(
  208. 'echo test; exec >&-; cat > /dev/null',
  209. stdin=subprocess.PIPE,
  210. stdout=subprocess.PIPE,
  211. stderr=subprocess.PIPE)
  212. # this will hang on test failure
  213. stdout = yield from asyncio.wait_for(p.stdout.read(), timeout=10)
  214. p.stdin.write(TEST_DATA)
  215. yield from p.stdin.drain()
  216. p.stdin.close()
  217. self.assertEqual(stdout.strip(), b'test',
  218. 'Received data differs from what was expected')
  219. # this may hang in some buggy cases
  220. self.assertFalse((yield from p.stderr.read()),
  221. 'Some data was printed to stderr')
  222. try:
  223. yield from asyncio.wait_for(p.wait(), timeout=1)
  224. except asyncio.TimeoutError:
  225. self.fail("Timeout, "
  226. "probably EOF wasn't transferred from the VM process")
  227. self.loop.run_until_complete(self.testvm1.start())
  228. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  229. self.loop.run_until_complete(run(self))
  230. def test_052_qrexec_vm_service_eof(self):
  231. """Test for EOF transmission VM(src)->VM(dst)"""
  232. self.loop.run_until_complete(asyncio.wait([
  233. self.testvm1.start(),
  234. self.testvm2.start()]))
  235. self.loop.run_until_complete(asyncio.wait([
  236. self.wait_for_session(self.testvm1),
  237. self.wait_for_session(self.testvm2)]))
  238. self.loop.run_until_complete(self.testvm2.run_for_stdio(
  239. 'cat > /etc/qubes-rpc/test.EOF',
  240. user='root',
  241. input=b'/bin/cat'))
  242. with self.qrexec_policy('test.EOF', self.testvm1, self.testvm2):
  243. try:
  244. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  245. self.testvm1.run_for_stdio('''\
  246. /usr/lib/qubes/qrexec-client-vm {} test.EOF \
  247. /bin/sh -c 'echo test; exec >&-; cat >&$SAVED_FD_1'
  248. '''.format(self.testvm2.name)),
  249. timeout=10))
  250. except asyncio.TimeoutError:
  251. self.fail("Timeout, probably EOF wasn't transferred")
  252. self.assertEqual(stdout, b'test\n',
  253. 'Received data differs from what was expected')
  254. @unittest.expectedFailure
  255. def test_053_qrexec_vm_service_eof_reverse(self):
  256. """Test for EOF transmission VM(src)<-VM(dst)"""
  257. self.loop.run_until_complete(asyncio.wait([
  258. self.testvm1.start(),
  259. self.testvm2.start()]))
  260. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.EOF',
  261. 'echo test; exec >&-; cat >/dev/null')
  262. with self.qrexec_policy('test.EOF', self.testvm1, self.testvm2):
  263. try:
  264. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  265. self.testvm1.run_for_stdio('''\
  266. /usr/lib/qubes/qrexec-client-vm {} test.EOF \
  267. /bin/sh -c 'cat >&$SAVED_FD_1'
  268. '''.format(self.testvm2.name)),
  269. timeout=10))
  270. except asyncio.TimeoutError:
  271. self.fail("Timeout, probably EOF wasn't transferred")
  272. self.assertEqual(stdout, b'test',
  273. 'Received data differs from what was expected')
  274. def test_055_qrexec_dom0_service_abort(self):
  275. """
  276. Test if service abort (by dom0) is properly handled by source VM.
  277. If "remote" part of the service terminates, the source part should
  278. properly be notified. This includes closing its stdin (which is
  279. already checked by test_053_qrexec_vm_service_eof_reverse), but also
  280. its stdout - otherwise such service might hang on write(2) call.
  281. """
  282. self.loop.run_until_complete(self.testvm1.start())
  283. self.create_local_file('/etc/qubes-rpc/test.Abort',
  284. 'sleep 1')
  285. with self.qrexec_policy('test.Abort', self.testvm1, 'dom0'):
  286. try:
  287. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  288. self.testvm1.run_for_stdio('''\
  289. /usr/lib/qubes/qrexec-client-vm dom0 test.Abort \
  290. /bin/cat /dev/zero; test $? -eq 141'''),
  291. timeout=10))
  292. except asyncio.TimeoutError:
  293. self.fail("Timeout, probably stdout wasn't closed")
  294. def test_060_qrexec_exit_code_dom0(self):
  295. self.loop.run_until_complete(self.testvm1.start())
  296. self.loop.run_until_complete(self.testvm1.run_for_stdio('exit 0'))
  297. with self.assertRaises(subprocess.CalledProcessError) as e:
  298. self.loop.run_until_complete(self.testvm1.run_for_stdio('exit 3'))
  299. self.assertEqual(e.exception.returncode, 3)
  300. def test_065_qrexec_exit_code_vm(self):
  301. self.loop.run_until_complete(asyncio.wait([
  302. self.testvm1.start(),
  303. self.testvm2.start()]))
  304. with self.qrexec_policy('test.Retcode', self.testvm1, self.testvm2):
  305. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Retcode',
  306. 'exit 0')
  307. (stdout, stderr) = self.loop.run_until_complete(
  308. self.testvm1.run_for_stdio('''\
  309. /usr/lib/qubes/qrexec-client-vm {} test.Retcode;
  310. echo $?'''.format(self.testvm2.name)))
  311. self.assertEqual(stdout, b'0\n')
  312. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Retcode',
  313. 'exit 3')
  314. (stdout, stderr) = self.loop.run_until_complete(
  315. self.testvm1.run_for_stdio('''\
  316. /usr/lib/qubes/qrexec-client-vm {} test.Retcode;
  317. echo $?'''.format(self.testvm2.name)))
  318. self.assertEqual(stdout, b'3\n')
  319. def test_070_qrexec_vm_simultaneous_write(self):
  320. """Test for simultaneous write in VM(src)->VM(dst) connection
  321. This is regression test for #1347
  322. Check for deadlock when initially both sides writes a lot of data
  323. (and not read anything). When one side starts reading, it should
  324. get the data and the remote side should be possible to write then more.
  325. There was a bug where remote side was waiting on write(2) and not
  326. handling anything else.
  327. """
  328. self.loop.run_until_complete(asyncio.wait([
  329. self.testvm1.start(),
  330. self.testvm2.start()]))
  331. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  332. # first write a lot of data
  333. dd if=/dev/zero bs=993 count=10000 iflag=fullblock
  334. # and only then read something
  335. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  336. ''')
  337. with self.qrexec_policy('test.write', self.testvm1, self.testvm2):
  338. try:
  339. self.loop.run_until_complete(asyncio.wait_for(
  340. # first write a lot of data to fill all the buffers
  341. # then after some time start reading
  342. self.testvm1.run_for_stdio('''\
  343. /usr/lib/qubes/qrexec-client-vm {} test.write \
  344. /bin/sh -c '
  345. dd if=/dev/zero bs=993 count=10000 iflag=fullblock &
  346. sleep 1;
  347. dd of=/dev/null bs=993 count=10000 iflag=fullblock;
  348. wait'
  349. '''.format(self.testvm2.name)), timeout=10))
  350. except subprocess.CalledProcessError:
  351. self.fail('Service call failed')
  352. except asyncio.TimeoutError:
  353. self.fail('Timeout, probably deadlock')
  354. def test_071_qrexec_dom0_simultaneous_write(self):
  355. """Test for simultaneous write in dom0(src)->VM(dst) connection
  356. Similar to test_070_qrexec_vm_simultaneous_write, but with dom0
  357. as a source.
  358. """
  359. self.loop.run_until_complete(self.testvm2.start())
  360. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  361. # first write a lot of data
  362. dd if=/dev/zero bs=993 count=10000 iflag=fullblock
  363. # and only then read something
  364. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  365. ''')
  366. # can't use subprocess.PIPE, because asyncio will claim those FDs
  367. pipe1_r, pipe1_w = os.pipe()
  368. pipe2_r, pipe2_w = os.pipe()
  369. try:
  370. local_proc = self.loop.run_until_complete(
  371. asyncio.create_subprocess_shell(
  372. # first write a lot of data to fill all the buffers
  373. "dd if=/dev/zero bs=993 count=10000 iflag=fullblock & "
  374. # then after some time start reading
  375. "sleep 1; "
  376. "dd of=/dev/null bs=993 count=10000 iflag=fullblock; "
  377. "wait", stdin=pipe1_r, stdout=pipe2_w))
  378. service_proc = self.loop.run_until_complete(self.testvm2.run_service(
  379. "test.write", stdin=pipe2_r, stdout=pipe1_w))
  380. finally:
  381. os.close(pipe1_r)
  382. os.close(pipe1_w)
  383. os.close(pipe2_r)
  384. os.close(pipe2_w)
  385. try:
  386. self.loop.run_until_complete(
  387. asyncio.wait_for(service_proc.wait(), timeout=10))
  388. except asyncio.TimeoutError:
  389. self.fail("Timeout, probably deadlock")
  390. else:
  391. self.assertEqual(service_proc.returncode, 0,
  392. "Service call failed")
  393. finally:
  394. try:
  395. service_proc.terminate()
  396. except ProcessLookupError:
  397. pass
  398. def test_072_qrexec_to_dom0_simultaneous_write(self):
  399. """Test for simultaneous write in dom0(src)<-VM(dst) connection
  400. Similar to test_071_qrexec_dom0_simultaneous_write, but with dom0
  401. as a "hanging" side.
  402. """
  403. self.loop.run_until_complete(self.testvm2.start())
  404. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  405. # first write a lot of data
  406. dd if=/dev/zero bs=993 count=10000 iflag=fullblock &
  407. # and only then read something
  408. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  409. sleep 1;
  410. wait
  411. ''')
  412. # can't use subprocess.PIPE, because asyncio will claim those FDs
  413. pipe1_r, pipe1_w = os.pipe()
  414. pipe2_r, pipe2_w = os.pipe()
  415. try:
  416. local_proc = self.loop.run_until_complete(
  417. asyncio.create_subprocess_shell(
  418. # first write a lot of data to fill all the buffers
  419. "dd if=/dev/zero bs=993 count=10000 iflag=fullblock & "
  420. # then, only when all written, read something
  421. "dd of=/dev/null bs=993 count=10000 iflag=fullblock; ",
  422. stdin=pipe1_r, stdout=pipe2_w))
  423. service_proc = self.loop.run_until_complete(self.testvm2.run_service(
  424. "test.write", stdin=pipe2_r, stdout=pipe1_w))
  425. finally:
  426. os.close(pipe1_r)
  427. os.close(pipe1_w)
  428. os.close(pipe2_r)
  429. os.close(pipe2_w)
  430. try:
  431. self.loop.run_until_complete(
  432. asyncio.wait_for(service_proc.wait(), timeout=10))
  433. except asyncio.TimeoutError:
  434. self.fail("Timeout, probably deadlock")
  435. else:
  436. self.assertEqual(service_proc.returncode, 0,
  437. "Service call failed")
  438. finally:
  439. try:
  440. service_proc.terminate()
  441. except ProcessLookupError:
  442. pass
  443. def test_080_qrexec_service_argument_allow_default(self):
  444. """Qrexec service call with argument"""
  445. self.loop.run_until_complete(asyncio.wait([
  446. self.testvm1.start(),
  447. self.testvm2.start()]))
  448. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  449. '/usr/bin/printf %s "$1"')
  450. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  451. stdout, stderr = self.loop.run_until_complete(
  452. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  453. '{} test.Argument+argument'.format(self.testvm2.name)))
  454. self.assertEqual(stdout, b'argument')
  455. def test_081_qrexec_service_argument_allow_specific(self):
  456. """Qrexec service call with argument - allow only specific value"""
  457. self.loop.run_until_complete(asyncio.wait([
  458. self.testvm1.start(),
  459. self.testvm2.start()]))
  460. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  461. '/usr/bin/printf %s "$1"')
  462. with self.qrexec_policy('test.Argument', '$anyvm', '$anyvm', False):
  463. with self.qrexec_policy('test.Argument+argument',
  464. self.testvm1.name, self.testvm2.name):
  465. stdout, stderr = self.loop.run_until_complete(
  466. self.testvm1.run_for_stdio(
  467. '/usr/lib/qubes/qrexec-client-vm '
  468. '{} test.Argument+argument'.format(self.testvm2.name)))
  469. self.assertEqual(stdout, b'argument')
  470. def test_082_qrexec_service_argument_deny_specific(self):
  471. """Qrexec service call with argument - deny specific value"""
  472. self.loop.run_until_complete(asyncio.wait([
  473. self.testvm1.start(),
  474. self.testvm2.start()]))
  475. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  476. '/usr/bin/printf %s "$1"')
  477. with self.qrexec_policy('test.Argument', '$anyvm', '$anyvm'):
  478. with self.qrexec_policy('test.Argument+argument',
  479. self.testvm1, self.testvm2, allow=False):
  480. with self.assertRaises(subprocess.CalledProcessError,
  481. msg='Service request should be denied'):
  482. self.loop.run_until_complete(
  483. self.testvm1.run_for_stdio(
  484. '/usr/lib/qubes/qrexec-client-vm {} '
  485. 'test.Argument+argument'.format(self.testvm2.name)))
  486. def test_083_qrexec_service_argument_specific_implementation(self):
  487. """Qrexec service call with argument - argument specific
  488. implementatation"""
  489. self.loop.run_until_complete(asyncio.wait([
  490. self.testvm1.start(),
  491. self.testvm2.start()]))
  492. self.create_remote_file(self.testvm2,
  493. '/etc/qubes-rpc/test.Argument',
  494. '/usr/bin/printf %s "$1"')
  495. self.create_remote_file(self.testvm2,
  496. '/etc/qubes-rpc/test.Argument+argument',
  497. '/usr/bin/printf "specific: %s" "$1"')
  498. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  499. stdout, stderr = self.loop.run_until_complete(
  500. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  501. '{} test.Argument+argument'.format(self.testvm2.name)))
  502. self.assertEqual(stdout, b'specific: argument')
  503. def test_084_qrexec_service_argument_extra_env(self):
  504. """Qrexec service call with argument - extra env variables"""
  505. self.loop.run_until_complete(asyncio.wait([
  506. self.testvm1.start(),
  507. self.testvm2.start()]))
  508. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  509. '/usr/bin/printf "%s %s" '
  510. '"$QREXEC_SERVICE_FULL_NAME" "$QREXEC_SERVICE_ARGUMENT"')
  511. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  512. stdout, stderr = self.loop.run_until_complete(
  513. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  514. '{} test.Argument+argument'.format(self.testvm2.name)))
  515. self.assertEqual(stdout, b'test.Argument+argument argument')
  516. def test_100_qrexec_filecopy(self):
  517. self.loop.run_until_complete(asyncio.wait([
  518. self.testvm1.start(),
  519. self.testvm2.start()]))
  520. with self.qrexec_policy('qubes.Filecopy', self.testvm1, self.testvm2):
  521. try:
  522. self.loop.run_until_complete(
  523. self.testvm1.run_for_stdio(
  524. 'qvm-copy-to-vm {} /etc/passwd'.format(
  525. self.testvm2.name)))
  526. except subprocess.CalledProcessError as e:
  527. self.fail('qvm-copy-to-vm failed: {}'.format(e.stderr))
  528. try:
  529. self.loop.run_until_complete(self.testvm2.run_for_stdio(
  530. 'diff /etc/passwd /home/user/QubesIncoming/{}/passwd'.format(
  531. self.testvm1.name)))
  532. except subprocess.CalledProcessError:
  533. self.fail('file differs')
  534. try:
  535. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  536. 'test -f /etc/passwd'))
  537. except subprocess.CalledProcessError:
  538. self.fail('source file got removed')
  539. def test_105_qrexec_filemove(self):
  540. self.loop.run_until_complete(asyncio.wait([
  541. self.testvm1.start(),
  542. self.testvm2.start()]))
  543. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  544. 'cp /etc/passwd /tmp/passwd'))
  545. with self.qrexec_policy('qubes.Filecopy', self.testvm1, self.testvm2):
  546. try:
  547. self.loop.run_until_complete(
  548. self.testvm1.run_for_stdio(
  549. 'qvm-move-to-vm {} /tmp/passwd'.format(
  550. self.testvm2.name)))
  551. except subprocess.CalledProcessError as e:
  552. self.fail('qvm-move-to-vm failed: {}'.format(e.stderr))
  553. try:
  554. self.loop.run_until_complete(self.testvm2.run_for_stdio(
  555. 'diff /etc/passwd /home/user/QubesIncoming/{}/passwd'.format(
  556. self.testvm1.name)))
  557. except subprocess.CalledProcessError:
  558. self.fail('file differs')
  559. with self.assertRaises(subprocess.CalledProcessError):
  560. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  561. 'test -f /tmp/passwd'))
  562. def test_101_qrexec_filecopy_with_autostart(self):
  563. self.loop.run_until_complete(self.testvm1.start())
  564. with self.qrexec_policy('qubes.Filecopy', self.testvm1, self.testvm2):
  565. try:
  566. self.loop.run_until_complete(
  567. self.testvm1.run_for_stdio(
  568. 'qvm-copy-to-vm {} /etc/passwd'.format(
  569. self.testvm2.name)))
  570. except subprocess.CalledProcessError as e:
  571. self.fail('qvm-copy-to-vm failed: {}'.format(e.stderr))
  572. # workaround for libvirt bug (domain ID isn't updated when is started
  573. # from other application) - details in
  574. # QubesOS/qubes-core-libvirt@63ede4dfb4485c4161dd6a2cc809e8fb45ca664f
  575. # XXX is it still true with qubesd? --woju 20170523
  576. self.testvm2._libvirt_domain = None
  577. self.assertTrue(self.testvm2.is_running())
  578. try:
  579. self.loop.run_until_complete(self.testvm2.run_for_stdio(
  580. 'diff /etc/passwd /home/user/QubesIncoming/{}/passwd'.format(
  581. self.testvm1.name)))
  582. except subprocess.CalledProcessError:
  583. self.fail('file differs')
  584. try:
  585. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  586. 'test -f /etc/passwd'))
  587. except subprocess.CalledProcessError:
  588. self.fail('source file got removed')
  589. def test_110_qrexec_filecopy_deny(self):
  590. self.loop.run_until_complete(asyncio.wait([
  591. self.testvm1.start(),
  592. self.testvm2.start()]))
  593. with self.qrexec_policy('qubes.Filecopy', self.testvm1, self.testvm2,
  594. allow=False):
  595. with self.assertRaises(subprocess.CalledProcessError):
  596. self.loop.run_until_complete(
  597. self.testvm1.run_for_stdio(
  598. 'qvm-copy-to-vm {} /etc/passwd'.format(
  599. self.testvm2.name)))
  600. with self.assertRaises(subprocess.CalledProcessError):
  601. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  602. 'test -d /home/user/QubesIncoming/{}'.format(
  603. self.testvm1.name)))
  604. @unittest.skip("Xen gntalloc driver crashes when page is mapped in the "
  605. "same domain")
  606. def test_120_qrexec_filecopy_self(self):
  607. self.testvm1.start()
  608. self.qrexec_policy('qubes.Filecopy', self.testvm1.name,
  609. self.testvm1.name)
  610. p = self.testvm1.run("qvm-copy-to-vm %s /etc/passwd" %
  611. self.testvm1.name, passio_popen=True,
  612. passio_stderr=True)
  613. p.wait()
  614. self.assertEqual(p.returncode, 0, "qvm-copy-to-vm failed: %s" %
  615. p.stderr.read())
  616. retcode = self.testvm1.run(
  617. "diff /etc/passwd /home/user/QubesIncoming/{}/passwd".format(
  618. self.testvm1.name),
  619. wait=True)
  620. self.assertEqual(retcode, 0, "file differs")
  621. @unittest.skipUnless(spawn.find_executable('xdotool'),
  622. "xdotool not installed")
  623. def test_130_qrexec_filemove_disk_full(self):
  624. self.loop.run_until_complete(asyncio.wait([
  625. self.testvm1.start(),
  626. self.testvm2.start()]))
  627. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  628. # Prepare test file
  629. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  630. 'yes teststring | dd of=/tmp/testfile bs=1M count=50 '
  631. 'iflag=fullblock'))
  632. # Prepare target directory with limited size
  633. self.loop.run_until_complete(self.testvm2.run_for_stdio(
  634. 'mkdir -p /home/user/QubesIncoming && '
  635. 'chown user /home/user/QubesIncoming && '
  636. 'mount -t tmpfs none /home/user/QubesIncoming -o size=48M',
  637. user='root'))
  638. with self.qrexec_policy('qubes.Filecopy', self.testvm1, self.testvm2):
  639. p = self.loop.run_until_complete(self.testvm1.run(
  640. 'qvm-move-to-vm {} /tmp/testfile'.format(
  641. self.testvm2.name)))
  642. # Close GUI error message
  643. try:
  644. self.enter_keys_in_window('Error', ['Return'])
  645. except subprocess.CalledProcessError:
  646. pass
  647. self.loop.run_until_complete(p.wait())
  648. self.assertNotEqual(p.returncode, 0)
  649. # the file shouldn't be removed in source vm
  650. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  651. 'test -f /tmp/testfile'))
  652. def test_200_timezone(self):
  653. """Test whether timezone setting is properly propagated to the VM"""
  654. if "whonix" in self.template:
  655. self.skipTest("Timezone propagation disabled on Whonix templates")
  656. self.loop.run_until_complete(self.testvm1.start())
  657. vm_tz, _ = self.loop.run_until_complete(self.testvm1.run_for_stdio(
  658. 'date +%Z'))
  659. dom0_tz = subprocess.check_output(['date', '+%Z'])
  660. self.assertEqual(vm_tz.strip(), dom0_tz.strip())
  661. # Check if reverting back to UTC works
  662. vm_tz, _ = self.loop.run_until_complete(self.testvm1.run_for_stdio(
  663. 'TZ=UTC date +%Z'))
  664. self.assertEqual(vm_tz.strip(), b'UTC')
  665. def test_210_time_sync(self):
  666. """Test time synchronization mechanism"""
  667. if self.template.startswith('whonix-'):
  668. self.skipTest('qvm-sync-clock disabled for Whonix VMs')
  669. self.loop.run_until_complete(asyncio.wait([
  670. self.testvm1.start(),
  671. self.testvm2.start(),]))
  672. start_time = subprocess.check_output(['date', '-u', '+%s'])
  673. try:
  674. self.app.clockvm = self.testvm1
  675. self.app.save()
  676. # break vm and dom0 time, to check if qvm-sync-clock would fix it
  677. subprocess.check_call(['sudo', 'date', '-s', '2001-01-01T12:34:56'],
  678. stdout=subprocess.DEVNULL)
  679. self.loop.run_until_complete(
  680. self.testvm2.run_for_stdio('date -s 2001-01-01T12:34:56',
  681. user='root'))
  682. self.loop.run_until_complete(
  683. self.testvm2.run_for_stdio('qvm-sync-clock',
  684. user='root'))
  685. p = self.loop.run_until_complete(
  686. asyncio.create_subprocess_exec('sudo', 'qvm-sync-clock',
  687. stdout=asyncio.subprocess.DEVNULL))
  688. self.loop.run_until_complete(p.wait())
  689. self.assertEqual(p.returncode, 0)
  690. vm_time, _ = self.loop.run_until_complete(
  691. self.testvm2.run_for_stdio('date -u +%s'))
  692. self.assertAlmostEquals(int(vm_time), int(start_time), delta=30)
  693. dom0_time = subprocess.check_output(['date', '-u', '+%s'])
  694. self.assertAlmostEquals(int(dom0_time), int(start_time), delta=30)
  695. except:
  696. # reset time to some approximation of the real time
  697. subprocess.Popen(
  698. ["sudo", "date", "-u", "-s", "@" + start_time.decode()])
  699. raise
  700. finally:
  701. self.app.clockvm = None
  702. @unittest.skipUnless(spawn.find_executable('parecord'),
  703. "pulseaudio-utils not installed in dom0")
  704. def test_220_audio_playback(self):
  705. if 'whonix-gw' in self.template:
  706. self.skipTest('whonix-gw have no audio')
  707. self.loop.run_until_complete(self.testvm1.start())
  708. try:
  709. self.loop.run_until_complete(
  710. self.testvm1.run_for_stdio('which parecord'))
  711. except subprocess.CalledProcessError:
  712. self.skipTest('pulseaudio-utils not installed in VM')
  713. self.loop.run_until_complete(
  714. self.wait_for_session(self.testvm1))
  715. # and some more...
  716. self.loop.run_until_complete(asyncio.sleep(1))
  717. # generate some "audio" data
  718. audio_in = b'\x20' * 44100
  719. self.loop.run_until_complete(
  720. self.testvm1.run_for_stdio('cat > audio_in.raw', input=audio_in))
  721. local_user = grp.getgrnam('qubes').gr_mem[0]
  722. with tempfile.NamedTemporaryFile() as recorded_audio:
  723. os.chmod(recorded_audio.name, 0o666)
  724. # FIXME: -d 0 assumes only one audio device
  725. p = subprocess.Popen(['sudo', '-E', '-u', local_user,
  726. 'parecord', '-d', '0', '--raw', recorded_audio.name],
  727. stdout=subprocess.PIPE)
  728. self.loop.run_until_complete(
  729. self.testvm1.run_for_stdio('paplay --raw audio_in.raw'))
  730. # wait for possible parecord buffering
  731. self.loop.run_until_complete(asyncio.sleep(1))
  732. p.terminate()
  733. # for some reason sudo do not relay SIGTERM sent above
  734. subprocess.check_call(['pkill', 'parecord'])
  735. p.wait()
  736. # allow few bytes missing, don't use assertIn, to avoid printing
  737. # the whole data in error message
  738. if audio_in[:-8] not in recorded_audio.file.read():
  739. self.fail('played sound not found in dom0')
  740. def _configure_audio_recording(self, vm):
  741. '''Connect VM's output-source to sink monitor instead of mic'''
  742. local_user = grp.getgrnam('qubes').gr_mem[0]
  743. sudo = ['sudo', '-E', '-u', local_user]
  744. source_outputs = subprocess.check_output(
  745. sudo + ['pacmd', 'list-source-outputs']).decode()
  746. last_index = None
  747. found = False
  748. for line in source_outputs.splitlines():
  749. if line.startswith(' index: '):
  750. last_index = line.split(':')[1].strip()
  751. elif line.startswith('\t\tapplication.name = '):
  752. app_name = line.split('=')[1].strip('" ')
  753. if vm.name == app_name:
  754. found = True
  755. break
  756. if not found:
  757. self.fail('source-output for VM {} not found'.format(vm.name))
  758. subprocess.check_call(sudo +
  759. ['pacmd', 'move-source-output', last_index, '0'])
  760. @unittest.skipUnless(spawn.find_executable('parecord'),
  761. "pulseaudio-utils not installed in dom0")
  762. def test_221_audio_record_muted(self):
  763. if 'whonix-gw' in self.template:
  764. self.skipTest('whonix-gw have no audio')
  765. self.loop.run_until_complete(self.testvm1.start())
  766. try:
  767. self.loop.run_until_complete(
  768. self.testvm1.run_for_stdio('which parecord'))
  769. except subprocess.CalledProcessError:
  770. self.skipTest('pulseaudio-utils not installed in VM')
  771. self.loop.run_until_complete(
  772. self.wait_for_session(self.testvm1))
  773. # and some more...
  774. self.loop.run_until_complete(asyncio.sleep(1))
  775. # connect VM's recording source output monitor (instead of mic)
  776. self._configure_audio_recording(self.testvm1)
  777. # generate some "audio" data
  778. audio_in = b'\x20' * 44100
  779. local_user = grp.getgrnam('qubes').gr_mem[0]
  780. record = self.loop.run_until_complete(
  781. self.testvm1.run('parecord --raw audio_rec.raw'))
  782. # give it time to start recording
  783. self.loop.run_until_complete(asyncio.sleep(0.5))
  784. p = subprocess.Popen(['sudo', '-E', '-u', local_user,
  785. 'paplay', '--raw'],
  786. stdin=subprocess.PIPE)
  787. p.communicate(audio_in)
  788. # wait for possible parecord buffering
  789. self.loop.run_until_complete(asyncio.sleep(1))
  790. self.loop.run_until_complete(
  791. self.testvm1.run_for_stdio('pkill parecord'))
  792. record.wait()
  793. recorded_audio, _ = self.loop.run_until_complete(
  794. self.testvm1.run_for_stdio('cat audio_rec.raw'))
  795. # should be empty or silence, so check just a little fragment
  796. if audio_in[:32] in recorded_audio:
  797. self.fail('VM recorded something, even though mic disabled')
  798. @unittest.skipUnless(spawn.find_executable('parecord'),
  799. "pulseaudio-utils not installed in dom0")
  800. def test_222_audio_record_unmuted(self):
  801. if 'whonix-gw' in self.template:
  802. self.skipTest('whonix-gw have no audio')
  803. self.loop.run_until_complete(self.testvm1.start())
  804. try:
  805. self.loop.run_until_complete(
  806. self.testvm1.run_for_stdio('which parecord'))
  807. except subprocess.CalledProcessError:
  808. self.skipTest('pulseaudio-utils not installed in VM')
  809. self.loop.run_until_complete(
  810. self.wait_for_session(self.testvm1))
  811. # and some more...
  812. self.loop.run_until_complete(asyncio.sleep(1))
  813. da = qubes.devices.DeviceAssignment(self.app.domains[0], 'mic')
  814. self.loop.run_until_complete(
  815. self.testvm1.devices['mic'].attach(da))
  816. # connect VM's recording source output monitor (instead of mic)
  817. self._configure_audio_recording(self.testvm1)
  818. # generate some "audio" data
  819. audio_in = b'\x20' * 44100
  820. local_user = grp.getgrnam('qubes').gr_mem[0]
  821. record = self.loop.run_until_complete(
  822. self.testvm1.run('parecord --raw audio_rec.raw'))
  823. # give it time to start recording
  824. self.loop.run_until_complete(asyncio.sleep(0.5))
  825. p = subprocess.Popen(['sudo', '-E', '-u', local_user,
  826. 'paplay', '--raw'],
  827. stdin=subprocess.PIPE)
  828. p.communicate(audio_in)
  829. # wait for possible parecord buffering
  830. self.loop.run_until_complete(asyncio.sleep(1))
  831. self.loop.run_until_complete(
  832. self.testvm1.run_for_stdio('pkill parecord'))
  833. record.wait()
  834. recorded_audio, _ = self.loop.run_until_complete(
  835. self.testvm1.run_for_stdio('cat audio_rec.raw'))
  836. # allow few bytes to be missing
  837. if audio_in[:-8] not in recorded_audio:
  838. self.fail('VM not recorded expected data')
  839. def test_250_resize_private_img(self):
  840. """
  841. Test private.img resize, both offline and online
  842. :return:
  843. """
  844. # First offline test
  845. self.loop.run_until_complete(
  846. self.testvm1.storage.resize('private', 4*1024**3))
  847. self.loop.run_until_complete(self.testvm1.start())
  848. df_cmd = '( df --output=size /rw || df /rw | awk \'{print $2}\' )|' \
  849. 'tail -n 1'
  850. # new_size in 1k-blocks
  851. new_size, _ = self.loop.run_until_complete(
  852. self.testvm1.run_for_stdio(df_cmd))
  853. # some safety margin for FS metadata
  854. self.assertGreater(int(new_size.strip()), 3.8*1024**2)
  855. # Then online test
  856. self.loop.run_until_complete(
  857. self.testvm1.storage.resize('private', 6*1024**3))
  858. # new_size in 1k-blocks
  859. new_size, _ = self.loop.run_until_complete(
  860. self.testvm1.run_for_stdio(df_cmd))
  861. # some safety margin for FS metadata
  862. self.assertGreater(int(new_size.strip()), 5.7*1024**2)
  863. @unittest.skipUnless(spawn.find_executable('xdotool'),
  864. "xdotool not installed")
  865. def test_300_bug_1028_gui_memory_pinning(self):
  866. """
  867. If VM window composition buffers are relocated in memory, GUI will
  868. still use old pointers and will display old pages
  869. :return:
  870. """
  871. # this test does too much asynchronous operations,
  872. # so let's rewrite it as a coroutine and call it as such
  873. return self.loop.run_until_complete(
  874. self._test_300_bug_1028_gui_memory_pinning())
  875. @asyncio.coroutine
  876. def _test_300_bug_1028_gui_memory_pinning(self):
  877. self.testvm1.memory = 800
  878. self.testvm1.maxmem = 800
  879. # exclude from memory balancing
  880. self.testvm1.features['service.meminfo-writer'] = False
  881. yield from self.testvm1.start()
  882. yield from self.wait_for_session(self.testvm1)
  883. # and allow large map count
  884. yield from self.testvm1.run('echo 256000 > /proc/sys/vm/max_map_count',
  885. user="root")
  886. allocator_c = '''
  887. #include <sys/mman.h>
  888. #include <stdlib.h>
  889. #include <stdio.h>
  890. int main(int argc, char **argv) {
  891. int total_pages;
  892. char *addr, *iter;
  893. total_pages = atoi(argv[1]);
  894. addr = mmap(NULL, total_pages * 0x1000, PROT_READ | PROT_WRITE,
  895. MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, -1, 0);
  896. if (addr == MAP_FAILED) {
  897. perror("mmap");
  898. exit(1);
  899. }
  900. printf("Stage1\\n");
  901. fflush(stdout);
  902. getchar();
  903. for (iter = addr; iter < addr + total_pages*0x1000; iter += 0x2000) {
  904. if (mlock(iter, 0x1000) == -1) {
  905. perror("mlock");
  906. fprintf(stderr, "%d of %d\\n", (iter-addr)/0x1000, total_pages);
  907. exit(1);
  908. }
  909. }
  910. printf("Stage2\\n");
  911. fflush(stdout);
  912. for (iter = addr+0x1000; iter < addr + total_pages*0x1000; iter += 0x2000) {
  913. if (munmap(iter, 0x1000) == -1) {
  914. perror(\"munmap\");
  915. exit(1);
  916. }
  917. }
  918. printf("Stage3\\n");
  919. fflush(stdout);
  920. fclose(stdout);
  921. getchar();
  922. return 0;
  923. }
  924. '''
  925. yield from self.testvm1.run_for_stdio('cat > allocator.c',
  926. input=allocator_c.encode())
  927. try:
  928. yield from self.testvm1.run_for_stdio(
  929. 'gcc allocator.c -o allocator')
  930. except subprocess.CalledProcessError as e:
  931. self.skipTest('allocator compile failed: {}'.format(e.stderr))
  932. # drop caches to have even more memory pressure
  933. yield from self.testvm1.run_for_stdio(
  934. 'echo 3 > /proc/sys/vm/drop_caches', user='root')
  935. # now fragment all free memory
  936. stdout, _ = yield from self.testvm1.run_for_stdio(
  937. "grep ^MemFree: /proc/meminfo|awk '{print $2}'")
  938. memory_pages = int(stdout) // 4 # 4k pages
  939. alloc1 = yield from self.testvm1.run(
  940. 'ulimit -l unlimited; exec /home/user/allocator {}'.format(
  941. memory_pages),
  942. user="root",
  943. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  944. stderr=subprocess.PIPE)
  945. # wait for memory being allocated; can't use just .read(), because EOF
  946. # passing is unreliable while the process is still running
  947. alloc1.stdin.write(b'\n')
  948. yield from alloc1.stdin.drain()
  949. try:
  950. alloc_out = yield from alloc1.stdout.readexactly(
  951. len('Stage1\nStage2\nStage3\n'))
  952. except asyncio.IncompleteReadError as e:
  953. alloc_out = e.partial
  954. if b'Stage3' not in alloc_out:
  955. # read stderr only in case of failed assert (), but still have nice
  956. # failure message (don't use self.fail() directly)
  957. #
  958. # stderr isn't always read, because on not-failed run, the process
  959. # is still running, so stderr.read() will wait (indefinitely).
  960. self.assertIn(b'Stage3', alloc_out,
  961. (yield from alloc1.stderr.read()))
  962. # now, launch some window - it should get fragmented composition buffer
  963. # it is important to have some changing content there, to generate
  964. # content update events (aka damage notify)
  965. proc = yield from self.testvm1.run(
  966. 'xterm -maximized -e top')
  967. # help xdotool a little...
  968. yield from asyncio.sleep(2)
  969. if proc.returncode is not None:
  970. self.fail('xterm failed to start')
  971. # get window ID
  972. winid = (yield from asyncio.get_event_loop().run_in_executor(None,
  973. subprocess.check_output,
  974. ['xdotool', 'search', '--sync', '--onlyvisible', '--class',
  975. self.testvm1.name + ':xterm'])).decode()
  976. xprop = yield from asyncio.get_event_loop().run_in_executor(None,
  977. subprocess.check_output,
  978. ['xprop', '-notype', '-id', winid, '_QUBES_VMWINDOWID'])
  979. vm_winid = xprop.decode().strip().split(' ')[4]
  980. # now free the fragmented memory and trigger compaction
  981. alloc1.stdin.write(b'\n')
  982. yield from alloc1.stdin.drain()
  983. yield from alloc1.wait()
  984. yield from self.testvm1.run_for_stdio(
  985. 'echo 1 > /proc/sys/vm/compact_memory', user='root')
  986. # now window may be already "broken"; to be sure, allocate (=zero)
  987. # some memory
  988. alloc2 = yield from self.testvm1.run(
  989. 'ulimit -l unlimited; /home/user/allocator {}'.format(memory_pages),
  990. user='root', stdout=subprocess.PIPE)
  991. yield from alloc2.stdout.read(len('Stage1\n'))
  992. # wait for damage notify - top updates every 3 sec by default
  993. yield from asyncio.sleep(6)
  994. # now take screenshot of the window, from dom0 and VM
  995. # choose pnm format, as it doesn't have any useless metadata - easy
  996. # to compare
  997. vm_image, _ = yield from self.testvm1.run_for_stdio(
  998. 'import -window {} pnm:-'.format(vm_winid))
  999. dom0_image = yield from asyncio.get_event_loop().run_in_executor(None,
  1000. subprocess.check_output, ['import', '-window', winid, 'pnm:-'])
  1001. if vm_image != dom0_image:
  1002. self.fail("Dom0 window doesn't match VM window content")
  1003. class TC_10_Generic(qubes.tests.SystemTestCase):
  1004. def setUp(self):
  1005. super(TC_10_Generic, self).setUp()
  1006. self.init_default_template()
  1007. self.vm = self.app.add_new_vm(
  1008. qubes.vm.appvm.AppVM,
  1009. name=self.make_vm_name('vm'),
  1010. label='red',
  1011. template=self.app.default_template)
  1012. self.loop.run_until_complete(self.vm.create_on_disk())
  1013. self.app.save()
  1014. self.vm = self.app.domains[self.vm.qid]
  1015. def test_000_anyvm_deny_dom0(self):
  1016. '''$anyvm in policy should not match dom0'''
  1017. policy = open("/etc/qubes-rpc/policy/test.AnyvmDeny", "w")
  1018. policy.write("%s $anyvm allow" % (self.vm.name,))
  1019. policy.close()
  1020. self.addCleanup(os.unlink, "/etc/qubes-rpc/policy/test.AnyvmDeny")
  1021. flagfile = '/tmp/test-anyvmdeny-flag'
  1022. if os.path.exists(flagfile):
  1023. os.remove(flagfile)
  1024. self.create_local_file('/etc/qubes-rpc/test.AnyvmDeny',
  1025. 'touch {}\necho service output\n'.format(flagfile))
  1026. self.loop.run_until_complete(self.vm.start())
  1027. with self.qrexec_policy('test.AnyvmDeny', self.vm, '$anyvm'):
  1028. with self.assertRaises(subprocess.CalledProcessError,
  1029. msg='$anyvm matched dom0') as e:
  1030. self.loop.run_until_complete(
  1031. self.vm.run_for_stdio(
  1032. '/usr/lib/qubes/qrexec-client-vm dom0 test.AnyvmDeny'))
  1033. stdout = e.exception.output
  1034. stderr = e.exception.stderr
  1035. self.assertFalse(os.path.exists(flagfile),
  1036. 'Flag file created (service was run) even though should be denied,'
  1037. ' qrexec-client-vm output: {} {}'.format(stdout, stderr))
  1038. def load_tests(loader, tests, pattern):
  1039. tests.addTests(loader.loadTestsFromNames(
  1040. qubes.tests.create_testcases_for_templates('TC_00_AppVM',
  1041. TC_00_AppVMMixin, qubes.tests.SystemTestCase,
  1042. module=sys.modules[__name__])))
  1043. return tests