Skip to content

Commit 392069b

Browse files
committed
Add checks for template/interface mis-declarations
Interfaces in the refpolicy should not: - declare anything (no side effects) - use prefix parameters Add one check to find interfaces that should be declared as a template and one check to find templates that can be declared as an interface. Refpolicy findings: qemu.if: 112: (S): Template qemu_role might be declared as an interface (S-012) wm.if: 142: (S): Interface wm_dbus_chat should be a template, due to parameter 0 (S-011) wm.if: 250: (S): Interface wm_write_pipes should be a template, due to parameter 0 (S-011) gnome.if: 673: (S): Interface gnome_dbus_chat_gkeyringd should be a template, due to parameter 0 (S-011) gnome.if: 741: (S): Interface gnome_stream_connect_gkeyringd should be a template, due to parameter 0 (S-011) userdomain.if: 1431: (S): Template userdom_security_admin_template might be declared as an interface (S-012) kismet.if: 18: (S): Template kismet_role might be declared as an interface (S-012) dbus.if: 193: (S): Interface dbus_connect_spec_session_bus should be a template, due to parameter 0 (S-011) dbus.if: 245: (S): Interface dbus_spec_session_bus_client should be a template, due to parameter 0 (S-011) dbus.if: 298: (S): Interface dbus_send_spec_session_bus should be a template, due to parameter 0 (S-011) dbus.if: 436: (S): Interface dbus_spec_session_domain should be a template, due to parameter 0 (S-011) rlogin.if: 32: (S): Template rlogin_read_home_content might be declared as an interface (S-012) git.if: 18: (S): Template git_role might be declared as an interface (S-012) Found the following issue counts: S-011: 8 S-012: 5 Closes: #205
1 parent 1d190c4 commit 392069b

File tree

9 files changed

+160
-0
lines changed

9 files changed

+160
-0
lines changed

README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ CHECK IDS
168168
S-008: Unquoted gen_require block
169169
S-009: Permission macro suffix does not match class name
170170
S-010: Permission macro usage suggested
171+
S-011: Interface should be decalred as template
172+
S-012: Template can be declared as interface
171173

172174
W-001: Type or attribute referenced without explicit declaration
173175
W-002: Type, attribute or role used but not listed in require block in interface

src/check_hooks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum style_ids {
4141
S_ID_UNQUOTE_GENREQ = 8,
4242
S_ID_PERM_SUFFIX = 9,
4343
S_ID_PERMMACRO = 10,
44+
S_ID_TEXT_IF_PARAM = 11,
45+
S_ID_VOID_TEMP_DECL = 12,
4446
S_END
4547
};
4648

src/if_checks.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,80 @@ struct check_result *check_unquoted_gen_require_block(__attribute__((unused)) co
232232
return NULL;
233233
}
234234

235+
struct check_result *check_text_param_in_interface(__attribute__((unused)) const struct
236+
check_data *data,
237+
const struct
238+
policy_node *node)
239+
{
240+
const char *if_name = node->data.str;
241+
const struct interface_trait *trait = look_up_in_if_traits_map(if_name);
242+
if (!trait || !trait->is_inferred) {
243+
return NULL;
244+
}
245+
246+
for (int i = 0; i < TRAIT_MAX_PARAMETERS; i++) {
247+
if (trait->parameters[i] == PARAM_TEXT) {
248+
return make_check_result('S', S_ID_TEXT_IF_PARAM,
249+
"Interface %s should be a template, due to parameter %d",
250+
if_name,
251+
i);
252+
}
253+
254+
if (trait->parameters[i] == PARAM_INITIAL) {
255+
break;
256+
}
257+
}
258+
259+
return NULL;
260+
}
261+
262+
struct check_result *check_unnecessary_template_definition(__attribute__((unused)) const struct
263+
check_data *data,
264+
const struct
265+
policy_node *node)
266+
{
267+
const char *temp_name = node->data.str;
268+
const struct interface_trait *trait = look_up_in_if_traits_map(temp_name);
269+
if (!trait || !trait->is_inferred) {
270+
return NULL;
271+
}
272+
273+
for (int i = 0; i < TRAIT_MAX_PARAMETERS; i++) {
274+
if (trait->parameters[i] == PARAM_TEXT) {
275+
return NULL;
276+
}
277+
278+
if (trait->parameters[i] == PARAM_INITIAL) {
279+
break;
280+
}
281+
}
282+
283+
for (const struct policy_node *cur = node->first_child; cur; cur = dfs_next(cur)) {
284+
if (cur == node || cur == node->next) {
285+
break;
286+
}
287+
288+
if (cur->flavor == NODE_GEN_REQ || cur->flavor == NODE_REQUIRE) {
289+
cur = cur->next;
290+
}
291+
292+
if (cur->flavor == NODE_DECL) {
293+
return NULL;
294+
}
295+
296+
if (cur->flavor == NODE_IF_CALL) {
297+
const struct if_call_data *ic_data = cur->data.ic_data;
298+
if (look_up_in_template_map(ic_data->name)) {
299+
return NULL;
300+
}
301+
}
302+
}
303+
304+
return make_check_result('S', S_ID_VOID_TEMP_DECL,
305+
"Template %s might be declared as an interface",
306+
temp_name);
307+
}
308+
235309
struct check_result *check_name_used_but_not_required_in_if(const struct
236310
check_data *data,
237311
const struct

src/if_checks.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ struct check_result *check_unquoted_gen_require_block(const struct
8181
const struct
8282
policy_node *node);
8383

84+
/*********************************************
85+
* Check that an interface has no text parameter
86+
* Called on NODE_INTERFACE_DEF nodes
87+
* data - metadata about the file
88+
* node - the node to check
89+
* returns NULL if passed or check_result for issue S-011
90+
*********************************************/
91+
struct check_result *check_text_param_in_interface(const struct
92+
check_data *data,
93+
const struct
94+
policy_node *node);
95+
96+
/*********************************************
97+
* Check for templates that can be declared as interface
98+
* Called on NODE_TEMP_DEF nodes
99+
* data - metadata about the file
100+
* node - the node to check
101+
* returns NULL if passed or check_result for issue S-012
102+
*********************************************/
103+
struct check_result *check_unnecessary_template_definition(const struct
104+
check_data *data,
105+
const struct
106+
policy_node *node);
107+
84108
/*********************************************
85109
* Check that all names referenced in interface are listed in its require block
86110
* (or declared in that template)

src/runner.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ struct checks *register_checks(char level,
189189
add_check(NODE_AV_RULE, ck, "S-010",
190190
check_perm_macro_available);
191191
}
192+
if (CHECK_ENABLED("S-011")) {
193+
add_check(NODE_INTERFACE_DEF, ck, "S-011",
194+
check_text_param_in_interface);
195+
}
196+
if (CHECK_ENABLED("S-012")) {
197+
add_check(NODE_TEMP_DEF, ck, "S-012",
198+
check_unnecessary_template_definition);
199+
}
192200
// FALLTHRU
193201
case 'W':
194202
if (CHECK_ENABLED("W-001")) {

tests/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ FUNCTIONAL_TEST_FILES=functional/end-to-end.bats \
157157
functional/policies/check_triggers/s08.if \
158158
functional/policies/check_triggers/s09.pass.te \
159159
functional/policies/check_triggers/s09.warn.te \
160+
functional/policies/check_triggers/s11.if \
161+
functional/policies/check_triggers/s12.if \
160162
functional/policies/check_triggers/w01_other.te \
161163
functional/policies/check_triggers/w01.te \
162164
functional/policies/check_triggers/w02.if \

tests/functional/end-to-end.bats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ test_parse_error_impl() {
188188
test_one_check_expect "S-009" "s09.warn.te" 6
189189
}
190190

191+
@test "S-011" {
192+
test_one_check "S-011" "s11.if"
193+
}
194+
195+
@test "S-012" {
196+
test_one_check "S-012" "s12.if"
197+
}
198+
191199
@test "W-001" {
192200
test_one_check_expect "W-001" "w01*" 5
193201
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface(`foo1', `
2+
gen_require(`
3+
type foo_t;
4+
')
5+
6+
allow $1 foo_t:file read;
7+
')
8+
9+
interface(`foo2', `
10+
gen_require(`
11+
type foo_t;
12+
')
13+
14+
allow $1_t foo_t:file read;
15+
')
16+
17+
interface(`foo3', `
18+
type foo_t;
19+
allow $1 foo_t:file read;
20+
')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
template(`foo1', `
2+
gen_require(`
3+
type foo_t;
4+
')
5+
6+
allow $1 foo_t:file read;
7+
')
8+
9+
template(`foo2', `
10+
gen_require(`
11+
type foo_t;
12+
')
13+
14+
allow $1_t foo_t:file read;
15+
')
16+
17+
template(`foo3', `
18+
type foo_t;
19+
allow $1 foo_t:file read;
20+
')

0 commit comments

Comments
 (0)