1

Yes I know, that there are some similar questions around, but none of them are satisfying

I know that it is a stupid idea, but I need to enter into kernel-mode (aka Ring 0) with my Visual Studio 2015 C++-Project.
I also want to do it with the minimal effort necessary (meaning, that I do not want to create a driver specifically for testing and having to sign and redeploy after every build as this is very tedious).

How can I achieve this?

It does not matter to me, whether the project is run on my host machine or on a remote one (or virtual one) -- I have enough machines at my disposal.


Background: I am currently working on the Cosmos operating system and I need to test X86-assembly instructions which need Ring 0 "privilege", e.g. rdmsr, out, in etc.
Running the following code will break on the 8th line with an 0xC0000096: Privileged instruction.-Error:
int* ptr = new int[4];
int* va = ptr;

__asm
{
    lea esi, va
    mov ecx, 0xe7
    rdmsr                //error, as this must run in ring0
    mov [esi + 4], eax
    mov [esi], edx
    mov ecx, 0xe8
    rdmsr
    mov [esi + 12], eax
    mov [esi + 8], edx
    xor eax, eax
}

....

And yes, I am fully aware of any risk I am taking, so do please not ask, why I would need to do such a thing or whether I am trying to get the programmer's darwin award ;)

unknown6656
  • 2,765
  • 2
  • 36
  • 52
  • If you can enter ring 0 without using a device driver, then there is a major security hole in the operating system that needs to be fixed. – jcoder Sep 19 '16 at 12:09
  • @jcoder: That is a very good point, however I would like to avoid the trouble of writing and deploying device drivers ;) – unknown6656 Sep 19 '16 at 12:11
  • 3
    You could perhaps write a driver that enters ring 0 and then calls code passed to it. Highly insecure... but you'd only have to install the driver once. – jcoder Sep 19 '16 at 12:17
  • @jcoder: That is a good idea .... I will try to write that – unknown6656 Sep 19 '16 at 12:19
  • Kernel-mode drivers (.sys files) are kind of a pain to get working. Even after you succeeed in getting the compiler to build it, a 64-bit system will need you to sign the driver (you will probably use a certificate you create yourself and add it to Windows as a trusted certificate), and even after that, you will need to start up Windows in a special "developer mode" which puts a watermark on the desktop, that is unless you have access to a certificate from an entity trusted by Microsoft. After you get all that going, jcoder's idea sounds good to me. – Christopher Oicles Sep 20 '16 at 00:38
  • @ChristopherOicles: Writing an actual driver is not the main problem for me .... I did it once before (it was on Win7, not on Win10, but that does hardly matter) ..... the problem is, as you say, it is a real pain in the a**, which I want to avoid :P – unknown6656 Sep 20 '16 at 07:27
  • In your shoes I would be tempted to just set up a DOS boot, maybe on a USB drive and install Watcom with DOS/4GW from the old days (as long as you can get by with 32-bit code). It could be that some open source movement even maintains and improves support for DOS tools that could help you out, but I have no current knowledge about a DOS "State of the Art". – Christopher Oicles Sep 20 '16 at 07:46

1 Answers1

1

AFAIK Visual Studio cannot debug kernel code, but there are other debuggers that can: WinDbg and KD. You'll need some time to figure them out, but there's no other way.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103