Skip to contentSkip to conversation

arch/x86/kernel: check the return value of insn_decode_kernel()

PATCH In review Li Zhong 2 participants 3 messages Sep 2, 2022 – Sep 7, 2022

Subsystems (2)
lore
Share & tools
lore

Li Zhong

author
#0 lore raw

PATCH 1/1 arch/x86/kernel: check the return value of insn_decode_kernel()

insn_decode() could fail and the insn.length could be invalid. So we need to check the return value first.

---
 arch/x86/kernel/jump_label.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/jump_label.c +3 -2
index f5b8ef02d172..3ecaf0cc71d0 100644
@@ -20,9 +20,10 @@
 int arch_jump_entry_size(struct jump_entry *entry)
 {
 	struct insn insn = {};
+	int ret;
 
-	insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
-	BUG_ON(insn.length != 2 && insn.length != 5);
+	ret = insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
+	BUG_ON(ret < 0 || insn.length != 2 && insn.length != 5);
 
 	return insn.length;
 }
Signature · 1 line
2.25.1
#1 lore raw

arch/x86/kernel/jump_label.c line 25

Quoted 9 lines · On Fri, 2 Sep 2022 00:47:06 -0700 Li Zhong <[email protected]> wrote:
arch/x86/kernel/jump_label.c +2 -2
@@ -20,9 +20,10 @@
 int arch_jump_entry_size(struct jump_entry *entry)
 {
 	struct insn insn = {};
+	int ret;
 
-	insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
-	BUG_ON(insn.length != 2 && insn.length != 5);
+	ret = insn_decode_kernel(&insn, (void *)jump_entry_code(entry));

It's highly unlikely that length will be 2 or 5 if ret is not zero (as it is initialized to zero going into this function).

arch/x86/kernel/jump_label.c line 26

Quoted 1 line
arch/x86/kernel/jump_label.c +1 -0
+	BUG_ON(ret < 0 || insn.length != 2 && insn.length != 5);

In any case, you need parenthesis around the && condition.

-- Steve

Quoted 2 lines
arch/x86/kernel/jump_label.c +0 -0
 	return insn.length;
author
#2 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 6 lines · On Fri, 2 Sep 2022 00:47:06 -0700 Li Zhong <[email protected]> wrote:
arch/x86/kernel/jump_label.c +1 -0
@@ -20,9 +20,10 @@
 int arch_jump_entry_size(struct jump_entry *entry)
 {
      struct insn insn = {};
+     int ret;
 
Quoted 7 lines

arch/x86/kernel/jump_label.c line 25

Quoted 3 lines
arch/x86/kernel/jump_label.c +1 -2
-     insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
-     BUG_ON(insn.length != 2 && insn.length != 5);
+     ret = insn_decode_kernel(&insn, (void *)jump_entry_code(entry));

It's highly unlikely that length will be 2 or 5 if ret is not zero (as it is initialized to zero going into this function).

In this case, I think maybe just BUG_ON(ret<0) is enough. However, the code that decides the value of insn->length could be modified in the future. And there are other variables insn->next_byte and insn->kaddr that are related to insn->length, which could also be modified. So I guess we can preserve all the assertion conditions to guarantee all assumptions are satisfied.

Quoted 4 lines

arch/x86/kernel/jump_label.c line 26

Quoted 1 line
arch/x86/kernel/jump_label.c +1 -0
+     BUG_ON(ret < 0 || insn.length != 2 && insn.length != 5);

In any case, you need parenthesis around the && condition.

Thanks. Will add this in v2 patch.

Quoted 5 lines

-- Steve

Quoted 2 lines
arch/x86/kernel/jump_label.c +0 -0
      return insn.length;