Skip to content

Instantly share code, notes, and snippets.

@acmorrow
Created October 18, 2016 20:39
Show Gist options
  • Save acmorrow/782a1cb1038245ea96deba25e376c81d to your computer and use it in GitHub Desktop.
Save acmorrow/782a1cb1038245ea96deba25e376c81d to your computer and use it in GitHub Desktop.
Manual stack canary
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