x86: Add address resolution code for UMIP and MPX
Subsystems (6)
v10 cover x86: Add address resolution code for UMIP and MPX
This a shortened version of the series "x86: Enable User-Mode Instruction Prevention (UMIP)". This series only includes the code that is used to compute 64-bit linear addresses with and without segmentation plus handling the special cases of each addressing mode.
The purpose of this series is to gather all the patches that have been reviewed during the last nine versions of the series and have it merged in the tip tree (hopefully!).
Thus far, this code is use by MPX. It will also be used by UMIP once enabled. A separate series will deal with the UMIP emulation code as well as 32-bit and 16-bit addresses. A discussion on UMIP and the need for emulation code can be found here [9].
For reference, the nine previous submissions can be found here [1], here [2], here[3], here[4], here[5], here[6], here[7], here[8] and here[9].
This version addresses the feedback comments from Borislav Petkov received on v9. Please see details in the change log.
=== How is this series laid out?
++ Preparatory work
As per suggestions from Andy Lutormirsky and Borislav Petkov, I moved the x86 page fault error codes to a header. Also, I made user_64bit_mode available to x86_32 builds. This helps to reuse code and reduce the number of #ifdef's in these patches. Borislav also suggested to uprobes should use the existing definitions in arch/x86/include/asm/inat.h instead of hard- coded values when checking instruction prefixes. I included this change in the series.
++ Fix bugs in MPX address decoder
I found very useful the code for Intel MPX (Memory Protection Extensions) used to parse opcodes and the memory locations contained in the general purpose registers when used as operands. I put this code in a separate library file that both MPX, UMIP and potentially others can access and avoid code duplication.
Before creating the new library, I fixed several of bugs that I found in in corner cases on how MPX determines the address contained in the instruction and operands.
++ Provide a new x86 instruction evaluating library
With bugs fixed, the MPX evaluating code is relocated in a new insn-eval.c library. The basic functionality of this library is extended to obtain the segment descriptor selected by either segment override prefixes or the default segment by the involved registers in the calculation of the effective address. It was also extended to obtain the default address and operand sizes as well as the segment base address. Armed with this arsenal, it is now possible to determine the linear address indicated by the operands of an instruction structure. This new library relies on and extends the capabilities of the existing instruction decoder in arch/x86/lib/insn.c.
++ Extensive tests
Extensive tests were performed to test all the combinations of ModRM, SiB and displacements for 64-bit addresses; including segmentation via the FS and GS segment registers. For this purpose, I relied on a CPU that features UMIP support for 64-bit process. Emulation could also be tested by using instructions that cause a #GP in readily available systems (e.g., use lgdt instead of sgdt). This change is not part of this patchset. Code of these tests can be found here [13].
++ Merging this series?
As stated, this series contains code that has been reasonably reviewed through 9 versions. It should be in good condition to be merged [14].
[1]. https://lwn.net/Articles/705877/
[2]. https://lkml.org/lkml/2016/12/23/265
[3]. https://lkml.org/lkml/2017/1/25/622
[4]. https://lkml.org/lkml/2017/2/23/40
[5]. https://lkml.org/lkml/2017/3/3/678
[6]. https://lkml.org/lkml/2017/3/7/866
[7]. https://lkml.org/lkml/2017/5/5/398
[8]. https://lkml.org/lkml/2017/8/18/992
[9]. https://lkml.org/lkml/2017/10/3/1066
[13]. https://github.com/01org/luv-yocto/tree/rneri/umip/meta-luv/recipes-core/umip/files
[14]. https://lkml.org/lkml/2017/10/20/763
Thanks and BR,
Ricardo
Changes since V9:
*Shortened the series to group the patches that have been reviewed thus far.
This comprises the code to resolve 64-bit linear addresses with segmentation.
*Reworked the handling of segment resolution for rIP. This is the only case
in which we don't have a valid instruction structure.
*Added a new function get_seg_base_addr() that resolves the segment register
and finds its associated address.
*Added a new function resolve_default_seg() to determine the default segment
associated by a given register. This is to simplify further the function resolve_seg_reg().
*Renamed function get_overridden_seg_reg_idx() as get_seg_reg_override_idx() and
several automatic variables of such function.
*Renamed function allow_seg_reg_overrides() as check_seg_overrides(). *Renamed the function insn_get_code_seg_defaults() as
insn_get_code_seg_params().
Changes since V8:
*Simplified error handling in the family of get_addr_ref_xx functions
by initializing linear address to -1L.
*Reworded commit that #define's an initial state of CR0 and removed unneeded
comment.
*Reworked get_desc() to get rid of one mutex_unlock(). Used a new local variable
to improve readability.
*Reworked the utility functions used to obtain the segment selector:
+ get_overridden_seg_reg_idx() now only inspects the instruction to find segment override prefixes.
+ A new function allow_seg_reg_overrides() determines if segment override prefixes can be used based on the register operand in use and the nature of the instruction (i.e., string instructions vs not).
+ resolve_seg_reg() uses the two functions above, along with user_64bit_mode() to resolve the segment register index: overridden, default or ignored.
*Renamed local variables to reflect the fact that our segment registers are
indexes and not the actual hardware regiters.
*Reworded function documentation for improved readability.
Changes since V7:
*UMIP is not enabled by default.
*Relocated definition of the initial state of CR0 into processor-flags.h *Updated uprobes to use the autogenerated INAT_PFX_xS definitions instead of
hard-coded values.
*In insn-eval.c, refer to segment override prefixes using the autogenerated
INAT_PFX_XS definitions.
*Removed enumeration for segment registers that reused the segment override
instruction prefixes. Instead, a new, separate, set of #defines is used in arch/x86/include/asm/inat.h
*Simplified function to identify string instruction.
*Split the code usde to determine the relevant segment register into two
functions: one to inspect segment overrides and a second one to determine default segment registers based on the instruction and operands. A third functions reads the segment register to obtain the segment selector.
*Reworked arithmetic to compute 32-bit and 64-bit effective addresses. Instead
of type casts, two separate functions are used in each case.
*Removed structure to hold segment default address and operand sizes. Used
#defines instead.
*Corrected bug when determining the limit of a segment.
*Updated various functions to use error codes from errno-base.h *Replaced prink_ratelimited with pr_err_ratelimited.
*Corrected typos and format errors in functions' documentation. *Fixed unimplemented handling of emulation of the SMSW instruction. *Added documentation to file containing implementation for UMIP. *Improved error handling in fixup_umip_exception() function.
Changes since V6:
*Reworded and addded more details on the special cases of ModRM and SIB
bytes. To avoid confusion, I ommited mentioning the involved registers (EBP and ESP).
*Replaced BUG() with printk_ratelimited in function get_reg_offset of
insn-eval.c
*Removed unused utility functions that obtain a register value from pt_regs
given a SIB base and index.
*Clarified nomenclature to call CS, DS, ES, FS, GS and SS segment registers
and their values segment selectors.
*Reworked function resolve_seg_register to issue an error when more than
one segment overrides prefixes are used in the instruction.
*Added logic in resolve_seg_register to ignore segment register when in
long mode and not using FS or GS.
*Added logic to ensure the effective address is within the limits of the
segment in protected mode.
*Added logic to ensure segment override prefixes are ignored when resolving
the segment of EIP and EDI with string instructions.
*Added code to make user_64bit_mode() available in CONFIG_X86_32... and
make it return false, of course.
*Merged the two functions that obtain the default address and operand size
of a code segment into one as they are always used together.
*Corrected logic of displacement-only addressing in long mode to make the
displacement relative to the RIP of the next instruction.
*Reworked logic to sign-extend 32-bit memory offsets into 64-bit signed
memory offsets. This include more checks and putting all together in an utility function.
*Removed the 'unlikely' of conditional statements as we are not in a
critical path.
*In virtual-8086 mode, ensure that effective addresses are always less
than 0x10000, even when address override prefixes are used. Also, ensure that linear addresses have a size of 20-bits.
Changes since V5:
* Relocate the page fault error code enumerations to traps.h
Changes since V4:
* Audited patches to use braces in all the branches of conditional. statements, except those in which the conditional action only takes one line.
* Implemented support in 64-builds for both 32-bit and 64-bit tasks in the instruction evaluating library.
* Split segment selector function in the instruction evaluating library into two functions to resolve the segment type by instruction override or default and a separate function to actually read the segment selector.
* Fixed a bug when evaluating 32-bit effective addresses with 64-bit kernels.
* Split patches further for for easier review.
* Use signed variables for computation of effective address.
* Fixed issue with a spurious static modifier in function insn_get_addr_ref found by kbuild test bot.
* Removed comparison between true and fixup_umip_exception.
* Reworked check logic when identifying erroneous vs invalid values of the SiB base and index.
Changes since V3:
* Limited emulation to 32-bit and 16-bit modes. For 64-bit mode, a general protection fault is still issued when UMIP-protected instructions are executed with CPL > 0.
* Expanded instruction-evaluating code to obtain segment descriptor along with their attributes such as base address and default address and operand sizes. Also, support for 16-bit encodings in protected mode was implemented.
* When getting a segment descriptor, this include support to obtain those of a local descriptor table.
* Now the instruction-evaluating code returns -EDOM when the value of registers should not be used in calculating the effective address. The value -EINVAL is left for errors.
* Incorporate the value of the segment base address in the computation of linear addresses.
* Renamed new instruction evaluation library from insn-kernel.c to insn-eval.c
* Exported functions insn_get_reg_offset_* to obtain the register offset by ModRM r/m, SiB base and SiB index.
* Improved documentation of functions.
* Split patches further for easier review.
Changes since V2:
* Added new utility functions to decode the memory addresses contained in registers when the 16-bit addressing encodings are used. This includes code to obtain and compute memory addresses using segment selectors for real-mode address translation.
* Added support to emulate UMIP-protected instructions for virtual-8086 tasks.
* Added self-tests for virtual-8086 mode that contains representative use cases: address represented as a displacement, address in registers and registers as operands.
* Instead of maintaining a static variable for the dummy base addresses of the IDT and GDT, a hard-coded value is used.
* The emulated SMSW instructions now return the value with which the CR0 register is programmed in head_32/64.S This is: PE | MP | ET | NE | WP | AM. For x86_64, PG is also enabled.
* The new file arch/x86/lib/insn-utils.c is now renamed as arch/x86/lib/ insn-kernel.c. It also has its own header. This helps keep in sync the the kernel and objtool instruction decoders. Also, the new insn-kernel.c contains utility functions that are only relevant in a kernel context.
* Removed printed warnings for errors that occur when decoding instructions with invalid operands.
* Added more comments on fixes in the instruction-decoding MPX functions.
* Now user_64bit_mode(regs) is used instead of test_thread_flag(TIF_IA32) to determine if the task is 32-bit or 64-bit.
* Found and fixed a bug in insn-decoder in which X86_MODRM_RM was incorrectly used to obtain the mod part of the ModRM byte.
* Added more explanatory code in emulation and instruction decoding code. This includes a comment regarding that copy_from_user could fail if there exists a memory protection key in place.
* Tested code with CONFIG_X86_DECODER_SELFTEST=y and everything passes now.
* Prefixed get_reg_offset_rm with insn_ as this function is exposed via a header file. For clarity, this function was added in a separate patch.
Changes since V1:
* Virtual-8086 mode tasks are not treated in a special manner. All code for this purpose was removed.
* Instead of attempting to disable UMIP during a context switch or when entering virtual-8086 mode, UMIP remains enabled all the time. General protection faults that occur are fixed-up by returning dummy values as detailed above.
* Removed umip= kernel parameter in favor of using clearcpuid=514 to disable UMIP.
* Removed selftests designed to detect the absence of SIGSEGV signals when running in virtual-8086 mode.
* Reused code from MPX to decode instructions operands. For this purpose code was put in a common location.
* Fixed two bugs in MPX code that decodes operands.
Ricardo Neri (18):
x86/mm: Relocate page fault error codes to traps.h x86/boot: Relocate definition of the initial state of CR0 ptrace,x86: Make user_64bit_mode() available to 32-bit builds uprobes/x86: Use existing definitions for segment override prefixes x86/mpx: Simplify handling of errors when computing linear addresses x86/mpx: Use signed variables to compute effective addresses x86/mpx: Do not use SIB.index if its value is 100b and ModRM.mod is
not 11b
x86/mpx: Do not use SIB.base if its value is 101b and ModRM.mod = 0 x86/mpx, x86/insn: Relocate insn util functions to a new insn-eval
file
x86/insn-eval: Do not BUG on invalid register type x86/insn-eval: Add a utility function to get register offsets x86/insn-eval: Add utility function to identify string instructions x86/insn-eval: Add utility functions to get segment selector x86/insn-eval: Add utility function to get segment descriptor x86/insn-eval: Add utility functions to get segment descriptor base
address and limit
x86/insn-eval: Add function to get default params of code segment x86/insn-eval: Indicate a 32-bit displacement if ModRM.mod is 0 and
ModRM.rm is 101b
x86/insn-eval: Incorporate segment base in linear address computation
arch/x86/include/asm/inat.h | 10 + arch/x86/include/asm/insn-eval.h | 23 + arch/x86/include/asm/ptrace.h | 6 +- arch/x86/include/asm/traps.h | 18 + arch/x86/include/uapi/asm/processor-flags.h | 3 + arch/x86/kernel/head_32.S | 3 - arch/x86/kernel/head_64.S | 3 - arch/x86/kernel/uprobes.c | 15 +- arch/x86/lib/Makefile | 2 +- arch/x86/lib/insn-eval.c | 854 ++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 88 ++- arch/x86/mm/mpx.c | 120 +--- 12 files changed, 959 insertions(+), 186 deletions(-) create mode 100644 arch/x86/include/asm/insn-eval.h create mode 100644 arch/x86/lib/insn-eval.c
Signature · 1 line
2.7.4
PATCH v10 1/18 x86/mm: Relocate page fault error codes to traps.h
Up to this point, only fault.c used the definitions of the page fault error codes. Thus, it made sense to keep them within such file. Other portions of code might be interested in those definitions too. For instance, the User- Mode Instruction Prevention emulation code will use such definitions to emulate a page fault when it is unable to successfully copy the results of the emulated instructions to user space.
While relocating the error code enumeration, the prefix X86_ is used to make it consistent with the rest of the definitions in traps.h. Of course, code using the enumeration had to be updated as well. No functional changes were performed.
--- arch/x86/include/asm/traps.h | 18 +++++++++ arch/x86/mm/fault.c | 88 +++++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 54 deletions(-)
arch/x86/include/asm/traps.h +18 -0
@@ -144,4 +144,22 @@ enum { X86_TRAP_IRET = 32, /* 32, IRET Exception */ }; +/* +* Page fault error code bits: +* +* bit 0 == 0: no page found 1: protection fault +* bit 1 == 0: read access 1: write access +* bit 2 == 0: kernel-mode access 1: user-mode access +* bit 3 == 1: use of reserved bit detected +* bit 4 == 1: fault was an instruction fetch +* bit 5 == 1: protection keys block access +*/ +enum x86_pf_error_code { +X86_PF_PROT =1 <<0 , +X86_PF_WRITE =1 <<1 , +X86_PF_USER =1 <<2 , +X86_PF_RSVD =1 <<3 , +X86_PF_INSTR =1 <<4 , +X86_PF_PK =1 <<5 , +};#endif /* _ASM_X86_TRAPS_H */
arch/x86/mm/fault.c +34 -54
249 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mm: Relocate page fault error codes to traps.h
x86/mm: Relocate page fault error codes to traps.h
Commit-ID: 1067f030994c69ca1fba8c607437c8895dcf8509 Gitweb: https://git.kernel.org/tip/1067f030994c69ca1fba8c607437c8895dcf8509 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:28 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:07 +0100
x86/mm: Relocate page fault error codes to traps.h
Up to this point, only fault.c used the definitions of the page fault error codes. Thus, it made sense to keep them within such file. Other portions of code might be interested in those definitions too. For instance, the User- Mode Instruction Prevention emulation code will use such definitions to emulate a page fault when it is unable to successfully copy the results of the emulated instructions to user space.
While relocating the error code enumeration, the prefix X86_ is used to make it consistent with the rest of the definitions in traps.h. Of course, code using the enumeration had to be updated as well. No functional changes were performed.
--- arch/x86/include/asm/traps.h | 18 +++++++++ arch/x86/mm/fault.c | 88 +++++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 54 deletions(-)
arch/x86/include/asm/traps.h +18 -0
@@ -144,4 +144,22 @@ enum { X86_TRAP_IRET = 32, /* 32, IRET Exception */ }; +/* +* Page fault error code bits: +* +* bit 0 == 0: no page found 1: protection fault +* bit 1 == 0: read access 1: write access +* bit 2 == 0: kernel-mode access 1: user-mode access +* bit 3 == 1: use of reserved bit detected +* bit 4 == 1: fault was an instruction fetch +* bit 5 == 1: protection keys block access +*/ +enum x86_pf_error_code { +X86_PF_PROT =1 <<0 , +X86_PF_WRITE =1 <<1 , +X86_PF_USER =1 <<2 , +X86_PF_RSVD =1 <<3 , +X86_PF_INSTR =1 <<4 , +X86_PF_PK =1 <<5 , +};#endif /* _ASM_X86_TRAPS_H */
arch/x86/mm/fault.c +34 -54
249 lines not on this page · raw message
PATCH v10 2/18 x86/boot: Relocate definition of the initial state of CR0
In reply to Ricardo NeriBoth head_32.S and head_64.S utilize the same value to initialize the control register CR0. Also, other parts of the kernel might want to access this initial definition (e.g., emulation code for User-Mode Instruction Prevention uses this state to provide a sane dummy value for CR0 when emulating the smsw instruction). Thus, relocate this definition to a header file from which it can be conveniently accessed.
--- arch/x86/include/uapi/asm/processor-flags.h | 3 +++ arch/x86/kernel/head_32.S | 3 --- arch/x86/kernel/head_64.S | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/include/uapi/asm/processor-flags.h +3 -0
@@ -151,5 +151,8 @@#define CX86_ARR_BASE 0xc4#define CX86_RCR_BASE 0xdc +#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ + X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ + X86_CR0_PG)#endif /* _UAPI_ASM_X86_PROCESSOR_FLAGS_H */
arch/x86/kernel/head_32.S +0 -3
@@ -211,9 +211,6 @@ ENTRY(startup_32_smp) #endif .Ldefault_entry: -#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ - X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ - X86_CR0_PG) movl $(CR0_STATE & ~X86_CR0_PG),%eax movl %eax,%cr0
arch/x86/kernel/head_64.S +0 -3
@@ -153,9 +153,6 @@ ENTRY(secondary_startup_64) 1: wrmsr /* Make changes effective */ /* Setup cr0 */ -#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ - X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ - X86_CR0_PG) movl $CR0_STATE, %eax /* Make changes effective */ movq %rax, %cr0
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/boot: Relocate definition of the initial state of CR0
x86/boot: Relocate definition of the initial state of CR0
Commit-ID: b0ce5b8c95c83a7b98c679b117e3d6ae6f97154b Gitweb: https://git.kernel.org/tip/b0ce5b8c95c83a7b98c679b117e3d6ae6f97154b Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:29 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:07 +0100
x86/boot: Relocate definition of the initial state of CR0
Both head_32.S and head_64.S utilize the same value to initialize the control register CR0. Also, other parts of the kernel might want to access this initial definition (e.g., emulation code for User-Mode Instruction Prevention uses this state to provide a sane dummy value for CR0 when emulating the smsw instruction). Thus, relocate this definition to a header file from which it can be conveniently accessed.
--- arch/x86/include/uapi/asm/processor-flags.h | 3 +++ arch/x86/kernel/head_32.S | 3 --- arch/x86/kernel/head_64.S | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/include/uapi/asm/processor-flags.h +3 -0
@@ -151,5 +151,8 @@#define CX86_ARR_BASE 0xc4#define CX86_RCR_BASE 0xdc +#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ + X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ + X86_CR0_PG)#endif /* _UAPI_ASM_X86_PROCESSOR_FLAGS_H */
arch/x86/kernel/head_32.S +0 -3
@@ -211,9 +211,6 @@ ENTRY(startup_32_smp) #endif .Ldefault_entry: -#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ - X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ - X86_CR0_PG) movl $(CR0_STATE & ~X86_CR0_PG),%eax movl %eax,%cr0
arch/x86/kernel/head_64.S +0 -3
@@ -149,9 +149,6 @@ ENTRY(secondary_startup_64) 1: wrmsr /* Make changes effective */ /* Setup cr0 */ -#define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \ - X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ - X86_CR0_PG) movl $CR0_STATE, %eax /* Make changes effective */ movq %rax, %cr0
PATCH v10 3/18 ptrace,x86: Make user_64bit_mode() available to 32-bit builds
In reply to Ricardo NeriIn its current form, user_64bit_mode() can only be used when CONFIG_X86_64 is selected. This implies that code built with CONFIG_X86_64=n cannot use it. If a piece of code needs to be built for both CONFIG_X86_64=y and CONFIG_X86_64=n and wants to use this function, it needs to wrap it in an #ifdef/#endif; potentially, in multiple places.
This can be easily avoided with a single #ifdef/#endif pair within user_64bit_mode() itself.
--- arch/x86/include/asm/ptrace.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/ptrace.h +5 -1
@@ -135,9 +135,9 @@ static inline int v8086_mode(struct pt_regs *regs)#endif } -#ifdef CONFIG_X86_64 static inline bool user_64bit_mode (struct pt_regs *regs) { +#ifdef CONFIG_X86_64 #ifndef CONFIG_PARAVIRT /* * On non-paravirt systems, this is the only long mode CPL 3
@@ -148,8 +148,12 @@ static inline bool user_64bit_mode(struct pt_regs *regs)/* Headers are too twisted for this to go in paravirt.h. */ return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;#endif +#else /* !CONFIG_X86_64 */ +return false; +#endif } +#ifdef CONFIG_X86_64 #define current_user_stack_pointer () current_pt_regs()->sp#define compat_user_stack_pointer () current_pt_regs()->sp#endif
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] ptrace,x86: Make user_64bit_mode() available to 32-bit builds
ptrace,x86: Make user_64bit_mode() available to 32-bit builds
Commit-ID: e27c310af5c05cf876d9cad006928076c27f54d4 Gitweb: https://git.kernel.org/tip/e27c310af5c05cf876d9cad006928076c27f54d4 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:30 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:08 +0100
ptrace,x86: Make user_64bit_mode() available to 32-bit builds
In its current form, user_64bit_mode() can only be used when CONFIG_X86_64 is selected. This implies that code built with CONFIG_X86_64=n cannot use it. If a piece of code needs to be built for both CONFIG_X86_64=y and CONFIG_X86_64=n and wants to use this function, it needs to wrap it in an #ifdef/#endif; potentially, in multiple places.
This can be easily avoided with a single #ifdef/#endif pair within user_64bit_mode() itself.
--- arch/x86/include/asm/ptrace.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/ptrace.h +5 -1
@@ -135,9 +135,9 @@ static inline int v8086_mode(struct pt_regs *regs)#endif } -#ifdef CONFIG_X86_64 static inline bool user_64bit_mode (struct pt_regs *regs) { +#ifdef CONFIG_X86_64 #ifndef CONFIG_PARAVIRT /* * On non-paravirt systems, this is the only long mode CPL 3
@@ -148,8 +148,12 @@ static inline bool user_64bit_mode(struct pt_regs *regs)/* Headers are too twisted for this to go in paravirt.h. */ return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;#endif +#else /* !CONFIG_X86_64 */ +return false; +#endif } +#ifdef CONFIG_X86_64 #define current_user_stack_pointer () current_pt_regs()->sp#define compat_user_stack_pointer () current_pt_regs()->sp#endif
PATCH v10 4/18 uprobes/x86: Use existing definitions for segment override prefixes
In reply to Ricardo NeriRather than using hard-coded values of the segment override prefixes, leverage the existing definitions provided in inat.h.
--- arch/x86/kernel/uprobes.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kernel/uprobes.c +9 -6
@@ -271,12 +271,15 @@ static bool is_prefix_bad(struct insn *insn)int i;for (i =0 ; i < insn->prefixes.nbytes; i++) { -switch (insn->prefixes.bytes[i]) { -case 0x26 :/* INAT_PFX_ES */ -case 0x2E :/* INAT_PFX_CS */ -case 0x36 :/* INAT_PFX_DS */ -case 0x3E :/* INAT_PFX_SS */ -case 0xF0 :/* INAT_PFX_LOCK */ +insn_attr_t attr; + + attr =inat_get_opcode_attribute (insn->prefixes.bytes[i]); +switch (attr) { +case INAT_MAKE_PREFIX (INAT_PFX_ES ): +case INAT_MAKE_PREFIX (INAT_PFX_CS ): +case INAT_MAKE_PREFIX (INAT_PFX_DS ): +case INAT_MAKE_PREFIX (INAT_PFX_SS ): +case INAT_MAKE_PREFIX (INAT_PFX_LOCK ):return true; } }
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] uprobes/x86: Use existing definitions for segment override prefixes
uprobes/x86: Use existing definitions for segment override prefixes
Commit-ID: ed40a10431701d683bfd59f7ca01a8c97408cf67 Gitweb: https://git.kernel.org/tip/ed40a10431701d683bfd59f7ca01a8c97408cf67 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:31 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:08 +0100
uprobes/x86: Use existing definitions for segment override prefixes
Rather than using hard-coded values of the segment override prefixes, leverage the existing definitions provided in inat.h.
--- arch/x86/kernel/uprobes.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kernel/uprobes.c +9 -6
@@ -271,12 +271,15 @@ static bool is_prefix_bad(struct insn *insn)int i;for (i =0 ; i < insn->prefixes.nbytes; i++) { -switch (insn->prefixes.bytes[i]) { -case 0x26 :/* INAT_PFX_ES */ -case 0x2E :/* INAT_PFX_CS */ -case 0x36 :/* INAT_PFX_DS */ -case 0x3E :/* INAT_PFX_SS */ -case 0xF0 :/* INAT_PFX_LOCK */ +insn_attr_t attr; + + attr =inat_get_opcode_attribute (insn->prefixes.bytes[i]); +switch (attr) { +case INAT_MAKE_PREFIX (INAT_PFX_ES ): +case INAT_MAKE_PREFIX (INAT_PFX_CS ): +case INAT_MAKE_PREFIX (INAT_PFX_DS ): +case INAT_MAKE_PREFIX (INAT_PFX_SS ): +case INAT_MAKE_PREFIX (INAT_PFX_LOCK ):return true; } }
PATCH v10 5/18 x86/mpx: Simplify handling of errors when computing linear addresses
In reply to Ricardo NeriWhen errors occur in the computation of the linear address, -1L is returned. Rather than having a separate return path for errors, the variable used to return the computed linear address can be initialized with the error value. Hence, only one return path is needed. This makes the function easier to read.
While here, ensure that the error value is -1L, a 64-bit value, rather than -1, a 32-bit value.
--- arch/x86/mm/mpx.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/mm/mpx.c +6 -7
@@ -138,7 +138,7 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,*/ static void __user *mpx_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -unsigned long addr, base, indx; +unsigned long addr =-1L , base, indx;int addr_offset, base_offset, indx_offset;insn_byte_t sib;
@@ -149,17 +149,17 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)if (X86_MODRM_MOD (insn->modrm.value) ==3 ) { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) - goto out_err; + goto out; addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE );if (base_offset <0 ) - goto out_err; + goto out; indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX );if (indx_offset <0 ) - goto out_err; + goto out; base =regs_get_register (regs, base_offset); indx =regs_get_register (regs, indx_offset);
@@ -167,14 +167,13 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) - goto out_err; + goto out; addr =regs_get_register (regs, addr_offset); } addr += insn->displacement.value; } +out:return (void __user *)addr; -out_err: -return (void __user *)-1 ; } static int mpx_insn_decode(struct insn *insn,
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mpx: Simplify handling of errors when computing linear addresses
x86/mpx: Simplify handling of errors when computing linear addresses
Commit-ID: b15d70df6e685912be8bbcb7557d277d48aa942c Gitweb: https://git.kernel.org/tip/b15d70df6e685912be8bbcb7557d277d48aa942c Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:32 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:08 +0100
x86/mpx: Simplify handling of errors when computing linear addresses
When errors occur in the computation of the linear address, -1L is returned. Rather than having a separate return path for errors, the variable used to return the computed linear address can be initialized with the error value. Hence, only one return path is needed. This makes the function easier to read.
While here, ensure that the error value is -1L, a 64-bit value, rather than -1, a 32-bit value.
--- arch/x86/mm/mpx.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/mm/mpx.c +6 -7
@@ -138,7 +138,7 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,*/ static void __user *mpx_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -unsigned long addr, base, indx; +unsigned long addr =-1L , base, indx;int addr_offset, base_offset, indx_offset;insn_byte_t sib;
@@ -149,17 +149,17 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)if (X86_MODRM_MOD (insn->modrm.value) ==3 ) { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) - goto out_err; + goto out; addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE );if (base_offset <0 ) - goto out_err; + goto out; indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX );if (indx_offset <0 ) - goto out_err; + goto out; base =regs_get_register (regs, base_offset); indx =regs_get_register (regs, indx_offset);
@@ -167,14 +167,13 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) - goto out_err; + goto out; addr =regs_get_register (regs, addr_offset); } addr += insn->displacement.value; } +out:return (void __user *)addr; -out_err: -return (void __user *)-1 ; } static int mpx_insn_decode(struct insn *insn,
PATCH v10 6/18 x86/mpx: Use signed variables to compute effective addresses
In reply to Ricardo NeriEven though memory addresses are unsigned, the operands used to compute the effective address do have a sign. This is true for ModRM.rm, SIB.base, SIB.index as well as the displacement bytes. Thus, signed variables shall be used when computing the effective address from these operands. Once the signed effective address has been computed, it is casted to an unsigned long to determine the linear address.
Variables are renamed to better reflect the type of address being computed.
--- arch/x86/mm/mpx.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
arch/x86/mm/mpx.c +14 -6
@@ -138,8 +138,9 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,*/ static void __user *mpx_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -unsigned long addr =-1L , base, indx;int addr_offset, base_offset, indx_offset; +unsigned long linear_addr =-1L ; +long eff_addr, base, indx;insn_byte_t sib;insn_get_modrm (insn);
@@ -150,7 +151,8 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) goto out; - addr =regs_get_register (regs, addr_offset); + + eff_addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE );
@@ -163,17 +165,23 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) base =regs_get_register (regs, base_offset); indx =regs_get_register (regs, indx_offset); - addr = base + indx * (1 <<X86_SIB_SCALE (sib)); + + eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) goto out; - addr =regs_get_register (regs, addr_offset); + + eff_addr =regs_get_register (regs, addr_offset); } - addr += insn->displacement.value; + + eff_addr += insn->displacement.value; } + + linear_addr = (unsigned long )eff_addr; + out: -return (void __user *)addr; +return (void __user *)linear_addr; } static int mpx_insn_decode(struct insn *insn,
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mpx: Use signed variables to compute effective addresses
x86/mpx: Use signed variables to compute effective addresses
Commit-ID: b8d2eff3b1c6e46238a5fb3f56843e9974b4889f Gitweb: https://git.kernel.org/tip/b8d2eff3b1c6e46238a5fb3f56843e9974b4889f Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:33 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:09 +0100
x86/mpx: Use signed variables to compute effective addresses
Even though memory addresses are unsigned, the operands used to compute the effective address do have a sign. This is true for ModRM.rm, SIB.base, SIB.index as well as the displacement bytes. Thus, signed variables shall be used when computing the effective address from these operands. Once the signed effective address has been computed, it is casted to an unsigned long to determine the linear address.
Variables are renamed to better reflect the type of address being computed.
--- arch/x86/mm/mpx.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
arch/x86/mm/mpx.c +14 -6
@@ -138,8 +138,9 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,*/ static void __user *mpx_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -unsigned long addr =-1L , base, indx;int addr_offset, base_offset, indx_offset; +unsigned long linear_addr =-1L ; +long eff_addr, base, indx;insn_byte_t sib;insn_get_modrm (insn);
@@ -150,7 +151,8 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) goto out; - addr =regs_get_register (regs, addr_offset); + + eff_addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE );
@@ -163,17 +165,23 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) base =regs_get_register (regs, base_offset); indx =regs_get_register (regs, indx_offset); - addr = base + indx * (1 <<X86_SIB_SCALE (sib)); + + eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );if (addr_offset <0 ) goto out; - addr =regs_get_register (regs, addr_offset); + + eff_addr =regs_get_register (regs, addr_offset); } - addr += insn->displacement.value; + + eff_addr += insn->displacement.value; } + + linear_addr = (unsigned long )eff_addr; + out: -return (void __user *)addr; +return (void __user *)linear_addr; } static int mpx_insn_decode(struct insn *insn,
PATCH v10 7/18 x86/mpx: Do not use SIB.index if its value is 100b and ModRM.mod is not 11b
In reply to Ricardo NeriSection 2.2.1.2 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that when ModRM.mod !=11b and ModRM.rm = 100b indexed register-indirect addressing is used. In other words, a SIB byte follows the ModRM byte. In the specific case of SIB.index = 100b, the scale*index portion of the computation of the effective address is null. To signal callers of this particular situation, get_reg_offset() can return -EDOM (-EINVAL continues to indicate that an error when decoding the SIB byte).
An example of this situation can be the following instruction:
8b 4c 23 80 mov -0x80(%rbx,%riz,1),%rcx ModRM: 0x4c [mod:1b][reg:1b][rm:100b] SIB: 0x23 [scale:0b][index:100b][base:11b] Displacement: 0x80 (1-byte, as per ModRM.mod = 1b)
The %riz 'register' indicates a null index.
In long mode, a REX prefix may be used. When a REX prefix is present, REX.X adds a fourth bit to the register selection of SIB.index. This gives the ability to refer to all the 16 general purpose registers. When REX.X is 1b and SIB.index is 100b, the index is indicated in %r12. In our example, this would look like:
42 8b 4c 23 80 mov -0x80(%rbx,%r12,1),%rcx REX: 0x42 [W:0b][R:0b][X:1b][B:0b] ModRM: 0x4c [mod:1b][reg:1b][rm:100b] SIB: 0x23 [scale:0b][.X: 1b, index:100b][.B:0b, base:11b] Displacement: 0x80 (1-byte, as per ModRM.mod = 1b)
%r12 is a valid register to use in the scale*index part of the effective address computation.
--- arch/x86/mm/mpx.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
arch/x86/mm/mpx.c +19 -2
@@ -110,6 +110,15 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs, regno =X86_SIB_INDEX (insn->sib.value);if (X86_REX_X (insn->rex_prefix.value)) regno +=8 ; + +/* +* If ModRM.mod != 3 and SIB.index = 4 the scale*index +* portion of the address computation is null. This is +* true only if REX.X is 0. In such a case, the SIB index +* is used in the address computation. +*/ +if (X86_MODRM_MOD (insn->modrm.value) !=3 && regno ==4 ) +return -EDOM ;break ;case REG_TYPE_BASE :
@@ -160,11 +169,19 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) goto out; indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX ); -if (indx_offset <0 ) +/* +* A negative offset generally means a error, except +* -EDOM, which means that the contents of the register +* should not be used as index. +*/ +if (indx_offset == -EDOM ) + indx =0 ; +else if (indx_offset <0 ) goto out; +else + indx =regs_get_register (regs, indx_offset); base =regs_get_register (regs, base_offset); - indx =regs_get_register (regs, indx_offset); eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else {
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mpx: Do not use SIB.index if its value is 100b and ModRM.mod is not 11b
x86/mpx: Do not use SIB.index if its value is 100b and ModRM.mod is not 11b
Commit-ID: ff9d78025c519046cfbc212b34f09116685402fc Gitweb: https://git.kernel.org/tip/ff9d78025c519046cfbc212b34f09116685402fc Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:34 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:09 +0100
x86/mpx: Do not use SIB.index if its value is 100b and ModRM.mod is not 11b
Section 2.2.1.2 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that when ModRM.mod !=11b and ModRM.rm = 100b indexed register-indirect addressing is used. In other words, a SIB byte follows the ModRM byte. In the specific case of SIB.index = 100b, the scale*index portion of the computation of the effective address is null. To signal callers of this particular situation, get_reg_offset() can return -EDOM (-EINVAL continues to indicate that an error when decoding the SIB byte).
An example of this situation can be the following instruction:
8b 4c 23 80 mov -0x80(%rbx,%riz,1),%rcx ModRM: 0x4c [mod:1b][reg:1b][rm:100b] SIB: 0x23 [scale:0b][index:100b][base:11b] Displacement: 0x80 (1-byte, as per ModRM.mod = 1b)
The %riz 'register' indicates a null index.
In long mode, a REX prefix may be used. When a REX prefix is present, REX.X adds a fourth bit to the register selection of SIB.index. This gives the ability to refer to all the 16 general purpose registers. When REX.X is 1b and SIB.index is 100b, the index is indicated in %r12. In our example, this would look like:
42 8b 4c 23 80 mov -0x80(%rbx,%r12,1),%rcx REX: 0x42 [W:0b][R:0b][X:1b][B:0b] ModRM: 0x4c [mod:1b][reg:1b][rm:100b] SIB: 0x23 [scale:0b][.X: 1b, index:100b][.B:0b, base:11b] Displacement: 0x80 (1-byte, as per ModRM.mod = 1b)
%r12 is a valid register to use in the scale*index part of the effective address computation.
--- arch/x86/mm/mpx.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
arch/x86/mm/mpx.c +19 -2
@@ -110,6 +110,15 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs, regno =X86_SIB_INDEX (insn->sib.value);if (X86_REX_X (insn->rex_prefix.value)) regno +=8 ; + +/* +* If ModRM.mod != 3 and SIB.index = 4 the scale*index +* portion of the address computation is null. This is +* true only if REX.X is 0. In such a case, the SIB index +* is used in the address computation. +*/ +if (X86_MODRM_MOD (insn->modrm.value) !=3 && regno ==4 ) +return -EDOM ;break ;case REG_TYPE_BASE :
@@ -160,11 +169,19 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) goto out; indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX ); -if (indx_offset <0 ) +/* +* A negative offset generally means a error, except +* -EDOM, which means that the contents of the register +* should not be used as index. +*/ +if (indx_offset == -EDOM ) + indx =0 ; +else if (indx_offset <0 ) goto out; +else + indx =regs_get_register (regs, indx_offset); base =regs_get_register (regs, base_offset); - indx =regs_get_register (regs, indx_offset); eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else {
PATCH v10 8/18 x86/mpx: Do not use SIB.base if its value is 101b and ModRM.mod = 0
In reply to Ricardo NeriSection 2.2.1.2 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that if a SIB byte is used and SIB.base is 101b and ModRM.mod is zero, then the base part of the base part of the effective address computation is null. To signal this situation, a -EDOM error is returned to indicate callers to ignore the base value present in the register operand.
In this scenario, a 32-bit displacement follows the SIB byte. Displacement is obtained when the instruction decoder parses the operands.
--- arch/x86/mm/mpx.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
arch/x86/mm/mpx.c +20 -8
@@ -123,6 +123,14 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,case REG_TYPE_BASE : regno =X86_SIB_BASE (insn->sib.value); +/* +* If ModRM.mod is 0 and SIB.base == 5, the base of the +* register-indirect addressing is 0. In this case, a +* 32-bit displacement follows the SIB byte. +*/ +if (!X86_MODRM_MOD (insn->modrm.value) && regno ==5 ) +return -EDOM ; +if (X86_REX_B (insn->rex_prefix.value)) regno +=8 ;break ;
@@ -164,16 +172,22 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { +/* +* Negative values in the base and index offset means +* an error when decoding the SIB byte. Except -EDOM, +* which means that the registers should not be used +* in the address computation. +*/ base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE ); -if (base_offset <0 ) +if (base_offset == -EDOM ) + base =0 ; +else if (base_offset <0 ) goto out; +else + base =regs_get_register (regs, base_offset); indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX ); -/* -* A negative offset generally means a error, except -* -EDOM, which means that the contents of the register -* should not be used as index. -*/ +if (indx_offset == -EDOM ) indx =0 ;else if (indx_offset <0 )
@@ -181,8 +195,6 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)else indx =regs_get_register (regs, indx_offset); - base =regs_get_register (regs, base_offset); - eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mpx: Do not use SIB.base if its value is 101b and ModRM.mod = 0
x86/mpx: Do not use SIB.base if its value is 101b and ModRM.mod = 0
Commit-ID: 4578f06fc93fb73c9c644ed838f4cdabbfdc4df1 Gitweb: https://git.kernel.org/tip/4578f06fc93fb73c9c644ed838f4cdabbfdc4df1 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:35 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:10 +0100
x86/mpx: Do not use SIB.base if its value is 101b and ModRM.mod = 0
Section 2.2.1.2 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that if a SIB byte is used and SIB.base is 101b and ModRM.mod is zero, then the base part of the base part of the effective address computation is null. To signal this situation, a -EDOM error is returned to indicate callers to ignore the base value present in the register operand.
In this scenario, a 32-bit displacement follows the SIB byte. Displacement is obtained when the instruction decoder parses the operands.
--- arch/x86/mm/mpx.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
arch/x86/mm/mpx.c +20 -8
@@ -123,6 +123,14 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,case REG_TYPE_BASE : regno =X86_SIB_BASE (insn->sib.value); +/* +* If ModRM.mod is 0 and SIB.base == 5, the base of the +* register-indirect addressing is 0. In this case, a +* 32-bit displacement follows the SIB byte. +*/ +if (!X86_MODRM_MOD (insn->modrm.value) && regno ==5 ) +return -EDOM ; +if (X86_REX_B (insn->rex_prefix.value)) regno +=8 ;break ;
@@ -164,16 +172,22 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr =regs_get_register (regs, addr_offset); }else {if (insn->sib.nbytes) { +/* +* Negative values in the base and index offset means +* an error when decoding the SIB byte. Except -EDOM, +* which means that the registers should not be used +* in the address computation. +*/ base_offset =get_reg_offset (insn, regs,REG_TYPE_BASE ); -if (base_offset <0 ) +if (base_offset == -EDOM ) + base =0 ; +else if (base_offset <0 ) goto out; +else + base =regs_get_register (regs, base_offset); indx_offset =get_reg_offset (insn, regs,REG_TYPE_INDEX ); -/* -* A negative offset generally means a error, except -* -EDOM, which means that the contents of the register -* should not be used as index. -*/ +if (indx_offset == -EDOM ) indx =0 ;else if (indx_offset <0 )
@@ -181,8 +195,6 @@ static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)else indx =regs_get_register (regs, indx_offset); - base =regs_get_register (regs, base_offset); - eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );
PATCH v10 9/18 x86/mpx, x86/insn: Relocate insn util functions to a new insn-eval file
In reply to Ricardo NeriOther kernel submodules can benefit from using the utility functions defined in mpx.c to obtain the addresses and values of operands contained in the general purpose registers. An instance of this is the emulation code used for instructions protected by the Intel User-Mode Instruction Prevention feature.
Thus, these functions are relocated to a new insn-eval.c file. The reason to not relocate these utilities into insn.c is that the latter solely analyses instructions given by a struct insn without any knowledge of the meaning of the values of instruction operands. This new utility insn- eval.c aims to be used to resolve userspace linear addresses based on the contents of the instruction operands as well as the contents of pt_regs structure.
These utilities come with a separate header. This is to avoid taking insn.c out of sync from the instructions decoders under tools/obj and tools/perf. This also avoids adding cumbersome #ifdef's for the #include'd files required to decode instructions in a kernel context.
Functions are simply relocated. There are not functional or indentation changes.
---
The checkpatch script issues the following warning with this commit:
WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()
+ BUG();
This warning will be fixed in a subsequent patch.
--- arch/x86/include/asm/insn-eval.h | 16 ++++ arch/x86/lib/Makefile | 2 +- arch/x86/lib/insn-eval.c | 163 +++++++++++++++++++++++++++++++++++++++ arch/x86/mm/mpx.c | 156 +------------------------------------ 4 files changed, 182 insertions(+), 155 deletions(-) create mode 100644 arch/x86/include/asm/insn-eval.h create mode 100644 arch/x86/lib/insn-eval.c
arch/x86/include/asm/insn-eval.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef _ASM_X86_INSN_EVAL_H +#define _ASM_X86_INSN_EVAL_H +/* +* A collection of utility functions for x86 instruction analysis to be +* used in a kernel context. Useful when, for instance, making sense +* of the registers indicated by operands. +*/ + +#include <linux/compiler.h> +#include <linux/bug.h> +#include <linux/err.h> +#include <asm/ptrace.h> + +void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs); + +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/Makefile +1 -1
@@ -23,7 +23,7 @@ lib-y := delay.o misc.o cmdline.o cpu.o lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o -lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o +lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
arch/x86/lib/insn-eval.c new file +163 -0
163 lines not on this page · raw message
arch/x86/mm/mpx.c +2 -154
174 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/mpx, x86/insn: Relocate insn util functions to a new insn-eval file
x86/mpx, x86/insn: Relocate insn util functions to a new insn-eval file
Commit-ID: 32542ee295bec38e5e1608f8c9d6d28e5a7e6112 Gitweb: https://git.kernel.org/tip/32542ee295bec38e5e1608f8c9d6d28e5a7e6112 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:36 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:10 +0100
x86/mpx, x86/insn: Relocate insn util functions to a new insn-eval file
Other kernel submodules can benefit from using the utility functions defined in mpx.c to obtain the addresses and values of operands contained in the general purpose registers. An instance of this is the emulation code used for instructions protected by the Intel User-Mode Instruction Prevention feature.
Thus, these functions are relocated to a new insn-eval.c file. The reason to not relocate these utilities into insn.c is that the latter solely analyses instructions given by a struct insn without any knowledge of the meaning of the values of instruction operands. This new utility insn- eval.c aims to be used to resolve userspace linear addresses based on the contents of the instruction operands as well as the contents of pt_regs structure.
These utilities come with a separate header. This is to avoid taking insn.c out of sync from the instructions decoders under tools/obj and tools/perf. This also avoids adding cumbersome #ifdef's for the #include'd files required to decode instructions in a kernel context.
Functions are simply relocated. There are not functional or indentation changes.
--- arch/x86/include/asm/insn-eval.h | 16 ++++ arch/x86/lib/Makefile | 2 +- arch/x86/lib/insn-eval.c | 163 +++++++++++++++++++++++++++++++++++++++ arch/x86/mm/mpx.c | 156 +------------------------------------ 4 files changed, 182 insertions(+), 155 deletions(-)
arch/x86/include/asm/insn-eval.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef _ASM_X86_INSN_EVAL_H +#define _ASM_X86_INSN_EVAL_H +/* +* A collection of utility functions for x86 instruction analysis to be +* used in a kernel context. Useful when, for instance, making sense +* of the registers indicated by operands. +*/ + +#include <linux/compiler.h> +#include <linux/bug.h> +#include <linux/err.h> +#include <asm/ptrace.h> + +void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs); + +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/Makefile +1 -1
@@ -23,7 +23,7 @@ lib-y := delay.o misc.o cmdline.o cpu.o lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o -lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o +lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
arch/x86/lib/insn-eval.c new file +163 -0
163 lines not on this page · raw message
arch/x86/mm/mpx.c +2 -154
174 lines not on this page · raw message
PATCH v10 10/18 x86/insn-eval: Do not BUG on invalid register type
In reply to Ricardo NeriWe are not in a critical failure path. The invalid register type is caused when trying to decode invalid instruction bytes from a user-space program. Thus, simply print an error message. To prevent this warning from being abused from user space programs, use the rate-limited variant of pr_err(). along with a descriptive prefix.
--- arch/x86/lib/insn-eval.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +6 -3
@@ -5,10 +5,14 @@*/ #include <linux/kernel.h> #include <linux/string.h> +#include <linux/ratelimit.h> #include <asm/inat.h> #include <asm/insn.h> #include <asm/insn-eval.h> +#undef pr_fmt +#define pr_fmt (fmt) "insn: " fmt +enum reg_type {REG_TYPE_RM =0 ,REG_TYPE_INDEX ,
@@ -85,9 +89,8 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,break ;default : -pr_err ("invalid register type" ); -BUG (); -break ; +pr_err_ratelimited ("invalid register type: %d\n" , type); +return -EINVAL ; }if (regno >= nr_registers) {
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Do not BUG on invalid register type
x86/insn-eval: Do not BUG on invalid register type
Commit-ID: ed594e4ba5bfe268d63d7cee3c1a827e3dd5056f Gitweb: https://git.kernel.org/tip/ed594e4ba5bfe268d63d7cee3c1a827e3dd5056f Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:37 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:10 +0100
x86/insn-eval: Do not BUG on invalid register type
We are not in a critical failure path. The invalid register type is caused when trying to decode invalid instruction bytes from a user-space program. Thus, simply print an error message. To prevent this warning from being abused from user space programs, use the rate-limited variant of pr_err(). along with a descriptive prefix.
--- arch/x86/lib/insn-eval.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +6 -3
@@ -5,10 +5,14 @@*/ #include <linux/kernel.h> #include <linux/string.h> +#include <linux/ratelimit.h> #include <asm/inat.h> #include <asm/insn.h> #include <asm/insn-eval.h> +#undef pr_fmt +#define pr_fmt (fmt) "insn: " fmt +enum reg_type {REG_TYPE_RM =0 ,REG_TYPE_INDEX ,
@@ -85,9 +89,8 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,break ;default : -pr_err ("invalid register type" ); -BUG (); -break ; +pr_err_ratelimited ("invalid register type: %d\n" , type); +return -EINVAL ; }if (regno >= nr_registers) {
PATCH v10 11/18 x86/insn-eval: Add a utility function to get register offsets
In reply to Ricardo NeriThe function get_reg_offset() returns the offset to the register the argument specifies as indicated in an enumeration of type offset. Callers of this function would need the definition of such enumeration. This is not needed. Instead, add helper functions for this purpose. These functions are useful in cases when, for instance, the caller needs to decide whether the operand is a register or a memory location by looking at the rm part of the ModRM byte. As of now, this is the only helper function that is needed.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -12,5 +12,6 @@#include <asm/ptrace.h> void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs); +int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +17 -0
@@ -100,6 +100,23 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,return regoff[regno]; } +/** +* insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the r/m part of the ModRM byte. The +* register is obtained as an offset from the base of pt_regs. In specific +* cases, the returned value can be -EDOM to indicate that the particular value +* of ModRM does not refer to a register and shall be ignored. +*/ +int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs) +{ +return get_reg_offset (insn, regs,REG_TYPE_RM ); +} +/* * return the address being referenced be instruction * for rm=3 returning the content of the rm reg
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add a utility function to get register offsets
x86/insn-eval: Add a utility function to get register offsets
Commit-ID: e5e45f11110191740ecb365fa8c7a25814ce8ac8 Gitweb: https://git.kernel.org/tip/e5e45f11110191740ecb365fa8c7a25814ce8ac8 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:38 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:11 +0100
x86/insn-eval: Add a utility function to get register offsets
The function get_reg_offset() returns the offset to the register the argument specifies as indicated in an enumeration of type offset. Callers of this function would need the definition of such enumeration. This is not needed. Instead, add helper functions for this purpose. These functions are useful in cases when, for instance, the caller needs to decide whether the operand is a register or a memory location by looking at the rm part of the ModRM byte. As of now, this is the only helper function that is needed.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -12,5 +12,6 @@#include <asm/ptrace.h> void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs); +int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +17 -0
@@ -100,6 +100,23 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,return regoff[regno]; } +/** +* insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the r/m part of the ModRM byte. The +* register is obtained as an offset from the base of pt_regs. In specific +* cases, the returned value can be -EDOM to indicate that the particular value +* of ModRM does not refer to a register and shall be ignored. +*/ +int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs) +{ +return get_reg_offset (insn, regs,REG_TYPE_RM ); +} +/* * return the address being referenced be instruction * for rm=3 returning the content of the rm reg
PATCH v10 12/18 x86/insn-eval: Add utility function to identify string instructions
In reply to Ricardo NeriString instructions are special because, in protected mode, the linear address is always obtained via the ES segment register in operands that use the (E)DI register; the DS segment register in operands that use the (E)SI register. Furthermore, segment override prefixes are ignored when calculating a linear address involving the (E)DI register; segment override prefixes can be used when calculating linear addresses involving the (E)SI register.
It follows that linear addresses are calculated differently for the case of string instructions. The purpose of this utility function is to identify such instructions for callers to determine a linear address correctly.
Note that this function only identifies string instructions; it does not determine what segment register to use in the address computation. That is left to callers. A subsequent commmit introduces a function to determine the segment register to use given the instruction, operands and segment override prefixes.
--- arch/x86/lib/insn-eval.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
arch/x86/lib/insn-eval.c +28 -0
@@ -19,6 +19,34 @@ enum reg_type { REG_TYPE_BASE, }; +/** +* is_string_insn() - Determine if instruction is a string instruction +* @insn: Instruction containing the opcode to inspect +* +* Returns: +* +* true if the instruction, determined by the opcode, is any of the +* string instructions as defined in the Intel Software Development manual. +* False otherwise. +*/ +static bool is_string_insn (struct insn *insn) +{ +insn_get_opcode (insn); + +/* All string instructions have a 1-byte opcode. */ +if (insn->opcode.nbytes !=1 ) +return false; + +switch (insn->opcode.bytes[0 ]) { + case 0x6c ... 0x6f: /* INS, OUTS */ + case 0xa4 ... 0xa7: /* MOVS, CMPS */ + case 0xaa ... 0xaf: /* STOS, LODS, SCAS */ +return true; +default : +return false; + } +} +static int get_reg_offset (struct insn *insn,struct pt_regs *regs,enum reg_type type) {
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add utility function to identify string instructions
x86/insn-eval: Add utility function to identify string instructions
Commit-ID: 536b815388f7f4d2a7cd1418939902fb037ea370 Gitweb: https://git.kernel.org/tip/536b815388f7f4d2a7cd1418939902fb037ea370 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:39 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:11 +0100
x86/insn-eval: Add utility function to identify string instructions
String instructions are special because, in protected mode, the linear address is always obtained via the ES segment register in operands that use the (E)DI register; the DS segment register in operands that use the (E)SI register. Furthermore, segment override prefixes are ignored when calculating a linear address involving the (E)DI register; segment override prefixes can be used when calculating linear addresses involving the (E)SI register.
It follows that linear addresses are calculated differently for the case of string instructions. The purpose of this utility function is to identify such instructions for callers to determine a linear address correctly.
Note that this function only identifies string instructions; it does not determine what segment register to use in the address computation. That is left to callers. A subsequent commmit introduces a function to determine the segment register to use given the instruction, operands and segment override prefixes.
--- arch/x86/lib/insn-eval.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
arch/x86/lib/insn-eval.c +28 -0
@@ -19,6 +19,34 @@ enum reg_type { REG_TYPE_BASE, }; +/** +* is_string_insn() - Determine if instruction is a string instruction +* @insn: Instruction containing the opcode to inspect +* +* Returns: +* +* true if the instruction, determined by the opcode, is any of the +* string instructions as defined in the Intel Software Development manual. +* False otherwise. +*/ +static bool is_string_insn (struct insn *insn) +{ +insn_get_opcode (insn); + +/* All string instructions have a 1-byte opcode. */ +if (insn->opcode.nbytes !=1 ) +return false; + +switch (insn->opcode.bytes[0 ]) { + case 0x6c ... 0x6f: /* INS, OUTS */ + case 0xa4 ... 0xa7: /* MOVS, CMPS */ + case 0xaa ... 0xaf: /* STOS, LODS, SCAS */ +return true; +default : +return false; + } +} +static int get_reg_offset (struct insn *insn,struct pt_regs *regs,enum reg_type type) {
PATCH v10 13/18 x86/insn-eval: Add utility functions to get segment selector
In reply to Ricardo NeriWhen computing a linear address and segmentation is used, we need to know the base address of the segment involved in the computation. In most of the cases, the segment base address will be zero as in USER_DS/USER32_DS. However, it may be possible that a user space program defines its own segments via a local descriptor table. In such a case, the segment base address may not be zero. Thus, the segment base address is needed to calculate correctly the linear address.
If running in protected mode, the segment selector to be used when computing a linear address is determined by either any of segment override prefixes in the instruction or inferred from the registers involved in the computation of the effective address; in that order. Also, there are cases when the segment override prefixes shall be ignored (i.e., code segments are always selected by the CS segment register; string instructions always use the ES segment register when using rDI register as operand). In long mode, segment registers are ignored, except for FS and GS. In these two cases, base addresses are obtained from the respective MSRs.
For clarity, this process can be split into four steps (and an equal number of functions): determine if segment prefixes overrides can be used; parse the segment override prefixes, and use them if found; if not found or cannot be used, use the default segment registers associated with the operand registers. Once the segment register to use has been identified, read its value to obtain the segment selector.
The method to obtain the segment selector depends on several factors. In 32-bit builds, segment selectors are saved into a pt_regs structure when switching to kernel mode. The same is also true for virtual-8086 mode. In 64-bit builds, segmentation is mostly ignored, except when running a program in 32-bit legacy mode. In this case, CS and SS can be obtained from pt_regs. DS, ES, FS and GS can be read directly from the respective segment registers.
In order to identify the segment registers, a new set of #defines is introduced. It also includes two special identifiers. One of them indicates when the default segment register associated with instruction operands shall be used. Another one indicates that the contents of the segment register shall be ignored; this identifier is used when in long mode.
--- arch/x86/include/asm/inat.h | 10 ++ arch/x86/lib/insn-eval.c | 340 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 350 insertions(+)
arch/x86/include/asm/inat.h +10 -0
@@ -97,6 +97,16 @@#define INAT_MAKE_GROUP (grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)#define INAT_MAKE_IMM (imm) (imm << INAT_IMM_OFFS) +/* Identifiers for segment registers */ +#define INAT_SEG_REG_IGNORE 0 +#define INAT_SEG_REG_DEFAULT 1 +#define INAT_SEG_REG_CS 2 +#define INAT_SEG_REG_SS 3 +#define INAT_SEG_REG_DS 4 +#define INAT_SEG_REG_ES 5 +#define INAT_SEG_REG_FS 6 +#define INAT_SEG_REG_GS 7 +/* Attribute search APIs */ extern insn_attr_t inat_get_opcode_attribute (insn_byte_t opcode);extern int inat_get_last_prefix_id (insn_byte_t last_pfx);
arch/x86/lib/insn-eval.c +340 -0
352 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add utility functions to get segment selector
x86/insn-eval: Add utility functions to get segment selector
Commit-ID: 32d0b95300db03c2b23b2ea2c94769a4a138e79d Gitweb: https://git.kernel.org/tip/32d0b95300db03c2b23b2ea2c94769a4a138e79d Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:40 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:11 +0100
x86/insn-eval: Add utility functions to get segment selector
When computing a linear address and segmentation is used, we need to know the base address of the segment involved in the computation. In most of the cases, the segment base address will be zero as in USER_DS/USER32_DS. However, it may be possible that a user space program defines its own segments via a local descriptor table. In such a case, the segment base address may not be zero. Thus, the segment base address is needed to calculate correctly the linear address.
If running in protected mode, the segment selector to be used when computing a linear address is determined by either any of segment override prefixes in the instruction or inferred from the registers involved in the computation of the effective address; in that order. Also, there are cases when the segment override prefixes shall be ignored (i.e., code segments are always selected by the CS segment register; string instructions always use the ES segment register when using rDI register as operand). In long mode, segment registers are ignored, except for FS and GS. In these two cases, base addresses are obtained from the respective MSRs.
For clarity, this process can be split into four steps (and an equal number of functions): determine if segment prefixes overrides can be used; parse the segment override prefixes, and use them if found; if not found or cannot be used, use the default segment registers associated with the operand registers. Once the segment register to use has been identified, read its value to obtain the segment selector.
The method to obtain the segment selector depends on several factors. In 32-bit builds, segment selectors are saved into a pt_regs structure when switching to kernel mode. The same is also true for virtual-8086 mode. In 64-bit builds, segmentation is mostly ignored, except when running a program in 32-bit legacy mode. In this case, CS and SS can be obtained from pt_regs. DS, ES, FS and GS can be read directly from the respective segment registers.
In order to identify the segment registers, a new set of #defines is introduced. It also includes two special identifiers. One of them indicates when the default segment register associated with instruction operands shall be used. Another one indicates that the contents of the segment register shall be ignored; this identifier is used when in long mode.
--- arch/x86/include/asm/inat.h | 10 ++ arch/x86/lib/insn-eval.c | 340 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 350 insertions(+)
arch/x86/include/asm/inat.h +10 -0
@@ -97,6 +97,16 @@#define INAT_MAKE_GROUP (grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)#define INAT_MAKE_IMM (imm) (imm << INAT_IMM_OFFS) +/* Identifiers for segment registers */ +#define INAT_SEG_REG_IGNORE 0 +#define INAT_SEG_REG_DEFAULT 1 +#define INAT_SEG_REG_CS 2 +#define INAT_SEG_REG_SS 3 +#define INAT_SEG_REG_DS 4 +#define INAT_SEG_REG_ES 5 +#define INAT_SEG_REG_FS 6 +#define INAT_SEG_REG_GS 7 +/* Attribute search APIs */ extern insn_attr_t inat_get_opcode_attribute (insn_byte_t opcode);extern int inat_get_last_prefix_id (insn_byte_t last_pfx);
arch/x86/lib/insn-eval.c +340 -0
352 lines not on this page · raw message
Arnd Bergmann
maintainerRe: [PATCH v10 13/18] x86/insn-eval: Add utility functions to get segment selector
In reply to Ricardo NeriQuoted 8 lines · Ricardo Neri
arch/x86/include/asm/inat.h +0
-0@@ -97,6 +97,16 @@#define INAT_MAKE_GROUP (grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)#define INAT_MAKE_IMM (imm) (imm << INAT_IMM_OFFS)
arch/x86/include/asm/inat.h line 109
Quoted 10 lines
arch/x86/include/asm/inat.h +10
-0+/* Identifiers for segment registers */ +#define INAT_SEG_REG_IGNORE 0 +#define INAT_SEG_REG_DEFAULT 1 +#define INAT_SEG_REG_CS 2 +#define INAT_SEG_REG_SS 3 +#define INAT_SEG_REG_DS 4 +#define INAT_SEG_REG_ES 5 +#define INAT_SEG_REG_FS 6 +#define INAT_SEG_REG_GS 7 +
linux-next still reports a warning because of this change:
Warning: synced file at 'tools/objtool/arch/x86/include/asm/inat.h' differs from latest kernel version at 'arch/x86/include/asm/inat.h'
Should the same change be applied to the objtool file in the tip:x86/mpx branch?
Arnd
Re: [PATCH v10 13/18] x86/insn-eval: Add utility functions to get segment selector
Quoted 24 lines · Arnd Bergmann
Quoted 8 lines · Ricardo Neri
arch/x86/include/asm/inat.h +0
-0@@ -97,6 +97,16 @@#define INAT_MAKE_GROUP (grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)#define INAT_MAKE_IMM (imm) (imm << INAT_IMM_OFFS)arch/x86/include/asm/inat.h line 109
Quoted 10 lines
arch/x86/include/asm/inat.h +10
-0+/* Identifiers for segment registers */ +#define INAT_SEG_REG_IGNORE 0 +#define INAT_SEG_REG_DEFAULT 1 +#define INAT_SEG_REG_CS 2 +#define INAT_SEG_REG_SS 3 +#define INAT_SEG_REG_DS 4 +#define INAT_SEG_REG_ES 5 +#define INAT_SEG_REG_FS 6 +#define INAT_SEG_REG_GS 7 +linux-next still reports a warning because of this change:
Quoted 5 lines
Warning: synced file at '
tools/objtool/arch/x86/include/asm/inat.h' differs from latest kernel version at 'arch/x86/include/asm/inat.h'Should the same change be applied to the objtool file in the tip:x86/mpx branch?
No, the best flow is that the headers will be synced once both components are upstream.
This is an integration artifact - and we don't want to 'pre sync', because what happens if Linus has to reject the arch/x86/include/asm/inat.h changes for whatever reason, and the tooling changes go upstream first?
Thanks,
Ingo
PATCH v10 14/18 x86/insn-eval: Add utility function to get segment descriptor
In reply to Ricardo NeriThe segment descriptor contains information that is relevant to how linear addresses need to be computed. It contains the default size of addresses as well as the base address of the segment. Thus, given a segment selector, we ought to look at segment descriptor to correctly calculate the linear address.
In protected mode, the segment selector might indicate a segment descriptor from either the global descriptor table or a local descriptor table. Both cases are considered in this function.
This function is a prerequisite for functions in subsequent commits that will obtain the aforementioned attributes of the segment descriptor.
--- arch/x86/lib/insn-eval.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
arch/x86/lib/insn-eval.c +57 -0
@@ -6,9 +6,13 @@#include <linux/kernel.h> #include <linux/string.h> #include <linux/ratelimit.h> +#include <linux/mmu_context.h> +#include <asm/desc_defs.h> +#include <asm/desc.h> #include <asm/inat.h> #include <asm/insn.h> #include <asm/insn-eval.h> +#include <asm/ldt.h> #include <asm/vm86.h> #undef pr_fmt
@@ -469,6 +473,59 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs, }/** +* get_desc() - Obtain pointer to a segment descriptor +* @sel: Segment selector +* +* Given a segment selector, obtain a pointer to the segment descriptor. +* Both global and local descriptor tables are supported. +* +* Returns: +* +* Pointer to segment descriptor on success. +* +* NULL on error. +*/ +static struct desc_struct *get_desc (unsigned short sel) +{ +struct desc_ptr gdt_desc = {0 ,0 }; +unsigned long desc_base; + +#ifdef CONFIG_MODIFY_LDT_SYSCALL +if ((sel &SEGMENT_TI_MASK ) ==SEGMENT_LDT ) { +struct desc_struct *desc =NULL ; +struct ldt_struct *ldt; + +/* Bits [15:3] contain the index of the desired entry. */ + sel >>=3 ; + +mutex_lock (¤t->active_mm->context.lock); + ldt = current->active_mm->context.ldt; +if (ldt && sel < ldt->nr_entries) + desc = &ldt->entries[sel]; + +mutex_unlock (¤t->active_mm->context.lock); + +return desc; + } +#endif +native_store_gdt (&gdt_desc); + +/* +* Segment descriptors have a size of 8 bytes. Thus, the index is +* multiplied by 8 to obtain the memory offset of the desired descriptor +* from the base of the GDT. As bits [15:3] of the segment selector +* contain the index, it can be regarded as multiplied by 8 already. +* All that remains is to clear bits [2:0]. +*/ + desc_base = sel & ~(SEGMENT_RPL_MASK |SEGMENT_TI_MASK ); + +if (desc_base > gdt_desc.size) +return NULL ; + +return (struct desc_struct *)(gdt_desc.address + desc_base); +} + +/** * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte * @insn: Instruction containing the ModRM byte * @regs: Register values as seen when entering kernel mode
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
x86/insn-eval: Add utility function to get segment descriptor
Commit-ID: 670f928ba09b06712da34a3c44be6c8fa561fb19 Gitweb: https://git.kernel.org/tip/670f928ba09b06712da34a3c44be6c8fa561fb19 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:41 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:12 +0100
x86/insn-eval: Add utility function to get segment descriptor
The segment descriptor contains information that is relevant to how linear addresses need to be computed. It contains the default size of addresses as well as the base address of the segment. Thus, given a segment selector, we ought to look at segment descriptor to correctly calculate the linear address.
In protected mode, the segment selector might indicate a segment descriptor from either the global descriptor table or a local descriptor table. Both cases are considered in this function.
This function is a prerequisite for functions in subsequent commits that will obtain the aforementioned attributes of the segment descriptor.
--- arch/x86/lib/insn-eval.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
arch/x86/lib/insn-eval.c +57 -0
@@ -6,9 +6,13 @@#include <linux/kernel.h> #include <linux/string.h> #include <linux/ratelimit.h> +#include <linux/mmu_context.h> +#include <asm/desc_defs.h> +#include <asm/desc.h> #include <asm/inat.h> #include <asm/insn.h> #include <asm/insn-eval.h> +#include <asm/ldt.h> #include <asm/vm86.h> #undef pr_fmt
@@ -469,6 +473,59 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs, }/** +* get_desc() - Obtain pointer to a segment descriptor +* @sel: Segment selector +* +* Given a segment selector, obtain a pointer to the segment descriptor. +* Both global and local descriptor tables are supported. +* +* Returns: +* +* Pointer to segment descriptor on success. +* +* NULL on error. +*/ +static struct desc_struct *get_desc (unsigned short sel) +{ +struct desc_ptr gdt_desc = {0 ,0 }; +unsigned long desc_base; + +#ifdef CONFIG_MODIFY_LDT_SYSCALL +if ((sel &SEGMENT_TI_MASK ) ==SEGMENT_LDT ) { +struct desc_struct *desc =NULL ; +struct ldt_struct *ldt; + +/* Bits [15:3] contain the index of the desired entry. */ + sel >>=3 ; + +mutex_lock (¤t->active_mm->context.lock); + ldt = current->active_mm->context.ldt; +if (ldt && sel < ldt->nr_entries) + desc = &ldt->entries[sel]; + +mutex_unlock (¤t->active_mm->context.lock); + +return desc; + } +#endif +native_store_gdt (&gdt_desc); + +/* +* Segment descriptors have a size of 8 bytes. Thus, the index is +* multiplied by 8 to obtain the memory offset of the desired descriptor +* from the base of the GDT. As bits [15:3] of the segment selector +* contain the index, it can be regarded as multiplied by 8 already. +* All that remains is to clear bits [2:0]. +*/ + desc_base = sel & ~(SEGMENT_RPL_MASK |SEGMENT_TI_MASK ); + +if (desc_base > gdt_desc.size) +return NULL ; + +return (struct desc_struct *)(gdt_desc.address + desc_base); +} + +/** * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte * @insn: Instruction containing the ModRM byte * @regs: Register values as seen when entering kernel mode
Peter Zijlstra
maintainerRe: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 13 lines · tip-bot for Ricardo Neri
(quoted diff) +13
-0+static struct desc_struct *get_desc(unsigned short sel) +{ + struct desc_ptr gdt_desc = {0, 0}; + unsigned long desc_base; + +#ifdef CONFIG_MODIFY_LDT_SYSCALL + if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) { + struct desc_struct *desc = NULL; + struct ldt_struct *ldt; + + /* Bits [15:3] contain the index of the desired entry. */ + sel >>= 3; +
Review
Quoted 10 lines
(quoted diff) +10
-0+ mutex_lock(¤t->active_mm->context.lock); + ldt = current->active_mm->context.ldt; + if (ldt && sel < ldt->nr_entries) + desc = &ldt->entries[sel]; + + mutex_unlock(¤t->active_mm->context.lock); + + return desc; + } +#endif
This is broken right? You unlock and then return @desc, which afaict can at that point get freed by free_ldt_struct().
Something like the below ought to cure; although its not entirely pretty either.
---
arch/x86/lib/insn-eval.c +46 -26
@@ -572,6 +572,11 @@ static struct desc_struct *get_desc(unsigned short sel)struct desc_ptr gdt_desc = {0 ,0 };unsigned long desc_base; +/* +* Relies on IRQs being disabled to serialize against the LDT. +*/ +lockdep_assert_irqs_disabled (); +#ifdef CONFIG_MODIFY_LDT_SYSCALL if ((sel &SEGMENT_TI_MASK ) ==SEGMENT_LDT ) {struct desc_struct *desc =NULL ;
@@ -580,13 +585,10 @@ static struct desc_struct *get_desc(unsigned short sel)/* Bits [15:3] contain the index of the desired entry. */ sel >>=3 ; -mutex_lock (¤t->active_mm->context.lock); ldt = current->active_mm->context.ldt;if (ldt && sel < ldt->nr_entries) desc = &ldt->entries_va[sel]; -mutex_unlock (¤t->active_mm->context.lock); - return desc; }#endif
@@ -626,6 +628,7 @@ static struct desc_struct *get_desc(unsigned short sel)*/ unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx) { +unsigned long base, flags;struct desc_struct *desc;short sel;
@@ -664,11 +667,15 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)if (!sel)return -1L ; + base =-1 ; + +local_irq_save (flags); desc =get_desc (sel); -if (!desc) -return -1L ; +if (desc) + base =get_desc_base (desc); +local_irq_restore (flags); -return get_desc_base (desc); +return base; }/**
@@ -690,8 +697,8 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)*/ static unsigned long get_seg_limit (struct pt_regs *regs,int seg_reg_idx) { +unsigned long flags, limit =0 ;struct desc_struct *desc; -unsigned long limit;short sel; sel =get_segment_selector (regs, seg_reg_idx);
@@ -704,19 +711,20 @@ static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)if (!sel)return 0 ; +local_irq_save (flags); desc =get_desc (sel); -if (!desc) -return 0 ; - -/* -* If the granularity bit is set, the limit is given in multiples -* of 4096. This also means that the 12 least significant bits are -* not tested when checking the segment limits. In practice, -* this means that the segment ends in (limit << 12) + 0xfff. -*/ - limit =get_desc_limit (desc); -if (desc->g) - limit = (limit <<12 ) +0xfff ; +if (desc) { +/* +* If the granularity bit is set, the limit is given in multiples +* of 4096. This also means that the 12 least significant bits are +* not tested when checking the segment limits. In practice, +* this means that the segment ends in (limit << 12) + 0xfff. +*/ + limit =get_desc_limit (desc); +if (desc->g) + limit = (limit <<12 ) +0xfff ; + } +local_irq_restore (flags);return limit; }
@@ -740,19 +748,23 @@ static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)int insn_get_code_seg_params (struct pt_regs *regs) {struct desc_struct *desc; +unsigned long flags; +int ret = -EINVAL ;short sel; -if (v8086_mode (regs)) +if (v8086_mode (regs)) {/* Address and operand size are both 16-bit. */ return INSN_CODE_SEG_PARAMS (2 ,2 ); + } sel =get_segment_selector (regs,INAT_SEG_REG_CS );if (sel <0 )return sel; +local_irq_save (flags); desc =get_desc (sel);if (!desc) -return -EINVAL ; + goto out;/* * The most significant byte of the Type field of the segment descriptor
@@ -760,29 +772,37 @@ int insn_get_code_seg_params(struct pt_regs *regs)* segment, return error. */ if (!(desc->type &BIT (3 ))) -return -EINVAL ; + goto out;switch ((desc->l <<1 ) | desc->d) {case 0 :/* * Legacy mode. CS.L=0, CS.D=0. Address and operand size are * both 16-bit. */ -return INSN_CODE_SEG_PARAMS (2 ,2 ); + ret =INSN_CODE_SEG_PARAMS (2 ,2 ); +break ;case 1 :/* * Legacy mode. CS.L=0, CS.D=1. Address and operand size are * both 32-bit. */ -return INSN_CODE_SEG_PARAMS (4 ,4 ); + ret =INSN_CODE_SEG_PARAMS (4 ,4 ); +break ;case 2 :/* * IA-32e 64-bit mode. CS.L=1, CS.D=0. Address size is 64-bit; * operand size is 32-bit. */ -return INSN_CODE_SEG_PARAMS (4 ,8 ); + ret =INSN_CODE_SEG_PARAMS (4 ,8 ); +break ; +case 3 :/* Invalid setting. CS.L=1, CS.D=1 */ /* fall through */ default : -return -EINVAL ; +break ; } +out: +local_irq_restore (flags); + +return ret; }/**
Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 5 lines · Peter Zijlstra
This is broken right? You unlock and then return @desc, which afaict can at that point get freed by
free_ldt_struct().Something like the below ought to cure; although its not entirely pretty either.
Right.
Or, instead of introducing all the locking, we could also not do anything because all that code runs inside fixup_umip_exception() so the desc will be valid there.
But, if other code is going to use those functions - and I believe that's the idea - otherwise they wouldn't be in arch/x86/lib/ - we should convert all those functions to return directly the desc field which is requested by the respective caller.
I.e., get_desc() will be called by a wrapper which returns desc base or desc limit or whatever...
In the case where desc has been freed, it should return error, of course.
How doed that sound?
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Peter Zijlstra
maintainerRe: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 4 lines · Borislav Petkov
Quoted 3 lines · Peter Zijlstra
This is broken right? You unlock and then return @desc, which afaict can at that point get freed by
free_ldt_struct().
Quoted 8 lines
Quoted 2 lines
Something like the below ought to cure; although its not entirely pretty either.
Right.
Or, instead of introducing all the locking, we could also not do anything because all that code runs inside
fixup_umip_exception()so the desc will be valid there.
Sorry what? So either this code is broken because it has IRQs enabled, or its broken because its trying to acquire a mutex with IRQs disabled. Which is it?
Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 3 lines · Peter Zijlstra
Sorry what? So either this code is broken because it has IRQs enabled, or its broken because its trying to acquire a mutex with IRQs disabled. Which is it?
Well, lemme try to sum up what Peter, Thomas and I discussed on IRC:
The problem is that there's no guarantee userspace won't change the LDT from under us while the UMIP code runs in the insn decoder.
So, we need a way to be able to query the desc fields the insn decoder needs *and* when the LDT changes through the syscall, to detect that case and handle it gracefully in the decoder.
So Thomas' idea is to keep a mm->context.ldt_seq sequence number which gets incremented (and wraps around) everytime a LDT changes.
That sequence number, i.e., cookie, gets handed down into the decoder and it uses it during desc lookup. If the sequence number changes, the decoder and the UMIP code must abort the emulation.
The lookup code needs to do that with IRQs disabled, of course, to protect itself from IPIs which could change the LDT.
I *think* this is the gist of what we talked about, tglx, please correct me if I missed something.
So, Ricardo, please take a look at fixing that as otherwise the UMIP code would choke and possibly rely on wrong data. If there are any questions, don't hesitate to ask.
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 9 lines · Borislav Petkov
Quoted 3 lines · Peter Zijlstra
Sorry what? So either this code is broken because it has IRQs enabled, or its broken because its trying to acquire a mutex with IRQs disabled. Which is it?
Well, lemme try to sum up what Peter, Thomas and I discussed on IRC:
The problem is that there's no guarantee userspace won't change the LDT from under us while the UMIP code runs in the insn decoder.
Yes, I see the problem now.
Quoted 11 lines
So, we need a way to be able to query the desc fields the insn decoder needs *and* when the LDT changes through the syscall, to detect that case and handle it gracefully in the decoder.
So Thomas' idea is to keep a
mm->context.ldt_seqsequence number which gets incremented (and wraps around) everytime a LDT changes.That sequence number, i.e., cookie, gets handed down into the decoder and it uses it during desc lookup. If the sequence number changes, the decoder and the UMIP code must abort the emulation.
In UMIP emulation we can potentially access the LDT twice. Once when determining the base address of the code segment and again when determining the base address and limit of the segment in which the result of the emulation is written. I guess that mm->context.ldt_seq needs to not change not only while decoding a particular linear address but across these two linear address decodings.
Quoted 10 lines
The lookup code needs to do that with IRQs disabled, of course, to protect itself from IPIs which could change the LDT.
I *think* this is the gist of what we talked about, tglx, please correct me if I missed something.
So, Ricardo, please take a look at fixing that as otherwise the UMIP code would choke and possibly rely on wrong data. If there are any questions, don't hesitate to ask.
Sure, I will look into implementing this idea and post patches for it.
Thanks and BR,
Ricardo
Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 6 lines · Ricardo Neri
In UMIP emulation we can potentially access the LDT twice. Once when determining the base address of the code segment and again when determining the base address and limit of the segment in which the result of the emulation is written. I guess that
mm->context.ldt_seqneeds to not change not only while decoding a particular linear address but across these two linear address decodings.
Yap, stuff which needs to see an *unchanged* LDT should use the cookie to verify that and the LDT code should change the cookie when the LDT is modified.
Quoted 1 line
Sure, I will look into implementing this idea and post patches for it.
Thanks!
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
In reply to Borislav PetkovQuoted 3 lines · Borislav Petkov
But, if other code is going to use those functions - and I believe that's the idea - otherwise they wouldn't be in
arch/x86/lib/
At the moment MPX and UMIP are using the insn-eval decoder to determine linear addresses.
Thanks and BR,
Ricardo
Re: [tip:x86/mpx] x86/insn-eval: Add utility function to get segment descriptor
Quoted 2 lines · Ricardo Neri
At the moment MPX and UMIP are using the insn-eval decoder to determine linear addresses.
If we're keeping a whole instruction decoder in the kernel, it better be designed generically enough and usable (and used) by everything that needs it.
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- PATCH v10 15/18 x86/insn-eval: Add utility functions to get segment descriptor base address and limit
In reply to Ricardo NeriWith segmentation, the base address of the segment is needed to compute a linear address. This base address is obtained from the applicable segment descriptor. Such segment descriptor is referenced from a segment selector. These new functions obtain the segment base and limit of the segment selector indicated by segment register index given as argument. This index is any of the INAT_SEG_REG_* family of #define's.
The logic to obtain the segment selector is wrapped in the function get_segment_selector() with the inputs described above. Once the selector is known, the base address is determined. In protected mode, the selector is used to obtain the segment descriptor and then its base address. In long mode, the segment base address is zero except when FS or GS are used. In virtual-8086 mode, the base address is computed as the value of the segment selector shifted 4 positions to the left.
In protected mode, segment limits are enforced. Thus, a function to determine the limit of the segment is added. Segment limits are not enforced in long or virtual-8086. For the latter, addresses are limited to 20 bits; address size will be handled when computing the linear address.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -13,5 +13,6 @@void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs); +unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +114 -0
120 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add utility functions to get segment descriptor base address and limit
x86/insn-eval: Add utility functions to get segment descriptor base address and limit
Commit-ID: bd5a410a5de3a6893eaacc749e706b85506dc908 Gitweb: https://git.kernel.org/tip/bd5a410a5de3a6893eaacc749e706b85506dc908 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:42 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:12 +0100
x86/insn-eval: Add utility functions to get segment descriptor base address and limit
With segmentation, the base address of the segment is needed to compute a linear address. This base address is obtained from the applicable segment descriptor. Such segment descriptor is referenced from a segment selector. These new functions obtain the segment base and limit of the segment selector indicated by segment register index given as argument. This index is any of the INAT_SEG_REG_* family of #define's.
The logic to obtain the segment selector is wrapped in the function get_segment_selector() with the inputs described above. Once the selector is known, the base address is determined. In protected mode, the selector is used to obtain the segment descriptor and then its base address. In long mode, the segment base address is zero except when FS or GS are used. In virtual-8086 mode, the base address is computed as the value of the segment selector shifted 4 positions to the left.
In protected mode, segment limits are enforced. Thus, a function to determine the limit of the segment is added. Segment limits are not enforced in long or virtual-8086. For the latter, addresses are limited to 20 bits; address size will be handled when computing the linear address.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -13,5 +13,6 @@void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs); +unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +114 -0
120 lines not on this page · raw message
PATCH v10 16/18 x86/insn-eval: Add function to get default params of code segment
In reply to Ricardo NeriObtain the default values of the address and operand sizes as specified in the D and L bits of the the segment descriptor selected by the register CS. The function can be used for both protected and long modes. For virtual-8086 mode, the default address and operand sizes are always 2 bytes.
The returned parameters are encoded in a signed 8-bit data type. Auxiliar macros are provided to encode and decode such values.
--- arch/x86/include/asm/insn-eval.h | 5 ++++ arch/x86/lib/insn-eval.c | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+)
arch/x86/include/asm/insn-eval.h +5 -0
@@ -11,8 +11,13 @@#include <linux/err.h> #include <asm/ptrace.h> +#define INSN_CODE_SEG_ADDR_SZ (params) ((params >> 4) & 0xf) +#define INSN_CODE_SEG_OPND_SZ (params) (params & 0xf) +#define INSN_CODE_SEG_PARAMS (oper_sz, addr_sz) (oper_sz | (addr_sz << 4)) +void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx); +char insn_get_code_seg_params (struct pt_regs *regs);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +64 -0
70 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Add function to get default params of code segment
x86/insn-eval: Add function to get default params of code segment
Commit-ID: 4efea85fb56fa1691b79af1eea4c1425660cf4e3 Gitweb: https://git.kernel.org/tip/4efea85fb56fa1691b79af1eea4c1425660cf4e3 Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:43 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:12 +0100
x86/insn-eval: Add function to get default params of code segment
Obtain the default values of the address and operand sizes as specified in the D and L bits of the the segment descriptor selected by the register CS. The function can be used for both protected and long modes. For virtual-8086 mode, the default address and operand sizes are always 2 bytes.
The returned parameters are encoded in a signed 8-bit data type. Auxiliar macros are provided to encode and decode such values.
--- arch/x86/include/asm/insn-eval.h | 5 ++++ arch/x86/lib/insn-eval.c | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+)
arch/x86/include/asm/insn-eval.h +5 -0
@@ -11,8 +11,13 @@#include <linux/err.h> #include <asm/ptrace.h> +#define INSN_CODE_SEG_ADDR_SZ (params) ((params >> 4) & 0xf) +#define INSN_CODE_SEG_OPND_SZ (params) (params & 0xf) +#define INSN_CODE_SEG_PARAMS (oper_sz, addr_sz) (oper_sz | (addr_sz << 4)) +void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx); +char insn_get_code_seg_params (struct pt_regs *regs);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +64 -0
70 lines not on this page · raw message
PATCH v10 17/18 x86/insn-eval: Indicate a 32-bit displacement if ModRM.mod is 0 and ModRM.rm is 101b
In reply to Ricardo NeriSection 2.2.1.3 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that when ModRM.mod is zero and ModRM.rm is 101b, a 32-bit displacement follows the ModRM byte. This means that none of the registers are used in the computation of the effective address. A return value of -EDOM indicates callers that they should not use the value of registers when computing the effective address for the instruction.
In long mode, the effective address is given by the 32-bit displacement plus the location of the next instruction. In protected mode, only the displacement is used.
The instruction decoder takes care of obtaining the displacement.
--- arch/x86/lib/insn-eval.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +22 -3
@@ -427,6 +427,14 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,switch (type) {case REG_TYPE_RM : regno =X86_MODRM_RM (insn->modrm.value); + +/* +* ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement +* follows the ModRM byte. +*/ +if (!X86_MODRM_MOD (insn->modrm.value) && regno ==5 ) +return -EDOM ; +if (X86_REX_B (insn->rex_prefix.value)) regno +=8 ;break ;
@@ -770,10 +778,21 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM ); -if (addr_offset <0 ) +/* +* -EDOM means that we must ignore the address_offset. +* In such a case, in 64-bit mode the effective address +* relative to the RIP of the following instruction. +*/ +if (addr_offset == -EDOM ) { +if (user_64bit_mode (regs)) + eff_addr = (long )regs->ip + insn->length; +else + eff_addr =0 ; + }else if (addr_offset <0 ) { goto out; - - eff_addr =regs_get_register (regs, addr_offset); + }else { + eff_addr =regs_get_register (regs, addr_offset); + } } eff_addr += insn->displacement.value;
Signature · 1 line
2.7.4
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Indicate a 32-bit displacement if ModRM.mod is 0 and ModRM.rm is 101b
x86/insn-eval: Indicate a 32-bit displacement if ModRM.mod is 0 and ModRM.rm is 101b
Commit-ID: e526a302e425ab11111efc5f59e52449bbcc768e Gitweb: https://git.kernel.org/tip/e526a302e425ab11111efc5f59e52449bbcc768e Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:44 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:13 +0100
x86/insn-eval: Indicate a 32-bit displacement if ModRM.mod is 0 and ModRM.rm is 101b
Section 2.2.1.3 of the Intel 64 and IA-32 Architectures Software Developer's Manual volume 2A states that when ModRM.mod is zero and ModRM.rm is 101b, a 32-bit displacement follows the ModRM byte. This means that none of the registers are used in the computation of the effective address. A return value of -EDOM indicates callers that they should not use the value of registers when computing the effective address for the instruction.
In long mode, the effective address is given by the 32-bit displacement plus the location of the next instruction. In protected mode, only the displacement is used.
The instruction decoder takes care of obtaining the displacement.
--- arch/x86/lib/insn-eval.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +22 -3
@@ -427,6 +427,14 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs,switch (type) {case REG_TYPE_RM : regno =X86_MODRM_RM (insn->modrm.value); + +/* +* ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement +* follows the ModRM byte. +*/ +if (!X86_MODRM_MOD (insn->modrm.value) && regno ==5 ) +return -EDOM ; +if (X86_REX_B (insn->rex_prefix.value)) regno +=8 ;break ;
@@ -770,10 +778,21 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM ); -if (addr_offset <0 ) +/* +* -EDOM means that we must ignore the address_offset. +* In such a case, in 64-bit mode the effective address +* relative to the RIP of the following instruction. +*/ +if (addr_offset == -EDOM ) { +if (user_64bit_mode (regs)) + eff_addr = (long )regs->ip + insn->length; +else + eff_addr =0 ; + }else if (addr_offset <0 ) { goto out; - - eff_addr =regs_get_register (regs, addr_offset); + }else { + eff_addr =regs_get_register (regs, addr_offset); + } } eff_addr += insn->displacement.value;
PATCH v10 18/18 x86/insn-eval: Incorporate segment base in linear address computation
In reply to Ricardo Neriinsn_get_addr_ref() returns the effective address as defined by the section 3.7.5.1 Vol 1 of the Intel 64 and IA-32 Architectures Software Developer's Manual. In order to compute the linear address, we must add to the effective address the segment base address as set in the segment descriptor. The segment descriptor to use depends on the register used as operand and segment override prefixes, if any.
In most cases, the segment base address will be 0 if the USER_DS/USER32_DS segment is used or if segmentation is not used. However, the base address is not necessarily zero if a user programs defines its own segments. This is possible by using a local descriptor table.
Since the effective address is a signed quantity, the unsigned segment base address is saved in a separate variable and added to the final, unsigned, effective address.
--- arch/x86/lib/insn-eval.c | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +52 -3
@@ -728,6 +728,43 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)return get_reg_offset (insn, regs,REG_TYPE_RM ); } +/** +* get_seg_base_addr() - obtain base address of a segment +* @insn: Instruction. Must be valid. +* @regs: Register values as seen when entering kernel mode +* @regoff: Operand offset, in pt_regs, used to resolve segment descriptor +* @base: Obtained segment base +* +* Obtain the base address of the segment associated with the operand @regoff +* and, if any or allowed, override prefixes in @insn. This function is +* different from insn_get_seg_base() as the latter does not resolve the segment +* associated with the instruction operand. +* +* Returns: +* +* 0 on success. @base will contain the base address of the resolved segment. +* +* -EINVAL on error. +*/ +static int get_seg_base_addr (struct insn *insn,struct pt_regs *regs, +int regoff,unsigned long *base) +{ +int seg_reg_idx; + +if (!base) +return -EINVAL ; + + seg_reg_idx =resolve_seg_reg (insn, regs, regoff); +if (seg_reg_idx <0 ) +return seg_reg_idx; + + *base =insn_get_seg_base (regs, seg_reg_idx); +if (*base ==-1L ) +return -EINVAL ; + +return 0 ; +} +/* * return the address being referenced be instruction * for rm=3 returning the content of the rm reg
@@ -735,8 +772,8 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)*/ void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -int addr_offset, base_offset, indx_offset; -unsigned long linear_addr =-1L ; +int addr_offset, base_offset, indx_offset, ret; +unsigned long linear_addr =-1L , seg_base;long eff_addr, base, indx;insn_byte_t sib;
@@ -750,6 +787,7 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) goto out; eff_addr =regs_get_register (regs, addr_offset); + }else {if (insn->sib.nbytes) {/*
@@ -776,6 +814,13 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) indx =regs_get_register (regs, indx_offset); eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); + +/* +* The base determines the segment used to compute +* the linear address. +*/ + addr_offset = base_offset; + }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );/*
@@ -798,7 +843,11 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr += insn->displacement.value; } - linear_addr = (unsigned long )eff_addr; + ret =get_seg_base_addr (insn, regs, addr_offset, &seg_base); +if (ret) + goto out; + + linear_addr = (unsigned long )eff_addr + seg_base; out:return (void __user *)linear_addr;
Signature · 1 line
2.7.4
Re: [PATCH v10 18/18] x86/insn-eval: Incorporate segment base in linear address computation
Quoted 32 lines · Ricardo Neri
insn_get_addr_ref()returns the effective address as defined by the section 3.7.5.1 Vol 1 of the Intel 64 and IA-32 Architectures Software Developer's Manual. In order to compute the linear address, we must add to the effective address the segment base address as set in the segment descriptor. The segment descriptor to use depends on the register used as operand and segment override prefixes, if any.In most cases, the segment base address will be 0 if the USER_DS/USER32_DS segment is used or if segmentation is not used. However, the base address is not necessarily zero if a user programs defines its own segments. This is possible by using a local descriptor table.
Since the effective address is a signed quantity, the unsigned segment base address is saved in a separate variable and added to the final, unsigned, effective address.
Cc Dave Hansen <[email protected]>Cc Adam Buchbinder <[email protected]>Cc Colin Ian King <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Qiaowei Ren <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Adrian Hunter <[email protected]>Cc Kees Cook <[email protected]>Cc Thomas Garnier <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Ravi V. Shankar <[email protected]>Signed-off-by Ricardo Neri <[email protected]>
Quoted 3 lines
--- arch/x86/lib/insn-eval.c | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-)
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- Re: [PATCH v10 18/18] x86/insn-eval: Incorporate segment base in linear address computation
Quoted 37 lines · Borislav Petkov
Quoted 32 lines · Ricardo Neri
insn_get_addr_ref()returns the effective address as defined by the section 3.7.5.1 Vol 1 of the Intel 64 and IA-32 Architectures Software Developer's Manual. In order to compute the linear address, we must add to the effective address the segment base address as set in the segment descriptor. The segment descriptor to use depends on the register used as operand and segment override prefixes, if any.In most cases, the segment base address will be 0 if the USER_DS/USER32_DS segment is used or if segmentation is not used. However, the base address is not necessarily zero if a user programs defines its own segments. This is possible by using a local descriptor table.
Since the effective address is a signed quantity, the unsigned segment base address is saved in a separate variable and added to the final, unsigned, effective address.
Cc Dave Hansen <[email protected]>Cc Adam Buchbinder <[email protected]>Cc Colin Ian King <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Qiaowei Ren <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Adrian Hunter <[email protected]>Cc Kees Cook <[email protected]>Cc Thomas Garnier <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Ravi V. Shankar <[email protected]>Signed-off-by Ricardo Neri <[email protected]>Quoted 3 lines
--- arch/x86/lib/insn-eval.c | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-)
Quoted 1 line
Reviewed-by Borislav Petkov <[email protected]>
Thank you Borislav! This should complete tne review of this series. As proposed earier [1], I guess that, if the tip maintainers are OK, this series can be merged in the tip tree?
BR,
Ricardo
tip-bot for Ricardo Neri
merge bot[tip:x86/mpx] x86/insn-eval: Incorporate segment base in linear address computation
In reply to Ricardo Nerix86/insn-eval: Incorporate segment base in linear address computation
Commit-ID: 108904442850c2884679f81121df3ef42d88cb9c Gitweb: https://git.kernel.org/tip/108904442850c2884679f81121df3ef42d88cb9c Author: Ricardo Neri <[email protected]> AuthorDate: Fri, 27 Oct 2017 13:25:45 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 1 Nov 2017 21:50:13 +0100
x86/insn-eval: Incorporate segment base in linear address computation
insn_get_addr_ref() returns the effective address as defined by the section 3.7.5.1 Vol 1 of the Intel 64 and IA-32 Architectures Software Developer's Manual. In order to compute the linear address, we must add to the effective address the segment base address as set in the segment descriptor. The segment descriptor to use depends on the register used as operand and segment override prefixes, if any.
In most cases, the segment base address will be 0 if the USER_DS/USER32_DS segment is used or if segmentation is not used. However, the base address is not necessarily zero if a user programs defines its own segments. This is possible by using a local descriptor table.
Since the effective address is a signed quantity, the unsigned segment base address is saved in a separate variable and added to the final, unsigned, effective address.
--- arch/x86/lib/insn-eval.c | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-)
arch/x86/lib/insn-eval.c +52 -3
@@ -728,6 +728,43 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)return get_reg_offset (insn, regs,REG_TYPE_RM ); } +/** +* get_seg_base_addr() - obtain base address of a segment +* @insn: Instruction. Must be valid. +* @regs: Register values as seen when entering kernel mode +* @regoff: Operand offset, in pt_regs, used to resolve segment descriptor +* @base: Obtained segment base +* +* Obtain the base address of the segment associated with the operand @regoff +* and, if any or allowed, override prefixes in @insn. This function is +* different from insn_get_seg_base() as the latter does not resolve the segment +* associated with the instruction operand. +* +* Returns: +* +* 0 on success. @base will contain the base address of the resolved segment. +* +* -EINVAL on error. +*/ +static int get_seg_base_addr (struct insn *insn,struct pt_regs *regs, +int regoff,unsigned long *base) +{ +int seg_reg_idx; + +if (!base) +return -EINVAL ; + + seg_reg_idx =resolve_seg_reg (insn, regs, regoff); +if (seg_reg_idx <0 ) +return seg_reg_idx; + + *base =insn_get_seg_base (regs, seg_reg_idx); +if (*base ==-1L ) +return -EINVAL ; + +return 0 ; +} +/* * return the address being referenced be instruction * for rm=3 returning the content of the rm reg
@@ -735,8 +772,8 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)*/ void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs) { -int addr_offset, base_offset, indx_offset; -unsigned long linear_addr =-1L ; +int addr_offset, base_offset, indx_offset, ret; +unsigned long linear_addr =-1L , seg_base;long eff_addr, base, indx;insn_byte_t sib;
@@ -750,6 +787,7 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) goto out; eff_addr =regs_get_register (regs, addr_offset); + }else {if (insn->sib.nbytes) {/*
@@ -776,6 +814,13 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) indx =regs_get_register (regs, indx_offset); eff_addr = base + indx * (1 <<X86_SIB_SCALE (sib)); + +/* +* The base determines the segment used to compute +* the linear address. +*/ + addr_offset = base_offset; + }else { addr_offset =get_reg_offset (insn, regs,REG_TYPE_RM );/*
@@ -798,7 +843,11 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs) eff_addr += insn->displacement.value; } - linear_addr = (unsigned long )eff_addr; + ret =get_seg_base_addr (insn, regs, addr_offset, &seg_base); +if (ret) + goto out; + + linear_addr = (unsigned long )eff_addr + seg_base; out:return (void __user *)linear_addr;