Skip to content

Commit 65104bc

Browse files
jukkarcfriedt
authored andcommitted
net: ipv4: Check localhost for incoming packet
If we receive a packet from non localhost interface, then drop it if either source or destination address is a localhost address. Signed-off-by: Jukka Rissanen <[email protected]> (cherry picked from commit 6d41e68)
1 parent ce4c30f commit 65104bc

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

subsys/net/ip/ipv4.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int net_ipv4_parse_hdr_options(struct net_pkt *pkt,
210210
}
211211
#endif
212212

213-
enum net_verdict net_ipv4_input(struct net_pkt *pkt)
213+
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback)
214214
{
215215
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv4_access, struct net_ipv4_hdr);
216216
NET_PKT_DATA_ACCESS_DEFINE(udp_access, struct net_udp_hdr);
@@ -266,6 +266,14 @@ enum net_verdict net_ipv4_input(struct net_pkt *pkt)
266266
net_pkt_update_length(pkt, pkt_len);
267267
}
268268

269+
if (!is_loopback) {
270+
if (net_ipv4_is_addr_loopback(&hdr->dst) ||
271+
net_ipv4_is_addr_loopback(&hdr->src)) {
272+
NET_DBG("DROP: localhost packet");
273+
goto drop;
274+
}
275+
}
276+
269277
if (net_ipv4_is_addr_mcast(&hdr->src)) {
270278
NET_DBG("DROP: src addr is %s", "mcast");
271279
goto drop;

subsys/net/ip/net_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
123123
#endif
124124
#if defined(CONFIG_NET_IPV4)
125125
case 0x40:
126-
return net_ipv4_input(pkt);
126+
return net_ipv4_input(pkt, is_loopback);
127127
#endif
128128
}
129129

subsys/net/ip/net_private.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ static inline const char *net_context_state(struct net_context *context)
6969
#endif
7070

7171
#if defined(CONFIG_NET_NATIVE)
72-
enum net_verdict net_ipv4_input(struct net_pkt *pkt);
72+
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback);
7373
enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback);
7474
#else
75-
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt)
75+
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt,
76+
bool is_loopback)
7677
{
7778
ARG_UNUSED(pkt);
79+
ARG_UNUSED(is_loopback);
7880

7981
return NET_CONTINUE;
8082
}

tests/net/icmpv4/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static void test_icmpv4_send_echo_req(void)
440440
zassert_true(false, "EchoRequest packet prep failed");
441441
}
442442

443-
if (net_ipv4_input(pkt)) {
443+
if (net_ipv4_input(pkt, false)) {
444444
net_pkt_unref(pkt);
445445
zassert_true(false, "Failed to send");
446446
}
@@ -457,7 +457,7 @@ static void test_icmpv4_send_echo_rep(void)
457457
zassert_true(false, "EchoReply packet prep failed");
458458
}
459459

460-
if (net_ipv4_input(pkt)) {
460+
if (net_ipv4_input(pkt, false)) {
461461
net_pkt_unref(pkt);
462462
zassert_true(false, "Failed to send");
463463
}
@@ -476,7 +476,7 @@ static void test_icmpv4_send_echo_req_opt(void)
476476
zassert_true(false, "EchoRequest with opts packet prep failed");
477477
}
478478

479-
if (net_ipv4_input(pkt)) {
479+
if (net_ipv4_input(pkt, false)) {
480480
net_pkt_unref(pkt);
481481
zassert_true(false, "Failed to send");
482482
}
@@ -492,7 +492,7 @@ static void test_icmpv4_send_echo_req_bad_opt(void)
492492
"EchoRequest with bad opts packet prep failed");
493493
}
494494

495-
if (!net_ipv4_input(pkt)) {
495+
if (!net_ipv4_input(pkt, false)) {
496496
net_pkt_unref(pkt);
497497
zassert_true(false, "Failed to send");
498498
}

tests/net/virtual/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ static void test_virtual_recv_data_from_tunnel(int remote_ip,
976976
net_pkt_cursor_init(outer);
977977

978978
if (peer_addr.sa_family == AF_INET) {
979-
verdict = net_ipv4_input(outer);
979+
verdict = net_ipv4_input(outer, false);
980980
} else {
981981
verdict = net_ipv6_input(outer, false);
982982
}

0 commit comments

Comments
 (0)