qrexec.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2020
  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 os
  23. import subprocess
  24. import sys
  25. import qubes.config
  26. import qubes.devices
  27. import qubes.tests
  28. import qubes.vm.appvm
  29. import qubes.vm.templatevm
  30. TEST_DATA = b"0123456789" * 1024
  31. class TC_00_QrexecMixin(object):
  32. def setUp(self):
  33. super().setUp()
  34. self.init_default_template(self.template)
  35. if self._testMethodName == 'test_210_time_sync':
  36. self.init_networking()
  37. self.testvm1 = self.app.add_new_vm(
  38. qubes.vm.appvm.AppVM,
  39. label='red',
  40. name=self.make_vm_name('vm1'),
  41. template=self.app.domains[self.template])
  42. self.loop.run_until_complete(self.testvm1.create_on_disk())
  43. self.testvm2 = self.app.add_new_vm(
  44. qubes.vm.appvm.AppVM,
  45. label='red',
  46. name=self.make_vm_name('vm2'),
  47. template=self.app.domains[self.template])
  48. self.loop.run_until_complete(self.testvm2.create_on_disk())
  49. self.app.save()
  50. def tearDown(self):
  51. # socket-based qrexec tests:
  52. if os.path.exists('/etc/qubes-rpc/test.Socket'):
  53. os.unlink('/etc/qubes-rpc/test.Socket')
  54. if hasattr(self, 'service_proc'):
  55. try:
  56. self.service_proc.terminate()
  57. self.loop.run_until_complete(self.service_proc.communicate())
  58. except ProcessLookupError:
  59. pass
  60. super().tearDown()
  61. def test_050_qrexec_simple_eof(self):
  62. """Test for data and EOF transmission dom0->VM"""
  63. # XXX is this still correct? this is no longer simple qrexec,
  64. # but qubes.VMShell
  65. self.loop.run_until_complete(self.testvm1.start())
  66. try:
  67. (stdout, stderr) = self.loop.run_until_complete(asyncio.wait_for(
  68. self.testvm1.run_for_stdio('cat', input=TEST_DATA),
  69. timeout=10))
  70. except asyncio.TimeoutError:
  71. self.fail(
  72. "Timeout, probably EOF wasn't transferred to the VM process")
  73. self.assertEqual(stdout, TEST_DATA,
  74. 'Received data differs from what was sent')
  75. self.assertFalse(stderr,
  76. 'Some data was printed to stderr')
  77. def test_051_qrexec_simple_eof_reverse(self):
  78. """Test for EOF transmission VM->dom0"""
  79. @asyncio.coroutine
  80. def run(self):
  81. p = yield from self.testvm1.run(
  82. 'echo test; exec >&-; cat > /dev/null',
  83. stdin=subprocess.PIPE,
  84. stdout=subprocess.PIPE,
  85. stderr=subprocess.PIPE)
  86. # this will hang on test failure
  87. stdout = yield from asyncio.wait_for(p.stdout.read(), timeout=10)
  88. p.stdin.write(TEST_DATA)
  89. yield from p.stdin.drain()
  90. p.stdin.close()
  91. self.assertEqual(stdout.strip(), b'test',
  92. 'Received data differs from what was expected')
  93. # this may hang in some buggy cases
  94. self.assertFalse((yield from p.stderr.read()),
  95. 'Some data was printed to stderr')
  96. try:
  97. yield from asyncio.wait_for(p.wait(), timeout=1)
  98. except asyncio.TimeoutError:
  99. self.fail("Timeout, "
  100. "probably EOF wasn't transferred from the VM process")
  101. self.loop.run_until_complete(self.testvm1.start())
  102. self.loop.run_until_complete(self.wait_for_session(self.testvm1))
  103. self.loop.run_until_complete(run(self))
  104. def test_052_qrexec_vm_service_eof(self):
  105. """Test for EOF transmission VM(src)->VM(dst)"""
  106. self.loop.run_until_complete(asyncio.wait([
  107. self.testvm1.start(),
  108. self.testvm2.start()]))
  109. self.loop.run_until_complete(asyncio.wait([
  110. self.wait_for_session(self.testvm1),
  111. self.wait_for_session(self.testvm2)]))
  112. self.create_remote_file(self.testvm2,
  113. '/etc/qubes-rpc/test.EOF',
  114. '#!/bin/sh\n/bin/cat\n')
  115. with self.qrexec_policy('test.EOF', self.testvm1, self.testvm2):
  116. try:
  117. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  118. self.testvm1.run_for_stdio('''\
  119. /usr/lib/qubes/qrexec-client-vm {} test.EOF \
  120. /bin/sh -c 'echo test; exec >&-; cat >&$SAVED_FD_1'
  121. '''.format(self.testvm2.name)),
  122. timeout=10))
  123. except subprocess.CalledProcessError as e:
  124. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  125. e.cmd, e.returncode, e.stderr))
  126. except asyncio.TimeoutError:
  127. self.fail("Timeout, probably EOF wasn't transferred")
  128. self.assertEqual(stdout, b'test\n',
  129. 'Received data differs from what was expected')
  130. def test_053_qrexec_vm_service_eof_reverse(self):
  131. """Test for EOF transmission VM(src)<-VM(dst)"""
  132. self.loop.run_until_complete(asyncio.wait([
  133. self.testvm1.start(),
  134. self.testvm2.start()]))
  135. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.EOF',
  136. '#!/bin/sh\n'
  137. 'echo test; exec >&-; cat >/dev/null')
  138. with self.qrexec_policy('test.EOF', self.testvm1, self.testvm2):
  139. try:
  140. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  141. self.testvm1.run_for_stdio('''\
  142. /usr/lib/qubes/qrexec-client-vm {} test.EOF \
  143. /bin/sh -c 'cat >&$SAVED_FD_1'
  144. '''.format(self.testvm2.name)),
  145. timeout=10))
  146. except subprocess.CalledProcessError as e:
  147. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  148. e.cmd, e.returncode, e.stderr))
  149. except asyncio.TimeoutError:
  150. self.fail("Timeout, probably EOF wasn't transferred")
  151. self.assertEqual(stdout, b'test\n',
  152. 'Received data differs from what was expected')
  153. def test_055_qrexec_dom0_service_abort(self):
  154. """
  155. Test if service abort (by dom0) is properly handled by source VM.
  156. If "remote" part of the service terminates, the source part should
  157. properly be notified. This includes closing its stdin (which is
  158. already checked by test_053_qrexec_vm_service_eof_reverse), but also
  159. its stdout - otherwise such service might hang on write(2) call.
  160. """
  161. self.loop.run_until_complete(self.testvm1.start())
  162. self.create_local_file('/etc/qubes-rpc/test.Abort',
  163. 'sleep 1')
  164. with self.qrexec_policy('test.Abort', self.testvm1, 'dom0'):
  165. try:
  166. # two possible exit codes, depending on when exactly dom0
  167. # service terminates:
  168. # exit code 141: EPIPE (no buffered data)
  169. # exit code 1: ECONNRESET (some buffered data remains)
  170. stdout, _ = self.loop.run_until_complete(asyncio.wait_for(
  171. self.testvm1.run_for_stdio('''\
  172. /usr/lib/qubes/qrexec-client-vm dom0 test.Abort \
  173. /bin/sh -c 'cat /dev/zero; echo $? >/tmp/exit-code';
  174. e=$(cat /tmp/exit-code);
  175. test $e -eq 141 -o $e -eq 1'''),
  176. timeout=10))
  177. except subprocess.CalledProcessError as e:
  178. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  179. e.cmd, e.returncode, e.stderr))
  180. except asyncio.TimeoutError:
  181. self.fail("Timeout, probably stdout wasn't closed")
  182. def test_060_qrexec_exit_code_dom0(self):
  183. self.loop.run_until_complete(self.testvm1.start())
  184. self.loop.run_until_complete(self.testvm1.run_for_stdio('exit 0'))
  185. with self.assertRaises(subprocess.CalledProcessError) as e:
  186. self.loop.run_until_complete(self.testvm1.run_for_stdio('exit 3'))
  187. self.assertEqual(e.exception.returncode, 3)
  188. def test_065_qrexec_exit_code_vm(self):
  189. self.loop.run_until_complete(asyncio.wait([
  190. self.testvm1.start(),
  191. self.testvm2.start()]))
  192. with self.qrexec_policy('test.Retcode', self.testvm1, self.testvm2):
  193. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Retcode',
  194. 'exit 0')
  195. (stdout, stderr) = self.loop.run_until_complete(
  196. self.testvm1.run_for_stdio('''\
  197. /usr/lib/qubes/qrexec-client-vm {} test.Retcode;
  198. echo $?'''.format(self.testvm2.name),
  199. stderr=None))
  200. self.assertEqual(stdout, b'0\n')
  201. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Retcode',
  202. 'exit 3')
  203. (stdout, stderr) = self.loop.run_until_complete(
  204. self.testvm1.run_for_stdio('''\
  205. /usr/lib/qubes/qrexec-client-vm {} test.Retcode;
  206. echo $?'''.format(self.testvm2.name),
  207. stderr=None))
  208. self.assertEqual(stdout, b'3\n')
  209. def test_070_qrexec_vm_simultaneous_write(self):
  210. """Test for simultaneous write in VM(src)->VM(dst) connection
  211. This is regression test for #1347
  212. Check for deadlock when initially both sides writes a lot of data
  213. (and not read anything). When one side starts reading, it should
  214. get the data and the remote side should be possible to write then more.
  215. There was a bug where remote side was waiting on write(2) and not
  216. handling anything else.
  217. """
  218. self.loop.run_until_complete(asyncio.wait([
  219. self.testvm1.start(),
  220. self.testvm2.start()]))
  221. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  222. # first write a lot of data
  223. dd if=/dev/zero bs=993 count=10000 iflag=fullblock
  224. # and only then read something
  225. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  226. ''')
  227. with self.qrexec_policy('test.write', self.testvm1, self.testvm2):
  228. try:
  229. self.loop.run_until_complete(asyncio.wait_for(
  230. # first write a lot of data to fill all the buffers
  231. # then after some time start reading
  232. self.testvm1.run_for_stdio('''\
  233. /usr/lib/qubes/qrexec-client-vm {} test.write \
  234. /bin/sh -c '
  235. dd if=/dev/zero bs=993 count=10000 iflag=fullblock &
  236. sleep 1;
  237. dd of=/dev/null bs=993 count=10000 iflag=fullblock;
  238. wait'
  239. '''.format(self.testvm2.name)), timeout=10))
  240. except subprocess.CalledProcessError as e:
  241. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  242. e.cmd, e.returncode, e.stderr))
  243. except asyncio.TimeoutError:
  244. self.fail('Timeout, probably deadlock')
  245. def test_071_qrexec_dom0_simultaneous_write(self):
  246. """Test for simultaneous write in dom0(src)->VM(dst) connection
  247. Similar to test_070_qrexec_vm_simultaneous_write, but with dom0
  248. as a source.
  249. """
  250. self.loop.run_until_complete(self.testvm2.start())
  251. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  252. # first write a lot of data
  253. dd if=/dev/zero bs=993 count=10000 iflag=fullblock
  254. # and only then read something
  255. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  256. ''')
  257. # can't use subprocess.PIPE, because asyncio will claim those FDs
  258. pipe1_r, pipe1_w = os.pipe()
  259. pipe2_r, pipe2_w = os.pipe()
  260. try:
  261. local_proc = self.loop.run_until_complete(
  262. asyncio.create_subprocess_shell(
  263. # first write a lot of data to fill all the buffers
  264. "dd if=/dev/zero bs=993 count=10000 iflag=fullblock & "
  265. # then after some time start reading
  266. "sleep 1; "
  267. "dd of=/dev/null bs=993 count=10000 iflag=fullblock; "
  268. "wait", stdin=pipe1_r, stdout=pipe2_w))
  269. self.service_proc = self.loop.run_until_complete(self.testvm2.run_service(
  270. "test.write", stdin=pipe2_r, stdout=pipe1_w))
  271. finally:
  272. os.close(pipe1_r)
  273. os.close(pipe1_w)
  274. os.close(pipe2_r)
  275. os.close(pipe2_w)
  276. try:
  277. self.loop.run_until_complete(
  278. asyncio.wait_for(self.service_proc.wait(), timeout=10))
  279. except asyncio.TimeoutError:
  280. self.fail("Timeout, probably deadlock")
  281. else:
  282. self.assertEqual(self.service_proc.returncode, 0,
  283. "Service call failed")
  284. def test_072_qrexec_to_dom0_simultaneous_write(self):
  285. """Test for simultaneous write in dom0(src)<-VM(dst) connection
  286. Similar to test_071_qrexec_dom0_simultaneous_write, but with dom0
  287. as a "hanging" side.
  288. """
  289. self.loop.run_until_complete(self.testvm2.start())
  290. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.write', '''\
  291. # first write a lot of data
  292. dd if=/dev/zero bs=993 count=10000 iflag=fullblock &
  293. # and only then read something
  294. dd of=/dev/null bs=993 count=10000 iflag=fullblock
  295. sleep 1;
  296. wait
  297. ''')
  298. # can't use subprocess.PIPE, because asyncio will claim those FDs
  299. pipe1_r, pipe1_w = os.pipe()
  300. pipe2_r, pipe2_w = os.pipe()
  301. try:
  302. local_proc = self.loop.run_until_complete(
  303. asyncio.create_subprocess_shell(
  304. # first write a lot of data to fill all the buffers
  305. "dd if=/dev/zero bs=993 count=10000 iflag=fullblock & "
  306. # then, only when all written, read something
  307. "dd of=/dev/null bs=993 count=10000 iflag=fullblock; ",
  308. stdin=pipe1_r, stdout=pipe2_w))
  309. self.service_proc = self.loop.run_until_complete(
  310. self.testvm2.run_service(
  311. "test.write", stdin=pipe2_r, stdout=pipe1_w))
  312. finally:
  313. os.close(pipe1_r)
  314. os.close(pipe1_w)
  315. os.close(pipe2_r)
  316. os.close(pipe2_w)
  317. try:
  318. self.loop.run_until_complete(
  319. asyncio.wait_for(self.service_proc.wait(), timeout=10))
  320. except asyncio.TimeoutError:
  321. self.fail("Timeout, probably deadlock")
  322. else:
  323. self.assertEqual(self.service_proc.returncode, 0,
  324. "Service call failed")
  325. def test_080_qrexec_service_argument_allow_default(self):
  326. """Qrexec service call with argument"""
  327. self.loop.run_until_complete(asyncio.wait([
  328. self.testvm1.start(),
  329. self.testvm2.start()]))
  330. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  331. '/usr/bin/printf %s "$1"')
  332. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  333. stdout, stderr = self.loop.run_until_complete(
  334. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  335. '{} test.Argument+argument'.format(self.testvm2.name),
  336. stderr=None))
  337. self.assertEqual(stdout, b'argument')
  338. def test_081_qrexec_service_argument_allow_specific(self):
  339. """Qrexec service call with argument - allow only specific value"""
  340. self.loop.run_until_complete(asyncio.wait([
  341. self.testvm1.start(),
  342. self.testvm2.start()]))
  343. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  344. '/usr/bin/printf %s "$1"')
  345. with self.qrexec_policy('test.Argument', '$anyvm', '$anyvm', False):
  346. with self.qrexec_policy('test.Argument+argument',
  347. self.testvm1.name, self.testvm2.name):
  348. stdout, stderr = self.loop.run_until_complete(
  349. self.testvm1.run_for_stdio(
  350. '/usr/lib/qubes/qrexec-client-vm '
  351. '{} test.Argument+argument'.format(self.testvm2.name),
  352. stderr=None))
  353. self.assertEqual(stdout, b'argument')
  354. def test_082_qrexec_service_argument_deny_specific(self):
  355. """Qrexec service call with argument - deny specific value"""
  356. self.loop.run_until_complete(asyncio.wait([
  357. self.testvm1.start(),
  358. self.testvm2.start()]))
  359. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  360. '/usr/bin/printf %s "$1"')
  361. with self.qrexec_policy('test.Argument', '$anyvm', '$anyvm'):
  362. with self.qrexec_policy('test.Argument+argument',
  363. self.testvm1, self.testvm2, allow=False):
  364. with self.assertRaises(subprocess.CalledProcessError,
  365. msg='Service request should be denied'):
  366. self.loop.run_until_complete(
  367. self.testvm1.run_for_stdio(
  368. '/usr/lib/qubes/qrexec-client-vm {} '
  369. 'test.Argument+argument'.format(self.testvm2.name),
  370. stderr=None))
  371. def test_083_qrexec_service_argument_specific_implementation(self):
  372. """Qrexec service call with argument - argument specific
  373. implementatation"""
  374. self.loop.run_until_complete(asyncio.wait([
  375. self.testvm1.start(),
  376. self.testvm2.start()]))
  377. self.create_remote_file(self.testvm2,
  378. '/etc/qubes-rpc/test.Argument',
  379. '/usr/bin/printf %s "$1"')
  380. self.create_remote_file(self.testvm2,
  381. '/etc/qubes-rpc/test.Argument+argument',
  382. '/usr/bin/printf "specific: %s" "$1"')
  383. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  384. stdout, stderr = self.loop.run_until_complete(
  385. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  386. '{} test.Argument+argument'.format(self.testvm2.name),
  387. stderr=None))
  388. self.assertEqual(stdout, b'specific: argument')
  389. def test_084_qrexec_service_argument_extra_env(self):
  390. """Qrexec service call with argument - extra env variables"""
  391. self.loop.run_until_complete(asyncio.wait([
  392. self.testvm1.start(),
  393. self.testvm2.start()]))
  394. self.create_remote_file(self.testvm2, '/etc/qubes-rpc/test.Argument',
  395. '/usr/bin/printf "%s %s" '
  396. '"$QREXEC_SERVICE_FULL_NAME" "$QREXEC_SERVICE_ARGUMENT"')
  397. with self.qrexec_policy('test.Argument', self.testvm1, self.testvm2):
  398. stdout, stderr = self.loop.run_until_complete(
  399. self.testvm1.run_for_stdio('/usr/lib/qubes/qrexec-client-vm '
  400. '{} test.Argument+argument'.format(self.testvm2.name),
  401. stderr=None))
  402. self.assertEqual(stdout, b'test.Argument+argument argument')
  403. def test_090_qrexec_service_socket_dom0(self):
  404. """Basic test socket services (dom0) - data receive"""
  405. self.loop.run_until_complete(self.testvm1.start())
  406. self.service_proc = self.loop.run_until_complete(
  407. asyncio.create_subprocess_shell(
  408. 'socat -u UNIX-LISTEN:/etc/qubes-rpc/test.Socket,mode=666 -',
  409. stdout=subprocess.PIPE, stdin=subprocess.PIPE))
  410. try:
  411. with self.qrexec_policy('test.Socket', self.testvm1, '@adminvm'):
  412. (stdout, stderr) = self.loop.run_until_complete(asyncio.wait_for(
  413. self.testvm1.run_for_stdio(
  414. 'qrexec-client-vm @adminvm test.Socket', input=TEST_DATA),
  415. timeout=10))
  416. except subprocess.CalledProcessError as e:
  417. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  418. e.cmd, e.returncode, e.stderr))
  419. except asyncio.TimeoutError:
  420. self.fail(
  421. "service timeout, probably EOF wasn't transferred to the VM process")
  422. try:
  423. (service_stdout, service_stderr) = self.loop.run_until_complete(
  424. asyncio.wait_for(
  425. self.service_proc.communicate(),
  426. timeout=10))
  427. except asyncio.TimeoutError:
  428. self.fail(
  429. "socat timeout, probably EOF wasn't transferred to the VM process")
  430. service_descriptor = b'test.Socket+ test-inst-vm1 keyword adminvm\0'
  431. self.assertEqual(service_stdout, service_descriptor + TEST_DATA,
  432. 'Received data differs from what was sent')
  433. self.assertFalse(stderr,
  434. 'Some data was printed to stderr')
  435. self.assertFalse(service_stderr,
  436. 'Some data was printed to stderr')
  437. def test_091_qrexec_service_socket_dom0_send(self):
  438. """Basic test socket services (dom0) - data send"""
  439. self.loop.run_until_complete(self.testvm1.start())
  440. self.create_local_file('/tmp/service-input', TEST_DATA.decode())
  441. self.service_proc = self.loop.run_until_complete(
  442. asyncio.create_subprocess_shell(
  443. 'socat -u OPEN:/tmp/service-input UNIX-LISTEN:/etc/qubes-rpc/test.Socket,mode=666'))
  444. try:
  445. with self.qrexec_policy('test.Socket', self.testvm1, '@adminvm'):
  446. stdout, stderr = self.loop.run_until_complete(asyncio.wait_for(
  447. self.testvm1.run_for_stdio(
  448. 'qrexec-client-vm @adminvm test.Socket'),
  449. timeout=10))
  450. except subprocess.CalledProcessError as e:
  451. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  452. e.cmd, e.returncode, e.stderr))
  453. except asyncio.TimeoutError:
  454. self.fail(
  455. "service timeout, probably EOF wasn't transferred to the VM process")
  456. try:
  457. (service_stdout, service_stderr) = self.loop.run_until_complete(
  458. asyncio.wait_for(
  459. self.service_proc.communicate(),
  460. timeout=10))
  461. except asyncio.TimeoutError:
  462. self.fail(
  463. "socat timeout, probably EOF wasn't transferred to the VM process")
  464. self.assertEqual(stdout, TEST_DATA,
  465. 'Received data differs from what was sent')
  466. self.assertFalse(stderr,
  467. 'Some data was printed to stderr')
  468. self.assertFalse(service_stderr,
  469. 'Some data was printed to stderr')
  470. def test_092_qrexec_service_socket_dom0_eof_reverse(self):
  471. """Test for EOF transmission dom0(socket)->VM"""
  472. self.loop.run_until_complete(self.testvm1.start())
  473. self.create_local_file(
  474. '/tmp/service_script',
  475. '#!/usr/bin/python3\n'
  476. 'import socket, os, sys, time\n'
  477. 's = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n'
  478. 'os.umask(0)\n'
  479. 's.bind("/etc/qubes-rpc/test.Socket")\n'
  480. 's.listen(1)\n'
  481. 'conn, addr = s.accept()\n'
  482. 'conn.send(b"test\\n")\n'
  483. 'conn.shutdown(socket.SHUT_WR)\n'
  484. # wait longer than the timeout below
  485. 'time.sleep(15)\n'
  486. )
  487. self.service_proc = self.loop.run_until_complete(
  488. asyncio.create_subprocess_shell('python3 /tmp/service_script',
  489. stdout=subprocess.PIPE, stdin=subprocess.PIPE))
  490. try:
  491. with self.qrexec_policy('test.Socket', self.testvm1, '@adminvm'):
  492. p = self.loop.run_until_complete(self.testvm1.run(
  493. 'qrexec-client-vm @adminvm test.Socket',
  494. stdout=subprocess.PIPE, stdin=subprocess.PIPE))
  495. stdout = self.loop.run_until_complete(asyncio.wait_for(
  496. p.stdout.read(),
  497. timeout=10))
  498. except asyncio.TimeoutError:
  499. self.fail(
  500. "service timeout, probably EOF wasn't transferred from the VM process")
  501. self.assertEqual(stdout, b'test\n',
  502. 'Received data differs from what was expected')
  503. def test_093_qrexec_service_socket_dom0_eof(self):
  504. """Test for EOF transmission VM->dom0(socket)"""
  505. self.loop.run_until_complete(self.testvm1.start())
  506. self.create_local_file(
  507. '/tmp/service_script',
  508. '#!/usr/bin/python3\n'
  509. 'import socket, os, sys, time\n'
  510. 's = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n'
  511. 'os.umask(0)\n'
  512. 's.bind("/etc/qubes-rpc/test.Socket")\n'
  513. 's.listen(1)\n'
  514. 'conn, addr = s.accept()\n'
  515. 'buf = conn.recv(100)\n'
  516. 'sys.stdout.buffer.write(buf)\n'
  517. 'buf = conn.recv(10)\n'
  518. 'sys.stdout.buffer.write(buf)\n'
  519. 'sys.stdout.buffer.flush()\n'
  520. 'os.close(1)\n'
  521. # wait longer than the timeout below
  522. 'time.sleep(15)\n'
  523. )
  524. self.service_proc = self.loop.run_until_complete(
  525. asyncio.create_subprocess_shell('python3 /tmp/service_script',
  526. stdout=subprocess.PIPE, stdin=subprocess.PIPE))
  527. try:
  528. with self.qrexec_policy('test.Socket', self.testvm1, '@adminvm'):
  529. p = self.loop.run_until_complete(self.testvm1.run(
  530. 'qrexec-client-vm @adminvm test.Socket',
  531. stdin=subprocess.PIPE))
  532. p.stdin.write(b'test1test2')
  533. p.stdin.write_eof()
  534. service_stdout = self.loop.run_until_complete(asyncio.wait_for(
  535. self.service_proc.stdout.read(),
  536. timeout=10))
  537. except asyncio.TimeoutError:
  538. self.fail(
  539. "service timeout, probably EOF wasn't transferred from the VM process")
  540. service_descriptor = b'test.Socket+ test-inst-vm1 keyword adminvm\0'
  541. self.assertEqual(service_stdout, service_descriptor + b'test1test2',
  542. 'Received data differs from what was expected')
  543. def _wait_for_socket_setup(self):
  544. try:
  545. self.loop.run_until_complete(asyncio.wait_for(
  546. self.testvm1.run_for_stdio(
  547. 'while ! test -e /etc/qubes-rpc/test.Socket; do sleep 0.1; done'),
  548. timeout=10))
  549. except asyncio.TimeoutError:
  550. self.fail(
  551. "waiting for /etc/qubes-rpc/test.Socket in VM timed out")
  552. def test_095_qrexec_service_socket_vm(self):
  553. """Basic test socket services (VM) - receive"""
  554. self.loop.run_until_complete(self.testvm1.start())
  555. self.service_proc = self.loop.run_until_complete(self.testvm1.run(
  556. 'socat -u UNIX-LISTEN:/etc/qubes-rpc/test.Socket,mode=666 -',
  557. stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  558. user='root'))
  559. self._wait_for_socket_setup()
  560. try:
  561. (stdout, stderr) = self.loop.run_until_complete(asyncio.wait_for(
  562. self.testvm1.run_service_for_stdio('test.Socket+', input=TEST_DATA),
  563. timeout=10))
  564. except subprocess.CalledProcessError as e:
  565. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  566. e.cmd, e.returncode, e.stderr))
  567. except asyncio.TimeoutError:
  568. self.fail(
  569. "service timeout, probably EOF wasn't transferred to the VM process")
  570. try:
  571. (service_stdout, service_stderr) = self.loop.run_until_complete(
  572. asyncio.wait_for(
  573. self.service_proc.communicate(),
  574. timeout=10))
  575. except asyncio.TimeoutError:
  576. self.fail(
  577. "socat timeout, probably EOF wasn't transferred to the VM process")
  578. service_descriptor = b'test.Socket+ dom0\0'
  579. self.assertEqual(service_stdout, service_descriptor + TEST_DATA,
  580. 'Received data differs from what was sent')
  581. self.assertFalse(stderr,
  582. 'Some data was printed to stderr')
  583. self.assertFalse(service_stderr,
  584. 'Some data was printed to stderr')
  585. def test_096_qrexec_service_socket_vm_send(self):
  586. """Basic test socket services (VM) - send"""
  587. self.loop.run_until_complete(self.testvm1.start())
  588. self.create_remote_file(self.testvm1,
  589. '/tmp/service-input',
  590. TEST_DATA.decode())
  591. self.service_proc = self.loop.run_until_complete(self.testvm1.run(
  592. 'socat -u OPEN:/tmp/service-input UNIX-LISTEN:/etc/qubes-rpc/test.Socket,mode=666',
  593. user='root'))
  594. self._wait_for_socket_setup()
  595. try:
  596. (stdout, stderr) = self.loop.run_until_complete(asyncio.wait_for(
  597. self.testvm1.run_service_for_stdio('test.Socket+'),
  598. timeout=10))
  599. except subprocess.CalledProcessError as e:
  600. self.fail('{} exited with non-zero code {}; stderr: {}'.format(
  601. e.cmd, e.returncode, e.stderr))
  602. except asyncio.TimeoutError:
  603. self.fail(
  604. "service timeout, probably EOF wasn't transferred to the VM process")
  605. try:
  606. (service_stdout, service_stderr) = self.loop.run_until_complete(
  607. asyncio.wait_for(
  608. self.service_proc.communicate(),
  609. timeout=10))
  610. except asyncio.TimeoutError:
  611. self.fail(
  612. "socat timeout, probably EOF wasn't transferred to the VM process")
  613. self.assertEqual(stdout, TEST_DATA,
  614. 'Received data differs from what was sent')
  615. self.assertFalse(stderr,
  616. 'Some data was printed to stderr')
  617. self.assertFalse(service_stderr,
  618. 'Some data was printed to stderr')
  619. def test_097_qrexec_service_socket_vm_eof_reverse(self):
  620. """Test for EOF transmission VM(socket)->dom0"""
  621. self.loop.run_until_complete(self.testvm1.start())
  622. self.create_remote_file(self.testvm1,
  623. '/tmp/service_script',
  624. '#!/usr/bin/python3\n'
  625. 'import socket, os, sys, time\n'
  626. 's = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n'
  627. 'os.umask(0)\n'
  628. 's.bind("/etc/qubes-rpc/test.Socket")\n'
  629. 's.listen(1)\n'
  630. 'conn, addr = s.accept()\n'
  631. 'conn.send(b"test\\n")\n'
  632. 'conn.shutdown(socket.SHUT_WR)\n'
  633. # wait longer than the timeout below
  634. 'time.sleep(15)\n'
  635. )
  636. self.service_proc = self.loop.run_until_complete(self.testvm1.run(
  637. 'python3 /tmp/service_script',
  638. stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  639. user='root'))
  640. self._wait_for_socket_setup()
  641. try:
  642. p = self.loop.run_until_complete(
  643. self.testvm1.run_service('test.Socket+',
  644. stdin=subprocess.PIPE, stdout=subprocess.PIPE))
  645. stdout = self.loop.run_until_complete(asyncio.wait_for(p.stdout.read(),
  646. timeout=10))
  647. except asyncio.TimeoutError:
  648. p.terminate()
  649. self.fail(
  650. "service timeout, probably EOF wasn't transferred from the VM process")
  651. finally:
  652. self.loop.run_until_complete(p.wait())
  653. self.assertEqual(stdout,
  654. b'test\n',
  655. 'Received data differs from what was expected')
  656. def test_098_qrexec_service_socket_vm_eof(self):
  657. """Test for EOF transmission dom0->VM(socket)"""
  658. self.loop.run_until_complete(self.testvm1.start())
  659. self.create_remote_file(
  660. self.testvm1,
  661. '/tmp/service_script',
  662. '#!/usr/bin/python3\n'
  663. 'import socket, os, sys, time\n'
  664. 's = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n'
  665. 'os.umask(0)\n'
  666. 's.bind("/etc/qubes-rpc/test.Socket")\n'
  667. 's.listen(1)\n'
  668. 'conn, addr = s.accept()\n'
  669. 'buf = conn.recv(100)\n'
  670. 'sys.stdout.buffer.write(buf)\n'
  671. 'buf = conn.recv(10)\n'
  672. 'sys.stdout.buffer.write(buf)\n'
  673. 'sys.stdout.buffer.flush()\n'
  674. 'os.close(1)\n'
  675. # wait longer than the timeout below
  676. 'time.sleep(15)\n'
  677. )
  678. self.service_proc = self.loop.run_until_complete(self.testvm1.run(
  679. 'python3 /tmp/service_script',
  680. stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  681. user='root'))
  682. self._wait_for_socket_setup()
  683. try:
  684. p = self.loop.run_until_complete(
  685. self.testvm1.run_service('test.Socket+',
  686. stdin=subprocess.PIPE, stdout=subprocess.PIPE))
  687. p.stdin.write(b'test1test2')
  688. self.loop.run_until_complete(
  689. asyncio.wait_for(p.stdin.drain(), timeout=10))
  690. p.stdin.close()
  691. service_stdout = self.loop.run_until_complete(asyncio.wait_for(
  692. self.service_proc.stdout.read(),
  693. timeout=10))
  694. except asyncio.TimeoutError:
  695. p.terminate()
  696. self.fail(
  697. "service timeout, probably EOF wasn't transferred to the VM process")
  698. finally:
  699. self.loop.run_until_complete(p.wait())
  700. service_descriptor = b'test.Socket+ dom0\0'
  701. self.assertEqual(service_stdout, service_descriptor + b'test1test2',
  702. 'Received data differs from what was expected')
  703. def create_testcases_for_templates():
  704. return qubes.tests.create_testcases_for_templates('TC_00_Qrexec',
  705. TC_00_QrexecMixin, qubes.tests.SystemTestCase,
  706. module=sys.modules[__name__])
  707. def load_tests(loader, tests, pattern):
  708. tests.addTests(loader.loadTestsFromNames(
  709. create_testcases_for_templates()))
  710. return tests
  711. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)