1 #include <dpl/log/log.h>
2 #include <dpl/test/test_runner.h>
7 #include <unordered_set>
8 #include <sys/capability.h>
11 #include <sys/socket.h>
13 #include <attr/xattr.h>
14 #include <linux/xattr.h>
17 #include <sys/types.h>
23 #include <libprivilege-control_test_common.h>
24 #include <tests_common.h>
26 #include <tzplatform_config.h>
27 #include <security-manager.h>
30 #include <sm_request.h>
31 #include <sm_user_request.h>
32 #include <temp_test_user.h>
33 #include <cynara_test_client.h>
34 #include <cynara_test_admin.h>
35 #include <service_manager.h>
37 using namespace SecurityManagerTest;
39 DEFINE_SMARTPTR(cap_free, _cap_struct, CapsSetsUniquePtr);
40 DEFINE_SMARTPTR(tzplatform_context_destroy, tzplatform_context, TzPlatformContextPtr);
42 static const privileges_t SM_ALLOWED_PRIVILEGES = {
43 "http://tizen.org/privilege/location",
44 "http://tizen.org/privilege/camera"
47 static const privileges_t SM_DENIED_PRIVILEGES = {
48 "http://tizen.org/privilege/bluetooth",
49 "http://tizen.org/privilege/power"
52 static const privileges_t SM_NO_PRIVILEGES = {
55 static const std::vector<std::string> SM_ALLOWED_GROUPS = {"db_browser", "db_alarm"};
57 static const char *const SM_PRIVATE_PATH = "/usr/apps/test_DIR/app_dir";
58 static const char *const SM_PUBLIC_RO_PATH = "/usr/apps/test_DIR/app_dir_public_ro";
59 static const char *const SM_DENIED_PATH = "/usr/apps/test_DIR/non_app_dir";
60 static const char *const ANY_USER_REPRESENTATION = "anyuser";/*this may be actually any string*/
61 static const std::string EXEC_FILE("exec");
62 static const std::string NORMAL_FILE("normal");
63 static const std::string LINK_PREFIX("link_to_");
65 static const std::string PRIVILEGE_MANAGER_APP = "privilege_manager";
66 static const std::string PRIVILEGE_MANAGER_PKG = "privilege_manager";
67 static const std::string PRIVILEGE_MANAGER_SELF_PRIVILEGE = "http://tizen.org/privilege/systemsettings";
68 static const std::string PRIVILEGE_MANAGER_ADMIN_PRIVILEGE = "http://tizen.org/privilege/systemsettings.admin";
70 static const std::vector<std::string> MANY_APPS = {
71 "security_manager_10_app_1",
72 "security_manager_10_app_2",
73 "security_manager_10_app_3",
74 "security_manager_10_app_4",
75 "security_manager_10_app_5"
78 static const std::map<std::string, std::string> MANY_APPS_PKGS = {
79 {"security_manager_10_app_1", "security_manager_10_pkg_1"},
80 {"security_manager_10_app_2", "security_manager_10_pkg_2"},
81 {"security_manager_10_app_3", "security_manager_10_pkg_3"},
82 {"security_manager_10_app_4", "security_manager_10_pkg_4"},
83 {"security_manager_10_app_5", "security_manager_10_pkg_5"},
84 {PRIVILEGE_MANAGER_APP, PRIVILEGE_MANAGER_PKG}
87 static const std::vector<privileges_t> MANY_APPS_PRIVILEGES = {
89 "http://tizen.org/privilege/internet",
90 "http://tizen.org/privilege/location"
93 "http://tizen.org/privilege/telephony",
94 "http://tizen.org/privilege/camera"
97 "http://tizen.org/privilege/contact.read",
98 "http://tizen.org/privilege/led",
99 "http://tizen.org/privilege/email"
102 "http://tizen.org/privilege/led",
103 "http://tizen.org/privilege/email",
104 "http://tizen.org/privilege/telephony",
105 "http://tizen.org/privilege/camera"
108 "http://tizen.org/privilege/internet",
109 "http://tizen.org/privilege/location",
110 "http://tizen.org/privilege/led",
111 "http://tizen.org/privilege/email"
115 static void generateAppLabel(const std::string &pkgId, std::string &label)
121 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
122 const char* correctLabel, bool transmute_test, bool exec_test)
126 char* label = nullptr;
129 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
130 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
131 labelPtr.reset(label);
132 RUNNER_ASSERT_MSG(label != nullptr, "ACCESS label on " << fpath << " is not set");
133 result = strcmp(correctLabel, label);
134 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect"
135 " (should be '" << correctLabel << "' and is '" << label << "')");
139 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
140 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
141 labelPtr.reset(label);
143 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
144 RUNNER_ASSERT_MSG(label != nullptr, "EXEC label on " << fpath << " is not set");
145 result = strcmp(correctLabel, label);
146 RUNNER_ASSERT_MSG(result == 0, "Incorrect EXEC label on executable file " << fpath);
148 RUNNER_ASSERT_MSG(label == nullptr, "EXEC label on " << fpath << " is set");
152 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
153 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
154 labelPtr.reset(label);
156 if (S_ISDIR(sb->st_mode) && transmute_test == true) {
157 RUNNER_ASSERT_MSG(label != nullptr, "TRANSMUTE label on " << fpath << " is not set at all");
158 RUNNER_ASSERT_MSG(strcmp(label,"TRUE") == 0,
159 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
161 RUNNER_ASSERT_MSG(label == nullptr, "TRANSMUTE label on " << fpath << " is set");
168 static int nftw_check_sm_labels_app_private_dir(const char *fpath, const struct stat *sb,
169 int /*typeflag*/, struct FTW* /*ftwbuf*/)
171 return nftw_check_sm_labels_app_dir(fpath, sb, USER_APP_ID, false, true);
174 static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct stat *sb,
175 int /*typeflag*/, struct FTW* /*ftwbuf*/)
178 return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
181 static void prepare_app_path()
185 result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
186 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
188 result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
189 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
191 result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
192 RUNNER_ASSERT_MSG(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
195 static void prepare_app_env()
200 /* TODO: add parameters to this function */
201 static void check_app_path_after_install()
205 result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
206 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
208 result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
209 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
211 result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
212 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
216 static void check_app_permissions(const char *const app_id, const char *const pkg_id, const char *const user,
217 const privileges_t &allowed_privs, const privileges_t &denied_privs)
220 std::string smackLabel;
221 generateAppLabel(pkg_id, smackLabel);
223 CynaraTestClient::Client ctc;
225 for (auto &priv : allowed_privs) {
226 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_ALLOWED);
229 for (auto &priv : denied_privs) {
230 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_DENIED);
234 static void check_app_gids(const char *const app_id, const std::vector<gid_t> &allowed_gids)
237 gid_t main_gid = getgid();
238 std::unordered_set<gid_t> reference_gids(allowed_gids.begin(), allowed_gids.end());
240 // Reset supplementary groups
241 ret = setgroups(0, NULL);
242 RUNNER_ASSERT_MSG(ret != -1, "Unable to set supplementary groups");
244 Api::setProcessGroups(app_id);
246 ret = getgroups(0, nullptr);
247 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
249 std::vector<gid_t> actual_gids(ret);
250 ret = getgroups(ret, actual_gids.data());
251 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
253 for (const auto &gid : actual_gids) {
254 RUNNER_ASSERT_MSG(gid == main_gid || reference_gids.count(gid) > 0,
255 "Application shouldn't get access to group " << gid);
256 reference_gids.erase(gid);
259 RUNNER_ASSERT_MSG(reference_gids.empty(), "Application didn't get access to some groups");
262 static void check_app_after_install(const char *const app_id, const char *const pkg_id,
263 const privileges_t &allowed_privs,
264 const privileges_t &denied_privs,
265 const std::vector<std::string> &allowed_groups)
267 TestSecurityManagerDatabase dbtest;
268 dbtest.test_db_after__app_install(app_id, pkg_id, allowed_privs);
269 dbtest.check_privileges_removed(app_id, pkg_id, denied_privs);
271 /*Privileges should be granted to all users if root installs app*/
272 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, allowed_privs, denied_privs);
274 /* Setup mapping of gids to privileges */
275 /* Do this for each privilege for extra check */
276 for (const auto &privilege : allowed_privs) {
277 dbtest.setup_privilege_groups(privilege, allowed_groups);
280 std::vector<gid_t> allowed_gids;
282 for (const auto &groupName : allowed_groups) {
284 struct group* grp = getgrnam(groupName.c_str());
285 RUNNER_ASSERT_ERRNO_MSG(grp, "Group: " << groupName << " not found");
286 allowed_gids.push_back(grp->gr_gid);
289 check_app_gids(app_id, allowed_gids);
292 static void check_app_after_install(const char *const app_id, const char *const pkg_id)
294 TestSecurityManagerDatabase dbtest;
295 dbtest.test_db_after__app_install(app_id, pkg_id);
298 static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
299 const privileges_t &privileges, const bool is_pkg_removed)
301 TestSecurityManagerDatabase dbtest;
302 dbtest.test_db_after__app_uninstall(app_id, pkg_id, privileges, is_pkg_removed);
305 /*Privileges should not be granted anymore to any user*/
306 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, SM_NO_PRIVILEGES, privileges);
309 static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
310 const bool is_pkg_removed)
312 TestSecurityManagerDatabase dbtest;
313 dbtest.test_db_after__app_uninstall(app_id, pkg_id, is_pkg_removed);
316 static void install_app(const char *app_id, const char *pkg_id, uid_t uid = 0)
318 InstallRequest request;
319 request.setAppId(app_id);
320 request.setPkgId(pkg_id);
322 Api::install(request);
324 check_app_after_install(app_id, pkg_id);
328 static void uninstall_app(const char *app_id, const char *pkg_id, bool expect_pkg_removed)
330 InstallRequest request;
331 request.setAppId(app_id);
333 Api::uninstall(request);
335 check_app_after_uninstall(app_id, pkg_id, expect_pkg_removed);
338 static inline void register_current_process_as_privilege_manager(uid_t uid, bool forAdmin = false)
340 InstallRequest request;
341 request.setAppId(PRIVILEGE_MANAGER_APP.c_str());
342 request.setPkgId(PRIVILEGE_MANAGER_PKG.c_str());
344 request.addPrivilege(PRIVILEGE_MANAGER_SELF_PRIVILEGE.c_str());
346 request.addPrivilege(PRIVILEGE_MANAGER_ADMIN_PRIVILEGE.c_str());
347 Api::install(request);
348 Api::setProcessLabel(PRIVILEGE_MANAGER_APP.c_str());
351 static inline struct passwd *getUserStruct(const std::string &userName) {
352 struct passwd *pw = nullptr;
355 while(!(pw = getpwnam(userName.c_str()))) {
356 RUNNER_ASSERT_ERRNO_MSG(errno == EINTR, "getpwnam() failed");
362 static inline struct passwd *getUserStruct(const uid_t uid) {
363 struct passwd *pw = nullptr;
366 while(!(pw = getpwuid(uid))) {
367 RUNNER_ASSERT_ERRNO_MSG(errno == EINTR, "getpwnam() failed");
373 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
376 RUNNER_TEST(security_manager_01a_app_double_install_double_uninstall)
378 const char *const sm_app_id = "sm_test_01a_app_id_double";
379 const char *const sm_pkg_id = "sm_test_01a_pkg_id_double";
381 InstallRequest requestInst;
382 requestInst.setAppId(sm_app_id);
383 requestInst.setPkgId(sm_pkg_id);
385 Api::install(requestInst);
386 Api::install(requestInst);
388 /* Check records in the security-manager database */
389 check_app_after_install(sm_app_id, sm_pkg_id);
391 InstallRequest requestUninst;
392 requestUninst.setAppId(sm_app_id);
394 Api::uninstall(requestUninst);
395 Api::uninstall(requestUninst);
397 /* Check records in the security-manager database */
398 check_app_after_uninstall(sm_app_id, sm_pkg_id, TestSecurityManagerDatabase::REMOVED);
402 RUNNER_TEST(security_manager_01b_app_double_install_wrong_pkg_id)
404 const char *const sm_app_id = "sm_test_01b_app";
405 const char *const sm_pkg_id = "sm_test_01b_pkg";
406 const char *const sm_pkg_id_wrong = "sm_test_01b_pkg_BAD";
408 InstallRequest requestInst;
409 requestInst.setAppId(sm_app_id);
410 requestInst.setPkgId(sm_pkg_id);
412 Api::install(requestInst);
414 InstallRequest requestInst2;
415 requestInst2.setAppId(sm_app_id);
416 requestInst2.setPkgId(sm_pkg_id_wrong);
418 Api::install(requestInst2, SECURITY_MANAGER_ERROR_INPUT_PARAM);
421 /* Check records in the security-manager database */
422 check_app_after_install(sm_app_id, sm_pkg_id);
424 InstallRequest requestUninst;
425 requestUninst.setAppId(sm_app_id);
427 Api::uninstall(requestUninst);
430 /* Check records in the security-manager database */
431 check_app_after_uninstall(sm_app_id, sm_pkg_id, TestSecurityManagerDatabase::REMOVED);
435 RUNNER_TEST(security_manager_01c_app_uninstall_pkg_id_ignored)
437 const char * const sm_app_id = "SM_TEST_01c_APPID";
438 const char * const sm_pkg_id = "SM_TEST_01c_PKGID";
439 const char * const sm_pkg_id_wrong = "SM_TEST_01c_PKGID_wrong";
441 InstallRequest requestInst;
442 requestInst.setAppId(sm_app_id);
443 requestInst.setPkgId(sm_pkg_id);
445 Api::install(requestInst);
447 /* Check records in the security-manager database */
448 check_app_after_install(sm_app_id, sm_pkg_id);
450 InstallRequest requestUninst;
451 requestUninst.setAppId(sm_app_id);
452 requestUninst.setPkgId(sm_pkg_id_wrong);
454 Api::uninstall(requestUninst);
456 check_app_after_uninstall(sm_app_id, sm_pkg_id, TestSecurityManagerDatabase::REMOVED);
460 RUNNER_TEST(security_manager_02_app_install_uninstall_full)
462 const char *const sm_app_id = "sm_test_02_app_id_full";
463 const char *const sm_pkg_id = "sm_test_02_pkg_id_full";
467 InstallRequest requestInst;
468 requestInst.setAppId(sm_app_id);
469 requestInst.setPkgId(sm_pkg_id);
470 requestInst.addPrivilege(SM_ALLOWED_PRIVILEGES[0].c_str());
471 requestInst.addPrivilege(SM_ALLOWED_PRIVILEGES[1].c_str());
472 requestInst.addPath(SM_PRIVATE_PATH, SECURITY_MANAGER_PATH_PRIVATE);
473 requestInst.addPath(SM_PUBLIC_RO_PATH, SECURITY_MANAGER_PATH_PUBLIC_RO);
475 Api::install(requestInst);
477 /* Check records in the security-manager database */
478 check_app_after_install(sm_app_id, sm_pkg_id,
479 SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES, SM_ALLOWED_GROUPS);
481 /* TODO: add parameters to this function */
482 check_app_path_after_install();
484 InstallRequest requestUninst;
485 requestUninst.setAppId(sm_app_id);
487 Api::uninstall(requestUninst);
489 /* Check records in the security-manager database,
490 * all previously allowed privileges should be removed */
491 check_app_after_uninstall(sm_app_id, sm_pkg_id,
492 SM_ALLOWED_PRIVILEGES, TestSecurityManagerDatabase::REMOVED);
495 RUNNER_CHILD_TEST_SMACK(security_manager_03_set_label_from_appid)
497 const char *const app_id = "sm_test_03_app_id_set_label_from_appid_smack";
498 const char *const pkg_id = "sm_test_03_pkg_id_set_label_from_appid_smack";
499 const char *const expected_label = USER_APP_ID;
500 const char *const socketLabel = "not_expected_label";
501 char *label = nullptr;
505 uninstall_app(app_id, pkg_id, true);
506 install_app(app_id, pkg_id);
508 struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
509 //Clean up before creating socket
511 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
512 RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed");
513 SockUniquePtr sockPtr(&sock);
514 //Bind socket to address
515 result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
516 RUNNER_ASSERT_ERRNO_MSG(result == 0, "bind failed");
517 //Set socket label to something different than expecedLabel
518 result = fsetxattr(sock, XATTR_NAME_SMACKIPIN, socketLabel,
519 strlen(socketLabel), 0);
520 RUNNER_ASSERT_ERRNO_MSG(result == 0,
521 "Can't set socket label. Result: " << result);
522 result = fsetxattr(sock, XATTR_NAME_SMACKIPOUT, socketLabel,
523 strlen(socketLabel), 0);
524 RUNNER_ASSERT_ERRNO_MSG(result == 0,
525 "Can't set socket label. Result: " << result);
527 Api::setProcessLabel(app_id);
529 char value[SMACK_LABEL_LEN + 1];
531 size = fgetxattr(sock, XATTR_NAME_SMACKIPIN, value, sizeof(value));
532 RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
533 result = strcmp(expected_label, value);
534 RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
535 expected_label << " Actual: " << value);
537 size = fgetxattr(sock, XATTR_NAME_SMACKIPOUT, value, sizeof(value));
538 RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
539 result = strcmp(expected_label, value);
540 RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
541 expected_label << " Actual: " << value);
543 result = smack_new_label_from_self(&label);
544 RUNNER_ASSERT_MSG(result >= 0,
545 " Error getting current process label");
546 RUNNER_ASSERT_MSG(label != nullptr,
547 " Process label is not set");
548 labelPtr.reset(label);
550 result = strcmp(expected_label, label);
551 RUNNER_ASSERT_MSG(result == 0,
552 " Process label is incorrect. Expected: \"" << expected_label <<
553 "\" Actual: \"" << label << "\"");
555 uninstall_app(app_id, pkg_id, true);
558 RUNNER_CHILD_TEST_NOSMACK(security_manager_03_set_label_from_appid_nosmack)
560 const char *const app_id = "sm_test_03_app_id_set_label_from_appid_nosmack";
561 const char *const pkg_id = "sm_test_03_pkg_id_set_label_from_appid_nosmack";
563 uninstall_app(app_id, pkg_id, true);
564 install_app(app_id, pkg_id);
566 Api::setProcessLabel(app_id);
568 uninstall_app(app_id, pkg_id, true);
571 static void prepare_request(InstallRequest &request,
572 const char *const app_id,
573 const char *const pkg_id,
574 app_install_path_type pathType,
575 const char *const path,
578 request.setAppId(app_id);
579 request.setPkgId(pkg_id);
580 request.addPath(path, pathType);
586 static uid_t getGlobalUserId(void)
588 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
591 static const std::string appDirPath(const TemporaryTestUser &user)
593 struct tzplatform_context *tzCtxPtr = nullptr;
595 RUNNER_ASSERT(0 == tzplatform_context_create(&tzCtxPtr));
596 TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
598 RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, user.getUid()),
599 "Unable to set user <" << user.getUserName() << "> for tzplatform context");
601 const char *appDir = tzplatform_context_getenv(tzCtxPtr,
602 getGlobalUserId() == user.getUid() ? TZ_SYS_RW_APP : TZ_USER_APP);
603 RUNNER_ASSERT_MSG(nullptr != appDir,
604 "tzplatform_context_getenv failed"
605 << "for getting sys rw app of user <" << user.getUserName() << ">");
610 static const std::string nonAppDirPath(const TemporaryTestUser &user)
612 return TMP_DIR + "/" + user.getUserName();
615 static const std::string uidToStr(const uid_t uid)
617 return std::to_string(static_cast<unsigned int>(uid));
620 static void install_and_check(const char *const sm_app_id,
621 const char *const sm_pkg_id,
622 const TemporaryTestUser& user,
623 const std::string &appDir,
626 InstallRequest requestPublic;
628 //install app for non-root user and try to register public path (should fail)
629 prepare_request(requestPublic, sm_app_id, sm_pkg_id,
630 SECURITY_MANAGER_PATH_PUBLIC, appDir.c_str(),
631 requestUid ? user.getUid() : 0);
633 Api::install(requestPublic, SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED);
635 InstallRequest requestPrivate;
637 //install app for non-root user
638 //should fail (users may only register folders inside their home)
639 prepare_request(requestPrivate, sm_app_id, sm_pkg_id,
640 SECURITY_MANAGER_PATH_PRIVATE, SM_PRIVATE_PATH,
641 requestUid ? user.getUid() : 0);
643 Api::install(requestPrivate, SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED);
645 InstallRequest requestPrivateUser;
647 //install app for non-root user
648 //should succeed - this time i register folder inside user's home dir
649 prepare_request(requestPrivateUser, sm_app_id, sm_pkg_id,
650 SECURITY_MANAGER_PATH_PRIVATE, appDir.c_str(),
651 requestUid ? user.getUid() : 0);
653 for (auto &privilege : SM_ALLOWED_PRIVILEGES)
654 requestPrivateUser.addPrivilege(privilege.c_str());
656 Api::install(requestPrivateUser);
658 check_app_permissions(sm_app_id, sm_pkg_id,
659 uidToStr(user.getUid()).c_str(),
660 SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES);
663 static void createTestDir(const std::string &dir)
665 mode_t dirMode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
666 mode_t execFileMode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
667 mode_t normalFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
669 mkdirSafe(dir, dirMode);
670 creatSafe(dir + "/" + EXEC_FILE, execFileMode);
671 creatSafe(dir + "/" + NORMAL_FILE, normalFileMode);
672 symlinkSafe(dir + "/" + EXEC_FILE, dir + "/" + LINK_PREFIX + EXEC_FILE);
673 symlinkSafe(dir + "/" + NORMAL_FILE, dir + "/" + LINK_PREFIX + NORMAL_FILE);
676 static void createInnerAppDir(const std::string &dir, const std::string &nonAppDir)
680 symlinkSafe(nonAppDir, dir + "/" + LINK_PREFIX + "non_app_dir");
681 symlinkSafe(nonAppDir + "/" + EXEC_FILE,
682 dir + "/" + LINK_PREFIX + "non_app_" + EXEC_FILE);
683 symlinkSafe(nonAppDir + "/" + NORMAL_FILE,
684 dir + "/" + LINK_PREFIX + "non_app_" + NORMAL_FILE);
687 static void generateAppDir(const TemporaryTestUser &user)
689 const std::string dir = appDirPath(user);
690 const std::string nonAppDir = nonAppDirPath(user);
692 createInnerAppDir(dir, nonAppDir);
693 createInnerAppDir(dir + "/.inner_dir", nonAppDir);
694 createInnerAppDir(dir + "/inner_dir", nonAppDir);
697 static void generateNonAppDir(const TemporaryTestUser &user)
699 const std::string dir = nonAppDirPath(user);
702 createTestDir(dir + "/.inner_dir");
703 createTestDir(dir + "/inner_dir");
706 static void createTestDirs(const TemporaryTestUser &user)
708 generateAppDir(user);
709 generateNonAppDir(user);
712 static void removeTestDirs(const TemporaryTestUser &user)
714 removeDir(appDirPath(user));
715 removeDir(nonAppDirPath(user));
718 RUNNER_CHILD_TEST(security_manager_04a_app_install_uninstall_by_app_user_for_self)
721 const char *const sm_app_id = "sm_test_04a_app_id_uid";
722 const char *const sm_pkg_id = "sm_test_04a_pkg_id_uid";
723 const std::string new_user_name = "sm_test_04a_user_name";
725 TemporaryTestUser testUser(new_user_name, GUM_USERTYPE_NORMAL, false);
728 removeTestDirs(testUser);
729 createTestDirs(testUser);
731 const std::string userAppDirPath = appDirPath(testUser);
733 //switch user to non-root
734 result = drop_root_privileges(testUser.getUid(), testUser.getGid());
735 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
737 install_and_check(sm_app_id, sm_pkg_id, testUser, userAppDirPath, false);
739 //uninstall app as non-root user
740 InstallRequest request;
741 request.setAppId(sm_app_id);
743 Api::uninstall(request);
745 check_app_permissions(sm_app_id, sm_pkg_id,
746 uidToStr(testUser.getUid()).c_str(),
747 SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
750 RUNNER_CHILD_TEST(security_manager_04b_app_install_by_root_for_app_user)
753 const char *const sm_app_id = "sm_test_04b_app_id_uid";
754 const char *const sm_pkg_id = "sm_test_04b_pkg_id_uid";
755 const std::string new_user_name = "sm_test_04b_user_name";
757 TemporaryTestUser testUser(new_user_name, GUM_USERTYPE_NORMAL, false);
760 removeTestDirs(testUser);
761 createTestDirs(testUser);
763 install_and_check(sm_app_id, sm_pkg_id, testUser, appDirPath(testUser), true);
765 //switch user to non-root - root may not uninstall apps for specified users
766 result = drop_root_privileges(testUser.getUid(), testUser.getGid());
767 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
769 //uninstall app as non-root user
770 InstallRequest request;
771 request.setAppId(sm_app_id);
773 Api::uninstall(request);
775 check_app_permissions(sm_app_id, sm_pkg_id,
776 uidToStr(testUser.getUid()).c_str(),
777 SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
781 RUNNER_CHILD_TEST(security_manager_05_drop_process_capabilities)
784 CapsSetsUniquePtr caps, caps_empty(cap_init());
786 caps.reset(cap_from_text("all=eip"));
787 RUNNER_ASSERT_MSG(caps, "can't convert capabilities from text");
788 result = cap_set_proc(caps.get());
789 RUNNER_ASSERT_MSG(result == 0,
790 "can't set capabilities. Result: " << result);
792 Api::dropProcessPrivileges();
794 caps.reset(cap_get_proc());
795 RUNNER_ASSERT_MSG(caps, "can't get proc capabilities");
797 result = cap_compare(caps.get(), caps_empty.get());
798 RUNNER_ASSERT_MSG(result == 0,
799 "capabilities not dropped. Current: " << cap_to_text(caps.get(), NULL));
802 RUNNER_CHILD_TEST(security_manager_06_install_app_offline)
804 const char *const app_id = "sm_test_06_app_id_install_app_offline";
805 const char *const pkg_id = "sm_test_06_pkg_id_install_app_offline";
806 ServiceManager serviceManager("security-manager.service");
808 uninstall_app(app_id, pkg_id, true);
809 serviceManager.maskService();
810 serviceManager.stopService();
812 install_app(app_id, pkg_id);
814 serviceManager.unmaskService();
815 serviceManager.startService();
817 uninstall_app(app_id, pkg_id, true);
820 RUNNER_CHILD_TEST(security_manager_07_user_add_app_install)
822 const char *const sm_app_id = "sm_test_07_app_id_user";
823 const char *const sm_pkg_id = "sm_test_07_pkg_id_user";
824 const std::string new_user_name = "sm_test_07_user_name";
825 std::string uid_string;
826 TemporaryTestUser test_user(new_user_name, GUM_USERTYPE_NORMAL, false);
828 uid_string = std::to_string(static_cast<unsigned int>(test_user.getUid()));
830 install_app(sm_app_id, sm_pkg_id, test_user.getUid());
832 check_app_after_install(sm_app_id, sm_pkg_id);
836 check_app_permissions(sm_app_id, sm_pkg_id, uid_string.c_str(), SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
838 check_app_after_uninstall(sm_app_id, sm_pkg_id, true);
841 RUNNER_CHILD_TEST(security_manager_08_user_double_add_double_remove)
843 UserRequest addUserRequest;
845 const char *const sm_app_id = "sm_test_08_app_id_user";
846 const char *const sm_pkg_id = "sm_test_08_pkg_id_user";
847 const char *const new_user_name = "sm_test_08_user_name";
848 std::string uid_string;
851 TemporaryTestUser test_user(new_user_name, GUM_USERTYPE_NORMAL, false);
853 uid_string = std::to_string(static_cast<unsigned int>(test_user.getUid()));
855 addUserRequest.setUid(test_user.getUid());
856 addUserRequest.setUserType(SM_USER_TYPE_NORMAL);
859 Api::addUser(addUserRequest);
861 install_app(sm_app_id, sm_pkg_id, test_user.getUid());
863 check_app_after_install(sm_app_id, sm_pkg_id);
867 UserRequest deleteUserRequest;
868 deleteUserRequest.setUid(test_user.getUid());
870 Api::deleteUser(deleteUserRequest);
872 check_app_permissions(sm_app_id, sm_pkg_id, uid_string.c_str(), SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
874 check_app_after_uninstall(sm_app_id, sm_pkg_id, true);
877 RUNNER_CHILD_TEST(security_manager_09_add_user_offline)
879 const char *const app_id = "security_manager_09_add_user_offline_app";
880 const char *const pkg_id = "security_manager_09_add_user_offline_pkg";
881 const std::string username("sm_test_09_user_name");
882 ServiceManager serviceManager("security-manager.service");
883 serviceManager.maskService();
884 serviceManager.stopService();
886 TemporaryTestUser user(username, GUM_USERTYPE_NORMAL, true);
889 install_app(app_id, pkg_id, user.getUid());
891 check_app_after_install(app_id, pkg_id);
893 serviceManager.unmaskService();
894 serviceManager.startService();
898 check_app_after_uninstall(app_id, pkg_id, true);
901 RUNNER_TEST(security_manager_10_privacy_manager_fetch_whole_policy_for_self)
904 const std::string username("sm_test_10_user_name");
905 unsigned int privileges_count = 0;
907 std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
908 std::map<std::string, std::set<std::string>> apps2PrivsMap;
910 for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
911 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
912 MANY_APPS.at(i), std::set<std::string>(
913 MANY_APPS_PRIVILEGES.at(i).begin(),
914 MANY_APPS_PRIVILEGES.at(i).end())));
915 privileges_count+=MANY_APPS_PRIVILEGES.at(i).size();
918 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
919 PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE}));
921 users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
926 RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
928 RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
931 if (pid != 0) { //parent process
932 TemporaryTestUser tmpUser(username, GUM_USERTYPE_NORMAL, false);
935 for(const auto &user : users2AppsMap) {
937 for(const auto &app : user.second) {
938 InstallRequest requestInst;
939 requestInst.setAppId(app.first.c_str());
941 requestInst.setPkgId(MANY_APPS_PKGS.at(app.first).c_str());
942 } catch (const std::out_of_range &e) {
943 RUNNER_FAIL_MSG("Couldn't find package for app: " << app.first);
945 requestInst.setUid(tmpUser.getUid());
947 for (const auto &privilege : app.second) {
948 requestInst.addPrivilege(privilege.c_str());
951 Api::install(requestInst);
954 //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
956 //Start child process
958 RUNNER_ASSERT_MSG(sem_post(mutex) == 0, "Error while opening mutex, errno: " << errno);
966 if (pid == 0) { //child process
968 RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child process failed, errno: " << errno);
969 //the above call, registers 1 new privilege for the given user, hence the incrementation of below variable
971 struct passwd *pw = getUserStruct(username);
972 register_current_process_as_privilege_manager(pw->pw_uid);
973 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
974 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
976 std::vector<PolicyEntry> policyEntries;
978 Api::getPolicy(filter, policyEntries);
980 RUNNER_ASSERT_MSG(policyEntries.size() != 0, "Policy is empty");
981 RUNNER_ASSERT_MSG(policyEntries.size() == privileges_count, "Number of policies doesn't match - should be: " << privileges_count << " and is " << policyEntries.size());
983 for (const auto &policyEntry : policyEntries) {
984 std::string user = policyEntry.getUser();
985 std::string app = policyEntry.getAppId();
986 std::string privilege = policyEntry.getPrivilege();
989 struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
990 std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
991 if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
992 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
993 } catch (const std::out_of_range &e) {
994 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
995 } catch (const std::invalid_argument& e) {
996 RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
1003 RUNNER_TEST(security_manager_11_privacy_manager_fetch_whole_policy_for_admin_unprivileged)
1006 const std::vector<std::string> usernames = {"sm_test_11_user_name_1", "sm_test_11_user_name_2"};
1007 unsigned int privileges_count = 0;
1009 std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
1010 std::map<std::string, std::set<std::string>> apps2PrivsMap;
1012 for (const auto &username : usernames) {
1013 //Only entries for one of the users will be listed
1014 privileges_count = 0;
1016 for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
1017 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
1018 MANY_APPS.at(i), std::set<std::string>(
1019 MANY_APPS_PRIVILEGES.at(i).begin(),
1020 MANY_APPS_PRIVILEGES.at(i).end())));
1021 privileges_count+=MANY_APPS_PRIVILEGES.at(i).size();
1024 users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
1027 users2AppsMap.at(usernames.at(0)).insert(std::pair<std::string, std::set<std::string>>(
1028 PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE}));
1035 RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
1037 RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
1040 if (pid != 0) { //parent process
1041 std::vector<TemporaryTestUser> users = {
1042 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
1043 TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
1046 users.at(0).create();
1047 users.at(1).create();
1049 //Install apps for both users
1050 for(const auto &user : users) {
1051 for(const auto &app : users2AppsMap.at(user.getUserName())) {
1052 InstallRequest requestInst;
1053 requestInst.setAppId(app.first.c_str());
1055 requestInst.setPkgId(MANY_APPS_PKGS.at(app.first).c_str());
1056 } catch (const std::out_of_range &e) {
1057 RUNNER_FAIL_MSG("Couldn't find package for app: " << app.first);
1059 requestInst.setUid(user.getUid());
1061 for (const auto &privilege : app.second) {
1062 requestInst.addPrivilege(privilege.c_str());
1065 Api::install(requestInst);
1068 //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
1072 RUNNER_ASSERT_MSG(sem_post(mutex) == 0, "Error while opening mutex, errno: " << errno);
1077 for(auto &user : users) {
1084 RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child failed, errno: " << errno);
1085 struct passwd *pw = getUserStruct(usernames.at(0));
1086 register_current_process_as_privilege_manager(pw->pw_uid);
1088 //change uid to normal user
1090 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
1091 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1093 std::vector<PolicyEntry> policyEntries;
1096 //this call should only return privileges belonging to the current uid
1097 Api::getPolicy(filter, policyEntries);
1099 RUNNER_ASSERT_MSG(policyEntries.size() != 0, "Policy is empty");
1100 RUNNER_ASSERT_MSG(policyEntries.size() == privileges_count, "Number of policies doesn't match - should be: " << privileges_count << " and is " << policyEntries.size());
1102 for (const auto &policyEntry : policyEntries) {
1103 std::string user = policyEntry.getUser();
1104 std::string app = policyEntry.getAppId();
1105 std::string privilege = policyEntry.getPrivilege();
1108 struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
1109 std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
1110 if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
1111 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
1112 } catch (const std::out_of_range &e) {
1113 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
1114 } catch (const std::invalid_argument& e) {
1115 RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
1122 RUNNER_TEST(security_manager_12_privacy_manager_fetch_whole_policy_for_admin_privileged)
1125 const std::vector<std::string> usernames = {"sm_test_12_user_name_1", "sm_test_12_user_name_2"};
1126 unsigned int privileges_count = 0;
1128 std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
1129 std::map<std::string, std::set<std::string>> apps2PrivsMap;
1131 for (const auto &username : usernames) {
1133 for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
1134 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
1135 MANY_APPS.at(i), std::set<std::string>(
1136 MANY_APPS_PRIVILEGES.at(i).begin(),
1137 MANY_APPS_PRIVILEGES.at(i).end())));
1138 privileges_count+=MANY_APPS_PRIVILEGES.at(i).size();
1141 users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
1144 users2AppsMap.at(usernames.at(1)).insert(std::pair<std::string, std::set<std::string>>(
1145 PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE, PRIVILEGE_MANAGER_ADMIN_PRIVILEGE}));
1147 privileges_count += 2;
1152 RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
1154 RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
1157 if (pid != 0) { //parent process
1158 std::vector<TemporaryTestUser> users = {
1159 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
1160 TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
1163 users.at(0).create();
1164 users.at(1).create();
1165 //Install apps for both users
1166 for(const auto &user : users) {
1167 for(const auto &app : users2AppsMap.at(user.getUserName())) {
1168 InstallRequest requestInst;
1169 requestInst.setAppId(app.first.c_str());
1171 requestInst.setPkgId(MANY_APPS_PKGS.at(app.first).c_str());
1172 } catch (const std::out_of_range &e) {
1173 RUNNER_FAIL_MSG("Couldn't find package for app: " << app.first);
1175 requestInst.setUid(user.getUid());
1177 for (const auto &privilege : app.second) {
1178 requestInst.addPrivilege(privilege.c_str());
1181 Api::install(requestInst);
1184 //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
1189 RUNNER_ASSERT_MSG(sem_post(mutex) == 0, "Error while opening mutex, errno: " << errno);
1191 //Wait for child to finish
1195 for(auto &user : users) {
1200 if (pid == 0) { //child process
1202 RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child failed, errno: " << errno);
1204 struct passwd *pw = getUserStruct(usernames.at(1));
1205 register_current_process_as_privilege_manager(pw->pw_uid, true);
1207 //change uid to normal user
1208 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
1209 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1211 std::vector<PolicyEntry> policyEntries;
1213 //this call should succeed as the calling user is privileged
1214 Api::getPolicy(filter, policyEntries);
1216 RUNNER_ASSERT_MSG(policyEntries.size() != 0, "Policy is empty");
1217 RUNNER_ASSERT_MSG(policyEntries.size() == privileges_count, "Number of policies doesn't match - should be: " << privileges_count << " and is " << policyEntries.size());
1219 for (const auto &policyEntry : policyEntries) {
1220 std::string user = policyEntry.getUser();
1221 std::string app = policyEntry.getAppId();
1222 std::string privilege = policyEntry.getPrivilege();
1225 struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
1226 std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
1227 if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
1228 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
1229 } catch (const std::out_of_range &e) {
1230 RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
1231 } catch (const std::invalid_argument& e) {
1232 RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
1240 RUNNER_TEST(security_manager_13_privacy_manager_fetch_policy_after_update_unprivileged)
1243 const std::vector<std::string> usernames = {"sm_test_13_user_name_1", "sm_test_13_user_name_2"};
1245 std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
1246 std::map<std::string, std::set<std::string>> apps2PrivsMap;
1248 for (const auto &username : usernames) {
1250 for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
1251 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
1252 MANY_APPS.at(i), std::set<std::string>(
1253 MANY_APPS_PRIVILEGES.at(i).begin(),
1254 MANY_APPS_PRIVILEGES.at(i).end())));
1257 users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
1260 users2AppsMap.at(usernames.at(1)).insert(std::pair<std::string, std::set<std::string>>(
1261 PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE}));
1268 RUNNER_ASSERT_MSG(((mutex[0] = sem_open("mutex_1", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex #1, errno: " << errno);
1270 RUNNER_ASSERT_MSG(((mutex[1] = sem_open("mutex_2", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex #2, errno: " << errno);
1272 RUNNER_ASSERT_MSG(sem_init(mutex[0], 1, 0) == 0, "failed to setup mutex #1, errno: " << errno);
1274 RUNNER_ASSERT_MSG(sem_init(mutex[1], 1, 0) == 0, "failed to setup mutex #2, errno: " << errno);
1275 std::vector<PolicyEntry> policyEntries;
1279 if(pid[0] == 0) { //child #1 process
1280 RUNNER_ASSERT_MSG(sem_wait(mutex[0]) == 0, "sem_wait in child #1 failed, errno: " << errno);
1281 struct passwd *pw = getUserStruct(usernames.at(0));
1282 register_current_process_as_privilege_manager(pw->pw_uid);
1284 //change uid to normal user
1285 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
1286 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1289 PolicyRequest policyRequest;
1290 //this call should succeed as the calling user is privileged
1291 Api::getPolicyForSelf(filter, policyEntries);
1293 RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty");
1295 PolicyEntry policyEntry(
1297 std::to_string(pw->pw_uid),
1298 "http://tizen.org/privilege/internet"
1300 policyEntry.setLevel("Deny");
1302 policyRequest.addEntry(policyEntry);
1303 policyEntry = PolicyEntry(
1305 std::to_string(pw->pw_uid),
1306 "http://tizen.org/privilege/location"
1308 policyEntry.setLevel("Deny");
1310 policyRequest.addEntry(policyEntry);
1311 Api::sendPolicy(policyRequest);
1312 Api::getPolicyForSelf(filter, policyEntries);
1314 RUNNER_ASSERT_MSG(policyEntries.size() == 2, "Number of policies doesn't match - should be: 2 and is " << policyEntries.size());
1318 if (pid[0] != 0) {//parent process
1321 if (pid[1] == 0) { //child #2 process
1323 RUNNER_ASSERT_MSG(sem_wait(mutex[1]) == 0, "sem_wait in child #2 failed, errno: " << errno);
1324 struct passwd *pw_target = getUserStruct(usernames.at(0));
1325 struct passwd *pw = getUserStruct(usernames.at(1));
1326 register_current_process_as_privilege_manager(pw->pw_uid);
1328 //change uid to normal user
1329 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
1330 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1332 PolicyEntry filter = PolicyEntry(
1333 SECURITY_MANAGER_ANY,
1334 std::to_string(pw_target->pw_uid),
1335 SECURITY_MANAGER_ANY
1338 //U2 requests contents of U1 privacy manager - should fail
1339 Api::getPolicyForSelf(filter, policyEntries);
1340 RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty");
1342 filter = PolicyEntry(
1343 SECURITY_MANAGER_ANY,
1344 SECURITY_MANAGER_ANY,
1345 SECURITY_MANAGER_ANY
1348 policyEntries.clear();
1350 //U2 requests contents of ADMIN bucket - should fail
1351 Api::getPolicyForAdmin(filter, policyEntries, SECURITY_MANAGER_ERROR_ACCESS_DENIED);
1352 RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty");
1356 if (pid[1] != 0) { //parent
1358 std::vector<TemporaryTestUser> users = {
1359 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
1360 TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
1363 users.at(0).create();
1364 users.at(1).create();
1366 //Install apps for both users
1367 for(const auto &user : users2AppsMap) {
1369 for(const auto &app : user.second) {
1370 InstallRequest requestInst;
1371 requestInst.setAppId(app.first.c_str());
1373 requestInst.setPkgId(MANY_APPS_PKGS.at(app.first).c_str());
1374 } catch (const std::out_of_range &e) {
1375 RUNNER_FAIL_MSG("Couldn't find package for app: " << app.first);
1377 requestInst.setUid(users.at(0).getUid());
1379 for (const auto &privilege : app.second) {
1380 requestInst.addPrivilege(privilege.c_str());
1383 Api::install(requestInst);
1386 //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
1392 RUNNER_ASSERT_MSG(sem_post(mutex[0]) == 0, "Error while opening mutex #1, errno: " << errno);
1394 //Wait until child #1 finishes
1395 pid_t ret = wait(&status);
1396 RUNNER_ASSERT_MSG((ret != -1) && WIFEXITED(status), "Updating privileges failed");
1400 RUNNER_ASSERT_MSG(sem_post(mutex[1]) == 0, "Error while opening mutex #2, errno: " << errno);
1401 //Wait until child #2 finishes
1402 ret = wait(&status);
1403 RUNNER_ASSERT_MSG((ret =-1) && WIFEXITED(status), "Listing privileges failed");
1405 for(auto &user : users) {
1409 sem_close(mutex[0]);
1410 sem_close(mutex[1]);
1415 RUNNER_TEST(security_manager_14_privacy_manager_fetch_and_update_policy_for_admin)
1418 const std::vector<std::string> usernames = {"sm_test_14_user_name_1", "sm_test_14_user_name_2"};
1419 unsigned int privileges_count = 0;
1421 std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
1422 std::map<std::string, std::set<std::string>> apps2PrivsMap;
1424 for (const auto &username : usernames) {
1426 for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
1427 apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
1428 MANY_APPS.at(i), std::set<std::string>(
1429 MANY_APPS_PRIVILEGES.at(i).begin(),
1430 MANY_APPS_PRIVILEGES.at(i).end())));
1431 privileges_count+=MANY_APPS_PRIVILEGES.at(i).size();
1434 users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
1437 users2AppsMap.at(usernames.at(1)).insert(std::pair<std::string, std::set<std::string>>(
1438 PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE}));
1440 privileges_count += 2;
1444 RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
1446 RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
1450 std::vector<TemporaryTestUser> users = {
1451 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
1452 TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
1455 users.at(0).create();
1456 users.at(1).create();
1458 //Install apps for both users
1459 for(const auto &user : users) {
1461 for(const auto &app : users2AppsMap.at(user.getUserName())) {
1462 InstallRequest requestInst;
1463 requestInst.setAppId(app.first.c_str());
1465 requestInst.setPkgId(MANY_APPS_PKGS.at(app.first).c_str());
1466 } catch (const std::out_of_range &e) {
1467 RUNNER_FAIL_MSG("Couldn't find package for app: " << app.first);
1469 requestInst.setUid(user.getUid());
1471 for (const auto &privilege : app.second) {
1472 requestInst.addPrivilege(privilege.c_str());
1475 Api::install(requestInst);
1478 //Start child process
1480 RUNNER_ASSERT_MSG(sem_post(mutex) == 0, "Error while opening mutex, errno: " << errno);
1482 //Wait for child process to finish
1485 //switch back to root
1486 for(auto &user : users) {
1493 if (pid == 0) { //child process
1495 RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child process failed, errno: " << errno);
1497 struct passwd *pw = getUserStruct(usernames.at(0));
1498 register_current_process_as_privilege_manager(pw->pw_uid, true);
1500 //change uid to normal user
1501 int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
1502 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1504 PolicyRequest *policyRequest = new PolicyRequest();
1506 std::vector<PolicyEntry> policyEntries;
1507 //this call should succeed as the calling user is privileged
1508 Api::getPolicyForSelf(filter, policyEntries);
1510 RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty");
1512 PolicyEntry policyEntry(
1513 SECURITY_MANAGER_ANY,
1514 SECURITY_MANAGER_ANY,
1515 "http://tizen.org/privilege/internet"
1517 policyEntry.setMaxLevel("Deny");
1519 policyRequest->addEntry(policyEntry);
1520 policyEntry = PolicyEntry(
1521 SECURITY_MANAGER_ANY,
1522 SECURITY_MANAGER_ANY,
1523 "http://tizen.org/privilege/location"
1525 policyEntry.setMaxLevel("Deny");
1527 policyRequest->addEntry(policyEntry);
1528 Api::sendPolicy(*policyRequest);
1529 Api::getPolicyForAdmin(filter, policyEntries);
1531 RUNNER_ASSERT_MSG(policyEntries.size() == 2, "Number of policies doesn't match - should be: 2 and is " << policyEntries.size());
1533 delete policyRequest;
1534 policyRequest = new PolicyRequest();
1535 policyEntry = PolicyEntry(
1536 SECURITY_MANAGER_ANY,
1537 SECURITY_MANAGER_ANY,
1538 "http://tizen.org/privilege/internet"
1540 policyEntry.setMaxLevel(SECURITY_MANAGER_DELETE);
1541 policyRequest->addEntry(policyEntry);
1543 policyEntry = PolicyEntry(
1544 SECURITY_MANAGER_ANY,
1545 SECURITY_MANAGER_ANY,
1546 "http://tizen.org/privilege/location"
1548 policyEntry.setMaxLevel(SECURITY_MANAGER_DELETE);
1550 policyRequest->addEntry(policyEntry);
1551 Api::sendPolicy(*policyRequest);
1553 policyEntries.clear();
1554 Api::getPolicyForAdmin(filter, policyEntries);
1555 RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Number of policies doesn't match - should be: 0 and is " << policyEntries.size());
1557 delete policyRequest;
1564 RUNNER_MULTIPROCESS_TEST(security_manager_15_privacy_manager_send_policy_update_for_admin)
1566 const char *const update_app_id = "security_manager_15_update_app_id";
1567 const char *const update_privilege = "http://tizen.org/privilege/led";
1568 const char *const check_start_bucket = "ADMIN";
1569 const std::string username("sm_test_15_username");
1570 PolicyRequest addPolicyRequest;
1571 CynaraTestAdmin::Admin admin;
1573 std::string smackLabel;
1574 generateAppLabel(update_app_id, smackLabel);
1585 RUNNER_ASSERT_MSG((pipe(pipefd) != -1),"pipe failed");
1587 TemporaryTestUser user(username, GUM_USERTYPE_ADMIN, false);
1591 RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
1592 if (pid != 0)//parent process
1594 FdUniquePtr pipeptr(pipefd+1);
1597 register_current_process_as_privilege_manager(user.getUid(), true);
1599 //send info to child
1600 msg.uid = user.getUid();
1601 msg.gid = user.getGid();
1603 ssize_t written = TEMP_FAILURE_RETRY(write(pipefd[1], &msg, sizeof(struct message)));
1604 RUNNER_ASSERT_MSG((written == sizeof(struct message)),"write failed");
1607 RUNNER_ASSERT_MSG(wait(&result) == pid, "wait failed");
1609 admin.adminCheck(check_start_bucket, false, smackLabel.c_str(),
1610 std::to_string(static_cast<int>(msg.uid)).c_str(), update_privilege, CYNARA_ADMIN_ALLOW, nullptr);
1614 FdUniquePtr pipeptr(pipefd);
1617 ssize_t fetched = TEMP_FAILURE_RETRY(read(pipefd[0], &msg, sizeof(struct message)));
1618 RUNNER_ASSERT_MSG(fetched == sizeof(struct message), "read failed");
1620 //become admin privacy manager manager
1621 Api::setProcessLabel(PRIVILEGE_MANAGER_APP.c_str());
1622 result = drop_root_privileges(msg.uid, msg.gid);
1623 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1625 PolicyEntry entry(update_app_id, std::to_string(static_cast<int>(msg.uid)), update_privilege);
1626 entry.setMaxLevel("Allow");
1628 addPolicyRequest.addEntry(entry);
1629 Api::sendPolicy(addPolicyRequest);
1634 RUNNER_MULTIPROCESS_TEST(security_manager_15_privacy_manager_send_policy_update_for_admin_wildcard)
1636 const char *const update_other_app_id = "security_manager_15_update_other_app_id";
1637 const char *const update_privilege = "http://tizen.org/privilege/led";
1638 const char *const check_start_bucket = "ADMIN";
1639 const std::string username("sm_test_15_username");
1640 PolicyRequest addPolicyRequest;
1641 CynaraTestAdmin::Admin admin;
1643 std::string smackLabel;
1644 generateAppLabel(update_other_app_id, smackLabel);
1655 RUNNER_ASSERT_MSG((pipe(pipefd) != -1),"pipe failed");
1657 TemporaryTestUser user(username, GUM_USERTYPE_ADMIN, false);
1661 RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
1662 if (pid != 0)//parent process
1664 FdUniquePtr pipeptr(pipefd+1);
1667 register_current_process_as_privilege_manager(user.getUid(), true);
1669 //send info to child
1670 msg.uid = user.getUid();
1671 msg.gid = user.getGid();
1673 ssize_t written = TEMP_FAILURE_RETRY(write(pipefd[1], &msg, sizeof(struct message)));
1674 RUNNER_ASSERT_MSG((written == sizeof(struct message)),"write failed");
1677 RUNNER_ASSERT_MSG(wait(&result) == pid, "wait failed");
1679 admin.adminCheck(check_start_bucket, false, smackLabel.c_str(),
1680 std::to_string(static_cast<int>(msg.uid)).c_str(), update_privilege, CYNARA_ADMIN_ALLOW, nullptr);
1684 FdUniquePtr pipeptr(pipefd);
1687 ssize_t fetched = TEMP_FAILURE_RETRY(read(pipefd[0], &msg, sizeof(struct message)));
1688 RUNNER_ASSERT_MSG(fetched == sizeof(struct message), "read failed");
1690 //become admin privacy manager manager
1691 Api::setProcessLabel(PRIVILEGE_MANAGER_APP.c_str());
1692 result = drop_root_privileges(msg.uid, msg.gid);
1693 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1695 // use wildcard as appId
1696 PolicyEntry entry(SECURITY_MANAGER_ANY, std::to_string(static_cast<int>(msg.uid)), update_privilege);
1697 entry.setMaxLevel("Allow");
1699 addPolicyRequest.addEntry(entry);
1700 Api::sendPolicy(addPolicyRequest);
1705 RUNNER_MULTIPROCESS_TEST(security_manager_15_privacy_manager_send_policy_update_for_self)
1707 const char *const update_app_id = "security_manager_15_update_app_id";
1708 const char *const update_privilege = "http://tizen.org/privilege/led";
1709 const char *const check_start_bucket = "";
1710 const std::string username("sm_test_15_username");
1711 PolicyRequest addPolicyRequest;
1712 CynaraTestAdmin::Admin admin;
1714 std::string smackLabel;
1715 generateAppLabel(update_app_id, smackLabel);
1726 RUNNER_ASSERT_MSG((pipe(pipefd) != -1),"pipe failed");
1728 TemporaryTestUser user(username, GUM_USERTYPE_NORMAL, false);
1732 RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
1733 if (pid != 0)//parent process
1735 FdUniquePtr pipeptr(pipefd+1);
1738 register_current_process_as_privilege_manager(user.getUid(), false);
1740 //send info to child
1741 msg.uid = user.getUid();
1742 msg.gid = user.getGid();
1744 ssize_t written = TEMP_FAILURE_RETRY(write(pipefd[1], &msg, sizeof(struct message)));
1745 RUNNER_ASSERT_MSG((written == sizeof(struct message)),"write failed");
1748 RUNNER_ASSERT_MSG(wait(&result) == pid, "wait failed");
1750 admin.adminCheck(check_start_bucket, false, smackLabel.c_str(),
1751 std::to_string(static_cast<int>(msg.uid)).c_str(), update_privilege, CYNARA_ADMIN_ALLOW, nullptr);
1755 FdUniquePtr pipeptr(pipefd);
1758 ssize_t fetched = TEMP_FAILURE_RETRY(read(pipefd[0], &msg, sizeof(struct message)));
1759 RUNNER_ASSERT_MSG(fetched == sizeof(struct message), "read failed");
1761 //become admin privacy manager manager
1762 Api::setProcessLabel(PRIVILEGE_MANAGER_APP.c_str());
1763 result = drop_root_privileges(msg.uid, msg.gid);
1764 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1766 PolicyEntry entry(update_app_id, std::to_string(static_cast<int>(msg.uid)), update_privilege);
1767 entry.setLevel("Allow");
1769 addPolicyRequest.addEntry(entry);
1770 Api::sendPolicy(addPolicyRequest);
1775 RUNNER_MULTIPROCESS_TEST(security_manager_16_policy_levels_get)
1777 const std::string username("sm_test_16_user_cynara_policy");
1778 CynaraTestAdmin::Admin admin;
1788 RUNNER_ASSERT_MSG((pipe(pipefd) != -1),"pipe failed");
1790 TemporaryTestUser user(username, GUM_USERTYPE_NORMAL, false);
1794 RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
1795 if (pid != 0)//parent process
1797 FdUniquePtr pipeptr(pipefd+1);
1800 //send info to child
1801 msg.uid = user.getUid();
1802 msg.gid = user.getGid();
1804 ssize_t written = TEMP_FAILURE_RETRY(write(pipefd[1], &msg, sizeof(struct message)));
1805 RUNNER_ASSERT_MSG((written == sizeof(struct message)),"write failed");
1808 RUNNER_ASSERT_MSG(wait(&result) == pid, "wait failed");
1814 std::string allow_policy, deny_policy;
1816 FdUniquePtr pipeptr(pipefd);
1819 ssize_t fetched = TEMP_FAILURE_RETRY(read(pipefd[0], &msg, sizeof(struct message)));
1820 RUNNER_ASSERT_MSG(fetched == sizeof(struct message), "read failed");
1822 //become admin privacy manager manager
1823 result = drop_root_privileges(msg.uid, msg.gid);
1824 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
1826 // without plugins there should only be 2 policies - Allow and Deny
1827 ret = security_manager_policy_levels_get(&levels, &count);
1829 RUNNER_ASSERT_MSG((lib_retcode)ret == SECURITY_MANAGER_SUCCESS,
1830 "Invlid return code: " << ret);
1832 RUNNER_ASSERT_MSG(count == 2, "Invalid number of policy levels. Should be 2, instead there is: " << static_cast<int>(count));
1834 deny_policy = std::string(levels[0]);
1835 allow_policy = std::string(levels[count-1]);
1837 // first should always be Deny
1838 RUNNER_ASSERT_MSG(deny_policy.compare("Deny") == 0,
1839 "Invalid first policy level. Should be Deny, instead there is: " << levels[0]);
1841 // last should always be Allow
1842 RUNNER_ASSERT_MSG(allow_policy.compare("Allow") == 0,
1843 "Invalid last policy level. Should be Allow, instead there is: " << levels[count-1]);
1845 security_manager_policy_levels_free(levels, count);
1850 int main(int argc, char *argv[])
1852 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);