-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[libc] add checksum for jmpbuf #101110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SchrodingerZhu
wants to merge
4
commits into
llvm:main
Choose a base branch
from
SchrodingerZhu:libc/jmpbuf-chksum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[libc] add checksum for jmpbuf #101110
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===-- Implementation header for jmpbuf checksum ---------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_SETJMP_CHECKSUM_H | ||
#define LLVM_LIBC_SRC_SETJMP_CHECKSUM_H | ||
|
||
#include "src/__support/hash.h" | ||
#include "src/__support/macros/attributes.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/setjmp/setjmp_impl.h" | ||
#include "src/stdlib/abort.h" | ||
#include "src/unistd/write.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
namespace jmpbuf { | ||
using HashState = internal::HashState; | ||
// Initial values generated by | ||
// https://www.random.org/cgi-bin/randbyte?nbytes=48&format=h | ||
// These values are only used for overlay targets. | ||
LIBC_INLINE_VAR uint64_t register_mangle_cookie = 0xdf8a883867040cbc; | ||
LIBC_INLINE_VAR uint64_t checksum_mangle_cookie = 0x9ed4fe406ebe9cf9; | ||
LIBC_INLINE_VAR uint64_t randomness[4] = { | ||
0x83b9df7dddf5ab3d, | ||
0x06c931cca75e15c6, | ||
0x08280ec9e9a778bf, | ||
0x111f67f4aafc9276, | ||
}; | ||
|
||
LIBC_INLINE int update_checksum(__jmp_buf *buf) { | ||
HashState state{ | ||
randomness[0], | ||
randomness[1], | ||
randomness[2], | ||
randomness[3], | ||
}; | ||
state.update(buf, offsetof(__jmp_buf, __chksum)); | ||
buf->__chksum = state.finish() ^ checksum_mangle_cookie; | ||
return 0; | ||
} | ||
|
||
LIBC_INLINE void verify(const __jmp_buf *buf) { | ||
HashState state{ | ||
randomness[0], | ||
randomness[1], | ||
randomness[2], | ||
randomness[3], | ||
}; | ||
state.update(buf, offsetof(__jmp_buf, __chksum)); | ||
auto chksum = state.finish() ^ checksum_mangle_cookie; | ||
if (chksum != buf->__chksum) { | ||
constexpr char MSG[] = "jump buffer corrupted\n"; | ||
LIBC_NAMESPACE::write(2, MSG, sizeof(MSG) - 1); | ||
LIBC_NAMESPACE::abort(); | ||
} | ||
} | ||
|
||
} // namespace jmpbuf | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_SETJMP_CHECKSUM_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it standard to use fixed keys in these implementations?
xor
encryption is already quite weak, is there a way we can initialize these using some form of entropy on systems that can support that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I plan to populate these keys on full build during startup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that sounds reasonable. For other targets, like baremetal, we may want to think about other means of initialization, but that's out of scope for this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the initialization fails, we should make that as obvious as possible since a fixed key is as bad as no key at all. I'd recommend making these initial values
0xAAAAAAAAAAAAAAAA
,0xBBBBBBBBBBBBBBBB
, and so on so that it's clear what has happened.