@@ -5226,14 +5226,16 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52265226 struct vmcs12 * vmcs12 ;
52275227 struct kvm_nested_state kvm_state = {
52285228 .flags = 0 ,
5229- .format = 0 ,
5229+ .format = KVM_STATE_NESTED_FORMAT_VMX ,
52305230 .size = sizeof (kvm_state ),
5231- .vmx .vmxon_pa = -1ull ,
5232- .vmx .vmcs_pa = -1ull ,
5231+ .hdr . vmx .vmxon_pa = -1ull ,
5232+ .hdr . vmx .vmcs12_pa = -1ull ,
52335233 };
5234+ struct kvm_vmx_nested_state_data __user * user_vmx_nested_state =
5235+ & user_kvm_nested_state -> data .vmx [0 ];
52345236
52355237 if (!vcpu )
5236- return kvm_state .size + 2 * VMCS12_SIZE ;
5238+ return kvm_state .size + sizeof ( * user_vmx_nested_state ) ;
52375239
52385240 vmx = to_vmx (vcpu );
52395241 vmcs12 = get_vmcs12 (vcpu );
@@ -5243,23 +5245,23 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52435245
52445246 if (nested_vmx_allowed (vcpu ) &&
52455247 (vmx -> nested .vmxon || vmx -> nested .smm .vmxon )) {
5246- kvm_state .vmx .vmxon_pa = vmx -> nested .vmxon_ptr ;
5247- kvm_state .vmx .vmcs_pa = vmx -> nested .current_vmptr ;
5248+ kvm_state .hdr . vmx .vmxon_pa = vmx -> nested .vmxon_ptr ;
5249+ kvm_state .hdr . vmx .vmcs12_pa = vmx -> nested .current_vmptr ;
52485250
52495251 if (vmx_has_valid_vmcs12 (vcpu )) {
5250- kvm_state .size += VMCS12_SIZE ;
5252+ kvm_state .size += sizeof ( user_vmx_nested_state -> vmcs12 ) ;
52515253
52525254 if (is_guest_mode (vcpu ) &&
52535255 nested_cpu_has_shadow_vmcs (vmcs12 ) &&
52545256 vmcs12 -> vmcs_link_pointer != -1ull )
5255- kvm_state .size += VMCS12_SIZE ;
5257+ kvm_state .size += sizeof ( user_vmx_nested_state -> shadow_vmcs12 ) ;
52565258 }
52575259
52585260 if (vmx -> nested .smm .vmxon )
5259- kvm_state .vmx .smm .flags |= KVM_STATE_NESTED_SMM_VMXON ;
5261+ kvm_state .hdr . vmx .smm .flags |= KVM_STATE_NESTED_SMM_VMXON ;
52605262
52615263 if (vmx -> nested .smm .guest_mode )
5262- kvm_state .vmx .smm .flags |= KVM_STATE_NESTED_SMM_GUEST_MODE ;
5264+ kvm_state .hdr . vmx .smm .flags |= KVM_STATE_NESTED_SMM_GUEST_MODE ;
52635265
52645266 if (is_guest_mode (vcpu )) {
52655267 kvm_state .flags |= KVM_STATE_NESTED_GUEST_MODE ;
@@ -5294,16 +5296,19 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52945296 copy_shadow_to_vmcs12 (vmx );
52955297 }
52965298
5299+ BUILD_BUG_ON (sizeof (user_vmx_nested_state -> vmcs12 ) < VMCS12_SIZE );
5300+ BUILD_BUG_ON (sizeof (user_vmx_nested_state -> shadow_vmcs12 ) < VMCS12_SIZE );
5301+
52975302 /*
52985303 * Copy over the full allocated size of vmcs12 rather than just the size
52995304 * of the struct.
53005305 */
5301- if (copy_to_user (user_kvm_nested_state -> data , vmcs12 , VMCS12_SIZE ))
5306+ if (copy_to_user (user_vmx_nested_state -> vmcs12 , vmcs12 , VMCS12_SIZE ))
53025307 return - EFAULT ;
53035308
53045309 if (nested_cpu_has_shadow_vmcs (vmcs12 ) &&
53055310 vmcs12 -> vmcs_link_pointer != -1ull ) {
5306- if (copy_to_user (user_kvm_nested_state -> data + VMCS12_SIZE ,
5311+ if (copy_to_user (user_vmx_nested_state -> shadow_vmcs12 ,
53075312 get_shadow_vmcs12 (vcpu ), VMCS12_SIZE ))
53085313 return - EFAULT ;
53095314 }
@@ -5331,33 +5336,35 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53315336 struct vcpu_vmx * vmx = to_vmx (vcpu );
53325337 struct vmcs12 * vmcs12 ;
53335338 u32 exit_qual ;
5339+ struct kvm_vmx_nested_state_data __user * user_vmx_nested_state =
5340+ & user_kvm_nested_state -> data .vmx [0 ];
53345341 int ret ;
53355342
5336- if (kvm_state -> format != 0 )
5343+ if (kvm_state -> format != KVM_STATE_NESTED_FORMAT_VMX )
53375344 return - EINVAL ;
53385345
53395346 if (!nested_vmx_allowed (vcpu ))
5340- return kvm_state -> vmx .vmxon_pa == -1ull ? 0 : - EINVAL ;
5347+ return kvm_state -> hdr . vmx .vmxon_pa == -1ull ? 0 : - EINVAL ;
53415348
5342- if (kvm_state -> vmx .vmxon_pa == -1ull ) {
5343- if (kvm_state -> vmx .smm .flags )
5349+ if (kvm_state -> hdr . vmx .vmxon_pa == -1ull ) {
5350+ if (kvm_state -> hdr . vmx .smm .flags )
53445351 return - EINVAL ;
53455352
5346- if (kvm_state -> vmx .vmcs_pa != -1ull )
5353+ if (kvm_state -> hdr . vmx .vmcs12_pa != -1ull )
53475354 return - EINVAL ;
53485355
53495356 vmx_leave_nested (vcpu );
53505357 return 0 ;
53515358 }
53525359
5353- if (!page_address_valid (vcpu , kvm_state -> vmx .vmxon_pa ))
5360+ if (!page_address_valid (vcpu , kvm_state -> hdr . vmx .vmxon_pa ))
53545361 return - EINVAL ;
53555362
5356- if ((kvm_state -> vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE ) &&
5363+ if ((kvm_state -> hdr . vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE ) &&
53575364 (kvm_state -> flags & KVM_STATE_NESTED_GUEST_MODE ))
53585365 return - EINVAL ;
53595366
5360- if (kvm_state -> vmx .smm .flags &
5367+ if (kvm_state -> hdr . vmx .smm .flags &
53615368 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON ))
53625369 return - EINVAL ;
53635370
@@ -5366,21 +5373,21 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53665373 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
53675374 * must be zero.
53685375 */
5369- if (is_smm (vcpu ) ? kvm_state -> flags : kvm_state -> vmx .smm .flags )
5376+ if (is_smm (vcpu ) ? kvm_state -> flags : kvm_state -> hdr . vmx .smm .flags )
53705377 return - EINVAL ;
53715378
5372- if ((kvm_state -> vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE ) &&
5373- !(kvm_state -> vmx .smm .flags & KVM_STATE_NESTED_SMM_VMXON ))
5379+ if ((kvm_state -> hdr . vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE ) &&
5380+ !(kvm_state -> hdr . vmx .smm .flags & KVM_STATE_NESTED_SMM_VMXON ))
53745381 return - EINVAL ;
53755382
53765383 vmx_leave_nested (vcpu );
5377- if (kvm_state -> vmx .vmxon_pa == -1ull )
5384+ if (kvm_state -> hdr . vmx .vmxon_pa == -1ull )
53785385 return 0 ;
53795386
53805387 if (kvm_state -> flags & KVM_STATE_NESTED_EVMCS )
53815388 nested_enable_evmcs (vcpu , NULL );
53825389
5383- vmx -> nested .vmxon_ptr = kvm_state -> vmx .vmxon_pa ;
5390+ vmx -> nested .vmxon_ptr = kvm_state -> hdr . vmx .vmxon_pa ;
53845391 ret = enter_vmx_operation (vcpu );
53855392 if (ret )
53865393 return ret ;
@@ -5389,12 +5396,12 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53895396 if (kvm_state -> size < sizeof (* kvm_state ) + sizeof (* vmcs12 ))
53905397 return 0 ;
53915398
5392- if (kvm_state -> vmx .vmcs_pa != -1ull ) {
5393- if (kvm_state -> vmx .vmcs_pa == kvm_state -> vmx .vmxon_pa ||
5394- !page_address_valid (vcpu , kvm_state -> vmx .vmcs_pa ))
5399+ if (kvm_state -> hdr . vmx .vmcs12_pa != -1ull ) {
5400+ if (kvm_state -> hdr . vmx .vmcs12_pa == kvm_state -> hdr . vmx .vmxon_pa ||
5401+ !page_address_valid (vcpu , kvm_state -> hdr . vmx .vmcs12_pa ))
53955402 return - EINVAL ;
53965403
5397- set_current_vmptr (vmx , kvm_state -> vmx .vmcs_pa );
5404+ set_current_vmptr (vmx , kvm_state -> hdr . vmx .vmcs12_pa );
53985405 } else if (kvm_state -> flags & KVM_STATE_NESTED_EVMCS ) {
53995406 /*
54005407 * Sync eVMCS upon entry as we may not have
@@ -5405,16 +5412,16 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
54055412 return - EINVAL ;
54065413 }
54075414
5408- if (kvm_state -> vmx .smm .flags & KVM_STATE_NESTED_SMM_VMXON ) {
5415+ if (kvm_state -> hdr . vmx .smm .flags & KVM_STATE_NESTED_SMM_VMXON ) {
54095416 vmx -> nested .smm .vmxon = true;
54105417 vmx -> nested .vmxon = false;
54115418
5412- if (kvm_state -> vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE )
5419+ if (kvm_state -> hdr . vmx .smm .flags & KVM_STATE_NESTED_SMM_GUEST_MODE )
54135420 vmx -> nested .smm .guest_mode = true;
54145421 }
54155422
54165423 vmcs12 = get_vmcs12 (vcpu );
5417- if (copy_from_user (vmcs12 , user_kvm_nested_state -> data , sizeof (* vmcs12 )))
5424+ if (copy_from_user (vmcs12 , user_vmx_nested_state -> vmcs12 , sizeof (* vmcs12 )))
54185425 return - EFAULT ;
54195426
54205427 if (vmcs12 -> hdr .revision_id != VMCS12_REVISION )
@@ -5431,12 +5438,14 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
54315438 vmcs12 -> vmcs_link_pointer != -1ull ) {
54325439 struct vmcs12 * shadow_vmcs12 = get_shadow_vmcs12 (vcpu );
54335440
5434- if (kvm_state -> size < sizeof (* kvm_state ) + VMCS12_SIZE + sizeof (* vmcs12 ))
5441+ if (kvm_state -> size <
5442+ sizeof (* kvm_state ) +
5443+ sizeof (user_vmx_nested_state -> vmcs12 ) + sizeof (* shadow_vmcs12 ))
54355444 goto error_guest_mode ;
54365445
54375446 if (copy_from_user (shadow_vmcs12 ,
5438- user_kvm_nested_state -> data + VMCS12_SIZE ,
5439- sizeof (* vmcs12 ))) {
5447+ user_vmx_nested_state -> shadow_vmcs12 ,
5448+ sizeof (* shadow_vmcs12 ))) {
54405449 ret = - EFAULT ;
54415450 goto error_guest_mode ;
54425451 }
0 commit comments