006QubesProxyVm.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 vmm.offline_mode:
  52. return
  53. if self.netvm is not None:
  54. self.netvm.add_external_ip_permission(self.get_xid())
  55. self.write_netvm_domid_entry()
  56. if old_netvm is not None:
  57. old_netvm.remove_external_ip_permission(self.get_xid())
  58. def post_vm_net_attach(self, vm):
  59. """ Called after some VM net-attached to this ProxyVm """
  60. self.write_iptables_qubesdb_entry()
  61. def post_vm_net_detach(self, vm):
  62. """ Called after some VM net-detached from this ProxyVm """
  63. self.write_iptables_qubesdb_entry()
  64. def start(self, **kwargs):
  65. if dry_run:
  66. return
  67. retcode = super(QubesProxyVm, self).start(**kwargs)
  68. if self.netvm is not None:
  69. self.netvm.add_external_ip_permission(self.get_xid())
  70. self.write_netvm_domid_entry()
  71. return retcode
  72. def force_shutdown(self, **kwargs):
  73. if dry_run:
  74. return
  75. if self.netvm is not None:
  76. self.netvm.remove_external_ip_permission(kwargs['xid'] if 'xid' in kwargs else self.get_xid())
  77. super(QubesProxyVm, self).force_shutdown(**kwargs)
  78. def create_qubesdb_entries(self):
  79. if dry_run:
  80. return
  81. super(QubesProxyVm, self).create_qubesdb_entries()
  82. self.qdb.write("/qubes-iptables-error", '')
  83. self.write_iptables_qubesdb_entry()
  84. def write_netvm_domid_entry(self, xid = -1):
  85. if not self.is_running():
  86. return
  87. if xid < 0:
  88. xid = self.get_xid()
  89. if self.netvm is None:
  90. self.qdb.write("/qubes-netvm-domid", '')
  91. else:
  92. self.qdb.write("/qubes-netvm-domid",
  93. "{0}".format(self.netvm.get_xid()))
  94. def write_iptables_qubesdb_entry(self):
  95. self.qdb.rm("/qubes-iptables-domainrules/")
  96. iptables = "# Generated by Qubes Core on {0}\n".format(datetime.now().ctime())
  97. iptables += "*filter\n"
  98. iptables += ":INPUT DROP [0:0]\n"
  99. iptables += ":FORWARD DROP [0:0]\n"
  100. iptables += ":OUTPUT ACCEPT [0:0]\n"
  101. # Strict INPUT rules
  102. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  103. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  104. "-j ACCEPT\n"
  105. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  106. iptables += "-A INPUT -i lo -j ACCEPT\n"
  107. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  108. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  109. "-j ACCEPT\n"
  110. # Allow dom0 networking
  111. iptables += "-A FORWARD -i vif0.0 -j ACCEPT\n"
  112. # Deny inter-VMs networking
  113. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  114. iptables += "COMMIT\n"
  115. self.qdb.write("/qubes-iptables-header", iptables)
  116. vms = [vm for vm in self.connected_vms.values()]
  117. for vm in vms:
  118. iptables="*filter\n"
  119. conf = vm.get_firewall_conf()
  120. xid = vm.get_xid()
  121. if xid < 0: # VM not active ATM
  122. continue
  123. ip = vm.ip
  124. if ip is None:
  125. continue
  126. # Anti-spoof rules are added by vif-script (vif-route-qubes), here we trust IP address
  127. accept_action = "ACCEPT"
  128. reject_action = "REJECT --reject-with icmp-host-prohibited"
  129. if conf["allow"]:
  130. default_action = accept_action
  131. rules_action = reject_action
  132. else:
  133. default_action = reject_action
  134. rules_action = accept_action
  135. for rule in conf["rules"]:
  136. iptables += "-A FORWARD -s {0} -d {1}".format(ip, rule["address"])
  137. if rule["netmask"] != 32:
  138. iptables += "/{0}".format(rule["netmask"])
  139. if rule["proto"] is not None and rule["proto"] != "any":
  140. iptables += " -p {0}".format(rule["proto"])
  141. if rule["portBegin"] is not None and rule["portBegin"] > 0:
  142. iptables += " --dport {0}".format(rule["portBegin"])
  143. if rule["portEnd"] is not None and rule["portEnd"] > rule["portBegin"]:
  144. iptables += ":{0}".format(rule["portEnd"])
  145. iptables += " -j {0}\n".format(rules_action)
  146. if conf["allowDns"] and self.netvm is not None:
  147. # PREROUTING does DNAT to NetVM DNSes, so we need self.netvm.
  148. # properties
  149. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  150. "ACCEPT\n".format(ip,self.netvm.gateway)
  151. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  152. "ACCEPT\n".format(ip,self.netvm.secondary_dns)
  153. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  154. "ACCEPT\n".format(ip,self.netvm.gateway)
  155. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  156. "ACCEPT\n".format(ip,self.netvm.secondary_dns)
  157. if conf["allowIcmp"]:
  158. iptables += "-A FORWARD -s {0} -p icmp -j ACCEPT\n".format(ip)
  159. if conf["allowYumProxy"]:
  160. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j ACCEPT\n".format(ip, yum_proxy_ip, yum_proxy_port)
  161. else:
  162. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j DROP\n".format(ip, yum_proxy_ip, yum_proxy_port)
  163. iptables += "-A FORWARD -s {0} -j {1}\n".format(ip, default_action)
  164. iptables += "COMMIT\n"
  165. self.qdb.write("/qubes-iptables-domainrules/"+str(xid), iptables)
  166. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  167. self.write_netvm_domid_entry()
  168. self.rules_applied = None
  169. self.qdb.write("/qubes-iptables", 'reload')
  170. register_qubes_vm_class(QubesProxyVm)