Re: stackinit unit test failures on m68k

From: Kees Cook
Date: Tue Feb 27 2024 - 17:19:17 EST


On Mon, Feb 12, 2024 at 09:34:02AM +0100, Geert Uytterhoeven wrote:
> Hi Günter,
>
> On Mon, Feb 12, 2024 at 12:06 AM Guenter Roeck <linux@xxxxxxxxxxxx> wrote:
> > I see the following stackinit unit test failures on m68k when running
> > the q800 emulation.
> >
> > # test_char_array_zero: ASSERTION FAILED at lib/stackinit_kunit.c:333
> > Expected stackinit_range_contains(fill_start, fill_size, target_start, target_size) to be true, but is false
> > stack fill missed target!? (fill 16 wide, target offset by -12)
> >
> > # test_char_array_none: ASSERTION FAILED at lib/stackinit_kunit.c:343
> > Expected stackinit_range_contains(fill_start, fill_size, target_start, target_size) to be true, but is false
> > stack fill missed target!? (fill 16 wide, target offset by -12)
> >
> > Do you happen to know if this a problem with the test, with m68k, or maybe
> > with the configuration ? My configuration is based on mac_defconfig with
> > various test options enabled. I use gcc 11.4 to build the image. I tried
> > with qemu v8.1 and v8.2.
>
> Thanks, I see the same failures in the logs of my last testrun on ARAnyM, too.
> I haven't looked into the details yet.
>
> Only two failures does look like a nice improvement, compared to the
> previous time I ran that test ;-)
>
> https://lore.kernel.org/all/CAMuHMdX_g1tbiUL9PUQdqaegrEzCNN3GtbSvSBFYAL4TzvstFg@xxxxxxxxxxxxxx

This is complaining that the stack frames across subsequent calls to the
same leaf function don't end up putting the same variable in the same
place.

It's a rather difficult set of macros used try many different
combinations, but it's specifically talking about the "leaf_..."
function at line 208 of lib/stackinit_kunit.c. This test passes for all
the integral types, but seems to fail for a character array.

It is basically doing this:

static void *fill_start, *target_start;
static size_t fill_size, target_size;

static noinline int leaf_char_array_none(unsigned long sp, bool fill,
unsigned char *arg)
{
char buf[32];
unsigned char var[16];

target_start = &var;
target_size = sizeof(var);
/*
* Keep this buffer around to make sure we've got a
* stack frame of SOME kind...
*/
memset(buf, (char)(sp & 0xff), sizeof(buf));
/* Fill variable with 0xFF. */
if (fill) {
fill_start = &var;
fill_size = sizeof(var);
memset(fill_start,
(char)((sp & 0xff) | forced_mask),
fill_size);
}

/* Silence "never initialized" warnings. */
do_nothing_char_array(var);

/* Exfiltrate "var". */
memcpy(check_buf, target_start, target_size);

return (int)buf[0] | (int)buf[sizeof(buf) - 1];
}

and it's called as:


ignored = leaf_char_array_none((unsigned long)&ignored, 1, zero);
...
ignored = leaf_char_array_none((unsigned long)&ignored, 0, zero);

The first call remembers where "var" is in the stack frame via the
fill_start assignment, and the second call records where "var" is via
the target_start assignment.

The complaint is that it _changes_ between the two calls. ... Oh, I
think I see what's happened. Between the two calls, the stack grows (and
is for some reason not reclaimed) due to the KUNIT checks between the two
leaf calls. Yes, moving that fixes it.

I'll send a patch!

-Kees

--
Kees Cook