aboutgitcodelistschat:MatrixIRC
path: root/cooker/seccomp_profile.c
blob: e75fda2e1dc6e5cce5b7b5980c98cce437fb4001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-License-Identifier: GPL-2.0-or-later

/* seitan - Syscall Expressive Interpreter, Transformer and Notifier
 *
 * cooker/emit.c - Generate gluten (bytecode) instructions and read-only data
 *
 * Copyright 2023 Red Hat GmbH
 * Author: Alice Frosi <afrosi@redhat.com>
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include "seccomp_profile.h"

static struct seccomp scmp_profile;
static unsigned int counter;
static unsigned int count_args;
static JSON_Object *root_obj;
static JSON_Value *root;
static bool ignore_syscall;

const char *scmp_act_str[] = {
	"SCMP_ACT_KILL_THREAD", "SCMP_ACT_TRAP",  "SCMP_ACT_ERRNO",
	"SCMP_ACT_TRACE",	"SCMP_ACT_ALLOW", "SCMP_ACT_LOG",
	"SCMP_ACT_NOTIFY",
};

const char *scmp_op_str[] = {
	"",
	"SCMP_CMP_NE",
	"SCMP_CMP_LT",
	"SCMP_CMP_LE",
	"SCMP_CMP_EQ",
	"SCMP_CMP_GE",
	"SCMP_CMP_GT",
	"SCMP_CMP_MASKED_EQ",
};

const char *arch_str[] = {
	"SCMP_ARCH_NATIVE",   "SCMP_ARCH_X86",	    "SCMP_ARCH_X86_64",
	"SCMP_ARCH_X32",      "SCMP_ARCH_ARM",	    "SCMP_ARCH_AARCH64",
	"SCMP_ARCH_MIPS",     "SCMP_ARCH_MIPS64",   "SCMP_ARCH_MIPS64N32",
	"SCMP_ARCH_MIPSEL",   "SCMP_ARCH_MIPSEL64", "SCMP_ARCH_MIPSEL64N32",
	"SCMP_ARCH_PPC",      "SCMP_ARCH_PPC64",    "SCMP_ARCH_PPC64LE",
	"SCMP_ARCH_S390",     "SCMP_ARCH_S390X",    "SCMP_ARCH_PARISC",
	"SCMP_ARCH_PARISC64", "SCMP_ARCH_RISCV64",
};

const char *scmp_filter_str[] = {
	"SECCOMP_FILTER_FLAG_TSYNC",
	"SECCOMP_FILTER_FLAG_LOG",
	"SECCOMP_FILTER_FLAG_SPEC_ALLOW",
	"SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV",
};

// TODO: decide defaults for when the original profile isn't definied
static void set_defaults_scmp_profile()
{
	scmp_profile.default_action = ACT_ERRNO;
	die("Not implemented yet");
}

static void parse_orig_scmp_profile(char *path)
{
	debug("Use %s as base for the generated seccomp profile", path);
	root = json_parse_file(path);
	if (root == NULL)
		die("  failed parsing JSON seccomp profile: %s", path);
}

/**
 * is_scmp_notify_set - Verify if one of the syscall entries has the SCMP_NOTIFY
 * action enabled
 *
 */
static bool is_scmp_notify_set(JSON_Array *syscalls)
{
	const char *action;
	JSON_Object *obj;
	unsigned int i;

	for (i = 0; i < json_array_get_count(syscalls); i++) {
		if (((obj = json_array_get_object(syscalls, i)) == NULL) ||
		    ((action = json_object_get_string(obj, "action")) == NULL))
			continue;
		if (strcmp(action, scmp_act_str[ACT_NOTIFY]) == 0)
			return true;
	}
	return false;
}

void scmp_profile_init(char *path)
{
	JSON_Array *syscalls;
	counter = 0;
	count_args = 0;

	if (path == NULL) {
		root = json_value_init_object();
		debug("Set defaults for the seccomp profile");
		set_defaults_scmp_profile();
	} else {
		parse_orig_scmp_profile(path);
	}
	if ((root_obj = json_value_get_object(root)) == NULL)
		die("  failed serialize JSON");

	if ((syscalls = json_object_get_array(root_obj, "syscalls")) == NULL)
		return;
	if (is_scmp_notify_set(syscalls))
		die("  the use of multiple seccomp notifiers isn't supported");
}

static bool is_syscall_present(const char *name)
{
	if (name == NULL)
		return false;
	for (unsigned int i = 0; i < counter; i++)
		if (strcmp(scmp_profile.syscalls[i].names, name) == 0)
			return true;

	return false;
}

void scmp_profile_notify(const char *name)
{
	ignore_syscall = false;
	if (is_syscall_present(name)) {
		ignore_syscall = true;
		return;
	}
	debug("  #%u add syscall %s to seccomp profile", counter, name);
	strcpy(scmp_profile.syscalls[counter].names, name);
	scmp_profile.syscalls[counter].action = ACT_NOTIFY;
}

void scmp_profile_add_check(int index, union value v, union value mask,
			    enum op_cmp_type cmp)
{
	char *name = scmp_profile.syscalls[counter].names;
	struct scmp_arg *arg;

	if (count_args > 5)
		die("  too many arguments for the syscall entry %d:%s", counter,
		    name);
	debug("  #%u add arg to syscall %s to seccomp profile", count_args);
	arg = &scmp_profile.syscalls[counter].args[count_args];
	arg->index = index;
	arg->value = v.v_num;
	arg->set = true;
	if (mask.v_num) {
		arg->valueTwo = mask.v_num;
		arg->op = OP_MASKEDEQUAL;
		return;
	}

	// TODO: check if also the other cmp operations are inverted in cooker
	switch (cmp) {
	case CMP_NE:
		arg->op = OP_EQUALTO;
		break;
	case CMP_EQ:
		arg->op = OP_NOTEQUAL;
		break;
	case CMP_LE:
		arg->op = OP_LESSEQUAL;
		break;
	case CMP_LT:
		arg->op = OP_LESSTHAN;
		break;
	case CMP_GE:
		arg->op = OP_GREATEREQUAL;
		break;
	case CMP_GT:
		arg->op = OP_GREATERTHAN;
		break;
	default:
		die("  operation not recognized");
		break;
	}
}

void scmp_profile_flush_args()
{
	if (ignore_syscall)
		return;
	debug("  flush args for syscall %s",
	      scmp_profile.syscalls[counter].names);
	counter++;
	count_args = 0;
}

static void json_append_syscall(JSON_Array *syscalls, struct syscall *syscall)
{
	JSON_Value *val = json_value_init_object();
	JSON_Object *obj = json_value_get_object(val);
	JSON_Value *arg = json_value_init_object();
	JSON_Object *arg_obj = json_value_get_object(arg);
	JSON_Array *names_array;
	JSON_Array *args_array;

	json_object_set_value(obj, "names", json_value_init_array());
	json_object_set_value(obj, "args", json_value_init_array());
	names_array = json_object_get_array(obj, "names");
	;
	args_array = json_object_get_array(obj, "args");
	;
	check_JSON_status(json_object_set_string(obj, "action",
						 scmp_act_str[ACT_NOTIFY]));
	check_JSON_status(
		json_array_append_string(names_array, &syscall->names[0]));
	for (unsigned int i = 0; i < 6; i++) {
		if (!syscall->args[i].set)
			continue;
		check_JSON_status(json_object_set_number(
			arg_obj, "index", syscall->args[i].index));
		check_JSON_status(json_object_set_number(
			arg_obj, "value", syscall->args[i].value));
		check_JSON_status(json_object_set_number(
			arg_obj, "valueTwo", syscall->args[i].valueTwo));
		check_JSON_status(json_object_set_string(
			arg_obj, "op", scmp_op_str[syscall->args[i].op]));
		check_JSON_status(json_array_append_value(args_array, arg));
		debug(" added args for syscall %s %d: ", syscall->names, i,
		      syscall->args[i].value, syscall->args[i].valueTwo,
		      scmp_op_str[syscall->args[i].op]);
	}
	json_array_append_value(syscalls, val);
}

/**
 * remove_existing_syscall() - Remove the syscall entry name from the list where
 * the syscall is listed as allowed and without parameters.
 * Eg. if in the original seccomp profile with the 'connect' syscall:
 *   "syscalls": [
 *                   {
 *                        "names": [ ..
 *	                           "connect",
 *	                           ..
 *                        "action": "SCMP_ACT_ALLOW",
 *                        "args": [],
 *                   }
 *
 */
static void remove_existing_syscall(JSON_Array *syscalls, char *name)
{
	const char *curr_syscall, *action;
	JSON_Array *names, *args;
	unsigned int i, k;
	JSON_Object *obj;

	for (i = 0; i < json_array_get_count(syscalls); i++) {
		if (((obj = json_array_get_object(syscalls, i)) == NULL) ||
		    ((names = json_object_get_array(obj, "names")) == NULL) ||
		    ((args = json_object_get_array(obj, "args")) == NULL) ||
		    ((action = json_object_get_string(obj, "action")) == NULL))
			continue;
		if ((strcmp(action, scmp_act_str[ACT_ALLOW]) != 0) ||
		    (json_array_get_count(args) > 0))
			continue;
		for (k = 0; k < json_array_get_count(names); k++) {
			curr_syscall = json_array_get_string(names, k);
			if (curr_syscall == NULL)
				die("  empty syscall name");
			if (strcmp(curr_syscall, name) == 0) {
				debug("  remove %s as duplicate at %d",
				      curr_syscall, k);
				json_array_remove(names, k);
			}
		}
	}
}

static void json_serialize_syscalls()
{
	JSON_Array *syscalls;
	unsigned int i;

	if ((syscalls = json_object_get_array(root_obj, "syscalls")) == NULL) {
		json_object_set_value(root_obj, "syscalls",
				      json_value_init_array());
		syscalls = json_object_get_array(root_obj, "syscalls");
	}

	for (i = 0; i < counter; i++) {
		remove_existing_syscall(syscalls,
					&scmp_profile.syscalls[i].names[0]);
		// Create syscall entry for the notify
		json_append_syscall(syscalls, &scmp_profile.syscalls[i]);
	}
}

void scmp_profile_write(const char *file)
{
	debug("Write seccomp profile to %s", file);
	json_serialize_syscalls();
	json_serialize_to_file_pretty(root, file);

	json_value_free(root);

	return;
}