2016-09-12 05:22:53 +02:00
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016
|
|
|
|
# Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
from distutils import spawn
|
|
|
|
|
|
|
|
import daemon
|
|
|
|
|
|
|
|
import qubesdb
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import signal
|
|
|
|
|
|
|
|
|
|
|
|
class RuleParseError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RuleApplyError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FirewallWorker(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.terminate_requested = False
|
|
|
|
self.qdb = qubesdb.QubesDB()
|
|
|
|
self.log = logging.getLogger('qubes.firewall')
|
|
|
|
self.log.addHandler(logging.StreamHandler(sys.stderr))
|
|
|
|
|
|
|
|
def init(self):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Create appropriate chains/tables"""
|
2016-09-12 05:22:53 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-04-20 16:38:25 +02:00
|
|
|
def sd_notify(self, state):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Send notification to systemd, if available"""
|
2018-04-20 16:38:25 +02:00
|
|
|
# based on sdnotify python module
|
2019-09-18 00:10:51 +02:00
|
|
|
if 'NOTIFY_SOCKET' not in os.environ:
|
2018-04-20 16:38:25 +02:00
|
|
|
return
|
|
|
|
addr = os.environ['NOTIFY_SOCKET']
|
|
|
|
if addr[0] == '@':
|
|
|
|
addr = '\0' + addr[1:]
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
|
|
|
sock.connect(addr)
|
|
|
|
sock.sendall(state.encode())
|
|
|
|
except:
|
|
|
|
# generally ignore error on systemd notification
|
|
|
|
pass
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
def cleanup(self):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Remove tables/chains - reverse work done by init"""
|
2016-09-12 05:22:53 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def apply_rules(self, source_addr, rules):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Apply rules in given source address"""
|
2016-09-12 05:22:53 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-01-09 18:20:05 +01:00
|
|
|
def update_connected_ips(self, family):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_connected_ips(self, family):
|
2020-01-14 10:14:00 +01:00
|
|
|
ips = self.qdb.read('/connected-ips6' if family == 6 else '/connected-ips')
|
2020-01-14 10:22:38 +01:00
|
|
|
if ips is None:
|
|
|
|
return []
|
2020-01-14 10:14:00 +01:00
|
|
|
return ips.decode().split()
|
2020-01-09 18:20:05 +01:00
|
|
|
|
2018-02-13 23:38:14 +01:00
|
|
|
def run_firewall_dir(self):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Run scripts dir contents, before user script"""
|
2018-02-14 05:39:28 +01:00
|
|
|
script_dir_paths = ['/etc/qubes/qubes-firewall.d',
|
2019-09-18 00:10:51 +02:00
|
|
|
'/rw/config/qubes-firewall.d']
|
2018-02-14 05:39:28 +01:00
|
|
|
for script_dir_path in script_dir_paths:
|
2019-09-18 00:10:51 +02:00
|
|
|
if not os.path.isdir(script_dir_path):
|
|
|
|
continue
|
|
|
|
for d_script in sorted(os.listdir(script_dir_path)):
|
|
|
|
d_script_path = os.path.join(script_dir_path, d_script)
|
|
|
|
if os.path.isfile(d_script_path) and \
|
|
|
|
os.access(d_script_path, os.X_OK):
|
|
|
|
subprocess.call([d_script_path])
|
2018-02-13 23:38:14 +01:00
|
|
|
|
2018-02-05 16:27:52 +01:00
|
|
|
def run_user_script(self):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Run user script in /rw/config"""
|
2018-02-05 16:27:52 +01:00
|
|
|
user_script_path = '/rw/config/qubes-firewall-user-script'
|
|
|
|
if os.path.isfile(user_script_path) and \
|
|
|
|
os.access(user_script_path, os.X_OK):
|
|
|
|
subprocess.call([user_script_path])
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
def read_rules(self, target):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Read rules from QubesDB and return them as a list of dicts"""
|
2016-09-12 05:22:53 +02:00
|
|
|
entries = self.qdb.multiread('/qubes-firewall/{}/'.format(target))
|
|
|
|
assert isinstance(entries, dict)
|
|
|
|
# drop full path
|
2019-09-18 01:42:51 +02:00
|
|
|
entries = dict(((k.split('/')[3], v.decode())
|
|
|
|
for k, v in entries.items()))
|
2016-09-12 05:22:53 +02:00
|
|
|
if 'policy' not in entries:
|
|
|
|
raise RuleParseError('No \'policy\' defined')
|
|
|
|
policy = entries.pop('policy')
|
|
|
|
rules = []
|
|
|
|
for ruleno, rule in sorted(entries.items()):
|
|
|
|
if len(ruleno) != 4 or not ruleno.isdigit():
|
|
|
|
raise RuleParseError(
|
|
|
|
'Unexpected non-rule found: {}={}'.format(ruleno, rule))
|
|
|
|
rule_dict = dict(elem.split('=') for elem in rule.split(' '))
|
|
|
|
if 'action' not in rule_dict:
|
|
|
|
raise RuleParseError('Rule \'{}\' lack action'.format(rule))
|
|
|
|
rules.append(rule_dict)
|
|
|
|
rules.append({'action': policy})
|
|
|
|
return rules
|
|
|
|
|
|
|
|
def list_targets(self):
|
|
|
|
return set(t.split('/')[2] for t in self.qdb.list('/qubes-firewall/'))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_ip6(addr):
|
|
|
|
return addr.count(':') > 0
|
|
|
|
|
|
|
|
def log_error(self, msg):
|
|
|
|
self.log.error(msg)
|
|
|
|
subprocess.call(
|
|
|
|
['notify-send', '-t', '3000', msg],
|
|
|
|
env=os.environ.copy().update({'DISPLAY': ':0'})
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle_addr(self, addr):
|
|
|
|
try:
|
|
|
|
rules = self.read_rules(addr)
|
|
|
|
self.apply_rules(addr, rules)
|
|
|
|
except RuleParseError as e:
|
|
|
|
self.log_error(
|
|
|
|
'Failed to parse rules for {} ({}), blocking traffic'.format(
|
|
|
|
addr, str(e)
|
|
|
|
))
|
|
|
|
self.apply_rules(addr, [{'action': 'drop'}])
|
|
|
|
except RuleApplyError as e:
|
|
|
|
self.log_error(
|
|
|
|
'Failed to apply rules for {} ({}), blocking traffic'.format(
|
|
|
|
addr, str(e))
|
|
|
|
)
|
|
|
|
# retry with fallback rules
|
|
|
|
try:
|
|
|
|
self.apply_rules(addr, [{'action': 'drop'}])
|
|
|
|
except RuleApplyError:
|
|
|
|
self.log_error(
|
|
|
|
'Failed to block traffic for {}'.format(addr))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dns_addresses(family=None):
|
|
|
|
with open('/etc/resolv.conf') as resolv:
|
|
|
|
for line in resolv.readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('nameserver'):
|
|
|
|
if line.count('.') == 3 and (family or 4) == 4:
|
|
|
|
yield line.split(' ')[1]
|
|
|
|
elif line.count(':') and (family or 6) == 6:
|
|
|
|
yield line.split(' ')[1]
|
|
|
|
|
|
|
|
def main(self):
|
|
|
|
self.terminate_requested = False
|
|
|
|
self.init()
|
2018-02-13 23:38:14 +01:00
|
|
|
self.run_firewall_dir()
|
2018-02-05 16:27:52 +01:00
|
|
|
self.run_user_script()
|
2018-04-20 16:38:25 +02:00
|
|
|
self.sd_notify('READY=1')
|
2016-09-12 05:22:53 +02:00
|
|
|
# initial load
|
|
|
|
for source_addr in self.list_targets():
|
|
|
|
self.handle_addr(source_addr)
|
2020-01-09 18:20:05 +01:00
|
|
|
self.update_connected_ips(4)
|
|
|
|
self.update_connected_ips(6)
|
2016-09-12 05:22:53 +02:00
|
|
|
self.qdb.watch('/qubes-firewall/')
|
2020-01-09 18:20:05 +01:00
|
|
|
self.qdb.watch('/connected-ips')
|
|
|
|
self.qdb.watch('/connected-ips6')
|
2016-09-12 05:22:53 +02:00
|
|
|
try:
|
|
|
|
for watch_path in iter(self.qdb.read_watch, None):
|
2020-01-09 18:20:05 +01:00
|
|
|
if watch_path == '/connected-ips':
|
|
|
|
self.update_connected_ips(4)
|
|
|
|
|
|
|
|
if watch_path == '/connected-ips6':
|
|
|
|
self.update_connected_ips(6)
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
# ignore writing rules itself - wait for final write at
|
|
|
|
# source_addr level empty write (/qubes-firewall/SOURCE_ADDR)
|
2020-01-09 18:20:05 +01:00
|
|
|
if watch_path.startswith('/qubes-firewall/') and watch_path.count('/') == 2:
|
|
|
|
source_addr = watch_path.split('/')[2]
|
|
|
|
self.handle_addr(source_addr)
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
except OSError: # EINTR
|
|
|
|
# signal received, don't continue the loop
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.cleanup()
|
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
self.terminate_requested = True
|
|
|
|
|
|
|
|
|
|
|
|
class IptablesWorker(FirewallWorker):
|
|
|
|
supported_rule_opts = ['action', 'proto', 'dst4', 'dst6', 'dsthost',
|
2019-09-18 00:10:51 +02:00
|
|
|
'dstports', 'specialtarget', 'icmptype']
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(IptablesWorker, self).__init__()
|
|
|
|
self.chains = {
|
|
|
|
4: set(),
|
|
|
|
6: set(),
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def chain_for_addr(addr):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Generate iptables chain name for given source address address"""
|
2017-12-05 17:56:46 +01:00
|
|
|
return 'qbs-' + addr.replace('.', '-').replace(':', '-')[-20:]
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
def run_ipt(self, family, args, **kwargs):
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
if family == 6:
|
|
|
|
subprocess.check_call(['ip6tables'] + args, **kwargs)
|
|
|
|
else:
|
|
|
|
subprocess.check_call(['iptables'] + args, **kwargs)
|
|
|
|
|
|
|
|
def run_ipt_restore(self, family, args):
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
if family == 6:
|
|
|
|
return subprocess.Popen(['ip6tables-restore'] + args,
|
2019-09-18 00:10:51 +02:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
2016-09-12 05:22:53 +02:00
|
|
|
else:
|
|
|
|
return subprocess.Popen(['iptables-restore'] + args,
|
2019-09-18 00:10:51 +02:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
def create_chain(self, addr, chain, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Create iptables chain and hook traffic coming from `addr` to it.
|
|
|
|
|
|
|
|
:param addr: source IP from which traffic should be handled by the
|
|
|
|
chain
|
|
|
|
:param chain: name of the chain to create
|
|
|
|
:param family: address family (4 or 6)
|
|
|
|
:return: None
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
self.run_ipt(family, ['-N', chain])
|
|
|
|
self.run_ipt(family,
|
2017-11-18 01:45:19 +01:00
|
|
|
['-I', 'QBS-FORWARD', '-s', addr, '-j', chain])
|
2016-09-12 05:22:53 +02:00
|
|
|
self.chains[family].add(chain)
|
|
|
|
|
|
|
|
def prepare_rules(self, chain, rules, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Helper function to translate rules list into input for iptables-restore
|
|
|
|
|
|
|
|
:param chain: name of the chain to put rules into
|
|
|
|
:param rules: list of rules
|
|
|
|
:param family: address family (4 or 6)
|
|
|
|
:return: input for iptables-restore
|
|
|
|
:rtype: str
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
iptables = "*filter\n"
|
|
|
|
|
|
|
|
fullmask = '/128' if family == 6 else '/32'
|
|
|
|
|
|
|
|
dns = list(addr + fullmask for addr in self.dns_addresses(family))
|
|
|
|
|
|
|
|
for rule in rules:
|
|
|
|
unsupported_opts = set(rule.keys()).difference(
|
|
|
|
set(self.supported_rule_opts))
|
|
|
|
if unsupported_opts:
|
|
|
|
raise RuleParseError(
|
|
|
|
'Unsupported rule option(s): {!s}'.format(unsupported_opts))
|
|
|
|
if 'dst4' in rule and family == 6:
|
|
|
|
raise RuleParseError('IPv4 rule found for IPv6 address')
|
|
|
|
if 'dst6' in rule and family == 4:
|
|
|
|
raise RuleParseError('dst6 rule found for IPv4 address')
|
|
|
|
|
|
|
|
if 'proto' in rule:
|
2017-12-05 17:56:46 +01:00
|
|
|
if rule['proto'] == 'icmp' and family == 6:
|
|
|
|
protos = ['icmpv6']
|
|
|
|
else:
|
|
|
|
protos = [rule['proto']]
|
2016-09-12 05:22:53 +02:00
|
|
|
else:
|
|
|
|
protos = None
|
|
|
|
|
|
|
|
if 'dst4' in rule:
|
|
|
|
dsthosts = [rule['dst4']]
|
|
|
|
elif 'dst6' in rule:
|
|
|
|
dsthosts = [rule['dst6']]
|
|
|
|
elif 'dsthost' in rule:
|
2017-12-28 05:15:00 +01:00
|
|
|
try:
|
|
|
|
addrinfo = socket.getaddrinfo(rule['dsthost'], None,
|
|
|
|
(socket.AF_INET6 if family == 6 else socket.AF_INET))
|
|
|
|
except socket.gaierror as e:
|
|
|
|
raise RuleParseError('Failed to resolve {}: {}'.format(
|
|
|
|
rule['dsthost'], str(e)))
|
2016-09-12 05:22:53 +02:00
|
|
|
dsthosts = set(item[4][0] + fullmask for item in addrinfo)
|
|
|
|
else:
|
|
|
|
dsthosts = None
|
|
|
|
|
|
|
|
if 'dstports' in rule:
|
|
|
|
dstports = rule['dstports'].replace('-', ':')
|
|
|
|
else:
|
|
|
|
dstports = None
|
|
|
|
|
|
|
|
if rule.get('specialtarget', None) == 'dns':
|
|
|
|
if dstports not in ('53:53', None):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
dstports = '53:53'
|
|
|
|
if not dns:
|
|
|
|
continue
|
|
|
|
if protos is not None:
|
|
|
|
protos = {'tcp', 'udp'}.intersection(protos)
|
|
|
|
else:
|
|
|
|
protos = {'tcp', 'udp'}
|
|
|
|
|
|
|
|
if dsthosts is not None:
|
|
|
|
dsthosts = set(dns).intersection(dsthosts)
|
|
|
|
else:
|
|
|
|
dsthosts = dns
|
|
|
|
|
|
|
|
if 'icmptype' in rule:
|
|
|
|
icmptype = rule['icmptype']
|
|
|
|
else:
|
|
|
|
icmptype = None
|
|
|
|
|
|
|
|
# make them iterable
|
|
|
|
if protos is None:
|
|
|
|
protos = [None]
|
|
|
|
if dsthosts is None:
|
|
|
|
dsthosts = [None]
|
|
|
|
|
2018-05-02 04:49:23 +02:00
|
|
|
if rule['action'] == 'accept':
|
|
|
|
action = 'ACCEPT'
|
|
|
|
elif rule['action'] == 'drop':
|
|
|
|
action = 'REJECT --reject-with {}'.format(
|
|
|
|
'icmp6-adm-prohibited' if family == 6 else
|
|
|
|
'icmp-admin-prohibited')
|
|
|
|
else:
|
|
|
|
raise RuleParseError(
|
|
|
|
'Invalid rule action {}'.format(rule['action']))
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
# sorting here is only to ease writing tests
|
|
|
|
for proto in sorted(protos):
|
|
|
|
for dsthost in sorted(dsthosts):
|
|
|
|
ipt_rule = '-A {}'.format(chain)
|
|
|
|
if dsthost is not None:
|
|
|
|
ipt_rule += ' -d {}'.format(dsthost)
|
|
|
|
if proto is not None:
|
|
|
|
ipt_rule += ' -p {}'.format(proto)
|
|
|
|
if dstports is not None:
|
|
|
|
ipt_rule += ' --dport {}'.format(dstports)
|
|
|
|
if icmptype is not None:
|
|
|
|
ipt_rule += ' --icmp-type {}'.format(icmptype)
|
2018-05-02 04:49:23 +02:00
|
|
|
ipt_rule += ' -j {}\n'.format(action)
|
2016-09-12 05:22:53 +02:00
|
|
|
iptables += ipt_rule
|
|
|
|
|
|
|
|
iptables += 'COMMIT\n'
|
|
|
|
return iptables
|
|
|
|
|
|
|
|
def apply_rules_family(self, source, rules, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Apply rules for given source address.
|
|
|
|
Handle only rules for given address family (IPv4 or IPv6).
|
|
|
|
|
|
|
|
:param source: source address
|
|
|
|
:param rules: rules list
|
|
|
|
:param family: address family, either 4 or 6
|
|
|
|
:return: None
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
chain = self.chain_for_addr(source)
|
|
|
|
if chain not in self.chains[family]:
|
|
|
|
self.create_chain(source, chain, family)
|
|
|
|
|
|
|
|
iptables = self.prepare_rules(chain, rules, family)
|
|
|
|
try:
|
|
|
|
self.run_ipt(family, ['-F', chain])
|
|
|
|
p = self.run_ipt_restore(family, ['-n'])
|
2019-09-18 01:42:51 +02:00
|
|
|
(output, _) = p.communicate(iptables.encode())
|
2016-09-12 05:22:53 +02:00
|
|
|
if p.returncode != 0:
|
|
|
|
raise RuleApplyError(
|
|
|
|
'iptables-restore failed: {}'.format(output))
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
raise RuleApplyError('\'iptables -F {}\' failed: {}'.format(
|
|
|
|
chain, e.output))
|
|
|
|
|
|
|
|
def apply_rules(self, source, rules):
|
|
|
|
if self.is_ip6(source):
|
|
|
|
self.apply_rules_family(source, rules, 6)
|
|
|
|
else:
|
|
|
|
self.apply_rules_family(source, rules, 4)
|
|
|
|
|
2020-01-09 18:20:05 +01:00
|
|
|
def update_connected_ips(self, family):
|
2020-01-14 11:22:16 +01:00
|
|
|
ips = self.get_connected_ips(family)
|
|
|
|
|
|
|
|
if not ips:
|
|
|
|
# Just flush.
|
|
|
|
self.run_ipt(family, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
|
|
|
|
self.run_ipt(family, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
|
|
|
|
return
|
|
|
|
|
|
|
|
# Temporarily set policy to DROP while updating the rules.
|
|
|
|
self.run_ipt(family, ['-t', 'raw', '-P', 'PREROUTING', 'DROP'])
|
|
|
|
self.run_ipt(family, ['-t', 'mangle', '-P', 'POSTROUTING', 'DROP'])
|
|
|
|
|
2020-01-10 09:19:32 +01:00
|
|
|
self.run_ipt(family, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_ipt(family, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
|
|
|
|
|
2020-01-14 11:22:16 +01:00
|
|
|
for ip in ips:
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_ipt(family, [
|
2020-01-10 09:19:32 +01:00
|
|
|
'-t', 'raw', '-A', 'QBS-PREROUTING',
|
2020-01-09 18:20:05 +01:00
|
|
|
'!', '-i', 'vif+', '-s', ip, '-j', 'DROP'])
|
|
|
|
self.run_ipt(family, [
|
|
|
|
'-t', 'mangle', '-A', 'QBS-POSTROUTING',
|
|
|
|
'!', '-o', 'vif+', '-d', ip, '-j', 'DROP'])
|
|
|
|
|
2020-01-14 11:22:16 +01:00
|
|
|
self.run_ipt(family, ['-t', 'raw', '-P', 'PREROUTING', 'ACCEPT'])
|
|
|
|
self.run_ipt(family, ['-t', 'mangle', '-P', 'POSTROUTING', 'ACCEPT'])
|
2020-01-09 18:20:05 +01:00
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
def init(self):
|
2020-01-09 18:20:05 +01:00
|
|
|
# Chains QBS-FORWARD, QBS-PREROUTING, QBS-POSTROUTING
|
|
|
|
# need to be created before running this.
|
2016-09-12 05:22:53 +02:00
|
|
|
try:
|
2017-11-18 01:45:19 +01:00
|
|
|
self.run_ipt(4, ['-F', 'QBS-FORWARD'])
|
2018-04-03 01:01:56 +02:00
|
|
|
self.run_ipt(4,
|
|
|
|
['-A', 'QBS-FORWARD', '!', '-i', 'vif+', '-j', 'RETURN'])
|
2017-11-18 01:45:19 +01:00
|
|
|
self.run_ipt(4, ['-A', 'QBS-FORWARD', '-j', 'DROP'])
|
2020-01-10 09:19:32 +01:00
|
|
|
self.run_ipt(4, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_ipt(4, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
|
|
|
|
|
2017-11-18 01:45:19 +01:00
|
|
|
self.run_ipt(6, ['-F', 'QBS-FORWARD'])
|
2018-04-03 01:01:56 +02:00
|
|
|
self.run_ipt(6,
|
|
|
|
['-A', 'QBS-FORWARD', '!', '-i', 'vif+', '-j', 'RETURN'])
|
2017-11-18 01:45:19 +01:00
|
|
|
self.run_ipt(6, ['-A', 'QBS-FORWARD', '-j', 'DROP'])
|
2020-01-10 09:19:32 +01:00
|
|
|
self.run_ipt(6, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_ipt(6, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
|
2016-09-12 05:22:53 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2020-01-09 18:20:05 +01:00
|
|
|
self.log_error(
|
|
|
|
'Error initializing iptables. '
|
|
|
|
'You probably need to create QBS-FORWARD, QBS-PREROUTING and '
|
|
|
|
'QBS-POSTROUTING chains first.'
|
|
|
|
)
|
2016-09-12 05:22:53 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
for family in (4, 6):
|
|
|
|
self.run_ipt(family, ['-F', 'QBS-FORWARD'])
|
2020-01-10 09:19:32 +01:00
|
|
|
self.run_ipt(family, ['-t', 'raw', '-F', 'QBS-PREROUTING'])
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_ipt(family, ['-t', 'mangle', '-F', 'QBS-POSTROUTING'])
|
2016-09-12 05:22:53 +02:00
|
|
|
for chain in self.chains[family]:
|
|
|
|
self.run_ipt(family, ['-F', chain])
|
|
|
|
self.run_ipt(family, ['-X', chain])
|
|
|
|
|
|
|
|
|
|
|
|
class NftablesWorker(FirewallWorker):
|
|
|
|
supported_rule_opts = ['action', 'proto', 'dst4', 'dst6', 'dsthost',
|
2019-09-18 00:10:51 +02:00
|
|
|
'dstports', 'specialtarget', 'icmptype']
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(NftablesWorker, self).__init__()
|
|
|
|
self.chains = {
|
|
|
|
4: set(),
|
|
|
|
6: set(),
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def chain_for_addr(addr):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""Generate iptables chain name for given source address address"""
|
2016-09-12 05:22:53 +02:00
|
|
|
return 'qbs-' + addr.replace('.', '-').replace(':', '-')
|
|
|
|
|
|
|
|
def run_nft(self, nft_input):
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
p = subprocess.Popen(['nft', '-f', '/dev/stdin'],
|
2019-09-18 00:10:51 +02:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
2019-09-18 01:42:51 +02:00
|
|
|
stdout, _ = p.communicate(nft_input.encode())
|
2016-09-12 05:22:53 +02:00
|
|
|
if p.returncode != 0:
|
|
|
|
raise RuleApplyError('nft failed: {}'.format(stdout))
|
|
|
|
|
|
|
|
def create_chain(self, addr, chain, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Create iptables chain and hook traffic coming from `addr` to it.
|
|
|
|
|
|
|
|
:param addr: source IP from which traffic should be handled by the
|
|
|
|
chain
|
|
|
|
:param chain: name of the chain to create
|
|
|
|
:param family: address family (4 or 6)
|
|
|
|
:return: None
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
nft_input = (
|
|
|
|
'table {family} {table} {{\n'
|
|
|
|
' chain {chain} {{\n'
|
|
|
|
' }}\n'
|
|
|
|
' chain forward {{\n'
|
|
|
|
' {family} saddr {ip} jump {chain}\n'
|
|
|
|
' }}\n'
|
|
|
|
'}}\n'.format(
|
|
|
|
family=("ip6" if family == 6 else "ip"),
|
|
|
|
table='qubes-firewall',
|
|
|
|
chain=chain,
|
|
|
|
ip=addr,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.run_nft(nft_input)
|
|
|
|
self.chains[family].add(chain)
|
|
|
|
|
2020-01-09 18:20:05 +01:00
|
|
|
def update_connected_ips(self, family):
|
2020-01-13 16:47:09 +01:00
|
|
|
family_name = ('ip6' if family == 6 else 'ip')
|
2020-01-14 10:22:38 +01:00
|
|
|
table = 'qubes-firewall'
|
2020-01-09 18:20:05 +01:00
|
|
|
|
2020-01-14 10:46:51 +01:00
|
|
|
nft_input = (
|
2020-01-13 16:47:09 +01:00
|
|
|
'flush chain {family_name} {table} prerouting\n'
|
|
|
|
'flush chain {family_name} {table} postrouting\n'
|
2020-01-14 10:46:51 +01:00
|
|
|
).format(family_name=family_name, table=table)
|
2020-01-14 10:22:38 +01:00
|
|
|
|
|
|
|
ips = self.get_connected_ips(family)
|
2020-01-14 10:46:51 +01:00
|
|
|
if ips:
|
|
|
|
addr = '{' + ', '.join(ips) + '}'
|
|
|
|
irule = 'iifname != "vif*" {family_name} saddr {addr} drop\n'.format(
|
|
|
|
family_name=family_name, addr=addr)
|
|
|
|
orule = 'oifname != "vif*" {family_name} daddr {addr} drop\n'.format(
|
|
|
|
family_name=family_name, addr=addr)
|
|
|
|
|
|
|
|
nft_input += (
|
|
|
|
'table {family_name} {table} {{\n'
|
|
|
|
' chain prerouting {{\n'
|
|
|
|
' {irule}'
|
|
|
|
' }}\n'
|
|
|
|
' chain postrouting {{\n'
|
|
|
|
' {orule}'
|
|
|
|
' }}\n'
|
|
|
|
'}}\n'
|
|
|
|
).format(
|
|
|
|
family_name=family_name,
|
|
|
|
table=table,
|
|
|
|
irule=irule,
|
|
|
|
orule=orule,
|
|
|
|
)
|
2020-01-14 10:22:38 +01:00
|
|
|
|
2020-01-09 18:20:05 +01:00
|
|
|
self.run_nft(nft_input)
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
def prepare_rules(self, chain, rules, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Helper function to translate rules list into input for iptables-restore
|
|
|
|
|
|
|
|
:param chain: name of the chain to put rules into
|
|
|
|
:param rules: list of rules
|
|
|
|
:param family: address family (4 or 6)
|
|
|
|
:return: input for iptables-restore
|
|
|
|
:rtype: str
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
assert family in (4, 6)
|
|
|
|
nft_rules = []
|
|
|
|
ip_match = 'ip6' if family == 6 else 'ip'
|
|
|
|
|
|
|
|
fullmask = '/128' if family == 6 else '/32'
|
|
|
|
|
|
|
|
dns = list(addr + fullmask for addr in self.dns_addresses(family))
|
|
|
|
|
|
|
|
for rule in rules:
|
|
|
|
unsupported_opts = set(rule.keys()).difference(
|
|
|
|
set(self.supported_rule_opts))
|
|
|
|
if unsupported_opts:
|
|
|
|
raise RuleParseError(
|
|
|
|
'Unsupported rule option(s): {!s}'.format(unsupported_opts))
|
|
|
|
if 'dst4' in rule and family == 6:
|
|
|
|
raise RuleParseError('IPv4 rule found for IPv6 address')
|
|
|
|
if 'dst6' in rule and family == 4:
|
|
|
|
raise RuleParseError('dst6 rule found for IPv4 address')
|
|
|
|
|
|
|
|
nft_rule = ""
|
|
|
|
|
2018-05-02 04:49:23 +02:00
|
|
|
if rule['action'] == 'accept':
|
|
|
|
action = 'accept'
|
|
|
|
elif rule['action'] == 'drop':
|
|
|
|
action = 'reject with icmp{} type admin-prohibited'.format(
|
2018-05-08 00:22:58 +02:00
|
|
|
'v6' if family == 6 else '')
|
2018-05-02 04:49:23 +02:00
|
|
|
else:
|
|
|
|
raise RuleParseError(
|
|
|
|
'Invalid rule action {}'.format(rule['action']))
|
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
if 'proto' in rule:
|
|
|
|
if family == 4:
|
|
|
|
nft_rule += ' ip protocol {}'.format(rule['proto'])
|
|
|
|
elif family == 6:
|
|
|
|
proto = 'icmpv6' if rule['proto'] == 'icmp' \
|
|
|
|
else rule['proto']
|
|
|
|
nft_rule += ' ip6 nexthdr {}'.format(proto)
|
|
|
|
|
|
|
|
if 'dst4' in rule:
|
|
|
|
nft_rule += ' ip daddr {}'.format(rule['dst4'])
|
|
|
|
elif 'dst6' in rule:
|
|
|
|
nft_rule += ' ip6 daddr {}'.format(rule['dst6'])
|
|
|
|
elif 'dsthost' in rule:
|
2017-12-28 05:15:00 +01:00
|
|
|
try:
|
|
|
|
addrinfo = socket.getaddrinfo(rule['dsthost'], None,
|
|
|
|
(socket.AF_INET6 if family == 6 else socket.AF_INET))
|
|
|
|
except socket.gaierror as e:
|
|
|
|
raise RuleParseError('Failed to resolve {}: {}'.format(
|
|
|
|
rule['dsthost'], str(e)))
|
2016-09-12 05:22:53 +02:00
|
|
|
nft_rule += ' {} daddr {{ {} }}'.format(ip_match,
|
|
|
|
', '.join(set(item[4][0] + fullmask for item in addrinfo)))
|
|
|
|
|
|
|
|
if 'dstports' in rule:
|
|
|
|
dstports = rule['dstports']
|
|
|
|
if len(set(dstports.split('-'))) == 1:
|
|
|
|
dstports = dstports.split('-')[0]
|
|
|
|
else:
|
|
|
|
dstports = None
|
|
|
|
|
|
|
|
if rule.get('specialtarget', None) == 'dns':
|
|
|
|
if dstports not in ('53', None):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
dstports = '53'
|
|
|
|
if not dns:
|
|
|
|
continue
|
|
|
|
nft_rule += ' {} daddr {{ {} }}'.format(ip_match, ', '.join(
|
|
|
|
dns))
|
|
|
|
|
|
|
|
if 'icmptype' in rule:
|
|
|
|
if family == 4:
|
|
|
|
nft_rule += ' icmp type {}'.format(rule['icmptype'])
|
|
|
|
elif family == 6:
|
|
|
|
nft_rule += ' icmpv6 type {}'.format(rule['icmptype'])
|
|
|
|
|
|
|
|
# now duplicate rules for tcp/udp if needed
|
|
|
|
# it isn't possible to specify "tcp dport xx || udp dport xx" in
|
|
|
|
# one rule
|
|
|
|
if dstports is not None:
|
|
|
|
if 'proto' not in rule:
|
|
|
|
nft_rules.append(
|
|
|
|
nft_rule + ' tcp dport {} {}'.format(
|
2018-05-02 04:49:23 +02:00
|
|
|
dstports, action))
|
2016-09-12 05:22:53 +02:00
|
|
|
nft_rules.append(
|
|
|
|
nft_rule + ' udp dport {} {}'.format(
|
2018-05-02 04:49:23 +02:00
|
|
|
dstports, action))
|
2016-09-12 05:22:53 +02:00
|
|
|
else:
|
|
|
|
nft_rules.append(
|
|
|
|
nft_rule + ' {} dport {} {}'.format(
|
2018-05-02 04:49:23 +02:00
|
|
|
rule['proto'], dstports, action))
|
2016-09-12 05:22:53 +02:00
|
|
|
else:
|
2018-05-02 04:49:23 +02:00
|
|
|
nft_rules.append(nft_rule + ' ' + action)
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
'flush chain {family} {table} {chain}\n'
|
|
|
|
'table {family} {table} {{\n'
|
|
|
|
' chain {chain} {{\n'
|
|
|
|
' {rules}\n'
|
|
|
|
' }}\n'
|
|
|
|
'}}\n'.format(
|
|
|
|
family=('ip6' if family == 6 else 'ip'),
|
|
|
|
table='qubes-firewall',
|
|
|
|
chain=chain,
|
|
|
|
rules='\n '.join(nft_rules)
|
|
|
|
))
|
|
|
|
|
|
|
|
def apply_rules_family(self, source, rules, family):
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
Apply rules for given source address.
|
|
|
|
Handle only rules for given address family (IPv4 or IPv6).
|
|
|
|
|
|
|
|
:param source: source address
|
|
|
|
:param rules: rules list
|
|
|
|
:param family: address family, either 4 or 6
|
|
|
|
:return: None
|
2019-09-18 00:10:51 +02:00
|
|
|
"""
|
2016-09-12 05:22:53 +02:00
|
|
|
|
|
|
|
chain = self.chain_for_addr(source)
|
|
|
|
if chain not in self.chains[family]:
|
|
|
|
self.create_chain(source, chain, family)
|
|
|
|
|
|
|
|
self.run_nft(self.prepare_rules(chain, rules, family))
|
|
|
|
|
|
|
|
def apply_rules(self, source, rules):
|
|
|
|
if self.is_ip6(source):
|
|
|
|
self.apply_rules_family(source, rules, 6)
|
|
|
|
else:
|
|
|
|
self.apply_rules_family(source, rules, 4)
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
nft_init = (
|
|
|
|
'table {family} qubes-firewall {{\n'
|
|
|
|
' chain forward {{\n'
|
|
|
|
' type filter hook forward priority 0;\n'
|
2017-11-18 01:45:19 +01:00
|
|
|
' policy drop;\n'
|
2017-12-28 05:27:24 +01:00
|
|
|
' ct state established,related accept\n'
|
2018-04-03 01:01:56 +02:00
|
|
|
' meta iifname != "vif*" accept\n'
|
2016-09-12 05:22:53 +02:00
|
|
|
' }}\n'
|
2020-01-09 18:20:05 +01:00
|
|
|
' chain prerouting {{\n'
|
2020-01-10 09:19:32 +01:00
|
|
|
' type filter hook prerouting priority -300;\n'
|
2020-01-09 18:20:05 +01:00
|
|
|
' policy accept;\n'
|
|
|
|
' }}\n'
|
|
|
|
' chain postrouting {{\n'
|
2020-01-10 09:19:32 +01:00
|
|
|
' type filter hook postrouting priority -300;\n'
|
2020-01-09 18:20:05 +01:00
|
|
|
' policy accept;\n'
|
|
|
|
' }}\n'
|
2016-09-12 05:22:53 +02:00
|
|
|
'}}\n'
|
|
|
|
)
|
|
|
|
nft_init = ''.join(
|
|
|
|
nft_init.format(family=family) for family in ('ip', 'ip6'))
|
|
|
|
self.run_nft(nft_init)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
nft_cleanup = (
|
|
|
|
'delete table ip qubes-firewall\n'
|
|
|
|
'delete table ip6 qubes-firewall\n'
|
|
|
|
)
|
|
|
|
self.run_nft(nft_cleanup)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if spawn.find_executable('nft'):
|
|
|
|
worker = NftablesWorker()
|
|
|
|
else:
|
|
|
|
worker = IptablesWorker()
|
|
|
|
context = daemon.DaemonContext()
|
|
|
|
context.stderr = sys.stderr
|
|
|
|
context.detach_process = False
|
|
|
|
context.files_preserve = [worker.qdb.watch_fd()]
|
|
|
|
context.signal_map = {
|
|
|
|
signal.SIGTERM: lambda _signal, _stack: worker.terminate(),
|
|
|
|
}
|
|
|
|
with context:
|
|
|
|
worker.main()
|
|
|
|
|
2019-09-18 00:10:51 +02:00
|
|
|
|
2016-09-12 05:22:53 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|