Skip to contentSkip to conversation

xen/livepatch: Fix .altinstructions safety checks

PATCH In review Andrew Cooper 4 participants 13 messages Apr 17, 2023 – Apr 9, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover xen/livepatch: Fix .altinstructions safety checks

This replaces the previous singleton patch, with several build fixes found by Gitlab. I also included some feedback from Jan on patch 3.

Andrew Cooper (3):
xen/ELF: Fix ELF32 PRI formatters
arm/alternatives: Rename alt_instr fields which are used in common code xen/livepatch: Fix .altinstructions safety checks

 xen/arch/arm/alternative.c             |  6 +--
 xen/arch/arm/include/asm/alternative.h | 12 ++---
 xen/common/livepatch.c                 | 68 +++++++++++++++++++++++---
 xen/common/livepatch_elf.c             |  6 +--
 xen/include/xen/elfstructs.h           |  6 ++-
 5 files changed, 78 insertions(+), 20 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/3 xen/ELF: Fix ELF32 PRI formatters

It is rude to hide width formatting inside a PRI* macro, doubly so when it's only in one bitness of the macro.

However its fully buggy when all the users use %#"PRI because then it expands to the common trap of %#08x which does not do what the author intends.

Switch the ELF PRI formatters to use plain integer PRI's, and move the width formatting to those callers where it matters.

No practical change.

Fixes 7597fabca76e ("livepatch: Include sizes when an mismatch occurs")
Fixes 380b229634f8 ("xsplice: Implement payload loading")

---
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/common/livepatch_elf.c   | 6 +++---
 xen/include/xen/elfstructs.h | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
xen/common/livepatch_elf.c +3 -3
index 45d73912a3cd..d37a7af84be6 100644
@@ -310,12 +310,12 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
                     break;
                 }
             }
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => 0x%08"PRIxElfAddr"\n",
                     elf->name, elf->sym[i].name, st_value);
             break;
 
         case SHN_ABS:
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => 0x%08"PRIxElfAddr"\n",
                     elf->name, elf->sym[i].name, sym->st_value);
             break;
 
@@ -344,7 +344,7 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
 
             st_value += (unsigned long)elf->sec[idx].load_addr;
             if ( elf->sym[i].name )
-                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => %#"PRIxElfAddr" (%s)\n",
+                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => 0x%08"PRIxElfAddr" (%s)\n",
                        elf->name, elf->sym[i].name,
                        st_value, elf->sec[idx].name);
         }
xen/include/xen/elfstructs.h +2 -2
index 06e6f87c3d80..3124469faeb4 100644
@@ -561,8 +561,8 @@ typedef struct {
 #endif
 
 #if defined(ELFSIZE) && (ELFSIZE == 32)
-#define PRIxElfAddr	"08x"
-#define PRIuElfWord	"8u"
+#define PRIxElfAddr 	PRIx32
+#define PRIuElfWord 	PRIu32
 
 #define Elf_Ehdr	Elf32_Ehdr
 #define Elf_Phdr	Elf32_Phdr
Signature · 1 line
2.30.2
Acked-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen/ELF: Fix ELF32 PRI formatters

xen/common/livepatch_elf.c int livepatch_elf_resolve_symbols(struct livepatch_elf *elf) · line 313

Quoted 8 lines · Andrew Cooper
xen/common/livepatch_elf.c +1 -1
@@ -310,12 +310,12 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
                     break;
                 }
             }
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => 0x%08"PRIxElfAddr"\n",

I don't see what's wrong with using %# here (and below); I also don't see what value it has to zero-pad to 8 digits when the printed value either is far below 4G (when representing just a section offset) or likely far above (when representing a real address on 64-bit). But once again I'll leave judging to the maintainers.

Quoted 23 lines
xen/common/livepatch_elf.c +2 -2
                     elf->name, elf->sym[i].name, st_value);
             break;
 
         case SHN_ABS:
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => 0x%08"PRIxElfAddr"\n",
                     elf->name, elf->sym[i].name, sym->st_value);
             break;
 
@@ -344,7 +344,7 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
 
             st_value += (unsigned long)elf->sec[idx].load_addr;
             if ( elf->sym[i].name )
-                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => %#"PRIxElfAddr" (%s)\n",
+                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => 0x%08"PRIxElfAddr" (%s)\n",
                        elf->name, elf->sym[i].name,
                        st_value, elf->sec[idx].name);
         }
xen/include/xen/elfstructs.h +0 -0
index 06e6f87c3d80..3124469faeb4 100644

xen/include/xen/elfstructs.h typedef struct { · line 568

Quoted 10 lines
xen/include/xen/elfstructs.h +2 -2
@@ -561,8 +561,8 @@ typedef struct {
 #endif
 
 #if defined(ELFSIZE) && (ELFSIZE == 32)
-#define PRIxElfAddr	"08x"
-#define PRIuElfWord	"8u"
+#define PRIxElfAddr 	PRIx32
+#define PRIuElfWord 	PRIu32
 
 #define Elf_Ehdr	Elf32_Ehdr
 #define Elf_Phdr	Elf32_Phdr

This part certainly

Jan

#3 lore raw

Re: [PATCH v2 1/3] xen/ELF: Fix ELF32 PRI formatters

Quoted 14 lines · Jan Beulich

xen/common/livepatch_elf.c int livepatch_elf_resolve_symbols(struct livepatch_elf *elf) · line 313

Quoted 8 lines · Andrew Cooper
xen/common/livepatch_elf.c +1 -1
@@ -310,12 +310,12 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
                     break;
                 }
             }
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => 0x%08"PRIxElfAddr"\n",

I don't see what's wrong with using %# here (and below); I also don't see what value it has to zero-pad to 8 digits when the printed value either is far below 4G (when representing just a section offset) or likely far above (when representing a real address on 64-bit). But once again I'll leave judging to the maintainers.

Hmm - I could be persuaded to drop everything in livepatch_elf.c.  I guess that makes it more consistent with the 64bit side too.

Ross?

Quoted 32 lines
Quoted 32 lines
xen/common/livepatch_elf.c +2 -2
                     elf->name, elf->sym[i].name, st_value);
             break;
 
         case SHN_ABS:
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Absolute symbol: %s => 0x%08"PRIxElfAddr"\n",
                     elf->name, elf->sym[i].name, sym->st_value);
             break;
 
@@ -344,7 +344,7 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
 
             st_value += (unsigned long)elf->sec[idx].load_addr;
             if ( elf->sym[i].name )
-                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => %#"PRIxElfAddr" (%s)\n",
+                dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Symbol resolved: %s => 0x%08"PRIxElfAddr" (%s)\n",
                        elf->name, elf->sym[i].name,
                        st_value, elf->sec[idx].name);
         }
xen/include/xen/elfstructs.h +2 -2
index 06e6f87c3d80..3124469faeb4 100644
@@ -561,8 +561,8 @@ typedef struct {
 #endif
 
 #if defined(ELFSIZE) && (ELFSIZE == 32)
-#define PRIxElfAddr	"08x"
-#define PRIuElfWord	"8u"
+#define PRIxElfAddr 	PRIx32
+#define PRIuElfWord 	PRIu32
 
Quoted 4 lines

xen/include/xen/elfstructs.h typedef struct { · line 568

Quoted 2 lines
xen/include/xen/elfstructs.h +0 -0
 #define Elf_Ehdr	Elf32_Ehdr
 #define Elf_Phdr	Elf32_Phdr

This part certainly

Thanks.

~Andrew

Reviewed-by
#4 lore raw

Re: [PATCH v2 1/3] xen/ELF: Fix ELF32 PRI formatters

Quoted 22 lines

From: Andrew Cooper <[email protected]>
Sent: Monday, April 17, 2023 2:47 PM
To: Jan Beulich <[email protected]>; Xen-devel <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Bertrand Marquis <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>
Subject: Re: [PATCH v2 1/3] xen/ELF: Fix ELF32 PRI formatters

Quoted 14 lines · Jan Beulich

xen/common/livepatch_elf.c int livepatch_elf_resolve_symbols(struct livepatch_elf *elf) · line 313

Quoted 8 lines · Andrew Cooper
xen/common/livepatch_elf.c +1 -1
@@ -310,12 +310,12 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
                       break;
                   }
               }
-            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
+            dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => 0x%08"PRIxElfAddr"\n",

I don't see what's wrong with using %# here (and below); I also don't see what value it has to zero-pad to 8 digits when the printed value either is far below 4G (when representing just a section offset) or likely far above (when representing a real address on 64-bit). But once again I'll leave judging to the maintainers.

Quoted 2 lines

Hmm - I could be persuaded to drop everything in livepatch_elf.c.  I guess that makes it more consistent with the 64bit side too.

Indeed, I would prefer without the changes in xen/common/livepatch_elf.c

With those dropped,

#5 lore raw

PATCH v2 2/3 arm/alternatives: Rename alt_instr fields which are used in common code

In reply to Andrew Cooper

Alternatives auditing for livepatches is currently broken. To fix it, the livepatch code needs to inspect more fields of alt_instr.

Rename ARM's fields to match x86's, because:

* ARM already exposes alt_offset under the repl name via ALT_REPL_PTR()
* "alt" is somewhat ambiguous in a structure entirely about alternatives already.
* "repl", being the same number of character as orig leads to slightly neater code.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

The other option is to make alt_instr an entirely common structure, but it's already different between ARM and x86 and I'm not sure the result of doing this would result in nicer code.

---
 xen/arch/arm/alternative.c             |  6 +++---
 xen/arch/arm/include/asm/alternative.h | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)
xen/arch/arm/alternative.c +3 -3
index f00e3b9b3c11..7366af4ea646 100644
@@ -44,7 +44,7 @@ static bool branch_insn_requires_update(const struct alt_instr *alt,
         return true;
 
     replptr = (unsigned long)ALT_REPL_PTR(alt);
-    if ( pc >= replptr && pc <= (replptr + alt->alt_len) )
+    if ( pc >= replptr && pc <= (replptr + alt->repl_len) )
         return false;
 
     /*
@@ -128,9 +128,9 @@ static int __apply_alternatives(const struct alt_region *region,
             continue;
 
         if ( alt->cpufeature == ARM_CB_PATCH )
-            BUG_ON(alt->alt_len != 0);
+            BUG_ON(alt->repl_len != 0);
         else
-            BUG_ON(alt->alt_len != alt->orig_len);
+            BUG_ON(alt->repl_len != alt->orig_len);
 
         origptr = ALT_ORIG_PTR(alt);
         updptr = (void *)origptr + update_offset;
xen/arch/arm/include/asm/alternative.h +6 -6
index 1eb4b60fbb3e..d3210e82f9e5 100644
@@ -13,16 +13,16 @@
 
 struct alt_instr {
 	s32 orig_offset;	/* offset to original instruction */
-	s32 alt_offset;		/* offset to replacement instruction */
+	s32 repl_offset;	/* offset to replacement instruction */
 	u16 cpufeature;		/* cpufeature bit set for replacement */
 	u8  orig_len;		/* size of original instruction(s) */
-	u8  alt_len;		/* size of new instruction(s), <= orig_len */
+	u8  repl_len;		/* size of new instruction(s), <= orig_len */
 };
 
 /* Xen: helpers used by common code. */
 #define __ALT_PTR(a,f)		((void *)&(a)->f + (a)->f)
 #define ALT_ORIG_PTR(a)		__ALT_PTR(a, orig_offset)
-#define ALT_REPL_PTR(a)		__ALT_PTR(a, alt_offset)
+#define ALT_REPL_PTR(a)		__ALT_PTR(a, repl_offset)
 
 typedef void (*alternative_cb_t)(const struct alt_instr *alt,
 				 const uint32_t *origptr, uint32_t *updptr,
@@ -90,12 +90,12 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
 #include <asm/asm_defns.h>
 #include <asm/macros.h>
 
-.macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
+.macro altinstruction_entry orig_offset repl_offset feature orig_len repl_len
 	.word \orig_offset - .
-	.word \alt_offset - .
+	.word \repl_offset - .
 	.hword \feature
 	.byte \orig_len
-	.byte \alt_len
+	.byte \repl_len
 .endm
 
 .macro alternative_insn insn1, insn2, cap, enable = 1
Signature · 1 line
2.30.2
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] arm/alternatives: Rename alt_instr fields which are used in common code

Quoted 5 lines · Andrew Cooper

Alternatives auditing for livepatches is currently broken. To fix it, the livepatch code needs to inspect more fields of alt_instr.

Rename ARM's fields to match x86's, because:

Quoted 7 lines

* ARM already exposes alt_offset under the repl name via ALT_REPL_PTR()
* "alt" is somewhat ambiguous in a structure entirely about alternatives already.
* "repl", being the same number of character as orig leads to slightly neater code.

Quoted 87 lines

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

The other option is to make alt_instr an entirely common structure, but it's already different between ARM and x86 and I'm not sure the result of doing this would result in nicer code.

---
 xen/arch/arm/alternative.c             |  6 +++---
 xen/arch/arm/include/asm/alternative.h | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)
xen/arch/arm/alternative.c +3 -3
index f00e3b9b3c11..7366af4ea646 100644
@@ -44,7 +44,7 @@ static bool branch_insn_requires_update(const struct alt_instr *alt,
         return true;
 
     replptr = (unsigned long)ALT_REPL_PTR(alt);
-    if ( pc >= replptr && pc <= (replptr + alt->alt_len) )
+    if ( pc >= replptr && pc <= (replptr + alt->repl_len) )
         return false;
 
     /*
@@ -128,9 +128,9 @@ static int __apply_alternatives(const struct alt_region *region,
             continue;
 
         if ( alt->cpufeature == ARM_CB_PATCH )
-            BUG_ON(alt->alt_len != 0);
+            BUG_ON(alt->repl_len != 0);
         else
-            BUG_ON(alt->alt_len != alt->orig_len);
+            BUG_ON(alt->repl_len != alt->orig_len);
 
         origptr = ALT_ORIG_PTR(alt);
         updptr = (void *)origptr + update_offset;
xen/arch/arm/include/asm/alternative.h +6 -6
index 1eb4b60fbb3e..d3210e82f9e5 100644
@@ -13,16 +13,16 @@
 
 struct alt_instr {
 	s32 orig_offset;	/* offset to original instruction */
-	s32 alt_offset;		/* offset to replacement instruction */
+	s32 repl_offset;	/* offset to replacement instruction */
 	u16 cpufeature;		/* cpufeature bit set for replacement */
 	u8  orig_len;		/* size of original instruction(s) */
-	u8  alt_len;		/* size of new instruction(s), <= orig_len */
+	u8  repl_len;		/* size of new instruction(s), <= orig_len */
 };
 
 /* Xen: helpers used by common code. */
 #define __ALT_PTR(a,f)		((void *)&(a)->f + (a)->f)
 #define ALT_ORIG_PTR(a)		__ALT_PTR(a, orig_offset)
-#define ALT_REPL_PTR(a)		__ALT_PTR(a, alt_offset)
+#define ALT_REPL_PTR(a)		__ALT_PTR(a, repl_offset)
 
 typedef void (*alternative_cb_t)(const struct alt_instr *alt,
 				 const uint32_t *origptr, uint32_t *updptr,
@@ -90,12 +90,12 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
 #include <asm/asm_defns.h>
 #include <asm/macros.h>
 
-.macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
+.macro altinstruction_entry orig_offset repl_offset feature orig_len repl_len
 	.word \orig_offset - .
-	.word \alt_offset - .
+	.word \repl_offset - .
 	.hword \feature
 	.byte \orig_len
-	.byte \alt_len
+	.byte \repl_len
 .endm
 
 .macro alternative_insn insn1, insn2, cap, enable = 1
Signature · 1 line
2.30.2
#7 lore raw

PATCH v2 3/3 xen/livepatch: Fix .altinstructions safety checks

In reply to Andrew Cooper

The prior check has && vs || mixups, making it tautologically false and thus providing no safety at all. There are boundary errors too.

First start with a comment describing how the .altinstructions and .altinstr_replacement sections interact, and perform suitable cross-checking.

Second, rewrite the alt_instr loop entirely from scratch. Origin sites have non-zero size, and must be fully contained within the livepatches .text section(s). Any non-zero sized replacements must be fully contained within the .altinstr_replacement section.

Fixes f8a10174e8b1 ("xsplice: Add support for alternatives")

---
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>

v2:
* Rebase over prior patches to keep the ARM build working
* Tweak commit message and comments for clarity

As a further observation, .altinstr_replacement shouldn't survive beyond its use in apply_alternatives(), but the disp32 relative references (for x86 at least) in alt_instr force .altinstr_replacement to be close to the payload while being applied.

---
 xen/common/livepatch.c       | 68 ++++++++++++++++++++++++++++++++----
 xen/include/xen/elfstructs.h |  2 ++
 2 files changed, 64 insertions(+), 6 deletions(-)
xen/common/livepatch.c +62 -6
index c10ab1f374e0..004b5a436569 100644
@@ -803,28 +803,84 @@ static int prepare_payload(struct payload *payload,
     if ( sec )
     {
 #ifdef CONFIG_HAS_ALTERNATIVE
+        /*
+         * (As of April 2023), Alternatives are formed of:
+         * - An .altinstructions section with an array of struct alt_instr's.
+         * - An .altinstr_replacement section containing instructions.
+         *
+         * An individual alt_instr contains:
+         * - An orig reference, pointing into .text with a nonzero length
+         * - A repl reference, pointing into .altinstr_replacement
+         *
+         * It is legal to have zero-length replacements, meaning it is legal
+         * for the .altinstr_replacement section to be empty too.  An
+         * implementation detail means that a zero-length replacement's repl
+         * reference will still be in the .altinstr_replacement section.
+         */
+        const struct livepatch_elf_sec *repl_sec;
         struct alt_instr *a, *start, *end;
 
         if ( !section_ok(elf, sec, sizeof(*a)) )
             return -EINVAL;
 
+        /* Tolerate an empty .altinstructions section... */
+        if ( sec->sec->sh_size == 0 )
+            goto alt_done;
+
+        /* ... but otherwise, there needs to be something to alter... */
+        if ( payload->text_size == 0 )
+        {
+            printk(XENLOG_ERR LIVEPATCH "%s Alternatives provided, but no .text\n",
+                   elf->name);
+            return -EINVAL;
+        }
+
+        /* ... and something to be altered to. */
+        repl_sec = livepatch_elf_sec_by_name(elf, ".altinstr_replacement");
+        if ( !repl_sec )
+        {
+            printk(XENLOG_ERR LIVEPATCH "%s .altinstructions provided, but no .altinstr_replacement\n",
+                   elf->name);
+            return -EINVAL;
+        }
+
         start = sec->load_addr;
         end = sec->load_addr + sec->sec->sh_size;
 
         for ( a = start; a < end; a++ )
         {
-            const void *instr = ALT_ORIG_PTR(a);
-            const void *replacement = ALT_REPL_PTR(a);
+            const void *orig = ALT_ORIG_PTR(a);
+            const void *repl = ALT_REPL_PTR(a);
+
+            /* orig must be fully within .text. */
+            if ( orig               < payload->text_addr ||
+                 a->orig_len        > payload->text_size ||
+                 orig + a->orig_len > payload->text_addr + payload->text_size )
+            {
+                printk(XENLOG_ERR LIVEPATCH
+                       "%s Alternative orig %p+%#x outside payload text %p+%#zx\n",
+                       elf->name, orig, a->orig_len,
+                       payload->text_addr, payload->text_size);
+                return -EINVAL;
+            }
 
-            if ( (instr < region->start && instr >= region->end) ||
-                 (replacement < region->start && replacement >= region->end) )
+            /*
+             * repl must be fully within .altinstr_replacement, even if the
+             * replacement and the section happen to both have zero length.
+             */
+            if ( repl               < repl_sec->load_addr ||
+                 a->repl_len        > repl_sec->sec->sh_size ||
+                 repl + a->repl_len > repl_sec->load_addr + repl_sec->sec->sh_size )
             {
-                printk(XENLOG_ERR LIVEPATCH "%s Alt patching outside payload: %p\n",
-                       elf->name, instr);
+                printk(XENLOG_ERR LIVEPATCH
+                       "%s Alternative repl %p+%#x outside .altinstr_replacement %p+%#"PRIxElfWord"\n",
+                       elf->name, repl, a->repl_len,
+                       repl_sec->load_addr, repl_sec->sec->sh_size);
                 return -EINVAL;
             }
         }
         apply_alternatives(start, end);
+    alt_done:;
 #else
         printk(XENLOG_ERR LIVEPATCH "%s: We don't support alternative patching\n",
                elf->name);
xen/include/xen/elfstructs.h +2 -0
index 3124469faeb4..eb6b87a823a8 100644
@@ -563,6 +563,7 @@ typedef struct {
 #if defined(ELFSIZE) && (ELFSIZE == 32)
 #define PRIxElfAddr 	PRIx32
 #define PRIuElfWord 	PRIu32
+#define PRIxElfWord 	PRIx32
 
 #define Elf_Ehdr	Elf32_Ehdr
 #define Elf_Phdr	Elf32_Phdr
@@ -591,6 +592,7 @@ typedef struct {
 #elif defined(ELFSIZE) && (ELFSIZE == 64)
 #define PRIxElfAddr	PRIx64
 #define PRIuElfWord	PRIu64
+#define PRIxElfWord	PRIx64
 
 #define Elf_Ehdr	Elf64_Ehdr
 #define Elf_Phdr	Elf64_Phdr
Signature · 1 line
2.30.2
#8 lore raw
Quoted 9 lines · Andrew Cooper
xen/common/livepatch.c +3 -0
@@ -803,28 +803,84 @@ static int prepare_payload(struct payload *payload,
     if ( sec )
     {
 #ifdef CONFIG_HAS_ALTERNATIVE
+        /*
+         * (As of April 2023), Alternatives are formed of:
+         * - An .altinstructions section with an array of struct alt_instr's.

xen/common/livepatch.c static int prepare_payload(struct payload *payload, · line 818

Quoted 10 lines
xen/common/livepatch.c +10 -0
+         * - An .altinstr_replacement section containing instructions.
+         *
+         * An individual alt_instr contains:
+         * - An orig reference, pointing into .text with a nonzero length
+         * - A repl reference, pointing into .altinstr_replacement
+         *
+         * It is legal to have zero-length replacements, meaning it is legal
+         * for the .altinstr_replacement section to be empty too.  An
+         * implementation detail means that a zero-length replacement's repl
+         * reference will still be in the .altinstr_replacement section.

Didn't you agree that "will" is not really true, and it's at best "may", but then also doesn't really matter here in the first place (suggesting that the sentence might best be dropped, to avoid drawing attention to something that might at best confuse the reader as to its relevance)?

Jan

#9 lore raw
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
xen/common/livepatch.c +9 -0
@@ -803,28 +803,84 @@ static int prepare_payload(struct payload *payload,
     if ( sec )
     {
 #ifdef CONFIG_HAS_ALTERNATIVE
+        /*
+         * (As of April 2023), Alternatives are formed of:
+         * - An .altinstructions section with an array of struct alt_instr's.
+         * - An .altinstr_replacement section containing instructions.
+         *
+         * An individual alt_instr contains:
+         * - An orig reference, pointing into .text with a nonzero length
+         * - A repl reference, pointing into .altinstr_replacement
+         *
Quoted 8 lines

xen/common/livepatch.c static int prepare_payload(struct payload *payload, · line 818

Quoted 4 lines
xen/common/livepatch.c +4 -0
+         * It is legal to have zero-length replacements, meaning it is legal
+         * for the .altinstr_replacement section to be empty too.  An
+         * implementation detail means that a zero-length replacement's repl
+         * reference will still be in the .altinstr_replacement section.

Didn't you agree that "will" is not really true, and it's at best "may", but then also doesn't really matter here in the first place (suggesting that the sentence might best be dropped, to avoid drawing attention to something that might at best confuse the reader as to its relevance)?

Only that "will be at 0" wasn't actually true.

Right now, the repl reference *will* be somewhere in altinstr_replacement.  It is discussed here because it is what the check enforces.

As an implementation detail, it is of course free to change in the future if needs be.

~Andrew

#10 lore raw
Quoted 26 lines · Andrew Cooper
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
xen/common/livepatch.c +9 -0
@@ -803,28 +803,84 @@ static int prepare_payload(struct payload *payload,
     if ( sec )
     {
 #ifdef CONFIG_HAS_ALTERNATIVE
+        /*
+         * (As of April 2023), Alternatives are formed of:
+         * - An .altinstructions section with an array of struct alt_instr's.
+         * - An .altinstr_replacement section containing instructions.
+         *
+         * An individual alt_instr contains:
+         * - An orig reference, pointing into .text with a nonzero length
+         * - A repl reference, pointing into .altinstr_replacement
+         *
Quoted 8 lines

xen/common/livepatch.c static int prepare_payload(struct payload *payload, · line 818

Quoted 4 lines
xen/common/livepatch.c +4 -0
+         * It is legal to have zero-length replacements, meaning it is legal
+         * for the .altinstr_replacement section to be empty too.  An
+         * implementation detail means that a zero-length replacement's repl
+         * reference will still be in the .altinstr_replacement section.

Didn't you agree that "will" is not really true, and it's at best "may", but then also doesn't really matter here in the first place (suggesting that the sentence might best be dropped, to avoid drawing attention to something that might at best confuse the reader as to its relevance)?

Quoted 1 line

Only that "will be at 0" wasn't actually true.

Oh, right - I'm sorry for the noise.

Jan

Quoted 8 lines

Right now, the repl reference *will* be somewhere in altinstr_replacement.  It is discussed here because it is what the check enforces.

As an implementation detail, it is of course free to change in the future if needs be.

~Andrew

Reviewed-by on v2 3/3
#11 lore raw
In reply to Andrew Cooper
Quoted 12 lines

From: Andrew Cooper <[email protected]>
Sent: Monday, April 17, 2023 1:13 PM
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>
Subject: [PATCH v2 3/3] xen/livepatch: Fix .altinstructions safety checks

The prior check has && vs || mixups, making it tautologically false and thus
providing no safety at all.  There are boundary errors too.

First start with a comment describing how the .altinstructions and .altinstr_replacement sections interact, and perform suitable cross-checking.

Quoted 8 lines

Second, rewrite the alt_instr loop entirely from scratch.  Origin sites have non-zero size, and must be fully contained within the livepatches .text section(s).  Any non-zero sized replacements must be fully contained within the .altinstr_replacement section.

Fixes f8a10174e8b1 ("xsplice: Add support for alternatives")

---

#12 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

This replaces the previous singleton patch, with several build fixes found by Gitlab. I also included some feedback from Jan on patch 3.

Andrew Cooper (3):
xen/ELF: Fix ELF32 PRI formatters
arm/alternatives: Rename alt_instr fields which are used in common code xen/livepatch: Fix .altinstructions safety checks

This series was committed into 4.18, but not picked up for backport despite several fixes tags.

We've just fallen over a livepatching issue which would have been caught by it, so I'm going to backport it to 4.17 while 4.17 is still in general support.

~Andrew