Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion files/lib/pbr/pbr.uc
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,12 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
let neg_list = 'phys_dev_negative mac_address_negative domain_negative ipv4_negative ipv6_negative';
let src_neg = collect_negations(neg_list, src_addr, 'src', null, null, name, false);
let has_positive = false;
let processed = src_neg.matched;
for (let fg in split(filter_list, /\s+/)) {
let filtered = V.filter_options(fg, src_addr);
if (!filtered) continue;
has_positive = true;
processed += (processed ? ' ' : '') + filtered;
if (V.str_contains(fg, 'ipv4') && !dest_dns_ipv4) continue;
if (V.str_contains(fg, 'ipv6') && !dest_dns_ipv6) continue;
dns_policy_routing(name, filtered, dest_dns, uid, dest_dns_port, dest_dns_ipv4, dest_dns_ipv6, src_neg);
Expand All @@ -728,6 +730,13 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
if (!has_positive && (src_neg.n4 || src_neg.n6))
dns_policy_routing(name, '', dest_dns, uid, dest_dns_port, dest_dns_ipv4, dest_dns_ipv6, src_neg);

for (let i in split(src_addr, /\s+/)) {
if (i && !V.str_contains(processed, i)) {
process_dns_policy_error = true;
push(state.errors, { code: 'errorPolicyProcessUnknownEntry', info: name + ': ' + i });
}
}

if (process_dns_policy_error) output.fail();
else output.ok();
}
Expand Down Expand Up @@ -899,9 +908,15 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
if (!length(src_groups)) push(src_groups, { fg: 'none', fv: '' });
if (!length(dest_groups)) push(dest_groups, { fg: 'none', fv: '' });

// A side that was configured but kept no usable entry must not fall
// through to the rule above: with no address on that side it would match
// every address, so one typo would send all traffic down this interface.
// The loops further down report the entries.
let unusable = (src_addr && !processed_src) || (dest_addr && !processed_dest);

// ucode's for-in yields an array's values, not its indices, so each of
// these is the { fg, fv } pushed above.
for (let src_group in src_groups) {
if (!unusable) for (let src_group in src_groups) {
for (let dest_group in dest_groups) {
if (V.str_contains(src_group.fg, 'ipv4') && V.str_contains(dest_group.fg, 'ipv6')) continue;
if (V.str_contains(src_group.fg, 'ipv6') && V.str_contains(dest_group.fg, 'ipv4')) continue;
Expand Down
9 changes: 7 additions & 2 deletions files/lib/pbr/validators.uc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ function str_contains(haystack, needle) { return haystack != null && needle != n
function str_contains_word(haystack, needle) { return !!(haystack && needle) && index(split(trim('' + haystack), /\s+/), '' + needle) >= 0; }
function str_first_word(s) { let m = s ? match(trim('' + s), /^(\S+)/) : null; return m ? m[1] : ''; }

// No leading zeros, in an octet or in the prefix: nft reads them as octal, so
// '192.168.0.09' and '/08' are not valid to it, and one such value in the
// generated file fails the single `nft -c -f` and with it every policy.
function is_ipv4(s) {
if (!s) return false;
return !!match('' + s, /^((25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(\/([0-2]?[0-9]|3[0-2]))?$/);
return !!match('' + s, /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\/([12]?[0-9]|3[0-2]))?$/);
}

function is_mac_address(s) {
Expand All @@ -30,7 +33,9 @@ function is_ipv6(s) {
function is_domain(s) {
if (!s) return false;
s = '' + s;
if (is_ipv4(s)) return false;
// Four numbers are a mistyped address, not a name: without this they would
// pass as a domain and turn into a set dnsmasq can never fill.
if (match(s, /^[0-9]+(\.[0-9]+){3}$/)) return false;
if (match(s, /^([0-9A-Fa-f]{2}-){5}([0-9A-Fa-f]{2})$/)) return false;
return !!match(s, /^[a-zA-Z0-9]$/) || !!match(s, /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,61}[a-zA-Z0-9]$/) ||
!!match(s, /^([a-zA-Z0-9]([a-zA-Z0-9_-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/);
Expand Down
61 changes: 61 additions & 0 deletions tests/01_validation/07_ipv4_no_leading_zero
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Testing that is_ipv4() and is_domain() both refuse an address with a leading
zero, or any other four numbers that are not an address.

nft reads a leading zero as octal, so '192.168.0.09' and '1.2.3.4/08' are not
valid to it, and the whole generated file fails with them. is_ipv4() must say no.
is_domain() must say no as well: it is asked next, and calling these a domain
would give a set dnsmasq can never fill.

-- Testcase --
import create_validators from 'validators';
let V = create_validators();

let refused = [
'192.168.0.09', '192.168.0.08', '192.168.09.1', '192.09.0.1', '09.2.3.4',
'01.2.3.4', '00.2.3.4', '1.2.3.00', '001.2.3.4', '010.0.0.1', '256.1.1.1',
'1.2.3.1000',
];
let refused_prefix = [
'1.2.3.4/08', '1.2.3.4/09', '1.2.3.4/00', '1.2.3.4/01', '1.2.3.4/032',
];
let still_valid = [
'0.0.0.0', '10.0.0.1', '100.0.0.1', '1.2.3.100', '1.2.3.199', '1.2.3.200',
'1.2.3.8', '1.2.3.9', '1.2.3.4/0', '1.2.3.4/8', '1.2.3.4/9', '1.2.3.4/10',
'1.2.3.4/29', '1.2.3.4/32',
];
let still_domains = [
'1.2.3', '1.2.3.4.5', '1.2.3.example', '1a.2.3.4', 'host1', '12345',
];

let pass = 0, fail = 0;
function check(what, value, actual, expected) {
if (actual === expected) {
pass++;
} else {
fail++;
printf("FAIL: %s(%J) — expected %J, got %J\n", what, value, expected, actual);
}
}

for (let v in refused) {
check('is_ipv4', v, V.is_ipv4(v), false);
check('is_domain', v, V.is_domain(v), false);
}
for (let v in refused_prefix) {
check('is_ipv4', v, V.is_ipv4(v), false);
check('is_domain', v, V.is_domain(v), false);
}
for (let v in still_valid) {
check('is_ipv4', v, V.is_ipv4(v), true);
check('is_domain', v, V.is_domain(v), false);
}
for (let v in still_domains) {
check('is_ipv4', v, V.is_ipv4(v), false);
check('is_domain', v, V.is_domain(v), true);
}
printf("ipv4_no_leading_zero: %d/%d passed\n", pass, pass + fail);
-- End --

-- Expect stdout --
ipv4_no_leading_zero: 74/74 passed
-- End --
207 changes: 207 additions & 0 deletions tests/04_policies/13_unusable_address_skips_policy
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
Testing that a policy left with no usable address on a side is skipped.

An entry that is neither an address, a domain, a device nor a MAC is reported as
errorPolicyProcessUnknownEntry. But the rule used to be written first, with that
side left empty, and a side with no address matches every address. So one typo in
src_addr gave a rule with no match at all: all traffic went down that interface,
and pbr carried on as if the policy were merely reported.

A side that was never configured stays what it always was, so 'Port Only' keeps
its rule. A side that keeps at least one good entry keeps its rule too, with the
bad entry reported. dns_policy reported nothing at all for a bad entry; it now
reports the same error.

-- Testcase --
import create_pbr from 'pbr';
let pbr = create_pbr();

let result = pbr.start_service(['on_start']);
let nft = global.mocklib.read_captured('/var/run/pbr.nft') || '';
let dnsmasq = global.mocklib.read_captured('/var/run/pbr.dnsmasq') || '';

function rules_for(n) {
return filter(split(nft, '\n'), l => index(l, 'comment "' + n + '"') >= 0);
}
function errors_for(info) {
return filter(result.errors || [],
e => e.code == 'errorPolicyProcessUnknownEntry' && '' + e.info == info);
}

let checks = [];
for (let p in [
['Bad Src', '192.168.0.09'], ['Bad Prefix', '1.2.3.4/33'],
['Bad Octal Prefix', '1.2.3.4/08'], ['Bad Dest', '1.2.3.08'],
['Bad Src Good Dest', '192.168.0.09'], ['Good Src Bad Dest', '256.1.1.1'],
['Bad Negated', '!192.168.0.09'], ['Bad With Port', '192.168.0.09'],
['Dns Bad', '192.168.0.09'],
]) {
push(checks, [p[0] + ': no rule', length(rules_for(p[0])) == 0]);
push(checks, [p[0] + ': reported once', length(errors_for(p[0] + ': ' + p[1])) == 1]);
}

let mixed = rules_for('Mixed Src');
let dns_mixed = rules_for('Dns Mixed');
push(checks,
['mixed keeps the good entry', length(mixed) > 0 &&
length(filter(mixed, l => index(l, '192.168.0.8') >= 0)) == length(mixed)],
['mixed drops the bad entry', index(nft, '192.168.0.09') < 0],
['mixed reported', length(errors_for('Mixed Src: 192.168.0.09')) == 1],
['dns mixed keeps the good entry', length(dns_mixed) > 0],
['dns mixed reported', length(errors_for('Dns Mixed: 192.168.0.09')) == 1],
['port only keeps its rule', length(rules_for('Port Only')) > 0],
['good policy kept', length(rules_for('Good')) > 0],
['good dns policy kept', length(rules_for('Dns Good')) > 0],
['no error for a good policy', length(filter(result.errors || [],
e => index('' + e.info, 'Good:') >= 0 || index('' + e.info, 'Port Only') >= 0)) == 0],
['nothing sent to dnsmasq', index(dnsmasq, '1.2.3.08') < 0 && index(dnsmasq, '256.1.1.1') < 0]
);

let pass = 0, fail = 0;
for (let c in checks) {
if (c[1]) {
pass++;
} else {
fail++;
printf("FAIL: %s\n", c[0]);
}
}
printf("unusable_address_skips_policy: %d/%d passed\n", pass, pass + fail);
-- End --

-- File fs/stat~_sys_class_net_br-lan.json --
{ "type": "link" }
-- End --

-- File uci/pbr.json --
{
"pbr": [
{
".name": "config",
"enabled": "1",
"fw_mask": "00ff0000",
"ipv6_enabled": "0",
"resolver_set": "dnsmasq.nftset",
"uplink_interface": "wan",
"uplink_interface6": "wan6",
"uplink_ip_rules_priority": "30000",
"uplink_mark": "00010000",
"verbosity": "2",
"lan_device": ["br-lan"]
}
],
"policy": [
{
".name": "p0",
"name": "Bad Src",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.09"
},
{
".name": "p1",
"name": "Bad Prefix",
"enabled": "1",
"interface": "wan",
"src_addr": "1.2.3.4/33"
},
{
".name": "p2",
"name": "Bad Octal Prefix",
"enabled": "1",
"interface": "wan",
"src_addr": "1.2.3.4/08"
},
{
".name": "p3",
"name": "Bad Dest",
"enabled": "1",
"interface": "wan",
"dest_addr": "1.2.3.08"
},
{
".name": "p4",
"name": "Bad Src Good Dest",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.09",
"dest_addr": "203.0.113.9"
},
{
".name": "p5",
"name": "Good Src Bad Dest",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.5",
"dest_addr": "256.1.1.1"
},
{
".name": "p6",
"name": "Bad Negated",
"enabled": "1",
"interface": "wan",
"src_addr": "!192.168.0.09"
},
{
".name": "p7",
"name": "Bad With Port",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.09",
"dest_port": "443"
},
{
".name": "p8",
"name": "Mixed Src",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.8 192.168.0.09"
},
{
".name": "p9",
"name": "Port Only",
"enabled": "1",
"interface": "wan",
"dest_port": "8443"
},
{
".name": "p10",
"name": "Good",
"enabled": "1",
"interface": "wan",
"src_addr": "192.168.0.7"
}
],
"dns_policy": [
{
".name": "d0",
"name": "Dns Bad",
"enabled": "1",
"src_addr": "192.168.0.09",
"dest_dns": "1.1.1.1",
"dest_dns_port": "53"
},
{
".name": "d1",
"name": "Dns Mixed",
"enabled": "1",
"src_addr": "192.168.0.8 192.168.0.09",
"dest_dns": "1.1.1.1",
"dest_dns_port": "53"
},
{
".name": "d2",
"name": "Dns Good",
"enabled": "1",
"src_addr": "192.168.0.7",
"dest_dns": "1.1.1.1",
"dest_dns_port": "53"
}
],
"include": [
]
}
-- End --

-- Expect stdout --
unusable_address_skips_policy: 28/28 passed
-- End --
Loading