x86/umip: Factor out instruction fetch
authorJoerg Roedel <jroedel@suse.de>
Mon, 7 Sep 2020 13:15:09 +0000 (15:15 +0200)
committerBorislav Petkov <bp@suse.de>
Mon, 7 Sep 2020 17:45:24 +0000 (19:45 +0200)
Factor out the code to fetch the instruction from user-space to a helper
function.

No functional changes.

Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20200907131613.12703-9-joro@8bytes.org
arch/x86/include/asm/insn-eval.h
arch/x86/kernel/umip.c
arch/x86/lib/insn-eval.c

index 2b6ccf2c49f11c9c54fbe448ea7d33d997b99ca6..b8b9ef1bbd06cc65852b1f5a478051cfd817f0dd 100644 (file)
@@ -19,5 +19,7 @@ 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);
 int insn_get_code_seg_params(struct pt_regs *regs);
+int insn_fetch_from_user(struct pt_regs *regs,
+                        unsigned char buf[MAX_INSN_SIZE]);
 
 #endif /* _ASM_X86_INSN_EVAL_H */
index 2c304fd0bb1a2a2a099aefcd8bd6443cdfdc8e41..ad135be4f1f0001d8edda7765ee89bc8870d639d 100644 (file)
@@ -335,11 +335,11 @@ static void force_sig_info_umip_fault(void __user *addr, struct pt_regs *regs)
  */
 bool fixup_umip_exception(struct pt_regs *regs)
 {
-       int not_copied, nr_copied, reg_offset, dummy_data_size, umip_inst;
-       unsigned long seg_base = 0, *reg_addr;
+       int nr_copied, reg_offset, dummy_data_size, umip_inst;
        /* 10 bytes is the maximum size of the result of UMIP instructions */
        unsigned char dummy_data[10] = { 0 };
        unsigned char buf[MAX_INSN_SIZE];
+       unsigned long *reg_addr;
        void __user *uaddr;
        struct insn insn;
        int seg_defs;
@@ -347,26 +347,12 @@ bool fixup_umip_exception(struct pt_regs *regs)
        if (!regs)
                return false;
 
-       /*
-        * If not in user-space long mode, a custom code segment could be in
-        * use. This is true in protected mode (if the process defined a local
-        * descriptor table), or virtual-8086 mode. In most of the cases
-        * seg_base will be zero as in USER_CS.
-        */
-       if (!user_64bit_mode(regs))
-               seg_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
-
-       if (seg_base == -1L)
-               return false;
-
-       not_copied = copy_from_user(buf, (void __user *)(seg_base + regs->ip),
-                                   sizeof(buf));
-       nr_copied = sizeof(buf) - not_copied;
+       nr_copied = insn_fetch_from_user(regs, buf);
 
        /*
-        * The copy_from_user above could have failed if user code is protected
-        * by a memory protection key. Give up on emulation in such a case.
-        * Should we issue a page fault?
+        * The insn_fetch_from_user above could have failed if user code
+        * is protected by a memory protection key. Give up on emulation
+        * in such a case.  Should we issue a page fault?
         */
        if (!nr_copied)
                return false;
index 5e69603ff63ffa3c7e0de515d7de8a1f7a179bfd..947b7f1a004282c8a6d3be92ef193b91f776da9b 100644 (file)
@@ -1367,3 +1367,41 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
                return (void __user *)-1L;
        }
 }
+
+/**
+ * insn_fetch_from_user() - Copy instruction bytes from user-space memory
+ * @regs:      Structure with register values as seen when entering kernel mode
+ * @buf:       Array to store the fetched instruction
+ *
+ * Gets the linear address of the instruction and copies the instruction bytes
+ * to the buf.
+ *
+ * Returns:
+ *
+ * Number of instruction bytes copied.
+ *
+ * 0 if nothing was copied.
+ */
+int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
+{
+       unsigned long seg_base = 0;
+       int not_copied;
+
+       /*
+        * If not in user-space long mode, a custom code segment could be in
+        * use. This is true in protected mode (if the process defined a local
+        * descriptor table), or virtual-8086 mode. In most of the cases
+        * seg_base will be zero as in USER_CS.
+        */
+       if (!user_64bit_mode(regs)) {
+               seg_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
+               if (seg_base == -1L)
+                       return 0;
+       }
+
+
+       not_copied = copy_from_user(buf, (void __user *)(seg_base + regs->ip),
+                                   MAX_INSN_SIZE);
+
+       return MAX_INSN_SIZE - not_copied;
+}