summaryrefslogtreecommitdiff
path: root/security/ipe/policy_tests.c
blob: 89521f6b99945ef74e99540f640f94661004d77d (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
 */

#include <linux/slab.h>
#include <linux/types.h>
#include <linux/list.h>
#include <kunit/test.h>
#include "policy.h"
struct policy_case {
	const char *const policy;
	int errno;
	const char *const desc;
};

static const struct policy_case policy_cases[] = {
	{
		"policy_name=allowall policy_version=0.0.0\n"
		"DEFAULT action=ALLOW",
		0,
		"basic",
	},
	{
		"policy_name=trailing_comment policy_version=152.0.0 #This is comment\n"
		"DEFAULT action=ALLOW",
		0,
		"trailing comment",
	},
	{
		"policy_name=allowallnewline policy_version=0.2.0\n"
		"DEFAULT action=ALLOW\n"
		"\n",
		0,
		"trailing newline",
	},
	{
		"policy_name=carriagereturnlinefeed policy_version=0.0.1\n"
		"DEFAULT action=ALLOW\n"
		"\r\n",
		0,
		"clrf newline",
	},
	{
		"policy_name=whitespace policy_version=0.0.0\n"
		"DEFAULT\taction=ALLOW\n"
		"     \t     DEFAULT \t    op=EXECUTE      action=DENY\n"
		"op=EXECUTE boot_verified=TRUE action=ALLOW\n"
		"# this is a\tcomment\t\t\t\t\n"
		"DEFAULT \t op=KMODULE\t\t\t  action=DENY\r\n"
		"op=KMODULE boot_verified=TRUE action=ALLOW\n",
		0,
		"various whitespaces and nested default",
	},
	{
		"policy_name=boot_verified policy_version=-1236.0.0\n"
		"DEFAULT\taction=ALLOW\n",
		-EINVAL,
		"negative version",
	},
	{
		"policy_name=$@!*&^%%\\:;{}() policy_version=0.0.0\n"
		"DEFAULT action=ALLOW",
		0,
		"special characters",
	},
	{
		"policy_name=test policy_version=999999.0.0\n"
		"DEFAULT action=ALLOW",
		-ERANGE,
		"overflow version",
	},
	{
		"policy_name=test policy_version=255.0\n"
		"DEFAULT action=ALLOW",
		-EBADMSG,
		"incomplete version",
	},
	{
		"policy_name=test policy_version=111.0.0.0\n"
		"DEFAULT action=ALLOW",
		-EBADMSG,
		"extra version",
	},
	{
		"",
		-EBADMSG,
		"0-length policy",
	},
	{
		"policy_name=test\0policy_version=0.0.0\n"
		"DEFAULT action=ALLOW",
		-EBADMSG,
		"random null in header",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"\0DEFAULT action=ALLOW",
		-EBADMSG,
		"incomplete policy from NULL",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=DENY\n\0"
		"op=EXECUTE dmverity_signature=TRUE action=ALLOW\n",
		0,
		"NULL truncates policy",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE dmverity_signature=abc action=ALLOW",
		-EBADMSG,
		"invalid property type",
	},
	{
		"DEFAULT action=ALLOW",
		-EBADMSG,
		"missing policy header",
	},
	{
		"policy_name=test policy_version=0.0.0\n",
		-EBADMSG,
		"missing default definition",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"dmverity_signature=TRUE op=EXECUTE action=ALLOW",
		-EBADMSG,
		"invalid rule ordering"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"action=ALLOW op=EXECUTE dmverity_signature=TRUE",
		-EBADMSG,
		"invalid rule ordering (2)",
	},
	{
		"policy_name=test policy_version=0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE dmverity_signature=TRUE action=ALLOW",
		-EBADMSG,
		"invalid version",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=UNKNOWN dmverity_signature=TRUE action=ALLOW",
		-EBADMSG,
		"unknown operation",
	},
	{
		"policy_name=asdvpolicy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n",
		-EBADMSG,
		"missing space after policy name",
	},
	{
		"policy_name=test\xFF\xEF policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE dmverity_signature=TRUE action=ALLOW",
		0,
		"expanded ascii",
	},
	{
		"policy_name=test\xFF\xEF policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE dmverity_roothash=GOOD_DOG action=ALLOW",
		-EBADMSG,
		"invalid property value (2)",
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"policy_name=test policy_version=0.1.0\n"
		"DEFAULT action=ALLOW",
		-EBADMSG,
		"double header"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"DEFAULT action=ALLOW\n",
		-EBADMSG,
		"double default"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"DEFAULT op=EXECUTE action=DENY\n"
		"DEFAULT op=EXECUTE action=ALLOW\n",
		-EBADMSG,
		"double operation default"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"DEFAULT op=EXECUTE action=DEN\n",
		-EBADMSG,
		"invalid action value"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"DEFAULT op=EXECUTE action\n",
		-EBADMSG,
		"invalid action value (2)"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"UNKNOWN value=true\n",
		-EBADMSG,
		"unrecognized statement"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE dmverity_roothash=1c0d7ee1f8343b7fbe418378e8eb22c061d7dec7 action=DENY\n",
		-EBADMSG,
		"old-style digest"
	},
	{
		"policy_name=test policy_version=0.0.0\n"
		"DEFAULT action=ALLOW\n"
		"op=EXECUTE fsverity_digest=1c0d7ee1f8343b7fbe418378e8eb22c061d7dec7 action=DENY\n",
		-EBADMSG,
		"old-style digest"
	}
};

static void pol_to_desc(const struct policy_case *c, char *desc)
{
	strscpy(desc, c->desc, KUNIT_PARAM_DESC_SIZE);
}

KUNIT_ARRAY_PARAM(ipe_policies, policy_cases, pol_to_desc);

/**
 * ipe_parser_unsigned_test - Test the parser by passing unsigned policies.
 * @test: Supplies a pointer to a kunit structure.
 *
 * This is called by the kunit harness. This test does not check the correctness
 * of the policy, but ensures that errors are handled correctly.
 */
static void ipe_parser_unsigned_test(struct kunit *test)
{
	const struct policy_case *p = test->param_value;
	struct ipe_policy *pol;

	pol = ipe_new_policy(p->policy, strlen(p->policy), NULL, 0);

	if (p->errno) {
		KUNIT_EXPECT_EQ(test, PTR_ERR(pol), p->errno);
		return;
	}

	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pol);
	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pol->parsed);
	KUNIT_EXPECT_STREQ(test, pol->text, p->policy);
	KUNIT_EXPECT_PTR_EQ(test, NULL, pol->pkcs7);
	KUNIT_EXPECT_EQ(test, 0, pol->pkcs7len);

	ipe_free_policy(pol);
}

/**
 * ipe_parser_widestring_test - Ensure parser fail on a wide string policy.
 * @test: Supplies a pointer to a kunit structure.
 *
 * This is called by the kunit harness.
 */
static void ipe_parser_widestring_test(struct kunit *test)
{
	const unsigned short policy[] = L"policy_name=Test policy_version=0.0.0\n"
					L"DEFAULT action=ALLOW";
	struct ipe_policy *pol = NULL;

	pol = ipe_new_policy((const char *)policy, (ARRAY_SIZE(policy) - 1) * 2, NULL, 0);
	KUNIT_EXPECT_TRUE(test, IS_ERR_OR_NULL(pol));

	ipe_free_policy(pol);
}

static struct kunit_case ipe_parser_test_cases[] = {
	KUNIT_CASE_PARAM(ipe_parser_unsigned_test, ipe_policies_gen_params),
	KUNIT_CASE(ipe_parser_widestring_test),
};

static struct kunit_suite ipe_parser_test_suite = {
	.name = "ipe-parser",
	.test_cases = ipe_parser_test_cases,
};

kunit_test_suite(ipe_parser_test_suite);