006QubesProxyVm.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. from datetime import datetime
  25. from qubes.qubes import QubesNetVm,register_qubes_vm_class,vmm,dry_run
  26. from qubes.qubes import QubesVmCollection,QubesException
  27. yum_proxy_ip = '10.137.255.254'
  28. yum_proxy_port = '8082'
  29. class QubesProxyVm(QubesNetVm):
  30. """
  31. A class that represents a ProxyVM, ex FirewallVM. A child of QubesNetVM.
  32. """
  33. def get_attrs_config(self):
  34. attrs_config = super(QubesProxyVm, self).get_attrs_config()
  35. attrs_config['uses_default_netvm']['func'] = lambda x: False
  36. # Save netvm prop again
  37. attrs_config['netvm']['save'] = \
  38. lambda: str(self.netvm.qid) if self.netvm is not None else "none"
  39. return attrs_config
  40. def __init__(self, **kwargs):
  41. super(QubesProxyVm, self).__init__(**kwargs)
  42. self.rules_applied = None
  43. @property
  44. def type(self):
  45. return "ProxyVM"
  46. def is_proxyvm(self):
  47. return True
  48. def _set_netvm(self, new_netvm):
  49. old_netvm = self.netvm
  50. super(QubesProxyVm, self)._set_netvm(new_netvm)
  51. if self.netvm is not None:
  52. self.netvm.add_external_ip_permission(self.get_xid())
  53. self.write_netvm_domid_entry()
  54. if old_netvm is not None:
  55. old_netvm.remove_external_ip_permission(self.get_xid())
  56. def post_vm_net_attach(self, vm):
  57. """ Called after some VM net-attached to this ProxyVm """
  58. self.write_iptables_xenstore_entry()
  59. def post_vm_net_detach(self, vm):
  60. """ Called after some VM net-detached from this ProxyVm """
  61. self.write_iptables_xenstore_entry()
  62. def start(self, **kwargs):
  63. if dry_run:
  64. return
  65. retcode = super(QubesProxyVm, self).start(**kwargs)
  66. if self.netvm is not None:
  67. self.netvm.add_external_ip_permission(self.get_xid())
  68. self.write_netvm_domid_entry()
  69. return retcode
  70. def force_shutdown(self, **kwargs):
  71. if dry_run:
  72. return
  73. if self.netvm is not None:
  74. self.netvm.remove_external_ip_permission(kwargs['xid'] if 'xid' in kwargs else self.get_xid())
  75. super(QubesProxyVm, self).force_shutdown(**kwargs)
  76. def create_xenstore_entries(self, xid = None):
  77. if dry_run:
  78. return
  79. if xid is None:
  80. xid = self.xid
  81. super(QubesProxyVm, self).create_xenstore_entries(xid)
  82. vmm.xs.write('', "/local/domain/{0}/qubes-iptables-error".format(xid), '')
  83. vmm.xs.set_permissions('', "/local/domain/{0}/qubes-iptables-error".format(xid),
  84. [{ 'dom': xid, 'write': True }])
  85. self.write_iptables_xenstore_entry()
  86. def write_netvm_domid_entry(self, xid = -1):
  87. if not self.is_running():
  88. return
  89. if xid < 0:
  90. xid = self.get_xid()
  91. if self.netvm is None:
  92. vmm.xs.write('', "/local/domain/{0}/qubes-netvm-domid".format(xid), '')
  93. else:
  94. vmm.xs.write('', "/local/domain/{0}/qubes-netvm-domid".format(xid),
  95. "{0}".format(self.netvm.get_xid()))
  96. def write_iptables_xenstore_entry(self):
  97. vmm.xs.rm('', "/local/domain/{0}/qubes-iptables-domainrules".format(self.get_xid()))
  98. iptables = "# Generated by Qubes Core on {0}\n".format(datetime.now().ctime())
  99. iptables += "*filter\n"
  100. iptables += ":INPUT DROP [0:0]\n"
  101. iptables += ":FORWARD DROP [0:0]\n"
  102. iptables += ":OUTPUT ACCEPT [0:0]\n"
  103. # Strict INPUT rules
  104. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  105. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  106. "-j ACCEPT\n"
  107. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  108. iptables += "-A INPUT -i lo -j ACCEPT\n"
  109. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  110. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  111. "-j ACCEPT\n"
  112. # Allow dom0 networking
  113. iptables += "-A FORWARD -i vif0.0 -j ACCEPT\n"
  114. # Deny inter-VMs networking
  115. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  116. iptables += "COMMIT\n"
  117. vmm.xs.write('', "/local/domain/{0}/qubes-iptables-header".format(self.get_xid()), iptables)
  118. vms = [vm for vm in self.connected_vms.values()]
  119. for vm in vms:
  120. iptables="*filter\n"
  121. conf = vm.get_firewall_conf()
  122. xid = vm.get_xid()
  123. if xid < 0: # VM not active ATM
  124. continue
  125. ip = vm.ip
  126. if ip is None:
  127. continue
  128. # Anti-spoof rules are added by vif-script (vif-route-qubes), here we trust IP address
  129. accept_action = "ACCEPT"
  130. reject_action = "REJECT --reject-with icmp-host-prohibited"
  131. if conf["allow"]:
  132. default_action = accept_action
  133. rules_action = reject_action
  134. else:
  135. default_action = reject_action
  136. rules_action = accept_action
  137. for rule in conf["rules"]:
  138. iptables += "-A FORWARD -s {0} -d {1}".format(ip, rule["address"])
  139. if rule["netmask"] != 32:
  140. iptables += "/{0}".format(rule["netmask"])
  141. if rule["proto"] is not None and rule["proto"] != "any":
  142. iptables += " -p {0}".format(rule["proto"])
  143. if rule["portBegin"] is not None and rule["portBegin"] > 0:
  144. iptables += " --dport {0}".format(rule["portBegin"])
  145. if rule["portEnd"] is not None and rule["portEnd"] > rule["portBegin"]:
  146. iptables += ":{0}".format(rule["portEnd"])
  147. iptables += " -j {0}\n".format(rules_action)
  148. if conf["allowDns"] and self.netvm is not None:
  149. # PREROUTING does DNAT to NetVM DNSes, so we need self.netvm.
  150. # properties
  151. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  152. "ACCEPT\n".format(ip,self.netvm.gateway)
  153. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  154. "ACCEPT\n".format(ip,self.netvm.secondary_dns)
  155. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  156. "ACCEPT\n".format(ip,self.netvm.gateway)
  157. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  158. "ACCEPT\n".format(ip,self.netvm.secondary_dns)
  159. if conf["allowIcmp"]:
  160. iptables += "-A FORWARD -s {0} -p icmp -j ACCEPT\n".format(ip)
  161. if conf["allowYumProxy"]:
  162. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j ACCEPT\n".format(ip, yum_proxy_ip, yum_proxy_port)
  163. else:
  164. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j DROP\n".format(ip, yum_proxy_ip, yum_proxy_port)
  165. iptables += "-A FORWARD -s {0} -j {1}\n".format(ip, default_action)
  166. iptables += "COMMIT\n"
  167. vmm.xs.write('', "/local/domain/"+str(self.get_xid())+"/qubes-iptables-domainrules/"+str(xid), iptables)
  168. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  169. self.write_netvm_domid_entry()
  170. self.rules_applied = None
  171. vmm.xs.write('', "/local/domain/{0}/qubes-iptables".format(self.get_xid()), 'reload')
  172. register_qubes_vm_class(QubesProxyVm)