1 #include <dpl/test/test_runner.h>
5 #include <summary_collector.h>
10 #include <linux/xattr.h>
12 #include <libprivilege-control_test_common.h>
13 #include <tests_common.h>
15 #include <security-manager.h>
17 DEFINE_SMARTPTR(security_manager_app_inst_req_free, app_inst_req, AppInstReqUniquePtr);
19 static const char *const SM_APP_ID1 = "sm_test_app_id_double";
20 static const char *const SM_PKG_ID1 = "sm_test_pkg_id_double";
22 static const char *const SM_APP_ID2 = "sm_test_app_id_full";
23 static const char *const SM_PKG_ID2 = "sm_test_pkg_id_full";
25 static const char *const SM_ALLOWED_PERMISSION1 = "security_manager_test_rules2_r";
26 static const char *const SM_ALLOWED_PERMISSION2 = "security_manager_test_rules2_no_r";
27 static const char *const XATTR_NAME_TIZENEXEC = XATTR_SECURITY_PREFIX "TIZEN_EXEC_LABEL";
29 static const rules_t SM_ALLOWED_RULES = {
30 { USER_APP_ID, "test_sm_book_8", "r" },
31 { USER_APP_ID, "test_sm_book_9", "w" },
32 { USER_APP_ID, "test_sm_book_10", "x" },
33 { USER_APP_ID, "test_sm_book_11", "rw" },
34 { USER_APP_ID, "test_sm_book_12", "rx" },
35 { USER_APP_ID, "test_sm_book_13", "wx" },
36 { USER_APP_ID, "test_sm_book_14", "rwx" },
37 { USER_APP_ID, "test_sm_book_15", "rwxat" },
38 { "test_sm_subject_8", USER_APP_ID, "r" },
39 { "test_sm_subject_9", USER_APP_ID, "w" },
40 { "test_sm_subject_10", USER_APP_ID, "x" },
41 { "test_sm_subject_11", USER_APP_ID, "rw" },
42 { "test_sm_subject_12", USER_APP_ID, "rx" },
43 { "test_sm_subject_13", USER_APP_ID, "wx" },
44 { "test_sm_subject_14", USER_APP_ID, "rwx" },
45 { "test_sm_subject_15", USER_APP_ID, "rwxat" }
47 static const rules_t SM_DENIED_RULES = {
48 { USER_APP_ID, "test_sm_book_1", "r" },
49 { USER_APP_ID, "test_sm_book_2", "w" },
50 { USER_APP_ID, "test_sm_book_3", "x" },
51 { USER_APP_ID, "test_sm_book_4", "rw" },
52 { USER_APP_ID, "test_sm_book_5", "rx" },
53 { USER_APP_ID, "test_sm_book_6", "wx" },
54 { USER_APP_ID, "test_sm_book_7", "rwx" },
55 { "test_sm_subject_1", USER_APP_ID, "r" },
56 { "test_sm_subject_2", USER_APP_ID, "w" },
57 { "test_sm_subject_3", USER_APP_ID, "x" },
58 { "test_sm_subject_4", USER_APP_ID, "rw" },
59 { "test_sm_subject_5", USER_APP_ID, "rx" },
60 { "test_sm_subject_6", USER_APP_ID, "wx" },
61 { "test_sm_subject_7", USER_APP_ID, "rwx" }
64 static const char *const SM_DENIED_PERMISSION1 = "security_manager_test_rules1";
65 static const char *const SM_DENIED_PERMISSION2 = "security_manager_test_rules2";
67 static const char *const SM_PRIVATE_PATH = "/etc/smack/test_DIR/app_dir";
68 static const char *const SM_PUBLIC_PATH = "/etc/smack/test_DIR/app_dir_public";
69 static const char *const SM_PUBLIC_RO_PATH = "/etc/smack/test_DIR/app_dir_public_ro";
70 static const char *const SM_DENIED_PATH = "/etc/smack/test_DIR/non_app_dir";
73 static bool isLinkToExec(const char *fpath, const struct stat *sb)
80 // check if it's a link
81 if ( !S_ISLNK(sb->st_mode))
84 target = realpath(fpath, NULL);
85 RUNNER_ASSERT_MSG_BT(target != 0, "Could not obtain real path from link.");
87 ret = stat(target, &buf);
88 RUNNER_ASSERT_MSG_BT(ret == 0, "Could not obtain real path's stat from link.");
90 if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG))
97 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
98 const char* correctLabel, bool transmute_test, bool exec_test)
105 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
106 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
107 labelPtr.reset(label);
108 RUNNER_ASSERT_MSG_BT(label != NULL, "ACCESS label on " << fpath << " is not set");
109 result = strcmp(correctLabel, label);
110 RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is incorrect"
111 " (should be '" << correctLabel << "' and is '" << label << "')");
115 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
116 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
117 labelPtr.reset(label);
119 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
120 RUNNER_ASSERT_MSG_BT(label != NULL, "EXEC label on " << fpath << " is not set");
121 result = strcmp(correctLabel, label);
122 RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect EXEC label on executable file " << fpath);
124 RUNNER_ASSERT_MSG_BT(label == NULL, "EXEC label on " << fpath << " is set");
128 if (isLinkToExec(fpath, sb) && exec_test) {
129 char buf[SMACK_LABEL_LEN+1];
130 result = lgetxattr(fpath, XATTR_NAME_TIZENEXEC, buf, sizeof(buf));
131 RUNNER_ASSERT_MSG_BT(result != -1, "Could not get label for the path "
132 << fpath << "("<<strerror(errno)<<")");
134 result = strcmp(correctLabel, buf);
135 RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect TIZEN_EXEC_LABEL attribute"
136 " on link to executable " << fpath);
142 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
143 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
144 labelPtr.reset(label);
146 if (S_ISDIR(sb->st_mode) && transmute_test == true) {
147 RUNNER_ASSERT_MSG_BT(label != NULL, "TRANSMUTE label on " << fpath << " is not set at all");
148 RUNNER_ASSERT_MSG_BT(strcmp(label,"TRUE") == 0,
149 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
151 RUNNER_ASSERT_MSG_BT(label == NULL, "TRANSMUTE label on " << fpath << " is set");
158 static int nftw_check_sm_labels_app_private_dir(const char *fpath, const struct stat *sb,
159 int /*typeflag*/, struct FTW* /*ftwbuf*/)
161 return nftw_check_sm_labels_app_dir(fpath, sb, USER_APP_ID, false, true);
164 static int nftw_check_sm_labels_app_public_dir(const char *fpath, const struct stat *sb,
165 int /*typeflag*/, struct FTW* /*ftwbuf*/)
168 return nftw_check_sm_labels_app_dir(fpath, sb, "User", true, false);
171 static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct stat *sb,
172 int /*typeflag*/, struct FTW* /*ftwbuf*/)
175 return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
179 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
181 static app_inst_req* do_app_inst_req_new()
184 app_inst_req *req = NULL;
186 result = security_manager_app_inst_req_new(&req);
187 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
188 "creation of new request failed. Result: " << result);
189 RUNNER_ASSERT_MSG_BT(req != NULL, "creation of new request did not allocate memory");
193 RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
196 AppInstReqUniquePtr request;
198 request.reset(do_app_inst_req_new());
200 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
201 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
202 "setting app id failed. Result: " << result);
204 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID1);
205 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
206 "setting pkg id failed. Result: " << result);
208 result = security_manager_app_install(request.get());
209 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
210 "installing app failed. Result: " << result);
212 result = security_manager_app_install(request.get());
213 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
214 "installing already installed app failed. Result: " << result);
216 request.reset(do_app_inst_req_new());
218 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
219 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
220 "setting app id failed. Result: " << result);
222 result = security_manager_app_uninstall(request.get());
223 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
224 "uninstalling app failed. Result: " << result);
226 result = security_manager_app_uninstall(request.get());
227 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
228 "uninstalling already uninstalled app failed. Result: " << result);
231 static void prepare_app_path()
235 result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
236 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
238 result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
239 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
241 result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
242 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
244 result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
245 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
248 static void check_app_path_after_install()
252 result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
253 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
255 result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
256 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
258 result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
259 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
261 result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
262 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
265 static void check_app_permission_after_install()
269 result = check_all_accesses(smack_check(), SM_ALLOWED_RULES);
270 RUNNER_ASSERT_MSG_BT(result, "Permissions not added.");
271 result = check_no_accesses(smack_check(), SM_DENIED_RULES);
272 RUNNER_ASSERT_MSG_BT(result, "Permissions added.");
274 check_perm_app_has_permission(USER_APP_ID, SM_ALLOWED_PERMISSION1, true);
275 check_perm_app_has_permission(USER_APP_ID, SM_ALLOWED_PERMISSION2, true);
276 check_perm_app_has_permission(USER_APP_ID, SM_DENIED_PERMISSION1, false);
277 check_perm_app_has_permission(USER_APP_ID, SM_DENIED_PERMISSION2, false);
280 static void prepare_app_env()
285 static void check_app_env_after_install()
287 check_app_path_after_install();
288 check_app_permission_after_install();
291 RUNNER_TEST(security_manager_02_app_install_uninstall_full)
294 AppInstReqUniquePtr request;
298 request.reset(do_app_inst_req_new());
300 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
301 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
302 "setting app id failed. Result: " << result);
304 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
305 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
306 "setting pkg id failed. Result: " << result);
308 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PERMISSION1);
309 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
310 "setting allowed permission failed. Result: " << result);
311 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PERMISSION2);
312 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
313 "setting allowed permission failed. Result: " << result);
315 result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
316 SECURITY_MANAGER_PATH_PRIVATE);
317 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
318 "setting allowed path failed. Result: " << result);
320 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
321 SECURITY_MANAGER_PATH_PUBLIC);
322 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
323 "setting allowed path failed. Result: " << result);
325 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
326 SECURITY_MANAGER_PATH_PUBLIC_RO);
327 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
328 "setting allowed path failed. Result: " << result);
330 result = security_manager_app_install(request.get());
331 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
332 "installing app failed. Result: " << result);
334 check_app_env_after_install();
336 request.reset(do_app_inst_req_new());
338 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
339 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
340 "setting app id failed. Result: " << result);
342 result = security_manager_app_uninstall(request.get());
343 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
344 "uninstalling app failed. Result: " << result);
348 int main(int argc, char *argv[])
350 SummaryCollector::Register();
351 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);