1 #include <dpl/test/test_runner.h>
5 #include <summary_collector.h>
7 #include <unordered_set>
8 #include <sys/capability.h>
10 #include <sys/socket.h>
12 #include <attr/xattr.h>
13 #include <linux/xattr.h>
18 #include <libprivilege-control_test_common.h>
19 #include <tests_common.h>
21 #include <security-manager.h>
23 #include <cynara_test_client.h>
25 DEFINE_SMARTPTR(security_manager_app_inst_req_free, app_inst_req, AppInstReqUniquePtr);
26 DEFINE_SMARTPTR(cap_free, _cap_struct, CapsSetsUniquePtr);
28 static const char *const SM_APP_ID1 = "sm_test_app_id_double";
29 static const char *const SM_PKG_ID1 = "sm_test_pkg_id_double";
31 static const char *const SM_APP_ID2 = "sm_test_app_id_full";
32 static const char *const SM_PKG_ID2 = "sm_test_pkg_id_full";
34 static const char *const SM_APP_ID3 = "sm_test_app_id_uid";
35 static const char *const SM_PKG_ID3 = "sm_test_pkg_id_uid";
37 static const privileges_t SM_ALLOWED_PRIVILEGES = {
38 "security_manager_test_rules2_r",
39 "security_manager_test_rules2_no_r"
42 static const privileges_t SM_DENIED_PRIVILEGES = {
43 "security_manager_test_rules1",
44 "security_manager_test_rules2"
47 static const privileges_t SM_NO_PRIVILEGES = {
50 static const std::vector<std::string> SM_ALLOWED_GROUPS = {"db_browser", "db_alarm"};
52 static const char *const SM_PRIVATE_PATH = "/etc/smack/test_DIR/app_dir";
53 static const char *const SM_PUBLIC_PATH = "/etc/smack/test_DIR/app_dir_public";
54 static const char *const SM_PUBLIC_RO_PATH = "/etc/smack/test_DIR/app_dir_public_ro";
55 static const char *const SM_DENIED_PATH = "/etc/smack/test_DIR/non_app_dir";
56 static const char *const SM_PRIVATE_PATH_FOR_USER = "/home/" APP_USER "/test_DIR";
57 static const char *const ANY_USER_REPRESENTATION = "anyuser";/*this may be actually any string*/
59 static void generateAppLabel(const std::string &pkgId, std::string &label)
65 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
66 const char* correctLabel, bool transmute_test, bool exec_test)
70 char* label = nullptr;
73 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
74 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
75 labelPtr.reset(label);
76 RUNNER_ASSERT_MSG(label != nullptr, "ACCESS label on " << fpath << " is not set");
77 result = strcmp(correctLabel, label);
78 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect"
79 " (should be '" << correctLabel << "' and is '" << label << "')");
83 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
84 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
85 labelPtr.reset(label);
87 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
88 RUNNER_ASSERT_MSG(label != nullptr, "EXEC label on " << fpath << " is not set");
89 result = strcmp(correctLabel, label);
90 RUNNER_ASSERT_MSG(result == 0, "Incorrect EXEC label on executable file " << fpath);
92 RUNNER_ASSERT_MSG(label == nullptr, "EXEC label on " << fpath << " is set");
96 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
97 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
98 labelPtr.reset(label);
100 if (S_ISDIR(sb->st_mode) && transmute_test == true) {
101 RUNNER_ASSERT_MSG(label != nullptr, "TRANSMUTE label on " << fpath << " is not set at all");
102 RUNNER_ASSERT_MSG(strcmp(label,"TRUE") == 0,
103 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
105 RUNNER_ASSERT_MSG(label == nullptr, "TRANSMUTE label on " << fpath << " is set");
112 static int nftw_check_sm_labels_app_private_dir(const char *fpath, const struct stat *sb,
113 int /*typeflag*/, struct FTW* /*ftwbuf*/)
115 return nftw_check_sm_labels_app_dir(fpath, sb, USER_APP_ID, false, true);
118 static int nftw_check_sm_labels_app_public_dir(const char *fpath, const struct stat *sb,
119 int /*typeflag*/, struct FTW* /*ftwbuf*/)
122 return nftw_check_sm_labels_app_dir(fpath, sb, "User", true, false);
125 static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct stat *sb,
126 int /*typeflag*/, struct FTW* /*ftwbuf*/)
129 return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
132 static app_inst_req* do_app_inst_req_new()
135 app_inst_req *req = nullptr;
137 result = security_manager_app_inst_req_new(&req);
138 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
139 "creation of new request failed. Result: " << result);
140 RUNNER_ASSERT_MSG(req != nullptr, "creation of new request did not allocate memory");
144 static void prepare_app_path()
148 result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
149 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
151 result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
152 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
154 result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
155 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
157 result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
158 RUNNER_ASSERT_MSG(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
161 static void prepare_app_env()
166 /* TODO: add parameters to this function */
167 static void check_app_path_after_install()
171 result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
172 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
174 result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
175 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
177 result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
178 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
180 result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
181 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
185 static void check_app_permissions(const char *const app_id, const char *const pkg_id, const char *const user,
186 const privileges_t &allowed_privs, const privileges_t &denied_privs)
189 std::string smackLabel;
190 generateAppLabel(pkg_id, smackLabel);
192 CynaraTestClient ctc;
194 for (auto &priv : allowed_privs) {
195 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_ALLOWED);
198 for (auto &priv : denied_privs) {
199 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_DENIED);
203 static void check_app_gids(const char *const app_id, const std::vector<gid_t> &allowed_gids)
206 gid_t main_gid = getgid();
207 std::unordered_set<gid_t> reference_gids(allowed_gids.begin(), allowed_gids.end());
209 // Reset supplementary groups
210 ret = setgroups(0, NULL);
211 RUNNER_ASSERT_MSG(ret != -1, "Unable to set supplementary groups");
213 ret = security_manager_set_process_groups_from_appid(app_id);
214 RUNNER_ASSERT_MSG(ret == SECURITY_MANAGER_SUCCESS,
215 "security_manager_set_process_groups_from_appid(" <<
216 app_id << ") failed. Result: " << ret);
218 ret = getgroups(0, nullptr);
219 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
221 std::vector<gid_t> actual_gids(ret);
222 ret = getgroups(ret, actual_gids.data());
223 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
225 for (const auto &gid : actual_gids) {
226 RUNNER_ASSERT_MSG(gid == main_gid || reference_gids.count(gid) > 0,
227 "Application shouldn't get access to group " << gid);
228 reference_gids.erase(gid);
231 RUNNER_ASSERT_MSG(reference_gids.empty(), "Application didn't get access to some groups");
234 static void check_app_after_install(const char *const app_id, const char *const pkg_id,
235 const privileges_t &allowed_privs,
236 const privileges_t &denied_privs,
237 const std::vector<std::string> &allowed_groups)
239 TestSecurityManagerDatabase dbtest;
240 dbtest.test_db_after__app_install(app_id, pkg_id, allowed_privs);
241 dbtest.check_privileges_removed(app_id, pkg_id, denied_privs);
243 /*Privileges should be granted to all users if root installs app*/
244 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, allowed_privs, denied_privs);
246 /* Setup mapping of gids to privileges */
247 /* Do this for each privilege for extra check */
248 for (const auto &privilege : allowed_privs) {
249 dbtest.setup_privilege_groups(privilege, allowed_groups);
252 std::vector<gid_t> allowed_gids;
254 for (const auto &groupName : allowed_groups) {
256 struct group* grp = getgrnam(groupName.c_str());
257 RUNNER_ASSERT_ERRNO_MSG(grp, "Group: " << groupName << " not found");
258 allowed_gids.push_back(grp->gr_gid);
261 check_app_gids(app_id, allowed_gids);
264 static void check_app_after_install(const char *const app_id, const char *const pkg_id)
266 TestSecurityManagerDatabase dbtest;
267 dbtest.test_db_after__app_install(app_id, pkg_id);
270 static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
271 const privileges_t &privileges, const bool is_pkg_removed)
273 TestSecurityManagerDatabase dbtest;
274 dbtest.test_db_after__app_uninstall(app_id, pkg_id, privileges, is_pkg_removed);
277 /*Privileges should not be granted anymore to any user*/
278 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, SM_NO_PRIVILEGES, privileges);
281 static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
282 const bool is_pkg_removed)
284 TestSecurityManagerDatabase dbtest;
285 dbtest.test_db_after__app_uninstall(app_id, pkg_id, is_pkg_removed);
288 static void install_app(const char *app_id, const char *pkg_id)
291 AppInstReqUniquePtr request;
292 request.reset(do_app_inst_req_new());
294 result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
295 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
296 "setting app id failed. Result: " << result);
298 result = security_manager_app_inst_req_set_pkg_id(request.get(), pkg_id);
299 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
300 "setting pkg id failed. Result: " << result);
302 result = security_manager_app_install(request.get());
303 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
304 "installing app failed. Result: " << result);
306 check_app_after_install(app_id, pkg_id);
310 static void uninstall_app(const char *app_id, const char *pkg_id,
311 bool expect_installed, bool expect_pkg_removed)
314 AppInstReqUniquePtr request;
315 request.reset(do_app_inst_req_new());
317 result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
318 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
319 "setting app id failed. Result: " << result);
321 result = security_manager_app_uninstall(request.get());
322 RUNNER_ASSERT_MSG(!expect_installed || (lib_retcode)result == SECURITY_MANAGER_SUCCESS,
323 "uninstalling app failed. Result: " << result);
325 check_app_after_uninstall(app_id, pkg_id, expect_pkg_removed);
329 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
332 RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
335 AppInstReqUniquePtr request;
337 request.reset(do_app_inst_req_new());
339 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
340 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
341 "setting app id failed. Result: " << result);
343 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID1);
344 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
345 "setting pkg id failed. Result: " << result);
347 result = security_manager_app_install(request.get());
348 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
349 "installing app failed. Result: " << result);
351 result = security_manager_app_install(request.get());
352 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
353 "installing already installed app failed. Result: " << result);
355 /* Check records in the security-manager database */
356 check_app_after_install(SM_APP_ID1, SM_PKG_ID1);
358 request.reset(do_app_inst_req_new());
360 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
361 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
362 "setting app id failed. Result: " << result);
364 result = security_manager_app_uninstall(request.get());
365 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
366 "uninstalling app failed. Result: " << result);
368 result = security_manager_app_uninstall(request.get());
369 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
370 "uninstalling already uninstalled app failed. Result: " << result);
372 /* Check records in the security-manager database */
373 check_app_after_uninstall(SM_APP_ID1, SM_PKG_ID1, TestSecurityManagerDatabase::REMOVED);
376 RUNNER_TEST(security_manager_02_app_install_uninstall_full)
379 AppInstReqUniquePtr request;
383 request.reset(do_app_inst_req_new());
385 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
386 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
387 "setting app id failed. Result: " << result);
389 result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
390 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
391 "setting pkg id failed. Result: " << result);
393 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[0].c_str());
394 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
395 "setting allowed permission failed. Result: " << result);
396 result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[1].c_str());
397 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
398 "setting allowed permission failed. Result: " << result);
400 result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
401 SECURITY_MANAGER_PATH_PRIVATE);
402 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
403 "setting allowed path failed. Result: " << result);
405 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
406 SECURITY_MANAGER_PATH_PUBLIC);
407 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
408 "setting allowed path failed. Result: " << result);
410 result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
411 SECURITY_MANAGER_PATH_PUBLIC_RO);
412 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
413 "setting allowed path failed. Result: " << result);
415 result = security_manager_app_install(request.get());
416 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
417 "installing app failed. Result: " << result);
419 /* Check records in the security-manager database */
420 check_app_after_install(SM_APP_ID2, SM_PKG_ID2,
421 SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES, SM_ALLOWED_GROUPS);
423 /* TODO: add parameters to this function */
424 check_app_path_after_install();
426 request.reset(do_app_inst_req_new());
428 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
429 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
430 "setting app id failed. Result: " << result);
432 result = security_manager_app_uninstall(request.get());
433 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
434 "uninstalling app failed. Result: " << result);
436 /* Check records in the security-manager database,
437 * all previously allowed privileges should be removed */
438 check_app_after_uninstall(SM_APP_ID2, SM_PKG_ID2,
439 SM_ALLOWED_PRIVILEGES, TestSecurityManagerDatabase::REMOVED);
442 RUNNER_CHILD_TEST_SMACK(security_manager_03_set_label_from_appid)
444 const char *const app_id = "sm_test_app_id_set_label_from_appid";
445 const char *const pkg_id = "sm_test_pkg_id_set_label_from_appid";
446 const char *const expected_label = USER_APP_ID;
447 const char *const socketLabel = "not_expected_label";
448 char *label = nullptr;
452 uninstall_app(app_id, pkg_id, false, true);
453 install_app(app_id, pkg_id);
455 struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
456 //Clean up before creating socket
458 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
459 RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed");
460 SockUniquePtr sockPtr(&sock);
461 //Bind socket to address
462 result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
463 RUNNER_ASSERT_ERRNO_MSG(result == 0, "bind failed");
464 //Set socket label to something different than expecedLabel
465 result = fsetxattr(sock, XATTR_NAME_SMACKIPIN, socketLabel,
466 strlen(socketLabel), 0);
467 RUNNER_ASSERT_ERRNO_MSG(result == 0,
468 "Can't set socket label. Result: " << result);
469 result = fsetxattr(sock, XATTR_NAME_SMACKIPOUT, socketLabel,
470 strlen(socketLabel), 0);
471 RUNNER_ASSERT_ERRNO_MSG(result == 0,
472 "Can't set socket label. Result: " << result);
474 result = security_manager_set_process_label_from_appid(app_id);
475 RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
476 "security_manager_set_process_label_from_appid(" <<
477 app_id << ") failed. Result: " << result);
479 char value[SMACK_LABEL_LEN + 1];
481 size = fgetxattr(sock, XATTR_NAME_SMACKIPIN, value, sizeof(value));
482 RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
483 result = strcmp(expected_label, value);
484 RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
485 expected_label << " Actual: " << value);
487 size = fgetxattr(sock, XATTR_NAME_SMACKIPOUT, value, sizeof(value));
488 RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
489 result = strcmp(expected_label, value);
490 RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
491 expected_label << " Actual: " << value);
493 result = smack_new_label_from_self(&label);
494 RUNNER_ASSERT_MSG(result >= 0,
495 " Error getting current process label");
496 RUNNER_ASSERT_MSG(label != nullptr,
497 " Process label is not set");
498 labelPtr.reset(label);
500 result = strcmp(expected_label, label);
501 RUNNER_ASSERT_MSG(result == 0,
502 " Process label is incorrect. Expected: \"" << expected_label <<
503 "\" Actual: \"" << label << "\"");
505 uninstall_app(app_id, pkg_id, true, true);
508 RUNNER_CHILD_TEST_NOSMACK(security_manager_03_set_label_from_appid_nosmack)
510 const char *const app_id = "sm_test_app_id_set_label_from_appid";
511 const char *const pkg_id = "sm_test_pkg_id_set_label_from_appid";
514 uninstall_app(app_id, pkg_id, false, true);
515 install_app(app_id, pkg_id);
517 result = security_manager_set_process_label_from_appid(app_id);
518 RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
519 "security_manager_set_process_label_from_appid(" <<
520 app_id << ") failed. Result: " << result);
522 uninstall_app(app_id, pkg_id, true, true);
527 static void prepare_request(AppInstReqUniquePtr &request,
528 const char *const app_id,
529 const char *const pkg_id,
530 app_install_path_type pathType,
531 const char *const path)
534 request.reset(do_app_inst_req_new());
536 result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
537 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
538 "setting app id failed. Result: " << result);
540 result = security_manager_app_inst_req_set_pkg_id(request.get(), pkg_id);
541 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
542 "setting pkg id failed. Result: " << result);
544 result = security_manager_app_inst_req_add_path(request.get(), path, pathType);
545 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
546 "setting allowed path failed. Result: " << result);
550 static struct passwd* get_app_pw()
552 struct passwd *pw = nullptr;
554 while(!(pw = getpwnam(APP_USER))) {
555 RUNNER_ASSERT_ERRNO_MSG(errno == EINTR, "getpwnam() failed");
560 RUNNER_CHILD_TEST(security_manager_04_app_install_uninstall_by_app_user)
563 AppInstReqUniquePtr request;
564 struct passwd *pw = get_app_pw();
565 const std::string user = std::to_string(static_cast<unsigned int>(pw->pw_uid));
567 //switch user to non-root
568 result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
569 RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
571 //install app as non-root user and try to register public path (should fail)
572 prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PUBLIC, SM_PRIVATE_PATH_FOR_USER);
574 result = security_manager_app_install(request.get());
575 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED,
576 "installing app not failed. Result: " << result);
578 //install app as non-root user
579 //should fail (non-root users may only register folders inside their home)
580 prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PRIVATE, SM_PRIVATE_PATH);
582 result = security_manager_app_install(request.get());
583 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED,
584 "installing app not failed. Result: " << result);
586 //install app as non-root user
587 //should succeed - this time i register folder inside user's home dir
588 prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PRIVATE, SM_PRIVATE_PATH_FOR_USER);
590 for (auto &privilege : SM_ALLOWED_PRIVILEGES) {
591 result = security_manager_app_inst_req_add_privilege(request.get(), privilege.c_str());
592 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
593 "setting allowed permission failed. Result: " << result);
596 result = security_manager_app_install(request.get());
597 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
598 "installing app failed. Result: " << result);
600 check_app_permissions(SM_APP_ID3, SM_PKG_ID3, user.c_str(), SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES);
602 //uninstall app as non-root user
603 request.reset(do_app_inst_req_new());
605 result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID3);
606 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
607 "setting app id failed. Result: " << result);
609 result = security_manager_app_uninstall(request.get());
610 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
611 "uninstalling app failed. Result: " << result);
613 check_app_permissions(SM_APP_ID3, SM_PKG_ID3, user.c_str(), SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
616 RUNNER_CHILD_TEST(security_manager_05_drop_process_capabilities)
619 CapsSetsUniquePtr caps, caps_empty(cap_init());
621 caps.reset(cap_from_text("all=eip"));
622 RUNNER_ASSERT_MSG(caps, "can't convert capabilities from text");
623 result = cap_set_proc(caps.get());
624 RUNNER_ASSERT_MSG(result == 0,
625 "can't set capabilities. Result: " << result);
627 result = security_manager_drop_process_privileges();
628 RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
629 "dropping caps failed. Result: " << result);
631 caps.reset(cap_get_proc());
632 RUNNER_ASSERT_MSG(caps, "can't get proc capabilities");
634 result = cap_compare(caps.get(), caps_empty.get());
635 RUNNER_ASSERT_MSG(result == 0,
636 "capabilities not dropped. Current: " << cap_to_text(caps.get(), NULL));
639 int main(int argc, char *argv[])
641 SummaryCollector::Register();
642 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);