Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Remove all variable-length arrays on the stack #45

Merged
merged 2 commits into from
Sep 24, 2018

Conversation

henrycg
Copy link
Collaborator

@henrycg henrycg commented Sep 24, 2018

This commit removes all stack-allocated VLAs.

Last week, @sadjad convinced me that stack-allocated arrays are dangerous: If an attacker can control the size of the allocated array, the attacker can allocate a huge array on the stack and crash the program using a stack overflow.

I don't think that this is a terribly pressing concern for libprio, but since MSVC doesn't support VLAs anyways, we might as well get rid of them entirely.

@rhelmer
Copy link
Contributor

rhelmer commented Sep 24, 2018

You can ignore the failed appveyor build, I haven't finished hooking that up yet in issue #42 (I didn't expect it to start trying heh)

@rhelmer
Copy link
Contributor

rhelmer commented Sep 24, 2018

I'll take a look over the patch tomorrow, I kicked off a Firefox tryserver CI run in the meantime:
https://treeherder.mozilla.org/#/jobs?repo=try&revision=941841840f590e6615bae6e131f114f8f6cbfbe6

Note that this picks up 2a49189 too - since these two changesets passed the Travis-CI tests I am not too worried, but this will at least exercise the Firefox integration code and unit test.

Copy link
Contributor

@rhelmer rhelmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to land as-is - I have a few questions and suggestions for the future, but the actual variable-length to fixed-length array conversion lgtm.

@@ -49,118 +52,116 @@ verify_full(void)
// Number of clients to simulate.
const int nclients = 10;

// New scope to avoid goto weirdness
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the cause of this comment (and the new scope) related to VLAs, or is this just coincidental?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be easier to see the change here as two separate commits in any case :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was related, but I'll move it into a separate commit for better readability.

In the earlier code, I had some VLAs defined in the middle of the function. This meant that there was a new stack frame created halfway down the function body and the cleanup label ended up in being placed in this new child stack frame. The PCHECK_C macro then wouldn't work in the first half of the function body (the parent stack frame), because the PCHECK_C macro contained a goto that was trying to jump into the child stack frame that didn't exist. Putting the new explicit scope in there meant that the cleanup label was in the parent stack frame and then PCHECK_C would work everywhere in the function...

@@ -192,6 +192,10 @@ verify_full(void)
free(for_server_a);
if (for_server_b)
free(for_server_b);
if (output)
Copy link
Contributor

@rhelmer rhelmer Sep 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is not enforced by the current style checker settings but it's safer to always use braces, the Mozilla style guide mentions the "dangling else" bug.

"Always brace controlled statements, even a single-line consequent of an if else else. This is redundant typically, but it avoids dangling else bugs, so it's safer at scale than fine-tuning."

The more common bug I see when braces aren't enforced around control structures though is the simpler "goto fail"-style bug, that is that a new patch author misreads the existing code and does something like:

- if (a)
-  doA();
+ if (a && b)
+  doA();
+  doB();

I don't think it's worth really worrying about in this patch since this style is already used elsewhere, I will make the case in another issue that this should be enforced and the code auto-formatted :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, enforcing this everywhere sounds good to me.

ptest/client_test.c Outdated Show resolved Hide resolved
@henrycg henrycg force-pushed the remove-variable-length-arrays branch from d2e3015 to 75f9b38 Compare September 24, 2018 14:37
@henrycg
Copy link
Collaborator Author

henrycg commented Sep 24, 2018

I just split the commit into two: one to remove the VLAs and one to fix the formatting.

@rhelmer rhelmer merged commit 02a81fb into mozilla:master Sep 24, 2018
@henrycg henrycg deleted the remove-variable-length-arrays branch October 4, 2018 15:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants