Inappropriate implementation of LTC_CLEAN_STACK using burn_stack() api #486
Labels
No labels
bug
duplicate
enhancement
feature
help wanted
improve API
invalid
needs documentation
not-planned
rtfm
stale
support
wishlist
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libtom/libtomcrypt#486
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
Some parts of
LibTomCrypthave been imported in an open source projectOP-TEE[1] for implementing crypto in software (imported code can be found here [2]). But recently we have enabledLTC_CLEAN_STACKso thatLibTomCryptwipes out key material and other sensitive data once no longer used.Given stack space is quiet expensive for
optee_osand currently its limited to 2k only. So while testing on ARM64 machine withLTC_CLEAN_STACKenabled,burn_stack()behaved inappropriately burning stack space 2 times the actual requirement leading to corruption of stack canaries.Looking deeper into
burn_stack()api [3], it seems to be a recursive function which assumes that only 32 bytes of stack space is utilized in every recursive call. But this assumption is incorrect as it doesn't take into account the usage of stack space during function calls. Specifically for function return pointer, stack frame pointer, any padding compiler introduces etc.[1] https://optee.readthedocs.io/
[2] https://github.com/OP-TEE/optee_os/tree/master/core/lib/libtomcrypt/src
[3] https://github.com/libtom/libtomcrypt/blob/develop/src/misc/burn_stack.c
Steps to Reproduce
Simply add a debug print to dump address of 32 bytes buffer kept on stack in
burn_stack()api as follows:Following are
buf addrdumps which are taken on ARM64 machine whereburn_stack()api is used to clean stack for_sha512_compress()api. Size to be cleared: 724 bytes but actual size cleared from below dump: 1472 bytes.Version
This could be reproduced on latest upstream
developbranch.Suggestion
We would suggest to clean stack in corresponding function only where it is being used. As it seems one can't assume anything about what stack space has been consumed by the compiler for a given function call.
Looking at reason behind introduction of
burn_stack()api [1], it seems like in case of a lot of stack variables, clearing every variable inside function makes it look bit messier and may be bit slow due to multiplezeromemcalls.So I have tried to come up with below approach to clear stack in corresponding function only and avoid multiple
zeromemcalls:Approach 1
Above approach works if compiler allocates stack variables at contiguous stack memory locations which might not be true in case padding involved which could lead to some stack memory uncleaned (equal to size of padding).
Approach 2
Multiple
zeromemcalls:I would be happy to hear any feedback/suggestions regarding which of above approach seems most suitable.
[1]
github.com/libtom/libtomcrypt@3fb0eea01b/changes (L1283)You could also stick everything in a struct...
I prefer "Approach 2" as it keeps it simple without making any assumptions about the stack layout.
Regarding performance I think either approach will beat the old burnstack approach.
@ksherlock Performance wise approach suggested by you looks better than "Approach 2". The only thing that doesn't looks good is a
definefor every variable which makes function look a bit messier even if compared with "Approach 2".Any more preferences among above 3 approaches (Approach 3: suggested by @ksherlock)? Or any further suggestions to address this issue?
i like the zeromem struct approach best. i wonder why there's not been any comment by @sjaeckel or @karel-m since this seems to be a serious issue.
because I'm not certain yet on how to solve it best...
I like approach 3, but prefer 2 for its simplicity and no macro magic
I already thought about an approach 4 where we leave the
burn_stack()approach but increase the buffer size, so the overshooting isn't that severe.What should be the appropriate buffer size? Since we can't have too big buffer for APIs that only need to clean few bytes and too small buffer that caused this overshoot. Overall this approach still seems to be fragile.
I prefer #2 for its simplicity.
@karel-m 2 fine for you as well?
I do not use
LTC_CLEAN_STACKin my builds. Approach 2 is fine.Thanks everyone for your feedback. Will use "Approach 2" to create a fix patch.
Does anyone think it's worth to keep the old approach alive? (probably as
LTC_BURN_STACK)if the initial analysis posted by OP is correct, the old approach uses undefined behaviour. i don't think that should be kept.
explicit zeromem calls for every single (sensitive) local variable seems to be an approach that leads to bigger functions (both in code as in binary), as well as error-prone because the programmer needs to manually take care of every single variable. if we instead stuff everything into a struct, there's only a single call to zeromem, and one can simply put new vars into the struct and forget about adding another call with the right arguments.
It seems likely that we can't get agreement on one of above discussed approaches. So how about following simple change to use variable length array instead of recursive call in
burn_stackapi?It seems to resolve stack overflow issue and does stack clean operation too. This change is something that was suggested by @daniel-thompson.
Except that it still relies on undefined behavior.
using vla's is the worst possible choice. there have been countless numbers of CVE's due to it.
also it doesn't make the code simpler at all, now every call site has to calculate correctly the length passed, which asks for trouble (whenever you add a variable, you have to adjust the burn_stack calls).
@rofl0r: I agree that in general VLAs should be avoided... but I don't agree that they are the worst possible choice since I think that spot is already occupied by the existing burn_stack() code ;-) !
I've got no skin in the game here but FWIW I like approach 3 best, either with macros or just making it such a common idiomatic construct across the library that nobody minds giving it a single letter variable name.
@rofl0r Agree but with a minimal change like VLA, we could improve the
burn_stack()API atleast. It was just an effort to make current code workable rather than making developer's life easier.Anyway, let me try to work on a common variable declaration construct that may make
approach 3look more better.Sorry for the delay, but I was busy in the last weeks.
If that's the case then I also think that the entire
burn_stack()approach as it is implemented now should be removed, but can you please explain why this is UB?The VLA approach would indeed be the least intrusive change, but then it uses VLA's which officially exist only since c99 ... so a clear no.
that's already the case, so this wouldn't change ;-)
but that's also one of the main reasons why I like approach 3, automatic determination of the size of what to clear.
I'm looking forward to an improved version of approach 3!
Since we've disabled the current implementation and nobody stepped up yet to provide one of the discussed approaches, this issue will stay open, but is not planned to be implemented.