x86emul: don't call ->read_segment() with x86_seg_none
PATCH 1/1 x86emul: don't call ->read_segment() with x86_seg_none
LAR, LSL, VERR, and VERW emulation involve calling protmode_load_seg() with x86_seg_none. The fuzzer's read_segment() hook function has an assertion which triggers in this case. Calling the hook function, however, makes little sense for those insns, as there's no data to retrieve. Instead zero-filling the output structure is what properly corresponds to those insns being invoked with a NUL selector.
Oss-fuzz: 70918
---
It is pure guesswork that one of those insns did trigger the assertion. The report from oss-fuzz sadly lacks details on the insn under emulation. I'm further surprised that AFL never found this.
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else
Quoted 10 lines · Jan Beulich
LAR, LSL, VERR, and VERW emulation involve calling
protmode_load_seg()withx86_seg_none. The fuzzer'sread_segment()hook function has an assertion which triggers in this case. Calling the hook function, however, makes little sense for those insns, as there's no data to retrieve. Instead zero-filling the output structure is what properly corresponds to those insns being invoked with a NUL selector.Fixes 06a3b8cd7ad2 ("x86emul: support LAR/LSL/VERR/VERW")Oss-fuzz: 70918
Signed-off-by Jan Beulich <[email protected]>
Looking at oss-fuzz's report and at this patch I think it is correct
That said, there are a few other places where read_segment is called without any checks for seg being x86_seg_none. The hvm implementation of read_segment (hvmemul_read_segment) seems to return error if x86_seg_none is passed as an argument, but there is no return error checks any time we call ops->read_segment in x86_emulate.c.
It seems that there might still be an issue?
Quoted 18 lines
---
It is pure guesswork that one of those insns did trigger the assertion. The report from oss-fuzz sadly lacks details on the insn under emulation. I'm further surprised that AFL never found this.xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else
Quoted 8 lines · Stefano Stabellini
Quoted 7 lines · Jan Beulich
LAR, LSL, VERR, and VERW emulation involve calling
protmode_load_seg()withx86_seg_none. The fuzzer'sread_segment()hook function has an assertion which triggers in this case. Calling the hook function, however, makes little sense for those insns, as there's no data to retrieve. Instead zero-filling the output structure is what properly corresponds to those insns being invoked with a NUL selector.
Quoted 7 lines
Quoted 3 lines
Fixes 06a3b8cd7ad2 ("x86emul: support LAR/LSL/VERR/VERW")Oss-fuzz: 70918
Signed-off-by Jan Beulich <[email protected]>Looking at oss-fuzz's report and at this patch I think it is correct
Signed-off-by Stefano Stabellini <[email protected]>
S-o-b?
Quoted 5 lines
That said, there are a few other places where
read_segmentis called without any checks for seg beingx86_seg_none. The hvm implementation ofread_segment(hvmemul_read_segment) seems to return error ifx86_seg_noneis passed as an argument, but there is no return error checks any time we callops->read_segmentinx86_emulate.c.
There is a pretty limited number of cases where x86_seg_none is used. For example, state->ea.mem.seg cannot hold this value. realmode_load_seg() also cannot be passed this value. We could add assertions to that effect, yet that can get unwieldy as further x86_seg_* enumerators are added (see "x86: introduce x86_seg_sys"; I expect we'll need at least one more when adding VMX/SVM insn emulation, where physical addresses are used as insn operands).
Quoted 1 line
It seems that there might still be an issue?
In my auditing I didn't spot any.
Jan
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Stefano Stabellini
Quoted 10 lines · Jan Beulich
LAR, LSL, VERR, and VERW emulation involve calling
protmode_load_seg()withx86_seg_none. The fuzzer'sread_segment()hook function has an assertion which triggers in this case. Calling the hook function, however, makes little sense for those insns, as there's no data to retrieve. Instead zero-filling the output structure is what properly corresponds to those insns being invoked with a NUL selector.Fixes 06a3b8cd7ad2 ("x86emul: support LAR/LSL/VERR/VERW")Oss-fuzz: 70918
Signed-off-by Jan Beulich <[email protected]>
Quoted 5 lines
Quoted 3 lines
Looking at oss-fuzz's report and at this patch I think it is correct
Signed-off-by Stefano Stabellini <[email protected]>S-o-b?
Sorry I meant reviewed-by
Quoted 14 lines
Quoted 5 lines
That said, there are a few other places where
read_segmentis called without any checks for seg beingx86_seg_none. The hvm implementation ofread_segment(hvmemul_read_segment) seems to return error ifx86_seg_noneis passed as an argument, but there is no return error checks any time we callops->read_segmentinx86_emulate.c.There is a pretty limited number of cases where
x86_seg_noneis used. For example,state->ea.mem.segcannot hold this value.realmode_load_seg()also cannot be passed this value. We could add assertions to that effect, yet that can get unwieldy as further x86_seg_* enumerators are added (see "x86: introducex86_seg_sys"; I expect we'll need at least one more when adding VMX/SVM insn emulation, where physical addresses are used as insn operands).
Quoted 3 lines
Quoted 1 line
It seems that there might still be an issue?
In my auditing I didn't spot any.
Following your explanation I come to the same conclusion as you, so I think it is OK. But knowing that state->ea.mem.seg cannot hold this value and realmode_load_seg() also cannot be passed this value is not something for the casual observer, so in my opinion there is room here for making the code more resilient and more obvious by always checking the return value of read_segment.
Quoted 7 lines · Jan Beulich
LAR, LSL, VERR, and VERW emulation involve calling
protmode_load_seg()withx86_seg_none. The fuzzer'sread_segment()hook function has an assertion which triggers in this case. Calling the hook function, however, makes little sense for those insns, as there's no data to retrieve. Instead zero-filling the output structure is what properly corresponds to those insns being invoked with a NUL selector.
Quoted 5 lines
Fixes 06a3b8cd7ad2 ("x86emul: support LAR/LSL/VERR/VERW")Oss-fuzz: 70918
Signed-off-by Jan Beulich <[email protected]>---
It is pure guesswork that one of those insns did trigger the assertion.
Tamas handed me the repro seeing as I don't have access to the bugs. It was VERR/VERW.
Quoted 2 lines
The report from oss-fuzz sadly lacks details on the insn under emulation.
I've got a still-pending patch to add --debug to the harness to dump that information.
Quoted 1 line
I'm further surprised that AFL never found this.
I haven't done any AFL testing since 06a3b8cd7ad2 was added.
This crash is specific to VERW/etc with a NULL selector, which will be a rare combination to encounter.
xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 846
Quoted 13 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else
While this fixes the crash, I'm not sure it will behave correctly for VERR/VERW.
protmode_load_seg() is unconditionally X86EMUL_OKAY for a NULL selector, and VERW checks for type != 0x8 which an empty attr will pass.
Interestingly, both Intel and AMD state that the NULL selector is neither readable nor writeable.
Also, looking at the emulator logic, we're missing the DPL vs CPL/RPL/Conforming checks.
~Andrew
Quoted 17 lines · Andrew Cooper
xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 846
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else While this fixes the crash, I'm not sure it will behave correctly for VERR/VERW.
Quoted 2 lines
protmode_load_seg()is unconditionallyX86EMUL_OKAYfor a NULL selector, and VERW checks for type !=0x8which an empty attr will pass.
That's past an sreg.s check, which will have failed (for sreg coming back all clear).
Also if there was a concern here, it would be orthogonal to the adding of the seg_none check: It would then have been similarly an issue for all possibilities of taking the memset() path.
Quoted 2 lines
Interestingly, both Intel and AMD state that the NULL selector is neither readable nor writeable.
Which makes sense, doesn't it? A nul selector is really more like a system segment in this regard (for VERR/VERW).
Quoted 2 lines
Also, looking at the emulator logic, we're missing the DPL vs CPL/RPL/Conforming checks.
There's surely nothing "conforming" for a nul selector. Hence perhaps you refer to something entirely unrelated?
Jan
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 846
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else While this fixes the crash, I'm not sure it will behave correctly for VERR/VERW.
Quoted 4 lines
Quoted 2 lines
protmode_load_seg()is unconditionallyX86EMUL_OKAYfor a NULL selector, and VERW checks for type !=0x8which an empty attr will pass.That's past an sreg.s check, which will have failed (for sreg coming back all clear).
Oh, so it is.
Any chance I could talk you into folding this hunk in too?
(quoted diff) +0 -0
b/xen/arch/x86/x86_emulate/x86_emulate.c
index 902538267051..d4d02c3e2eb3 100644
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -2852,7 +2852,7 @@ x86_emulate(
&sreg, ctxt, ops) ) {
case X86EMUL_OKAY:
- if ( sreg.s &&
+ if ( sreg.s /* Excludes NULL selector too */ && ((modrm_reg & 1) ? ((sreg.type & 0xa) == 0x2) : ((sreg.type & 0xa) != 0x8)) ) _regs.eflags |= X86_EFLAGS_ZF;
because it is relevant to judging whether the subsequent logic is correct or not.
Quoted 9 lines
Also if there was a concern here, it would be orthogonal to the adding of the
seg_nonecheck: It would then have been similarly an issue for all possibilities of taking thememset()path.Quoted 2 lines
Interestingly, both Intel and AMD state that the NULL selector is neither readable nor writeable.
Which makes sense, doesn't it? A nul selector is really more like a system segment in this regard (for VERR/VERW).
In the 32bit days, yes, the NULL selector was entirely unusable, but that changed in 64bit.
So IMO it really depends on whether VERR/VERW means "can I use this selector for $X", or "what does the GDT/LDT say about $X".
AMD say "Verifies whether a code or data segment specified by the segment selector in the 16-bit register or memory operand is readable from the current privilege level."
Intel say "Verifies whether the code or data segment specified with the source operand is readable (VERR) or writable[sic] (VERW) from the current privilege level (CPL)."
So that's clearly the former meaning rather than the latter meaning.
Nevertheless, AMD clearly decided it wasn't worth changing the behaviour of the instruction in a 64bit mode, probably for the same reason that Intel reused VERW for scrubbing; that no-one had really bought into x86's segmented memory model since the 386 replaced the 286.
I just found it odd, that was all.
Quoted 5 lines
Quoted 3 lines
Also, looking at the emulator logic, we're missing the DPL vs CPL/RPL/Conforming checks.
There's surely nothing "conforming" for a nul selector. Hence perhaps you refer to something entirely unrelated?
Sorry, yes. I think this is a general bug in how we emulate VERW/VERR, unrelated to this specific OSS-fuzz report.
~Andrew
Quoted 43 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 846
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else While this fixes the crash, I'm not sure it will behave correctly for VERR/VERW.
Quoted 4 lines
Quoted 2 lines
protmode_load_seg()is unconditionallyX86EMUL_OKAYfor a NULL selector, and VERW checks for type !=0x8which an empty attr will pass.That's past an sreg.s check, which will have failed (for sreg coming back all clear).
Oh, so it is.
Any chance I could talk you into folding this hunk in too?
xen/arch/x86/x86_emulate/x86_emulate.c +0
-0@@ -2852,7 +2852,7 @@ x86_emulate( &sreg, ctxt, ops) ) {case
X86EMUL_OKAY:
- if ( sreg.s &&
+ if ( sreg.s /* Excludes NULL selector too */ && ((modrm_reg& 1) ? ((sreg.type &0xa) ==0x2) : ((sreg.type &0xa) !=0x8)) ) _regs.eflags |=X86_EFLAGS_ZF;
Quoted 2 lines
because it is relevant to judging whether the subsequent logic is correct or not.
No problem at all. Am I okay to commit then, with Stefano's R-b?
Quoted 19 lines
Quoted 8 lines
Also if there was a concern here, it would be orthogonal to the adding of the
seg_nonecheck: It would then have been similarly an issue for all possibilities of taking thememset()path.Quoted 2 lines
Interestingly, both Intel and AMD state that the NULL selector is neither readable nor writeable.
Which makes sense, doesn't it? A nul selector is really more like a system segment in this regard (for VERR/VERW).
In the 32bit days, yes, the NULL selector was entirely unusable, but that changed in 64bit.
So IMO it really depends on whether VERR/VERW means "can I use this selector for $X", or "what does the GDT/LDT say about $X".
AMD say "Verifies whether a code or data segment specified by the segment selector in the 16-bit register or memory operand is readable from the current privilege level."
Quoted 5 lines
Intel say "Verifies whether the code or data segment specified with the source operand is readable (VERR) or writable[sic] (VERW) from the current privilege level (CPL)."
So that's clearly the former meaning rather than the latter meaning.
Not really. There's no "code or data segment" specified with a nul selector. The use of GDT/LDT is implicit in this wording, but imo it is there.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Also, looking at the emulator logic, we're missing the DPL vs CPL/RPL/Conforming checks.
There's surely nothing "conforming" for a nul selector. Hence perhaps you refer to something entirely unrelated?
Sorry, yes. I think this is a general bug in how we emulate VERW/VERR, unrelated to this specific OSS-fuzz report.
I'll check that later and reply separately (here or with a patch).
Jan
Andrew Cooper
Acked-byQuoted 43 lines · Jan Beulich
Quoted 42 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 846
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +2
-1@@ -839,7 +839,8 @@ protmode_load_seg(case x86_seg_tr: goto raise_exn; } -if ( !_amd_like (cp) ||vcpu_has_nscb () || !ops->read_segment || +if ( seg == x86_seg_none || !_amd_like (cp) ||vcpu_has_nscb () || + !ops->read_segment || ops->read_segment (seg, sreg, ctxt) !=X86EMUL_OKAY )memset (sreg,0 ,sizeof (*sreg));else While this fixes the crash, I'm not sure it will behave correctly for VERR/VERW.
Quoted 4 lines
Quoted 2 lines
protmode_load_seg()is unconditionallyX86EMUL_OKAYfor a NULL selector, and VERW checks for type !=0x8which an empty attr will pass.That's past an sreg.s check, which will have failed (for sreg coming back all clear).
Oh, so it is.
Any chance I could talk you into folding this hunk in too?
xen/arch/x86/x86_emulate/x86_emulate.c +0
-0@@ -2852,7 +2852,7 @@ x86_emulate( &sreg, ctxt, ops) ) {case
X86EMUL_OKAY:
- if ( sreg.s &&
+ if ( sreg.s /* Excludes NULL selector too */ && ((modrm_reg& 1) ? ((sreg.type &0xa) ==0x2) : ((sreg.type &0xa) !=0x8)) ) _regs.eflags |=X86_EFLAGS_ZF;
Quoted 3 lines
Quoted 2 lines
because it is relevant to judging whether the subsequent logic is correct or not.
No problem at all. Am I okay to commit then, with Stefano's R-b?
and don't forget the conversion to
~Andrew
Quoted 9 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
Also, looking at the emulator logic, we're missing the DPL vs CPL/RPL/Conforming checks.
There's surely nothing "conforming" for a nul selector. Hence perhaps you refer to something entirely unrelated?
Sorry, yes. I think this is a general bug in how we emulate VERW/VERR, unrelated to this specific OSS-fuzz report.
In protmode_load_seg() we have
case x86_seg_none:
/* Non-conforming segment: check DPL against RPL and CPL. */
if ( ((desc.b & (0x1c << 8)) != (0x1c << 8)) &&
((dpl < cpl) || (dpl < rpl)) )
return X86EMUL_EXCEPTION;
a_flag = 0;
break;Is there anything else you think is needed?
Jan