Skip to content

Commit 7530d0d

Browse files
executor: sys/linux: implement SYZOS_API_NESTED_INTEL_VMWRITE_MASK
The new command allows mutation of Intel VMCS fields with the help of vmwrite instruction. In addition to VM ID and field ID, @nested_intel_vmwrite_mask takes three 64-bit numbers: the set mask, the unset mask, and the flip mask. This allows to make bitwise modifications to VMCS without disturbing the execution too much. Also add sys/linux/test/amd64-syz_kvm_nested_vmwrite_mask to test the new command behavior.
1 parent 52ed5c9 commit 7530d0d

File tree

3 files changed

+114
-15
lines changed

3 files changed

+114
-15
lines changed

executor/common_kvm_amd64_syzos.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef enum {
3131
SYZOS_API_NESTED_LOAD_CODE = 302,
3232
SYZOS_API_NESTED_VMLAUNCH = 303,
3333
SYZOS_API_NESTED_VMRESUME = 304,
34+
SYZOS_API_NESTED_INTEL_VMWRITE_MASK = 340,
3435
SYZOS_API_STOP, // Must be the last one
3536
} syzos_api_id;
3637

@@ -76,6 +77,11 @@ struct api_call_3 {
7677
uint64 args[3];
7778
};
7879

80+
struct api_call_5 {
81+
struct api_call_header header;
82+
uint64 args[5];
83+
};
84+
7985
// This struct must match the push/pop order in nested_vm_exit_handler_intel_asm().
8086
struct l2_guest_regs {
8187
uint64 rax, rbx, rcx, rdx, rsi, rdi, rbp;
@@ -104,6 +110,7 @@ GUEST_CODE static void guest_handle_nested_create_vm(struct api_call_1* cmd, uin
104110
GUEST_CODE static void guest_handle_nested_load_code(struct api_call_nested_load_code* cmd, uint64 cpu_id);
105111
GUEST_CODE static void guest_handle_nested_vmlaunch(struct api_call_1* cmd, uint64 cpu_id);
106112
GUEST_CODE static void guest_handle_nested_vmresume(struct api_call_1* cmd, uint64 cpu_id);
113+
GUEST_CODE static void guest_handle_nested_intel_vmwrite_mask(struct api_call_5* cmd, uint64 cpu_id);
107114

108115
typedef enum {
109116
UEXIT_END = (uint64)-1,
@@ -213,6 +220,9 @@ guest_main(uint64 size, uint64 cpu)
213220
} else if (call == SYZOS_API_NESTED_VMRESUME) {
214221
// Resume a nested VM.
215222
guest_handle_nested_vmresume((struct api_call_1*)cmd, cpu);
223+
} else if (call == SYZOS_API_NESTED_INTEL_VMWRITE_MASK) {
224+
// Write to a VMCS field using masks.
225+
guest_handle_nested_intel_vmwrite_mask((struct api_call_5*)cmd, cpu);
216226
}
217227
addr += cmd->size;
218228
size -= cmd->size;
@@ -1228,4 +1238,22 @@ guest_handle_nested_vmresume(struct api_call_1* cmd, uint64 cpu_id)
12281238
}
12291239
}
12301240

1241+
GUEST_CODE static noinline void
1242+
guest_handle_nested_intel_vmwrite_mask(struct api_call_5* cmd, uint64 cpu_id)
1243+
{
1244+
if (get_cpu_vendor() != CPU_VENDOR_INTEL)
1245+
return;
1246+
uint64 vm_id = cmd->args[0];
1247+
nested_vmptrld(cpu_id, vm_id);
1248+
uint64 field = cmd->args[1];
1249+
uint64 set_mask = cmd->args[2];
1250+
uint64 unset_mask = cmd->args[3];
1251+
uint64 flip_mask = cmd->args[4];
1252+
1253+
uint64 current_value = vmread(field);
1254+
uint64 new_value = (current_value & ~unset_mask) | set_mask;
1255+
new_value ^= flip_mask;
1256+
vmwrite(field, new_value);
1257+
}
1258+
12311259
#endif // EXECUTOR_COMMON_KVM_AMD64_SYZOS_H

sys/linux/dev_kvm_amd64.txt

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,54 @@ syzos_api_nested_load_code {
101101
insns text[x86_64]
102102
} [packed]
103103

104+
# VMCS Component Encoding is a 32-bit value, but only bits 0:15 are used.
105+
# Certain bit combinations are invalid, because the number encodes additional
106+
# parameters like access type and width.
107+
# Constants as per Intel SDM vol. 3C, Appendix B.
108+
vmcs_field_encoding [
109+
control16 int64[0x0:0x4, 2]
110+
guest16 int64[0x800:0x812, 2]
111+
host16 int64[0xc00:0xc0c, 2]
112+
control64 int64[0x2000:0x2033]
113+
ro64 int64[0x2400:0x2401]
114+
guest64 int64[0x2800:0x2813]
115+
host64 int64[0x2c00:0x2c05]
116+
control32 int64[0x4000:0x4022, 2]
117+
ro32 int64[0x4400:0x440e, 2]
118+
guest32 int64[0x4800:0x482e, 2]
119+
host32 int64[0x4c00:0x4c00]
120+
control_nat int64[0x6000:0x600e, 2]
121+
ro_nat int64[0x6400:0x640a, 2]
122+
guest_nat int64[0x6800:0x6826, 2]
123+
host_nat int64[0x6c00:0x6c16, 2]
124+
]
125+
126+
syzos_api_nested_intel_vmwrite_mask {
127+
vm_id syzos_api_vm_id
128+
field vmcs_field_encoding
129+
set_mask int64
130+
unset_mask int64
131+
flip_mask int64
132+
}
133+
104134
# IDs here must match those in executor/common_kvm_amd64_syzos.h.
105135
syzos_api_call$x86 [
106-
uexit syzos_api$x86[0, intptr]
107-
code syzos_api$x86[10, syzos_api_code$x86]
108-
cpuid syzos_api$x86[100, syzos_api_cpuid]
109-
wrmsr syzos_api$x86[101, syzos_api_wrmsr]
110-
rdmsr syzos_api$x86[102, syzos_api_rdmsr]
111-
wr_crn syzos_api$x86[103, syzos_api_wr_crn]
112-
wr_drn syzos_api$x86[104, syzos_api_wr_drn]
113-
in_dx syzos_api$x86[105, syzos_api_in_dx]
114-
out_dx syzos_api$x86[106, syzos_api_out_dx]
115-
set_irq_handler syzos_api$x86[200, syzos_api_set_irq_handler]
116-
enable_nested syzos_api$x86[300, const[0, intptr]]
117-
nested_create_vm syzos_api$x86[301, syzos_api_vm_id]
118-
nested_load_code syzos_api$x86[302, syzos_api_nested_load_code]
119-
nested_vmlaunch syzos_api$x86[303, syzos_api_vm_id]
120-
nested_vmresume syzos_api$x86[304, syzos_api_vm_id]
136+
uexit syzos_api$x86[0, intptr]
137+
code syzos_api$x86[10, syzos_api_code$x86]
138+
cpuid syzos_api$x86[100, syzos_api_cpuid]
139+
wrmsr syzos_api$x86[101, syzos_api_wrmsr]
140+
rdmsr syzos_api$x86[102, syzos_api_rdmsr]
141+
wr_crn syzos_api$x86[103, syzos_api_wr_crn]
142+
wr_drn syzos_api$x86[104, syzos_api_wr_drn]
143+
in_dx syzos_api$x86[105, syzos_api_in_dx]
144+
out_dx syzos_api$x86[106, syzos_api_out_dx]
145+
set_irq_handler syzos_api$x86[200, syzos_api_set_irq_handler]
146+
enable_nested syzos_api$x86[300, const[0, intptr]]
147+
nested_create_vm syzos_api$x86[301, syzos_api_vm_id]
148+
nested_load_code syzos_api$x86[302, syzos_api_nested_load_code]
149+
nested_vmlaunch syzos_api$x86[303, syzos_api_vm_id]
150+
nested_vmresume syzos_api$x86[304, syzos_api_vm_id]
151+
nested_intel_vmwrite_mask syzos_api$x86[340, syzos_api_nested_intel_vmwrite_mask]
121152
] [varlen]
122153

123154
kvm_text_x86 [
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# requires: arch=amd64 -threaded
3+
#
4+
r0 = openat$kvm(0, &AUTO='/dev/kvm\x00', 0x0, 0x0)
5+
r1 = ioctl$KVM_CREATE_VM(r0, AUTO, 0x0)
6+
r2 = syz_kvm_setup_syzos_vm$x86(r1, &(0x7f0000c00000/0x400000)=nil)
7+
8+
# Create a nested VM that performs HLT and INVD.
9+
# 1. L2 executes HLT -> exit to L1.
10+
# 2. L1 disables HLT exiting via vmwrite to CPU_BASED_VM_EXEC_CONTROL.
11+
# 3. L1 resumes L2 (RIP still at HLT).
12+
# 4. L2 executes HLT (no exit), which is delivered to L0.
13+
# 5. L0 resumes L1, which is still running L2.
14+
# 6. L2 executes INVD -> exit to L1.
15+
#
16+
r3 = syz_kvm_add_vcpu$x86(r2, &AUTO={0x0, &AUTO=[@enable_nested={AUTO, AUTO, 0x0}, @nested_create_vm={AUTO, AUTO, 0x0}, @nested_load_code={AUTO, AUTO, {0x0, "f40f08"}}, @nested_vmlaunch={AUTO, AUTO, 0x0}, @nested_intel_vmwrite_mask={AUTO, AUTO, {0x0, @control32=0x4002, 0x0, 0x80, 0x0}}, @nested_vmresume={AUTO, AUTO, 0x0}], AUTO})
17+
r4 = ioctl$KVM_GET_VCPU_MMAP_SIZE(r0, AUTO)
18+
r5 = mmap$KVM_VCPU(&(0x7f0000009000/0x1000)=nil, r4, 0x3, 0x1, r3, 0x0)
19+
20+
# L2 VM executes HLT. Exit reason is mapped to 0xe2e20001.
21+
#
22+
ioctl$KVM_RUN(r3, AUTO, 0x0)
23+
syz_kvm_assert_syzos_uexit$x86(r5, 0xe2e20001)
24+
25+
# L1 disables HLT exiting and resumes L2. L2 resumes at HLT, and breaks out of KVM_RUN.
26+
# 0x05 is KVM_EXIT_HLT.
27+
# This confirms that the vmwrite command to disable HLT exiting was successful.
28+
#
29+
ioctl$KVM_RUN(r3, AUTO, 0x0)
30+
syz_kvm_assert_syzos_kvm_exit$x86(r5, 0x5)
31+
32+
# After resuming, L2 executes INVD. Exit reason is mapped to 0xe2e20002.
33+
#
34+
ioctl$KVM_RUN(r3, AUTO, 0x0)
35+
syz_kvm_assert_syzos_uexit$x86(r5, 0xe2e20002)
36+
37+
# guest_main should finish with guest_uexit(-1).
38+
#
39+
ioctl$KVM_RUN(r3, AUTO, 0x0)
40+
syz_kvm_assert_syzos_uexit$x86(r5, 0xffffffff)

0 commit comments

Comments
 (0)