[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20171012094833.xjk6jg5us3ffkih4@pd.tnic>
Date: Thu, 12 Oct 2017 11:48:33 +0200
From: Borislav Petkov <bp@...e.de>
To: Ricardo Neri <ricardo.neri-calderon@...ux.intel.com>
Cc: Ingo Molnar <mingo@...hat.com>,
Thomas Gleixner <tglx@...utronix.de>,
"H. Peter Anvin" <hpa@...or.com>,
Andy Lutomirski <luto@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Brian Gerst <brgerst@...il.com>,
Chris Metcalf <cmetcalf@...lanox.com>,
Dave Hansen <dave.hansen@...ux.intel.com>,
Paolo Bonzini <pbonzini@...hat.com>,
Masami Hiramatsu <mhiramat@...nel.org>,
Huang Rui <ray.huang@....com>, Jiri Slaby <jslaby@...e.cz>,
Jonathan Corbet <corbet@....net>,
"Michael S. Tsirkin" <mst@...hat.com>,
Paul Gortmaker <paul.gortmaker@...driver.com>,
Vlastimil Babka <vbabka@...e.cz>,
Chen Yucong <slaoub@...il.com>,
"Ravi V. Shankar" <ravi.v.shankar@...el.com>,
Shuah Khan <shuah@...nel.org>, linux-kernel@...r.kernel.org,
x86@...nel.org, Adam Buchbinder <adam.buchbinder@...il.com>,
Colin Ian King <colin.king@...onical.com>,
Lorenzo Stoakes <lstoakes@...il.com>,
Qiaowei Ren <qiaowei.ren@...el.com>,
Arnaldo Carvalho de Melo <acme@...hat.com>,
Adrian Hunter <adrian.hunter@...el.com>,
Kees Cook <keescook@...omium.org>,
Thomas Garnier <thgarnie@...gle.com>,
Dmitry Vyukov <dvyukov@...gle.com>
Subject: Re: [PATCH v9 13/29] x86/insn-eval: Add utility functions to get
segment selector
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. :-\
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().
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.
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.
Thx.
---
diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index e7e82b343bd0..3c65fa9178bc 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -71,9 +71,6 @@ static int get_seg_reg_override_idx(struct insn *insn)
int idx = INAT_SEG_REG_DEFAULT;
int num_overrides = 0, i;
- if (!insn)
- return -EINVAL;
-
insn_get_prefixes(insn);
/* Look for any segment override prefixes. */
@@ -128,28 +125,16 @@ static int get_seg_reg_override_idx(struct insn *insn)
*
* Returns:
*
- * 1 if segment override prefixes can be used with the register indicated
- * in regoff. 0 if otherwise.
+ * True if segment override prefixes can be used with the register indicated
+ * in regoff. False if otherwise.
*
- * -EINVAL in case of error.
*/
-static int check_seg_overrides(struct insn *insn, int regoff)
+static bool check_seg_overrides(struct insn *insn, int regoff)
{
- /*
- * Segment override prefixes should not be used for rIP. It is not
- * necessary to inspect the instruction structure.
- */
- if (regoff == offsetof(struct pt_regs, ip))
- return 0;
-
- /* Subsequent checks require a valid insn. */
- if (!insn)
- return -EINVAL;
-
if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
- return 0;
+ return false;
- return 1;
+ return true;
}
static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
@@ -247,18 +232,21 @@ static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
*/
static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
{
- int ret, idx;
+ int idx;
- ret = check_seg_overrides(insn, regoff);
- if (ret < 0)
- return ret;
-
- if (!ret)
- return resolve_default_seg(insn, regs, regoff);
+ /*
+ * Segment override prefixes should not be used for rIP. It is not
+ * necessary to inspect the instruction.
+ */
+ if (regoff == offsetof(struct pt_regs, ip))
+ return INAT_SEG_REG_CS;
if (!insn)
return -EINVAL;
+ if (!check_seg_overrides(insn, regoff))
+ return resolve_default_seg(insn, regs, regoff);
+
idx = get_seg_reg_override_idx(insn);
if (idx < 0)
return idx;
> Could this check be removed? insn is not used for anything but passed to other
> functions that do perform this check.
Yap, eventually. :)
--
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
--
Powered by blists - more mailing lists