Re: [PATCH bpf-next] bpf: Make trampolines W^X

From: Justin Capella
Date: Sat Jan 04 2020 - 20:19:29 EST

I'm guessing 2 pages are used to allow for different protections? Does
only the first page's protections need to be changed? Is that

+ set_memory_nx((unsigned long)image, 1);
+ set_memory_rw((unsigned long)image, 1);

+ set_memory_ro((unsigned long)new_image, 1);
+ set_memory_x((unsigned long)new_image, 1);


+ void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE;
+ void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE

> > - Mark the memory as read-only (set_memory_ro)
> > - Mark the memory as executable (set_memory_x)
> No, thanks. Thereâs very little excuse for doing two IPI flushes when one would suffice.

If there were checks between these steps to verify the trampoline
wasn't tampered with while the page was writable it would make sense
to do so before enabling execution.

Could some of these int's be unsigned to be extra cautious?

One last thought, if the extra checks are implemented, maybe comparing
against the old image prior to setting rw would be worthwhile?