Re: [PATCH v2 2/3] x86/traps: Print non-canonical address on #GP

From: Jann Horn
Date: Wed Nov 20 2019 - 05:32:16 EST


On Wed, Nov 20, 2019 at 5:25 AM Andi Kleen <ak@xxxxxxxxxxxxxxx> wrote:
> Jann Horn <jannh@xxxxxxxxxx> writes:
> > + if (error_code)
> > + pr_alert("GPF is segment-related (see error code)\n");
> > + else
> > + print_kernel_gp_address(regs);
>
> Is this really correct? There are a lot of instructions that can do #GP
> (it's the CPU's equivalent of EINVAL) and I'm pretty sure many of them
> don't set an error code, and many don't have operands either.
>
> You would need to make sure the instruction decoder handles these
> cases correctly, and ideally that you detect it instead of printing
> a bogus address.

Is there a specific concern you have about the instruction decoder? As
far as I can tell, all the paths of insn_get_addr_ref() only work if
the instruction has a mod R/M byte according to the instruction
tables, and then figures out the address based on that. While that
means that there's a wide variety of cases in which we won't be able
to figure out the address, I'm not aware of anything specific that is
likely to lead to false positives.

But Andy did suggest that we hedge a bit in the error message because
even if the address passed to the instruction is non-canonical, we
don't know for sure whether that's actually the reason why things
failed, and that's why it says "probably" in the message about the
address now.