File: check_startup.c

package info (click to toggle)
selint 1.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,948 kB
  • sloc: ansic: 12,163; yacc: 1,149; makefile: 389; lex: 183; sh: 11
file content (151 lines) | stat: -rw-r--r-- 4,820 bytes parent folder | download | duplicates (2)
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
/*
* Copyright 2019 Tresys Technology, LLC
*
* 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 <check.h>

#include "../src/startup.h"
#include "../src/maps.h"
#include "../src/selint_error.h"
#include "../src/parse_functions.h"

#define MODULES_CONF_PATH SAMPLE_POL_DIR "modules.conf"
#define BAD_MODULES_CONF_PATH SAMPLE_POL_DIR "bad_modules.conf"
#define OBJ_PERM_SETS_PATH SAMPLE_POL_DIR "obj_perm_sets.spt"
#define BAD_OBJ_PERM_SETS_PATH SAMPLE_POL_DIR "bad_obj_perm_sets.spt"
#define SAMPLE_AV_PATH SAMPLE_AV_DIR
#define ACCESS_VECTORS_PATH SAMPLE_POL_DIR "access_vectors"

START_TEST (test_load_access_vectors_kernel) {

	ck_assert_int_eq(load_access_vectors_kernel(SAMPLE_AV_PATH), SELINT_SUCCESS);

	ck_assert_int_eq(decl_map_count(DECL_CLASS), 3);
	ck_assert_int_eq(decl_map_count(DECL_PERM), 37);

	ck_assert_str_eq(look_up_in_decl_map("file", DECL_CLASS), "class");
	ck_assert_str_eq(look_up_in_decl_map("append", DECL_PERM), "perm");
	ck_assert_str_eq(look_up_in_decl_map("listen", DECL_PERM), "perm");
	ck_assert_str_eq(look_up_in_decl_map("use", DECL_PERM), "perm");

	free_all_maps();

}
END_TEST

START_TEST (test_load_access_vectors_source) {

	ck_assert_int_eq(load_access_vectors_source(ACCESS_VECTORS_PATH), SELINT_SUCCESS);

	ck_assert_int_eq(decl_map_count(DECL_CLASS), 8);
	ck_assert_int_eq(decl_map_count(DECL_PERM), 33);

	ck_assert_str_eq(look_up_in_decl_map("file", DECL_CLASS), "__av_file__");
	ck_assert_str_eq(look_up_in_decl_map("append", DECL_PERM), "__av_file__");
	ck_assert_str_eq(look_up_in_decl_map("use", DECL_PERM), "__av_file__");

	free_all_maps();

}
END_TEST

START_TEST (test_load_modules_source) {

	enum selint_error res = load_modules_source(MODULES_CONF_PATH);

	ck_assert_int_eq(SELINT_SUCCESS, res);

	ck_assert_str_eq("base", look_up_in_mods_map("sysadm"));
	ck_assert_str_eq("module", look_up_in_mods_map("sudo"));
	ck_assert_str_eq("off", look_up_in_mods_map("games"));

	res = load_modules_source(BAD_MODULES_CONF_PATH);

	ck_assert_int_eq(SELINT_PARSE_ERROR, res);

	free_all_maps();

}
END_TEST

START_TEST (test_load_obj_perm_sets_source) {

	enum selint_error res = load_obj_perm_sets_source(OBJ_PERM_SETS_PATH);
	ck_assert_int_eq(SELINT_SUCCESS, res);

	const struct string_list *mount_fs_perms = look_up_in_permmacros_map("mount_fs_perms");
	ck_assert_ptr_nonnull(mount_fs_perms);
	ck_assert_int_eq(1, str_in_sl("mount", mount_fs_perms));
	ck_assert_int_eq(1, str_in_sl("remount", mount_fs_perms));
	ck_assert_int_eq(1, str_in_sl("unmount", mount_fs_perms));
	ck_assert_int_eq(1, str_in_sl("getattr", mount_fs_perms));
	ck_assert_int_eq(0, str_in_sl("search", mount_fs_perms));

	const struct string_list *rw_socket_perms = look_up_in_permmacros_map("rw_socket_perms");
	ck_assert_ptr_nonnull(rw_socket_perms);
	ck_assert_int_eq(1, str_in_sl("ioctl", rw_socket_perms));
	ck_assert_int_eq(1, str_in_sl("getattr", rw_socket_perms));
	ck_assert_int_eq(1, str_in_sl("setattr", rw_socket_perms));
	ck_assert_int_eq(1, str_in_sl("bind", rw_socket_perms));
	ck_assert_int_eq(1, str_in_sl("getopt", rw_socket_perms));
	ck_assert_int_eq(1, str_in_sl("shutdown", rw_socket_perms));
	ck_assert_int_eq(0, str_in_sl("admin", rw_socket_perms));

	const struct string_list *remount_fs_perms = look_up_in_permmacros_map("remount_fs_perms");
	ck_assert_ptr_nonnull(remount_fs_perms);
	ck_assert_int_eq(1, str_in_sl("remount", remount_fs_perms));
	ck_assert_int_eq(1, str_in_sl("getattr", remount_fs_perms));

	ck_assert_ptr_null(look_up_in_permmacros_map("dir_file_class_set"));

	res = load_obj_perm_sets_source(BAD_OBJ_PERM_SETS_PATH);
	ck_assert_int_eq(SELINT_PARSE_ERROR, res);

	cleanup_parsing();

}
END_TEST

static Suite *startup_suite(void) {
	Suite *s;
	TCase *tc_core;

	s = suite_create("Startup");

	tc_core = tcase_create("Core");

	tcase_add_test(tc_core, test_load_access_vectors_kernel);
	tcase_add_test(tc_core, test_load_access_vectors_source);
	tcase_add_test(tc_core, test_load_modules_source);
	tcase_add_test(tc_core, test_load_obj_perm_sets_source);
	suite_add_tcase(s, tc_core);

	return s;
}

int main(void) {

	int number_failed = 0;
	Suite *s;
	SRunner *sr;

	s = startup_suite();
	sr = srunner_create(s);
	srunner_run_all(sr, CK_NORMAL);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0)? 0 : -1;
}