forked from google/kafel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.c
149 lines (121 loc) · 3.66 KB
/
policy.c
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
/*
Kafel - policy
-----------------------------------------
Copyright 2016 Google Inc. All Rights Reserved.
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
http://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.
*/
#include "policy.h"
#include <stdlib.h>
#include <string.h>
#include "common.h"
struct policy* policy_create(const char* name, struct entrieslist* entries) {
struct policy* rv = calloc(1, sizeof(*rv));
rv->name = strdup(name);
TAILQ_INIT(&rv->entries);
if (entries != NULL) {
TAILQ_CONCAT(&rv->entries, entries, entries);
}
return rv;
}
struct policy_entry* policy_action_create(uint32_t action,
struct filterslist* filters) {
struct policy_entry* rv = calloc(1, sizeof(*rv));
rv->type = POLICY_ACTION;
rv->action = action;
TAILQ_INIT(&rv->filters);
if (filters != NULL) {
TAILQ_CONCAT(&rv->filters, filters, filters);
}
return rv;
}
struct policy_entry* policy_use_create(struct policy* used) {
struct policy_entry* rv = calloc(1, sizeof(*rv));
rv->type = POLICY_USE;
rv->used = used;
return rv;
}
static void policy_action_destroy(struct policy_entry** entry) {
ASSERT(entry != NULL);
ASSERT((*entry) != NULL);
syscall_filters_destroy(&(*entry)->filters);
free(*entry);
*entry = NULL;
}
static void policy_use_destroy(struct policy_entry** entry) {
ASSERT(entry != NULL);
ASSERT((*entry) != NULL);
free(*entry);
*entry = NULL;
}
void policy_entry_destroy(struct policy_entry** entry) {
ASSERT(entry != NULL);
ASSERT((*entry) != NULL);
switch ((*entry)->type) {
case POLICY_ACTION:
policy_action_destroy(entry);
break;
case POLICY_USE:
policy_use_destroy(entry);
break;
default:
ASSERT(0); // should not happen
}
}
void policy_entries_destroy(struct entrieslist* entries) {
ASSERT(entries != NULL);
// FIXME dirty hack
if (!TAILQ_EMPTY(entries)) {
TAILQ_FIRST(entries)->entries.tqe_prev = &entries->tqh_first;
}
while (!TAILQ_EMPTY(entries)) {
struct policy_entry* entry = TAILQ_FIRST(entries);
TAILQ_REMOVE(entries, entry, entries);
policy_entry_destroy(&entry);
}
}
void policy_destroy(struct policy** policy) {
ASSERT(policy != NULL);
ASSERT((*policy) != NULL);
policy_entries_destroy(&(*policy)->entries);
free((*policy)->name);
free(*policy);
*policy = NULL;
}
struct syscall_filter* syscall_filter_create(uint32_t nr,
struct expr_tree* expr) {
struct syscall_filter* rv = calloc(1, sizeof(*rv));
rv->syscall_nr = nr;
if (expr != NULL) {
expr_simplify(&expr);
}
rv->expr = expr;
return rv;
}
void syscall_filter_destroy(struct syscall_filter** filter) {
ASSERT(filter != NULL);
ASSERT((*filter) != NULL);
if ((*filter)->expr != NULL) {
expr_destroy(&(*filter)->expr);
}
free(*filter);
}
void syscall_filters_destroy(struct filterslist* filters) {
ASSERT(filters != NULL);
// FIXME dirty hack
if (!TAILQ_EMPTY(filters)) {
TAILQ_FIRST(filters)->filters.tqe_prev = &filters->tqh_first;
}
while (!TAILQ_EMPTY(filters)) {
struct syscall_filter* filter = TAILQ_FIRST(filters);
TAILQ_REMOVE(filters, filter, filters);
syscall_filter_destroy(&filter);
}
}