Skip to contentSkip to conversation

s390/kexec_file: improve error handling and messages

PATCH Superseded Philipp Rudo 2 participants 6 messages Dec 7, 2021 s390

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover s390/kexec_file: improve error handling and messages

Hi everybody,

here is v2 of the patch I sent yesterday.

Thanks
Philipp

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 | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 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 | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
arch/s390/kernel/machine_kexec_file.c +12 -3
index 9975ad200d74..b53fa670e289 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>
@@ -304,15 +306,22 @@ 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_shndx == SHN_UNDEF) {
+			pr_err("Undefined symbol\n");
 			return -ENOEXEC;
+		}
 
-		if (sym->st_shndx == SHN_COMMON)
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol in common section\n");
 			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\n",
+			       sym->st_shndx);
 			return -ENOEXEC;
+		}
 
 		loc = pi->purgatory_buf;
 		loc += section->sh_offset;
Signature · 1 line
2.31.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] s390/kexec_file: print some more error messages

Hi Philipp,

Quoted 3 lines · Philipp Rudo

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

...

Quoted 12 lines
arch/s390/kernel/machine_kexec_file.c +4 -2
@@ -304,15 +306,22 @@ 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_shndx == SHN_UNDEF) {
+			pr_err("Undefined symbol\n");
 			return -ENOEXEC;
+		}
 
-		if (sym->st_shndx == SHN_COMMON)
+		if (sym->st_shndx == SHN_COMMON) {

arch/s390/kernel/machine_kexec_file.c int arch_kexec_apply_relocations_add(struct purgatory_info *pi, · line 323

Quoted 10 lines
arch/s390/kernel/machine_kexec_file.c +5 -1
+			pr_err("symbol in common section\n");
 			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\n",
+			       sym->st_shndx);
 			return -ENOEXEC;

So, if you add the additional error messages here, then I'd really like to see also the name of the symbol which is causing problems. Just like it is done on x86.
Sorry for nitpicking :)

#3 lore raw

Re: [PATCH v2 1/2] s390/kexec_file: print some more error messages

Hi Heiko,

Quoted 23 lines · On Tue, 7 Dec 2021 16:29:55 +0100 Heiko Carstens <[email protected]> wrote:

Hi Philipp,

Quoted 3 lines · Philipp Rudo

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

...

Quoted 16 lines
arch/s390/kernel/machine_kexec_file.c +6 -2
@@ -304,15 +306,22 @@ 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_shndx == SHN_UNDEF) {
+			pr_err("Undefined symbol\n");
 			return -ENOEXEC;
+		}
 
-		if (sym->st_shndx == SHN_COMMON)
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol in common section\n");
 			return -ENOEXEC;
+		}
 
Quoted 11 lines

arch/s390/kernel/machine_kexec_file.c int arch_kexec_apply_relocations_add(struct purgatory_info *pi, · line 323

Quoted 6 lines
arch/s390/kernel/machine_kexec_file.c +3 -1
 		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\n",
+			       sym->st_shndx);
 			return -ENOEXEC;  

So, if you add the additional error messages here, then I'd really like to see also the name of the symbol which is causing problems. Just like it is done on x86.
Sorry for nitpicking :)

I actually dropped the name on purpose. At least for my work flow knowing which check failed is more important as that already allows me to search for, e.g. all undefined symbols as each of them can cause trouble. Which symbol exactly triggered the check isn't that important. In addition, the code to get a symbol name is rather ugly. At least when you compare it to its usefulness.

But when you insist...

Philipp

P.S. To avoid an other round for that patch. Do you also want the two
pr_debugs?

maintainer
#4 lore raw

Re: [PATCH v2 1/2] s390/kexec_file: print some more error messages

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
arch/s390/kernel/machine_kexec_file.c +3 -1
-		if (sym->st_shndx == SHN_COMMON)
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol in common section\n");
 			return -ENOEXEC;
+		}
Quoted 11 lines

arch/s390/kernel/machine_kexec_file.c int arch_kexec_apply_relocations_add(struct purgatory_info *pi, · line 323

Quoted 6 lines
arch/s390/kernel/machine_kexec_file.c +3 -1
 		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\n",
+			       sym->st_shndx);
 			return -ENOEXEC;  

So, if you add the additional error messages here, then I'd really like to see also the name of the symbol which is causing problems. Just like it is done on x86.
Sorry for nitpicking :)

Quoted 8 lines

I actually dropped the name on purpose. At least for my work flow knowing which check failed is more important as that already allows me to search for, e.g. all undefined symbols as each of them can cause trouble. Which symbol exactly triggered the check isn't that important. In addition, the code to get a symbol name is rather ugly. At least when you compare it to its usefulness.

But when you insist...

Yes, please.

Quoted 2 lines

P.S. To avoid an other round for that patch. Do you also want the two
pr_debugs?

No, I don't think they are needed.

Thank you!

#5 lore raw

PATCH v2 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 b53fa670e289..5e1da2e67cb7 100644
@@ -294,6 +294,7 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
 {
 	Elf_Rela *relas;
 	int i, r_type;
+	int ret;
 
 	relas = (void *)pi->ehdr + relsec->sh_offset;
 
@@ -335,7 +336,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