Skip to contentSkip to conversation

Fix LKDTM for PPC64/IA64/PARISC

PATCH Superseded Christophe Leroy 5 participants 38 messages Oct 11, 2021 – Oct 14, 2021

Subsystems (7)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes 2d291e902791 ("Fix compile failure with non modular builds")
---
 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
index b8425e3cfd81..64b523848cd7 100644
@@ -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
index 860c59291bfc..308857123a08 100644
@@ -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
#5 lore raw

PATCH 2/10 powerpc: Rename 'funcaddr' to 'addr' in 'struct ppc64_opd_entry'

In reply to 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.

---
 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
index 64b523848cd7..90c3259a81ab 100644
@@ -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
index 6e4af4492a14..32e7035863ac 100644
@@ -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
index 6baa676e7cb6..82908c9be627 100644
@@ -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
#7 lore raw

PATCH 3/10 ia64: Rename 'ip' to 'addr' in 'struct fdesc'

In reply to 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.

---
 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
index 6629301a2620..2ef5f9966ad1 100644
@@ -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
index 3a033d2008b3..35f24e52149a 100644
@@ -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
index 2cba53c1da82..4f6400cbf79e 100644
@@ -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
#9 lore raw

PATCH 4/10 asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs

In reply to Christophe Leroy

Use 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
index 35f24e52149a..80f5868afb06 100644
@@ -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_b
 extern char __start_unwind[], __end_unwind[];
 extern char __start_ivt_text[], __end_ivt_text[];
 
-#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1
-
 #undef dereference_function_descriptor
 static inline void *dereference_function_descriptor(void *ptr)
 {
arch/parisc/include/asm/sections.h +4 -2
index bb52aea0cb21..2e781ee19b66 100644
@@ -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_descriptor
 void *dereference_function_descriptor(void *);
 
arch/powerpc/include/asm/sections.h +4 -2
index 32e7035863ac..b7f1ba04e756 100644
@@ -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_descriptor
 static inline void *dereference_function_descriptor(void *ptr)
 {
include/asm-generic/sections.h +2 -1
index d16302d3eb59..1db5cfd69817 100644
@@ -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
#15 lore raw

PATCH 5/10 asm-generic: Define 'funct_descr_t' to commonly describe function descriptors

In reply to 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, 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
index 80f5868afb06..929b5c535620 100644
@@ -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
index 2e781ee19b66..329e80f7af0a 100644
@@ -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
index b7f1ba04e756..d0d5287fa568 100644
@@ -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
index 1db5cfd69817..436412d94054 100644
@@ -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
#19 lore raw

PATCH 6/10 asm-generic: Refactor dereference_[kernel]_function_descriptor()

In reply to Christophe Leroy

dereference_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
index 929b5c535620..d9addaea8339 100644
@@ -30,23 +30,4 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_b
 extern 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
index 329e80f7af0a..b041fb32a300 100644
@@ -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
index 38ec4ae81239..7382576b52a8 100644
@@ -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
index d0d5287fa568..8f8e95f234e2 100644
@@ -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
index 436412d94054..5baaf9d7c671 100644
@@ -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
#23 lore raw

PATCH 7/10 lkdtm: Force do_nothing() out of line

In reply to Christophe Leroy

LKDTM 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
index 2dede2ef658f..60b3b2fe929d 100644
@@ -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
#25 lore raw

PATCH 8/10 lkdtm: Really write into kernel text in WRITE_KERN

In reply to Christophe Leroy

WRITE_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
index 60b3b2fe929d..442d60ed25ef 100644
@@ -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
#28 lore raw

PATCH 9/10 lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Christophe Leroy

Behind 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
index 442d60ed25ef..da16564e1ecd 100644
@@ -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
#35 lore raw

PATCH 10/10 lkdtm: Fix execute_[user]_location()

In reply to Christophe Leroy

execute_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
index da16564e1ecd..1d03cd44c21d 100644
@@ -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
maintainer
#10 lore raw

Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs

In reply to Christophe Leroy
Quoted 86 lines · Christophe Leroy

Use 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
index 35f24e52149a..80f5868afb06 100644
@@ -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_b
 extern char __start_unwind[], __end_unwind[];
 extern char __start_ivt_text[], __end_ivt_text[];
 
-#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1
-
 #undef dereference_function_descriptor
 static inline void *dereference_function_descriptor(void *ptr)
 {
arch/parisc/include/asm/sections.h +4 -2
index bb52aea0cb21..2e781ee19b66 100644
@@ -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_descriptor
 void *dereference_function_descriptor(void *);
 
arch/powerpc/include/asm/sections.h +4 -2
index 32e7035863ac..b7f1ba04e756 100644
@@ -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_descriptor
 static inline void *dereference_function_descriptor(void *ptr)
 {
include/asm-generic/sections.h +0 -0
index d16302d3eb59..1db5cfd69817 100644

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
#11 lore raw

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_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
index 35f24e52149a..80f5868afb06 100644
@@ -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_b
  extern char __start_unwind[], __end_unwind[];
  extern char __start_ivt_text[], __end_ivt_text[];
 
-#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1
-
  #undef dereference_function_descriptor
  static inline void *dereference_function_descriptor(void *ptr)
  {
arch/parisc/include/asm/sections.h +4 -2
index bb52aea0cb21..2e781ee19b66 100644
@@ -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_descriptor
  void *dereference_function_descriptor(void *);
 
arch/powerpc/include/asm/sections.h +4 -2
index 32e7035863ac..b7f1ba04e756 100644
@@ -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_descriptor
  static inline void *dereference_function_descriptor(void *ptr)
  {
include/asm-generic/sections.h +0 -0
index d16302d3eb59..1db5cfd69817 100644
@@ -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
#ifndef HAVE_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
maintainer
#12 lore raw

Re: [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_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
index 35f24e52149a..80f5868afb06 100644
@@ -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_b
  extern char __start_unwind[], __end_unwind[];
  extern char __start_ivt_text[], __end_ivt_text[];
 
-#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1
-
  #undef dereference_function_descriptor
  static inline void *dereference_function_descriptor(void *ptr)
  {
arch/parisc/include/asm/sections.h +4 -2
index bb52aea0cb21..2e781ee19b66 100644
@@ -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_descriptor
  void *dereference_function_descriptor(void *);
 
arch/powerpc/include/asm/sections.h +4 -2
index 32e7035863ac..b7f1ba04e756 100644
@@ -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_descriptor
  static inline void *dereference_function_descriptor(void *ptr)
  {
include/asm-generic/sections.h +0 -0
index d16302d3eb59..1db5cfd69817 100644
@@ -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
#ifndef HAVE_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
index b041fb32a300..177983490e7c 100644
@@ -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

#2 lore raw

Re: [PATCH v1 01/10] powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h

In reply to Christophe Leroy
Quoted 1 line · Christophe Leroy

'struct ppc64_opd_entry' doesn't belong to uapi/asm/elf.h

Agree, but ...

Quoted 6 lines

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

... 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
index b8425e3cfd81..64b523848cd7 100644
@@ -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
index 860c59291bfc..308857123a08 100644

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

maintainer
#3 lore raw

Re: [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 to uapi/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

... 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

Reviewed-by on v1 1/10
#4 lore raw

Re: [PATCH v1 01/10] powerpc: Move 'struct ppc64_opd_entry' back into asm/elf.h

In reply to Christophe Leroy
Quoted 9 lines · Christophe Leroy

'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

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")

I'd agree with Arnd: this is a reasonable cleanup and nothing should be using it.

Signature · 1 line
Kees Cook
Reviewed-by on v1 2/10
#6 lore raw

Re: [PATCH v1 02/10] powerpc: Rename 'funcaddr' to 'addr' in 'struct ppc64_opd_entry'

In reply to Christophe Leroy
Quoted 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.

Reasonable. :)

Signature · 1 line
Kees Cook
Reviewed-by on v1 3/10
#8 lore raw

Re: [PATCH v1 03/10] ia64: Rename 'ip' to 'addr' in 'struct fdesc'

In reply to Christophe Leroy
Quoted 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.

Signature · 1 line
Kees Cook
Reviewed-by on v1 4/10
#13 lore raw

Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs

In reply to Christophe Leroy
Quoted 4 lines · Christophe Leroy

Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR instead of 'dereference_function_descriptor' to know whether arch has function descriptors.

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
Reviewed-by on v1 5/10
#16 lore raw

Re: [PATCH v1 05/10] asm-generic: Define 'funct_descr_t' to commonly describe function descriptors

In reply to Christophe Leroy
Quoted 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.

---
 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
index 80f5868afb06..929b5c535620 100644
@@ -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
index 2e781ee19b66..329e80f7af0a 100644
@@ -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
index b7f1ba04e756..d0d5287fa568 100644
@@ -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
index 1db5cfd69817..436412d94054 100644
@@ -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
Reviewed-by on v1 6/10
#20 lore raw

Re: [PATCH v1 06/10] asm-generic: Refactor dereference_[kernel]_function_descriptor()

In reply to Christophe Leroy
Quoted 6 lines · Christophe Leroy

dereference_function_descriptor() and dereference_kernel_function_descriptor() are identical on the three architectures implementing them.

Make it common.

Quoted 8 lines
---
 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
index 929b5c535620..d9addaea8339 100644
@@ -30,23 +30,4 @@ extern char __start_gate_brl_fsys_bubble_down_patchlist[], __end_gate_brl_fsys_b
 extern 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
index 329e80f7af0a..b041fb32a300 100644
@@ -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
index 38ec4ae81239..7382576b52a8 100644
@@ -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
index d0d5287fa568..8f8e95f234e2 100644
@@ -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
index 436412d94054..5baaf9d7c671 100644
@@ -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
Acked-by on v1 7/10
#24 lore raw

Re: [PATCH v1 07/10] lkdtm: Force do_nothing() out of line

In reply to Christophe Leroy
Quoted 8 lines · Christophe Leroy

LKDTM 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.

Signature · 1 line
Kees Cook
Acked-by on v1 8/10
#26 lore raw

Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN

In reply to Christophe Leroy
Quoted 22 lines · Christophe Leroy

WRITE_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 +0 -0
index 60b3b2fe929d..442d60ed25ef 100644

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 -0
 	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
Signature · 1 line
Kees Cook
#29 lore raw

Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Christophe Leroy
Quoted 4 lines · Christophe Leroy

Behind a location, lkdtm_EXEC_RODATA() executes a real function, not a copy of do_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.

---
 drivers/misc/lkdtm/perms.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/perms.c +8 -1
index 442d60ed25ef..da16564e1ecd 100644
@@ -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
#36 lore raw

Re: [PATCH v1 10/10] lkdtm: Fix execute_[user]_location()

In reply to Christophe Leroy
Quoted 55 lines · Christophe Leroy

execute_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 +21 -2
index da16564e1ecd..1d03cd44c21d 100644
@@ -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 -2
 	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;

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 -3
 	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,

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 -0
 	func();
 	pr_err("FAIL: func returned\n");
 }
Signature · 1 line
2.31.1
Signature · 1 line
Kees Cook
#31 lore raw

Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Christophe Leroy
Quoted 20 lines · Christophe Leroy

Behind 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 +0 -0
index 442d60ed25ef..da16564e1ecd 100644
@@ -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

#17 lore raw

Re: [PATCH v1 05/10] asm-generic: Define 'funct_descr_t' to commonly describe function descriptors

In reply to Kees Cook

Le 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_t reads weird to me. why not func_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
Quoted 63 lines

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
index 80f5868afb06..929b5c535620 100644
@@ -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
index 2e781ee19b66..329e80f7af0a 100644
@@ -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
index b7f1ba04e756..d0d5287fa568 100644
@@ -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
index 1db5cfd69817..436412d94054 100644
@@ -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
#18 lore raw

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_t reads weird to me. why not func_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.

Ah-ha! That works for me. :) Thanks!

-Kees

Signature · 1 line
Kees Cook
#27 lore raw

Re: [PATCH v1 08/10] lkdtm: Really write into kernel text in WRITE_KERN

In reply to Kees Cook

Le 13/10/2021 à 09:05, Kees Cook a écrit :

Quoted 18 lines · Christophe Leroy
Quoted 17 lines

WRITE_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(-)
Quoted 11 lines

drivers/misc/lkdtm/perms.c line 8

Quoted 9 lines
drivers/misc/lkdtm/perms.c +1 -0
index 60b3b2fe929d..442d60ed25ef 100644
@@ -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!

Quoted 6 lines
drivers/misc/lkdtm/perms.c +0 -0
  	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
#30 lore raw

Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Kees Cook

Le 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 of do_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.

---
  drivers/misc/lkdtm/perms.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/perms.c +8 -1
index 442d60ed25ef..da16564e1ecd 100644
@@ -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
#32 lore raw

Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Kees Cook

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 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 +0 -0
index 442d60ed25ef..da16564e1ecd 100644
@@ -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");
}
Quoted 3 lines

What are the conditions for which dereference_symbol_descriptor works but dereference _function_descriptor doesn't?

When LKDTM is built as a module I guess ?

Christophe

#33 lore raw

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 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 +0 -0
index 442d60ed25ef..da16564e1ecd 100644
@@ -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");
}

Quoted 6 lines
Quoted 3 lines

What are the conditions for which dereference_symbol_descriptor works but dereference _function_descriptor doesn'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

#21 lore raw

Re: [PATCH v1 06/10] asm-generic: Refactor dereference_[kernel]_function_descriptor()

In reply to Kees Cook

Le 13/10/2021 à 09:02, Kees Cook a écrit :

Quoted 16 lines · Christophe Leroy
Quoted 6 lines

dereference_function_descriptor() and dereference_kernel_function_descriptor() are identical on the three architectures implementing them.

Make it common.

Quoted 8 lines
---
  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. :)

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.

maintainer Reviewed-by
#22 lore raw

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() 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(-)
Quoted 3 lines
Quoted 3 lines

A diffstat to love. :)

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

#37 lore raw

Re: [PATCH v1 10/10] lkdtm: Fix execute_[user]_location()

In reply to Kees Cook

Le 13/10/2021 à 09:16, Kees Cook a écrit :

Quoted 69 lines · Christophe Leroy
Quoted 55 lines

execute_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 +21 -2
index da16564e1ecd..1d03cd44c21d 100644
@@ -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 -2
  	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;

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 the setup_function_descriptor() call.

Quoted 17 lines
drivers/misc/lkdtm/perms.c +4 -3
  	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,

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 -0
  	func();
  	pr_err("FAIL: func returned\n");
  }
Signature · 1 line
2.31.1
#34 lore raw

Re: [PATCH v1 09/10] lkdtm: Fix lkdtm_EXEC_RODATA()

In reply to Christophe Leroy

Le 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 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 +0 -0
index 442d60ed25ef..da16564e1ecd 100644
@@ -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");
}

Quoted 6 lines
Quoted 3 lines

What are the conditions for which dereference_symbol_descriptor works but dereference _function_descriptor doesn'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()

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 of dereference_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.

#14 lore raw

Re: [PATCH v1 04/10] asm-generic: Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR to define associated stubs

In reply to Kees Cook

Le 13/10/2021 à 09:00, Kees Cook a écrit :

Quoted 11 lines · Christophe Leroy
Quoted 4 lines

Use HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR instead of 'dereference_function_descriptor' to know whether arch has function descriptors.

I'd mention the intentionally-empty #if/#else in the commit log, as I, like Helge, mentally tripped over it in the review. :)

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