Created
October 18, 2016 20:39
-
-
Save acmorrow/782a1cb1038245ea96deba25e376c81d to your computer and use it in GitHub Desktop.
Manual stack canary
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
namespace { | |
class Canary { | |
public: | |
static constexpr size_t kSize = 1024; | |
explicit Canary(volatile unsigned char* const t) noexcept : _t(t) { | |
::memset(const_cast<unsigned char*>(_t), kBits, kSize); | |
_verify(); | |
} | |
~Canary() { | |
_verify(); | |
} | |
private: | |
static constexpr uint8_t kBits = 0xCD; | |
static constexpr size_t kChecksum = kSize * size_t(kBits); | |
void _verify() const noexcept { | |
invariant(std::accumulate(&_t[0], &_t[kSize], 0UL) == kChecksum); | |
} | |
const volatile unsigned char* const _t; | |
}; | |
} // namespace | |
Status ... { | |
volatile unsigned char* const cookie = static_cast<unsigned char *>(alloca(Canary::kSize)); | |
const Canary c(cookie); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment