david942j / kvm-kernel-example Goto Github PK
View Code? Open in Web Editor NEWExamples for: Learning KVM - implement your own kernel
Examples for: Learning KVM - implement your own kernel
Many thanks for making this code available. I have one doubt on the working of the code.
In the function setup_regs in hypervisor.c, shouldn't the value of RSP be set to MAX_KERNEL_SIZE + 0x5000 + KERNEL_STACK_SIZE instead of MAX_KERNEL_SIZE +KERNEL_STACK_SIZE? My understanding of the memory layout of the guest is that you place the kernel, then the page table, and then the kernel stack. With the current setting of RSP, the stack is writing to the same page that is used for the page tables. Of course, the code works correctly even now, because the page table and the kernel stack are both not using the full page. But, if they did, it would cause issues.
Please let me know if my understanding is correct.
I tried to add exception/interruput handler based on this but fail. The VM exit with "shutdown" reason.
I realize there is not gdt setup in the orignal code. But even after I add it, it still doesn't work.
Could you please let me know what I need to do beside setup gdt and idt?
The functions that do the hypercall for read
and write
does check whether if the beginning of the buffer is valid memory, but the buffer could extend into memory that is not valid.
If the user is able to gain control of the kernel, they can at the very least leak memory that is mmap
'd right before the the guest memory segment. If, however, the mmap'd segment is also writable, the user can also write to that memory segment, and compromise the hypervisor, by passing data as stdin, and invoking a hp read call.
Any idea? :3
alon @ ~/kvm-test/kvm-kernel-example - [master] $ sudo make
make -C hypervisor
make[1]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/hypervisor'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/hypervisor'
make -C kernel
make[1]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel'
make -C syscalls/ syscalls.a
make[2]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel/syscalls'
make[2]: 'syscalls.a' is up to date.
make[2]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel/syscalls'
make -C hypercalls/ hypercalls.a
make[2]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel/hypercalls'
make[2]: 'hypercalls.a' is up to date.
make[2]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel/hypercalls'
make -C mm/ mm.a
make[2]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel/mm'
make[2]: 'mm.a' is up to date.
make[2]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel/mm'
make -C elf/ elf.a
make[2]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel/elf'
make[2]: 'elf.a' is up to date.
make[2]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel/elf'
make -C utils/ utils.a
make[2]: Entering directory '/home/alon/kvm-test/kvm-kernel-example/kernel/utils'
cc -nostdlib -Os -Wall -Werror -fPIE -pie -masm=intel -I.. -c -MMD -MP string.c
string.c: In function ‘strlen’:
string.c:6:10: error: ‘strnlen’ specified bound 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Werror=stringop-overflow=]
6 | return strnlen(s, ~0ll);
| ^~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [../template.mk:16: string.o] Error 1
make[2]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel/utils'
make[1]: *** [Makefile:24: utils/utils.a] Error 2
make[1]: Leaving directory '/home/alon/kvm-test/kvm-kernel-example/kernel'
make: *** [Makefile:6: kernel] Error 2
I was seeing the following errors when compiling on Ubuntu 22.04:
as syscall_entry.s -o syscall_entry.o
syscall_entry.s: Assembler messages:
syscall_entry.s:36: Error: ambiguous operand size for `sysret'
cc -nostdlib -Os -Wall -Werror -fPIE -pie -masm=intel -I.. -c -MMD -MP sys_execve.c
syscall_entry.s: Assembler messages:
syscall_entry.s:36: Error: ambiguous operand size for `sysret'
make[2]: *** [../template.mk:19: syscall_entry.o] Error 1
make[2]: *** Waiting for unfinished jobs....
sys_execve.c: Assembler messages:
sys_execve.c:123: Error: ambiguous operand size for `sysret'
I changed sysret
to sysretq
in both files and it worked.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.