Re: [PATCH v1] arch/x86/kernel: check the return value of insn_decode_kernel()

From: Li Zhong
Date: Wed Sep 07 2022 - 04:41:56 EST


On Tue, Sep 6, 2022 at 6:58 PM Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
>
> On Fri, 2 Sep 2022 00:47:06 -0700
> Li Zhong <floridsleeves@xxxxxxxxx> wrote:
>
> > @@ -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).
>

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.

> > + 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.

> -- Steve
>
>
> >
> > return insn.length;