init.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/mount.h>
  5. #include <sys/stat.h>
  6. #include <sys/wait.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include "../vendor/verify/sha256.h"
  10. #ifndef ARK_KEY
  11. #define ARK_KEY "UNKNOWN_KEY"
  12. #endif
  13. void trigger_kernel_panic(const char *msg) {
  14. printf("\n======================================================\n");
  15. printf(" KERNEL PANIC\n");
  16. printf(" Please reboot your computer.\n");
  17. printf("======================================================\n");
  18. printf("VFS: Unable to mount root fs on unknown-block(0,0)\n");
  19. printf("ArkOS Verified Boot: %s\n", msg);
  20. while (1) {
  21. sleep(1);
  22. }
  23. }
  24. int verify_os_signature() {
  25. // Read signature.bin
  26. int sig_fd = open("/signature.bin", O_RDONLY);
  27. if (sig_fd < 0) {
  28. trigger_kernel_panic("Missing signature.bin!");
  29. return 0;
  30. }
  31. char expected_sig[65] = {0};
  32. read(sig_fd, expected_sig, 64);
  33. close(sig_fd);
  34. // Read arkrt
  35. int os_fd = open("/arkrt", O_RDONLY);
  36. if (os_fd < 0) {
  37. trigger_kernel_panic("Missing /arkrt OS binary!");
  38. return 0;
  39. }
  40. struct stat st;
  41. fstat(os_fd, &st);
  42. uint8_t *os_data = malloc(st.st_size);
  43. if (!os_data) {
  44. trigger_kernel_panic("Out of memory during verification!");
  45. return 0;
  46. }
  47. // Read entire binary
  48. size_t total_read = 0;
  49. while (total_read < st.st_size) {
  50. ssize_t r = read(os_fd, os_data + total_read, st.st_size - total_read);
  51. if (r <= 0) break;
  52. total_read += r;
  53. }
  54. close(os_fd);
  55. // Compute SHA256(KEY + OS_DATA)
  56. SHA256_CTX ctx;
  57. sha256_init(&ctx);
  58. sha256_update(&ctx, (const uint8_t*)ARK_KEY, strlen(ARK_KEY));
  59. sha256_update(&ctx, os_data, st.st_size);
  60. uint8_t hash[32];
  61. sha256_final(&ctx, hash);
  62. free(os_data);
  63. // Convert hash to hex string
  64. char computed_sig[65] = {0};
  65. for (int i = 0; i < 32; i++) {
  66. sprintf(&computed_sig[i*2], "%02x", hash[i]);
  67. }
  68. if (strncmp(expected_sig, computed_sig, 64) != 0) {
  69. printf("Expected Signature: %s\n", expected_sig);
  70. printf("Computed Signature: %s\n", computed_sig);
  71. trigger_kernel_panic("Signature mismatch! System compromised or invalid key.");
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. int main() {
  77. // Mount essential filesystems
  78. mkdir("/dev", 0755);
  79. mkdir("/proc", 0755);
  80. mkdir("/sys", 0755);
  81. mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);
  82. mount("proc", "/proc", "proc", 0, NULL);
  83. mount("sysfs", "/sys", "sysfs", 0, NULL);
  84. // Set up standard file descriptors
  85. int fd = open("/dev/console", O_RDWR);
  86. if (fd >= 0) {
  87. dup2(fd, 0);
  88. dup2(fd, 1);
  89. dup2(fd, 2);
  90. if (fd > 2) close(fd);
  91. }
  92. // Force black background, clear screen, hide cursor (prevents white flash)
  93. printf("\033[0;40m\033[2J\033[H\033[?25l");
  94. fflush(stdout);
  95. // VERIFIED BOOT CHECK
  96. verify_os_signature();
  97. // Launch the swift application
  98. pid_t pid = fork();
  99. if (pid == 0) {
  100. char *argv[] = { "/arkrt", NULL };
  101. char *envp[] = { "PATH=/bin:/usr/bin:/sbin", NULL };
  102. execve("/arkrt", argv, envp);
  103. printf("Execve failed!\n");
  104. exit(1);
  105. }
  106. // PID 1 must never exit
  107. int status;
  108. waitpid(pid, &status, 0);
  109. printf("ArkOS Service Manager (arkrt) exited. Hanging system to prevent kernel panic...\n");
  110. while(1) sleep(1);
  111. return 0;
  112. }