mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@redhat.com>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: linux-kernel@vger.kernel.org, Peter Zijlstra <peterz@infradead.org>
Subject: Re: [PATCH 4/4] module: clean up RO/NX handling.
Date: Mon, 9 Nov 2015 13:51:53 -0600	[thread overview]
Message-ID: <20151109195153.GB3914@treble.redhat.com> (raw)
In-Reply-To: <1447043037-10833-5-git-send-email-rusty@rustcorp.com.au>

On Mon, Nov 09, 2015 at 02:53:57PM +1030, Rusty Russell wrote:

> @@ -1858,74 +1849,75 @@ static void mod_sysfs_teardown(struct module *mod)
>  /*
>   * LKM RO/NX protection: protect module's text/ro-data
>   * from modification and any data from execution.
> + *
> + * General layout of module is:
> + *          [text] [read-only-data] [writable data]
> + * text_size -----^                ^               ^
> + * ro_size ------------------------|               |
> + * size -------------------------------------------|
> + *
> + * These values are always page-aligned (as is base)
>   */
> -void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
> +static void frob_text(const struct module_layout *layout,
> +		      int (*set_memory)(unsigned long start, int num_pages))
>  {
> -	unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
> -	unsigned long end_pfn = PFN_DOWN((unsigned long)end);
> -
> -	if (end_pfn > begin_pfn)
> -		set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
> +	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
> +	set_memory((unsigned long)layout->base,
> +		   layout->text_size >> PAGE_SHIFT);

Should the set_memory() call be skipped if text_size is 0?

>  }
>  
> -static void set_section_ro_nx(void *base,
> -			unsigned long text_size,
> -			unsigned long ro_size,
> -			unsigned long total_size,
> -			int (*set_ro)(unsigned long start, int num_pages),
> -			int (*set_nx)(unsigned long start, int num_pages))
> +static void frob_rodata(const struct module_layout *layout,
> +			int (*set_memory)(unsigned long start, int num_pages))
>  {
> -	/* begin and end PFNs of the current subsection */
> -	unsigned long begin_pfn;
> -	unsigned long end_pfn;
> -
> -	/*
> -	 * Set RO for module text and RO-data:
> -	 * - Always protect first page.
> -	 * - Do not protect last partial page.
> -	 */
> -	if (ro_size > 0)
> -		set_page_attributes(base, base + ro_size, set_ro);
> +	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
> +	set_memory((unsigned long)layout->base + layout->text_size,
> +		   (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
> +}

Same here, what if ro_size == text_size (no rodata)?

>  
> -	/*
> -	 * Set NX permissions for module data:
> -	 * - Do not protect first partial page.
> -	 * - Always protect last page.
> -	 */
> -	if (total_size > text_size) {
> -		begin_pfn = PFN_UP((unsigned long)base + text_size);
> -		end_pfn = PFN_UP((unsigned long)base + total_size);
> -		if (end_pfn > begin_pfn)
> -			set_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
> -	}
> +static void frob_writable_data(const struct module_layout *layout,
> +			       int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
> +	set_memory((unsigned long)layout->base + layout->ro_size,
> +		   (layout->size - layout->ro_size) >> PAGE_SHIFT);
>  }

Ditto for size == ro_size (no writable data).


-- 
Josh

  reply	other threads:[~2015-11-09 19:51 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-09  4:23 [PATCH 0/4] module RO/NX cleanups Rusty Russell
2015-11-09  4:23 ` [PATCH 1/4] module: Use the same logic for setting and unsetting RO/NX Rusty Russell
2015-11-09  4:23 ` [PATCH 2/4] gcov: use within_module() helper Rusty Russell
2015-11-09  8:19   ` Peter Oberparleiter
2015-11-09  4:23 ` [PATCH 3/4] module: use a structure to encapsulate layout Rusty Russell
2015-11-09  9:41   ` Peter Zijlstra
2015-11-10  1:41     ` Rusty Russell
2015-11-09 16:54   ` Josh Poimboeuf
2015-11-09  4:23 ` [PATCH 4/4] module: clean up RO/NX handling Rusty Russell
2015-11-09 19:51   ` Josh Poimboeuf [this message]
2015-11-10  1:57     ` Rusty Russell
2015-11-10  4:27       ` Josh Poimboeuf
2015-11-12  1:28         ` Rusty Russell
2015-11-12  3:41           ` Josh Poimboeuf

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=20151109195153.GB3914@treble.redhat.com \
    --to=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rusty@rustcorp.com.au \
    /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

all inboxes | Powered by JetHome®