DynamoRIO / DynamoRIO/drmemory

true uninitialized read in ntdll!RtlRestoreContext

Open
#2,170 0 comments 0 reactions 0 assignees View on GitHub
OpSys-Windows ThirdParty-Bug
Dominant language
C
Stars
2.7k
Forks
290
PR merge metrics
No merged PRs in 30d

Description

On Win10 1809 I'm seeing these types of errors reported on the `state`, `cs2butMTd`, and maybe more tests in 64-bit:

```
Error #8: UNINITIALIZED READ: reading 0x000000a1860fd560-0x000000a1860fd5c0 96 byte(s) within 0x000000a1860fd4c0-0x000000a1860fd6c0
# 0 ntdll.dll!RcConsolidateFrames (0x00007ffda3783c73 ) modid:6
# 1 ntdll.dll!RtlUnwindEx (0x00007ffda36e6182 ) modid:6
# 2 _UnwindNestedFrames [f:\dd\vctools\crt\crtw32\eh\amd64\trnsctrl.cpp:482] (0x00007ff643d940ab ) modid:3
# 3 main [d:\derek\drmemory\git\src\tests\cs2bug.cpp:245] (0x00007ff643d820d0 ) modid:3
Note: @0:00:08.687 in thread 1448
Note: instruction: fxrstor 0x00000100(%rcx)
```

state has 2 uninits but they seem related:
```
Error #2: UNINITIALIZED READ: reading 0x000000c1a54fea00-0x000000c1a54fea60 96 byte(s) within 0x000000c1a54fe960-0x000000c1a54feb60
# 0 ntdll.dll!RtlRestoreContext (0x00007ffda3783a76 )
??:0
# 1 ntdll.dll!RtlUnwindEx (0x00007ffda36e6182 )
??:0
# 2 ntdll.dll!RtlUserThreadStart (0x00007ffda374a251 )
??:0
Note: @0:00:02.484 in thread 10356
Note: instruction: fxrstor 0x00000100(%rcx)

Error #3: UNINITIALIZED READ: reading 0x000000c1a54fe7da-0x000000c1a54fe7e0 6 byte(s) within 0x000000c1a54fe7d0-0x000000c1a54fe7f8
# 0 ntdll.dll!RtlRestoreContext (0x00007ffda3783b8f )
??:0
# 1 ntdll.dll!RtlUnwindEx (0x00007ffda36e6182 )
??:0
# 2 ntdll.dll!RtlUserThreadStart (0x00007ffda374a251 )
??:0
Note: @0:00:02.484 in thread 10356
Note: instruction: iret %rsp (%rsp) -> %rsp
```

Those 96 bytes are at offset +160 (==0xa0) in the 512-byte structure.
Those are the xmm0-xmm7 slots in the fxsave format. Beyond them is all "reserved"; below them is floating-point and other state.

This is at offset 0x100 in CONTEXT, hence the +0x100.

Yet RtlpCopyContext skips that! This fxsave area is +0x100 from the
context. RtlpCopyContext skips those 96 bytes +0x1a0 to +0x200!!
```
000> Uf ntdll!RtlpCopyContext
ntdll!RtlpCopyContext:
00007ffd`a36e6770 48895c2410 mov qword ptr [rsp+10h],rbx
00007ffd`a36e6775 57 push rdi
00007ffd`a36e6776 4883ec20 sub rsp,20h
00007ffd`a36e677a 488bfa mov rdi,rdx
00007ffd`a36e677d 488bd9 mov rbx,rcx
00007ffd`a36e6780 483bd1 cmp rdx,rcx
00007ffd`a36e6783 0f84e6010000 je ntdll!RtlpCopyContext+0x1ff (00007ffd`a36e696f) Branch

ntdll!RtlpCopyContext+0x19:
00007ffd`a36e6789 8b4930 mov ecx,dword ptr [rcx+30h]
00007ffd`a36e678c c7433000000000 mov dword ptr [rbx+30h],0
00007ffd`a36e6793 8b4230 mov eax,dword ptr [rdx+30h]
00007ffd`a36e6796 2540001000 and eax,offset drmemorylib!options_print_usage+0x53700 (00000000`00100040)
00007ffd`a36e679b 3d40001000 cmp eax,offset drmemorylib!options_print_usage+0x53700 (00000000`00100040)
00007ffd`a36e67a0 0f84b2010000 je ntdll!RtlpCopyContext+0x1e8 (00007ffd`a36e6958) Branch

ntdll!RtlpCopyContext+0x36:
00007ffd`a36e67a6 8b4730 mov eax,dword ptr [rdi+30h]
00007ffd`a36e67a9 250f001000 and eax,offset drmemorylib!options_print_usage+0x536cf (00000000`0010000f)
00007ffd`a36e67ae 094330 or dword ptr [rbx+30h],eax
00007ffd`a36e67b1 488b87f8000000 mov rax,qword ptr [rdi+0F8h]
00007ffd`a36e67b8 488983f8000000 mov qword ptr [rbx+0F8h],rax
00007ffd`a36e67bf 488b8790000000 mov rax,qword ptr [rdi+90h]
00007ffd`a36e67c6 48898390000000 mov qword ptr [rbx+90h],rax
00007ffd`a36e67cd 488b8798000000 mov rax,qword ptr [rdi+98h]
00007ffd`a36e67d4 48898398000000 mov qword ptr [rbx+98h],rax
00007ffd`a36e67db 488b87a0000000 mov rax,qword ptr [rdi+0A0h]
00007ffd`a36e67e2 488983a0000000 mov qword ptr [rbx+0A0h],rax
00007ffd`a36e67e9 488b87a8000000 mov rax,qword ptr [rdi+0A8h]
00007ffd`a36e67f0 488983a8000000 mov qword ptr [rbx+0A8h],rax
00007ffd`a36e67f7 488b87b0000000 mov rax,qword ptr [rdi+0B0h]
00007ffd`a36e67fe 488983b0000000 mov qword ptr [rbx+0B0h],rax
00007ffd`a36e6805 488b87d8000000 mov rax,qword ptr [rdi+0D8h]
00007ffd`a36e680c 488983d8000000 mov qword ptr [rbx+0D8h],rax
00007ffd`a36e6813 488b87e0000000 mov rax,qword ptr [rdi+0E0h]
00007ffd`a36e681a 488983e0000000 mov qword ptr [rbx+0E0h],rax
00007ffd`a36e6821 488b87e8000000 mov rax,qword ptr [rdi+0E8h]
00007ffd`a36e6828 488983e8000000 mov qword ptr [rbx+0E8h],rax
00007ffd`a36e682f 488b87f0000000 mov rax,qword ptr [rdi+0F0h]
00007ffd`a36e6836 488983f0000000 mov qword ptr [rbx+0F0h],rax
00007ffd`a36e683d 0f108700020000 movups xmm0,xmmword ptr [rdi+200h]
00007ffd`a36e6844 0f118300020000 movups xmmword ptr [rbx+200h],xmm0
00007ffd`a36e684b 0f108710020000 movups xmm0,xmmword ptr [rdi+210h]
00007ffd`a36e6852 0f118310020000 movups xmmword ptr [rbx+210h],xmm0
00007ffd`a36e6859 0f108720020000 movups xmm0,xmmword ptr [rdi+220h]
00007ffd`a36e6860 0f118320020000 movups xmmword ptr [rbx+220h],xmm0
00007ffd`a36e6867 0f108730020000 movups xmm0,xmmword ptr [rdi+230h]
00007ffd`a36e686e 0f118330020000 movups xmmword ptr [rbx+230h],xmm0
00007ffd`a36e6875 0f108740020000 movups xmm0,xmmword ptr [rdi+240h]
00007ffd`a36e687c 0f118340020000 movups xmmword ptr [rbx+240h],xmm0
00007ffd`a36e6883 0f108750020000 movups xmm0,xmmword ptr [rdi+250h]
00007ffd`a36e688a 0f118350020000 movups xmmword ptr [rbx+250h],xmm0
00007ffd`a36e6891 0f108760020000 movups xmm0,xmmword ptr [rdi+260h]
00007ffd`a36e6898 0f118360020000 movups xmmword ptr [rbx+260h],xmm0
00007ffd`a36e689f 0f108770020000 movups xmm0,xmmword ptr [rdi+270h]
00007ffd`a36e68a6 0f118370020000 movups xmmword ptr [rbx+270h],xmm0
00007ffd`a36e68ad 0f108780020000 movups xmm0,xmmword ptr [rdi+280h]
00007ffd`a36e68b4 0f118380020000 movups xmmword ptr [rbx+280h],xmm0
00007ffd`a36e68bb 0f108790020000 movups xmm0,xmmword ptr [rdi+290h]
00007ffd`a36e68c2 0f118390020000 movups xmmword ptr [rbx+290h],xmm0
00007ffd`a36e68c9 0fb74738 movzx eax,word ptr [rdi+38h]
00007ffd`a36e68cd 66894338 mov word ptr [rbx+38h],ax
00007ffd`a36e68d1 0fb74742 movzx eax,word ptr [rdi+42h]
00007ffd`a36e68d5 66894342 mov word ptr [rbx+42h],ax
00007ffd`a36e68d9 8b4734 mov eax,dword ptr [rdi+34h]
00007ffd`a36e68dc 894334 mov dword ptr [rbx+34h],eax
00007ffd`a36e68df 8b4744 mov eax,dword ptr [rdi+44h]
00007ffd`a36e68e2 894344 mov dword ptr [rbx+44h],eax
00007ffd`a36e68e5 488d8700010000 lea rax,[rdi+100h]
00007ffd`a36e68ec 0f1000 movups xmm0,xmmword ptr [rax]
00007ffd`a36e68ef 4881c300010000 add rbx,100h
00007ffd`a36e68f6 0f1103 movups xmmword ptr [rbx],xmm0
00007ffd`a36e68f9 0f104810 movups xmm1,xmmword ptr [rax+10h]
00007ffd`a36e68fd 0f114b10 movups xmmword ptr [rbx+10h],xmm1
00007ffd`a36e6901 0f104020 movups xmm0,xmmword ptr [rax+20h]
00007ffd`a36e6905 0f114320 movups xmmword ptr [rbx+20h],xmm0
00007ffd`a36e6909 0f104830 movups xmm1,xmmword ptr [rax+30h]
00007ffd`a36e690d 0f114b30 movups xmmword ptr [rbx+30h],xmm1
00007ffd`a36e6911 0f104040 movups xmm0,xmmword ptr [rax+40h]
00007ffd`a36e6915 0f114340 movups xmmword ptr [rbx+40h],xmm0
00007ffd`a36e6919 0f104850 movups xmm1,xmmword ptr [rax+50h]
00007ffd`a36e691d 0f114b50 movups xmmword ptr [rbx+50h],xmm1
00007ffd`a36e6921 0f104060 movups xmm0,xmmword ptr [rax+60h]
00007ffd`a36e6925 0f114360 movups xmmword ptr [rbx+60h],xmm0
00007ffd`a36e6929 0f104070 movups xmm0,xmmword ptr [rax+70h]
00007ffd`a36e692d 0f114370 movups xmmword ptr [rbx+70h],xmm0
00007ffd`a36e6931 0f108880000000 movups xmm1,xmmword ptr [rax+80h]
00007ffd`a36e6938 0f118b80000000 movups xmmword ptr [rbx+80h],xmm1
00007ffd`a36e693f 0f108090000000 movups xmm0,xmmword ptr [rax+90h]
00007ffd`a36e6946 0f118390000000 movups xmmword ptr [rbx+90h],xmm0
00007ffd`a36e694d 488b5c2438 mov rbx,qword ptr [rsp+38h]
00007ffd`a36e6952 4883c420 add rsp,20h
00007ffd`a36e6956 5f pop rdi
00007ffd`a36e6957 c3 ret
```

windbg is failing to step past the exception: keeps looping.
I had to put a "getchar" into the top-level SEH handler and attach there.
Then I could walk:
```
# Child-SP RetAddr Call Site
00 000000f1`cffae618 00007ffd`a36e5bf8 ntdll!RtlCaptureContext2
01 000000f1`cffae620 00007ffd`a376fd79 ntdll!RtlUnwindEx+0x108
02 000000f1`cffaed30 00007ffd`a378468f ntdll!_C_specific_handler+0xd9
03 000000f1`cffaeda0 00007ffd`a36e4bef ntdll!RtlpExecuteHandlerForException+0xf
04 000000f1`cffaedd0 00007ffd`a37833fe ntdll!RtlDispatchException+0x40f
05 000000f1`cffaf500 00007ff7`969c11c0 ntdll!KiUserExceptionDispatch+0x2e
06 000000f1`cffafc10 00000000`00000000 state!test_fault_asm+0x10 [D:\derek\drmemory\git\build_DRtoo_x64_dbg\tests\CMakeFiles\state.dir\state.c_asm.asm.obj.s @ 952]
```

The fxsave wrote 0's for the xmm regs.

I then broke on the RtlpCopyContext. I wrote 0xabababababababab over the
1st xmm slot, offset 0x100+0xa0 in the context. I put a read watchpoint
there. It did not trigger until the fxrstor, where my ab's were still
there:
```
<...>
000000f1`cffae7f0 00007ff7`969d503d 00007ff7`969c3a7d
000000f1`cffae800 00007ff7`969d60d0 000001f6`6814cb40
000000f1`cffae810 000000f1`cffae930 00000000`00000000
0:000> eq 000000f1cffae660+100+a0 abababababababab
0:000> dq 000000f1cffae660+100 L40
000000f1`cffae7f0 00007ff7`969d503d 00007ff7`969c3a7d
000000f1`cffae800 abababab`abababab 000001f6`6814cb40
000000f1`cffae810 000000f1`cffae930 00000000`00000000
<...>
Breakpoint 4 hit
ntdll!RtlRestoreContext+0x1dd:
00007ffd`a3783a7d 0f2881a0010000 movaps xmm0,xmmword ptr [rcx+1A0h] ds:000000f1`cffae800=000001f66814cb40abababababababab
0:000> bl
0 e Disable Clear 00007ffd`a37838a0 0001 (0001) 0:**** ntdll!RtlRestoreContext
1 e Disable Clear 00007ffd`a37837b0 0001 (0001) 0:**** ntdll!RtlCaptureContext2
2 e Disable Clear 00007ffd`a36e6770 0001 (0001) 0:**** ntdll!RtlpCopyContext
4 e Disable Clear 000000f1`cffae800 r 8 0001 (0001) 0:****
5 e Disable Clear 000000f1`cffaefb0 r 8 0001 (0001) 0:****
0:000> rX xmm0:ub
xmm0=00 00 01 f6 68 14 cb 40 ab ab ab ab ab ab ab ab
```

(The movaps after the fxrstor seem redundant.)

Looks like a true uninitialized read.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.