Skip to contentSkip to conversation

livepatch-build-tools: fix for livepatch hooks

PATCH In review Roger Pau Monne 3 participants 12 messages Nov 7, 2024 – Jan 21, 2025

lore
Share & tools
lore
#0 lore raw

cover livepatch-build-tools: fix for livepatch hooks

Hello,

First two patches in the series are misc (IMO trivial) fixes. Last two patches fix the usage of hooks.

Thanks, Roger.

Roger Pau Monne (4):
livepatch-build: allow patch file name sizes up to 127 characters
create-diff-object: update default alt_instr size
create-diff-object: don't include symbols for .livepatch.hooks.*
sections
create-diff-object: also include relas that point to changed sections

 create-diff-object.c | 14 +++++++++-----
 livepatch-build      |  5 +++--
 2 files changed, 12 insertions(+), 7 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/4 livepatch-build: allow patch file name sizes up to 127 characters

XenServer uses quite long Xen version names, and encode such in the livepatch filename, and it's currently running out of space in the file name.

Bump max filename size to 127, so it also matches the patch name length in the hypervisor interface. Note the size of the buffer is 128 characters, and the last one is reserved for the null terminator.

---
Changes since v1:
- Take into account the null terminator.

---
 livepatch-build | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
livepatch-build +3 -2
index 948b2acfc2f6..f3ca9399d149 100755
@@ -72,8 +72,9 @@ function make_patch_name()
     fi
 
     # Only allow alphanumerics and '_' and '-' in the patch name.  Everything
-    # else is replaced with '-'.  Truncate to 48 chars.
-    echo ${PATCHNAME//[^a-zA-Z0-9_-]/-} |cut -c 1-48
+    # else is replaced with '-'.  Truncate to 127 chars
+    # (XEN_LIVEPATCH_NAME_SIZE - 1).
+    echo ${PATCHNAME//[^a-zA-Z0-9_-]/-} |cut -c -127
 }
 
 # Do a full normal build
Signature · 1 line
2.46.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] livepatch-build: allow patch file name sizes up to 127 characters

Quoted 21 lines · Roger Pau Monne

XenServer uses quite long Xen version names, and encode such in the livepatch filename, and it's currently running out of space in the file name.

Bump max filename size to 127, so it also matches the patch name length in the hypervisor interface. Note the size of the buffer is 128 characters, and the last one is reserved for the null terminator.

---
Changes since v1:
- Take into account the null terminator.

---
 livepatch-build | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
livepatch-build +0 -0
index 948b2acfc2f6..f3ca9399d149 100755

livepatch-build function make_patch_name() · line 78

Quoted 9 lines
livepatch-build +3 -2
@@ -72,8 +72,9 @@ function make_patch_name()
     fi
 
     # Only allow alphanumerics and '_' and '-' in the patch name.  Everything
-    # else is replaced with '-'.  Truncate to 48 chars.
-    echo ${PATCHNAME//[^a-zA-Z0-9_-]/-} |cut -c 1-48
+    # else is replaced with '-'.  Truncate to 127 chars
+    # (XEN_LIVEPATCH_NAME_SIZE - 1).
+    echo ${PATCHNAME//[^a-zA-Z0-9_-]/-} |cut -c -127
 }

I think this 48 char limit erroneously came from kpatch / Linux's module name length limit and is therefore not relevant.

Thanks

#3 lore raw

PATCH 2/4 create-diff-object: update default alt_instr size

In reply to Roger Pau Monne

The size of the alt_instr structure in Xen is 14 instead of 12 bytes, adjust it.

---
 create-diff-object.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
create-diff-object.c +1 -1
index fed360a9aa68..d8a2afbf2774 100644
@@ -1000,7 +1000,7 @@ static int altinstructions_group_size(struct kpatch_elf *kelf, int offset)
 	char *str;
 	if (!size) {
 		str = getenv("ALT_STRUCT_SIZE");
-		size = str ? atoi(str) : 12;
+		size = str ? atoi(str) : 14;
 	}
 
 	log_debug("altinstr_size=%d\n", size);
Signature · 1 line
2.46.0
#4 lore raw

Re: [PATCH 2/4] create-diff-object: update default alt_instr size

Quoted 2 lines · Roger Pau Monne

The size of the alt_instr structure in Xen is 14 instead of 12 bytes, adjust it.

Nowadays yes. Isn't the tool supposed to be usable with all livepatch-capable Xen versions, though? As a random data point, 4.7 still had the size at 12.

Jan

#5 lore raw

Re: [PATCH 2/4] create-diff-object: update default alt_instr size

Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monne

The size of the alt_instr structure in Xen is 14 instead of 12 bytes, adjust it.

Nowadays yes. Isn't the tool supposed to be usable with all livepatch-capable Xen versions, though? As a random data point, 4.7 still had the size at 12.

Yes, livepatch-build-tools will fetch the alt_intsrt size from the DWARF info.

However when using create-diff-object without the env variable being set a default value is used, and that's currently 12. I think it would be best to update that default to the value used by the current Xen version.

Thanks, Roger.

#6 lore raw

Re: [PATCH 2/4] create-diff-object: update default alt_instr size

Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monne

The size of the alt_instr structure in Xen is 14 instead of 12 bytes, adjust it.

Nowadays yes. Isn't the tool supposed to be usable with all livepatch-capable Xen versions, though? As a random data point, 4.7 still had the size at 12.

Quoted 7 lines

Yes, livepatch-build-tools will fetch the alt_intsrt size from the DWARF info.

However when using create-diff-object without the env variable being set a default value is used, and that's currently 12. I think it would be best to update that default to the value used by the current Xen version.

Oh, I see. Makes sense of course.

Jan

Reviewed-by on v1 2/4
#7 lore raw

Re: [PATCH 2/4] create-diff-object: update default alt_instr size

In reply to Roger Pau Monne
Quoted 23 lines · Roger Pau Monne

The size of the alt_instr structure in Xen is 14 instead of 12 bytes, adjust it.

---
 create-diff-object.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
create-diff-object.c +1 -2
index fed360a9aa68..d8a2afbf2774 100644
@@ -1000,7 +1000,7 @@ static int altinstructions_group_size(struct kpatch_elf *kelf, int offset)
        char *str;
        if (!size) {
                str = getenv("ALT_STRUCT_SIZE");
-               size = str ? atoi(str) : 12;
+               size = str ? atoi(str) : 14;
        }
 
        log_debug("altinstr_size=%d\n", size);
--
Quoted 2 lines

2.46.0

#8 lore raw

PATCH 3/4 create-diff-object: don't include symbols for .livepatch.hooks.* sections

In reply to Roger Pau Monne

Not all toolchains generate symbols for the .livepatch.hooks.* sections, neither those symbols are required by the livepatch loading logic in Xen to find and process the hooks. Hooks in livepatch payloads are found and processed based exclusively on section data.

The unconditional attempt to expect each hook serction to have a matching symbol leads to a segmentation fault in create-diff-object when such symbol is not present, as the code references a NULL pointer.

Fix this by not attempting to include symbols associated with hook sections.

---
 create-diff-object.c | 2 --
 1 file changed, 2 deletions(-)
create-diff-object.c +0 -2
index d8a2afbf2774..924059a1842b 100644
@@ -1555,8 +1555,6 @@ static int kpatch_include_hook_elements(struct kpatch_elf *kelf)
 				sym->sec->sym = NULL;
 				/* use section symbol instead */
 				rela->sym = sym->sec->secsym;
-			} else {
-				sec->secsym->include = 1;
 			}
 		}
 	}
Signature · 1 line
2.46.0
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] create-diff-object: don't include symbols for .livepatch.hooks.* sections

Quoted 31 lines · Roger Pau Monne

Not all toolchains generate symbols for the .livepatch.hooks.* sections, neither those symbols are required by the livepatch loading logic in Xen to find and process the hooks. Hooks in livepatch payloads are found and processed based exclusively on section data.

The unconditional attempt to expect each hook serction to have a matching symbol leads to a segmentation fault in create-diff-object when such symbol is not present, as the code references a NULL pointer.

Fix this by not attempting to include symbols associated with hook sections.

---
 create-diff-object.c | 2 --
 1 file changed, 2 deletions(-)
create-diff-object.c +0 -3
index d8a2afbf2774..924059a1842b 100644
@@ -1555,8 +1555,6 @@ static int kpatch_include_hook_elements(struct kpatch_elf *kelf)
                                sym->sec->sym = NULL;
                                /* use section symbol instead */
                                rela->sym = sym->sec->secsym;
-                       } else {
-                               sec->secsym->include = 1;
                        }
                }
        }
--
Quoted 2 lines

2.46.0

Thanks

#10 lore raw

PATCH 4/4 create-diff-object: also include relas that point to changed sections

In reply to Roger Pau Monne

create-diff-object has a special handling for some specific sections, like .altinstructions or .livepatch.hooks.*. The contents of those sections are in the form of array elements, where each element can be processed independently of the rest. For example an element in .altinstructions is a set of replacement coordinates, with the layout specified by the alt_instr struct. In the case of .livepatch.hooks.* each element is a pointer to a hook function to call.

The contents of this array is processed element wise, so that create-diff-object can decide whether the element relates to the content in the livepatch and thus needs keeping. Such relation is driven based on the contents of the relocations for the special sections. If a relocation to be applied to a special section element depends on any symbol to be included in the livepatch then the special element is also considered required and thus added to the livepatch contents.

However relocations don't always reference function type symbols, they can also reference sections type symbols, and that's usually the case with hook symbols that have relocations based on section symbols, as an example:

RELOCATION RECORDS FOR [.livepatch.hooks.load]:
OFFSET TYPE VALUE
0000000000000000 R_X86_64_64 .text.foobar

Symbol information for .text.foobar:

0000000000000000 l d .text.foobar 0000000000000000 .text.foobar

As seen above, the .livepatch.hooks.load relocation uses a non-function symbol, which given the current code in should_keep_rela_group() would mean it's not considered for inclusion in the livepatch.

Fix this by allowing should_keep_rela_group() to also keep relocations if they either point to function or section symbols.

---
 create-diff-object.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
create-diff-object.c +8 -2
index 924059a1842b..c21cc576052a 100644
@@ -1158,11 +1158,17 @@ static int should_keep_rela_group(struct section *sec, int start, int size)
 	struct rela *rela;
 	int found = 0;
 
-	/* check if any relas in the group reference any changed functions */
+	/*
+	 * Check if any relas in the group reference any changed functions or
+	 * sections.  As seen by hook related relocations (.livepatch.hooks.*),
+	 * it's possible they use the section symbol as a reference rather than
+	 * the function symbol.
+	 */
 	list_for_each_entry(rela, &sec->relas, list) {
 		if (rela->offset >= start &&
 		    rela->offset < start + size &&
-		    rela->sym->type == STT_FUNC &&
+		    (rela->sym->type == STT_FUNC ||
+		     rela->sym->type == STT_SECTION) &&
 		    rela->sym->sec->include) {
 			found = 1;
 			log_debug("new/changed symbol %s found in special section %s\n",
Signature · 1 line
2.46.0
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH 4/4] create-diff-object: also include relas that point to changed sections

Quoted 66 lines · Roger Pau Monne

create-diff-object has a special handling for some specific sections, like .altinstructions or .livepatch.hooks.*. The contents of those sections are in the form of array elements, where each element can be processed independently of the rest. For example an element in .altinstructions is a set of replacement coordinates, with the layout specified by the alt_instr struct. In the case of .livepatch.hooks.* each element is a pointer to a hook function to call.

The contents of this array is processed element wise, so that create-diff-object can decide whether the element relates to the content in the livepatch and thus needs keeping. Such relation is driven based on the contents of the relocations for the special sections. If a relocation to be applied to a special section element depends on any symbol to be included in the livepatch then the special element is also considered required and thus added to the livepatch contents.

However relocations don't always reference function type symbols, they can also reference sections type symbols, and that's usually the case with hook symbols that have relocations based on section symbols, as an example:

RELOCATION RECORDS FOR [.livepatch.hooks.load]:
OFFSET TYPE VALUE
0000000000000000 R_X86_64_64 .text.foobar

Symbol information for .text.foobar:

0000000000000000 l d .text.foobar 0000000000000000 .text.foobar

As seen above, the .livepatch.hooks.load relocation uses a non-function symbol, which given the current code in should_keep_rela_group() would mean it's not considered for inclusion in the livepatch.

Fix this by allowing should_keep_rela_group() to also keep relocations if they either point to function or section symbols.

---
 create-diff-object.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
create-diff-object.c +8 -3
index 924059a1842b..c21cc576052a 100644
@@ -1158,11 +1158,17 @@ static int should_keep_rela_group(struct section *sec, int start, int size)
        struct rela *rela;
        int found = 0;
 
-       /* check if any relas in the group reference any changed functions */
+       /*
+        * Check if any relas in the group reference any changed functions or
+        * sections.  As seen by hook related relocations (.livepatch.hooks.*),
+        * it's possible they use the section symbol as a reference rather than
+        * the function symbol.
+        */
        list_for_each_entry(rela, &sec->relas, list) {
                if (rela->offset >= start &&
                    rela->offset < start + size &&
-                   rela->sym->type == STT_FUNC &&
+                   (rela->sym->type == STT_FUNC ||
+                    rela->sym->type == STT_SECTION) &&
                    rela->sym->sec->include) {
                        found = 1;
                        log_debug("new/changed symbol %s found in special section %s\n",
--
Quoted 2 lines

2.46.0

Thanks