firewall.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. # vim: fileencoding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2016
  6. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (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 along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import logging
  23. import os
  24. import socket
  25. import subprocess
  26. from distutils import spawn
  27. import daemon
  28. import qubesdb
  29. import sys
  30. import signal
  31. class RuleParseError(Exception):
  32. pass
  33. class RuleApplyError(Exception):
  34. pass
  35. class FirewallWorker(object):
  36. def __init__(self):
  37. self.terminate_requested = False
  38. self.qdb = qubesdb.QubesDB()
  39. self.log = logging.getLogger('qubes.firewall')
  40. self.log.addHandler(logging.StreamHandler(sys.stderr))
  41. def init(self):
  42. """Create appropriate chains/tables"""
  43. raise NotImplementedError
  44. def sd_notify(self, state):
  45. """Send notification to systemd, if available"""
  46. # based on sdnotify python module
  47. if 'NOTIFY_SOCKET' not in os.environ:
  48. return
  49. addr = os.environ['NOTIFY_SOCKET']
  50. if addr[0] == '@':
  51. addr = '\0' + addr[1:]
  52. try:
  53. sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  54. sock.connect(addr)
  55. sock.sendall(state.encode())
  56. except:
  57. # generally ignore error on systemd notification
  58. pass
  59. def cleanup(self):
  60. """Remove tables/chains - reverse work done by init"""
  61. raise NotImplementedError
  62. def apply_rules(self, source_addr, rules):
  63. """Apply rules in given source address"""
  64. raise NotImplementedError
  65. def update_connected_ips(self, family):
  66. raise NotImplementedError
  67. def get_connected_ips(self, family):
  68. if family == 6:
  69. return self.qdb.read('/connected-ips6').split()
  70. else:
  71. return self.qdb.read('/connected-ips').split()
  72. def run_firewall_dir(self):
  73. """Run scripts dir contents, before user script"""
  74. script_dir_paths = ['/etc/qubes/qubes-firewall.d',
  75. '/rw/config/qubes-firewall.d']
  76. for script_dir_path in script_dir_paths:
  77. if not os.path.isdir(script_dir_path):
  78. continue
  79. for d_script in sorted(os.listdir(script_dir_path)):
  80. d_script_path = os.path.join(script_dir_path, d_script)
  81. if os.path.isfile(d_script_path) and \
  82. os.access(d_script_path, os.X_OK):
  83. subprocess.call([d_script_path])
  84. def run_user_script(self):
  85. """Run user script in /rw/config"""
  86. user_script_path = '/rw/config/qubes-firewall-user-script'
  87. if os.path.isfile(user_script_path) and \
  88. os.access(user_script_path, os.X_OK):
  89. subprocess.call([user_script_path])
  90. def read_rules(self, target):
  91. """Read rules from QubesDB and return them as a list of dicts"""
  92. entries = self.qdb.multiread('/qubes-firewall/{}/'.format(target))
  93. assert isinstance(entries, dict)
  94. # drop full path
  95. entries = dict(((k.split('/')[3], v.decode())
  96. for k, v in entries.items()))
  97. if 'policy' not in entries:
  98. raise RuleParseError('No \'policy\' defined')
  99. policy = entries.pop('policy')
  100. rules = []
  101. for ruleno, rule in sorted(entries.items()):
  102. if len(ruleno) != 4 or not ruleno.isdigit():
  103. raise RuleParseError(
  104. 'Unexpected non-rule found: {}={}'.format(ruleno, rule))
  105. rule_dict = dict(elem.split('=') for elem in rule.split(' '))
  106. if 'action' not in rule_dict:
  107. raise RuleParseError('Rule \'{}\' lack action'.format(rule))
  108. rules.append(rule_dict)
  109. rules.append({'action': policy})
  110. return rules
  111. def list_targets(self):
  112. return set(t.split('/')[2] for t in self.qdb.list('/qubes-firewall/'))
  113. @staticmethod
  114. def is_ip6(addr):
  115. return addr.count(':') > 0
  116. def log_error(self, msg):
  117. self.log.error(msg)
  118. subprocess.call(
  119. ['notify-send', '-t', '3000', msg],
  120. env=os.environ.copy().update({'DISPLAY': ':0'})
  121. )
  122. def handle_addr(self, addr):
  123. try:
  124. rules = self.read_rules(addr)
  125. self.apply_rules(addr, rules)
  126. except RuleParseError as e:
  127. self.log_error(
  128. 'Failed to parse rules for {} ({}), blocking traffic'.format(
  129. addr, str(e)
  130. ))
  131. self.apply_rules(addr, [{'action': 'drop'}])
  132. except RuleApplyError as e:
  133. self.log_error(
  134. 'Failed to apply rules for {} ({}), blocking traffic'.format(
  135. addr, str(e))
  136. )
  137. # retry with fallback rules
  138. try:
  139. self.apply_rules(addr, [{'action': 'drop'}])
  140. except RuleApplyError:
  141. self.log_error(
  142. 'Failed to block traffic for {}'.format(addr))
  143. @staticmethod
  144. def dns_addresses(family=None):
  145. with open('/etc/resolv.conf') as resolv:
  146. for line in resolv.readlines():
  147. line = line.strip()
  148. if line.startswith('nameserver'):
  149. if line.count('.') == 3 and (family or 4) == 4:
  150. yield line.split(' ')[1]
  151. elif line.count(':') and (family or 6) == 6:
  152. yield line.split(' ')[1]
  153. def main(self):
  154. self.terminate_requested = False
  155. self.init()
  156. self.run_firewall_dir()
  157. self.run_user_script()
  158. self.sd_notify('READY=1')
  159. # initial load
  160. for source_addr in self.list_targets():
  161. self.handle_addr(source_addr)
  162. self.update_connected_ips(4)
  163. self.update_connected_ips(6)
  164. self.qdb.watch('/qubes-firewall/')
  165. self.qdb.watch('/connected-ips')
  166. self.qdb.watch('/connected-ips6')
  167. try:
  168. for watch_path in iter(self.qdb.read_watch, None):
  169. if watch_path == '/connected-ips':
  170. self.update_connected_ips(4)
  171. if watch_path == '/connected-ips6':
  172. self.update_connected_ips(6)
  173. # ignore writing rules itself - wait for final write at
  174. # source_addr level empty write (/qubes-firewall/SOURCE_ADDR)
  175. if watch_path.startswith('/qubes-firewall/') and watch_path.count('/') == 2:
  176. source_addr = watch_path.split('/')[2]
  177. self.handle_addr(source_addr)
  178. except OSError: # EINTR
  179. # signal received, don't continue the loop
  180. pass
  181. self.cleanup()
  182. def terminate(self):
  183. self.terminate_requested = True
  184. class IptablesWorker(FirewallWorker):
  185. supported_rule_opts = ['action', 'proto', 'dst4', 'dst6', 'dsthost',
  186. 'dstports', 'specialtarget', 'icmptype']
  187. def __init__(self):
  188. super(IptablesWorker, self).__init__()
  189. self.chains = {
  190. 4: set(),
  191. 6: set(),
  192. }
  193. @staticmethod
  194. def chain_for_addr(addr):
  195. """Generate iptables chain name for given source address address"""
  196. return 'qbs-' + addr.replace('.', '-').replace(':', '-')[-20:]
  197. def run_ipt(self, family, args, **kwargs):
  198. # pylint: disable=no-self-use
  199. if family == 6:
  200. subprocess.check_call(['ip6tables'] + args, **kwargs)
  201. else:
  202. subprocess.check_call(['iptables'] + args, **kwargs)
  203. def run_ipt_restore(self, family, args):
  204. # pylint: disable=no-self-use
  205. if family == 6:
  206. return subprocess.Popen(['ip6tables-restore'] + args,
  207. stdin=subprocess.PIPE,
  208. stdout=subprocess.PIPE,
  209. stderr=subprocess.STDOUT)
  210. else:
  211. return subprocess.Popen(['iptables-restore'] + args,
  212. stdin=subprocess.PIPE,
  213. stdout=subprocess.PIPE,
  214. stderr=subprocess.STDOUT)
  215. def create_chain(self, addr, chain, family):
  216. """
  217. Create iptables chain and hook traffic coming from `addr` to it.
  218. :param addr: source IP from which traffic should be handled by the
  219. chain
  220. :param chain: name of the chain to create
  221. :param family: address family (4 or 6)
  222. :return: None
  223. """
  224. self.run_ipt(family, ['-N', chain])
  225. self.run_ipt(family,
  226. ['-I', 'QBS-FORWARD', '-s', addr, '-j', chain])
  227. self.chains[family].add(chain)
  228. def prepare_rules(self, chain, rules, family):
  229. """
  230. Helper function to translate rules list into input for iptables-restore
  231. :param chain: name of the chain to put rules into
  232. :param rules: list of rules
  233. :param family: address family (4 or 6)
  234. :return: input for iptables-restore
  235. :rtype: str
  236. """
  237. iptables = "*filter\n"
  238. fullmask = '/128' if family == 6 else '/32'
  239. dns = list(addr + fullmask for addr in self.dns_addresses(family))
  240. for rule in rules:
  241. unsupported_opts = set(rule.keys()).difference(
  242. set(self.supported_rule_opts))
  243. if unsupported_opts:
  244. raise RuleParseError(
  245. 'Unsupported rule option(s): {!s}'.format(unsupported_opts))
  246. if 'dst4' in rule and family == 6:
  247. raise RuleParseError('IPv4 rule found for IPv6 address')
  248. if 'dst6' in rule and family == 4:
  249. raise RuleParseError('dst6 rule found for IPv4 address')
  250. if 'proto' in rule:
  251. if rule['proto'] == 'icmp' and family == 6:
  252. protos = ['icmpv6']
  253. else:
  254. protos = [rule['proto']]
  255. else:
  256. protos = None
  257. if 'dst4' in rule:
  258. dsthosts = [rule['dst4']]
  259. elif 'dst6' in rule:
  260. dsthosts = [rule['dst6']]
  261. elif 'dsthost' in rule:
  262. try:
  263. addrinfo = socket.getaddrinfo(rule['dsthost'], None,
  264. (socket.AF_INET6 if family == 6 else socket.AF_INET))
  265. except socket.gaierror as e:
  266. raise RuleParseError('Failed to resolve {}: {}'.format(
  267. rule['dsthost'], str(e)))
  268. dsthosts = set(item[4][0] + fullmask for item in addrinfo)
  269. else:
  270. dsthosts = None
  271. if 'dstports' in rule:
  272. dstports = rule['dstports'].replace('-', ':')
  273. else:
  274. dstports = None
  275. if rule.get('specialtarget', None) == 'dns':
  276. if dstports not in ('53:53', None):
  277. continue
  278. else:
  279. dstports = '53:53'
  280. if not dns:
  281. continue
  282. if protos is not None:
  283. protos = {'tcp', 'udp'}.intersection(protos)
  284. else:
  285. protos = {'tcp', 'udp'}
  286. if dsthosts is not None:
  287. dsthosts = set(dns).intersection(dsthosts)
  288. else:
  289. dsthosts = dns
  290. if 'icmptype' in rule:
  291. icmptype = rule['icmptype']
  292. else:
  293. icmptype = None
  294. # make them iterable
  295. if protos is None:
  296. protos = [None]
  297. if dsthosts is None:
  298. dsthosts = [None]
  299. if rule['action'] == 'accept':
  300. action = 'ACCEPT'
  301. elif rule['action'] == 'drop':
  302. action = 'REJECT --reject-with {}'.format(
  303. 'icmp6-adm-prohibited' if family == 6 else
  304. 'icmp-admin-prohibited')
  305. else:
  306. raise RuleParseError(
  307. 'Invalid rule action {}'.format(rule['action']))
  308. # sorting here is only to ease writing tests
  309. for proto in sorted(protos):
  310. for dsthost in sorted(dsthosts):
  311. ipt_rule = '-A {}'.format(chain)
  312. if dsthost is not None:
  313. ipt_rule += ' -d {}'.format(dsthost)
  314. if proto is not None:
  315. ipt_rule += ' -p {}'.format(proto)
  316. if dstports is not None:
  317. ipt_rule += ' --dport {}'.format(dstports)
  318. if icmptype is not None:
  319. ipt_rule += ' --icmp-type {}'.format(icmptype)
  320. ipt_rule += ' -j {}\n'.format(action)
  321. iptables += ipt_rule
  322. iptables += 'COMMIT\n'
  323. return iptables
  324. def apply_rules_family(self, source, rules, family):
  325. """
  326. Apply rules for given source address.
  327. Handle only rules for given address family (IPv4 or IPv6).
  328. :param source: source address
  329. :param rules: rules list
  330. :param family: address family, either 4 or 6
  331. :return: None
  332. """
  333. chain = self.chain_for_addr(source)
  334. if chain not in self.chains[family]:
  335. self.create_chain(source, chain, family)
  336. iptables = self.prepare_rules(chain, rules, family)
  337. try:
  338. self.run_ipt(family, ['-F', chain])
  339. p = self.run_ipt_restore(family, ['-n'])
  340. (output, _) = p.communicate(iptables.encode())
  341. if p.returncode != 0:
  342. raise RuleApplyError(
  343. 'iptables-restore failed: {}'.format(output))
  344. except subprocess.CalledProcessError as e:
  345. raise RuleApplyError('\'iptables -F {}\' failed: {}'.format(
  346. chain, e.output))
  347. def apply_rules(self, source, rules):
  348. if self.is_ip6(source):
  349. self.apply_rules_family(source, rules, 6)
  350. else:
  351. self.apply_rules_family(source, rules, 4)
  352. def update_connected_ips(self, family):
  353. self.run_ipt(family, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
  354. self.run_ipt(family, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
  355. for ip in self.get_connected_ips(family):
  356. self.run_ipt(family, [
  357. '-t', 'raw', '-A', 'QBS-PREROUTING',
  358. '!', '-i', 'vif+', '-s', ip, '-j', 'DROP'])
  359. self.run_ipt(family, [
  360. '-t', 'mangle', '-A', 'QBS-POSTROUTING',
  361. '!', '-o', 'vif+', '-d', ip, '-j', 'DROP'])
  362. def init(self):
  363. # Chains QBS-FORWARD, QBS-PREROUTING, QBS-POSTROUTING
  364. # need to be created before running this.
  365. try:
  366. self.run_ipt(4, ['-F', 'QBS-FORWARD'])
  367. self.run_ipt(4,
  368. ['-A', 'QBS-FORWARD', '!', '-i', 'vif+', '-j', 'RETURN'])
  369. self.run_ipt(4, ['-A', 'QBS-FORWARD', '-j', 'DROP'])
  370. self.run_ipt(4, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
  371. self.run_ipt(4, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
  372. self.run_ipt(6, ['-F', 'QBS-FORWARD'])
  373. self.run_ipt(6,
  374. ['-A', 'QBS-FORWARD', '!', '-i', 'vif+', '-j', 'RETURN'])
  375. self.run_ipt(6, ['-A', 'QBS-FORWARD', '-j', 'DROP'])
  376. self.run_ipt(6, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
  377. self.run_ipt(6, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
  378. except subprocess.CalledProcessError:
  379. self.log_error(
  380. 'Error initializing iptables. '
  381. 'You probably need to create QBS-FORWARD, QBS-PREROUTING and '
  382. 'QBS-POSTROUTING chains first.'
  383. )
  384. sys.exit(1)
  385. def cleanup(self):
  386. for family in (4, 6):
  387. self.run_ipt(family, ['-F', 'QBS-FORWARD'])
  388. self.run_ipt(family, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
  389. self.run_ipt(family, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
  390. for chain in self.chains[family]:
  391. self.run_ipt(family, ['-F', chain])
  392. self.run_ipt(family, ['-X', chain])
  393. class NftablesWorker(FirewallWorker):
  394. supported_rule_opts = ['action', 'proto', 'dst4', 'dst6', 'dsthost',
  395. 'dstports', 'specialtarget', 'icmptype']
  396. def __init__(self):
  397. super(NftablesWorker, self).__init__()
  398. self.chains = {
  399. 4: set(),
  400. 6: set(),
  401. }
  402. @staticmethod
  403. def chain_for_addr(addr):
  404. """Generate iptables chain name for given source address address"""
  405. return 'qbs-' + addr.replace('.', '-').replace(':', '-')
  406. def run_nft(self, nft_input):
  407. # pylint: disable=no-self-use
  408. p = subprocess.Popen(['nft', '-f', '/dev/stdin'],
  409. stdin=subprocess.PIPE,
  410. stdout=subprocess.PIPE,
  411. stderr=subprocess.STDOUT)
  412. stdout, _ = p.communicate(nft_input.encode())
  413. if p.returncode != 0:
  414. raise RuleApplyError('nft failed: {}'.format(stdout))
  415. def create_chain(self, addr, chain, family):
  416. """
  417. Create iptables chain and hook traffic coming from `addr` to it.
  418. :param addr: source IP from which traffic should be handled by the
  419. chain
  420. :param chain: name of the chain to create
  421. :param family: address family (4 or 6)
  422. :return: None
  423. """
  424. nft_input = (
  425. 'table {family} {table} {{\n'
  426. ' chain {chain} {{\n'
  427. ' }}\n'
  428. ' chain forward {{\n'
  429. ' {family} saddr {ip} jump {chain}\n'
  430. ' }}\n'
  431. '}}\n'.format(
  432. family=("ip6" if family == 6 else "ip"),
  433. table='qubes-firewall',
  434. chain=chain,
  435. ip=addr,
  436. )
  437. )
  438. self.run_nft(nft_input)
  439. self.chains[family].add(chain)
  440. def update_connected_ips(self, family):
  441. addr = '{' + ', '.join(self.get_connected_ips(family)) + '}'
  442. nft_input = (
  443. 'flush chain {family} {table} prerouting\n'
  444. 'flush chain {family} {table} postrouting\n'
  445. 'table {family} {table} {{\n'
  446. ' chain prerouting {{\n'
  447. ' iifname != "vif*" {family} saddr {addr} drop\n'
  448. ' }}\n'
  449. ' chain postrouting {{\n'
  450. ' oifname != "vif*" {family} daddr {addr} drop\n'
  451. ' }}\n'
  452. '}}\n'
  453. ).format(
  454. family=('ip6' if family == 6 else 'ip'),
  455. table='qubes-firewall',
  456. addr=addr,
  457. )
  458. self.run_nft(nft_input)
  459. def prepare_rules(self, chain, rules, family):
  460. """
  461. Helper function to translate rules list into input for iptables-restore
  462. :param chain: name of the chain to put rules into
  463. :param rules: list of rules
  464. :param family: address family (4 or 6)
  465. :return: input for iptables-restore
  466. :rtype: str
  467. """
  468. assert family in (4, 6)
  469. nft_rules = []
  470. ip_match = 'ip6' if family == 6 else 'ip'
  471. fullmask = '/128' if family == 6 else '/32'
  472. dns = list(addr + fullmask for addr in self.dns_addresses(family))
  473. for rule in rules:
  474. unsupported_opts = set(rule.keys()).difference(
  475. set(self.supported_rule_opts))
  476. if unsupported_opts:
  477. raise RuleParseError(
  478. 'Unsupported rule option(s): {!s}'.format(unsupported_opts))
  479. if 'dst4' in rule and family == 6:
  480. raise RuleParseError('IPv4 rule found for IPv6 address')
  481. if 'dst6' in rule and family == 4:
  482. raise RuleParseError('dst6 rule found for IPv4 address')
  483. nft_rule = ""
  484. if rule['action'] == 'accept':
  485. action = 'accept'
  486. elif rule['action'] == 'drop':
  487. action = 'reject with icmp{} type admin-prohibited'.format(
  488. 'v6' if family == 6 else '')
  489. else:
  490. raise RuleParseError(
  491. 'Invalid rule action {}'.format(rule['action']))
  492. if 'proto' in rule:
  493. if family == 4:
  494. nft_rule += ' ip protocol {}'.format(rule['proto'])
  495. elif family == 6:
  496. proto = 'icmpv6' if rule['proto'] == 'icmp' \
  497. else rule['proto']
  498. nft_rule += ' ip6 nexthdr {}'.format(proto)
  499. if 'dst4' in rule:
  500. nft_rule += ' ip daddr {}'.format(rule['dst4'])
  501. elif 'dst6' in rule:
  502. nft_rule += ' ip6 daddr {}'.format(rule['dst6'])
  503. elif 'dsthost' in rule:
  504. try:
  505. addrinfo = socket.getaddrinfo(rule['dsthost'], None,
  506. (socket.AF_INET6 if family == 6 else socket.AF_INET))
  507. except socket.gaierror as e:
  508. raise RuleParseError('Failed to resolve {}: {}'.format(
  509. rule['dsthost'], str(e)))
  510. nft_rule += ' {} daddr {{ {} }}'.format(ip_match,
  511. ', '.join(set(item[4][0] + fullmask for item in addrinfo)))
  512. if 'dstports' in rule:
  513. dstports = rule['dstports']
  514. if len(set(dstports.split('-'))) == 1:
  515. dstports = dstports.split('-')[0]
  516. else:
  517. dstports = None
  518. if rule.get('specialtarget', None) == 'dns':
  519. if dstports not in ('53', None):
  520. continue
  521. else:
  522. dstports = '53'
  523. if not dns:
  524. continue
  525. nft_rule += ' {} daddr {{ {} }}'.format(ip_match, ', '.join(
  526. dns))
  527. if 'icmptype' in rule:
  528. if family == 4:
  529. nft_rule += ' icmp type {}'.format(rule['icmptype'])
  530. elif family == 6:
  531. nft_rule += ' icmpv6 type {}'.format(rule['icmptype'])
  532. # now duplicate rules for tcp/udp if needed
  533. # it isn't possible to specify "tcp dport xx || udp dport xx" in
  534. # one rule
  535. if dstports is not None:
  536. if 'proto' not in rule:
  537. nft_rules.append(
  538. nft_rule + ' tcp dport {} {}'.format(
  539. dstports, action))
  540. nft_rules.append(
  541. nft_rule + ' udp dport {} {}'.format(
  542. dstports, action))
  543. else:
  544. nft_rules.append(
  545. nft_rule + ' {} dport {} {}'.format(
  546. rule['proto'], dstports, action))
  547. else:
  548. nft_rules.append(nft_rule + ' ' + action)
  549. return (
  550. 'flush chain {family} {table} {chain}\n'
  551. 'table {family} {table} {{\n'
  552. ' chain {chain} {{\n'
  553. ' {rules}\n'
  554. ' }}\n'
  555. '}}\n'.format(
  556. family=('ip6' if family == 6 else 'ip'),
  557. table='qubes-firewall',
  558. chain=chain,
  559. rules='\n '.join(nft_rules)
  560. ))
  561. def apply_rules_family(self, source, rules, family):
  562. """
  563. Apply rules for given source address.
  564. Handle only rules for given address family (IPv4 or IPv6).
  565. :param source: source address
  566. :param rules: rules list
  567. :param family: address family, either 4 or 6
  568. :return: None
  569. """
  570. chain = self.chain_for_addr(source)
  571. if chain not in self.chains[family]:
  572. self.create_chain(source, chain, family)
  573. self.run_nft(self.prepare_rules(chain, rules, family))
  574. def apply_rules(self, source, rules):
  575. if self.is_ip6(source):
  576. self.apply_rules_family(source, rules, 6)
  577. else:
  578. self.apply_rules_family(source, rules, 4)
  579. def init(self):
  580. nft_init = (
  581. 'table {family} qubes-firewall {{\n'
  582. ' chain forward {{\n'
  583. ' type filter hook forward priority 0;\n'
  584. ' policy drop;\n'
  585. ' ct state established,related accept\n'
  586. ' meta iifname != "vif*" accept\n'
  587. ' }}\n'
  588. ' chain prerouting {{\n'
  589. ' type filter hook prerouting priority -300;\n'
  590. ' policy accept;\n'
  591. ' }}\n'
  592. ' chain postrouting {{\n'
  593. ' type filter hook postrouting priority -300;\n'
  594. ' policy accept;\n'
  595. ' }}\n'
  596. '}}\n'
  597. )
  598. nft_init = ''.join(
  599. nft_init.format(family=family) for family in ('ip', 'ip6'))
  600. self.run_nft(nft_init)
  601. def cleanup(self):
  602. nft_cleanup = (
  603. 'delete table ip qubes-firewall\n'
  604. 'delete table ip6 qubes-firewall\n'
  605. )
  606. self.run_nft(nft_cleanup)
  607. def main():
  608. if spawn.find_executable('nft'):
  609. worker = NftablesWorker()
  610. else:
  611. worker = IptablesWorker()
  612. context = daemon.DaemonContext()
  613. context.stderr = sys.stderr
  614. context.detach_process = False
  615. context.files_preserve = [worker.qdb.watch_fd()]
  616. context.signal_map = {
  617. signal.SIGTERM: lambda _signal, _stack: worker.terminate(),
  618. }
  619. with context:
  620. worker.main()
  621. if __name__ == '__main__':
  622. main()