WCharacter / RDTSC-KVM-Handler

my patches for linux kernel to spoof rdtsc and make vm exit undetected
209 stars 27 forks source link

cant build :( #7

Closed KaiserBloo closed 2 years ago

KaiserBloo commented 2 years ago

i have tried building on both 5.14.11 and linux-5.15-rc5 from kernels.org and also tried by cloning torvalds/linux but i get the same error everytime

arch/x86/kvm/svm/svm.c:3187:43: error: initialization of ‘int ()(struct kvm_vcpu )’ from incompatible pointer type ‘int ()(struct vcpu_svm )’ [-Werror=incompatible-pointer-types]

tried both saving the svm.c from the repo and also tried manually adding the patches

Kaydax commented 2 years ago

This is an issue with the fact that Linux kernel 5.14.11+ changed what args svm takes in. Its now more in line with how the intel vmx function is. Here is the fix for amd users:

static u32 print_once = 1;

static int handle_rdtsc_interception(struct kvm_vcpu *vcpu) 
{
  static u64 rdtsc_fake = 0;
    static u64 rdtsc_prev = 0;
    u64 rdtsc_real = rdtsc();

    if(print_once)
    {
        printk("[handle_rdtsc] fake rdtsc svm function is working\n");
        print_once = 0;
        rdtsc_fake = rdtsc_real;
    }

    if(rdtsc_prev != 0)
    {
        if(rdtsc_real > rdtsc_prev)
        {
            u64 diff = rdtsc_real - rdtsc_prev;
            u64 fake_diff =  diff / 19; //This is for Ryzen 7 5800x with a base of 3.8. Change the 19 to the number you need. To get the number, divide your base clock by a number to get 0.2
            rdtsc_fake += fake_diff;
        }
    }
    if(rdtsc_fake > rdtsc_real)
    {
        rdtsc_fake = rdtsc_real;
    }

    rdtsc_prev = rdtsc_real;
    vcpu->arch.regs[VCPU_REGS_RAX] = rdtsc_fake & -1u;
  vcpu->arch.regs[VCPU_REGS_RDX] = (rdtsc_fake >> 32) & -1u;

  return skip_emulated_instruction(vcpu);
}
WCharacter commented 2 years ago

Thank you for this fix, i'm updating repository.