syscall.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/Iterator.h>
  27. #include <AK/Vector.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <mman.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <syscall.h>
  34. #define SC_NARG 4
  35. FlatPtr arg[SC_NARG];
  36. char outbuf[BUFSIZ];
  37. using Arguments = Vector<const char*>;
  38. using ArgIter = Arguments::Iterator;
  39. static FlatPtr parse_from(ArgIter&);
  40. int main(int argc, char** argv)
  41. {
  42. bool output_buffer = false;
  43. bool list_syscalls = false;
  44. Vector<const char*> arguments;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
  47. args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
  48. args_parser.add_positional_argument(arguments, "Syscall arguments; can be a string, 'buf' for the output buffer, or numbers like 1234 or 0xffffffff, or a buffer that must begin with '[' and end with ']'. If the first character is ',' (comma), the argument is interpreted as a string, no matter what. This is useful if the string is '[' or '0x0'.", "syscall-arguments");
  49. args_parser.parse(argc, argv);
  50. ArgIter iter = arguments.begin();
  51. for (size_t i = 0; i < SC_NARG && !iter.is_end(); i++) {
  52. arg[i] = parse_from(iter);
  53. }
  54. if (!iter.is_end()) {
  55. fprintf(stderr, "Too many arguments (did you want to use '[ parameter buffers ]'?)\n");
  56. return -1;
  57. }
  58. if (arg[0] > Syscall::Function::__Count) {
  59. for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
  60. if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
  61. arg[0] = sc;
  62. break;
  63. }
  64. }
  65. if (arg[0] > Syscall::Function::__Count) {
  66. fprintf(stderr, "Invalid syscall entry %s\n", (char*)arg[0]);
  67. return -1;
  68. }
  69. }
  70. int rc = syscall(arg[0], arg[1], arg[2], arg[3]);
  71. if (rc == -1)
  72. perror("syscall");
  73. if (output_buffer)
  74. fwrite(outbuf, 1, sizeof(outbuf), stdout);
  75. fprintf(stderr, "Syscall return: %d\n", rc);
  76. return 0;
  77. }
  78. static FlatPtr as_buf(Vector<FlatPtr> params_vec)
  79. {
  80. size_t params_size = sizeof(FlatPtr) * params_vec.size();
  81. size_t buf_size = round_up_to_power_of_two(params_size + 1, PAGE_SIZE);
  82. void* buf = mmap(nullptr, buf_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  83. if (buf == MAP_FAILED) {
  84. fprintf(stderr, "Warning: Could not allocate buffer of size %zu (low memory?)\n", buf_size);
  85. exit(1);
  86. }
  87. // It's probably good to ensure zero-initialization.
  88. memset(buf, 0, buf_size);
  89. memcpy(buf, params_vec.data(), params_size);
  90. // Leak the buffer here. We need to keep it until the special syscall happens,
  91. // and we terminate immediately afterwards anyway.
  92. return (FlatPtr)buf;
  93. }
  94. static FlatPtr parse_parameter_buffer(ArgIter& iter)
  95. {
  96. Vector<FlatPtr> params_vec;
  97. while (!iter.is_end()) {
  98. if (strcmp(*iter, "]") == 0) {
  99. ++iter;
  100. return as_buf(params_vec);
  101. }
  102. params_vec.append(parse_from(iter));
  103. }
  104. fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
  105. exit(1);
  106. ASSERT_NOT_REACHED();
  107. }
  108. static FlatPtr parse_from(ArgIter& iter)
  109. {
  110. const char* this_arg = *iter;
  111. ++iter;
  112. // Is it a forced literal?
  113. if (this_arg[0] == ',')
  114. return (FlatPtr)(this_arg + 1);
  115. // Is it the output buffer?
  116. if (strcmp(this_arg, "buf") == 0)
  117. return (FlatPtr)outbuf;
  118. // Is it a parameter buffer?
  119. if (strcmp(this_arg, "[") == 0)
  120. return parse_parameter_buffer(iter);
  121. // Is it a number?
  122. char* endptr = nullptr;
  123. FlatPtr l = strtoul(this_arg, &endptr, 0);
  124. if (*endptr == 0) {
  125. return l;
  126. }
  127. // Then it must be a string:
  128. if (strcmp(this_arg, "]") == 0)
  129. fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
  130. return (FlatPtr)this_arg;
  131. }