8000 net: apply traffic_rule entries in config order by matepad007 · Pull Request #419 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content
Open
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ ifeq ($(NL3_EXISTS), yes)
$(call run_test, ./nsjail --config tests/traffic-drop-tcp4.cfg -q -t 1 -- /bin/bash -c 'sleep 10', 137)
$(call run_test, ./nsjail --config tests/traffic-drop-udp6.cfg -q -t 1 -- /bin/bash -c 'sleep 10', 137)
$(call run_test, ./nsjail --config tests/traffic-mixed.cfg -q -t 1 -- /bin/bash -c 'sleep 10', 137)
$(call run_test, ./tests/traffic_rule_order_test.sh ./nsjail, 0)
endif

# --- IPv4-only NAT tests ---
Expand Down
1 change: 1 addition & 0 deletions config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ message NsJailConfig {
/* Address family: IPV4 (default) or IPV6 */
optional IpFamily ip_family = 15 [default = IPV4];
}
/* Evaluated in the order listed. */
repeated TrafficRule traffic_rule = 96;

message UserNet {
Expand Down
20 changes: 17 additions & 3 deletions net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,24 @@ static bool parseIp6(const std::string& ip_str, struct in6_addr* addr, int* mask
}

#ifdef HAVE_LIBNL3
static bool applyTrafficRule(
struct nl_sock* sk, const nsjail::NsJailConfig_TrafficRule& rule, int family) {
/*
* Rules without an explicit priority are placed before the previously added one, which would
* evaluate them in reverse config order. Give each rule its own priority, ascending in config
* order, and keep them all below the main table lookup (32766).
*/
static constexpr uint32_t kTrafficRulePrioBase = 1000;
static constexpr size_t kTrafficRuleMax = 30000;

static bool applyTrafficRule(struct nl_sock* sk, const nsjail::NsJailConfig_TrafficRule& rule,
int family, uint32_t prio) {
struct rtnl_rule* rtnl_rule = rtnl_rule_alloc();
if (!rtnl_rule) {
LOG_E("rtnl_rule_alloc() failed");
return false;
}

rtnl_rule_set_family(rtnl_rule, family);
rtnl_rule_set_prio(rtnl_rule, prio);

if (rule.has_src_ip() && !rule.src_ip().empty()) {
struct nl_addr* addr;
Expand Down Expand Up @@ -933,11 +942,16 @@ bool initNsFromChild(nsj_t* nsj) {
LOG_E("nl_connect() failed");
return false;
}
if (static_cast<size_t>(nsj->njc.traffic_rule_size()) > kTrafficRuleMax) {
LOG_E("Too many traffic_rule entries (max %zu)", kTrafficRuleMax);
return false;
}
uint32_t prio = kTrafficRulePrioBase;
for (const auto& rule : nsj->njc.traffic_rule()) {
int family = (rule.ip_family() == nsjail::NsJailConfig_TrafficRule::IPV6)
? AF_INET6
: AF_INET;
if (!applyTrafficRule(sk, rule, family)) {
if (!applyTrafficRule(sk, rule, family, prio++)) {
return false;
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/traffic_rule_order_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh
# Regression test: traffic_rule entries must be evaluated in config order, for both
# IPv4 and IPv6. A narrower ALLOW listed before a broader DROP must come first in the
# jail's policy routing rules.
set -eu

nsjail_bin=${1:-./nsjail}
if [ ! -x "$nsjail_bin" ]; then
echo "FAIL: nsjail binary not found at '$nsjail_bin'" >&2
exit 1
fi
if ! command -v ip > /dev/null 2>&1; then
echo "FAIL: ip(8) not found" >&2
exit 1
fi

T=$(mktemp -d "${TMPDIR:-/tmp}/nsjail-traffic-rule-order.XXXXXXXX")
cleanup() {
rm -rf "$T"
}
trap cleanup EXIT HUP INT TERM

cat > "$T/order.cfg" <<EOT
mode: ONCE
skip_setsid: true
mount { src: "/" dst: "/" is_bind: true }
traffic_rule { dst_ip: "10.1.0.0/16" action: ALLOW }
traffic_rule { dst_ip: "10.0.0.0/8" action: DROP }
traffic_rule { ip_family: IPV6 dst_ip: "fd00:1::/32" action: ALLOW }
traffic_rule { ip_family: IPV6 dst_ip: "fd00::/8" action: DROP }
EOT

# Line number of the first line of an "ip rule" listing containing $2.
check_before() {
family=$1
first=$2
second=$3
rules=$("$nsjail_bin" -q --user 99999 --group 99999 --config "$T/order.cfg" -- \
/bin/sh -c "ip $family rule show") || {
echo "FAIL: nsjail failed to start" >&2
exit 1
}
a=$(printf '%s\n' "$rules" | grep -n -F "to $first " | head -n1 | cut -d: -f1)
b=$(printf '%s\n' "$rules" | grep -n -F "to $second " | head -n1 | cut -d: -f1)
if [ -z "$a" ] || [ -z "$b" ]; then
echo "FAIL: missing rule for $first or $second:" >&2
printf '%s\n' "$rules" >&2
exit 1
fi
if [ "$a" -ge "$b" ]; then
echo "FAIL: $first is evaluated after $second:" >&2
printf '%s\n' "$rules" >&2
exit 1
fi
}

check_before -4 10.1.0.0/16 10.0.0.0/8
check_before -6 fd00:1::/32 fd00::/8

echo "PASS: traffic_rule_order_test"
0