006QubesProxyVm.py 7.8 KB

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