Skip to contentSkip to conversation

s390/kexec_file: improve error handling and messages

PATCH Applied Philipp Rudo 2 participants 4 messages Dec 8, 2021 s390

lore
Share & tools
lore
#0 lore raw

v3 cover s390/kexec_file: improve error handling and messages

Hi everybody,

here is v3.

@Heiko: I hope you are happy now.

Thanks
Philipp

v3:
- add the symbol name to the error messages

v2:
- EINVAL -> ENOEXEC
- print error message when encountering an unknown relocation
- new patch to print error messages for all error cases in the function

Philipp Rudo (2):
s390/kexec_file: print some more error messages s390/kexec_file: fix error handling when applying relocations

 arch/s390/kernel/machine_kexec_file.c | 34 +++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/2 s390/kexec_file: print some more error messages

Be kind and give some more information on what went wrong.

---
 arch/s390/kernel/machine_kexec_file.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
arch/s390/kernel/machine_kexec_file.c +24 -3
index 9975ad200d74..a8bfa7c8cbba 100644
@@ -7,6 +7,8 @@
  * Author(s): Philipp Rudo <[email protected]>
  */
 
+#define pr_fmt(fmt)	"kexec: " fmt
+
 #include <linux/elf.h>
 #include <linux/errno.h>
 #include <linux/kexec.h>
@@ -290,9 +292,16 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
 				     const Elf_Shdr *relsec,
 				     const Elf_Shdr *symtab)
 {
+	const char *strtab, *name, *shstrtab;
+	const Elf_Shdr *sechdrs;
 	Elf_Rela *relas;
 	int i, r_type;
 
+	/* String & section header string table */
+	sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
+	strtab = (char *)pi->ehdr + sechdrs[symtab->sh_link].sh_offset;
+	shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
+
 	relas = (void *)pi->ehdr + relsec->sh_offset;
 
 	for (i = 0; i < relsec->sh_size / sizeof(*relas); i++) {
@@ -304,15 +313,27 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
 		sym = (void *)pi->ehdr + symtab->sh_offset;
 		sym += ELF64_R_SYM(relas[i].r_info);
 
-		if (sym->st_shndx == SHN_UNDEF)
+		if (sym->st_name)
+			name = strtab + sym->st_name;
+		else
+			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
+
+		if (sym->st_shndx == SHN_UNDEF) {
+			pr_err("Undefined symbol: %s\n", name);
 			return -ENOEXEC;
+		}
 
-		if (sym->st_shndx == SHN_COMMON)
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol '%s' in common section\n", name);
 			return -ENOEXEC;
+		}
 
 		if (sym->st_shndx >= pi->ehdr->e_shnum &&
-		    sym->st_shndx != SHN_ABS)
+		    sym->st_shndx != SHN_ABS) {
+			pr_err("Invalid section %d for symbol %s\n",
+			       sym->st_shndx, name);
 			return -ENOEXEC;
+		}
 
 		loc = pi->purgatory_buf;
 		loc += section->sh_offset;
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/2 s390/kexec_file: fix error handling when applying relocations

In reply to Philipp Rudo

arch_kexec_apply_relocations_add currently ignores all errors returned by arch_kexec_do_relocs. This means that every unknown relocation is silently skipped causing unpredictable behavior while the relocated code runs. Fix this by checking for errors and fail kexec_file_load if an unknown relocation type is encountered.

The problem was found after gcc changed its behavior and used R_390_PLT32DBL relocations for brasl instruction and relied on ld to resolve the relocations in the final link in case direct calls are possible. As the purgatory code is only linked partially (option -r) ld didn't resolve the relocations leaving them for arch_kexec_do_relocs. But arch_kexec_do_relocs doesn't know how to handle R_390_PLT32DBL relocations so they were silently skipped. This ultimately caused an endless loop in the purgatory as the brasl instructions kept branching to itself.

Fixes 71406883fd35 ("s390/kexec_file: Add kexec_file_load system call")
---
 arch/s390/kernel/machine_kexec_file.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/s390/kernel/machine_kexec_file.c +6 -1
index a8bfa7c8cbba..876cdd3c994e 100644
@@ -296,6 +296,7 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
 	const Elf_Shdr *sechdrs;
 	Elf_Rela *relas;
 	int i, r_type;
+	int ret;
 
 	/* String & section header string table */
 	sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
@@ -347,7 +348,11 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
 		addr = section->sh_addr + relas[i].r_offset;
 
 		r_type = ELF64_R_TYPE(relas[i].r_info);
-		arch_kexec_do_relocs(r_type, loc, val, addr);
+		ret = arch_kexec_do_relocs(r_type, loc, val, addr);
+		if (ret) {
+			pr_err("Unknown rela relocation: %d\n", r_type);
+			return -ENOEXEC;
+		}
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
maintainer Applied
#3 lore raw
In reply to Philipp Rudo
Quoted 5 lines · Philipp Rudo

Hi everybody,

here is v3.

@Heiko: I hope you are happy now.

Yes, very happy!

Quoted 8 lines

v3:
- add the symbol name to the error messages

v2:
- EINVAL -> ENOEXEC
- print error message when encountering an unknown relocation
- new patch to print error messages for all error cases in the function

Quoted 6 lines

Philipp Rudo (2):
s390/kexec_file: print some more error messages s390/kexec_file: fix error handling when applying relocations

 arch/s390/kernel/machine_kexec_file.c | 34 +++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

Applied both. Thank you!