From ef403b6687ec5739933f9f215093e1ea5c923666 Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Wed, 17 May 2023 15:46:57 +0200 Subject: cooker, seitan: simple working example parser: - add OP_BLOCK and OP_RETURN to the parser seitan: - fix op_cmp in seitan, it was jmp when comparison was true Working example: demo.json: [ { "match": [ /* qemu-pr-helper and similar */ { "connect": { "addr": { "family": "unix", "path": "/tmp/test.sock" } } } ], "return": 0 } ] Create gluten and the bpf filter: $ seitan-cooker demo.hjson demo.gluten demo.bpf Launch the seitan eater with the target program: $ seitan-eater -i demo.bpf -- strace -e connect tests-utils/test-syscalls connect Start seitan with gluten: $ seitan -i demo.gluten -p $(pgrep seitan-eater) Seitan mocks the connect syscall and `connect` returns successfully: $ seitan-eater -i demo.bpf -- strace -e connect tests-utils/test-syscalls connect Test syscall: connect connect(4, {sa_family=AF_UNIX, sun_path="/tmp/test.sock"}, 108) = 0 --- cooker/emit.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'cooker/emit.c') diff --git a/cooker/emit.c b/cooker/emit.c index c233b0a..3fcddba 100644 --- a/cooker/emit.c +++ b/cooker/emit.c @@ -6,6 +6,7 @@ * * Copyright 2023 Red Hat GmbH * Author: Stefano Brivio + * Alice Frosi */ #include "cooker.h" @@ -136,6 +137,43 @@ void emit_cmp_field(struct gluten_ctx *g, enum op_cmp_type cmp, jmp); } +/** + * emit_return() - Emit OP_RETURN instruction: return value + * @g: gluten context + * @v: Pointer to return value + */ +void emit_return(struct gluten_ctx *g, struct gluten_offset v) +{ + struct op *op = (struct op *)gluten_ptr(&g->g, g->ip); + struct op_return *ret = &op->op.ret; + + op->type = OP_RETURN; + ret->val = v; + + debug(" %i: OP_RETURN:", g->ip.offset); + + if (++g->ip.offset > INST_MAX) + die("Too many instructions"); +} +/** + * emit_block() - Emit OP_BLOCK instruction: return error value + * @g: gluten context + * @error: Error value + */ +void emit_block(struct gluten_ctx *g, int32_t error) +{ + struct op *op = (struct op *)gluten_ptr(&g->g, g->ip); + struct op_block *block = &op->op.block; + + op->type = OP_BLOCK; + block->error = error; + + debug(" %i: OP_BLOCK: %d", g->ip.offset, error); + + if (++g->ip.offset > INST_MAX) + die("Too many instructions"); +} + struct gluten_offset emit_data(struct gluten_ctx *g, enum type type, size_t str_len, union value *value) { @@ -153,6 +191,16 @@ struct gluten_offset emit_data(struct gluten_ctx *g, enum type type, g->cp.offset += sizeof(int); break; + case U64: + if (g->cp.offset + sizeof(uint64_t) > RO_DATA_SIZE) + die(" Read-only data section exceeded"); + + *(uint64_t *)p = value->v_u64; + debug(" C#%i: (%s) %i", g->cp.offset, type_str[type], + value->v_u64); + + g->cp.offset += sizeof(uint64_t); + break; case STRING: if (g->cp.offset + str_len > RO_DATA_SIZE) die(" Read-only data section exceeded"); -- cgit v1.2.3