006QubesProxyVm.py 8.2 KB

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