-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvalidation-pbt.rs
408 lines (391 loc) · 17 KB
/
validation-pbt.rs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![no_main]
use cedar_drt::initialize_log;
use cedar_drt_inner::*;
use cedar_policy_core::ast;
use cedar_policy_core::authorizer::{AuthorizationError, Authorizer};
use cedar_policy_core::entities::Entities;
use cedar_policy_core::evaluator::EvaluationError;
use cedar_policy_generators::{
abac::{ABACPolicy, ABACRequest},
err::{Error, Result},
hierarchy::{Hierarchy, HierarchyGenerator},
schema::Schema,
settings::ABACSettings,
};
use cedar_policy_validator::{json_schema, ValidationMode, Validator, ValidatorSchema};
use libfuzzer_sys::arbitrary::{self, Arbitrary, Unstructured};
use log::debug;
use serde::Serialize;
use std::convert::TryFrom;
/// Input expected by this fuzz target:
/// An ABAC hierarchy, schema, and 8 associated policies
#[derive(Debug, Clone, Serialize)]
struct FuzzTargetInput {
/// generated schema
#[serde(skip)]
pub schema: Schema,
/// generated hierarchy
#[serde(skip)]
pub hierarchy: Hierarchy,
/// the policy which we will see if it validates
pub policy: ABACPolicy,
/// the requests to try, if the policy validates.
/// We try 8 requests per validated policy.
#[serde(skip)]
pub requests: [ABACRequest; 8],
}
/// settings for this fuzz target
const SETTINGS: ABACSettings = ABACSettings {
match_types: false,
enable_extensions: true,
max_depth: 7,
max_width: 7,
enable_additional_attributes: true,
enable_like: true,
enable_action_groups_and_attrs: true,
enable_arbitrary_func_call: true,
enable_unknowns: false,
enable_action_in_constraints: true,
enable_unspecified_apply_spec: true,
// It's Ok to enable this flag because this target is PBT.
enable_datetime_extension: true,
};
const LOG_FILENAME_GENERATION_START: &str = "./logs/01_generation_start.txt";
const LOG_FILENAME_GENERATED_SCHEMA: &str = "./logs/02_generated_schema.txt";
const LOG_FILENAME_GENERATED_HIERARCHY: &str = "./logs/03_generated_hierarchy.txt";
const LOG_FILENAME_GENERATED_POLICY: &str = "./logs/04_generated_policy.txt";
const LOG_FILENAME_GENERATED_REQUESTS: &str = "./logs/05_generated_requests.txt";
const LOG_FILENAME_SCHEMA_VALID: &str = "./logs/06_schema_valid.txt";
const LOG_FILENAME_HIERARCHY_VALID: &str = "./logs/07_hierarchy_valid.txt";
const LOG_FILENAME_VALIDATION_PASS: &str = "./logs/08_validation_pass.txt";
const LOG_FILENAME_ERR_NOT_ENOUGH_DATA: &str = "./logs/err_not_enough_data.txt";
const LOG_FILENAME_ERR_EMPTY_CHOOSE: &str = "./logs/err_empty_choose.txt";
const LOG_FILENAME_ERR_TOO_DEEP: &str = "./logs/err_too_deep.txt";
const LOG_FILENAME_ERR_NO_VALID_TYPES: &str = "./logs/err_no_valid_types.txt";
const LOG_FILENAME_ERR_EXTENSIONS_DISABLED: &str = "./logs/err_extensions_disabled.txt";
const LOG_FILENAME_ERR_LIKE_DISABLED: &str = "./logs/err_like_disabled.txt";
const LOG_FILENAME_ERR_CONTEXT: &str = "./logs/err_context.txt";
const LOG_FILENAME_ERR_INCORRECT_FORMAT: &str = "./logs/err_incorrect_format.txt";
const LOG_FILENAME_ERR_OTHER: &str = "./logs/err_other.txt";
const LOG_FILENAME_ENTITIES_ERROR: &str = "./logs/err_entities.txt";
const LOG_FILENAME_SCHEMA_ERROR: &str = "./logs/err_schema.txt";
// In the below, "vyes" means the schema passed validation, while "vno" means we
// got to the point of running the validator but validation failed
const LOG_FILENAME_TOTAL_ENTITY_TYPES: &str = "./logs/schemastats/total_entity_types";
const LOG_FILENAME_TOTAL_ACTIONS: &str = "./logs/schemastats/total_actions";
const LOG_FILENAME_APPLIES_TO_NONE: &str = "./logs/schemastats/applies_to_none";
const LOG_FILENAME_APPLIES_TO_PRINCIPAL_LEN: &str = "./logs/schemastats/applies_to_principal_len";
const LOG_FILENAME_APPLIES_TO_RESOURCE_LEN: &str = "./logs/schemastats/applies_to_resource_len";
const LOG_FILENAME_TOTAL_ENTITIES: &str = "./logs/hierarchystats/total_entities";
const LOG_FILENAME_TOTAL_SUBEXPRESSIONS: &str = "./logs/policystats/total_subexpressions";
/// Append to the given filename to indicate we've reached the corresponding
/// checkpoint, or the corresponding event has happened
fn checkpoint(filename: impl AsRef<std::path::Path>) {
if std::env::var("FUZZ_LOG_STATS").is_ok() {
use std::io::Write;
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(filename.as_ref())
.unwrap();
writeln!(file, "y").unwrap();
}
}
fn log_err<T>(res: Result<T>, doing_what: &str) -> Result<T> {
if std::env::var("FUZZ_LOG_STATS").is_ok() {
match &res {
Err(Error::EntitiesError(_)) => {
checkpoint(LOG_FILENAME_ENTITIES_ERROR.to_string() + "_" + doing_what)
}
Err(Error::SchemaError(_)) => {
checkpoint(LOG_FILENAME_SCHEMA_ERROR.to_string() + "_" + doing_what)
}
Err(Error::NotEnoughData) => {
checkpoint(LOG_FILENAME_ERR_NOT_ENOUGH_DATA.to_string() + "_" + doing_what)
}
Err(Error::EmptyChoose {
doing_what: doing_2,
}) => checkpoint(
LOG_FILENAME_ERR_EMPTY_CHOOSE.to_string()
+ "_"
+ doing_what
+ "_"
+ &doing_2.replace(' ', "_"),
),
Err(Error::TooDeep) => {
checkpoint(LOG_FILENAME_ERR_TOO_DEEP.to_string() + "_" + doing_what)
}
Err(Error::NoValidPrincipalOrResourceTypes) => {
checkpoint(LOG_FILENAME_ERR_NO_VALID_TYPES.to_string() + "_" + doing_what)
}
Err(Error::ExtensionsDisabled) => {
checkpoint(LOG_FILENAME_ERR_EXTENSIONS_DISABLED.to_string() + "_" + doing_what)
}
Err(Error::LikeDisabled) => {
checkpoint(LOG_FILENAME_ERR_LIKE_DISABLED.to_string() + "_" + doing_what)
}
Err(Error::ContextError(_)) => {
checkpoint(LOG_FILENAME_ERR_CONTEXT.to_string() + "_" + doing_what)
}
Err(Error::IncorrectFormat {
doing_what: doing_2,
}) => checkpoint(
LOG_FILENAME_ERR_INCORRECT_FORMAT.to_string()
+ "_"
+ doing_what
+ "_"
+ &doing_2.replace(' ', "_"),
),
Err(Error::OtherArbitrary(_)) => {
checkpoint(LOG_FILENAME_ERR_OTHER.to_string() + "_" + doing_what)
}
Ok(_) | Err(Error::DatetimeExtensionsDisabled) => (),
}
}
res
}
fn maybe_log_schemastats<N>(schema: Option<&json_schema::NamespaceDefinition<N>>, suffix: &str) {
if std::env::var("FUZZ_LOG_STATS").is_ok() {
let schema = schema.expect("should be SOME if FUZZ_LOG_STATS is ok");
checkpoint(
LOG_FILENAME_TOTAL_ENTITY_TYPES.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", schema.entity_types.len()),
);
checkpoint(
LOG_FILENAME_TOTAL_ACTIONS.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", schema.actions.len()),
);
for action in schema.actions.values() {
match action.applies_to.as_ref() {
None => checkpoint(LOG_FILENAME_APPLIES_TO_NONE.to_string() + "_" + suffix),
Some(json_schema::ApplySpec {
principal_types,
resource_types,
..
}) => {
checkpoint(
LOG_FILENAME_APPLIES_TO_PRINCIPAL_LEN.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", principal_types.len()),
);
checkpoint(
LOG_FILENAME_APPLIES_TO_RESOURCE_LEN.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", resource_types.len()),
);
}
}
}
}
}
fn maybe_log_hierarchystats(hierarchy: &Hierarchy, suffix: &str) {
if std::env::var("FUZZ_LOG_STATS").is_ok() {
checkpoint(
LOG_FILENAME_TOTAL_ENTITIES.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", hierarchy.num_entities()),
);
}
}
fn maybe_log_policystats(policy: &ast::StaticPolicy, suffix: &str) {
if std::env::var("FUZZ_LOG_STATS").is_ok() {
let total_subexpressions = policy.condition().subexpressions().count();
checkpoint(
LOG_FILENAME_TOTAL_SUBEXPRESSIONS.to_string()
+ "_"
+ suffix
+ "_"
+ &format!("{:03}", total_subexpressions),
);
}
}
impl<'a> Arbitrary<'a> for FuzzTargetInput {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
checkpoint(LOG_FILENAME_GENERATION_START);
let schema: Schema = log_err(Schema::arbitrary(SETTINGS.clone(), u), "generating_schema")?;
checkpoint(LOG_FILENAME_GENERATED_SCHEMA);
let hierarchy = log_err(schema.arbitrary_hierarchy(u), "generating_hierarchy")?;
checkpoint(LOG_FILENAME_GENERATED_HIERARCHY);
let policy = log_err(schema.arbitrary_policy(&hierarchy, u), "generating_policy")?;
checkpoint(LOG_FILENAME_GENERATED_POLICY);
let requests = [
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
log_err(
schema.arbitrary_request(&hierarchy, u),
"generating_request",
)?,
];
checkpoint(LOG_FILENAME_GENERATED_REQUESTS);
Ok(Self {
schema,
hierarchy,
policy,
requests,
})
}
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
]))
}
}
/// helper function that just tells us whether a policyset passes validation
fn passes_validation(
validator: &Validator,
policyset: &ast::PolicySet,
mode: ValidationMode,
) -> bool {
validator.validate(policyset, mode).validation_passed()
}
// The main fuzz target. This is for PBT on the validator
fuzz_target!(|input: FuzzTargetInput| {
initialize_log();
// preserve the schemafile for later logging, if we'll need it
let schemafile = if std::env::var("FUZZ_LOG_STATS").is_ok() {
Some(input.schema.schemafile().clone())
} else {
None
};
// preserve the schema in string format, which may be needed for error messages later
let schemafile_string = input.schema.schemafile_string();
if let Ok(schema) = ValidatorSchema::try_from(input.schema) {
debug!("Schema: {:?}", schema);
checkpoint(LOG_FILENAME_SCHEMA_VALID);
if let Ok(entities) = Entities::try_from(input.hierarchy.clone()) {
checkpoint(LOG_FILENAME_HIERARCHY_VALID);
let validator = Validator::new(schema);
let mut policyset = ast::PolicySet::new();
let policy: ast::StaticPolicy = input.policy.into();
policyset.add_static(policy.clone()).unwrap();
let passes_strict = passes_validation(&validator, &policyset, ValidationMode::Strict);
let passes_permissive =
passes_validation(&validator, &policyset, ValidationMode::Permissive);
if passes_permissive {
checkpoint(LOG_FILENAME_VALIDATION_PASS);
let suffix = if passes_strict { "vyes" } else { "vpermissive" };
maybe_log_schemastats(schemafile.as_ref(), suffix);
maybe_log_hierarchystats(&input.hierarchy, suffix);
maybe_log_policystats(&policy, suffix);
// policy successfully validated, let's make sure we don't get any
// dynamic type errors
let authorizer = Authorizer::new();
debug!("Policies: {policyset}");
debug!("Entities: {entities}");
for r in input.requests.into_iter() {
let q = ast::Request::from(r);
debug!("Request: {q}");
let ans = authorizer.is_authorized(q.clone(), &policyset, &entities);
let unexpected_errs = ans
.diagnostics
.errors
.iter()
.filter_map(|error| match error {
AuthorizationError::PolicyEvaluationError { error, .. } => {
match error {
// Evaluation errors the validator should prevent.
EvaluationError::RecordAttrDoesNotExist(_)
| EvaluationError::EntityAttrDoesNotExist(_)
| EvaluationError::FailedExtensionFunctionLookup(_)
| EvaluationError::TypeError(_)
| EvaluationError::WrongNumArguments(_) => {
Some(error.to_string())
}
// Evaluation errors it shouldn't prevent. Not
// written with a catch all so that we must
// consider if a new error type should cause
// this target to fail.
EvaluationError::EntityDoesNotExist(_)
| EvaluationError::IntegerOverflow(_)
| EvaluationError::UnlinkedSlot(_)
| EvaluationError::FailedExtensionFunctionExecution(_)
| EvaluationError::NonValue(_)
| EvaluationError::RecursionLimit(_) => None,
}
}
})
.collect::<Vec<_>>();
assert_eq!(
unexpected_errs,
Vec::<String>::new(),
"validated policy produced unexpected errors {unexpected_errs:?}!\npolicies:\n{policyset}\nentities:\n{entities}\nschema:\n{schemafile_string}\nrequest:\n{q}\n",
)
}
} else {
maybe_log_schemastats(schemafile.as_ref(), "vno");
maybe_log_hierarchystats(&input.hierarchy, "vno");
maybe_log_policystats(&policy, "vno");
assert_eq!(
false,
passes_strict,
"policy fails permissive validation but passes strict validation!\npolicies:\n{policyset}\nentities:\n{entities}\nschema:\n{schemafile_string}\n",
);
}
}
}
});