Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v3.5-branch] net: ipv4: Check localhost for incoming packet #66739

Merged
merged 2 commits into from
Dec 29, 2023
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
15 changes: 14 additions & 1 deletion subsys/net/ip/ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int net_ipv4_parse_hdr_options(struct net_pkt *pkt,
}
#endif

enum net_verdict net_ipv4_input(struct net_pkt *pkt)
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback)
{
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv4_access, struct net_ipv4_hdr);
NET_PKT_DATA_ACCESS_DEFINE(udp_access, struct net_udp_hdr);
Expand Down Expand Up @@ -281,6 +281,19 @@ enum net_verdict net_ipv4_input(struct net_pkt *pkt)
net_pkt_update_length(pkt, pkt_len);
}

if (!is_loopback) {
if (net_ipv4_is_addr_loopback((struct in_addr *)hdr->dst) ||
net_ipv4_is_addr_loopback((struct in_addr *)hdr->src)) {
NET_DBG("DROP: localhost packet");
goto drop;
}

if (net_ipv4_is_my_addr((struct in_addr *)hdr->src)) {
NET_DBG("DROP: src addr is %s", "mine");
goto drop;
}
}

if (net_ipv4_is_addr_mcast((struct in_addr *)hdr->src)) {
NET_DBG("DROP: src addr is %s", "mcast");
goto drop;
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/ip/net_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
if (IS_ENABLED(CONFIG_NET_IPV6) && vtc_vhl == 0x60) {
return net_ipv6_input(pkt, is_loopback);
} else if (IS_ENABLED(CONFIG_NET_IPV4) && vtc_vhl == 0x40) {
return net_ipv4_input(pkt);
return net_ipv4_input(pkt, is_loopback);
}

NET_DBG("Unknown IP family packet (0x%x)", NET_IPV6_HDR(pkt)->vtc & 0xf0);
Expand Down
6 changes: 4 additions & 2 deletions subsys/net/ip/net_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ static inline bool net_context_is_reuseport_set(struct net_context *context)
#endif

#if defined(CONFIG_NET_NATIVE)
enum net_verdict net_ipv4_input(struct net_pkt *pkt);
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback);
enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback);
#else
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt)
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt,
bool is_loopback)
{
ARG_UNUSED(pkt);
ARG_UNUSED(is_loopback);

return NET_CONTINUE;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/net/icmpv4/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ static void icmpv4_send_echo_req(void)
zassert_true(false, "EchoRequest packet prep failed");
}

if (net_ipv4_input(pkt)) {
if (net_ipv4_input(pkt, false)) {
net_pkt_unref(pkt);
zassert_true(false, "Failed to send");
}
Expand All @@ -474,7 +474,7 @@ static void icmpv4_send_echo_rep(void)
zassert_true(false, "EchoReply packet prep failed");
}

if (net_ipv4_input(pkt)) {
if (net_ipv4_input(pkt, false)) {
net_pkt_unref(pkt);
zassert_true(false, "Failed to send");
}
Expand All @@ -494,7 +494,7 @@ ZTEST(net_icmpv4, test_icmpv4_send_echo_req_opt)
zassert_true(false, "EchoRequest with opts packet prep failed");
}

if (net_ipv4_input(pkt)) {
if (net_ipv4_input(pkt, false)) {
net_pkt_unref(pkt);
zassert_true(false, "Failed to send");
}
Expand All @@ -510,7 +510,7 @@ ZTEST(net_icmpv4, test_send_echo_req_bad_opt)
"EchoRequest with bad opts packet prep failed");
}

if (net_ipv4_input(pkt)) {
if (net_ipv4_input(pkt, false)) {
net_pkt_unref(pkt);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/net/virtual/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ static void test_virtual_recv_data_from_tunnel(int remote_ip,
net_pkt_cursor_init(outer);

if (peer_addr.sa_family == AF_INET) {
verdict = net_ipv4_input(outer);
verdict = net_ipv4_input(outer, false);
} else {
verdict = net_ipv6_input(outer, false);
}
Expand Down
Loading