linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Arnd Bergmann <arnd@arndb.de>, Jessica Yu <jeyu@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	linux-arch@vger.kernel.org, Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@de.ibm.com>,
	Alexander Egorenkov <egorenar@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Ilya Leoshkevich <iii@linux.ibm.com>,
	"Steven Rostedt (VMware)" <rostedt@goodmis.org>,
	Ingo Molnar <mingo@kernel.org>,
	Sami Tolvanen <samitolvanen@google.com>,
	linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH 3/4] module: Use a list of strings for ro_after_init sections
Date: Fri, 3 Sep 2021 09:38:42 -0700	[thread overview]
Message-ID: <202109030932.1358C4093@keescook> (raw)
In-Reply-To: <20210903064951.to4dhiu7zua7s6dn@treble>

On Thu, Sep 02, 2021 at 11:49:51PM -0700, Josh Poimboeuf wrote:
> On Wed, Sep 01, 2021 at 04:37:56PM -0700, Kees Cook wrote:
> > Instead of open-coding the section names, use a list for the sections that
> > need to be marked read-only after init. Unfortunately, it seems we can't
> > do normal section merging with scripts/module.lds.S as ld.bfd doesn't
> > correctly update symbol tables. For more details, see commit 6a3193cdd5e5
> > ("kbuild: lto: Merge module sections if and only if CONFIG_LTO_CLANG
> > is enabled").
> 
> I'm missing what this has to do with section merging.  Can you connect
> the dots here, i.e. what sections would we want to merge and how would
> that help here?

Right, sorry, if ld.bfd didn't have this issue, we could use section
merging in the module.lds.S file the way we do in vmlinux.lds:

#ifndef RO_AFTER_INIT_DATA
#define RO_AFTER_INIT_DATA                                              \
        . = ALIGN(8);                                                   \
        __start_ro_after_init = .;                                      \
        *(.data..ro_after_init)                                         \
        JUMP_TABLE_DATA                                                 \
        STATIC_CALL_DATA                                                \
        __end_ro_after_init = .;
#endif
...
        . = ALIGN((align));                                             \
        .rodata           : AT(ADDR(.rodata) - LOAD_OFFSET) {           \
                __start_rodata = .;                                     \
                *(.rodata) *(.rodata.*)                                 \
                SCHED_DATA                                              \
                RO_AFTER_INIT_DATA      /* Read only after init */      \
                . = ALIGN(8);                                           \
                __start___tracepoints_ptrs = .;                         \
                KEEP(*(__tracepoints_ptrs)) /* Tracepoints: pointer array */ \
                __stop___tracepoints_ptrs = .;                          \
                *(__tracepoints_strings)/* Tracepoints: strings */      \
        }                                                               \

Then jump_table and static_call sections could be collected into a
new section, as the module loader would only need to look for that
single name.

> Instead of hard-coding section names in module.c, I'm wondering if we
> can do something like the following to set SHF_RO_AFTER_INIT when first
> creating the sections.  Completely untested...
> 
> 
> diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h
> index 0449b125d27f..d4ff34c6199c 100644
> --- a/arch/x86/include/asm/jump_label.h
> +++ b/arch/x86/include/asm/jump_label.h
> @@ -13,7 +13,7 @@
>  #include <linux/types.h>
>  
>  #define JUMP_TABLE_ENTRY				\
> -	".pushsection __jump_table,  \"aw\" \n\t"	\
> +	".pushsection __jump_table, \"0x00200003\" \n\t"\
>  	_ASM_ALIGN "\n\t"				\
>  	".long 1b - . \n\t"				\
>  	".long %l[l_yes] - . \n\t"			\
> diff --git a/kernel/module.c b/kernel/module.c
> index 40ec9a030eec..1dda33c9ae49 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -3549,15 +3549,6 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
>  	 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
>  	 */
>  	ndx = find_sec(info, ".data..ro_after_init");
> -	if (ndx)
> -		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
> -	/*
> -	 * Mark the __jump_table section as ro_after_init as well: these data
> -	 * structures are never modified, with the exception of entries that
> -	 * refer to code in the __init section, which are annotated as such
> -	 * at module load time.
> -	 */
> -	ndx = find_sec(info, "__jump_table");
>  	if (ndx)
>  		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
>  
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index e5947fbb9e7a..b25ca38179ea 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -20,6 +20,9 @@
>  #include <linux/kernel.h>
>  #include <linux/static_call_types.h>
>  
> +/* cribbed from include/uapi/linux/elf.h */
> +#define SHF_RO_AFTER_INIT	0x00200000
> +
>  struct alternative {
>  	struct list_head list;
>  	struct instruction *insn;
> @@ -466,7 +469,8 @@ static int create_static_call_sections(struct objtool_file *file)
>  	list_for_each_entry(insn, &file->static_call_list, call_node)
>  		idx++;
>  
> -	sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
> +	sec = elf_create_section(file->elf, ".static_call_sites",
> +				 SHF_WRITE | SHF_RO_AFTER_INIT,
>  				 sizeof(struct static_call_site), idx);
>  	if (!sec)
>  		return -1;

Interesting! I got the impression from the module code that this wasn't
possible since it'd be exposing an internal set of flags to the external
linker, and would break the vmlinux section merging (since it _is_
supposed to live in the .rodata section ultimately). The modules handle
permissions slightly differently (i.e. more exact temporal), than the
kernel though. (Most of the architecture's vmlinux logic starts with
everything writable, and only does the read-only-ness after __init,
though I think s390 does it "correctly" and as such has a separate area
for the ro-after-init section.)

-- 
Kees Cook

  reply	other threads:[~2021-09-03 16:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-01 23:37 [PATCH 0/4] Fix ro_after_init vs static_call Kees Cook
2021-09-01 23:37 ` [PATCH 1/4] vmlinux.lds.h: Use regular *RODATA and *RO_AFTER_INIT_DATA suffixes Kees Cook
2021-09-03  5:55   ` Josh Poimboeuf
2021-09-03  8:17   ` Heiko Carstens
2021-09-01 23:37 ` [PATCH 2/4] vmlinux.lds.h: Split .static_call_sites from .static_call_tramp_key Kees Cook
2021-09-03  5:55   ` Josh Poimboeuf
2021-09-01 23:37 ` [PATCH 3/4] module: Use a list of strings for ro_after_init sections Kees Cook
2021-09-03  6:49   ` Josh Poimboeuf
2021-09-03 16:38     ` Kees Cook [this message]
2021-09-04  4:09       ` Josh Poimboeuf
2021-09-04 14:40         ` Kees Cook
2021-09-01 23:37 ` [PATCH 4/4] module: Include .static_call_sites in module ro_after_init Kees Cook

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=202109030932.1358C4093@keescook \
    --to=keescook@chromium.org \
    --cc=arnd@arndb.de \
    --cc=borntraeger@de.ibm.com \
    --cc=egorenar@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=iii@linux.ibm.com \
    --cc=jeyu@kernel.org \
    --cc=jpoimboe@redhat.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=samitolvanen@google.com \
    --cc=svens@linux.ibm.com \
    /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;
as well as URLs for NNTP newsgroup(s).