[lkml]   [2017]   [Oct]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
SubjectRe: [PATCH v9 13/29] x86/insn-eval: Add utility functions to get segment selector
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

@@ -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;
                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,

 \ /
  Last update: 2017-10-13 03:09    [W:0.085 / U:73.992 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site