r3compatibility.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  6. # <marmarek@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 datetime
  22. import qubes.ext
  23. import qubes.firewall
  24. import qubes.vm.qubesvm
  25. import qubes.vm.appvm
  26. import qubes.vm.templatevm
  27. import qubes.utils
  28. yum_proxy_ip = '10.137.255.254'
  29. yum_proxy_port = '8082'
  30. class R3Compatibility(qubes.ext.Extension):
  31. '''Maintain VM interface compatibility with R3.0 and R3.1.
  32. At least where possible.
  33. '''
  34. features_to_services = {
  35. 'service.ntpd': 'ntpd',
  36. 'check-updates': 'qubes-update-check',
  37. 'dvm': 'qubes-dvm',
  38. }
  39. # noinspection PyUnusedLocal
  40. @qubes.ext.handler('domain-qdb-create')
  41. def on_domain_qdb_create(self, vm, event):
  42. '''
  43. :param qubes.vm.qubesvm.QubesVM vm: \
  44. VM on which QubesDB entries were just created
  45. ''' # pylint: disable=unused-argument
  46. # /qubes-vm-type: AppVM, NetVM, ProxyVM, TemplateVM
  47. if isinstance(vm, qubes.vm.templatevm.TemplateVM):
  48. vmtype = 'TemplateVM'
  49. elif vm.netvm is not None and vm.provides_network:
  50. vmtype = 'ProxyVM'
  51. elif vm.netvm is None and vm.provides_network:
  52. vmtype = 'NetVM'
  53. else:
  54. vmtype = 'AppVM'
  55. vm.untrusted_qdb.write('/qubes-vm-type', vmtype)
  56. vm.untrusted_qdb.write("/qubes-iptables-error", '')
  57. self.write_iptables_qubesdb_entry(vm)
  58. self.write_services(vm)
  59. @qubes.ext.handler('domain-spawn')
  60. def on_domain_started(self, vm, event, **kwargs):
  61. # pylint: disable=unused-argument
  62. if vm.netvm:
  63. self.write_iptables_qubesdb_entry(vm.netvm)
  64. @qubes.ext.handler('firewall-changed')
  65. def on_firewall_changed(self, vm, event):
  66. # pylint: disable=unused-argument
  67. if vm.is_running() and vm.netvm:
  68. self.write_iptables_qubesdb_entry(vm.netvm)
  69. def write_iptables_qubesdb_entry(self, firewallvm):
  70. # pylint: disable=no-self-use
  71. # skip compatibility rules if new format support is advertised
  72. if firewallvm.features.check_with_template('qubes-firewall', False):
  73. return
  74. firewallvm.untrusted_qdb.rm("/qubes-iptables-domainrules/")
  75. iptables = "# Generated by Qubes Core on {0}\n".format(
  76. datetime.datetime.now().ctime())
  77. iptables += "*filter\n"
  78. iptables += ":INPUT DROP [0:0]\n"
  79. iptables += ":FORWARD DROP [0:0]\n"
  80. iptables += ":OUTPUT ACCEPT [0:0]\n"
  81. # Strict INPUT rules
  82. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  83. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  84. "-j ACCEPT\n"
  85. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  86. iptables += "-A INPUT -i lo -j ACCEPT\n"
  87. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  88. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  89. "-j ACCEPT\n"
  90. # Deny inter-VMs networking
  91. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  92. iptables += "COMMIT\n"
  93. firewallvm.untrusted_qdb.write("/qubes-iptables-header", iptables)
  94. for vm in firewallvm.connected_vms:
  95. iptables = "*filter\n"
  96. conf = vm.firewall
  97. xid = vm.xid
  98. if xid < 0: # VM not active ATM
  99. continue
  100. ip = vm.ip
  101. if ip is None:
  102. continue
  103. # Anti-spoof rules are added by vif-script (vif-route-qubes),
  104. # here we trust IP address
  105. for rule in conf.rules:
  106. if rule.specialtarget == 'dns':
  107. if rule.dstports not in ('53', None):
  108. continue
  109. if rule.proto:
  110. protos = {'tcp', 'udp'}.intersection(str(rule.proto))
  111. else:
  112. protos = {'tcp', 'udp'}
  113. for proto in protos:
  114. if rule.dsthost:
  115. dsthosts = set(vm.dns).intersection(
  116. [str(rule.dsthost).replace('/24', '')])
  117. else:
  118. dsthosts = vm.dns
  119. for dsthost in dsthosts:
  120. iptables += '-A FORWARD -s {}'.format(ip)
  121. iptables += ' -d {!s}'.format(dsthost)
  122. iptables += ' -p {!s}'.format(proto)
  123. iptables += ' --dport 53'
  124. iptables += ' -j {}\n'.format(
  125. str(rule.action).upper())
  126. else:
  127. iptables += '-A FORWARD -s {}'.format(ip)
  128. if rule.dsthost:
  129. iptables += ' -d {!s}'.format(rule.dsthost)
  130. if rule.proto:
  131. iptables += ' -p {!s}'.format(rule.proto)
  132. if rule.dstports:
  133. iptables += ' --dport {}'.format(
  134. str(rule.dstports).replace('-', ':'))
  135. iptables += ' -j {0}\n'.format(str(rule.action).upper())
  136. iptables += '-A FORWARD -s {0} -j {1}\n'.format(ip,
  137. str(conf.policy).upper())
  138. iptables += 'COMMIT\n'
  139. firewallvm.untrusted_qdb.write(
  140. '/qubes-iptables-domainrules/' + str(xid),
  141. iptables)
  142. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  143. firewallvm.untrusted_qdb.write('/qubes-iptables', 'reload')
  144. def write_services(self, vm):
  145. for feature, value in vm.features.items():
  146. service = self.features_to_services.get(feature, None)
  147. if service is None:
  148. continue
  149. # forcefully convert to '0' or '1'
  150. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  151. str(int(bool(value))))
  152. if 'updates-proxy-setup' in vm.features.keys():
  153. vm.untrusted_qdb.write(
  154. '/qubes-service/{}'.format('yum-proxy-setup'),
  155. str(int(bool(vm.features['updates-proxy-setup']))))