Fix LKDTM for PPC64/IA64/PARISC
Subsystems (7)
cover Fix LKDTM for PPC64/IA64/PARISC
PPC64/IA64/PARISC have function descriptors. LKDTM doesn't work on those three architectures because LKDTM messes up function descriptors with functions.
This series does some cleanup in the three architectures and refactors function descriptors so that it can then easily use it in a generic way in LKDTM.
Patch 6 is not absolutely necessary but it is a good trivial cleanup.
Christophe Leroy (10):
powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h
powerpc: Rename 'funcaddr' to 'addr' in 'struct ppc64_opd_entry'
ia64: Rename 'ip' to 'addr' in 'struct fdesc'
asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define
associated stubs
asm-generic: Define 'funct_descr_t' to commonly describe function
descriptors
asm-generic: Refactor dereference_[kernel]_function_descriptor()
lkdtm: Force do_nothing() out of line
lkdtm: Really write into kernel text in WRITE_KERN
lkdtm: Fix lkdtm_EXEC_RODATA()
lkdtm: Fix execute_[user]_location()
arch/ia64/include/asm/elf.h | 2 +- arch/ia64/include/asm/sections.h | 24 ++--------- arch/ia64/kernel/module.c | 6 +-- arch/parisc/include/asm/sections.h | 16 +++---- arch/parisc/kernel/process.c | 21 --------- arch/powerpc/include/asm/elf.h | 7 +++ arch/powerpc/include/asm/sections.h | 30 +++---------- arch/powerpc/include/uapi/asm/elf.h | 8 ---- arch/powerpc/kernel/module_64.c | 6 +-- drivers/misc/lkdtm/perms.c | 66 +++++++++++++++++++++++------ include/asm-generic/sections.h | 24 ++++++++++- 11 files changed, 102 insertions(+), 108 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/10 powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h
'struct ppc64_opd_entry' doesn't belong to uapi/asm/elf.h
It was initially in module_64.c and commit 2d291e902791 ("Fix compile failure with non modular builds") moved it into asm/elf.h
But it was by mistake added outside of __KERNEL__ section, therefore commit c3617f72036c ("UAPI: (Scripted) Disintegrate arch/powerpc/include/asm") moved it to uapi/asm/elf.h
Move it back into asm/elf.h, this brings it back in line with IA64 and PARISC architectures.
--- arch/powerpc/include/asm/elf.h | 7 +++++++ arch/powerpc/include/uapi/asm/elf.h | 8 -------- 2 files changed, 7 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/elf.h +7 -0
@@ -176,4 +176,11 @@ do { \/* Relocate the kernel image to @final_address */ void relocate (unsigned long final_address); +/* There's actually a third entry here, but it's unused */ +struct ppc64_opd_entry +{ +unsigned long funcaddr; +unsigned long r2; +}; +#endif /* _ASM_POWERPC_ELF_H */
arch/powerpc/include/uapi/asm/elf.h +0 -8
@@ -289,12 +289,4 @@ typedef elf_fpreg_t elf_vsrreghalf_t32[ELF_NVSRHALFREG];/* Keep this the last entry. */ #define R_PPC64_NUM 253 -/* There's actually a third entry here, but it's unused */ -struct ppc64_opd_entry -{ -unsigned long funcaddr; -unsigned long r2; -}; - -#endif /* _UAPI_ASM_POWERPC_ELF_H */
Signature · 1 line
2.31.1
PATCH 2/10 powerpc: Rename 'funcaddr' to 'addr' in 'struct ppc64_opd_entry'
In reply to Christophe LeroyThere are three architectures with function descriptors, try to have common names for the address they contain in order to refactor some functions into generic functions later.
powerpc has 'funcaddr'
ia64 has 'ip'
parisc has 'addr'
Vote for 'addr' and update 'struct ppc64_opd_entry' accordingly.
--- arch/powerpc/include/asm/elf.h | 2 +- arch/powerpc/include/asm/sections.h | 2 +- arch/powerpc/kernel/module_64.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)
arch/powerpc/include/asm/elf.h +1 -1
@@ -179,7 +179,7 @@ void relocate(unsigned long final_address);/* There's actually a third entry here, but it's unused */ struct ppc64_opd_entry { -unsigned long funcaddr; +unsigned long addr;unsigned long r2; };
arch/powerpc/include/asm/sections.h +1 -1
@@ -77,7 +77,7 @@ static inline void *dereference_function_descriptor(void *ptr)struct ppc64_opd_entry *desc = ptr;void *p; -if (!get_kernel_nofault (p, (void *)&desc->funcaddr)) +if (!get_kernel_nofault (p, (void *)&desc->addr)) ptr = p;return ptr; }
arch/powerpc/kernel/module_64.c +3 -3
@@ -72,11 +72,11 @@ static func_desc_t func_desc(unsigned long addr) }static unsigned long func_addr (unsigned long addr) { -return func_desc (addr).funcaddr; +return func_desc (addr).addr; }static unsigned long stub_func_addr (func_desc_t func) { -return func.funcaddr; +return func.addr; }static unsigned int local_entry_offset (const Elf64_Sym *sym) {
@@ -187,7 +187,7 @@ static int relacmp(const void *_x, const void *_y)static unsigned long get_stubs_size (const Elf64_Ehdr *hdr,const Elf64_Shdr *sechdrs) { -/* One extra reloc so it's always 0-funcaddr terminated */ +/* One extra reloc so it's always 0-addr terminated */ unsigned long relocs =1 ;unsigned i;
Signature · 1 line
2.31.1
PATCH 3/10 ia64: Rename 'ip' to 'addr' in 'struct fdesc'
In reply to Christophe LeroyThere are three architectures with function descriptors, try to have common names for the address they contain in order to refactor some functions into generic functions later.
powerpc has 'funcaddr'
ia64 has 'ip'
parisc has 'addr'
Vote for 'addr' and update 'struct fdesc' accordingly.
--- arch/ia64/include/asm/elf.h | 2 +- arch/ia64/include/asm/sections.h | 2 +- arch/ia64/kernel/module.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)
arch/ia64/include/asm/elf.h +1 -1
@@ -226,7 +226,7 @@ struct got_entry {* Layout of the Function Descriptor */ struct fdesc { -uint64_t ip; +uint64_t addr;uint64_t gp; };
arch/ia64/include/asm/sections.h +1 -1
@@ -35,7 +35,7 @@ static inline void *dereference_function_descriptor(void *ptr)struct fdesc *desc = ptr;void *p; -if (!get_kernel_nofault (p, (void *)&desc->ip)) +if (!get_kernel_nofault (p, (void *)&desc->addr)) ptr = p;return ptr; }
arch/ia64/kernel/module.c +3 -3
@@ -602,15 +602,15 @@ get_fdesc (struct module *mod, uint64_t value, int *okp)return value;/* Look for existing function descriptor. */ -while (fdesc->ip) { -if (fdesc->ip = value) +while (fdesc->addr) { +if (fdesc->addr = value)return (uint64_t )fdesc;if ((uint64_t ) ++fdesc >= mod->arch.opd->sh_addr + mod->arch.opd->sh_size)BUG (); }/* Create new one */ - fdesc->ip = value; + fdesc->addr = value; fdesc->gp = mod->arch.gp;return (uint64_t ) fdesc; }
Signature · 1 line
2.31.1
PATCH 4/10 asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
In reply to Christophe LeroyUse HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR instead of 'dereference_function_descriptor' to know whether arch has function descriptors.
--- arch/ia64/include/asm/sections.h | 4 ++-- arch/parisc/include/asm/sections.h | 6 ++++-- arch/powerpc/include/asm/sections.h | 6 ++++-- include/asm-generic/sections.h | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-)
arch/ia64/include/asm/sections.h +2 -2
@@ -7,6 +7,8 @@* David Mosberger-Tang <[email protected]> */ +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#include <linux/elf.h> #include <linux/uaccess.h> #include <asm-generic/sections.h>
@@ -27,8 +29,6 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {
arch/parisc/include/asm/sections.h +4 -2
@@ -2,6 +2,10 @@#ifndef _PARISC_SECTIONS_H#define _PARISC_SECTIONS_H +#ifdef CONFIG_64BIT +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +/* nothing to see, move along */ #include <asm-generic/sections.h>
@@ -9,8 +13,6 @@ extern char __alt_instructions[], __alt_instructions_end[];#ifdef CONFIG_64BIT -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorvoid *dereference_function_descriptor (void *);
arch/powerpc/include/asm/sections.h +4 -2
@@ -8,6 +8,10 @@#define arch_is_kernel_initmem_freed arch_is_kernel_initmem_freed +#ifdef PPC64_ELF_ABI_v1 +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +#include <asm-generic/sections.h> extern bool init_mem_is_free;
@@ -69,8 +73,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)#ifdef PPC64_ELF_ABI_v1 -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {
include/asm-generic/sections.h +2 -1
@@ -59,7 +59,8 @@ extern char __noinstr_text_start[], __noinstr_text_end[]; extern __visible const void __nosave_begin, __nosave_end;/* Function descriptor handling (if any). Override in asm/sections.h */ -#ifndef dereference_function_descriptor +#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p))#endif
Signature · 1 line
2.31.1
PATCH 5/10 asm-generic: Define 'funct_descr_t' to commonly describe function descriptors
In reply to Christophe LeroyWe have three architectures using function descriptors, each with its own name.
Add a common typedef that can be used in generic code.
Also add a stub typedef for architecture without function descriptors, to avoid a forest of #ifdefs.
--- arch/ia64/include/asm/sections.h | 1 + arch/parisc/include/asm/sections.h | 1 + arch/powerpc/include/asm/sections.h | 1 + include/asm-generic/sections.h | 3 +++ 4 files changed, 6 insertions(+)
arch/ia64/include/asm/sections.h +1 -0
@@ -8,6 +8,7 @@*/ #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct fdesc funct_descr_t ;#include <linux/elf.h> #include <linux/uaccess.h>
arch/parisc/include/asm/sections.h +1 -0
@@ -4,6 +4,7 @@#ifdef CONFIG_64BIT #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef Elf64_Fdesc funct_descr_t ;#endif /* nothing to see, move along */
arch/powerpc/include/asm/sections.h +1 -0
@@ -10,6 +10,7 @@#ifdef PPC64_ELF_ABI_v1#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct ppc64_opd_entry funct_descr_t ;#endif #include <asm-generic/sections.h>
include/asm-generic/sections.h +3 -0
@@ -63,6 +63,9 @@ extern __visible const void __nosave_begin, __nosave_end;#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p)) +typedef struct { +unsigned long addr; +}funct_descr_t ;#endif /* random extra sections (if any). Override
Signature · 1 line
2.31.1
PATCH 6/10 asm-generic: Refactor dereference_[kernel]_function_descriptor()
In reply to Christophe Leroydereference_function_descriptor() and dereference_kernel_function_descriptor() are identical on the three architectures implementing them.
Make it common.
--- arch/ia64/include/asm/sections.h | 19 ------------------- arch/parisc/include/asm/sections.h | 9 --------- arch/parisc/kernel/process.c | 21 --------------------- arch/powerpc/include/asm/sections.h | 23 ----------------------- include/asm-generic/sections.h | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+), 72 deletions(-)
arch/ia64/include/asm/sections.h +0 -19
@@ -30,23 +30,4 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#undef dereference_function_descriptor -static inline void *dereference_function_descriptor (void *ptr) -{ -struct fdesc *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -#undef dereference_kernel_function_descriptor -static inline void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) -return ptr; -return dereference_function_descriptor (ptr); -} -#endif /* _ASM_IA64_SECTIONS_H */
arch/parisc/include/asm/sections.h +0 -9
@@ -12,13 +12,4 @@ typedef Elf64_Fdesc funct_descr_t;extern char __alt_instructions[], __alt_instructions_end[]; -#ifdef CONFIG_64BIT - -#undef dereference_function_descriptor -void *dereference_function_descriptor (void *); - -#undef dereference_kernel_function_descriptor -void *dereference_kernel_function_descriptor (void *); -#endif -#endif
arch/parisc/kernel/process.c +0 -21
@@ -266,27 +266,6 @@ get_wchan(struct task_struct *p)return 0 ; } -#ifdef CONFIG_64BIT -void *dereference_function_descriptor (void *ptr) -{ -Elf64_Fdesc *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || - ptr >= (void *)__end_opd) -return ptr; - -return dereference_function_descriptor (ptr); -} -#endif -static inline unsigned long brk_rnd (void ) {return (get_random_int () &BRK_RND_MASK ) <<PAGE_SHIFT ;
arch/powerpc/include/asm/sections.h +0 -23
@@ -72,29 +72,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end) (unsigned long )_stext < end; } -#ifdef PPC64_ELF_ABI_v1 - -#undef dereference_function_descriptor -static inline void *dereference_function_descriptor (void *ptr) -{ -struct ppc64_opd_entry *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -#undef dereference_kernel_function_descriptor -static inline void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) -return ptr; - -return dereference_function_descriptor (ptr); -} -#endif /* PPC64_ELF_ABI_v1 */ -#endif #endif /* __KERNEL__ */
include/asm-generic/sections.h +18 -0
@@ -60,6 +60,24 @@ extern __visible const void __nosave_begin, __nosave_end;/* Function descriptor handling (if any). Override in asm/sections.h */ #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +static inline void *dereference_function_descriptor (void *ptr) +{ +funct_descr_t *desc = ptr; +void *p; + +if (!get_kernel_nofault (p, (void *)&desc->addr)) + ptr = p; +return ptr; +} + +static inline void *dereference_kernel_function_descriptor (void *ptr) +{ +if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) +return ptr; + +return dereference_function_descriptor (ptr); +} +#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p))
Signature · 1 line
2.31.1
PATCH 7/10 lkdtm: Force do_nothing() out of line
In reply to Christophe LeroyLKDTM tests display that the run do_nothing() at a given address, but in reality do_nothing() is inlined into the caller.
Force it out of line so that it really runs text at the displayed address.
--- drivers/misc/lkdtm/perms.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/misc/lkdtm/perms.c +2 -2
@@ -21,7 +21,7 @@/* This is non-const, so it will end up in the .data section. */ static u8 data_area[EXEC_SIZE ]; -/* This is cost, so it will end up in the .rodata section. */ +/* This is const, so it will end up in the .rodata section. */ static const unsigned long rodata =0xAA55AA55 ;/* This is marked __ro_after_init, so it should ultimately be .rodata. */
@@ -31,7 +31,7 @@ static unsigned long ro_after_init __ro_after_init = 0x55AA5500;* This just returns to the caller. It is designed to be copied into * non-executable memory regions. */ -static void do_nothing (void ) +static noinline void do_nothing(void) {return ; }
Signature · 1 line
2.31.1
PATCH 8/10 lkdtm: Really write into kernel text in WRITE_KERN
In reply to Christophe LeroyWRITE_KERN is supposed to overwrite some kernel text, namely do_overwritten() function.
But at the time being it overwrites do_overwritten() function descriptor, not function text.
Fix it by dereferencing the function descriptor to obtain function text pointer.
And make do_overwritten() noinline so that it is really do_overwritten() which is called by lkdtm_WRITE_KERN().
--- drivers/misc/lkdtm/perms.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/misc/lkdtm/perms.c +5 -3
@@ -5,6 +5,7 @@* even non-readable regions. */ #include "lkdtm.h" +#include <linux/kallsyms.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/mman.h>
@@ -37,7 +38,7 @@ static noinline void do_nothing(void) }/* Must immediately follow do_nothing for size calculuations to work out. */ -static void do_overwritten (void ) +static noinline void do_overwritten(void) {pr_info ("do_overwritten wasn't overwritten!\n" );return ;
@@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void)size_t size;volatile unsigned char *ptr; - size = (unsigned long )do_overwritten - (unsigned long )do_nothing; - ptr = (unsigned char *)do_overwritten; + size = (unsigned long )dereference_symbol_descriptor (do_overwritten) - + (unsigned long )dereference_symbol_descriptor (do_nothing); + ptr =dereference_symbol_descriptor (do_overwritten);pr_info ("attempting bad %zu byte write at %px\n" , size, ptr);memcpy ((void *)ptr, (unsigned char *)do_nothing, size);
Signature · 1 line
2.31.1
PATCH 9/10 lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Christophe LeroyBehind a location, lkdtm_EXEC_RODATA() executes a real function, not a copy of do_nothing().
So do it directly instead of using execute_location().
And fix displayed addresses by dereferencing the function descriptors.
--- drivers/misc/lkdtm/perms.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/perms.c +8 -1
@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void)void lkdtm_EXEC_RODATA (void ) { -execute_location (lkdtm_rodata_do_nothing,CODE_AS_IS ); +pr_info ("attempting ok execution at %px\n" , +dereference_symbol_descriptor (do_nothing)); +do_nothing (); + +pr_info ("attempting bad execution at %px\n" , +dereference_symbol_descriptor (lkdtm_rodata_do_nothing)); +lkdtm_rodata_do_nothing (); +pr_err ("FAIL: func returned\n" ); }void lkdtm_EXEC_USERSPACE (void )
Signature · 1 line
2.31.1
PATCH 10/10 lkdtm: Fix execute_[user]_location()
In reply to Christophe Leroyexecute_location() and execute_user_location() intent
to copy do_nothing() text and execute it at a new location. However, at the time being it doesn't copy do_nothing() function but do_nothing() function descriptor which still points to the original text. So at the end it still executes do_nothing() at its original location allthough using a copied function descriptor.
So, fix that by really copying do_nothing() text and build a new function descriptor by copying do_nothing() function descriptor and updating the target address with the new location.
Also fix the displayed addresses by dereferencing do_nothing() function descriptor.
--- drivers/misc/lkdtm/perms.c | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-)
drivers/misc/lkdtm/perms.c +37 -8
@@ -44,19 +44,42 @@ static noinline void do_overwritten(void)return ; } +static void *setup_function_descriptor (funct_descr_t *fdesc,void *dst) +{ +int err; + +if (!__is_defined (HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR )) +return dst; + + err =copy_from_kernel_nofault (fdesc, do_nothing,sizeof (*fdesc)); +if (err <0 ) +return ERR_PTR (err); + + fdesc->addr = (unsigned long )dst; +barrier (); + +return fdesc; +} + static noinline void execute_location(void *dst, bool write) { -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing ();if (write =CODE_WRITE ) { -memcpy (dst, do_nothing,EXEC_SIZE ); +memcpy (dst, do_nothing_text,EXEC_SIZE );flush_icache_range ((unsigned long )dst, (unsigned long )dst +EXEC_SIZE ); } -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ; + +pr_info ("attempting bad execution at %px\n" , dst);func ();pr_err ("FAIL: func returned\n" ); }
@@ -66,16 +89,22 @@ static void execute_user_location(void *dst)int copied;/* Intentionally crossing kernel/user memory boundary. */ -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing (); - copied =access_process_vm (current, (unsigned long )dst, do_nothing, + copied =access_process_vm (current, (unsigned long )dst, do_nothing_text,EXEC_SIZE ,FOLL_WRITE );if (copied <EXEC_SIZE )return ; -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ; + +pr_info ("attempting bad execution at %px\n" , dst);func ();pr_err ("FAIL: func returned\n" ); }
Signature · 1 line
2.31.1
Helge Deller
maintainerRe: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
In reply to Christophe LeroyQuoted 86 lines · Christophe Leroy
Use
HAVE_DEREFERENCE_FUNCTION_DESCRIPTORinstead of 'dereference_function_descriptor' to know whether arch has function descriptors.Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 4 ++-- arch/parisc/include/asm/sections.h | 6 ++++-- arch/powerpc/include/asm/sections.h | 6 ++++-- include/asm-generic/sections.h | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-)arch/ia64/include/asm/sections.h +2
-2@@ -7,6 +7,8 @@* David Mosberger-Tang <[email protected]> */ +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#include <linux/elf.h> #include <linux/uaccess.h> #include <asm-generic/sections.h> @@ -27,8 +29,6 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {arch/parisc/include/asm/sections.h +4
-2@@ -2,6 +2,10 @@#ifndef _PARISC_SECTIONS_H#define _PARISC_SECTIONS_H +#ifdef CONFIG_64BIT +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +/* nothing to see, move along */ #include <asm-generic/sections.h> @@ -9,8 +13,6 @@ extern char __alt_instructions[], __alt_instructions_end[];#ifdef CONFIG_64BIT -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorvoid *dereference_function_descriptor (void *);arch/powerpc/include/asm/sections.h +4
-2@@ -8,6 +8,10 @@#define arch_is_kernel_initmem_freed arch_is_kernel_initmem_freed +#ifdef PPC64_ELF_ABI_v1 +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +#include <asm-generic/sections.h> extern bool init_mem_is_free;@@ -69,8 +73,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)#ifdef PPC64_ELF_ABI_v1 -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {include/asm-generic/sections.h +0
-0
include/asm-generic/sections.h extern char __noinstr_text_start[], __noinstr_text_end[]; · line 63
Quoted 6 lines
include/asm-generic/sections.h +2
-1@@ -59,7 +59,8 @@ extern char __noinstr_text_start[], __noinstr_text_end[]; extern __visible const void __nosave_begin, __nosave_end;/* Function descriptor handling (if any). Override in asm/sections.h */ -#ifndef dereference_function_descriptor +#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +#else
why not
#ifndef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
instead of #if/#else ?
Quoted 4 lines
include/asm-generic/sections.h +0
-0#define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p))#endif
Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
Le 12/10/2021 à 08:02, Helge Deller a écrit :
Quoted 89 lines · Christophe Leroy
Quoted 88 lines
Use
HAVE_DEREFERENCE_FUNCTION_DESCRIPTORinstead of 'dereference_function_descriptor' to know whether arch has function descriptors.Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 4 ++-- arch/parisc/include/asm/sections.h | 6 ++++-- arch/powerpc/include/asm/sections.h | 6 ++++-- include/asm-generic/sections.h | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-)arch/ia64/include/asm/sections.h +2
-2@@ -7,6 +7,8 @@* David Mosberger-Tang <[email protected]> */ +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#include <linux/elf.h> #include <linux/uaccess.h> #include <asm-generic/sections.h> @@ -27,8 +29,6 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {arch/parisc/include/asm/sections.h +4
-2@@ -2,6 +2,10 @@#ifndef _PARISC_SECTIONS_H#define _PARISC_SECTIONS_H +#ifdef CONFIG_64BIT +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +/* nothing to see, move along */ #include <asm-generic/sections.h> @@ -9,8 +13,6 @@ extern char __alt_instructions[], __alt_instructions_end[];#ifdef CONFIG_64BIT -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorvoid *dereference_function_descriptor (void *);arch/powerpc/include/asm/sections.h +4
-2@@ -8,6 +8,10 @@#define arch_is_kernel_initmem_freed arch_is_kernel_initmem_freed +#ifdef PPC64_ELF_ABI_v1 +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +#include <asm-generic/sections.h> extern bool init_mem_is_free;@@ -69,8 +73,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)#ifdef PPC64_ELF_ABI_v1 -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {include/asm-generic/sections.h +0
-0@@ -59,7 +59,8 @@ extern char __noinstr_text_start[], __noinstr_text_end[]; extern __visible const void __nosave_begin, __nosave_end;
Quoted 8 lines
include/asm-generic/sections.h extern char __noinstr_text_start[], __noinstr_text_end[]; · line 63
Quoted 4 lines
include/asm-generic/sections.h +2
-1/* Function descriptor handling (if any). Override in asm/sections.h */ -#ifndef dereference_function_descriptor +#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +#else why not
#ifndefHAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
instead of #if/#else ?
To avoid changing it again in patch 6, or getting an ugly #ifndef/#else at the end.
Quoted 6 lines
Quoted 5 lines
include/asm-generic/sections.h +0
-0#define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p))#endif
Helge Deller
maintainerRe: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
* Christophe Leroy <[email protected]>:
Quoted 101 lines
Le 12/10/2021 à 08:02, Helge Deller a écrit :
Quoted 89 lines · Christophe Leroy
Quoted 88 lines
Use
HAVE_DEREFERENCE_FUNCTION_DESCRIPTORinstead of 'dereference_function_descriptor' to know whether arch has function descriptors.Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 4 ++-- arch/parisc/include/asm/sections.h | 6 ++++-- arch/powerpc/include/asm/sections.h | 6 ++++-- include/asm-generic/sections.h | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-)arch/ia64/include/asm/sections.h +2
-2@@ -7,6 +7,8 @@* David Mosberger-Tang <[email protected]> */ +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#include <linux/elf.h> #include <linux/uaccess.h> #include <asm-generic/sections.h> @@ -27,8 +29,6 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {arch/parisc/include/asm/sections.h +4
-2@@ -2,6 +2,10 @@#ifndef _PARISC_SECTIONS_H#define _PARISC_SECTIONS_H +#ifdef CONFIG_64BIT +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +/* nothing to see, move along */ #include <asm-generic/sections.h> @@ -9,8 +13,6 @@ extern char __alt_instructions[], __alt_instructions_end[];#ifdef CONFIG_64BIT -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorvoid *dereference_function_descriptor (void *);arch/powerpc/include/asm/sections.h +4
-2@@ -8,6 +8,10 @@#define arch_is_kernel_initmem_freed arch_is_kernel_initmem_freed +#ifdef PPC64_ELF_ABI_v1 +#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#endif +#include <asm-generic/sections.h> extern bool init_mem_is_free;@@ -69,8 +73,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)#ifdef PPC64_ELF_ABI_v1 -#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 -#undef dereference_function_descriptorstatic inline void *dereference_function_descriptor (void *ptr) {include/asm-generic/sections.h +0
-0@@ -59,7 +59,8 @@ extern char __noinstr_text_start[], __noinstr_text_end[]; extern __visible const void __nosave_begin, __nosave_end;Quoted 8 lines
include/asm-generic/sections.h extern char __noinstr_text_start[], __noinstr_text_end[]; · line 63
Quoted 4 lines
include/asm-generic/sections.h +2
-1/* Function descriptor handling (if any). Override in asm/sections.h */ -#ifndef dereference_function_descriptor +#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +#else why not
#ifndefHAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
instead of #if/#else ?
Quoted 2 lines
To avoid changing it again in patch 6, or getting an ugly #ifndef/#else at the end.
Ok.
Building on parisc fails at multiple files like this:
CC mm/filemap.o
In file included from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/interrupt.h:21,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/trace_recursion.h:5,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/ftrace.h:10,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/perf_event.h:49,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/trace_events.h:10,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/trace/syscall.h:7,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/include/linux/syscalls.h:87,
from /home/cvs/parisc/git-kernel/linus-linux-2.6/init/initramfs.c:11:
/home/cvs/parisc/git-kernel/linus-linux-2.6/arch/parisc/include/asm/sections.h:7:9: error: unknown type name ‘Elf64_Fdesc’
7 | typedef Elf64_Fdesc funct_descr_t;
| ^~~~~~~~~~~So, you still need e.g. this patch:
arch/parisc/include/asm/sections.h +2 -0
@@ -4,6 +4,8 @@#ifdef CONFIG_64BIT #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +#include <asm/elf.h> +#include <linux/uaccess.h> typedef Elf64_Fdesc funct_descr_t ;#endif
In general to save space I think it would be beneficial if the dereference_kernel_function_descriptor() and dereference_kernel_function_descriptor() functions would go as real functions a c-file instead of just being inlined functions in the asm-generic/sections.h header file.
Other than that it's a nice cleanup!
Helge
Michael Ellerman
reviewerRe: [PATCH v1 01/10] powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h
In reply to Christophe LeroyQuoted 1 line · Christophe Leroy
'struct
ppc64_opd_entry' doesn't belong touapi/asm/elf.h
Agree, but ...
Quoted 6 lines
It was initially in
module_64.cand commit2d291e902791("Fix compile failure with non modular builds") moved it intoasm/elf.hBut it was by mistake added outside of
__KERNEL__section, therefore commitc3617f72036c("UAPI: (Scripted) Disintegratearch/powerpc/include/asm") moved it touapi/asm/elf.h
... it's been visible to userspace since the first commit moved it, ~13 years ago in 2008, v2.6.27.
Quoted 2 lines
Move it back into
asm/elf.h, this brings it back in line with IA64 and PARISC architectures.
Removing it from the uapi header risks breaking userspace, I doubt anything uses it, but who knows.
Given how long it's been there I think it's a bit risky to remove it :/
Quoted 21 lines
arch/powerpc/include/asm/elf.h +7
-0@@ -176,4 +176,11 @@ do { \/* Relocate the kernel image to @final_address */ void relocate (unsigned long final_address); +/* There's actually a third entry here, but it's unused */ +struct ppc64_opd_entry +{ +unsigned long funcaddr; +unsigned long r2; +}; +#endif /* _ASM_POWERPC_ELF_H */ arch/powerpc/include/uapi/asm/elf.h +0
-0
arch/powerpc/include/uapi/asm/elf.h typedef elf_fpreg_t elf_vsrreghalf_t32[ELF_NVSRHALFREG]; · line 297
Quoted 9 lines
arch/powerpc/include/uapi/asm/elf.h +0
-6@@ -289,12 +289,4 @@ typedef elf_fpreg_t elf_vsrreghalf_t32[ELF_NVSRHALFREG];/* Keep this the last entry. */ #define R_PPC64_NUM 253 -/* There's actually a third entry here, but it's unused */ -struct ppc64_opd_entry -{ -unsigned long funcaddr; -unsigned long r2; -};
Rather than removing it we can make it uapi only with:
#ifndef __KERNEL__
/* There's actually a third entry here, but it's unused */ struct ppc64_opd_entry
{
unsigned long funcaddr;
unsigned long r2;
};#endif
And then we can do whatever we want with the kernel internal version.
cheers
Arnd Bergmann
maintainerRe: [PATCH v1 01/10] powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h
Quoted 9 lines · Michael Ellerman
Quoted 4 lines · Christophe Leroy
'struct
ppc64_opd_entry' doesn't belong touapi/asm/elf.hBut it was by mistake added outside of__KERNEL__section, therefore commitc3617f72036c("UAPI: (Scripted) Disintegratearch/powerpc/include/asm") moved it touapi/asm/elf.h... it's been visible to userspace since the first commit moved it, ~13 years ago in 2008, v2.6.27.
Quoted 7 lines
Quoted 2 lines
Move it back into
asm/elf.h, this brings it back in line with IA64 and PARISC architectures.Removing it from the uapi header risks breaking userspace, I doubt anything uses it, but who knows.
Given how long it's been there I think it's a bit risky to remove it :/
I would not be too worried about it. While we should absolutely never break existing binaries, changing the visibility of internal structures in header files only breaks compiling applications that do rely on these entries, and they really should not be using this in the first place.
Arnd
Re: [PATCH v1 01/10] powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h
In reply to Christophe LeroyQuoted 9 lines · Christophe Leroy
'struct
ppc64_opd_entry' doesn't belong touapi/asm/elf.hIt was initially in
module_64.cand commit2d291e902791("Fix compile failure with non modular builds") moved it intoasm/elf.hBut it was by mistake added outside of
__KERNEL__section, therefore commitc3617f72036c("UAPI: (Scripted) Disintegratearch/powerpc/include/asm") moved it touapi/asm/elf.h
Quoted 5 lines
Move it back into
asm/elf.h, this brings it back in line with IA64 and PARISC architectures.Fixes 2d291e902791 ("Fix compile failure with non modular builds")Signed-off-by Christophe Leroy <[email protected]>
I'd agree with Arnd: this is a reasonable cleanup and nothing should be using it.
Signature · 1 line
Kees Cook
Re: [PATCH v1 02/10] powerpc: Rename 'funcaddr' to 'addr' in 'struct ppc64_opd_entry'
In reply to Christophe LeroyQuoted 11 lines · Christophe Leroy
There are three architectures with function descriptors, try to have common names for the address they contain in order to refactor some functions into generic functions later.
powerpc has 'funcaddr'
ia64 has 'ip'
parisc has 'addr'Vote for 'addr' and update 'struct
ppc64_opd_entry' accordingly.Signed-off-by Christophe Leroy <[email protected]>
Reasonable. :)
Signature · 1 line
Kees Cook
Re: [PATCH v1 03/10] ia64: Rename 'ip' to 'addr' in 'struct fdesc'
In reply to Christophe LeroyQuoted 11 lines · Christophe Leroy
There are three architectures with function descriptors, try to have common names for the address they contain in order to refactor some functions into generic functions later.
powerpc has 'funcaddr'
ia64 has 'ip'
parisc has 'addr'Vote for 'addr' and update 'struct fdesc' accordingly.
Signed-off-by Christophe Leroy <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
In reply to Christophe LeroyQuoted 4 lines · Christophe Leroy
Use
HAVE_DEREFERENCE_FUNCTION_DESCRIPTORinstead of 'dereference_function_descriptor' to know whether arch has function descriptors.Signed-off-by Christophe Leroy <[email protected]>
I'd mention the intentionally-empty #if/#else in the commit log, as I, like Helge, mentally tripped over it in the review. :)
Signature · 1 line
Kees Cook
Re: [PATCH v1 05/10] asm-generic: Define 'funct_descr_t' to commonly describe function descriptors
In reply to Christophe LeroyQuoted 6 lines · Christophe Leroy
We have three architectures using function descriptors, each with its own name.
Add a common typedef that can be used in generic code.
Also add a stub typedef for architecture without function descriptors,
nit: funct_descr_t reads weird to me. why not func_desc_t ? Either way:
Quoted 63 lines
to avoid a forest of #ifdefs.
Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 1 + arch/parisc/include/asm/sections.h | 1 + arch/powerpc/include/asm/sections.h | 1 + include/asm-generic/sections.h | 3 +++ 4 files changed, 6 insertions(+)arch/ia64/include/asm/sections.h +1
-0@@ -8,6 +8,7 @@*/ #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct fdesc funct_descr_t ;#include <linux/elf.h> #include <linux/uaccess.h> arch/parisc/include/asm/sections.h +1
-0@@ -4,6 +4,7 @@#ifdef CONFIG_64BIT #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef Elf64_Fdesc funct_descr_t ;#endif /* nothing to see, move along */ arch/powerpc/include/asm/sections.h +1
-0@@ -10,6 +10,7 @@#ifdef PPC64_ELF_ABI_v1#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct ppc64_opd_entry funct_descr_t ;#endif #include <asm-generic/sections.h> include/asm-generic/sections.h +3
-0@@ -63,6 +63,9 @@ extern __visible const void __nosave_begin, __nosave_end;#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p)) +typedef struct { +unsigned long addr; +}funct_descr_t ;#endif /* random extra sections (if any). Override Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
Re: [PATCH v1 06/10] asm-generic: Refactor dereference_[kernel]_function_descriptor()
In reply to Christophe LeroyQuoted 6 lines · Christophe Leroy
dereference_function_descriptor()anddereference_kernel_function_descriptor()are identical on the three architectures implementing them.Make it common.
Quoted 8 lines
Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 19 ------------------- arch/parisc/include/asm/sections.h | 9 --------- arch/parisc/kernel/process.c | 21 --------------------- arch/powerpc/include/asm/sections.h | 23 ----------------------- include/asm-generic/sections.h | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+), 72 deletions(-)
A diffstat to love. :)
Quoted 145 lines
arch/ia64/include/asm/sections.h +0
-19@@ -30,23 +30,4 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_bextern char __start_unwind[], __end_unwind[];extern char __start_ivt_text[], __end_ivt_text[]; -#undef dereference_function_descriptor -static inline void *dereference_function_descriptor (void *ptr) -{ -struct fdesc *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -#undef dereference_kernel_function_descriptor -static inline void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) -return ptr; -return dereference_function_descriptor (ptr); -} -#endif /* _ASM_IA64_SECTIONS_H */ arch/parisc/include/asm/sections.h +0
-9@@ -12,13 +12,4 @@ typedef Elf64_Fdesc funct_descr_t;extern char __alt_instructions[], __alt_instructions_end[]; -#ifdef CONFIG_64BIT - -#undef dereference_function_descriptor -void *dereference_function_descriptor (void *); - -#undef dereference_kernel_function_descriptor -void *dereference_kernel_function_descriptor (void *); -#endif -#endif arch/parisc/kernel/process.c +0
-21@@ -266,27 +266,6 @@ get_wchan(struct task_struct *p)return 0 ; } -#ifdef CONFIG_64BIT -void *dereference_function_descriptor (void *ptr) -{ -Elf64_Fdesc *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || - ptr >= (void *)__end_opd) -return ptr; - -return dereference_function_descriptor (ptr); -} -#endif -static inline unsigned long brk_rnd (void ) {return (get_random_int () &BRK_RND_MASK ) <<PAGE_SHIFT ;arch/powerpc/include/asm/sections.h +0
-23@@ -72,29 +72,6 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end) (unsigned long )_stext < end; } -#ifdef PPC64_ELF_ABI_v1 - -#undef dereference_function_descriptor -static inline void *dereference_function_descriptor (void *ptr) -{ -struct ppc64_opd_entry *desc = ptr; -void *p; - -if (!get_kernel_nofault (p, (void *)&desc->addr)) - ptr = p; -return ptr; -} - -#undef dereference_kernel_function_descriptor -static inline void *dereference_kernel_function_descriptor (void *ptr) -{ -if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) -return ptr; - -return dereference_function_descriptor (ptr); -} -#endif /* PPC64_ELF_ABI_v1 */ -#endif #endif /* __KERNEL__ */ include/asm-generic/sections.h +18
-0@@ -60,6 +60,24 @@ extern __visible const void __nosave_begin, __nosave_end;/* Function descriptor handling (if any). Override in asm/sections.h */ #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR +static inline void *dereference_function_descriptor (void *ptr) +{ +funct_descr_t *desc = ptr; +void *p; + +if (!get_kernel_nofault (p, (void *)&desc->addr)) + ptr = p; +return ptr; +} + +static inline void *dereference_kernel_function_descriptor (void *ptr) +{ +if (ptr < (void *)__start_opd || ptr >= (void *)__end_opd) +return ptr; + +return dereference_function_descriptor (ptr); +} +#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p))Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
Re: [PATCH v1 07/10] lkdtm: Force do_nothing() out of line
In reply to Christophe LeroyQuoted 8 lines · Christophe Leroy
LKDTM tests display that the run
do_nothing()at a given address, but in realitydo_nothing()is inlined into the caller.Force it out of line so that it really runs text at the displayed address.
Signed-off-by Christophe Leroy <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN
In reply to Christophe LeroyQuoted 22 lines · Christophe Leroy
WRITE_KERNis supposed to overwrite some kernel text, namelydo_overwritten()function.But at the time being it overwrites
do_overwritten()function descriptor, not function text.Fix it by dereferencing the function descriptor to obtain function text pointer.
And make
do_overwritten()noinline so that it is reallydo_overwritten()which is called bylkdtm_WRITE_KERN().Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/misc/lkdtm/perms.c +0
-0
drivers/misc/lkdtm/perms.c line 8
Quoted 4 lines
drivers/misc/lkdtm/perms.c +1
-0@@ -5,6 +5,7 @@* even non-readable regions. */ #include "lkdtm.h" +#include <linux/kallsyms.h>
Why not #include <asm/sections.h> instead here?
Quoted 13 lines
drivers/misc/lkdtm/perms.c +1
-1#include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/mman.h> @@ -37,7 +38,7 @@ static noinline void do_nothing(void) }/* Must immediately follow do_nothing for size calculuations to work out. */ -static void do_overwritten (void ) +static noinline void do_overwritten(void) {pr_info ("do_overwritten wasn't overwritten!\n" );return ;
drivers/misc/lkdtm/perms.c void lkdtm_WRITE_KERN(void) · line 119
Quoted 8 lines
drivers/misc/lkdtm/perms.c +3
-2@@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void)size_t size;volatile unsigned char *ptr; - size = (unsigned long )do_overwritten - (unsigned long )do_nothing; - ptr = (unsigned char *)do_overwritten; + size = (unsigned long )dereference_symbol_descriptor (do_overwritten) - + (unsigned long )dereference_symbol_descriptor (do_nothing); + ptr =dereference_symbol_descriptor (do_overwritten);
But otherwise, yup, I expect there will be a bunch of things like this to clean up in LKDTM. :| Sorry about that!
Quoted 6 lines
drivers/misc/lkdtm/perms.c +0
-0pr_info ("attempting bad %zu byte write at %px\n" , size, ptr);memcpy ((void *)ptr, (unsigned char *)do_nothing, size);Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Christophe LeroyQuoted 4 lines · Christophe Leroy
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().
I don't understand this. Why does the next patch not fix this?
-Kees
Quoted 31 lines
And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +8
-1@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void)void lkdtm_EXEC_RODATA (void ) { -execute_location (lkdtm_rodata_do_nothing,CODE_AS_IS ); +pr_info ("attempting ok execution at %px\n" , +dereference_symbol_descriptor (do_nothing)); +do_nothing (); + +pr_info ("attempting bad execution at %px\n" , +dereference_symbol_descriptor (lkdtm_rodata_do_nothing)); +lkdtm_rodata_do_nothing (); +pr_err ("FAIL: func returned\n" ); }void lkdtm_EXEC_USERSPACE (void )Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
Re: [PATCH v1 10/10] lkdtm: Fix execute_[user]_location()
In reply to Christophe LeroyQuoted 55 lines · Christophe Leroy
execute_location()andexecute_user_location()intent
to copydo_nothing()text and execute it at a new location. However, at the time being it doesn't copydo_nothing()function butdo_nothing()function descriptor which still points to the original text. So at the end it still executesdo_nothing()at its original location allthough using a copied function descriptor.So, fix that by really copying
do_nothing()text and build a new function descriptor by copyingdo_nothing()function descriptor and updating the target address with the new location.Also fix the displayed addresses by dereferencing
do_nothing()function descriptor.Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-)drivers/misc/lkdtm/perms.c +21
-2@@ -44,19 +44,42 @@ static noinline void do_overwritten(void)return ; } +static void *setup_function_descriptor (funct_descr_t *fdesc,void *dst) +{ +int err; + +if (!__is_defined (HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR )) +return dst; + + err =copy_from_kernel_nofault (fdesc, do_nothing,sizeof (*fdesc)); +if (err <0 ) +return ERR_PTR (err); + + fdesc->addr = (unsigned long )dst; +barrier (); + +return fdesc; +} + static noinline void execute_location(void *dst, bool write) { -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing ();
drivers/misc/lkdtm/perms.c static noinline void do_overwritten(void) · line 80
Quoted 10 lines
drivers/misc/lkdtm/perms.c +4
-2if (write =CODE_WRITE ) { -memcpy (dst, do_nothing,EXEC_SIZE ); +memcpy (dst, do_nothing_text,EXEC_SIZE );flush_icache_range ((unsigned long )dst, (unsigned long )dst +EXEC_SIZE ); } -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ;
I think this error case should complain with some details. :) Maybe:
pr_err("FAIL: could not build function descriptor for %px\n", dst);drivers/misc/lkdtm/perms.c static noinline void do_overwritten(void) · line 82
Quoted 2 lines
drivers/misc/lkdtm/perms.c +2
-0+ +pr_info ("attempting bad execution at %px\n" , dst);
And then leave this pr_info() as it was, before the setup_function_descriptor() call.
Quoted 17 lines
drivers/misc/lkdtm/perms.c +4
-3func ();pr_err ("FAIL: func returned\n" ); }@@ -66,16 +89,22 @@ static void execute_user_location(void *dst)int copied;/* Intentionally crossing kernel/user memory boundary. */ -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing (); - copied = access_process_vm(current, (unsigned long)dst, do_nothing,
drivers/misc/lkdtm/perms.c static void execute_user_location(void *dst) · line 107
Quoted 10 lines
drivers/misc/lkdtm/perms.c +6
-1+ copied =access_process_vm (current, (unsigned long )dst, do_nothing_text,EXEC_SIZE ,FOLL_WRITE );if (copied <EXEC_SIZE )return ; -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ; + +pr_info ("attempting bad execution at %px\n" , dst);
Same here.
Quoted 6 lines
drivers/misc/lkdtm/perms.c +0
-0func ();pr_err ("FAIL: func returned\n" ); }Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Christophe LeroyQuoted 20 lines · Christophe Leroy
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +0
-0@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void)void lkdtm_EXEC_RODATA (void ) {
drivers/misc/lkdtm/perms.c void lkdtm_EXEC_VMALLOC(void) · line 164
Quoted 10 lines
drivers/misc/lkdtm/perms.c +8
-1-execute_location (lkdtm_rodata_do_nothing,CODE_AS_IS ); +pr_info ("attempting ok execution at %px\n" , +dereference_symbol_descriptor (do_nothing)); +do_nothing (); + +pr_info ("attempting bad execution at %px\n" , +dereference_symbol_descriptor (lkdtm_rodata_do_nothing)); +lkdtm_rodata_do_nothing (); +pr_err ("FAIL: func returned\n" ); }
(In re-reading this more carefully, I see now why kallsyms.h is used
earlier: _function_ vs _symbol_ descriptor.)
In the next patch:
static noinline void execute_location(void *dst, bool write) {
...
func = setup_function_descriptor(&fdesc, dst);
if (IS_ERR(func))
return;
pr_info("attempting bad execution at %px\n", dst);
func();
pr_err("FAIL: func returned\n");
}What are the conditions for which dereference_symbol_descriptor works but dereference _function_descriptor doesn't?
--
Kees Cook
Re: [PATCH v1 05/10] asm-generic: Define 'funct_descr_t' to commonly describe function descriptors
In reply to Kees CookLe 13/10/2021 à 09:01, Kees Cook a écrit :
Quoted 9 lines · Christophe Leroy
Quoted 6 lines
We have three architectures using function descriptors, each with its own name.
Add a common typedef that can be used in generic code.
Also add a stub typedef for architecture without function descriptors,
nit:
funct_descr_treads weird to me. why notfunc_desc_t? Either way:
func_desc_t already exists in powerpc. I have a patch to remove it as it is redundant with struct ppc64_opd_entry, but I didnt' want to include it in this series.
But after all I can add it in this series, I'll add it in v2.
Christophe
Quoted 68 lines
Reviewed-by Kees Cook <[email protected]>Quoted 63 lines
to avoid a forest of #ifdefs.
Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 1 + arch/parisc/include/asm/sections.h | 1 + arch/powerpc/include/asm/sections.h | 1 + include/asm-generic/sections.h | 3 +++ 4 files changed, 6 insertions(+)arch/ia64/include/asm/sections.h +1
-0@@ -8,6 +8,7 @@*/ #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct fdesc funct_descr_t ;#include <linux/elf.h> #include <linux/uaccess.h> arch/parisc/include/asm/sections.h +1
-0@@ -4,6 +4,7 @@#ifdef CONFIG_64BIT #define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef Elf64_Fdesc funct_descr_t ;#endif /* nothing to see, move along */ arch/powerpc/include/asm/sections.h +1
-0@@ -10,6 +10,7 @@#ifdef PPC64_ELF_ABI_v1#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1 +typedef struct ppc64_opd_entry funct_descr_t ;#endif #include <asm-generic/sections.h> include/asm-generic/sections.h +3
-0@@ -63,6 +63,9 @@ extern __visible const void __nosave_begin, __nosave_end;#else #define dereference_function_descriptor (p) ((void *)(p))#define dereference_kernel_function_descriptor (p) ((void *)(p)) +typedef struct { +unsigned long addr; +}funct_descr_t ;#endif /* random extra sections (if any). Override Signature · 1 line
2.31.1
Re: [PATCH v1 05/10] asm-generic: Define 'funct_descr_t' to commonly describe function descriptors
Quoted 11 lines · Christophe Leroy
Le 13/10/2021 à 09:01, Kees Cook a écrit :
Quoted 8 lines · Christophe Leroy
Quoted 6 lines
We have three architectures using function descriptors, each with its own name.
Add a common typedef that can be used in generic code.
Also add a stub typedef for architecture without function descriptors,
Quoted 7 lines
Quoted 1 line
nit:
funct_descr_treads weird to me. why notfunc_desc_t? Either way:
func_desc_talready exists in powerpc. I have a patch to remove it as it is redundant with structppc64_opd_entry, but I didnt' want to include it in this series.But after all I can add it in this series, I'll add it in v2.
Ah-ha! That works for me. :) Thanks!
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN
In reply to Kees CookLe 13/10/2021 à 09:05, Kees Cook a écrit :
Quoted 18 lines · Christophe Leroy
Quoted 17 lines
WRITE_KERNis supposed to overwrite some kernel text, namelydo_overwritten()function.But at the time being it overwrites
do_overwritten()function descriptor, not function text.Fix it by dereferencing the function descriptor to obtain function text pointer.
And make
do_overwritten()noinline so that it is reallydo_overwritten()which is called bylkdtm_WRITE_KERN().Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
Quoted 11 lines
drivers/misc/lkdtm/perms.c line 8
Quoted 9 lines
drivers/misc/lkdtm/perms.c +1
-0@@ -5,6 +5,7 @@* even non-readable regions. */ #include "lkdtm.h" +#include <linux/kallsyms.h> Why not #include <
asm/sections.h> instead here?
dereference_symbol_descriptor() is defined in linux/kallsyms.h
Quoted 35 lines
Quoted 14 lines
drivers/misc/lkdtm/perms.c +1
-1#include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/mman.h> @@ -37,7 +38,7 @@ static noinline void do_nothing(void) }/* Must immediately follow do_nothing for size calculuations to work out. */ -static void do_overwritten (void ) +static noinline void do_overwritten(void) {pr_info ("do_overwritten wasn't overwritten!\n" );return ;drivers/misc/lkdtm/perms.c void lkdtm_WRITE_KERN(void) · line 119
Quoted 8 lines
drivers/misc/lkdtm/perms.c +3
-2@@ -113,8 +114,9 @@ void lkdtm_WRITE_KERN(void)size_t size;volatile unsigned char *ptr; - size = (unsigned long )do_overwritten - (unsigned long )do_nothing; - ptr = (unsigned char *)do_overwritten; + size = (unsigned long )dereference_symbol_descriptor (do_overwritten) - + (unsigned long )dereference_symbol_descriptor (do_nothing); + ptr =dereference_symbol_descriptor (do_overwritten);But otherwise, yup, I expect there will be a bunch of things like this to clean up in LKDTM. :| Sorry about that!
Acked-by Kees Cook <[email protected]>Quoted 6 lines
drivers/misc/lkdtm/perms.c +0
-0pr_info ("attempting bad %zu byte write at %px\n" , size, ptr);memcpy ((void *)ptr, (unsigned char *)do_nothing, size);Signature · 1 line
2.31.1
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Kees CookLe 13/10/2021 à 09:09, Kees Cook a écrit :
Quoted 7 lines · Christophe Leroy
Quoted 4 lines
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().I don't understand this. Why does the next patch not fix this?
Well, probably it would, but it looked incorrect in my mind.
lkdtm_rodata_do_nothing() is a function which has its own function descriptor, it is not a copy of do_nothing().
If we use execute_location() as modified by next patch, then we will execute it using the function descriptor of do_nothing(). Allthough it most likely works (at least on powerpc as it uses the same TOC) it looks odd to me to do so.
Am I missing something ?
Christophe
Quoted 35 lines
-Kees
Quoted 31 lines
And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +8
-1@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void)void lkdtm_EXEC_RODATA (void ) { -execute_location (lkdtm_rodata_do_nothing,CODE_AS_IS ); +pr_info ("attempting ok execution at %px\n" , +dereference_symbol_descriptor (do_nothing)); +do_nothing (); + +pr_info ("attempting bad execution at %px\n" , +dereference_symbol_descriptor (lkdtm_rodata_do_nothing)); +lkdtm_rodata_do_nothing (); +pr_err ("FAIL: func returned\n" ); }void lkdtm_EXEC_USERSPACE (void )Signature · 1 line
2.31.1
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Kees CookLe 13/10/2021 à 09:23, Kees Cook a écrit :
Quoted 49 lines · Christophe Leroy
Quoted 20 lines
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +0
-0@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void)void lkdtm_EXEC_RODATA (void ) {drivers/misc/lkdtm/perms.c void lkdtm_EXEC_VMALLOC(void) · line 164
Quoted 10 lines
drivers/misc/lkdtm/perms.c +8
-1-execute_location (lkdtm_rodata_do_nothing,CODE_AS_IS ); +pr_info ("attempting ok execution at %px\n" , +dereference_symbol_descriptor (do_nothing)); +do_nothing (); + +pr_info ("attempting bad execution at %px\n" , +dereference_symbol_descriptor (lkdtm_rodata_do_nothing)); +lkdtm_rodata_do_nothing (); +pr_err ("FAIL: func returned\n" ); }(In re-reading this more carefully, I see now why
kallsyms.his used
earlier: _function_ vs _symbol_ descriptor.)In the next patch:
static noinline void
execute_location(void *dst, bool write){
...func = setup_function_descriptor(&fdesc, dst); if (IS_ERR(func)) return; pr_info("attempting bad execution at %px\n", dst); func(); pr_err("FAIL: func returned\n"); }
Quoted 3 lines
What are the conditions for which
dereference_symbol_descriptorworks but dereference_function_descriptordoesn't?
When LKDTM is built as a module I guess ?
Christophe
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
Le 13/10/2021 à 09:39, Christophe Leroy a écrit :
Quoted 52 lines
Le 13/10/2021 à 09:23, Kees Cook a écrit :
Quoted 49 lines · Christophe Leroy
Quoted 20 lines
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>---
drivers/misc/lkdtm/perms.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +0
-0@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void) void lkdtm_EXEC_RODATA(void) {drivers/misc/lkdtm/perms.c void lkdtm_EXEC_VMALLOC(void) · line 164
Quoted 10 lines
drivers/misc/lkdtm/perms.c +8
-1- execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); + pr_info("attempting ok execution at %px\n", + dereference_symbol_descriptor(do_nothing)); + do_nothing(); + + pr_info("attempting bad execution at %px\n", + dereference_symbol_descriptor(lkdtm_rodata_do_nothing)); + lkdtm_rodata_do_nothing(); + pr_err("FAIL: func returned\n"); }(In re-reading this more carefully, I see now why
kallsyms.his used
earlier: _function_ vs _symbol_ descriptor.)In the next patch:
static noinline void
execute_location(void *dst, bool write){
...
func =setup_function_descriptor(&fdesc, dst); if (IS_ERR(func))
return;
pr_info("attempting bad execution at %px\n", dst);func();
pr_err("FAIL: func returned\n");
}
Quoted 6 lines
Quoted 3 lines
What are the conditions for which
dereference_symbol_descriptorworks but dereference_function_descriptordoesn't?When LKDTM is built as a module I guess ?
To be more precise, dereference_symbol_descriptor() calls either dereference_kernel_function_descriptor() or dereference_module_function_descriptor()
Both functions call dereference_function_descriptor() after checking that we want to dereference something that is in the OPD section.
If we call dereference_function_descriptor() directly instead of dereference_symbol_descriptor() we skip the range verification and may dereference something that is not a function descriptor.
Should we do that ?
Christophe
Re: [PATCH v1 06/10] asm-generic: Refactor dereference_[kernel]_function_descriptor()
In reply to Kees CookLe 13/10/2021 à 09:02, Kees Cook a écrit :
Quoted 16 lines · Christophe Leroy
Quoted 6 lines
dereference_function_descriptor()anddereference_kernel_function_descriptor()are identical on the three architectures implementing them.Make it common.
Quoted 8 lines
Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 19 ------------------- arch/parisc/include/asm/sections.h | 9 --------- arch/parisc/kernel/process.c | 21 --------------------- arch/powerpc/include/asm/sections.h | 23 ----------------------- include/asm-generic/sections.h | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+), 72 deletions(-)
Quoted 3 lines
A diffstat to love. :)
Reviewed-by Kees Cook <[email protected]>
Unless somebody minds, I will make them out of line as suggested by Helge in he's comment to patch 4.
Allthough there is no spectacular size reduction, the functions are not worth being inlined as they are not used in critical pathes.
Re: [PATCH v1 06/10] asm-generic: Refactor dereference_[kernel]_function_descriptor()
Quoted 17 lines · Christophe Leroy
Quoted 16 lines · Kees Cook
Quoted 14 lines · Christophe Leroy
dereference_function_descriptor()anddereference_kernel_function_descriptor()are identical on the three architectures implementing them.Make it common.
Signed-off-by Christophe Leroy <[email protected]>--- arch/ia64/include/asm/sections.h | 19 ------------------- arch/parisc/include/asm/sections.h | 9 --------- arch/parisc/kernel/process.c | 21 --------------------- arch/powerpc/include/asm/sections.h | 23 ----------------------- include/asm-generic/sections.h | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+), 72 deletions(-)
Quoted 3 lines
Quoted 3 lines
A diffstat to love. :)
Reviewed-by Kees Cook <[email protected]>
Quoted 5 lines
Unless somebody minds, I will make them out of line as suggested by Helge in he's comment to patch 4.
Allthough there is no spectacular size reduction, the functions are not worth being inlined as they are not used in critical pathes.
Sounds good to me.
Arnd
Re: [PATCH v1 10/10] lkdtm: Fix execute_[user]_location()
In reply to Kees CookLe 13/10/2021 à 09:16, Kees Cook a écrit :
Quoted 69 lines · Christophe Leroy
Quoted 55 lines
execute_location()andexecute_user_location()intent
to copydo_nothing()text and execute it at a new location. However, at the time being it doesn't copydo_nothing()function butdo_nothing()function descriptor which still points to the original text. So at the end it still executesdo_nothing()at its original location allthough using a copied function descriptor.So, fix that by really copying
do_nothing()text and build a new function descriptor by copyingdo_nothing()function descriptor and updating the target address with the new location.Also fix the displayed addresses by dereferencing
do_nothing()function descriptor.Signed-off-by Christophe Leroy <[email protected]>--- drivers/misc/lkdtm/perms.c | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-)drivers/misc/lkdtm/perms.c +21
-2@@ -44,19 +44,42 @@ static noinline void do_overwritten(void)return ; } +static void *setup_function_descriptor (funct_descr_t *fdesc,void *dst) +{ +int err; + +if (!__is_defined (HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR )) +return dst; + + err =copy_from_kernel_nofault (fdesc, do_nothing,sizeof (*fdesc)); +if (err <0 ) +return ERR_PTR (err); + + fdesc->addr = (unsigned long )dst; +barrier (); + +return fdesc; +} + static noinline void execute_location(void *dst, bool write) { -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing ();drivers/misc/lkdtm/perms.c static noinline void do_overwritten(void) · line 80
Quoted 10 lines
drivers/misc/lkdtm/perms.c +4
-2if (write =CODE_WRITE ) { -memcpy (dst, do_nothing,EXEC_SIZE ); +memcpy (dst, do_nothing_text,EXEC_SIZE );flush_icache_range ((unsigned long )dst, (unsigned long )dst +EXEC_SIZE ); } -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ;I think this error case should complain with some details. :) Maybe:
Quoted 1 line
pr_err("FAIL: could not build function descriptor for %px\n", dst);Ok, I was going to add that in v2, but after one more thought I realise that there's no need to use copy_from_kernel_nofault() here, we are copying a static object into our stack, so a memcpy() will be good enough.
Quoted 44 lines
drivers/misc/lkdtm/perms.c static noinline void do_overwritten(void) · line 82
Quoted 3 lines
drivers/misc/lkdtm/perms.c +2
-0+ +pr_info ("attempting bad execution at %px\n" , dst);And then leave this
pr_info()as it was, before thesetup_function_descriptor()call.Quoted 17 lines
drivers/misc/lkdtm/perms.c +4
-3func ();pr_err ("FAIL: func returned\n" ); }@@ -66,16 +89,22 @@ static void execute_user_location(void *dst)int copied;/* Intentionally crossing kernel/user memory boundary. */ -void (*func)(void ) = dst; +void (*func)(void ); +funct_descr_t fdesc; +void *do_nothing_text =dereference_symbol_descriptor (do_nothing); -pr_info ("attempting ok execution at %px\n" , do_nothing); +pr_info ("attempting ok execution at %px\n" , do_nothing_text);do_nothing (); - copied = access_process_vm(current, (unsigned long)dst, do_nothing,drivers/misc/lkdtm/perms.c static void execute_user_location(void *dst) · line 107
Quoted 10 lines
drivers/misc/lkdtm/perms.c +6
-1+ copied =access_process_vm (current, (unsigned long )dst, do_nothing_text,EXEC_SIZE ,FOLL_WRITE );if (copied <EXEC_SIZE )return ; -pr_info ("attempting bad execution at %px\n" , func); + func =setup_function_descriptor (&fdesc, dst); +if (IS_ERR (func)) +return ; + +pr_info ("attempting bad execution at %px\n" , dst);Same here.
Quoted 6 lines
drivers/misc/lkdtm/perms.c +0
-0func ();pr_err ("FAIL: func returned\n" ); }Signature · 1 line
2.31.1
Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()
In reply to Christophe LeroyLe 13/10/2021 à 09:48, Christophe Leroy a écrit :
Quoted 66 lines
Le 13/10/2021 à 09:39, Christophe Leroy a écrit :
Quoted 52 lines
Le 13/10/2021 à 09:23, Kees Cook a écrit :
Quoted 49 lines · Christophe Leroy
Quoted 20 lines
Behind a location,
lkdtm_EXEC_RODATA()executes a real function, not a copy ofdo_nothing().So do it directly instead of using
execute_location().And fix displayed addresses by dereferencing the function descriptors.
Signed-off-by Christophe Leroy <[email protected]>---
drivers/misc/lkdtm/perms.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)drivers/misc/lkdtm/perms.c +0
-0@@ -153,7 +153,14 @@ void lkdtm_EXEC_VMALLOC(void) void lkdtm_EXEC_RODATA(void) {drivers/misc/lkdtm/perms.c void lkdtm_EXEC_VMALLOC(void) · line 164
Quoted 10 lines
drivers/misc/lkdtm/perms.c +8
-1- execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); + pr_info("attempting ok execution at %px\n", + dereference_symbol_descriptor(do_nothing)); + do_nothing(); + + pr_info("attempting bad execution at %px\n", + dereference_symbol_descriptor(lkdtm_rodata_do_nothing)); + lkdtm_rodata_do_nothing(); + pr_err("FAIL: func returned\n"); }(In re-reading this more carefully, I see now why
kallsyms.his used
earlier: _function_ vs _symbol_ descriptor.)In the next patch:
static noinline void
execute_location(void *dst, bool write){
...
func =setup_function_descriptor(&fdesc, dst); if (IS_ERR(func))
return;
pr_info("attempting bad execution at %px\n", dst);func();
pr_err("FAIL: func returned\n");
}Quoted 6 lines
Quoted 3 lines
What are the conditions for which
dereference_symbol_descriptorworks but dereference_function_descriptordoesn't?When LKDTM is built as a module I guess ?
To be more precise,
dereference_symbol_descriptor()calls eitherdereference_kernel_function_descriptor()ordereference_module_function_descriptor()
Quoted 9 lines
Both functions call
dereference_function_descriptor()after checking that we want to dereference something that is in the OPD section.If we call
dereference_function_descriptor()directly instead ofdereference_symbol_descriptor()we skip the range verification and may dereference something that is not a function descriptor.Should we do that ?
Indeed we are using it only for well known functions so using dereference_function_descriptor() is good enough. I'll use that in v2.
Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs
In reply to Kees CookLe 13/10/2021 à 09:00, Kees Cook a écrit :
Quoted 11 lines · Christophe Leroy
Quoted 4 lines
Use
HAVE_DEREFERENCE_FUNCTION_DESCRIPTORinstead of 'dereference_function_descriptor' to know whether arch has function descriptors.Signed-off-by Christophe Leroy <[email protected]>I'd mention the intentionally-empty #if/#else in the commit log, as I, like Helge, mentally tripped over it in the review. :)
Reviewed-by Kees Cook <[email protected]>
Ok, I did it in v2.
I also renamed it HAVE_FUNCTION_DESCRIPTORS because behind the fact that it has some functions to dereference function descriptor, the main fact is that they have and use function descriptors.
Christophe