Re: [BUG] arch/x86/kvm/x86.c: In function ‘prepare_emulation_failure_exit’: error: use of NULL ‘data’ where non-null expected

From: Sean Christopherson
Date: Fri Jul 19 2024 - 15:01:42 EST


On Fri, Jul 19, 2024, Mirsad Todorovac wrote:
> Hi, all!
>
> On linux-stable 6.10 vanilla tree, another NULL pointer is passed, which was detected
> by the fortify-string.h mechanism.
>
> arch/x86/kvm/x86.c
> ==================
>
> 13667 kvm_prepare_emulation_failure_exit(vcpu);
>
> calls
>
> 8796 __kvm_prepare_emulation_failure_exit(vcpu, NULL, 0);
>
> which calls
>
> 8790 prepare_emulation_failure_exit(vcpu, data, ndata, NULL, 0);
>
> Note here that data == NULL and ndata = 0.
>
> again data == NULL and ndata == 0, which passes unchanged all until
>
> 8773 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, ndata * sizeof(data[0]));

My reading of the C99 is that KVM's behavior is fine.

Where an argument declared as size_t n specifies the length of the array for a
function, n can have the value zero on a call to that function. Unless explicitly stated
otherwise in the description of a particular function in this subclause, pointer arguments
on such a call shall still have valid values, as described in 7.1.4. On such a call, a
function that locates a character finds no occurrence, a function that compares two
character sequences returns zero, and a function that copies characters copies zero
characters.

If the function copies zero characters, then there can't be a store to the NULL
pointer, and if there's no store, there's no NULL pointer explosion.

I suppose arguably one could argue the builtin memcpy() could deliberately fail
on an invalid pointer, but that'd be rather ridiculous.