* [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* Re: [PATCH] selftests: net: ipmr: Avoid memcpy() from NULL in nl_add_rtattr()
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
1 sibling, 0 replies; 4+ messages in thread
From: Hangbin Liu @ 2026-09-01 8:57 UTC (permalink / raw)
To: Chaithanya Lagisetty
Cc: David Ahern, Ido Schimmel, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Shuah Khan,
Kuniyuki Iwashima, netdev, linux-kselftest, linux-kernel
On Tue, Sep 01, 2026 at 07:13:53AM +0000, Chaithanya Lagisetty wrote:
> 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
>
Reviewed-by: Hangbin Liu <liuhangbin@kylinos.cn>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] selftests: net: ipmr: Avoid memcpy() from NULL in nl_add_rtattr()
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
1 sibling, 1 reply; 4+ messages in thread
From: Kuniyuki Iwashima @ 2026-09-01 23:49 UTC (permalink / raw)
To: Chaithanya Lagisetty
Cc: David Ahern, Ido Schimmel, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Shuah Khan, netdev,
linux-kselftest, linux-kernel
On Tue, Sep 1, 2026 at 12:14 AM Chaithanya Lagisetty
<nagachaithanya9911@gmail.com> wrote:
>
> 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.
It's not flagged since NULL is passed via nl_add_rtattr(), not directly
to memcpy().
Also, the behaviour will be well-defined with N3322 in C2y.
Given there is no real harm and we do not bother revisiting this,
there is no need to change that.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] selftests: net: ipmr: Avoid memcpy() from NULL in nl_add_rtattr()
2026-09-01 23:49 ` Kuniyuki Iwashima
@ 2026-09-02 11:52 ` Chaithanya Lagisetty
0 siblings, 0 replies; 4+ messages in thread
From: Chaithanya Lagisetty @ 2026-09-02 11:52 UTC (permalink / raw)
To: kuniyu
Cc: hangbin.liu, liuhangbin, dsahern, idosch, davem, edumazet, kuba,
pabeni, horms, shuah, netdev, linux-kselftest, linux-kernel
On Tue, Sep 01, 2026 at 11:49:46PM +0000, Kuniyuki Iwashima wrote:
> It's not flagged since NULL is passed via nl_add_rtattr(), not directly
> to memcpy().
>
> Also, the behaviour will be well-defined with N3322 in C2y.
>
> Given there is no real harm and we do not bother revisiting this,
> there is no need to change that.
You are right, and thanks for the correction. The NULL is laundered
through the function parameter, so it is not diagnosed at the call site
the way my changelog claimed. Together with N3322 making this
well-defined, I agree there is nothing worth changing here.
Dropping the patch.
Hangbin, thanks for the review as well; sorry for the churn.
Thanks,
Chaithanya
^ permalink raw reply [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