Thread (12 messages) 12 messages, 6 authors, 2020-01-10

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

From: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>
Date: 2020-01-06 22:25:32
Also in: bpf, lkml

On Sat, 2020-01-04 at 09:49 +0900, Andy Lutomirski wrote:
quoted
On Jan 4, 2020, at 8:47 AM, KP Singh [off-list ref] wrote:

From: KP Singh [off-list ref]

The image for the BPF trampolines is allocated with
bpf_jit_alloc_exe_page which marks this allocated page executable. This
means that the allocated memory is W and X at the same time making it
susceptible to WX based attacks.

Since the allocated memory is shared between two trampolines (the
current and the next), 2 pages must be allocated to adhere to W^X and
the following sequence is obeyed where trampolines are modified:
Can we please do better rather than piling garbage on top of garbage?
quoted
- Mark memory as non executable (set_memory_nx). While module_alloc for
x86 allocates the memory as PAGE_KERNEL and not PAGE_KERNEL_EXEC, not
all implementations of module_alloc do so
How about fixing this instead?
quoted
- Mark the memory as read/write (set_memory_rw)
Probably harmless, but see above about fixing it.
quoted
- Modify the trampoline
Seems reasonable. It’s worth noting that this whole approach is suboptimal:
the “module” allocator should really be returning a list of pages to be
written (not at the final address!) with the actual executable mapping to be
materialized later, but that’s a bigger project that you’re welcome to ignore
for now.  (Concretely, it should produce a vmap address with backing pages but
with the vmap alias either entirely unmapped or read-only. A subsequent healer
would, all at once, make the direct map pages RO or not-present and make the
vmap alias RX.)
quoted
- 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.

As far as I know, all architectures can do this with a single flush without
races  x86 certainly can. The module freeing code gets this sequence right.
Please reuse its mechanism or, if needed, export the relevant interfaces.
So if I understand this right, some trampolines have been added that are
currently set as RWX at modification time AND left that way during runtime? The
discussion on the order of set_memory_() calls in the commit message made me
think that this was just a modification time thing at first.

Also, is there a reason you couldn't use text_poke() to modify the trampoline
with a single flush?

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help