diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-09-14 21:30:11 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-09-14 21:30:11 +0300 |
| commit | cabbf335824db0df835e4c541c19d3803ce38454 (patch) | |
| tree | e7397d2b6637545b8e18f26135e3ad2a0a2fbe1e /common/canary.c | |
| parent | 025778e89e7d0fe3cd1ad53537c9cc6f3cd819cc (diff) | |
| download | kmi-cabbf335824db0df835e4c541c19d3803ce38454.tar.gz kmi-cabbf335824db0df835e4c541c19d3803ce38454.zip | |
add canary to kernel stack
Diffstat (limited to 'common/canary.c')
| -rw-r--r-- | common/canary.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/common/canary.c b/common/canary.c new file mode 100644 index 0000000..9fd89ff --- /dev/null +++ b/common/canary.c @@ -0,0 +1,39 @@ +#include <apos/canary.h> +#include <apos/mem.h> + +/** + * @file canary.c + * Kernel stack canary implementation. + */ + +/** Typedef for canary value type. */ +typedef uint32_t canary_t; + +/** Canary magic value. */ +static const canary_t canary = 0xb00b1e5; + +/** + * Helper for calculating the canary location of \p t. + * + * @param t \ref tcb to calculate canary position of. + * @return Pointer to canary location, that is bottom of stack. + * \todo This assumes that all stacks grow downwards. Unlikely to ever be + * ported to a platform that doesn't abide by this, but keep it in mind anyway. + */ +static canary_t *get_canary(struct tcb *t) +{ + size_t s = order_size(KERNEL_STACK_PAGE_ORDER); + return (canary_t *)align_down((uintptr_t)t, s); +} + +void set_canary(struct tcb *t) +{ + canary_t *c = get_canary(t); + *c = canary; +} + +bool check_canary(struct tcb *t) +{ + canary_t *c = get_canary(t); + return *c != canary; +} |
