[v1,04/19] x86/insn-eval: Handle return values from the decoder
diff mbox series

Message ID 20201223174233.28638-5-bp@alien8.de
State New, archived
Headers show
Series
  • x86/insn: Add an insn_decode() API
Related show

Commit Message

Borislav Petkov Dec. 23, 2020, 5:42 p.m. UTC
From: Borislav Petkov <bp@suse.de>

Now that the different instruction-inspecting functions return a value,
test that and return early from callers if error has been encountered.

While at it, do not call insn_get_modrm() when calling
insn_get_displacement() because latter will make sure to call
insn_get_modrm() if ModRM hasn't been parsed yet.

Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/lib/insn-eval.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

Comments

Sean Christopherson Dec. 28, 2020, 6:51 p.m. UTC | #1
On Wed, Dec 23, 2020, Borislav Petkov wrote:
> From: Borislav Petkov <bp@suse.de>
> 
> Now that the different instruction-inspecting functions return a value,
> test that and return early from callers if error has been encountered.
>
> While at it, do not call insn_get_modrm() when calling
> insn_get_displacement() because latter will make sure to call
> insn_get_modrm() if ModRM hasn't been parsed yet.
> 
> Signed-off-by: Borislav Petkov <bp@suse.de>
> ---
>  arch/x86/lib/insn-eval.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
> index 265d23a0c334..7e49aaf5454c 100644
> --- a/arch/x86/lib/insn-eval.c
> +++ b/arch/x86/lib/insn-eval.c
> @@ -1106,18 +1106,21 @@ static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
>   * @base_offset will have a register, as an offset from the base of pt_regs,
>   * that can be used to resolve the associated segment.
>   *
> - * -EINVAL on error.
> + * Negative value on error.
>   */
>  static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
>  			    int *base_offset, long *eff_addr)
>  {
>  	long base, indx;
>  	int indx_offset;
> +	int ret;
>  
>  	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
>  		return -EINVAL;
>  
> -	insn_get_modrm(insn);
> +	ret = insn_get_modrm(insn);

This patch is incomplete/inconsistent, and arguably wrong.

  - get_eff_addr_reg() and get_eff_addr_modrm() still ignore the return of
    insn_get_modrm() after this patch.

  - Calling insn_get_modrm() from get_eff_addr_sib() is unnecessary (unless the
    caller passed uninitialized garbage in @insn) as get_eff_addr_sib() is
    called if and only if sib.nbytes!=0, and sib.nbytes can be non-zero if and
    only if the modrm and sib have been got.

  - get_addr_ref_16() does insn_get_displacement, i.e. guarantees the modrm is
    parsed, while the 32/64 variants do not.

What about adding a prereq patch (or three) to call insn_get_displacement() in
insn_get_addr_ref() prior to switching on insn->addr_bytes?  Then the various
internal helpers could be changed to either omit the sanity checks entirely or
WARN on invalid calls?  Or better yet, add an INSN_WARN_ON() macro that compiles
out the checks by default?  E.g. something like:

diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index 7e49aaf5454c..348969146e0f 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -928,12 +928,8 @@ static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index 7e49aaf5454c..348969146e0f 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -928,12 +928,8 @@ static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
 static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
                            int *regoff, long *eff_addr)
 {
-       insn_get_modrm(insn);
-
-       if (!insn->modrm.nbytes)
-               return -EINVAL;
-
-       if (X86_MODRM_MOD(insn->modrm.value) != 3)
+       if (INSN_WARN_ON(!insn->modrm.got || !insn->modrm.nbytes ||
+                        X86_MODRM_MOD(insn->modrm.value) != 3)
                return -EINVAL;

        *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
@@ -978,15 +974,9 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
 {
        long tmp;

-       if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
-               return -EINVAL;
-
-       insn_get_modrm(insn);
-
-       if (!insn->modrm.nbytes)
-               return -EINVAL;
-
-       if (X86_MODRM_MOD(insn->modrm.value) > 2)
+       if (INSN_WARN_ON((insn->addr_bytes != 8 && insn->addr_bytes != 4) ||
+                        !insn->modrm.got || !insn->modrm.nbytes ||
+                        X86_MODRM_MOD(insn->modrm.value) > 2)
                return -EINVAL;

        *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
@@ -1046,15 +1036,8 @@ static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
        int addr_offset1, addr_offset2, ret;
        short addr1 = 0, addr2 = 0, displacement;

-       if (insn->addr_bytes != 2)
-               return -EINVAL;
-
-       insn_get_modrm(insn);
-
-       if (!insn->modrm.nbytes)
-               return -EINVAL;
-
-       if (X86_MODRM_MOD(insn->modrm.value) > 2)
+       if (WARN_ON_ONCE(insn->addr_bytes != 2 || !insn->modrm.got ||
+                        !insn->modrm.nbytes || insn->modrm.value > 2))
                return -EINVAL;

        ret = get_reg_offset_16(insn, regs, &addr_offset1, &addr_offset2);
@@ -1199,10 +1182,7 @@ static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
        short eff_addr;
        long tmp;

-       if (insn_get_displacement(insn))
-               goto out;
-
-       if (insn->addr_bytes != 2)
+       if (INSN_WARN_ON(insn->addr_bytes != 2))
                goto out;

        if (X86_MODRM_MOD(insn->modrm.value) == 3) {
@@ -1263,7 +1243,7 @@ static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
        long tmp;
        int ret;

-       if (insn->addr_bytes != 4)
+       if (INSN_WARN_ON(insn->addr_bytes != 4))
                goto out;

        if (X86_MODRM_MOD(insn->modrm.value) == 3) {
@@ -1356,7 +1336,7 @@ static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
        int regoff, ret;
        long eff_addr;

-       if (insn->addr_bytes != 8)
+       if (INSN_WARN_ON(insn->addr_bytes != 8))
                goto out;

        if (X86_MODRM_MOD(insn->modrm.value) == 3) {
@@ -1408,6 +1388,9 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
        if (!insn || !regs)
                return (void __user *)-1L;

+       if (insn_get_displacement(insn))
+               return (void __user *)-1L;
+
        switch (insn->addr_bytes) {
        case 2:
                return get_addr_ref_16(insn, regs);
Borislav Petkov Dec. 28, 2020, 7:06 p.m. UTC | #2
On Mon, Dec 28, 2020 at 10:51:15AM -0800, Sean Christopherson wrote:
> This patch is incomplete/inconsistent, and arguably wrong.
> 
>   - get_eff_addr_reg() and get_eff_addr_modrm() still ignore the return of
>     insn_get_modrm() after this patch.

Ah, will fix, thx.

>   - Calling insn_get_modrm() from get_eff_addr_sib() is unnecessary (unless the
>     caller passed uninitialized garbage in @insn) as get_eff_addr_sib() is
>     called if and only if sib.nbytes!=0, and sib.nbytes can be non-zero if and
>     only if the modrm and sib have been got.
> 
>   - get_addr_ref_16() does insn_get_displacement, i.e. guarantees the modrm is
>     parsed, while the 32/64 variants do not.
> 
> What about adding a prereq patch (or three) to call insn_get_displacement() in
> insn_get_addr_ref() prior to switching on insn->addr_bytes?  Then the various
> internal helpers could be changed to either omit the sanity checks entirely or
> WARN on invalid calls?  Or better yet, add an INSN_WARN_ON() macro that compiles
> out the checks by default?  E.g. something like:

So the idea is one construction site at a time (that's a German saying :)).

This set deals with whacking the insn decoder into returning
proper error/success values. The next set should do
simplifications/fixes/cleanups/whatever but not all at the same time for
obvious reasons.

So yeah, I'm all for omitting useless code but let's do that ontop.

Thx.

Patch
diff mbox series

diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index 265d23a0c334..7e49aaf5454c 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -1106,18 +1106,21 @@  static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
  * @base_offset will have a register, as an offset from the base of pt_regs,
  * that can be used to resolve the associated segment.
  *
- * -EINVAL on error.
+ * Negative value on error.
  */
 static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 			    int *base_offset, long *eff_addr)
 {
 	long base, indx;
 	int indx_offset;
+	int ret;
 
 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
 		return -EINVAL;
 
-	insn_get_modrm(insn);
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->modrm.nbytes)
 		return -EINVAL;
@@ -1125,7 +1128,9 @@  static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
 		return -EINVAL;
 
-	insn_get_sib(insn);
+	ret = insn_get_sib(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->sib.nbytes)
 		return -EINVAL;
@@ -1194,8 +1199,8 @@  static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
 	short eff_addr;
 	long tmp;
 
-	insn_get_modrm(insn);
-	insn_get_displacement(insn);
+	if (insn_get_displacement(insn))
+		goto out;
 
 	if (insn->addr_bytes != 2)
 		goto out;
@@ -1491,7 +1496,9 @@  bool insn_decode_regs(struct insn *insn, struct pt_regs *regs,
 	insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs);
 	insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs);
 
-	insn_get_length(insn);
+	if (insn_get_length(insn))
+		return false;
+
 	if (buf_size < insn->length)
 		return false;