aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRicardo Neri <ricardo.neri-calderon@linux.intel.com>2017-11-05 21:27:47 -0500
committerIngo Molnar <mingo@kernel.org>2017-11-08 05:16:19 -0500
commit7a6daf79123a086f03b8cdfbc953958c8e1c1287 (patch)
treedb005391953ef4ab447b0145dc6b31579beeb424
parent70e57c0f4b502f2435b7649a201861fe212c2e4e (diff)
x86/insn-eval: Add support to resolve 32-bit address encodings
32-bit and 64-bit address encodings are identical. Thus, the same logic could be used to resolve the effective address. However, there are two key differences: address size and enforcement of segment limits. If running a 32-bit process on a 64-bit kernel, it is best to perform the address calculation using 32-bit data types. In this manner hardware is used for the arithmetic, including handling of signs and overflows. 32-bit addresses are generally used in protected mode; segment limits are enforced in this mode. This implementation obtains the limit of the segment associated with the instruction operands and prefixes. If the computed address is outside the segment limits, an error is returned. It is also possible to use 32-bit address in long mode and virtual-8086 mode by using an address override prefix. In such cases, segment limits are not enforced. Support to use 32-bit arithmetic is added to the utility functions that compute effective addresses. However, the end result is stored in a variable of type long (which has a width of 8 bytes in 64-bit builds). Hence, once a 32-bit effective address is computed, the 4 most significant bytes are masked out to avoid sign extension. The newly added function get_addr_ref_32() is almost identical to the existing function insn_get_addr_ref() (used for 64-bit addresses). The only difference is that it verifies that the effective address is within the limits of the segment. Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: Adam Buchbinder <adam.buchbinder@gmail.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Borislav Petkov <bp@suse.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Chen Yucong <slaoub@gmail.com> Cc: Chris Metcalf <cmetcalf@mellanox.com> Cc: Colin Ian King <colin.king@canonical.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Huang Rui <ray.huang@amd.com> Cc: Jiri Slaby <jslaby@suse.cz> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Lorenzo Stoakes <lstoakes@gmail.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Michael S. Tsirkin <mst@redhat.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Paul Gortmaker <paul.gortmaker@windriver.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Qiaowei Ren <qiaowei.ren@intel.com> Cc: Ravi V. Shankar <ravi.v.shankar@intel.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Thomas Garnier <thgarnie@google.com> Cc: Tony Luck <tony.luck@intel.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: ricardo.neri@intel.com Link: http://lkml.kernel.org/r/1509935277-22138-3-git-send-email-ricardo.neri-calderon@linux.intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--arch/x86/lib/insn-eval.c112
1 files changed, 106 insertions, 6 deletions
diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index a4427b4e293c..e6cb68a4f7a4 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -814,7 +814,11 @@ static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
814 if (*regoff < 0) 814 if (*regoff < 0)
815 return -EINVAL; 815 return -EINVAL;
816 816
817 *eff_addr = regs_get_register(regs, *regoff); 817 /* Ignore bytes that are outside the address size. */
818 if (insn->addr_bytes == 4)
819 *eff_addr = regs_get_register(regs, *regoff) & 0xffffffff;
820 else /* 64-bit address */
821 *eff_addr = regs_get_register(regs, *regoff);
818 822
819 return 0; 823 return 0;
820} 824}
@@ -846,7 +850,7 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
846{ 850{
847 long tmp; 851 long tmp;
848 852
849 if (insn->addr_bytes != 8) 853 if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
850 return -EINVAL; 854 return -EINVAL;
851 855
852 insn_get_modrm(insn); 856 insn_get_modrm(insn);
@@ -875,7 +879,13 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
875 tmp = regs_get_register(regs, *regoff); 879 tmp = regs_get_register(regs, *regoff);
876 } 880 }
877 881
878 *eff_addr = tmp + insn->displacement.value; 882 if (insn->addr_bytes == 4) {
883 int addr32 = (int)(tmp & 0xffffffff) + insn->displacement.value;
884
885 *eff_addr = addr32 & 0xffffffff;
886 } else {
887 *eff_addr = tmp + insn->displacement.value;
888 }
879 889
880 return 0; 890 return 0;
881} 891}
@@ -908,7 +918,7 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
908 long base, indx; 918 long base, indx;
909 int indx_offset; 919 int indx_offset;
910 920
911 if (insn->addr_bytes != 8) 921 if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
912 return -EINVAL; 922 return -EINVAL;
913 923
914 insn_get_modrm(insn); 924 insn_get_modrm(insn);
@@ -946,12 +956,102 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
946 else 956 else
947 indx = regs_get_register(regs, indx_offset); 957 indx = regs_get_register(regs, indx_offset);
948 958
949 *eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value)); 959 if (insn->addr_bytes == 4) {
960 int addr32, base32, idx32;
961
962 base32 = base & 0xffffffff;
963 idx32 = indx & 0xffffffff;
950 964
951 *eff_addr += insn->displacement.value; 965 addr32 = base32 + idx32 * (1 << X86_SIB_SCALE(insn->sib.value));
966 addr32 += insn->displacement.value;
967
968 *eff_addr = addr32 & 0xffffffff;
969 } else {
970 *eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value));
971 *eff_addr += insn->displacement.value;
972 }
952 973
953 return 0; 974 return 0;
954} 975}
976
977/**
978 * get_addr_ref_32() - Obtain a 32-bit linear address
979 * @insn: Instruction with ModRM, SIB bytes and displacement
980 * @regs: Register values as seen when entering kernel mode
981 *
982 * This function is to be used with 32-bit address encodings to obtain the
983 * linear memory address referred by the instruction's ModRM, SIB,
984 * displacement bytes and segment base address, as applicable. If in protected
985 * mode, segment limits are enforced.
986 *
987 * Returns:
988 *
989 * Linear address referenced by instruction and registers on success.
990 *
991 * -1L on error.
992 */
993static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
994{
995 unsigned long linear_addr = -1L, seg_base, seg_limit;
996 int eff_addr, regoff;
997 long tmp;
998 int ret;
999
1000 if (insn->addr_bytes != 4)
1001 goto out;
1002
1003 if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1004 ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
1005 if (ret)
1006 goto out;
1007
1008 eff_addr = tmp;
1009
1010 } else {
1011 if (insn->sib.nbytes) {
1012 ret = get_eff_addr_sib(insn, regs, &regoff, &tmp);
1013 if (ret)
1014 goto out;
1015
1016 eff_addr = tmp;
1017 } else {
1018 ret = get_eff_addr_modrm(insn, regs, &regoff, &tmp);
1019 if (ret)
1020 goto out;
1021
1022 eff_addr = tmp;
1023 }
1024 }
1025
1026 ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
1027 if (ret)
1028 goto out;
1029
1030 /*
1031 * In protected mode, before computing the linear address, make sure
1032 * the effective address is within the limits of the segment.
1033 * 32-bit addresses can be used in long and virtual-8086 modes if an
1034 * address override prefix is used. In such cases, segment limits are
1035 * not enforced. When in virtual-8086 mode, the segment limit is -1L
1036 * to reflect this situation.
1037 *
1038 * After computed, the effective address is treated as an unsigned
1039 * quantity.
1040 */
1041 if (!user_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit))
1042 goto out;
1043
1044 /*
1045 * Data type long could be 64 bits in size. Ensure that our 32-bit
1046 * effective address is not sign-extended when computing the linear
1047 * address.
1048 */
1049 linear_addr = (unsigned long)(eff_addr & 0xffffffff) + seg_base;
1050
1051out:
1052 return (void __user *)linear_addr;
1053}
1054
955/* 1055/*
956 * return the address being referenced be instruction 1056 * return the address being referenced be instruction
957 * for rm=3 returning the content of the rm reg 1057 * for rm=3 returning the content of the rm reg