syscall.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/Iterator.h>
  28. #include <AK/LogStream.h>
  29. #include <AK/Vector.h>
  30. #include <LibCore/ArgsParser.h>
  31. #include <mman.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <syscall.h>
  36. #define SC_NARG 4
  37. FlatPtr arg[SC_NARG];
  38. char outbuf[BUFSIZ];
  39. using Arguments = Vector<const char*>;
  40. using ArgIter = Arguments::Iterator;
  41. static FlatPtr parse_from(ArgIter&);
  42. int main(int argc, char** argv)
  43. {
  44. bool output_buffer = false;
  45. bool list_syscalls = false;
  46. Vector<const char*> arguments;
  47. Core::ArgsParser args_parser;
  48. args_parser.set_general_help(
  49. "Enables you to do a direct syscall, even those that use a 'SC_*_params' buffer.\n"
  50. "Arguments can be literal strings, numbers, the output buffer, or parameter buffers:\n"
  51. " - Arguments that begin with a comma are stripped of the comma and treated as string arguments, for example ',0x0' or ',['.\n"
  52. " - 'buf' is replaced by a pointer to the output buffer.\n"
  53. " - Numbers can be written like 1234 or 0xDEADC0DE.\n"
  54. " - 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"
  55. " - The first argument may also be any syscall function name. Run 'syscall -l' to see the list.\n"
  56. " - Arguments that cannot be interpreted are treated as string arguments, for example 'Hello, friends!'.\n"
  57. "\n"
  58. "Full example: syscall -o realpath [ /usr/share/man/man2/getgid.md 1024 buf 1024 ]");
  59. args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
  60. args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
  61. args_parser.add_positional_argument(arguments, "Syscall arguments; see general help.", "syscall-arguments");
  62. args_parser.parse(argc, argv);
  63. ArgIter iter = arguments.begin();
  64. for (size_t i = 0; i < SC_NARG && !iter.is_end(); i++) {
  65. arg[i] = parse_from(iter);
  66. }
  67. if (!iter.is_end()) {
  68. warnln("Too many arguments (did you want to use '[ parameter buffers ]'?)");
  69. return -1;
  70. }
  71. if (arg[0] > Syscall::Function::__Count) {
  72. for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
  73. if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
  74. arg[0] = sc;
  75. break;
  76. }
  77. }
  78. if (arg[0] > Syscall::Function::__Count) {
  79. warnln("Invalid syscall entry {}", (char*)arg[0]);
  80. return -1;
  81. }
  82. }
  83. dbgln_if(SYSCALL_1_DEBUG, "Calling {} {:p} {:p} {:p}\n", arg[0], arg[1], arg[2], arg[3]);
  84. int rc = syscall(arg[0], arg[1], arg[2], arg[3]);
  85. if (rc == -1)
  86. perror("syscall");
  87. if (output_buffer)
  88. fwrite(outbuf, 1, sizeof(outbuf), stdout);
  89. warnln("Syscall return: {}", rc);
  90. return 0;
  91. }
  92. static FlatPtr as_buf(Vector<FlatPtr> params_vec)
  93. {
  94. size_t params_size = sizeof(FlatPtr) * params_vec.size();
  95. size_t buf_size = round_up_to_power_of_two(params_size + 1, PAGE_SIZE);
  96. void* buf = mmap(nullptr, buf_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  97. if (buf == MAP_FAILED) {
  98. fprintf(stderr, "Warning: Could not allocate buffer of size %zu (low memory?)\n", buf_size);
  99. exit(1);
  100. }
  101. // It's probably good to ensure zero-initialization.
  102. memset(buf, 0, buf_size);
  103. memcpy(buf, params_vec.data(), params_size);
  104. if constexpr (SYSCALL_1_DEBUG) {
  105. StringBuilder builder;
  106. builder.append("Prepared [");
  107. for (size_t i = 0; i < params_vec.size(); ++i) {
  108. builder.appendff(" {:p}", params_vec[i]);
  109. }
  110. builder.appendff(" ] at {:p}", (FlatPtr)buf);
  111. dbgln(builder.to_string());
  112. }
  113. // Leak the buffer here. We need to keep it until the special syscall happens,
  114. // and we terminate immediately afterwards anyway.
  115. return (FlatPtr)buf;
  116. }
  117. static FlatPtr parse_parameter_buffer(ArgIter& iter)
  118. {
  119. Vector<FlatPtr> params_vec;
  120. while (!iter.is_end()) {
  121. if (strcmp(*iter, "]") == 0) {
  122. ++iter;
  123. return as_buf(params_vec);
  124. }
  125. params_vec.append(parse_from(iter));
  126. }
  127. fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
  128. exit(1);
  129. ASSERT_NOT_REACHED();
  130. }
  131. static FlatPtr parse_from(ArgIter& iter)
  132. {
  133. const char* this_arg = *iter;
  134. ++iter;
  135. // Is it a forced literal?
  136. if (this_arg[0] == ',') {
  137. this_arg += 1;
  138. dbgln_if(SYSCALL_1_DEBUG, "Using (forced) string >>{}<< at {:p}", this_arg, (FlatPtr)this_arg);
  139. return (FlatPtr)this_arg;
  140. }
  141. // Is it the output buffer?
  142. if (strcmp(this_arg, "buf") == 0)
  143. return (FlatPtr)outbuf;
  144. // Is it a parameter buffer?
  145. if (strcmp(this_arg, "[") == 0)
  146. return parse_parameter_buffer(iter);
  147. // Is it a number?
  148. char* endptr = nullptr;
  149. FlatPtr l = strtoul(this_arg, &endptr, 0);
  150. if (*endptr == 0) {
  151. return l;
  152. }
  153. // Then it must be a string:
  154. if (strcmp(this_arg, "]") == 0)
  155. fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
  156. dbgln_if(SYSCALL_1_DEBUG, "Using (detected) string >>{}<< at {:p}", this_arg, (FlatPtr)this_arg);
  157. return (FlatPtr)this_arg;
  158. }