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>
18 DEFINE_SMARTPTR(security_manager_app_inst_req_free, app_inst_req, AppInstReqUniquePtr);
20 static const char *const SM_APP_ID1 = "sm_test_app_id_double";
21 static const char *const SM_PKG_ID1 = "sm_test_pkg_id_double";
23 static const char *const SM_APP_ID2 = "sm_test_app_id_full";
24 static const char *const SM_PKG_ID2 = "sm_test_pkg_id_full";
26 static const privileges_t SM_ALLOWED_PRIVILEGES = {
27 "security_manager_test_rules2_r",
28 "security_manager_test_rules2_no_r"
31 static const privileges_t SM_DENIED_PRIVILEGES = {
32 "security_manager_test_rules1",
33 "security_manager_test_rules2"
36 static const char *const XATTR_NAME_TIZENEXEC = XATTR_SECURITY_PREFIX "TIZEN_EXEC_LABEL";
38 static const rules_t SM_ALLOWED_RULES = {
39 { USER_APP_ID, "test_sm_book_8", "r" },
40 { USER_APP_ID, "test_sm_book_9", "w" },
41 { USER_APP_ID, "test_sm_book_10", "x" },
42 { USER_APP_ID, "test_sm_book_11", "rw" },
43 { USER_APP_ID, "test_sm_book_12", "rx" },
44 { USER_APP_ID, "test_sm_book_13", "wx" },
45 { USER_APP_ID, "test_sm_book_14", "rwx" },
46 { USER_APP_ID, "test_sm_book_15", "rwxat" },
47 { "test_sm_subject_8", USER_APP_ID, "r" },
48 { "test_sm_subject_9", USER_APP_ID, "w" },
49 { "test_sm_subject_10", USER_APP_ID, "x" },
50 { "test_sm_subject_11", USER_APP_ID, "rw" },
51 { "test_sm_subject_12", USER_APP_ID, "rx" },
52 { "test_sm_subject_13", USER_APP_ID, "wx" },
53 { "test_sm_subject_14", USER_APP_ID, "rwx" },
54 { "test_sm_subject_15", USER_APP_ID, "rwxat" }
56 static const rules_t SM_DENIED_RULES = {
57 { USER_APP_ID, "test_sm_book_1", "r" },
58 { USER_APP_ID, "test_sm_book_2", "w" },
59 { USER_APP_ID, "test_sm_book_3", "x" },
60 { USER_APP_ID, "test_sm_book_4", "rw" },
61 { USER_APP_ID, "test_sm_book_5", "rx" },
62 { USER_APP_ID, "test_sm_book_6", "wx" },
63 { USER_APP_ID, "test_sm_book_7", "rwx" },
64 { "test_sm_subject_1", USER_APP_ID, "r" },
65 { "test_sm_subject_2", USER_APP_ID, "w" },
66 { "test_sm_subject_3", USER_APP_ID, "x" },
67 { "test_sm_subject_4", USER_APP_ID, "rw" },
68 { "test_sm_subject_5", USER_APP_ID, "rx" },
69 { "test_sm_subject_6", USER_APP_ID, "wx" },
70 { "test_sm_subject_7", USER_APP_ID, "rwx" }
73 static const char *const SM_PRIVATE_PATH = "/etc/smack/test_DIR/app_dir";
74 static const char *const SM_PUBLIC_PATH = "/etc/smack/test_DIR/app_dir_public";
75 static const char *const SM_PUBLIC_RO_PATH = "/etc/smack/test_DIR/app_dir_public_ro";
76 static const char *const SM_DENIED_PATH = "/etc/smack/test_DIR/non_app_dir";
79 static bool isLinkToExec(const char *fpath, const struct stat *sb)
86 // check if it's a link
87 if ( !S_ISLNK(sb->st_mode))
90 target = realpath(fpath, NULL);
91 RUNNER_ASSERT_MSG_BT(target != 0, "Could not obtain real path from link.");
93 ret = stat(target, &buf);
94 RUNNER_ASSERT_MSG_BT(ret == 0, "Could not obtain real path's stat from link.");
96 if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG))
103 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
104 const char* correctLabel, bool transmute_test, bool exec_test)
111 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
112 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
113 labelPtr.reset(label);
114 RUNNER_ASSERT_MSG_BT(label != NULL, "ACCESS label on " << fpath << " is not set");
115 result = strcmp(correctLabel, label);
116 RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is incorrect"
117 " (should be '" << correctLabel << "' and is '" << label << "')");
121 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
122 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
123 labelPtr.reset(label);
125 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
126 RUNNER_ASSERT_MSG_BT(label != NULL, "EXEC label on " << fpath << " is not set");
127 result = strcmp(correctLabel, label);
128 RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect EXEC label on executable file " << fpath);
130 RUNNER_ASSERT_MSG_BT(label == NULL, "EXEC label on " << fpath << " is set");
134 if (isLinkToExec(fpath, sb) && exec_test) {
135 char buf[SMACK_LABEL_LEN+1];
136 result = lgetxattr(fpath, XATTR_NAME_TIZENEXEC, buf, sizeof(buf));
137 RUNNER_ASSERT_MSG_BT(result != -1, "Could not get label for the path "
138 << fpath << "("<<strerror(errno)<<")");
140 result = strcmp(correctLabel, buf);
141 RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect TIZEN_EXEC_LABEL attribute"
142 " on link to executable " << fpath);
148 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
149 RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
150 labelPtr.reset(label);
152 if (S_ISDIR(sb->st_mode) && transmute_test == true) {
153 RUNNER_ASSERT_MSG_BT(label != NULL, "TRANSMUTE label on " << fpath << " is not set at all");
154 RUNNER_ASSERT_MSG_BT(strcmp(label,"TRUE") == 0,
155 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
157 RUNNER_ASSERT_MSG_BT(label == NULL, "TRANSMUTE label on " << fpath << " is set");
164 static int nftw_check_sm_labels_app_private_dir(const char *fpath, const struct stat *sb,
165 int /*typeflag*/, struct FTW* /*ftwbuf*/)
167 return nftw_check_sm_labels_app_dir(fpath, sb, USER_APP_ID, false, true);
170 static int nftw_check_sm_labels_app_public_dir(const char *fpath, const struct stat *sb,
171 int /*typeflag*/, struct FTW* /*ftwbuf*/)
174 return nftw_check_sm_labels_app_dir(fpath, sb, "User", true, false);
177 static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct stat *sb,
178 int /*typeflag*/, struct FTW* /*ftwbuf*/)
181 return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
184 static app_inst_req* do_app_inst_req_new()
187 app_inst_req *req = NULL;
189 result = security_manager_app_inst_req_new(&req);
190 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
191 "creation of new request failed. Result: " << result);
192 RUNNER_ASSERT_MSG_BT(req != NULL, "creation of new request did not allocate memory");
196 static void prepare_app_path()
200 result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
201 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
203 result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
204 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
206 result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
207 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
209 result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
210 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
213 static void prepare_app_env()
218 /* TODO: add parameters to this function */
219 static void check_app_path_after_install()
223 result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
224 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
226 result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
227 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
229 result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
230 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
232 result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
233 RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
236 static void check_app_permissions(const char *const app_id, const char *const pkg_id,
237 const privileges_t &allowed_privs, const privileges_t &denied_privs,
238 const rules_t &allowed_rules, const rules_t &denied_rules)
242 result = check_all_accesses(smack_check(), allowed_rules);
243 RUNNER_ASSERT_MSG_BT(result, "Permissions not added.");
244 result = check_no_accesses(smack_check(), denied_rules);
245 RUNNER_ASSERT_MSG_BT(result, "Permissions added.");
247 /* TODO: USER_APP_ID is hardcoded in the following checks, because libprivilege always generate
248 * label "User" for all installed apps. Adjust it when libprivilege is upgraded. */
249 (void)app_id; // unused parameter
250 (void)pkg_id; // unused parameter
252 for (auto it = allowed_privs.begin(); it != allowed_privs.end(); ++it)
253 check_perm_app_has_permission(USER_APP_ID, (*it).c_str(), true);
255 for (auto it = denied_privs.begin(); it != denied_privs.end(); ++it)
256 check_perm_app_has_permission(USER_APP_ID, (*it).c_str(), false);
259 static void check_app_after_install(const char *const app_id, const char *const pkg_id,
260 const privileges_t &allowed_privs, const privileges_t &denied_privs,
261 const rules_t &allowed_rules, const rules_t &denied_rules)
263 check_app_permissions(app_id, pkg_id,
264 allowed_privs, denied_privs,
265 allowed_rules, denied_rules);
268 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
271 RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
274 AppInstReqUniquePtr request;
276 request.reset(do_app_inst_req_new());
278 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
279 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
280 "setting app id failed. Result: " << result);
282 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID1);
283 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
284 "setting pkg id failed. Result: " << result);
286 result = security_manager_app_install(request.get());
287 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
288 "installing app failed. Result: " << result);
290 result = security_manager_app_install(request.get());
291 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
292 "installing already installed app failed. Result: " << result);
294 request.reset(do_app_inst_req_new());
296 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
297 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
298 "setting app id failed. Result: " << result);
300 result = security_manager_app_uninstall(request.get());
301 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
302 "uninstalling app failed. Result: " << result);
304 result = security_manager_app_uninstall(request.get());
305 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
306 "uninstalling already uninstalled app failed. Result: " << result);
309 RUNNER_TEST(security_manager_02_app_install_uninstall_full)
312 AppInstReqUniquePtr request;
316 request.reset(do_app_inst_req_new());
318 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
319 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
320 "setting app id failed. Result: " << result);
322 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
323 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
324 "setting pkg id failed. Result: " << result);
326 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[0].c_str());
327 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
328 "setting allowed permission failed. Result: " << result);
329 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[1].c_str());
330 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
331 "setting allowed permission failed. Result: " << result);
333 result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
334 SECURITY_MANAGER_PATH_PRIVATE);
335 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
336 "setting allowed path failed. Result: " << result);
338 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
339 SECURITY_MANAGER_PATH_PUBLIC);
340 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
341 "setting allowed path failed. Result: " << result);
343 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
344 SECURITY_MANAGER_PATH_PUBLIC_RO);
345 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
346 "setting allowed path failed. Result: " << result);
348 result = security_manager_app_install(request.get());
349 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
350 "installing app failed. Result: " << result);
352 check_app_after_install(SM_APP_ID2, SM_PKG_ID2,
353 SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES,
354 SM_ALLOWED_RULES, SM_DENIED_RULES);
356 /* TODO: add parameters to this function */
357 check_app_path_after_install();
359 request.reset(do_app_inst_req_new());
361 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
362 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
363 "setting app id failed. Result: " << result);
365 result = security_manager_app_uninstall(request.get());
366 RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
367 "uninstalling app failed. Result: " << result);
371 int main(int argc, char *argv[])
373 SummaryCollector::Register();
374 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);