From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: "Alice C. Munduruca" <alice.munduruca@canonical.com>,
netdev@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org,
"Alice C. Munduruca" <alice.munduruca@canonical.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
Willem de Bruijn <willemb@google.com>,
Cengiz Can <cengiz.can@canonical.com>,
cbulinaru@gmail.com
Subject: Re: [PATCH net v3] selftests: net: fix "buffer overflow detected" for tap.c
Date: Tue, 16 Dec 2025 14:54:57 -0500 [thread overview]
Message-ID: <willemdebruijn.kernel.311f094b4d393@gmail.com> (raw)
In-Reply-To: <20251216170641.250494-1-alice.munduruca@canonical.com>
Alice C. Munduruca wrote:
> 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>
Cc: original author cbulinaru@gmail.com in case we're overlooking a
reason for the split between rtattr_add_str and rtattr_add_strsz.
the first avoids the \0 and is used for IFLA_NAME. Device names are
guaranteed to fit into IFNAMSIZ, including the terminating \0.
> ---
> tools/testing/selftests/net/tap.c | 16 +++++-----------
> 1 file changed, 5 insertions(+), 11 deletions(-)
>
> diff --git a/tools/testing/selftests/net/tap.c b/tools/testing/selftests/net/tap.c
> index 247c3b3ac1c9..51a209014f1c 100644
> --- a/tools/testing/selftests/net/tap.c
> +++ b/tools/testing/selftests/net/tap.c
> @@ -56,18 +56,12 @@ static void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
> static struct rtattr *rtattr_add_str(struct nlmsghdr *nh, unsigned short type,
> const char *s)
> {
> - struct rtattr *rta = rtattr_add(nh, type, strlen(s));
> + unsigned int strsz = strlen(s) + 1;
> + struct rtattr *rta;
>
> - memcpy(RTA_DATA(rta), s, strlen(s));
> - return rta;
> -}
> -
> -static struct rtattr *rtattr_add_strsz(struct nlmsghdr *nh, unsigned short type,
> - const char *s)
> -{
> - struct rtattr *rta = rtattr_add(nh, type, strlen(s) + 1);
> + rta = rtattr_add(nh, type, strsz);
>
> - strcpy(RTA_DATA(rta), s);
> + memcpy(RTA_DATA(rta), s, strsz);
> return rta;
> }
>
> @@ -119,7 +113,7 @@ static int dev_create(const char *dev, const char *link_type,
>
> link_info = rtattr_begin(&req.nh, IFLA_LINKINFO);
>
> - rtattr_add_strsz(&req.nh, IFLA_INFO_KIND, link_type);
> + rtattr_add_str(&req.nh, IFLA_INFO_KIND, link_type);
>
> if (fill_info_data) {
> info_data = rtattr_begin(&req.nh, IFLA_INFO_DATA);
> --
> 2.48.1
>
prev parent reply other threads:[~2025-12-16 19:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-16 17:06 [PATCH net v3] selftests: net: fix "buffer overflow detected" for tap.c Alice C. Munduruca
2025-12-16 19:54 ` Willem de Bruijn [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=willemdebruijn.kernel.311f094b4d393@gmail.com \
--to=willemdebruijn.kernel@gmail.com \
--cc=alice.munduruca@canonical.com \
--cc=cbulinaru@gmail.com \
--cc=cengiz.can@canonical.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).