syscall.cpp 6.1 KB

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