lib: do not call memcpy with sz zero and null pointer

Message ID 20220907150503.6212-1-henning.schild@siemens.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series lib: do not call memcpy with sz zero and null pointer |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail Compilation issues
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS

Commit Message

Henning Schild Sept. 7, 2022, 3:05 p.m. UTC
  There is no point in such a call and UBSan complains about a call to
memcpy with a null pointer as second arg.

When building with -Db_sanitize=undefined, Clang gives the following
warning
../lib/bpf/bpf_load.c:37:20: runtime error: null pointer passed as
	argument 2, which is declared to never be null

A check of the sz before calling memcpy fixes that.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
---
 lib/bpf/bpf_load.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Thomas Monjalon Sept. 21, 2022, 1:06 p.m. UTC | #1
07/09/2022 17:05, Henning Schild:
> There is no point in such a call and UBSan complains about a call to
> memcpy with a null pointer as second arg.
> 
> When building with -Db_sanitize=undefined, Clang gives the following
> warning
> ../lib/bpf/bpf_load.c:37:20: runtime error: null pointer passed as
> 	argument 2, which is declared to never be null
> 
> A check of the sz before calling memcpy fixes that.
> 
> Signed-off-by: Henning Schild <henning.schild@siemens.com>
> ---
> --- a/lib/bpf/bpf_load.c
> +++ b/lib/bpf/bpf_load.c
> @@ -34,7 +34,8 @@ bpf_load(const struct rte_bpf_prm *prm)
>  
>  	memcpy(&bpf->prm, prm, sizeof(bpf->prm));
>  
> -	memcpy(buf + bsz, prm->xsym, xsz);
> +	if (xsz)
> +		memcpy(buf + bsz, prm->xsym, xsz);*

I assume I can safely change it to
	if (xsz > 0)
to comply with the code style.

Applied, thanks.
  
Henning Schild Sept. 21, 2022, 1:57 p.m. UTC | #2
Am Wed, 21 Sep 2022 15:06:12 +0200
schrieb Thomas Monjalon <thomas@monjalon.net>:

> 07/09/2022 17:05, Henning Schild:
> > There is no point in such a call and UBSan complains about a call to
> > memcpy with a null pointer as second arg.
> > 
> > When building with -Db_sanitize=undefined, Clang gives the following
> > warning
> > ../lib/bpf/bpf_load.c:37:20: runtime error: null pointer passed as
> > 	argument 2, which is declared to never be null
> > 
> > A check of the sz before calling memcpy fixes that.
> > 
> > Signed-off-by: Henning Schild <henning.schild@siemens.com>
> > ---
> > --- a/lib/bpf/bpf_load.c
> > +++ b/lib/bpf/bpf_load.c
> > @@ -34,7 +34,8 @@ bpf_load(const struct rte_bpf_prm *prm)
> >  
> >  	memcpy(&bpf->prm, prm, sizeof(bpf->prm));
> >  
> > -	memcpy(buf + bsz, prm->xsym, xsz);
> > +	if (xsz)
> > +		memcpy(buf + bsz, prm->xsym, xsz);*  
> 
> I assume I can safely change it to
> 	if (xsz > 0)
> to comply with the code style.

Sure, thanks!

Henning

> 
> Applied, thanks.
> 
>
  

Patch

diff --git a/lib/bpf/bpf_load.c b/lib/bpf/bpf_load.c
index 0c4ac7be6c55..48d3d80ac3e3 100644
--- a/lib/bpf/bpf_load.c
+++ b/lib/bpf/bpf_load.c
@@ -34,7 +34,8 @@  bpf_load(const struct rte_bpf_prm *prm)
 
 	memcpy(&bpf->prm, prm, sizeof(bpf->prm));
 
-	memcpy(buf + bsz, prm->xsym, xsz);
+	if (xsz)
+		memcpy(buf + bsz, prm->xsym, xsz);
 	memcpy(buf + bsz + xsz, prm->ins, insz);
 
 	bpf->prm.xsym = (void *)(buf + bsz);