Re: [PATCH v9 13/29] x86/insn-eval: Add utility functions to get segment selector

From: Ricardo Neri
Date: Thu Oct 12 2017 - 21:09:51 EST


On Thu, 2017-10-12 at 11:48 +0200, Borislav Petkov wrote:
> On Wed, Oct 11, 2017 at 06:12:30PM -0700, Ricardo Neri wrote:
> >
> > Shouldn't this function check for a null insn since it is used here?
> I have to say, this whole codepath from insn_get_seg_base() with
> insn==NULL is nasty but I don't see a way around it as we need to know
> how many bytes to copy and from where. Can't think of a better solution
> without duplicating a lot of code. :-\

I have looked at your two proposals. I think I prefer the first one plus a
couple of tweaks.

>
> So how about this?
>
> If the patch is hard to read, you can apply it and look at the code. But
> here's the gist:
>
> * You pull up the rIP check and do that directly in resolve_seg_reg()
> and return INAT_SEG_REG_CS there immediately so you don't have to call
> resolve_default_seg().

In my opinion it would be better to have all the checks in a single place. This
makes the code easier to read that having this special case directly
inÂresolve_default_seg(). Also, strictly speaking we would need to
returnÂINAT_SEG_REG_IGNORE in long mode. Indeed,Âinsn_get_seg_base() would
return base 0 in such a case, but I feel it is better if this logic is explicit
inÂresolve_default_seg().
>
> This way, you get the only case out of the way where insn can be NULL.
>
> Then you can do the if (!insn) check once and now you have a valid insn.

Rather than checking for null insn inÂresolve_seg_reg(), which does not use it,
let the functions it calls do the check if they need to.
>
> check_seg_overrides() can then return simply bool and you can get rid of
> the remaining if (!insn) checks down the road.
>
> But please double-check me if I missed a case - the flow is not trivial.

This is a diff based on your first proposal (I hope text does not wrap). I feel
this makes it clear howÂresolve_seg_reg() handles errors as well it uses
overridden or default segment register indices. Plus, insn is only checked when
used.

@@ -155,6 +155,16 @@ static int resolve_default_seg(struct insn *insn, struct
pt_regs *regs, int off)
Â{
ÂÂÂÂÂÂÂÂif (user_64bit_mode(regs))
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn INAT_SEG_REG_IGNORE;
+
+ÂÂÂÂÂÂÂ/*
+ÂÂÂÂÂÂÂÂ* insn may be null as we may be about to copy the instruction.
+ÂÂÂÂÂÂÂÂ* However is not needed at all.
+ÂÂÂÂÂÂÂÂ*/
+ÂÂÂÂÂÂÂif (off == offsetof(struct pt_regs, ip))
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂINAT_SEG_REG_CS;
+
+ÂÂÂÂÂÂÂif(!insn)
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn -EINVAL;
ÂÂÂÂÂÂÂÂ/*
ÂÂÂÂÂÂÂÂÂ* If we are here, we use the default segment register as described
ÂÂÂÂÂÂÂÂÂ* in the Intel documentation:
@@ -191,9 +201,6 @@ static int resolve_default_seg(struct insn *insn, struct
pt_regs *regs, int off)
ÂÂÂÂÂÂÂÂcase offsetof(struct pt_regs, sp):
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn INAT_SEG_REG_SS;
Â
-ÂÂÂÂÂÂÂcase offsetof(struct pt_regs, ip):
-ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn INAT_SEG_REG_CS;
-
ÂÂÂÂÂÂÂÂdefault:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn -EINVAL;
ÂÂÂÂÂÂÂÂ}
@@ -254,9 +261,6 @@ static int resolve_seg_reg(struct insn *insn, struct pt_regs
*regs, int regoff)
ÂÂÂÂÂÂÂÂif (!ret)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn resolve_default_seg(insn, regs, regoff);
Â
-ÂÂÂÂÂÂÂif (!insn)
-ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn -EINVAL;
-
ÂÂÂÂÂÂÂÂidx = get_seg_reg_override_idx(insn);
ÂÂÂÂÂÂÂÂif (idx < 0)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn idx;

Thanks and BR,
Ricardo