Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: net: fix "buffer overflow detected" for tap.c

When the selftest 'tap.c' is compiled with '-D_FORTIFY_SOURCE=3',
the strcpy() in rtattr_add_strsz() is replaced with a checked
version which causes the test to consistently fail when compiled
with toolchains for which this option is enabled by default.

TAP version 13
1..3
# Starting 3 tests from 1 test cases.
# RUN tap.test_packet_valid_udp_gso ...
*** buffer overflow detected ***: terminated
# test_packet_valid_udp_gso: Test terminated by assertion
# FAIL tap.test_packet_valid_udp_gso
not ok 1 tap.test_packet_valid_udp_gso
# RUN tap.test_packet_valid_udp_csum ...
*** buffer overflow detected ***: terminated
# test_packet_valid_udp_csum: Test terminated by assertion
# FAIL tap.test_packet_valid_udp_csum
not ok 2 tap.test_packet_valid_udp_csum
# RUN tap.test_packet_crash_tap_invalid_eth_proto ...
*** buffer overflow detected ***: terminated
# test_packet_crash_tap_invalid_eth_proto: Test terminated by assertion
# FAIL tap.test_packet_crash_tap_invalid_eth_proto
not ok 3 tap.test_packet_crash_tap_invalid_eth_proto
# FAILED: 0 / 3 tests passed.
# Totals: pass:0 fail:3 xfail:0 xpass:0 skip:0 error:0

A buffer overflow is detected by the fortified glibc __strcpy_chk()
since the __builtin_object_size() of `RTA_DATA(rta)` is incorrectly
reported as 1, even though there is ample space in its bounding
buffer `req`.

Additionally, given that IFLA_IFNAME also expects a null-terminated
string, callers of rtaddr_add_str{,sz}() could simply use the
rtaddr_add_strsz() variant. (which has been renamed to remove the
trailing `sz`) memset() has been used for this function since it
is unchecked and thus circumvents the issue discussed in the
previous paragraph.

Fixes: 2e64fe4624d1 ("selftests: add few test cases for tap driver")
Signed-off-by: Alice C. Munduruca <alice.munduruca@canonical.com>
Reviewed-by: Cengiz Can <cengiz.can@canonical.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/20251216170641.250494-1-alice.munduruca@canonical.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Alice C. Munduruca and committed by
Paolo Abeni
472c5dd6 12cab119

+5 -11
+5 -11
tools/testing/selftests/net/tap.c
··· 56 56 static struct rtattr *rtattr_add_str(struct nlmsghdr *nh, unsigned short type, 57 57 const char *s) 58 58 { 59 - struct rtattr *rta = rtattr_add(nh, type, strlen(s)); 59 + unsigned int strsz = strlen(s) + 1; 60 + struct rtattr *rta; 60 61 61 - memcpy(RTA_DATA(rta), s, strlen(s)); 62 - return rta; 63 - } 62 + rta = rtattr_add(nh, type, strsz); 64 63 65 - static struct rtattr *rtattr_add_strsz(struct nlmsghdr *nh, unsigned short type, 66 - const char *s) 67 - { 68 - struct rtattr *rta = rtattr_add(nh, type, strlen(s) + 1); 69 - 70 - strcpy(RTA_DATA(rta), s); 64 + memcpy(RTA_DATA(rta), s, strsz); 71 65 return rta; 72 66 } 73 67 ··· 113 119 114 120 link_info = rtattr_begin(&req.nh, IFLA_LINKINFO); 115 121 116 - rtattr_add_strsz(&req.nh, IFLA_INFO_KIND, link_type); 122 + rtattr_add_str(&req.nh, IFLA_INFO_KIND, link_type); 117 123 118 124 if (fill_info_data) { 119 125 info_data = rtattr_begin(&req.nh, IFLA_INFO_DATA);