From: Hyeonggon Yoo <42.hyeyoo@gmail.com>
To: Song Liu <song@kernel.org>
Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
linux-mm@kvack.org, ast@kernel.org, daniel@iogearbox.net,
peterz@infradead.org, mcgrof@kernel.org,
torvalds@linux-foundation.org, rick.p.edgecombe@intel.com,
kernel-team@fb.com
Subject: Re: [PATCH v3 bpf-next 2/8] x86/alternative: introduce text_poke_set
Date: Sun, 22 May 2022 05:38:44 +0000 [thread overview]
Message-ID: <YonMZLyn6YJYnmjp@n2.us-central1-a.c.spheric-algebra-350919.internal> (raw)
In-Reply-To: <20220520031548.338934-3-song@kernel.org>
On Thu, May 19, 2022 at 08:15:42PM -0700, Song Liu wrote:
> Introduce a memset like API for text_poke. This will be used to fill the
> unused RX memory with illegal instructions.
>
> Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Signed-off-by: Song Liu <song@kernel.org>
> ---
> arch/x86/include/asm/text-patching.h | 1 +
> arch/x86/kernel/alternative.c | 67 +++++++++++++++++++++++-----
> 2 files changed, 58 insertions(+), 10 deletions(-)
>
> diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h
> index d20ab0921480..1cc15528ce29 100644
> --- a/arch/x86/include/asm/text-patching.h
> +++ b/arch/x86/include/asm/text-patching.h
> @@ -45,6 +45,7 @@ extern void *text_poke(void *addr, const void *opcode, size_t len);
> extern void text_poke_sync(void);
> extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len);
> extern void *text_poke_copy(void *addr, const void *opcode, size_t len);
> +extern void *text_poke_set(void *addr, int c, size_t len);
> extern int poke_int3_handler(struct pt_regs *regs);
> extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate);
>
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index d374cb3cf024..7563b5bc8328 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -994,7 +994,21 @@ static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
> __ro_after_init struct mm_struct *poking_mm;
> __ro_after_init unsigned long poking_addr;
>
> -static void *__text_poke(void *addr, const void *opcode, size_t len)
> +static void text_poke_memcpy(void *dst, const void *src, size_t len)
> +{
> + memcpy(dst, src, len);
> +}
> +
> +static void text_poke_memset(void *dst, const void *src, size_t len)
> +{
> + int c = *(const int *)src;
> +
> + memset(dst, c, len);
> +}
> +
> +typedef void text_poke_f(void *dst, const void *src, size_t len);
> +
> +static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t len)
> {
> bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE;
> struct page *pages[2] = {NULL};
> @@ -1059,7 +1073,7 @@ static void *__text_poke(void *addr, const void *opcode, size_t len)
> prev = use_temporary_mm(poking_mm);
>
> kasan_disable_current();
> - memcpy((u8 *)poking_addr + offset_in_page(addr), opcode, len);
> + func((u8 *)poking_addr + offset_in_page(addr), src, len);
> kasan_enable_current();
>
> /*
> @@ -1087,11 +1101,13 @@ static void *__text_poke(void *addr, const void *opcode, size_t len)
> (cross_page_boundary ? 2 : 1) * PAGE_SIZE,
> PAGE_SHIFT, false);
>
> - /*
> - * If the text does not match what we just wrote then something is
> - * fundamentally screwy; there's nothing we can really do about that.
> - */
> - BUG_ON(memcmp(addr, opcode, len));
> + if (func == text_poke_memcpy) {
> + /*
> + * If the text does not match what we just wrote then something is
> + * fundamentally screwy; there's nothing we can really do about that.
> + */
> + BUG_ON(memcmp(addr, src, len));
Maybe something like this?
} else if (func == text_poke_memset) {
WARN_ON or BUG_ON(memchr_inv(addr, *((const int *)src), len));
}
Thanks,
Hyeonggon
>
> local_irq_restore(flags);
> pte_unmap_unlock(ptep, ptl);
> @@ -1118,7 +1134,7 @@ void *text_poke(void *addr, const void *opcode, size_t len)
> {
> lockdep_assert_held(&text_mutex);
>
> - return __text_poke(addr, opcode, len);
> + return __text_poke(text_poke_memcpy, addr, opcode, len);
> }
>
> /**
> @@ -1137,7 +1153,7 @@ void *text_poke(void *addr, const void *opcode, size_t len)
> */
> void *text_poke_kgdb(void *addr, const void *opcode, size_t len)
> {
> - return __text_poke(addr, opcode, len);
> + return __text_poke(text_poke_memcpy, addr, opcode, len);
> }
>
> /**
> @@ -1167,7 +1183,38 @@ void *text_poke_copy(void *addr, const void *opcode, size_t len)
>
> s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
>
> - __text_poke((void *)ptr, opcode + patched, s);
> + __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s);
> + patched += s;
> + }
> + mutex_unlock(&text_mutex);
> + return addr;
> +}
> +
> +/**
> + * text_poke_set - memset into (an unused part of) RX memory
> + * @addr: address to modify
> + * @c: the byte to fill the area with
> + * @len: length to copy, could be more than 2x PAGE_SIZE
> + *
> + * This is useful to overwrite unused regions of RX memory with illegal
> + * instructions.
> + */
> +void *text_poke_set(void *addr, int c, size_t len)
> +{
> + unsigned long start = (unsigned long)addr;
> + size_t patched = 0;
> +
> + if (WARN_ON_ONCE(core_kernel_text(start)))
> + return NULL;
> +
> + mutex_lock(&text_mutex);
> + while (patched < len) {
> + unsigned long ptr = start + patched;
> + size_t s;
> +
> + s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
> +
> + __text_poke(text_poke_memset, (void *)ptr, (void *)&c, s);
> patched += s;
> }
> mutex_unlock(&text_mutex);
> --
> 2.30.2
>
>
next prev parent reply other threads:[~2022-05-22 22:04 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-20 3:15 [PATCH v3 bpf-next 0/8] bpf_prog_pack followup Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 1/8] bpf: fill new bpf_prog_pack with illegal instructions Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 2/8] x86/alternative: introduce text_poke_set Song Liu
2022-05-22 5:38 ` Hyeonggon Yoo [this message]
2022-05-20 3:15 ` [PATCH v3 bpf-next 3/8] bpf: introduce bpf_arch_text_invalidate for bpf_prog_pack Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 4/8] module: introduce module_alloc_huge Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 5/8] bpf: use module_alloc_huge for bpf_prog_pack Song Liu
2022-05-21 1:00 ` Luis Chamberlain
2022-05-21 1:20 ` Luis Chamberlain
2022-05-21 3:20 ` Edgecombe, Rick P
2022-05-21 20:06 ` Luis Chamberlain
2022-05-24 17:40 ` Edgecombe, Rick P
2022-05-24 22:08 ` Luis Chamberlain
2022-05-25 6:01 ` hch
2022-05-20 3:15 ` [PATCH v3 bpf-next 6/8] vmalloc: WARN for set_vm_flush_reset_perms() on huge pages Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 7/8] vmalloc: introduce huge_vmalloc_supported Song Liu
2022-05-20 3:15 ` [PATCH v3 bpf-next 8/8] bpf: simplify select_bpf_prog_pack_size Song Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YonMZLyn6YJYnmjp@n2.us-central1-a.c.spheric-algebra-350919.internal \
--to=42.hyeyoo@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mcgrof@kernel.org \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=song@kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox