diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-05-24 13:24:27 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-05-24 18:13:43 +0300 |
| commit | bc600ecc3bdf0f189861dfb840f70c2339a7a853 (patch) | |
| tree | d9840b1dc1b865442a028c03ad7109ac67894f6e /src/canary.c | |
| parent | 6a7073e5f262db9a4578ff00b5b28e34335564ce (diff) | |
| download | kmi-bc600ecc3bdf0f189861dfb840f70c2339a7a853.tar.gz kmi-bc600ecc3bdf0f189861dfb840f70c2339a7a853.zip | |
rename common to src
+ I keep starting to type src and wondering why autocomplete won't work,
I guess src is just uncounciously a better name
Diffstat (limited to 'src/canary.c')
| -rw-r--r-- | src/canary.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/canary.c b/src/canary.c new file mode 100644 index 0000000..4f1f8ba --- /dev/null +++ b/src/canary.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#include <kmi/canary.h> +#include <kmi/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; +} |
