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

net: ip: net_pkt: Fix net_pkt_pull data buffer corruption #22335

Merged
merged 2 commits into from
Feb 3, 2020
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
2 changes: 1 addition & 1 deletion subsys/net/ip/net_pkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@ int net_pkt_pull(struct net_pkt *pkt, size_t length)
net_pkt_cursor_backup(pkt, &backup);
a8961713 marked this conversation as resolved.
Show resolved Hide resolved

while (length) {
u8_t left, rem;
size_t left, rem;

pkt_cursor_advance(pkt, false);

Expand Down
46 changes: 45 additions & 1 deletion tests/net/net_pkt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,49 @@ void test_net_pkt_copy(void)
"Pkt not properly unreferenced");
a8961713 marked this conversation as resolved.
Show resolved Hide resolved
}

#define PULL_TEST_PKT_DATA_SIZE 600

void test_net_pkt_pull(void)
{
const int PULL_AMOUNT = 8;
struct net_pkt *dummy_pkt;
static u8_t pkt_data[PULL_TEST_PKT_DATA_SIZE];
static u8_t pkt_data_readback[PULL_TEST_PKT_DATA_SIZE];
int i;

for (i = 0; i < PULL_TEST_PKT_DATA_SIZE; ++i) {
pkt_data[i] = i & 0xff;
}

dummy_pkt = net_pkt_alloc_with_buffer(eth_if,
PULL_TEST_PKT_DATA_SIZE,
AF_UNSPEC,
0,
K_NO_WAIT);
zassert_true(dummy_pkt != NULL, "Pkt not allocated");

zassert_true(net_pkt_write(dummy_pkt,
pkt_data,
PULL_TEST_PKT_DATA_SIZE) == 0,
"Write packet failed");

net_pkt_cursor_init(dummy_pkt);
net_pkt_pull(dummy_pkt, PULL_AMOUNT);
zassert_equal(net_pkt_get_len(dummy_pkt),
PULL_TEST_PKT_DATA_SIZE - PULL_AMOUNT,
"Pull failed to set new size");
zassert_true(net_pkt_read(dummy_pkt,
pkt_data_readback,
PULL_TEST_PKT_DATA_SIZE - PULL_AMOUNT) == 0,
"Read packet failed");
zassert_mem_equal(pkt_data_readback,
&pkt_data[PULL_AMOUNT],
PULL_TEST_PKT_DATA_SIZE - PULL_AMOUNT,
"Packet data changed");

net_pkt_unref(dummy_pkt);
}

void test_main(void)
{
eth_if = net_if_get_default();
Expand All @@ -639,7 +682,8 @@ void test_main(void)
ztest_unit_test(test_net_pkt_basics_of_rw),
ztest_unit_test(test_net_pkt_advanced_basics),
ztest_unit_test(test_net_pkt_easier_rw_usage),
ztest_unit_test(test_net_pkt_copy)
ztest_unit_test(test_net_pkt_copy),
ztest_unit_test(test_net_pkt_pull)
);

ztest_run_test_suite(net_pkt_tests);
Expand Down
6 changes: 6 additions & 0 deletions tests/net/net_pkt/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ tests:
net.packet:
min_ram: 20
tags: net
net.packet.large_buffer:
min_ram: 20
tags: net
extra_configs:
- CONFIG_NET_BUF_FIXED_DATA_SIZE=y
- CONFIG_NET_BUF_DATA_SIZE=512