lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:	Fri, 27 Feb 2009 13:07:24 -0500
From:	Mathieu Desnoyers <mathieu.desnoyers@...ymtl.ca>
To:	Masami Hiramatsu <mhiramat@...hat.com>
Cc:	Steven Rostedt <rostedt@...dmis.org>,
	Andi Kleen <andi@...stfloor.org>, linux-kernel@...r.kernel.org,
	Ingo Molnar <mingo@...e.hu>,
	Andrew Morton <akpm@...ux-foundation.org>,
	Thomas Gleixner <tglx@...utronix.de>,
	Peter Zijlstra <peterz@...radead.org>,
	Frederic Weisbecker <fweisbec@...il.com>,
	Linus Torvalds <torvalds@...ux-foundation.org>,
	Arjan van de Ven <arjan@...radead.org>,
	Rusty Russell <rusty@...tcorp.com.au>,
	"H. Peter Anvin" <hpa@...or.com>,
	Steven Rostedt <srostedt@...hat.com>
Subject: Re: [PATCH 4/6] ftrace, x86: make kernel text writable only for
	conversions

* Masami Hiramatsu (mhiramat@...hat.com) wrote:
> Steven Rostedt wrote:
> > On Mon, 23 Feb 2009, Mathieu Desnoyers wrote:
> >>> Hmm, lets see. I simply set a bit in the PTE mappings. There's not many, 
> >>> since a lot are 2M pages, for x86_64. Call stop_machine, and now I can 
> >>> modify 1 or 20,000 locations. Set the PTE bit back. Note, the changing of 
> >>> the bits are only done when CONFIG_DEBUG_RODATA is set.
> >>>
> >>> text_poke requires allocating a page. Map the page into memory. Set up a 
> >>> break point.
> >> text_poke does not _require_ a break point. text_poke can work with
> >> stop_machine.
> > 
> > It can? Doesn't text_poke require allocating pages? The code called by 
> > stop_machine is all atomic. vmap does not give an option to allocate with 
> > GFP_ATOMIC.
> 
> Hi,
> 
> With my patch, text_poke() never allocate pages any more :)
> 
> BTW, IMHO, both of your methods are useful and have trade-off.
> 
> ftrace wants to change massive amount of code at once. If we do
> that with text_poke(), we have to map/unmap pages each time and
> it will take a long time -- might be longer than one stop_machine_run().
> 
> On the other hand, text_poke() user like as kprobes and tracepoints,
> just want to change a few amount of code at once, and it will be
> added/removed incrementally. If we do that with stop_machine_run(),
> we'll be annoyed by frequent machine stops.(Moreover, kprobes uses
> breakpoint, so it doesn't need stop_machine_run())
> 

Hi Masami,

Is this text_poke version executable in atomic context ? If yes, then
that would be good to add a comment saying it. Please see below for
comments.

> 
> Thank you,
> 
[...]
> Use map_vm_area() instead of vmap() in text_poke() for avoiding page allocation
> and delayed unmapping.
> 
> Signed-off-by: Masami Hiramatsu <mhiramat@...hat.com>
> ---
>  arch/x86/include/asm/alternative.h |    1 +
>  arch/x86/kernel/alternative.c      |   25 ++++++++++++++++++++-----
>  init/main.c                        |    3 +++
>  3 files changed, 24 insertions(+), 5 deletions(-)
> 
> Index: linux-2.6/arch/x86/include/asm/alternative.h
> ===================================================================
> --- linux-2.6.orig/arch/x86/include/asm/alternative.h
> +++ linux-2.6/arch/x86/include/asm/alternative.h
> @@ -177,6 +177,7 @@ extern void add_nops(void *insns, unsign
>   * The _early version expects the memory to already be RW.
>   */
>  
> +extern void text_poke_init(void);
>  extern void *text_poke(void *addr, const void *opcode, size_t len);
>  extern void *text_poke_early(void *addr, const void *opcode, size_t len);
>  
> Index: linux-2.6/arch/x86/kernel/alternative.c
> ===================================================================
> --- linux-2.6.orig/arch/x86/kernel/alternative.c
> +++ linux-2.6/arch/x86/kernel/alternative.c
> @@ -485,6 +485,16 @@ void *text_poke_early(void *addr, const 
>  	return addr;
>  }
>  
> +static struct vm_struct *text_poke_area[2];
> +static DEFINE_SPINLOCK(text_poke_lock);
> +
> +void __init text_poke_init(void)
> +{
> +	text_poke_area[0] = get_vm_area(PAGE_SIZE, VM_ALLOC);
> +	text_poke_area[1] = get_vm_area(2 * PAGE_SIZE, VM_ALLOC);

Why is this text_poke_area[1] 2 * PAGE_SIZE in size ? I would have
thought that text_poke_area[0] would be PAGE_SIZE, text_poke_area[1]
also be PAGE_SIZE, and that the sum of both would be 2 * PAGE_SIZE..

> +	BUG_ON(!text_poke_area[0] || !text_poke_area[1]);
> +}
> +
>  /**
>   * text_poke - Update instructions on a live kernel
>   * @addr: address to modify
> @@ -501,8 +511,9 @@ void *__kprobes text_poke(void *addr, co
>  	unsigned long flags;
>  	char *vaddr;
>  	int nr_pages = 2;
> -	struct page *pages[2];
> -	int i;
> +	struct page *pages[2], **pgp = pages;

Hrm, why do you need **pgp ? Could you simply pass &pages to map_vm_area ?

Thanks,

Mathieu

> +	int i, ret;
> +	struct vm_struct *vma;
>  
>  	if (!core_kernel_text((unsigned long)addr)) {
>  		pages[0] = vmalloc_to_page(addr);
> @@ -515,12 +526,16 @@ void *__kprobes text_poke(void *addr, co
>  	BUG_ON(!pages[0]);
>  	if (!pages[1])
>  		nr_pages = 1;
> -	vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
> -	BUG_ON(!vaddr);
> +	spin_lock(&text_poke_lock);
> +	vma = text_poke_area[nr_pages-1];
> +	ret = map_vm_area(vma, PAGE_KERNEL, &pgp);
> +	BUG_ON(ret);
> +	vaddr = vma->addr;
>  	local_irq_save(flags);
>  	memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
>  	local_irq_restore(flags);
> -	vunmap(vaddr);
> +	unmap_kernel_range((unsigned long)vma->addr, (unsigned long)vma->size);
> +	spin_unlock(&text_poke_lock);
>  	sync_core();
>  	/* Could also do a CLFLUSH here to speed up CPU recovery; but
>  	   that causes hangs on some VIA CPUs. */
> @@ -528,3 +543,4 @@ void *__kprobes text_poke(void *addr, co
>  		BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
>  	return addr;
>  }
> +EXPORT_SYMBOL_GPL(text_poke);
> Index: linux-2.6/init/main.c
> ===================================================================
> --- linux-2.6.orig/init/main.c
> +++ linux-2.6/init/main.c
> @@ -676,6 +676,9 @@ asmlinkage void __init start_kernel(void
>  	taskstats_init_early();
>  	delayacct_init();
>  
> +#ifdef CONFIG_X86
> +	text_poke_init();
> +#endif
>  	check_bugs();
>  
>  	acpi_early_init(); /* before LAPIC and SMP init */


-- 
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ