Skip to contentSkip to conversation

tools/nolibc: tests: add test for -fstack-protector

PATCH Alexey Dobriyan 3 participants 9 messages Mar 26, 2023 – Mar 28, 2023

lore
Share & tools
lore
#0 lore raw
Quoted 6 lines · Willy Tarreau

#if defined(__clang__)
__attribute__((optnone))
#elif defined(__GNUC__)
__attribute__((optimize("O0")))
#endif
static int smash_stack(void)

Quoted 8 lines
{
	char buf[100];

	for (size_t i = 0; i < 200; i++)
		buf[i] = 'P';

	return 1;
}

If you want to corrupt the stack, corrupt the stack!

asm(
".globl f\n"
"f:\n"
"movq $0, (%rsp)\n"
"ret\n"
".type f,@function\n"
".size f,.-f"
);

No problems with optimisation levels.

#1 lore raw

Mar 26, 2023 13:30:21 Alexey Dobriyan <[email protected]>:

Quoted 10 lines
Quoted 9 lines · Willy Tarreau

#if defined(__clang__)
__attribute__((optnone))
#elif defined(__GNUC__)
__attribute__((optimize("O0")))
#endif
static int smash_stack(void)
{
    char buf[100];

Quoted 7 lines
Quoted 5 lines

    for (size_t i = 0; i < 200; i++)
        buf[i] = 'P';

    return 1;
}

If you want to corrupt the stack, corrupt the stack!

I do!

Quoted 9 lines

asm(
".globl f\n"
"f:\n"
"movq $0, (%rsp)\n"
"ret\n"
".type f,@function\n"
".size f,.-f"
);

Quoted 1 line

No problems with optimisation levels.

Wouldn't this be architecture-specific?

#2 lore raw
Quoted 31 lines · Thomas Weißschuh

Mar 26, 2023 13:30:21 Alexey Dobriyan <[email protected]>:

Quoted 10 lines
Quoted 9 lines · Willy Tarreau

#if defined(__clang__)
__attribute__((optnone))
#elif defined(__GNUC__)
__attribute__((optimize("O0")))
#endif
static int smash_stack(void)
{
    char buf[100];

Quoted 7 lines
Quoted 5 lines

    for (size_t i = 0; i < 200; i++)
        buf[i] = 'P';

    return 1;
}

If you want to corrupt the stack, corrupt the stack!

I do!

Quoted 8 lines

asm(
".globl f\n"
"f:\n"
"movq $0, (%rsp)\n"
"ret\n"
".type f,@function\n"
".size f,.-f"
);

Quoted 3 lines
Quoted 1 line
Quoted 1 line

No problems with optimisation levels.

Wouldn't this be architecture-specific?

Yes it would. I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

Willy

#3 lore raw
Quoted 32 lines · Willy Tarreau
Quoted 31 lines · Thomas Weißschuh

Mar 26, 2023 13:30:21 Alexey Dobriyan <[email protected]>:

Quoted 10 lines
Quoted 9 lines · Willy Tarreau

#if defined(__clang__)
__attribute__((optnone))
#elif defined(__GNUC__)
__attribute__((optimize("O0")))
#endif
static int smash_stack(void)
{
    char buf[100];

Quoted 7 lines
Quoted 5 lines

    for (size_t i = 0; i < 200; i++)
        buf[i] = 'P';

    return 1;
}

If you want to corrupt the stack, corrupt the stack!

I do!

Quoted 8 lines

asm(
".globl f\n"
"f:\n"
"movq $0, (%rsp)\n"
"ret\n"
".type f,@function\n"
".size f,.-f"
);

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

No problems with optimisation levels.

Wouldn't this be architecture-specific?

Yes it would.

Which is OK. Corrupting return address is very arch-specific.

Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

#4 lore raw
Quoted 6 lines · Alexey Dobriyan
Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

Sure but it's for a regtest which can easily be adjusted and its posrtability and ease of maintenance outweights its reliability, especially when in practice what the code does is what we want to test for. And if an extra zero needs to be added to the loop, it can be at a lower cost than maintaining arch-specific asm code.

Willy

#5 lore raw
Quoted 5 lines · Willy Tarreau
Quoted 4 lines · Alexey Dobriyan
Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

Quoted 8 lines
Quoted 2 lines

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

Sure but it's for a regtest which can easily be adjusted and its posrtability and ease of maintenance outweights its reliability, especially when in practice what the code does is what we want to test for. And if an extra zero needs to be added to the loop, it can be at a lower cost than maintaining arch-specific asm code.

For the record, I disagree. Use volatile writes at least.

#6 lore raw
Quoted 9 lines · Alexey Dobriyan
Quoted 8 lines · Willy Tarreau
Quoted 6 lines · Alexey Dobriyan
Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

Quoted 7 lines
Quoted 5 lines

Sure but it's for a regtest which can easily be adjusted and its posrtability and ease of maintenance outweights its reliability, especially when in practice what the code does is what we want to test for. And if an extra zero needs to be added to the loop, it can be at a lower cost than maintaining arch-specific asm code.

For the record, I disagree. Use volatile writes at least.

Yeah I agree on the volatile one.

Willy

#7 lore raw
Quoted 16 lines · Willy Tarreau
Quoted 15 lines · Alexey Dobriyan
Quoted 13 lines · Willy Tarreau
Quoted 6 lines · Alexey Dobriyan
Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

Sure but it's for a regtest which can easily be adjusted and its posrtability and ease of maintenance outweights its reliability, especially when in practice what the code does is what we want to test for. And if an extra zero needs to be added to the loop, it can be at a lower cost than maintaining arch-specific asm code.

Quoted 3 lines
Quoted 1 line

For the record, I disagree. Use volatile writes at least.

Yeah I agree on the volatile one.

Sounds good.

How do we proceed?

Do I send a new revision?
Will you fix up the series?
Will someone create a new patch? If so who?

Thomas

#8 lore raw

Hi Thomas,

Quoted 21 lines · Thomas Weißschuh
Quoted 16 lines · Willy Tarreau
Quoted 15 lines · Alexey Dobriyan
Quoted 13 lines · Willy Tarreau
Quoted 6 lines · Alexey Dobriyan
Quoted 3 lines

I'm not seeing any issue with your approach instead, let's keep it as-is for now (also it does what the stack protector is supposed to catch anyway).

There are no guarantess about stack layout and dead writes. The test doesn't corrupt stack reliably, just 99.99% reliably.

Sure but it's for a regtest which can easily be adjusted and its posrtability and ease of maintenance outweights its reliability, especially when in practice what the code does is what we want to test for. And if an extra zero needs to be added to the loop, it can be at a lower cost than maintaining arch-specific asm code.

Quoted 3 lines
Quoted 1 line

For the record, I disagree. Use volatile writes at least.

Yeah I agree on the volatile one.

Quoted 7 lines

Sounds good.

How do we proceed?

Do I send a new revision?
Will you fix up the series?
Will someone create a new patch? If so who?

Please just send an additional patch to be applied on top of the existing series that turns this to volatile, and add a Reported-by: with Alexey's e-mail.

You may even verify that once you do this it's safe to remove the optimize attributes.

Thank you!
Willy