Skip to content

Commit

Permalink
c18n: Compare pointers by converting them to integers first
Browse files Browse the repository at this point in the history
Strictly speaking, using >= to compare pointers to different trusted
frames is undefined behaviour.
  • Loading branch information
dpgao committed Oct 16, 2024
1 parent 4c342cb commit 7b227c8
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions libexec/rtld-elf/rtld_c18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ unwind_stack(struct jmp_args ret, void *rcsp, struct trusted_frame *target)

tf = get_trusted_stk();

if (!cheri_is_subset(tf, target) || tf->previous >= target) {
if (!cheri_is_subset(tf, target) ||
(ptraddr_t)tf->previous >= (ptraddr_t)target) {
rtld_fdprintf(STDERR_FILENO,
"c18n: Illegal unwind from %#p to %#p\n", tf, target);
abort();
Expand All @@ -961,7 +962,7 @@ unwind_stack(struct jmp_args ret, void *rcsp, struct trusted_frame *target)
cid = index_to_cid(index);
ospp = &table->entries[cid].stack;

if (*ospp > cur->osp) {
if ((ptraddr_t)*ospp > (ptraddr_t)cur->osp) {
rtld_fdprintf(STDERR_FILENO,
"c18n: Cannot unwind %s from %#p to %#p\n",
comparts.data[cid].name, *ospp, cur->osp);
Expand All @@ -970,9 +971,9 @@ unwind_stack(struct jmp_args ret, void *rcsp, struct trusted_frame *target)

*ospp = cur->osp;
cur = cur->previous;
} while (cur < target);
} while ((ptraddr_t)cur < (ptraddr_t)target);

if (cur != target) {
if ((ptraddr_t)cur != (ptraddr_t)target) {
rtld_fdprintf(STDERR_FILENO,
"c18n: Illegal unwind from %#p to %#p\n", cur, target);
abort();
Expand All @@ -983,7 +984,7 @@ unwind_stack(struct jmp_args ret, void *rcsp, struct trusted_frame *target)
* topmost trusted frame to restore the untrusted stack when it is
* popped.
*/
if (rcsp > *ospp) {
if ((ptraddr_t)rcsp > (ptraddr_t)*ospp) {
rtld_fdprintf(STDERR_FILENO,
"c18n: Cannot complete unwind %s from %#p to %#p, ",
"tf: %#p -> %#p\n", comparts.data[cid].name, rcsp, *ospp,
Expand Down

0 comments on commit 7b227c8

Please sign in to comment.