syscall.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Debug.h>
  8. #include <AK/Iterator.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <syscall.h>
  16. #define SC_NARG 4
  17. FlatPtr arg[SC_NARG];
  18. char outbuf[BUFSIZ];
  19. using Arguments = Vector<const char*>;
  20. using ArgIter = Arguments::Iterator;
  21. static FlatPtr parse_from(ArgIter&);
  22. template<>
  23. struct AK::Formatter<Syscall::Function> : Formatter<StringView> {
  24. void format(FormatBuilder& builder, Syscall::Function function)
  25. {
  26. return Formatter<StringView>::format(builder, to_string(function));
  27. }
  28. };
  29. int main(int argc, char** argv)
  30. {
  31. bool output_buffer = false;
  32. bool list_syscalls = false;
  33. Vector<const char*> arguments;
  34. Core::ArgsParser args_parser;
  35. args_parser.set_general_help(
  36. "Enables you to do a direct syscall, even those that use a 'SC_*_params' buffer.\n"
  37. "Arguments can be literal strings, numbers, the output buffer, or parameter buffers:\n"
  38. " - Arguments that begin with a comma are stripped of the comma and treated as string arguments, for example ',0x0' or ',['.\n"
  39. " - 'buf' is replaced by a pointer to the output buffer.\n"
  40. " - Numbers can be written like 1234 or 0xDEADC0DE.\n"
  41. " - Parameter buffer (e.g. SC_realpath_params) can be passed by wrapping them in '[' and ']'. Note that '[' and ']' must be separate arguments to syscall(1). Buffers can be used recursively.\n"
  42. " - The first argument may also be any syscall function name. Run 'syscall -l' to see the list.\n"
  43. " - Arguments that cannot be interpreted are treated as string arguments, for example 'Hello, friends!'.\n"
  44. "\n"
  45. "Full example: syscall -o realpath [ /usr/share/man/man2/getgid.md 1024 buf 1024 ]");
  46. args_parser.add_option(list_syscalls, "List all existing syscalls, and exit", "list-syscalls", 'l');
  47. args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
  48. args_parser.add_positional_argument(arguments, "Syscall arguments; see general help.", "syscall-arguments", Core::ArgsParser::Required::No);
  49. args_parser.parse(argc, argv);
  50. if (list_syscalls) {
  51. outln("syscall list:");
  52. for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
  53. outln(" \033[33;1m{}\033[0m - {}", sc, static_cast<Syscall::Function>(sc));
  54. }
  55. exit(0);
  56. }
  57. if (arguments.is_empty()) {
  58. args_parser.print_usage(stderr, argv[0]);
  59. exit(1);
  60. }
  61. ArgIter iter = arguments.begin();
  62. for (size_t i = 0; i < SC_NARG && !iter.is_end(); i++) {
  63. arg[i] = parse_from(iter);
  64. }
  65. if (!iter.is_end()) {
  66. warnln("Too many arguments (did you want to use '[ parameter buffers ]'?)");
  67. return -1;
  68. }
  69. if (arg[0] > Syscall::Function::__Count) {
  70. for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
  71. if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
  72. arg[0] = sc;
  73. break;
  74. }
  75. }
  76. if (arg[0] > Syscall::Function::__Count) {
  77. warnln("Invalid syscall entry {}", (char*)arg[0]);
  78. return -1;
  79. }
  80. }
  81. dbgln_if(SYSCALL_1_DEBUG, "Calling {} {:p} {:p} {:p}\n", arg[0], arg[1], arg[2], arg[3]);
  82. int rc = syscall(arg[0], arg[1], arg[2], arg[3]);
  83. if (rc == -1)
  84. perror("syscall");
  85. if (output_buffer)
  86. fwrite(outbuf, 1, sizeof(outbuf), stdout);
  87. warnln("Syscall return: {}", rc);
  88. return 0;
  89. }
  90. static FlatPtr as_buf(Vector<FlatPtr> params_vec)
  91. {
  92. size_t params_size = sizeof(FlatPtr) * params_vec.size();
  93. size_t buf_size = round_up_to_power_of_two(params_size + 1, PAGE_SIZE);
  94. void* buf = mmap(nullptr, buf_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  95. if (buf == MAP_FAILED) {
  96. fprintf(stderr, "Warning: Could not allocate buffer of size %zu (low memory?)\n", buf_size);
  97. exit(1);
  98. }
  99. // It's probably good to ensure zero-initialization.
  100. memset(buf, 0, buf_size);
  101. memcpy(buf, params_vec.data(), params_size);
  102. if constexpr (SYSCALL_1_DEBUG) {
  103. StringBuilder builder;
  104. builder.append("Prepared [");
  105. for (size_t i = 0; i < params_vec.size(); ++i) {
  106. builder.appendff(" {:p}", params_vec[i]);
  107. }
  108. builder.appendff(" ] at {:p}", (FlatPtr)buf);
  109. dbgln("{}", builder.to_string());
  110. }
  111. // Leak the buffer here. We need to keep it until the special syscall happens,
  112. // and we terminate immediately afterwards anyway.
  113. return (FlatPtr)buf;
  114. }
  115. static FlatPtr parse_parameter_buffer(ArgIter& iter)
  116. {
  117. Vector<FlatPtr> params_vec;
  118. while (!iter.is_end()) {
  119. if (strcmp(*iter, "]") == 0) {
  120. ++iter;
  121. return as_buf(params_vec);
  122. }
  123. params_vec.append(parse_from(iter));
  124. }
  125. fprintf(stderr, "Error: Unmatched '['?!\n");
  126. exit(1);
  127. VERIFY_NOT_REACHED();
  128. }
  129. static FlatPtr parse_from(ArgIter& iter)
  130. {
  131. const char* this_arg = *iter;
  132. ++iter;
  133. // Is it a forced literal?
  134. if (this_arg[0] == ',') {
  135. this_arg += 1;
  136. dbgln_if(SYSCALL_1_DEBUG, "Using (forced) string >>{}<< at {:p}", this_arg, (FlatPtr)this_arg);
  137. return (FlatPtr)this_arg;
  138. }
  139. // Is it the output buffer?
  140. if (strcmp(this_arg, "buf") == 0)
  141. return (FlatPtr)outbuf;
  142. // Is it a parameter buffer?
  143. if (strcmp(this_arg, "[") == 0)
  144. return parse_parameter_buffer(iter);
  145. // Is it a number?
  146. char* endptr = nullptr;
  147. FlatPtr l = strtoul(this_arg, &endptr, 0);
  148. if (*endptr == 0) {
  149. return l;
  150. }
  151. // Then it must be a string:
  152. if (strcmp(this_arg, "]") == 0)
  153. fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
  154. dbgln_if(SYSCALL_1_DEBUG, "Using (detected) string >>{}<< at {:p}", this_arg, (FlatPtr)this_arg);
  155. return (FlatPtr)this_arg;
  156. }