• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Utility functions for x86 operand and address decoding
3  *
4  * Copyright (C) Intel Corporation 2017
5  */
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/ratelimit.h>
9 #include <linux/mmu_context.h>
10 #include <asm/desc_defs.h>
11 #include <asm/desc.h>
12 #include <asm/inat.h>
13 #include <asm/insn.h>
14 #include <asm/insn-eval.h>
15 #include <asm/ldt.h>
16 #include <asm/vm86.h>
17 
18 #undef pr_fmt
19 #define pr_fmt(fmt) "insn: " fmt
20 
21 enum reg_type {
22 	REG_TYPE_RM = 0,
23 	REG_TYPE_REG,
24 	REG_TYPE_INDEX,
25 	REG_TYPE_BASE,
26 };
27 
28 /**
29  * is_string_insn() - Determine if instruction is a string instruction
30  * @insn:	Instruction containing the opcode to inspect
31  *
32  * Returns:
33  *
34  * true if the instruction, determined by the opcode, is any of the
35  * string instructions as defined in the Intel Software Development manual.
36  * False otherwise.
37  */
is_string_insn(struct insn * insn)38 static bool is_string_insn(struct insn *insn)
39 {
40 	insn_get_opcode(insn);
41 
42 	/* All string instructions have a 1-byte opcode. */
43 	if (insn->opcode.nbytes != 1)
44 		return false;
45 
46 	switch (insn->opcode.bytes[0]) {
47 	case 0x6c ... 0x6f:	/* INS, OUTS */
48 	case 0xa4 ... 0xa7:	/* MOVS, CMPS */
49 	case 0xaa ... 0xaf:	/* STOS, LODS, SCAS */
50 		return true;
51 	default:
52 		return false;
53 	}
54 }
55 
56 /**
57  * insn_has_rep_prefix() - Determine if instruction has a REP prefix
58  * @insn:	Instruction containing the prefix to inspect
59  *
60  * Returns:
61  *
62  * true if the instruction has a REP prefix, false if not.
63  */
insn_has_rep_prefix(struct insn * insn)64 bool insn_has_rep_prefix(struct insn *insn)
65 {
66 	insn_byte_t p;
67 	int i;
68 
69 	insn_get_prefixes(insn);
70 
71 	for_each_insn_prefix(insn, i, p) {
72 		if (p == 0xf2 || p == 0xf3)
73 			return true;
74 	}
75 
76 	return false;
77 }
78 
79 /**
80  * get_seg_reg_override_idx() - obtain segment register override index
81  * @insn:	Valid instruction with segment override prefixes
82  *
83  * Inspect the instruction prefixes in @insn and find segment overrides, if any.
84  *
85  * Returns:
86  *
87  * A constant identifying the segment register to use, among CS, SS, DS,
88  * ES, FS, or GS. INAT_SEG_REG_DEFAULT is returned if no segment override
89  * prefixes were found.
90  *
91  * -EINVAL in case of error.
92  */
get_seg_reg_override_idx(struct insn * insn)93 static int get_seg_reg_override_idx(struct insn *insn)
94 {
95 	int idx = INAT_SEG_REG_DEFAULT;
96 	int num_overrides = 0, i;
97 	insn_byte_t p;
98 
99 	insn_get_prefixes(insn);
100 
101 	/* Look for any segment override prefixes. */
102 	for_each_insn_prefix(insn, i, p) {
103 		insn_attr_t attr;
104 
105 		attr = inat_get_opcode_attribute(p);
106 		switch (attr) {
107 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
108 			idx = INAT_SEG_REG_CS;
109 			num_overrides++;
110 			break;
111 		case INAT_MAKE_PREFIX(INAT_PFX_SS):
112 			idx = INAT_SEG_REG_SS;
113 			num_overrides++;
114 			break;
115 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
116 			idx = INAT_SEG_REG_DS;
117 			num_overrides++;
118 			break;
119 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
120 			idx = INAT_SEG_REG_ES;
121 			num_overrides++;
122 			break;
123 		case INAT_MAKE_PREFIX(INAT_PFX_FS):
124 			idx = INAT_SEG_REG_FS;
125 			num_overrides++;
126 			break;
127 		case INAT_MAKE_PREFIX(INAT_PFX_GS):
128 			idx = INAT_SEG_REG_GS;
129 			num_overrides++;
130 			break;
131 		/* No default action needed. */
132 		}
133 	}
134 
135 	/* More than one segment override prefix leads to undefined behavior. */
136 	if (num_overrides > 1)
137 		return -EINVAL;
138 
139 	return idx;
140 }
141 
142 /**
143  * check_seg_overrides() - check if segment override prefixes are allowed
144  * @insn:	Valid instruction with segment override prefixes
145  * @regoff:	Operand offset, in pt_regs, for which the check is performed
146  *
147  * For a particular register used in register-indirect addressing, determine if
148  * segment override prefixes can be used. Specifically, no overrides are allowed
149  * for rDI if used with a string instruction.
150  *
151  * Returns:
152  *
153  * True if segment override prefixes can be used with the register indicated
154  * in @regoff. False if otherwise.
155  */
check_seg_overrides(struct insn * insn,int regoff)156 static bool check_seg_overrides(struct insn *insn, int regoff)
157 {
158 	if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
159 		return false;
160 
161 	return true;
162 }
163 
164 /**
165  * resolve_default_seg() - resolve default segment register index for an operand
166  * @insn:	Instruction with opcode and address size. Must be valid.
167  * @regs:	Register values as seen when entering kernel mode
168  * @off:	Operand offset, in pt_regs, for which resolution is needed
169  *
170  * Resolve the default segment register index associated with the instruction
171  * operand register indicated by @off. Such index is resolved based on defaults
172  * described in the Intel Software Development Manual.
173  *
174  * Returns:
175  *
176  * If in protected mode, a constant identifying the segment register to use,
177  * among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
178  *
179  * -EINVAL in case of error.
180  */
resolve_default_seg(struct insn * insn,struct pt_regs * regs,int off)181 static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
182 {
183 	if (any_64bit_mode(regs))
184 		return INAT_SEG_REG_IGNORE;
185 	/*
186 	 * Resolve the default segment register as described in Section 3.7.4
187 	 * of the Intel Software Development Manual Vol. 1:
188 	 *
189 	 *  + DS for all references involving r[ABCD]X, and rSI.
190 	 *  + If used in a string instruction, ES for rDI. Otherwise, DS.
191 	 *  + AX, CX and DX are not valid register operands in 16-bit address
192 	 *    encodings but are valid for 32-bit and 64-bit encodings.
193 	 *  + -EDOM is reserved to identify for cases in which no register
194 	 *    is used (i.e., displacement-only addressing). Use DS.
195 	 *  + SS for rSP or rBP.
196 	 *  + CS for rIP.
197 	 */
198 
199 	switch (off) {
200 	case offsetof(struct pt_regs, ax):
201 	case offsetof(struct pt_regs, cx):
202 	case offsetof(struct pt_regs, dx):
203 		/* Need insn to verify address size. */
204 		if (insn->addr_bytes == 2)
205 			return -EINVAL;
206 
207 		fallthrough;
208 
209 	case -EDOM:
210 	case offsetof(struct pt_regs, bx):
211 	case offsetof(struct pt_regs, si):
212 		return INAT_SEG_REG_DS;
213 
214 	case offsetof(struct pt_regs, di):
215 		if (is_string_insn(insn))
216 			return INAT_SEG_REG_ES;
217 		return INAT_SEG_REG_DS;
218 
219 	case offsetof(struct pt_regs, bp):
220 	case offsetof(struct pt_regs, sp):
221 		return INAT_SEG_REG_SS;
222 
223 	case offsetof(struct pt_regs, ip):
224 		return INAT_SEG_REG_CS;
225 
226 	default:
227 		return -EINVAL;
228 	}
229 }
230 
231 /**
232  * resolve_seg_reg() - obtain segment register index
233  * @insn:	Instruction with operands
234  * @regs:	Register values as seen when entering kernel mode
235  * @regoff:	Operand offset, in pt_regs, used to determine segment register
236  *
237  * Determine the segment register associated with the operands and, if
238  * applicable, prefixes and the instruction pointed by @insn.
239  *
240  * The segment register associated to an operand used in register-indirect
241  * addressing depends on:
242  *
243  * a) Whether running in long mode (in such a case segments are ignored, except
244  * if FS or GS are used).
245  *
246  * b) Whether segment override prefixes can be used. Certain instructions and
247  *    registers do not allow override prefixes.
248  *
249  * c) Whether segment overrides prefixes are found in the instruction prefixes.
250  *
251  * d) If there are not segment override prefixes or they cannot be used, the
252  *    default segment register associated with the operand register is used.
253  *
254  * The function checks first if segment override prefixes can be used with the
255  * operand indicated by @regoff. If allowed, obtain such overridden segment
256  * register index. Lastly, if not prefixes were found or cannot be used, resolve
257  * the segment register index to use based on the defaults described in the
258  * Intel documentation. In long mode, all segment register indexes will be
259  * ignored, except if overrides were found for FS or GS. All these operations
260  * are done using helper functions.
261  *
262  * The operand register, @regoff, is represented as the offset from the base of
263  * pt_regs.
264  *
265  * As stated, the main use of this function is to determine the segment register
266  * index based on the instruction, its operands and prefixes. Hence, @insn
267  * must be valid. However, if @regoff indicates rIP, we don't need to inspect
268  * @insn at all as in this case CS is used in all cases. This case is checked
269  * before proceeding further.
270  *
271  * Please note that this function does not return the value in the segment
272  * register (i.e., the segment selector) but our defined index. The segment
273  * selector needs to be obtained using get_segment_selector() and passing the
274  * segment register index resolved by this function.
275  *
276  * Returns:
277  *
278  * An index identifying the segment register to use, among CS, SS, DS,
279  * ES, FS, or GS. INAT_SEG_REG_IGNORE is returned if running in long mode.
280  *
281  * -EINVAL in case of error.
282  */
resolve_seg_reg(struct insn * insn,struct pt_regs * regs,int regoff)283 static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
284 {
285 	int idx;
286 
287 	/*
288 	 * In the unlikely event of having to resolve the segment register
289 	 * index for rIP, do it first. Segment override prefixes should not
290 	 * be used. Hence, it is not necessary to inspect the instruction,
291 	 * which may be invalid at this point.
292 	 */
293 	if (regoff == offsetof(struct pt_regs, ip)) {
294 		if (any_64bit_mode(regs))
295 			return INAT_SEG_REG_IGNORE;
296 		else
297 			return INAT_SEG_REG_CS;
298 	}
299 
300 	if (!insn)
301 		return -EINVAL;
302 
303 	if (!check_seg_overrides(insn, regoff))
304 		return resolve_default_seg(insn, regs, regoff);
305 
306 	idx = get_seg_reg_override_idx(insn);
307 	if (idx < 0)
308 		return idx;
309 
310 	if (idx == INAT_SEG_REG_DEFAULT)
311 		return resolve_default_seg(insn, regs, regoff);
312 
313 	/*
314 	 * In long mode, segment override prefixes are ignored, except for
315 	 * overrides for FS and GS.
316 	 */
317 	if (any_64bit_mode(regs)) {
318 		if (idx != INAT_SEG_REG_FS &&
319 		    idx != INAT_SEG_REG_GS)
320 			idx = INAT_SEG_REG_IGNORE;
321 	}
322 
323 	return idx;
324 }
325 
326 /**
327  * get_segment_selector() - obtain segment selector
328  * @regs:		Register values as seen when entering kernel mode
329  * @seg_reg_idx:	Segment register index to use
330  *
331  * Obtain the segment selector from any of the CS, SS, DS, ES, FS, GS segment
332  * registers. In CONFIG_X86_32, the segment is obtained from either pt_regs or
333  * kernel_vm86_regs as applicable. In CONFIG_X86_64, CS and SS are obtained
334  * from pt_regs. DS, ES, FS and GS are obtained by reading the actual CPU
335  * registers. This done for only for completeness as in CONFIG_X86_64 segment
336  * registers are ignored.
337  *
338  * Returns:
339  *
340  * Value of the segment selector, including null when running in
341  * long mode.
342  *
343  * -EINVAL on error.
344  */
get_segment_selector(struct pt_regs * regs,int seg_reg_idx)345 static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)
346 {
347 #ifdef CONFIG_X86_64
348 	unsigned short sel;
349 
350 	switch (seg_reg_idx) {
351 	case INAT_SEG_REG_IGNORE:
352 		return 0;
353 	case INAT_SEG_REG_CS:
354 		return (unsigned short)(regs->cs & 0xffff);
355 	case INAT_SEG_REG_SS:
356 		return (unsigned short)(regs->ss & 0xffff);
357 	case INAT_SEG_REG_DS:
358 		savesegment(ds, sel);
359 		return sel;
360 	case INAT_SEG_REG_ES:
361 		savesegment(es, sel);
362 		return sel;
363 	case INAT_SEG_REG_FS:
364 		savesegment(fs, sel);
365 		return sel;
366 	case INAT_SEG_REG_GS:
367 		savesegment(gs, sel);
368 		return sel;
369 	default:
370 		return -EINVAL;
371 	}
372 #else /* CONFIG_X86_32 */
373 	struct kernel_vm86_regs *vm86regs = (struct kernel_vm86_regs *)regs;
374 
375 	if (v8086_mode(regs)) {
376 		switch (seg_reg_idx) {
377 		case INAT_SEG_REG_CS:
378 			return (unsigned short)(regs->cs & 0xffff);
379 		case INAT_SEG_REG_SS:
380 			return (unsigned short)(regs->ss & 0xffff);
381 		case INAT_SEG_REG_DS:
382 			return vm86regs->ds;
383 		case INAT_SEG_REG_ES:
384 			return vm86regs->es;
385 		case INAT_SEG_REG_FS:
386 			return vm86regs->fs;
387 		case INAT_SEG_REG_GS:
388 			return vm86regs->gs;
389 		case INAT_SEG_REG_IGNORE:
390 		default:
391 			return -EINVAL;
392 		}
393 	}
394 
395 	switch (seg_reg_idx) {
396 	case INAT_SEG_REG_CS:
397 		return (unsigned short)(regs->cs & 0xffff);
398 	case INAT_SEG_REG_SS:
399 		return (unsigned short)(regs->ss & 0xffff);
400 	case INAT_SEG_REG_DS:
401 		return (unsigned short)(regs->ds & 0xffff);
402 	case INAT_SEG_REG_ES:
403 		return (unsigned short)(regs->es & 0xffff);
404 	case INAT_SEG_REG_FS:
405 		return (unsigned short)(regs->fs & 0xffff);
406 	case INAT_SEG_REG_GS:
407 		return get_user_gs(regs);
408 	case INAT_SEG_REG_IGNORE:
409 	default:
410 		return -EINVAL;
411 	}
412 #endif /* CONFIG_X86_64 */
413 }
414 
415 static const int pt_regoff[] = {
416 	offsetof(struct pt_regs, ax),
417 	offsetof(struct pt_regs, cx),
418 	offsetof(struct pt_regs, dx),
419 	offsetof(struct pt_regs, bx),
420 	offsetof(struct pt_regs, sp),
421 	offsetof(struct pt_regs, bp),
422 	offsetof(struct pt_regs, si),
423 	offsetof(struct pt_regs, di),
424 #ifdef CONFIG_X86_64
425 	offsetof(struct pt_regs, r8),
426 	offsetof(struct pt_regs, r9),
427 	offsetof(struct pt_regs, r10),
428 	offsetof(struct pt_regs, r11),
429 	offsetof(struct pt_regs, r12),
430 	offsetof(struct pt_regs, r13),
431 	offsetof(struct pt_regs, r14),
432 	offsetof(struct pt_regs, r15),
433 #else
434 	offsetof(struct pt_regs, ds),
435 	offsetof(struct pt_regs, es),
436 	offsetof(struct pt_regs, fs),
437 	offsetof(struct pt_regs, gs),
438 #endif
439 };
440 
pt_regs_offset(struct pt_regs * regs,int regno)441 int pt_regs_offset(struct pt_regs *regs, int regno)
442 {
443 	if ((unsigned)regno < ARRAY_SIZE(pt_regoff))
444 		return pt_regoff[regno];
445 	return -EDOM;
446 }
447 
get_regno(struct insn * insn,enum reg_type type)448 static int get_regno(struct insn *insn, enum reg_type type)
449 {
450 	int nr_registers = ARRAY_SIZE(pt_regoff);
451 	int regno = 0;
452 
453 	/*
454 	 * Don't possibly decode a 32-bit instructions as
455 	 * reading a 64-bit-only register.
456 	 */
457 	if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
458 		nr_registers -= 8;
459 
460 	switch (type) {
461 	case REG_TYPE_RM:
462 		regno = X86_MODRM_RM(insn->modrm.value);
463 
464 		/*
465 		 * ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement
466 		 * follows the ModRM byte.
467 		 */
468 		if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
469 			return -EDOM;
470 
471 		if (X86_REX_B(insn->rex_prefix.value))
472 			regno += 8;
473 		break;
474 
475 	case REG_TYPE_REG:
476 		regno = X86_MODRM_REG(insn->modrm.value);
477 
478 		if (X86_REX_R(insn->rex_prefix.value))
479 			regno += 8;
480 		break;
481 
482 	case REG_TYPE_INDEX:
483 		regno = X86_SIB_INDEX(insn->sib.value);
484 		if (X86_REX_X(insn->rex_prefix.value))
485 			regno += 8;
486 
487 		/*
488 		 * If ModRM.mod != 3 and SIB.index = 4 the scale*index
489 		 * portion of the address computation is null. This is
490 		 * true only if REX.X is 0. In such a case, the SIB index
491 		 * is used in the address computation.
492 		 */
493 		if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
494 			return -EDOM;
495 		break;
496 
497 	case REG_TYPE_BASE:
498 		regno = X86_SIB_BASE(insn->sib.value);
499 		/*
500 		 * If ModRM.mod is 0 and SIB.base == 5, the base of the
501 		 * register-indirect addressing is 0. In this case, a
502 		 * 32-bit displacement follows the SIB byte.
503 		 */
504 		if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
505 			return -EDOM;
506 
507 		if (X86_REX_B(insn->rex_prefix.value))
508 			regno += 8;
509 		break;
510 
511 	default:
512 		pr_err_ratelimited("invalid register type: %d\n", type);
513 		return -EINVAL;
514 	}
515 
516 	if (regno >= nr_registers) {
517 		WARN_ONCE(1, "decoded an instruction with an invalid register");
518 		return -EINVAL;
519 	}
520 	return regno;
521 }
522 
get_reg_offset(struct insn * insn,struct pt_regs * regs,enum reg_type type)523 static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
524 			  enum reg_type type)
525 {
526 	int regno = get_regno(insn, type);
527 
528 	if (regno < 0)
529 		return regno;
530 
531 	return pt_regs_offset(regs, regno);
532 }
533 
534 /**
535  * get_reg_offset_16() - Obtain offset of register indicated by instruction
536  * @insn:	Instruction containing ModRM byte
537  * @regs:	Register values as seen when entering kernel mode
538  * @offs1:	Offset of the first operand register
539  * @offs2:	Offset of the second operand register, if applicable
540  *
541  * Obtain the offset, in pt_regs, of the registers indicated by the ModRM byte
542  * in @insn. This function is to be used with 16-bit address encodings. The
543  * @offs1 and @offs2 will be written with the offset of the two registers
544  * indicated by the instruction. In cases where any of the registers is not
545  * referenced by the instruction, the value will be set to -EDOM.
546  *
547  * Returns:
548  *
549  * 0 on success, -EINVAL on error.
550  */
get_reg_offset_16(struct insn * insn,struct pt_regs * regs,int * offs1,int * offs2)551 static int get_reg_offset_16(struct insn *insn, struct pt_regs *regs,
552 			     int *offs1, int *offs2)
553 {
554 	/*
555 	 * 16-bit addressing can use one or two registers. Specifics of
556 	 * encodings are given in Table 2-1. "16-Bit Addressing Forms with the
557 	 * ModR/M Byte" of the Intel Software Development Manual.
558 	 */
559 	static const int regoff1[] = {
560 		offsetof(struct pt_regs, bx),
561 		offsetof(struct pt_regs, bx),
562 		offsetof(struct pt_regs, bp),
563 		offsetof(struct pt_regs, bp),
564 		offsetof(struct pt_regs, si),
565 		offsetof(struct pt_regs, di),
566 		offsetof(struct pt_regs, bp),
567 		offsetof(struct pt_regs, bx),
568 	};
569 
570 	static const int regoff2[] = {
571 		offsetof(struct pt_regs, si),
572 		offsetof(struct pt_regs, di),
573 		offsetof(struct pt_regs, si),
574 		offsetof(struct pt_regs, di),
575 		-EDOM,
576 		-EDOM,
577 		-EDOM,
578 		-EDOM,
579 	};
580 
581 	if (!offs1 || !offs2)
582 		return -EINVAL;
583 
584 	/* Operand is a register, use the generic function. */
585 	if (X86_MODRM_MOD(insn->modrm.value) == 3) {
586 		*offs1 = insn_get_modrm_rm_off(insn, regs);
587 		*offs2 = -EDOM;
588 		return 0;
589 	}
590 
591 	*offs1 = regoff1[X86_MODRM_RM(insn->modrm.value)];
592 	*offs2 = regoff2[X86_MODRM_RM(insn->modrm.value)];
593 
594 	/*
595 	 * If ModRM.mod is 0 and ModRM.rm is 110b, then we use displacement-
596 	 * only addressing. This means that no registers are involved in
597 	 * computing the effective address. Thus, ensure that the first
598 	 * register offset is invalid. The second register offset is already
599 	 * invalid under the aforementioned conditions.
600 	 */
601 	if ((X86_MODRM_MOD(insn->modrm.value) == 0) &&
602 	    (X86_MODRM_RM(insn->modrm.value) == 6))
603 		*offs1 = -EDOM;
604 
605 	return 0;
606 }
607 
608 /**
609  * get_desc() - Obtain contents of a segment descriptor
610  * @out:	Segment descriptor contents on success
611  * @sel:	Segment selector
612  *
613  * Given a segment selector, obtain a pointer to the segment descriptor.
614  * Both global and local descriptor tables are supported.
615  *
616  * Returns:
617  *
618  * True on success, false on failure.
619  *
620  * NULL on error.
621  */
get_desc(struct desc_struct * out,unsigned short sel)622 static bool get_desc(struct desc_struct *out, unsigned short sel)
623 {
624 	struct desc_ptr gdt_desc = {0, 0};
625 	unsigned long desc_base;
626 
627 #ifdef CONFIG_MODIFY_LDT_SYSCALL
628 	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) {
629 		bool success = false;
630 		struct ldt_struct *ldt;
631 
632 		/* Bits [15:3] contain the index of the desired entry. */
633 		sel >>= 3;
634 
635 		mutex_lock(&current->active_mm->context.lock);
636 		ldt = current->active_mm->context.ldt;
637 		if (ldt && sel < ldt->nr_entries) {
638 			*out = ldt->entries[sel];
639 			success = true;
640 		}
641 
642 		mutex_unlock(&current->active_mm->context.lock);
643 
644 		return success;
645 	}
646 #endif
647 	native_store_gdt(&gdt_desc);
648 
649 	/*
650 	 * Segment descriptors have a size of 8 bytes. Thus, the index is
651 	 * multiplied by 8 to obtain the memory offset of the desired descriptor
652 	 * from the base of the GDT. As bits [15:3] of the segment selector
653 	 * contain the index, it can be regarded as multiplied by 8 already.
654 	 * All that remains is to clear bits [2:0].
655 	 */
656 	desc_base = sel & ~(SEGMENT_RPL_MASK | SEGMENT_TI_MASK);
657 
658 	if (desc_base > gdt_desc.size)
659 		return false;
660 
661 	*out = *(struct desc_struct *)(gdt_desc.address + desc_base);
662 	return true;
663 }
664 
665 /**
666  * insn_get_seg_base() - Obtain base address of segment descriptor.
667  * @regs:		Register values as seen when entering kernel mode
668  * @seg_reg_idx:	Index of the segment register pointing to seg descriptor
669  *
670  * Obtain the base address of the segment as indicated by the segment descriptor
671  * pointed by the segment selector. The segment selector is obtained from the
672  * input segment register index @seg_reg_idx.
673  *
674  * Returns:
675  *
676  * In protected mode, base address of the segment. Zero in long mode,
677  * except when FS or GS are used. In virtual-8086 mode, the segment
678  * selector shifted 4 bits to the right.
679  *
680  * -1L in case of error.
681  */
insn_get_seg_base(struct pt_regs * regs,int seg_reg_idx)682 unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
683 {
684 	struct desc_struct desc;
685 	short sel;
686 
687 	sel = get_segment_selector(regs, seg_reg_idx);
688 	if (sel < 0)
689 		return -1L;
690 
691 	if (v8086_mode(regs))
692 		/*
693 		 * Base is simply the segment selector shifted 4
694 		 * bits to the right.
695 		 */
696 		return (unsigned long)(sel << 4);
697 
698 	if (any_64bit_mode(regs)) {
699 		/*
700 		 * Only FS or GS will have a base address, the rest of
701 		 * the segments' bases are forced to 0.
702 		 */
703 		unsigned long base;
704 
705 		if (seg_reg_idx == INAT_SEG_REG_FS) {
706 			rdmsrl(MSR_FS_BASE, base);
707 		} else if (seg_reg_idx == INAT_SEG_REG_GS) {
708 			/*
709 			 * swapgs was called at the kernel entry point. Thus,
710 			 * MSR_KERNEL_GS_BASE will have the user-space GS base.
711 			 */
712 			if (user_mode(regs))
713 				rdmsrl(MSR_KERNEL_GS_BASE, base);
714 			else
715 				rdmsrl(MSR_GS_BASE, base);
716 		} else {
717 			base = 0;
718 		}
719 		return base;
720 	}
721 
722 	/* In protected mode the segment selector cannot be null. */
723 	if (!sel)
724 		return -1L;
725 
726 	if (!get_desc(&desc, sel))
727 		return -1L;
728 
729 	return get_desc_base(&desc);
730 }
731 
732 /**
733  * get_seg_limit() - Obtain the limit of a segment descriptor
734  * @regs:		Register values as seen when entering kernel mode
735  * @seg_reg_idx:	Index of the segment register pointing to seg descriptor
736  *
737  * Obtain the limit of the segment as indicated by the segment descriptor
738  * pointed by the segment selector. The segment selector is obtained from the
739  * input segment register index @seg_reg_idx.
740  *
741  * Returns:
742  *
743  * In protected mode, the limit of the segment descriptor in bytes.
744  * In long mode and virtual-8086 mode, segment limits are not enforced. Thus,
745  * limit is returned as -1L to imply a limit-less segment.
746  *
747  * Zero is returned on error.
748  */
get_seg_limit(struct pt_regs * regs,int seg_reg_idx)749 static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)
750 {
751 	struct desc_struct desc;
752 	unsigned long limit;
753 	short sel;
754 
755 	sel = get_segment_selector(regs, seg_reg_idx);
756 	if (sel < 0)
757 		return 0;
758 
759 	if (any_64bit_mode(regs) || v8086_mode(regs))
760 		return -1L;
761 
762 	if (!sel)
763 		return 0;
764 
765 	if (!get_desc(&desc, sel))
766 		return 0;
767 
768 	/*
769 	 * If the granularity bit is set, the limit is given in multiples
770 	 * of 4096. This also means that the 12 least significant bits are
771 	 * not tested when checking the segment limits. In practice,
772 	 * this means that the segment ends in (limit << 12) + 0xfff.
773 	 */
774 	limit = get_desc_limit(&desc);
775 	if (desc.g)
776 		limit = (limit << 12) + 0xfff;
777 
778 	return limit;
779 }
780 
781 /**
782  * insn_get_code_seg_params() - Obtain code segment parameters
783  * @regs:	Structure with register values as seen when entering kernel mode
784  *
785  * Obtain address and operand sizes of the code segment. It is obtained from the
786  * selector contained in the CS register in regs. In protected mode, the default
787  * address is determined by inspecting the L and D bits of the segment
788  * descriptor. In virtual-8086 mode, the default is always two bytes for both
789  * address and operand sizes.
790  *
791  * Returns:
792  *
793  * An int containing ORed-in default parameters on success.
794  *
795  * -EINVAL on error.
796  */
insn_get_code_seg_params(struct pt_regs * regs)797 int insn_get_code_seg_params(struct pt_regs *regs)
798 {
799 	struct desc_struct desc;
800 	short sel;
801 
802 	if (v8086_mode(regs))
803 		/* Address and operand size are both 16-bit. */
804 		return INSN_CODE_SEG_PARAMS(2, 2);
805 
806 	sel = get_segment_selector(regs, INAT_SEG_REG_CS);
807 	if (sel < 0)
808 		return sel;
809 
810 	if (!get_desc(&desc, sel))
811 		return -EINVAL;
812 
813 	/*
814 	 * The most significant byte of the Type field of the segment descriptor
815 	 * determines whether a segment contains data or code. If this is a data
816 	 * segment, return error.
817 	 */
818 	if (!(desc.type & BIT(3)))
819 		return -EINVAL;
820 
821 	switch ((desc.l << 1) | desc.d) {
822 	case 0: /*
823 		 * Legacy mode. CS.L=0, CS.D=0. Address and operand size are
824 		 * both 16-bit.
825 		 */
826 		return INSN_CODE_SEG_PARAMS(2, 2);
827 	case 1: /*
828 		 * Legacy mode. CS.L=0, CS.D=1. Address and operand size are
829 		 * both 32-bit.
830 		 */
831 		return INSN_CODE_SEG_PARAMS(4, 4);
832 	case 2: /*
833 		 * IA-32e 64-bit mode. CS.L=1, CS.D=0. Address size is 64-bit;
834 		 * operand size is 32-bit.
835 		 */
836 		return INSN_CODE_SEG_PARAMS(4, 8);
837 	case 3: /* Invalid setting. CS.L=1, CS.D=1 */
838 		fallthrough;
839 	default:
840 		return -EINVAL;
841 	}
842 }
843 
844 /**
845  * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
846  * @insn:	Instruction containing the ModRM byte
847  * @regs:	Register values as seen when entering kernel mode
848  *
849  * Returns:
850  *
851  * The register indicated by the r/m part of the ModRM byte. The
852  * register is obtained as an offset from the base of pt_regs. In specific
853  * cases, the returned value can be -EDOM to indicate that the particular value
854  * of ModRM does not refer to a register and shall be ignored.
855  */
insn_get_modrm_rm_off(struct insn * insn,struct pt_regs * regs)856 int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
857 {
858 	return get_reg_offset(insn, regs, REG_TYPE_RM);
859 }
860 
861 /**
862  * insn_get_modrm_reg_off() - Obtain register in reg part of the ModRM byte
863  * @insn:	Instruction containing the ModRM byte
864  * @regs:	Register values as seen when entering kernel mode
865  *
866  * Returns:
867  *
868  * The register indicated by the reg part of the ModRM byte. The
869  * register is obtained as an offset from the base of pt_regs.
870  */
insn_get_modrm_reg_off(struct insn * insn,struct pt_regs * regs)871 int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs)
872 {
873 	return get_reg_offset(insn, regs, REG_TYPE_REG);
874 }
875 
876 /**
877  * get_seg_base_limit() - obtain base address and limit of a segment
878  * @insn:	Instruction. Must be valid.
879  * @regs:	Register values as seen when entering kernel mode
880  * @regoff:	Operand offset, in pt_regs, used to resolve segment descriptor
881  * @base:	Obtained segment base
882  * @limit:	Obtained segment limit
883  *
884  * Obtain the base address and limit of the segment associated with the operand
885  * @regoff and, if any or allowed, override prefixes in @insn. This function is
886  * different from insn_get_seg_base() as the latter does not resolve the segment
887  * associated with the instruction operand. If a limit is not needed (e.g.,
888  * when running in long mode), @limit can be NULL.
889  *
890  * Returns:
891  *
892  * 0 on success. @base and @limit will contain the base address and of the
893  * resolved segment, respectively.
894  *
895  * -EINVAL on error.
896  */
get_seg_base_limit(struct insn * insn,struct pt_regs * regs,int regoff,unsigned long * base,unsigned long * limit)897 static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
898 			      int regoff, unsigned long *base,
899 			      unsigned long *limit)
900 {
901 	int seg_reg_idx;
902 
903 	if (!base)
904 		return -EINVAL;
905 
906 	seg_reg_idx = resolve_seg_reg(insn, regs, regoff);
907 	if (seg_reg_idx < 0)
908 		return seg_reg_idx;
909 
910 	*base = insn_get_seg_base(regs, seg_reg_idx);
911 	if (*base == -1L)
912 		return -EINVAL;
913 
914 	if (!limit)
915 		return 0;
916 
917 	*limit = get_seg_limit(regs, seg_reg_idx);
918 	if (!(*limit))
919 		return -EINVAL;
920 
921 	return 0;
922 }
923 
924 /**
925  * get_eff_addr_reg() - Obtain effective address from register operand
926  * @insn:	Instruction. Must be valid.
927  * @regs:	Register values as seen when entering kernel mode
928  * @regoff:	Obtained operand offset, in pt_regs, with the effective address
929  * @eff_addr:	Obtained effective address
930  *
931  * Obtain the effective address stored in the register operand as indicated by
932  * the ModRM byte. This function is to be used only with register addressing
933  * (i.e.,  ModRM.mod is 3). The effective address is saved in @eff_addr. The
934  * register operand, as an offset from the base of pt_regs, is saved in @regoff;
935  * such offset can then be used to resolve the segment associated with the
936  * operand. This function can be used with any of the supported address sizes
937  * in x86.
938  *
939  * Returns:
940  *
941  * 0 on success. @eff_addr will have the effective address stored in the
942  * operand indicated by ModRM. @regoff will have such operand as an offset from
943  * the base of pt_regs.
944  *
945  * -EINVAL on error.
946  */
get_eff_addr_reg(struct insn * insn,struct pt_regs * regs,int * regoff,long * eff_addr)947 static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
948 			    int *regoff, long *eff_addr)
949 {
950 	int ret;
951 
952 	ret = insn_get_modrm(insn);
953 	if (ret)
954 		return ret;
955 
956 	if (X86_MODRM_MOD(insn->modrm.value) != 3)
957 		return -EINVAL;
958 
959 	*regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
960 	if (*regoff < 0)
961 		return -EINVAL;
962 
963 	/* Ignore bytes that are outside the address size. */
964 	if (insn->addr_bytes == 2)
965 		*eff_addr = regs_get_register(regs, *regoff) & 0xffff;
966 	else if (insn->addr_bytes == 4)
967 		*eff_addr = regs_get_register(regs, *regoff) & 0xffffffff;
968 	else /* 64-bit address */
969 		*eff_addr = regs_get_register(regs, *regoff);
970 
971 	return 0;
972 }
973 
974 /**
975  * get_eff_addr_modrm() - Obtain referenced effective address via ModRM
976  * @insn:	Instruction. Must be valid.
977  * @regs:	Register values as seen when entering kernel mode
978  * @regoff:	Obtained operand offset, in pt_regs, associated with segment
979  * @eff_addr:	Obtained effective address
980  *
981  * Obtain the effective address referenced by the ModRM byte of @insn. After
982  * identifying the registers involved in the register-indirect memory reference,
983  * its value is obtained from the operands in @regs. The computed address is
984  * stored @eff_addr. Also, the register operand that indicates the associated
985  * segment is stored in @regoff, this parameter can later be used to determine
986  * such segment.
987  *
988  * Returns:
989  *
990  * 0 on success. @eff_addr will have the referenced effective address. @regoff
991  * will have a register, as an offset from the base of pt_regs, that can be used
992  * to resolve the associated segment.
993  *
994  * -EINVAL on error.
995  */
get_eff_addr_modrm(struct insn * insn,struct pt_regs * regs,int * regoff,long * eff_addr)996 static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
997 			      int *regoff, long *eff_addr)
998 {
999 	long tmp;
1000 	int ret;
1001 
1002 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
1003 		return -EINVAL;
1004 
1005 	ret = insn_get_modrm(insn);
1006 	if (ret)
1007 		return ret;
1008 
1009 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
1010 		return -EINVAL;
1011 
1012 	*regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
1013 
1014 	/*
1015 	 * -EDOM means that we must ignore the address_offset. In such a case,
1016 	 * in 64-bit mode the effective address relative to the rIP of the
1017 	 * following instruction.
1018 	 */
1019 	if (*regoff == -EDOM) {
1020 		if (any_64bit_mode(regs))
1021 			tmp = regs->ip + insn->length;
1022 		else
1023 			tmp = 0;
1024 	} else if (*regoff < 0) {
1025 		return -EINVAL;
1026 	} else {
1027 		tmp = regs_get_register(regs, *regoff);
1028 	}
1029 
1030 	if (insn->addr_bytes == 4) {
1031 		int addr32 = (int)(tmp & 0xffffffff) + insn->displacement.value;
1032 
1033 		*eff_addr = addr32 & 0xffffffff;
1034 	} else {
1035 		*eff_addr = tmp + insn->displacement.value;
1036 	}
1037 
1038 	return 0;
1039 }
1040 
1041 /**
1042  * get_eff_addr_modrm_16() - Obtain referenced effective address via ModRM
1043  * @insn:	Instruction. Must be valid.
1044  * @regs:	Register values as seen when entering kernel mode
1045  * @regoff:	Obtained operand offset, in pt_regs, associated with segment
1046  * @eff_addr:	Obtained effective address
1047  *
1048  * Obtain the 16-bit effective address referenced by the ModRM byte of @insn.
1049  * After identifying the registers involved in the register-indirect memory
1050  * reference, its value is obtained from the operands in @regs. The computed
1051  * address is stored @eff_addr. Also, the register operand that indicates
1052  * the associated segment is stored in @regoff, this parameter can later be used
1053  * to determine such segment.
1054  *
1055  * Returns:
1056  *
1057  * 0 on success. @eff_addr will have the referenced effective address. @regoff
1058  * will have a register, as an offset from the base of pt_regs, that can be used
1059  * to resolve the associated segment.
1060  *
1061  * -EINVAL on error.
1062  */
get_eff_addr_modrm_16(struct insn * insn,struct pt_regs * regs,int * regoff,short * eff_addr)1063 static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
1064 				 int *regoff, short *eff_addr)
1065 {
1066 	int addr_offset1, addr_offset2, ret;
1067 	short addr1 = 0, addr2 = 0, displacement;
1068 
1069 	if (insn->addr_bytes != 2)
1070 		return -EINVAL;
1071 
1072 	insn_get_modrm(insn);
1073 
1074 	if (!insn->modrm.nbytes)
1075 		return -EINVAL;
1076 
1077 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
1078 		return -EINVAL;
1079 
1080 	ret = get_reg_offset_16(insn, regs, &addr_offset1, &addr_offset2);
1081 	if (ret < 0)
1082 		return -EINVAL;
1083 
1084 	/*
1085 	 * Don't fail on invalid offset values. They might be invalid because
1086 	 * they cannot be used for this particular value of ModRM. Instead, use
1087 	 * them in the computation only if they contain a valid value.
1088 	 */
1089 	if (addr_offset1 != -EDOM)
1090 		addr1 = regs_get_register(regs, addr_offset1) & 0xffff;
1091 
1092 	if (addr_offset2 != -EDOM)
1093 		addr2 = regs_get_register(regs, addr_offset2) & 0xffff;
1094 
1095 	displacement = insn->displacement.value & 0xffff;
1096 	*eff_addr = addr1 + addr2 + displacement;
1097 
1098 	/*
1099 	 * The first operand register could indicate to use of either SS or DS
1100 	 * registers to obtain the segment selector.  The second operand
1101 	 * register can only indicate the use of DS. Thus, the first operand
1102 	 * will be used to obtain the segment selector.
1103 	 */
1104 	*regoff = addr_offset1;
1105 
1106 	return 0;
1107 }
1108 
1109 /**
1110  * get_eff_addr_sib() - Obtain referenced effective address via SIB
1111  * @insn:	Instruction. Must be valid.
1112  * @regs:	Register values as seen when entering kernel mode
1113  * @regoff:	Obtained operand offset, in pt_regs, associated with segment
1114  * @eff_addr:	Obtained effective address
1115  *
1116  * Obtain the effective address referenced by the SIB byte of @insn. After
1117  * identifying the registers involved in the indexed, register-indirect memory
1118  * reference, its value is obtained from the operands in @regs. The computed
1119  * address is stored @eff_addr. Also, the register operand that indicates the
1120  * associated segment is stored in @regoff, this parameter can later be used to
1121  * determine such segment.
1122  *
1123  * Returns:
1124  *
1125  * 0 on success. @eff_addr will have the referenced effective address.
1126  * @base_offset will have a register, as an offset from the base of pt_regs,
1127  * that can be used to resolve the associated segment.
1128  *
1129  * Negative value on error.
1130  */
get_eff_addr_sib(struct insn * insn,struct pt_regs * regs,int * base_offset,long * eff_addr)1131 static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
1132 			    int *base_offset, long *eff_addr)
1133 {
1134 	long base, indx;
1135 	int indx_offset;
1136 	int ret;
1137 
1138 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
1139 		return -EINVAL;
1140 
1141 	ret = insn_get_modrm(insn);
1142 	if (ret)
1143 		return ret;
1144 
1145 	if (!insn->modrm.nbytes)
1146 		return -EINVAL;
1147 
1148 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
1149 		return -EINVAL;
1150 
1151 	ret = insn_get_sib(insn);
1152 	if (ret)
1153 		return ret;
1154 
1155 	if (!insn->sib.nbytes)
1156 		return -EINVAL;
1157 
1158 	*base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
1159 	indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
1160 
1161 	/*
1162 	 * Negative values in the base and index offset means an error when
1163 	 * decoding the SIB byte. Except -EDOM, which means that the registers
1164 	 * should not be used in the address computation.
1165 	 */
1166 	if (*base_offset == -EDOM)
1167 		base = 0;
1168 	else if (*base_offset < 0)
1169 		return -EINVAL;
1170 	else
1171 		base = regs_get_register(regs, *base_offset);
1172 
1173 	if (indx_offset == -EDOM)
1174 		indx = 0;
1175 	else if (indx_offset < 0)
1176 		return -EINVAL;
1177 	else
1178 		indx = regs_get_register(regs, indx_offset);
1179 
1180 	if (insn->addr_bytes == 4) {
1181 		int addr32, base32, idx32;
1182 
1183 		base32 = base & 0xffffffff;
1184 		idx32 = indx & 0xffffffff;
1185 
1186 		addr32 = base32 + idx32 * (1 << X86_SIB_SCALE(insn->sib.value));
1187 		addr32 += insn->displacement.value;
1188 
1189 		*eff_addr = addr32 & 0xffffffff;
1190 	} else {
1191 		*eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value));
1192 		*eff_addr += insn->displacement.value;
1193 	}
1194 
1195 	return 0;
1196 }
1197 
1198 /**
1199  * get_addr_ref_16() - Obtain the 16-bit address referred by instruction
1200  * @insn:	Instruction containing ModRM byte and displacement
1201  * @regs:	Register values as seen when entering kernel mode
1202  *
1203  * This function is to be used with 16-bit address encodings. Obtain the memory
1204  * address referred by the instruction's ModRM and displacement bytes. Also, the
1205  * segment used as base is determined by either any segment override prefixes in
1206  * @insn or the default segment of the registers involved in the address
1207  * computation. In protected mode, segment limits are enforced.
1208  *
1209  * Returns:
1210  *
1211  * Linear address referenced by the instruction operands on success.
1212  *
1213  * -1L on error.
1214  */
get_addr_ref_16(struct insn * insn,struct pt_regs * regs)1215 static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
1216 {
1217 	unsigned long linear_addr = -1L, seg_base, seg_limit;
1218 	int ret, regoff;
1219 	short eff_addr;
1220 	long tmp;
1221 
1222 	if (insn_get_displacement(insn))
1223 		goto out;
1224 
1225 	if (insn->addr_bytes != 2)
1226 		goto out;
1227 
1228 	if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1229 		ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
1230 		if (ret)
1231 			goto out;
1232 
1233 		eff_addr = tmp;
1234 	} else {
1235 		ret = get_eff_addr_modrm_16(insn, regs, &regoff, &eff_addr);
1236 		if (ret)
1237 			goto out;
1238 	}
1239 
1240 	ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
1241 	if (ret)
1242 		goto out;
1243 
1244 	/*
1245 	 * Before computing the linear address, make sure the effective address
1246 	 * is within the limits of the segment. In virtual-8086 mode, segment
1247 	 * limits are not enforced. In such a case, the segment limit is -1L to
1248 	 * reflect this fact.
1249 	 */
1250 	if ((unsigned long)(eff_addr & 0xffff) > seg_limit)
1251 		goto out;
1252 
1253 	linear_addr = (unsigned long)(eff_addr & 0xffff) + seg_base;
1254 
1255 	/* Limit linear address to 20 bits */
1256 	if (v8086_mode(regs))
1257 		linear_addr &= 0xfffff;
1258 
1259 out:
1260 	return (void __user *)linear_addr;
1261 }
1262 
1263 /**
1264  * get_addr_ref_32() - Obtain a 32-bit linear address
1265  * @insn:	Instruction with ModRM, SIB bytes and displacement
1266  * @regs:	Register values as seen when entering kernel mode
1267  *
1268  * This function is to be used with 32-bit address encodings to obtain the
1269  * linear memory address referred by the instruction's ModRM, SIB,
1270  * displacement bytes and segment base address, as applicable. If in protected
1271  * mode, segment limits are enforced.
1272  *
1273  * Returns:
1274  *
1275  * Linear address referenced by instruction and registers on success.
1276  *
1277  * -1L on error.
1278  */
get_addr_ref_32(struct insn * insn,struct pt_regs * regs)1279 static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
1280 {
1281 	unsigned long linear_addr = -1L, seg_base, seg_limit;
1282 	int eff_addr, regoff;
1283 	long tmp;
1284 	int ret;
1285 
1286 	if (insn->addr_bytes != 4)
1287 		goto out;
1288 
1289 	if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1290 		ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
1291 		if (ret)
1292 			goto out;
1293 
1294 		eff_addr = tmp;
1295 
1296 	} else {
1297 		if (insn->sib.nbytes) {
1298 			ret = get_eff_addr_sib(insn, regs, &regoff, &tmp);
1299 			if (ret)
1300 				goto out;
1301 
1302 			eff_addr = tmp;
1303 		} else {
1304 			ret = get_eff_addr_modrm(insn, regs, &regoff, &tmp);
1305 			if (ret)
1306 				goto out;
1307 
1308 			eff_addr = tmp;
1309 		}
1310 	}
1311 
1312 	ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
1313 	if (ret)
1314 		goto out;
1315 
1316 	/*
1317 	 * In protected mode, before computing the linear address, make sure
1318 	 * the effective address is within the limits of the segment.
1319 	 * 32-bit addresses can be used in long and virtual-8086 modes if an
1320 	 * address override prefix is used. In such cases, segment limits are
1321 	 * not enforced. When in virtual-8086 mode, the segment limit is -1L
1322 	 * to reflect this situation.
1323 	 *
1324 	 * After computed, the effective address is treated as an unsigned
1325 	 * quantity.
1326 	 */
1327 	if (!any_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit))
1328 		goto out;
1329 
1330 	/*
1331 	 * Even though 32-bit address encodings are allowed in virtual-8086
1332 	 * mode, the address range is still limited to [0x-0xffff].
1333 	 */
1334 	if (v8086_mode(regs) && (eff_addr & ~0xffff))
1335 		goto out;
1336 
1337 	/*
1338 	 * Data type long could be 64 bits in size. Ensure that our 32-bit
1339 	 * effective address is not sign-extended when computing the linear
1340 	 * address.
1341 	 */
1342 	linear_addr = (unsigned long)(eff_addr & 0xffffffff) + seg_base;
1343 
1344 	/* Limit linear address to 20 bits */
1345 	if (v8086_mode(regs))
1346 		linear_addr &= 0xfffff;
1347 
1348 out:
1349 	return (void __user *)linear_addr;
1350 }
1351 
1352 /**
1353  * get_addr_ref_64() - Obtain a 64-bit linear address
1354  * @insn:	Instruction struct with ModRM and SIB bytes and displacement
1355  * @regs:	Structure with register values as seen when entering kernel mode
1356  *
1357  * This function is to be used with 64-bit address encodings to obtain the
1358  * linear memory address referred by the instruction's ModRM, SIB,
1359  * displacement bytes and segment base address, as applicable.
1360  *
1361  * Returns:
1362  *
1363  * Linear address referenced by instruction and registers on success.
1364  *
1365  * -1L on error.
1366  */
1367 #ifndef CONFIG_X86_64
get_addr_ref_64(struct insn * insn,struct pt_regs * regs)1368 static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
1369 {
1370 	return (void __user *)-1L;
1371 }
1372 #else
get_addr_ref_64(struct insn * insn,struct pt_regs * regs)1373 static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
1374 {
1375 	unsigned long linear_addr = -1L, seg_base;
1376 	int regoff, ret;
1377 	long eff_addr;
1378 
1379 	if (insn->addr_bytes != 8)
1380 		goto out;
1381 
1382 	if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1383 		ret = get_eff_addr_reg(insn, regs, &regoff, &eff_addr);
1384 		if (ret)
1385 			goto out;
1386 
1387 	} else {
1388 		if (insn->sib.nbytes) {
1389 			ret = get_eff_addr_sib(insn, regs, &regoff, &eff_addr);
1390 			if (ret)
1391 				goto out;
1392 		} else {
1393 			ret = get_eff_addr_modrm(insn, regs, &regoff, &eff_addr);
1394 			if (ret)
1395 				goto out;
1396 		}
1397 
1398 	}
1399 
1400 	ret = get_seg_base_limit(insn, regs, regoff, &seg_base, NULL);
1401 	if (ret)
1402 		goto out;
1403 
1404 	linear_addr = (unsigned long)eff_addr + seg_base;
1405 
1406 out:
1407 	return (void __user *)linear_addr;
1408 }
1409 #endif /* CONFIG_X86_64 */
1410 
1411 /**
1412  * insn_get_addr_ref() - Obtain the linear address referred by instruction
1413  * @insn:	Instruction structure containing ModRM byte and displacement
1414  * @regs:	Structure with register values as seen when entering kernel mode
1415  *
1416  * Obtain the linear address referred by the instruction's ModRM, SIB and
1417  * displacement bytes, and segment base, as applicable. In protected mode,
1418  * segment limits are enforced.
1419  *
1420  * Returns:
1421  *
1422  * Linear address referenced by instruction and registers on success.
1423  *
1424  * -1L on error.
1425  */
insn_get_addr_ref(struct insn * insn,struct pt_regs * regs)1426 void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
1427 {
1428 	if (!insn || !regs)
1429 		return (void __user *)-1L;
1430 
1431 	switch (insn->addr_bytes) {
1432 	case 2:
1433 		return get_addr_ref_16(insn, regs);
1434 	case 4:
1435 		return get_addr_ref_32(insn, regs);
1436 	case 8:
1437 		return get_addr_ref_64(insn, regs);
1438 	default:
1439 		return (void __user *)-1L;
1440 	}
1441 }
1442 
insn_get_effective_ip(struct pt_regs * regs,unsigned long * ip)1443 int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
1444 {
1445 	unsigned long seg_base = 0;
1446 
1447 	/*
1448 	 * If not in user-space long mode, a custom code segment could be in
1449 	 * use. This is true in protected mode (if the process defined a local
1450 	 * descriptor table), or virtual-8086 mode. In most of the cases
1451 	 * seg_base will be zero as in USER_CS.
1452 	 */
1453 	if (!user_64bit_mode(regs)) {
1454 		seg_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
1455 		if (seg_base == -1L)
1456 			return -EINVAL;
1457 	}
1458 
1459 	*ip = seg_base + regs->ip;
1460 
1461 	return 0;
1462 }
1463 
1464 /**
1465  * insn_fetch_from_user() - Copy instruction bytes from user-space memory
1466  * @regs:	Structure with register values as seen when entering kernel mode
1467  * @buf:	Array to store the fetched instruction
1468  *
1469  * Gets the linear address of the instruction and copies the instruction bytes
1470  * to the buf.
1471  *
1472  * Returns:
1473  *
1474  * - number of instruction bytes copied.
1475  * - 0 if nothing was copied.
1476  * - -EINVAL if the linear address of the instruction could not be calculated
1477  */
insn_fetch_from_user(struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE])1478 int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
1479 {
1480 	unsigned long ip;
1481 	int not_copied;
1482 
1483 	if (insn_get_effective_ip(regs, &ip))
1484 		return -EINVAL;
1485 
1486 	not_copied = copy_from_user(buf, (void __user *)ip, MAX_INSN_SIZE);
1487 
1488 	return MAX_INSN_SIZE - not_copied;
1489 }
1490 
1491 /**
1492  * insn_fetch_from_user_inatomic() - Copy instruction bytes from user-space memory
1493  *                                   while in atomic code
1494  * @regs:	Structure with register values as seen when entering kernel mode
1495  * @buf:	Array to store the fetched instruction
1496  *
1497  * Gets the linear address of the instruction and copies the instruction bytes
1498  * to the buf. This function must be used in atomic context.
1499  *
1500  * Returns:
1501  *
1502  *  - number of instruction bytes copied.
1503  *  - 0 if nothing was copied.
1504  *  - -EINVAL if the linear address of the instruction could not be calculated.
1505  */
insn_fetch_from_user_inatomic(struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE])1506 int insn_fetch_from_user_inatomic(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
1507 {
1508 	unsigned long ip;
1509 	int not_copied;
1510 
1511 	if (insn_get_effective_ip(regs, &ip))
1512 		return -EINVAL;
1513 
1514 	not_copied = __copy_from_user_inatomic(buf, (void __user *)ip, MAX_INSN_SIZE);
1515 
1516 	return MAX_INSN_SIZE - not_copied;
1517 }
1518 
1519 /**
1520  * insn_decode_from_regs() - Decode an instruction
1521  * @insn:	Structure to store decoded instruction
1522  * @regs:	Structure with register values as seen when entering kernel mode
1523  * @buf:	Buffer containing the instruction bytes
1524  * @buf_size:   Number of instruction bytes available in buf
1525  *
1526  * Decodes the instruction provided in buf and stores the decoding results in
1527  * insn. Also determines the correct address and operand sizes.
1528  *
1529  * Returns:
1530  *
1531  * True if instruction was decoded, False otherwise.
1532  */
insn_decode_from_regs(struct insn * insn,struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE],int buf_size)1533 bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
1534 			   unsigned char buf[MAX_INSN_SIZE], int buf_size)
1535 {
1536 	int seg_defs;
1537 
1538 	insn_init(insn, buf, buf_size, user_64bit_mode(regs));
1539 
1540 	/*
1541 	 * Override the default operand and address sizes with what is specified
1542 	 * in the code segment descriptor. The instruction decoder only sets
1543 	 * the address size it to either 4 or 8 address bytes and does nothing
1544 	 * for the operand bytes. This OK for most of the cases, but we could
1545 	 * have special cases where, for instance, a 16-bit code segment
1546 	 * descriptor is used.
1547 	 * If there is an address override prefix, the instruction decoder
1548 	 * correctly updates these values, even for 16-bit defaults.
1549 	 */
1550 	seg_defs = insn_get_code_seg_params(regs);
1551 	if (seg_defs == -EINVAL)
1552 		return false;
1553 
1554 	insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs);
1555 	insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs);
1556 
1557 	if (insn_get_length(insn))
1558 		return false;
1559 
1560 	if (buf_size < insn->length)
1561 		return false;
1562 
1563 	return true;
1564 }
1565