Skip to content

Commit 3e65311

Browse files
committed
Addressed more comments from copilot
1 parent 6cbe141 commit 3e65311

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/port/posix/linux_tun.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ int tun_init(struct wolfIP_ll_dev *ll, const char *ifname,
180180

181181
memset(&ifr, 0, sizeof(ifr));
182182
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
183+
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
183184
if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) {
184185
perror("ioctl SIOCGIFFLAGS");
185186
close(sock_fd);

src/test/unit/unit.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10346,6 +10346,79 @@ START_TEST(test_ll_send_frame_non_ethernet_short_len)
1034610346
}
1034710347
END_TEST
1034810348

10349+
START_TEST(test_ll_helpers_invalid_inputs)
10350+
{
10351+
struct wolfIP s;
10352+
uint8_t buf[4] = {0};
10353+
10354+
wolfIP_init(&s);
10355+
mock_link_init(&s);
10356+
last_frame_sent_size = 0;
10357+
10358+
ck_assert_int_eq(wolfIP_ll_is_non_ethernet(NULL, 0), 0);
10359+
ck_assert_int_eq(wolfIP_ll_is_non_ethernet(&s, s.if_count), 0);
10360+
10361+
wolfIP_ll_send_frame(NULL, 0, buf, (uint32_t)sizeof(buf));
10362+
ck_assert_uint_eq(last_frame_sent_size, 0);
10363+
10364+
wolfIP_ll_send_frame(&s, s.if_count, buf, (uint32_t)sizeof(buf));
10365+
ck_assert_uint_eq(last_frame_sent_size, 0);
10366+
}
10367+
END_TEST
10368+
10369+
START_TEST(test_non_ethernet_recv_oversize_dropped)
10370+
{
10371+
struct wolfIP s;
10372+
struct tsocket *ts;
10373+
struct wolfIP_ll_dev *ll;
10374+
struct wolfIP_ip_packet tmp;
10375+
uint8_t *ip_hdr;
10376+
uint8_t buf[LINK_MTU];
10377+
uint32_t local_ip = 0x0A000001U;
10378+
uint32_t src_ip = 0x0A0000A1U;
10379+
uint32_t dst_ip = local_ip;
10380+
struct {
10381+
uint16_t src_port;
10382+
uint16_t dst_port;
10383+
uint16_t len;
10384+
uint16_t csum;
10385+
} PACKED udp_hdr;
10386+
10387+
wolfIP_init(&s);
10388+
mock_link_init(&s);
10389+
wolfIP_ipconfig_set(&s, local_ip, 0xFFFFFF00U, 0);
10390+
10391+
ll = wolfIP_getdev_ex(&s, TEST_PRIMARY_IF);
10392+
ck_assert_ptr_nonnull(ll);
10393+
ll->non_ethernet = 1;
10394+
10395+
ts = udp_new_socket(&s);
10396+
ck_assert_ptr_nonnull(ts);
10397+
ts->src_port = 1234;
10398+
ts->local_ip = local_ip;
10399+
10400+
memset(buf, 0, sizeof(buf));
10401+
memset(&tmp, 0, sizeof(tmp));
10402+
tmp.ver_ihl = 0x45;
10403+
tmp.ttl = 64;
10404+
tmp.proto = WI_IPPROTO_UDP;
10405+
tmp.len = ee16(IP_HEADER_LEN + UDP_HEADER_LEN);
10406+
tmp.src = ee32(src_ip);
10407+
tmp.dst = ee32(dst_ip);
10408+
iphdr_set_checksum(&tmp);
10409+
ip_hdr = ((uint8_t *)&tmp) + ETH_HEADER_LEN;
10410+
memcpy(buf, ip_hdr, IP_HEADER_LEN);
10411+
udp_hdr.src_port = ee16(1111);
10412+
udp_hdr.dst_port = ee16(1234);
10413+
udp_hdr.len = ee16(UDP_HEADER_LEN);
10414+
udp_hdr.csum = 0;
10415+
memcpy(buf + IP_HEADER_LEN, &udp_hdr, sizeof(udp_hdr));
10416+
10417+
wolfIP_recv_ex(&s, TEST_PRIMARY_IF, buf, (uint32_t)(LINK_MTU - ETH_HEADER_LEN + 1));
10418+
ck_assert_ptr_eq(fifo_peek(&ts->sock.udp.rxbuf), NULL);
10419+
}
10420+
END_TEST
10421+
1034910422
START_TEST(test_forward_packet_filter_drop_udp_icmp)
1035010423
{
1035110424
struct wolfIP s;
@@ -18589,6 +18662,8 @@ Suite *wolf_suite(void)
1858918662
tcase_add_test(tc_utils, test_forward_packet_filter_drop_udp_icmp);
1859018663
tcase_add_test(tc_utils, test_ll_send_frame_non_ethernet_strips);
1859118664
tcase_add_test(tc_utils, test_ll_send_frame_non_ethernet_short_len);
18665+
tcase_add_test(tc_utils, test_ll_helpers_invalid_inputs);
18666+
tcase_add_test(tc_utils, test_non_ethernet_recv_oversize_dropped);
1859218667
#endif
1859318668
tcase_add_test(tc_utils, test_dns_format_ptr_name);
1859418669
tcase_add_test(tc_utils, test_dns_skip_and_copy_name);

src/wolfip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,7 +5650,7 @@ void wolfIP_recv(struct wolfIP *s, void *buf, uint32_t len)
56505650
if (wolfIP_ll_is_non_ethernet(s, WOLFIP_PRIMARY_IF_IDX)) {
56515651
uint8_t frame[LINK_MTU];
56525652
if (len > LINK_MTU - ETH_HEADER_LEN)
5653-
len = LINK_MTU - ETH_HEADER_LEN;
5653+
return;
56545654
#if ETH_HEADER_LEN > 0
56555655
memset(frame, 0, ETH_HEADER_LEN);
56565656
#endif
@@ -5666,7 +5666,7 @@ void wolfIP_recv_ex(struct wolfIP *s, unsigned int if_idx, void *buf, uint32_t l
56665666
if (wolfIP_ll_is_non_ethernet(s, if_idx)) {
56675667
uint8_t frame[LINK_MTU];
56685668
if (len > LINK_MTU - ETH_HEADER_LEN)
5669-
len = LINK_MTU - ETH_HEADER_LEN;
5669+
return;
56705670
#if ETH_HEADER_LEN > 0
56715671
memset(frame, 0, ETH_HEADER_LEN);
56725672
#endif

0 commit comments

Comments
 (0)