Skip to content

Commit 878aed8

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: force port remap to prevent shadowing well-known ports
If destination port is above 32k and source port below 16k assume this might cause 'port shadowing' where a 'new' inbound connection matches an existing one, e.g. inbound X:41234 -> Y:53 matches existing conntrack entry Z:53 -> X:4123, where Z got natted to X. In this case, new packet is natted to Z:53 which is likely unwanted. We avoid the rewrite for connections that originate from local host: port-shadowing is only possible with forwarded connections. Also adjust test case. v3: no need to call tuple_force_port_remap if already in random mode (Phil) Signed-off-by: Florian Westphal <fw@strlen.de> Acked-by: Phil Sutter <phil@nwl.cc> Acked-by: Eric Garver <eric@garver.life> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 4a6fbdd commit 878aed8

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎net/netfilter/nf_nat_core.c‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,38 @@ static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
494494
goto another_round;
495495
}
496496

497+
static bool tuple_force_port_remap(const struct nf_conntrack_tuple *tuple)
498+
{
499+
u16 sp, dp;
500+
501+
switch (tuple->dst.protonum) {
502+
case IPPROTO_TCP:
503+
sp = ntohs(tuple->src.u.tcp.port);
504+
dp = ntohs(tuple->dst.u.tcp.port);
505+
break;
506+
case IPPROTO_UDP:
507+
case IPPROTO_UDPLITE:
508+
sp = ntohs(tuple->src.u.udp.port);
509+
dp = ntohs(tuple->dst.u.udp.port);
510+
break;
511+
default:
512+
return false;
513+
}
514+
515+
/* IANA: System port range: 1-1023,
516+
* user port range: 1024-49151,
517+
* private port range: 49152-65535.
518+
*
519+
* Linux default ephemeral port range is 32768-60999.
520+
*
521+
* Enforce port remapping if sport is significantly lower
522+
* than dport to prevent NAT port shadowing, i.e.
523+
* accidental match of 'new' inbound connection vs.
524+
* existing outbound one.
525+
*/
526+
return sp < 16384 && dp >= 32768;
527+
}
528+
497529
/* Manipulate the tuple into the range given. For NF_INET_POST_ROUTING,
498530
* we change the source to map into the range. For NF_INET_PRE_ROUTING
499531
* and NF_INET_LOCAL_OUT, we change the destination to map into the
@@ -507,11 +539,17 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
507539
struct nf_conn *ct,
508540
enum nf_nat_manip_type maniptype)
509541
{
542+
bool random_port = range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL;
510543
const struct nf_conntrack_zone *zone;
511544
struct net *net = nf_ct_net(ct);
512545

513546
zone = nf_ct_zone(ct);
514547

548+
if (maniptype == NF_NAT_MANIP_SRC &&
549+
!random_port &&
550+
!ct->local_origin)
551+
random_port = tuple_force_port_remap(orig_tuple);
552+
515553
/* 1) If this srcip/proto/src-proto-part is currently mapped,
516554
* and that same mapping gives a unique tuple within the given
517555
* range, use that.
@@ -520,8 +558,7 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
520558
* So far, we don't do local source mappings, so multiple
521559
* manips not an issue.
522560
*/
523-
if (maniptype == NF_NAT_MANIP_SRC &&
524-
!(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL)) {
561+
if (maniptype == NF_NAT_MANIP_SRC && !random_port) {
525562
/* try the original tuple first */
526563
if (in_range(orig_tuple, range)) {
527564
if (!nf_nat_used_tuple(orig_tuple, ct)) {
@@ -545,7 +582,7 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
545582
*/
546583

547584
/* Only bother mapping if it's not already in range and unique */
548-
if (!(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL)) {
585+
if (!random_port) {
549586
if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
550587
if (!(range->flags & NF_NAT_RANGE_PROTO_OFFSET) &&
551588
l4proto_in_range(tuple, maniptype,

‎tools/testing/selftests/netfilter/nft_nat.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,9 @@ EOF
880880
return $ksft_skip
881881
fi
882882

883-
# test default behaviour. Packet from ns1 to ns0 is redirected to ns2.
884-
test_port_shadow "default" "CLIENT"
883+
# test default behaviour. Packet from ns1 to ns0 is not redirected
884+
# due to automatic port translation.
885+
test_port_shadow "default" "ROUTER"
885886

886887
# test packet filter based mitigation: prevent forwarding of
887888
# packets claiming to come from the service port.

0 commit comments

Comments
 (0)