mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:17:36 +00:00
Utilities: Enable syscall(1) to use SC_*_params buffers
This commit is contained in:
parent
244c81bcf2
commit
cb9a9a3e03
1 changed files with 89 additions and 26 deletions
|
@ -24,7 +24,10 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Iterator.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <mman.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -33,9 +36,12 @@
|
||||||
#define SC_NARG 4
|
#define SC_NARG 4
|
||||||
|
|
||||||
FlatPtr arg[SC_NARG];
|
FlatPtr arg[SC_NARG];
|
||||||
char buf[BUFSIZ];
|
char outbuf[BUFSIZ];
|
||||||
|
|
||||||
static FlatPtr parse(const char* s);
|
using Arguments = Vector<const char*>;
|
||||||
|
using ArgIter = Arguments::Iterator;
|
||||||
|
|
||||||
|
static FlatPtr parse_from(ArgIter&);
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
@ -46,45 +52,102 @@ int main(int argc, char** argv)
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
|
args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
|
||||||
args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
|
args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
|
||||||
args_parser.add_positional_argument(arguments, "Syscall arguments (can be strings, 'buf' for the output buffer, or numbers like 1234 or 0xffffffff)", "syscall-arguments");
|
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");
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
for (size_t i = 0; i < arguments.size(); i++) {
|
ArgIter iter = arguments.begin();
|
||||||
arg[i] = parse(arguments[i]);
|
for (size_t i = 0; i < SC_NARG && !iter.is_end(); i++) {
|
||||||
|
arg[i] = parse_from(iter);
|
||||||
|
}
|
||||||
|
if (!iter.is_end()) {
|
||||||
|
fprintf(stderr, "Too many arguments (did you want to use '[ parameter buffers ]'?)\n");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (arg[0] > Syscall::Function::__Count) {
|
||||||
for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
|
for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
|
||||||
if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
|
if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
|
||||||
int rc = syscall(sc, arg[1], arg[2], arg[3]);
|
arg[0] = sc;
|
||||||
if (rc == -1) {
|
break;
|
||||||
perror("syscall");
|
|
||||||
} else {
|
|
||||||
if (output_buffer)
|
|
||||||
fwrite(buf, 1, sizeof(buf), stdout);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (arg[0] > Syscall::Function::__Count) {
|
||||||
|
fprintf(stderr, "Invalid syscall entry %s\n", (char*)arg[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int rc = syscall(arg[0], arg[1], arg[2], arg[3]);
|
||||||
|
if (rc == -1)
|
||||||
|
perror("syscall");
|
||||||
|
if (output_buffer)
|
||||||
|
fwrite(outbuf, 1, sizeof(outbuf), stdout);
|
||||||
|
|
||||||
fprintf(stderr, "Syscall return: %d\n", rc);
|
fprintf(stderr, "Syscall return: %d\n", rc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stderr, "Invalid syscall entry %s\n", (char*)arg[0]);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FlatPtr parse(const char* s)
|
static FlatPtr as_buf(Vector<FlatPtr> params_vec)
|
||||||
{
|
{
|
||||||
char* t;
|
size_t params_size = sizeof(FlatPtr) * params_vec.size();
|
||||||
FlatPtr l;
|
size_t buf_size = round_up_to_power_of_two(params_size + 1, PAGE_SIZE);
|
||||||
|
void* buf = mmap(nullptr, buf_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
|
||||||
if (strcmp(s, "buf") == 0) {
|
if (buf == MAP_FAILED) {
|
||||||
|
fprintf(stderr, "Warning: Could not allocate buffer of size %zu (low memory?)\n", buf_size);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
// It's probably good to ensure zero-initialization.
|
||||||
|
memset(buf, 0, buf_size);
|
||||||
|
memcpy(buf, params_vec.data(), params_size);
|
||||||
|
// Leak the buffer here. We need to keep it until the special syscall happens,
|
||||||
|
// and we terminate immediately afterwards anyway.
|
||||||
return (FlatPtr)buf;
|
return (FlatPtr)buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FlatPtr parse_parameter_buffer(ArgIter& iter)
|
||||||
|
{
|
||||||
|
Vector<FlatPtr> params_vec;
|
||||||
|
while (!iter.is_end()) {
|
||||||
|
if (strcmp(*iter, "]") == 0) {
|
||||||
|
++iter;
|
||||||
|
return as_buf(params_vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
l = strtoul(s, &t, 0);
|
params_vec.append(parse_from(iter));
|
||||||
if (t > s && *t == 0) {
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
|
||||||
|
exit(1);
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
static FlatPtr parse_from(ArgIter& iter)
|
||||||
|
{
|
||||||
|
const char* this_arg = *iter;
|
||||||
|
++iter;
|
||||||
|
|
||||||
|
// Is it a forced literal?
|
||||||
|
if (this_arg[0] == ',')
|
||||||
|
return (FlatPtr)(this_arg + 1);
|
||||||
|
|
||||||
|
// Is it the output buffer?
|
||||||
|
if (strcmp(this_arg, "buf") == 0)
|
||||||
|
return (FlatPtr)outbuf;
|
||||||
|
|
||||||
|
// Is it a parameter buffer?
|
||||||
|
if (strcmp(this_arg, "[") == 0)
|
||||||
|
return parse_parameter_buffer(iter);
|
||||||
|
|
||||||
|
// Is it a number?
|
||||||
|
char* endptr = nullptr;
|
||||||
|
FlatPtr l = strtoul(this_arg, &endptr, 0);
|
||||||
|
if (*endptr == 0) {
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (FlatPtr)s;
|
// Then it must be a string:
|
||||||
|
if (strcmp(this_arg, "]") == 0)
|
||||||
|
fprintf(stderr, "Warning: Treating unmatched ']' as literal string\n");
|
||||||
|
|
||||||
|
return (FlatPtr)this_arg;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue