Netdev List
 help / color / mirror / Atom feed
* [PATCH] selftests: net: ipmr: Avoid memcpy() from NULL in nl_add_rtattr()
@ 2026-09-01  7:13 Chaithanya Lagisetty
  2026-09-01  8:57 ` Hangbin Liu
  2026-09-01 23:49 ` Kuniyuki Iwashima
  0 siblings, 2 replies; 4+ messages in thread
From: Chaithanya Lagisetty @ 2026-09-01  7:13 UTC (permalink / raw)
  To: David Ahern, Ido Schimmel, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Shuah Khan
  Cc: Kuniyuki Iwashima, netdev, linux-kselftest, linux-kernel,
	Chaithanya Lagisetty

nl_add_rtattr() unconditionally does memcpy(RTA_DATA(rta), data, len).
For zero-length attributes the callers pass data == NULL and len == 0,
for example the RTA_PREFSRC attribute added for proxy MFC entries:

	if (mfc_attr->proxy)
		rta = nl_add_rtattr(nlmsg, rta, RTA_PREFSRC, NULL, 0);

Passing a NULL pointer to memcpy() is undefined behaviour even when the
length is zero, because its source parameter is marked
__attribute__((nonnull)); it is flagged by fortify/-Wnonnull.

Only call memcpy() when len is non-zero.

Fixes: 05068eaa67b2 ("selftest: net: Add basic functionality tests for ipmr.")
Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
 tools/testing/selftests/net/forwarding/ipmr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/forwarding/ipmr.c b/tools/testing/selftests/net/forwarding/ipmr.c
index 9cd9f70de132..d3e26341821c 100644
--- a/tools/testing/selftests/net/forwarding/ipmr.c
+++ b/tools/testing/selftests/net/forwarding/ipmr.c
@@ -120,7 +120,8 @@ static struct rtattr *nl_add_rtattr(struct nlmsghdr *nlmsg, struct rtattr *rta,
 
 	rta->rta_type = type;
 	rta->rta_len = RTA_LENGTH(len);
-	memcpy(RTA_DATA(rta), data, len);
+	if (len)
+		memcpy(RTA_DATA(rta), data, len);
 
 	nlmsg->nlmsg_len += NLMSG_ALIGN(rta->rta_len);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-02 11:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-01  7:13 [PATCH] selftests: net: ipmr: Avoid memcpy() from NULL in nl_add_rtattr() Chaithanya Lagisetty
2026-09-01  8:57 ` Hangbin Liu
2026-09-01 23:49 ` Kuniyuki Iwashima
2026-09-02 11:52   ` Chaithanya Lagisetty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox