2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5 * @file security_server_tests_server.cpp
6 * @author Bumjin Im (bj.im@samsung.com)
7 * @author Mariusz Domanski (m.domanski@samsung.com)
9 * @brief Test cases for security server
15 #include <sys/types.h>
16 #include <sys/param.h>
21 #include <sys/socket.h>
23 #include <sys/smack.h>
26 #include "security-server.h"
27 #include "security_server_clean_env.h"
28 #include <dpl/test/test_runner.h>
29 #include <dpl/test/test_runner_child.h>
31 #include <privilege-control.h>
33 #include "security_server_tests_common.h"
34 #include "tests_common.h"
35 #include <smack_access.h>
36 #include <access_provider.h>
37 #include <summary_collector.h>
39 const char *TEST03_SUBJECT = "subject_0f09f7cc";
40 const char *TEST04_SUBJECT = "subject_57dfbfc5";
41 const char *TEST05_SUBJECT = "subject_1d6eda7d";
42 const char *TEST06_SUBJECT = "subject_1d414140";
43 const char *TEST07_SUBJECT = "subject_cd738844";
44 const char *TEST08_SUBJECT = "subject_fd84ba7f";
45 const char *TEST09_SUBJECT = "subject_sstest09";
46 const char *TEST10_SUBJECT = "subject_sstest10";
47 const char *TEST11_SUBJECT = "subject_sstest11";
48 const char *TEST12_SUBJECT = "subject_sstest12";
50 const char *API_PASSWD_SET = "security-server::api-password-set";
51 const char *API_PASSWD_CHECK = "security-server::api-password-check";
52 const char *API_PASSWD_RESET = "security-server::api-password-reset";
53 const char *API_RULE_REQUIRED = "w";
54 const char *PROC_AUDIO_GROUP_NAME = "audio";
56 int clear_password(char ** /*error*/)
59 unsigned int attempt, max_attempt, expire_sec;
60 const char *subject_allow = "subject_allow";
61 struct smack_accesses *handle = NULL;
64 reset_security_server();
66 ret = smack_accesses_new(&handle);
67 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << ret);
69 /* our subject 'subject_allow' has access to security-server::api-password-check */
70 ret = smack_accesses_add(handle, subject_allow, API_PASSWD_CHECK, API_RULE_REQUIRED);
71 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << ret);
73 ret = smack_accesses_apply(handle);
74 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << ret);
76 ret = smack_set_label_for_self(subject_allow);
77 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << ret);
79 smack_accesses_free(handle);
81 attempt = max_attempt = expire_sec = UINT_MAX;
82 ret = security_server_is_pwd_valid(&attempt, &max_attempt, &expire_sec);
84 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD, "ret: " << ret);
85 RUNNER_ASSERT_BT(expire_sec == 0);
86 RUNNER_ASSERT_BT(max_attempt == 0);
87 RUNNER_ASSERT_BT(attempt == 0);
89 /* we revoke all rules for subject 'subject_allow' */
90 ret = smack_revoke_subject(subject_allow);
91 RUNNER_ASSERT_MSG_BT(ret == 0, "Revoking subject didn't work.");
101 * Add a new group to the current process groups.
103 void add_process_group(const char* group_name)
105 // get group ID by gtoup name
106 group *gr = getgrnam(group_name);
107 RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
108 const gid_t new_group_id = gr->gr_gid;
110 // get number of groups that the current process belongs to
111 int ngroups = getgroups(0, NULL);
113 //allocate groups table + space for new group entry
114 std::vector<gid_t> groups(ngroups + 1);
115 getgroups(ngroups, groups.data());
117 // check if the process already belongs to the group
118 for (int i = 0; i < ngroups; ++i)
119 if (groups[i] == new_group_id)
122 // add new group & apply change
123 groups[ngroups] = new_group_id;
124 int ret = setgroups(ngroups + 1, groups.data());
125 RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups failed. ret = " << ret);
129 * Remove specific group from the current process groups.
131 void remove_process_group(const char* group_name)
133 // get group ID by gtoup name
134 group *gr = getgrnam(group_name);
135 RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
136 const gid_t new_group_id = gr->gr_gid;
138 // get number of groups that the current process belongs to
139 int ngroups = getgroups(0, NULL);
141 //allocate groups table + space for new group entry
142 std::vector<gid_t> groups(ngroups);
143 getgroups(ngroups, groups.data());
145 // check if the process already belongs to the group
146 for (int i = 0; i < ngroups; ++i)
147 if (groups[i] == new_group_id) {
148 groups[i] = groups[ngroups-1]; // replace with last
151 int ret = setgroups(ngroups - 1, groups.data());
152 RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups failed. ret = " << ret);
157 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_SERVER);
159 RUNNER_TEST(tc_security_server_get_gid_normal_case_trying_to_get_gid_of_tel_gprs)
161 RUNNER_ASSERT_BT(security_server_get_gid("tel_gprs") >= 0);
164 RUNNER_TEST(tc_security_server_get_gid_empty_object_name)
166 RUNNER_ASSERT_BT(security_server_get_gid("") == SECURITY_SERVER_API_ERROR_INPUT_PARAM);
169 RUNNER_TEST(tc_security_server_get_gid_wrong_object_name_teltel)
171 RUNNER_ASSERT_BT(security_server_get_gid("teltel") == SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT);
174 //RUNNER_CHILD_TEST(tc_cookie_check_groups_privilege_negative)
176 // remove_process_group(PROC_AUDIO_GROUP_NAME);
178 // RUNNER_ASSERT_BT(security_server_request_cookie((char*)cookie, COOKIE_SIZE) ==
179 // SECURITY_SERVER_API_SUCCESS);
180 // ret = security_server_get_gid(PROC_AUDIO_GROUP_NAME);
181 // ret = security_server_check_privilege((char*) cookie, ret);
182 // RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
185 //RUNNER_CHILD_TEST(tc_cookie_check_groups_privilege_positive)
187 // add_process_group(PROC_AUDIO_GROUP_NAME);
189 // RUNNER_ASSERT_BT(security_server_request_cookie((char*)cookie, COOKIE_SIZE) ==
190 // SECURITY_SERVER_API_SUCCESS);
191 // ret = security_server_get_gid(PROC_AUDIO_GROUP_NAME);
192 // ret = security_server_check_privilege((char*) cookie, ret);
193 // RUNNER_ASSERT_BT(ret == SECURITY_SERVER_API_SUCCESS);
196 //RUNNER_TEST(tc_ask_for_privilege_with_default_cookie_case_with_wrong_cookie)
198 // ret = security_server_get_gid(PROC_AUDIO_GROUP_NAME);
199 // srand(time(NULL));
200 // for (i = 0; i < COOKIE_SIZE; i++)
201 // wrong_cookie[i] = rand() % 255;
202 // ret = security_server_check_privilege((const char*) wrong_cookie, ret);
203 // RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
207 //RUNNER_TEST(tc_fake_security_server_get_gid)
209 // /* Close socket just after sending request msg.
210 // * This is done with fake security_server_get_gid()*/
212 // ret = fake_get_gid(PROC_AUDIO_GROUP_NAME);
213 // RUNNER_IGNORED_MSG("Watch whether security server has crashed or not.");
216 RUNNER_TEST(tc_ask_for_privilege_with_default_cookie_case_with_wrong_cookie)
218 const char wrong_cookie[20] = {'w','a','t','?'};
219 int audioGID = security_server_get_gid("audio");
220 RUNNER_ASSERT_BT(SECURITY_SERVER_API_ERROR_ACCESS_DENIED
221 == security_server_check_privilege((const char*) wrong_cookie, audioGID));
224 RUNNER_TEST(tc_get_pid_of_non_existing_cookie)
226 const char wrong_cookie[20] = {'w', 'a', 't', '?'};
227 RUNNER_ASSERT_BT(security_server_get_cookie_pid(wrong_cookie) == SECURITY_SERVER_API_ERROR_NO_SUCH_COOKIE);
230 RUNNER_TEST(tc_get_pid_of_null_cookie)
232 RUNNER_ASSERT_BT(security_server_get_cookie_pid(NULL) == SECURITY_SERVER_API_ERROR_INPUT_PARAM);
235 RUNNER_CHILD_TEST_SMACK(tc01a_security_server_app_give_access)
237 const char *subject = "abc345v34sfa";
238 const char *object = "efg678x2lkjz";
239 const char *server_api = "security-server::api-data-share";
242 smack.add(subject, object, "-----");
243 smack.add(object, server_api, "rw");
246 smack_set_label_for_self(object);
248 RUNNER_ASSERT_MSG_BT(drop_root_privileges() == 0, "uid = " << getuid());
250 security_server_app_give_access(subject, getpid());
252 RUNNER_ASSERT_BT(1 == smack_have_access(subject, object, "rwxat"));
256 * Currently we are NOT revoking any permissions given by
257 * security_server_app_give_access function
259 /*RUNNER_TEST(tc01b_security_server_app_give_access)
261 const char *subject = "abc345v34sfa";
262 const char *object = "efg678x2lkjz";
264 // After part A thread from security-server will be notified about
265 // process end and revoke permissions. We need to give him some
269 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "r----"));
270 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "-w---"));
271 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "--x--"));
272 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "---a-"));
273 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "----t"));
276 RUNNER_CHILD_TEST_SMACK(tc01c_security_server_app_give_access_no_access)
278 const char *subject = "xxx45v34sfa";
279 const char *object = "yyy78x2lkjz";
282 smack.add(subject, object, "-----");
285 RUNNER_ASSERT_MSG_BT(0 == smack_set_label_for_self(object), "Error in smack_label_for_self");
287 RUNNER_ASSERT_MSG_BT(drop_root_privileges() == 0, "uid = " << getuid());
289 RUNNER_ASSERT_BT(SECURITY_SERVER_API_ERROR_ACCESS_DENIED == security_server_app_give_access(subject, getpid()));
291 RUNNER_ASSERT_BT(0 == smack_have_access(subject, object, "r"));
294 RUNNER_TEST_SMACK(tc02_check_privilege_by_pid)
301 //we checking existing rule, it should return positive
302 ret = security_server_check_privilege_by_pid(pid, "_", "rx");
303 RUNNER_ASSERT_BT(ret == SECURITY_SERVER_API_SUCCESS);
305 //we checking rule with label that not exist
306 ret = security_server_check_privilege_by_pid(pid, "thislabelisnotreal", "rwxat");
307 RUNNER_ASSERT_BT(ret != SECURITY_SERVER_API_SUCCESS);
310 RUNNER_CHILD_TEST_SMACK(tc03_check_API_passwd_allow)
313 unsigned int attempt, max_attempt, expire_sec;
314 char *str = (char*) malloc(256);
316 attempt = max_attempt = expire_sec = 0;
318 ret = clear_password(&str);
319 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << str);
321 SecurityServer::AccessProvider provider(TEST03_SUBJECT);
322 provider.allowAPI(API_PASSWD_CHECK, API_RULE_REQUIRED);
323 provider.allowAPI(API_PASSWD_SET, API_RULE_REQUIRED);
324 provider.allowAPI(API_PASSWD_RESET, API_RULE_REQUIRED);
325 provider.applyAndSwithToUser(APP_UID, APP_GID);
327 ret = security_server_set_pwd_validity(10);
328 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD, "ret: " << ret);
330 ret = security_server_set_pwd_max_challenge(5);
331 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD, "ret: " << ret);
333 ret = security_server_is_pwd_valid(&attempt, &max_attempt, &expire_sec);
334 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD, "ret: " << ret);
337 ret = security_server_set_pwd(NULL, "12345", 0, 0);
338 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
341 ret = security_server_reset_pwd("12345",0, 0);
342 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
345 ret = security_server_chk_pwd("12345", &attempt, &max_attempt, &expire_sec);
346 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
349 ret = security_server_set_pwd_history(10);
350 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
353 RUNNER_CHILD_TEST(tc04_check_API_passwd_denied)
356 unsigned int attempt, max_attempt, expire_sec;
358 attempt = max_attempt = expire_sec = 0;
360 SecurityServer::AccessProvider privider(TEST04_SUBJECT);
361 privider.applyAndSwithToUser(APP_UID, APP_GID);
364 * now SS should return error
365 * at the moment SS doesn't check return code from
366 * authorize_SS_API_caller_socket() so it should give access
367 * you can check in logs if it's working properly
368 * has access result = 1
369 * no access result = 0
370 * D/SECURITY_SERVER( 2510): security-server-main.c: authorize_SS_API_caller_socket(205) >
371 * [SECURE_LOG] SS_SMACK: caller_pid=5278, subject=subject_allow,
372 * object=security-server::api-password-check, access=w, result=1,
373 * caller_path=/usr/bin/security-server-tests-server
374 * E/SECURITY_SERVER( 2510): security-server-main.c: authorize_SS_API_caller_socket(207) >
375 * [SECURE_LOG] SS_SMACK: caller_pid=5278, subject=subject_allow,
376 * object=security-server::api-password-check, access=w, result=0,
377 * caller_path=/usr/bin/security-server-tests-server
380 ret = security_server_set_pwd_validity(10);
381 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
383 ret = security_server_set_pwd_max_challenge(5);
384 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
386 ret = security_server_is_pwd_valid(&attempt, &max_attempt, &expire_sec);
387 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
390 ret = security_server_set_pwd("12345", "12346", 0, 0);
391 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
394 ret = security_server_reset_pwd("12346",0, 0);
395 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
398 ret = security_server_chk_pwd("12346", &attempt, &max_attempt, &expire_sec);
399 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
402 ret = security_server_set_pwd_history(10);
403 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
406 RUNNER_CHILD_TEST_SMACK(tc05_check_API_middleware_allow)
409 size_t cookie_size = security_server_get_cookie_size();
411 char *ss_label = NULL;
413 add_process_group(PROC_AUDIO_GROUP_NAME);
415 SecurityServer::AccessProvider provider(TEST05_SUBJECT);
416 provider.allowFunction("security_server_get_gid");
417 provider.allowFunction("security_server_request_cookie");
418 provider.allowFunction("security_server_check_privilege");
419 provider.allowFunction("security_server_get_cookie_pid");
420 provider.allowFunction("security_server_get_smacklabel_cookie");
421 provider.allowFunction("security_server_check_privilege_by_pid");
422 provider.applyAndSwithToUser(APP_UID, APP_GID);
424 ret = security_server_request_cookie(cookie, cookie_size);
425 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
427 ret = security_server_get_gid(PROC_AUDIO_GROUP_NAME);
428 ret = security_server_check_privilege(cookie, ret);
429 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
431 ret = security_server_get_gid("root");
432 RUNNER_ASSERT_MSG_BT(ret > -1, "ret: " << ret);
434 ret = security_server_get_cookie_pid(cookie);
435 RUNNER_ASSERT_MSG_BT(ret == getpid(), "ret: " << ret);
437 ss_label = security_server_get_smacklabel_cookie(cookie);
438 RUNNER_ASSERT_MSG_BT(ss_label != NULL, "ret: " << ss_label);
440 ret = security_server_check_privilege_by_pid(getpid(), "_", "rx");
441 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
444 RUNNER_CHILD_TEST(tc06_check_API_middleware_denied)
447 size_t cookie_size = security_server_get_cookie_size();
449 char *ss_label = NULL;
451 SecurityServer::AccessProvider provider(TEST06_SUBJECT);
452 provider.applyAndSwithToUser(APP_UID, APP_GID);
454 ret = security_server_request_cookie(cookie, cookie_size);
455 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
457 ret = security_server_check_privilege(cookie, DB_ALARM_GID);
458 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
460 ret = security_server_get_gid("root");
461 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
463 ret = security_server_get_cookie_pid(cookie);
464 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
466 ss_label = security_server_get_smacklabel_cookie(cookie);
467 RUNNER_ASSERT_MSG_BT(ss_label != NULL && !strcmp(ss_label, TEST06_SUBJECT), "label:" << ss_label);
469 ret = security_server_check_privilege_by_pid(getpid(), "_", "rx");
470 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
473 RUNNER_CHILD_TEST_SMACK(tc07_check_API_data_share_allow)
475 SecurityServer::AccessProvider provider(TEST07_SUBJECT);
476 provider.allowFunction("security_server_app_give_access");
477 provider.applyAndSwithToUser(APP_UID, APP_GID);
479 int ret = security_server_app_give_access(TEST07_SUBJECT, getpid());
480 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
483 RUNNER_CHILD_TEST_SMACK(tc08_check_API_data_share_denied)
485 SecurityServer::AccessProvider provider(TEST08_SUBJECT);
486 provider.applyAndSwithToUser(APP_UID, APP_GID);
488 int ret = security_server_app_give_access(TEST08_SUBJECT, getpid());
489 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "ret: " << ret);
492 RUNNER_CHILD_TEST(tc09_check_API_app_enable_permissions)
495 const char *perm_list[] = {"org.tizen.privilege.contact.read",
496 "org.tizen.privilege.contact.write",
500 // need to install WGT once again, in case it was removed before
502 ret = perm_app_uninstall(WGT_APP_ID);
503 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot uninstall WGT_APP_ID, ret: " << ret);
504 ret = perm_app_install(WGT_APP_ID);
505 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot install WGT_APP_ID, ret: " << ret);
509 ret = security_server_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, perm_list, persistent);
510 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
512 SecurityServer::AccessProvider provider(TEST09_SUBJECT);
513 provider.allowFunction("security_server_app_has_privilege");
514 provider.applyAndSwithToUser(APP_UID, APP_GID);
516 // Check if permissions are given
517 check_app_has_privilege(WGT_APP_ID, APP_TYPE_WGT, perm_list, true);
520 RUNNER_CHILD_TEST(tc10_check_API_app_disable_permissions)
523 const char *perm_list[] = {"org.tizen.privilege.contact.read",
524 "org.tizen.privilege.contact.write",
527 // need to install WGT once again, in case it was removed before
529 ret = perm_app_uninstall(WGT_APP_ID);
530 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot uninstall WGT_APP_ID, ret: " << ret);
531 ret = perm_app_install(WGT_APP_ID);
532 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot install WGT_APP_ID, ret: " << ret);
535 // disable permission
536 ret = security_server_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, perm_list);
537 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
539 SecurityServer::AccessProvider provider(TEST10_SUBJECT);
540 provider.allowFunction("security_server_app_has_privilege");
541 provider.applyAndSwithToUser(APP_UID, APP_GID);
543 // Check if permissions are disabled
544 check_app_has_privilege(WGT_APP_ID, APP_TYPE_WGT, perm_list, false);
547 RUNNER_TEST(tc11_security_server_app_has_privilege)
550 const char *perm_list_pers[] = {"org.tizen.privilege.contact.read",
551 "org.tizen.privilege.contact.write",
553 const char *perm_list_temp[] = {"org.tizen.privilege.calendar.read",
554 "org.tizen.privilege.calendar.write",
556 const char *perm_list_disabled[] = {"org.tizen.privilege.alarm",
559 ret = perm_app_uninstall(TEST11_SUBJECT);
560 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot uninstall TEST11_SUBJECT, ret: " << ret);
561 ret = perm_app_install(TEST11_SUBJECT);
562 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot install TEST11_SUBJECT, ret: " << ret);
566 ret = security_server_app_enable_permissions(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_pers, 1);
567 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
568 ret = security_server_app_enable_permissions(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_temp, 0);
569 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
571 // Check if permissions are given using API with app_label parameter
572 check_app_has_privilege(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_pers, true);
573 check_app_has_privilege(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_temp, true);
574 check_app_has_privilege(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_disabled, false);
577 RUNNER_CHILD_TEST(tc12_security_server_app_caller_has_privilege)
580 const char *perm_list_pers[] = {"org.tizen.privilege.contact.read",
581 "org.tizen.privilege.contact.write",
583 const char *perm_list_temp[] = {"org.tizen.privilege.calendar.read",
584 "org.tizen.privilege.calendar.write",
586 const char *perm_list_disabled[] = {"org.tizen.privilege.alarm",
590 ret = perm_app_uninstall(TEST11_SUBJECT);
591 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot uninstall TEST11_SUBJECT, ret: " << ret);
592 ret = perm_app_install(TEST11_SUBJECT);
593 RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Cannot install TEST11_SUBJECT, ret: " << ret);
597 ret = security_server_app_enable_permissions(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_pers, 1);
598 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
599 ret = security_server_app_enable_permissions(TEST11_SUBJECT, APP_TYPE_WGT, perm_list_temp, 0);
600 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret: " << ret);
602 SecurityServer::AccessProvider provider(TEST11_SUBJECT);
603 provider.allowFunction("security_server_app_caller_has_privilege");
604 provider.applyAndSwithToUser(APP_UID, APP_GID);
606 // Check if permissions are given using "caller" API
607 check_app_caller_has_privilege(APP_TYPE_WGT, perm_list_pers, true);
608 check_app_caller_has_privilege(APP_TYPE_WGT, perm_list_temp, true);
609 check_app_caller_has_privilege(APP_TYPE_WGT, perm_list_disabled, false);
612 RUNNER_CHILD_TEST(tc13_check_API_app_has_privilege_denied)
615 const char *perm_list[] = {"org.tizen.privilege.contact.read",
616 "org.tizen.privilege.contact.write",
619 // set smack label without previously assigned permissions to api socket
620 ret = smack_set_label_for_self(TEST12_SUBJECT);
621 RUNNER_ASSERT_MSG_BT(ret == 0, "ret: " << ret);
623 // drop root privileges
624 RUNNER_ASSERT_MSG_BT(drop_root_privileges() == 0, "uid = " << getuid());
626 // call common function to perform the check
627 check_app_caller_has_privilege_denied(APP_TYPE_WGT, perm_list);
629 // call also second common function
630 check_app_has_privilege_denied(TEST12_SUBJECT, APP_TYPE_WGT, perm_list);
633 //////////////////////////////////////////
634 /////////NOSMACK ENV TESTS////////////////
635 //////////////////////////////////////////
638 * NOSMACK version of tc01a and tc01c tests.
640 * SMACK is turned off - that means for us, that we don't need any accesses added to our process
641 * in SMACK before dropping root privileges. This test drops root privileges, calls
642 * security_server_app_give_access and then checks if smack_have_access returns error (because
645 * security_server_app_give_access shouldn't return anything else than success when SMACK is off,
646 * hence there is only one test that replaces tests tc01a and tc01c.
648 RUNNER_CHILD_TEST_NOSMACK(tc01_security_server_app_give_access_nosmack)
650 const char* subject = "abc345v34sfa";
651 const char* object = "efg678x2lkjz";
654 result = drop_root_privileges();
655 RUNNER_ASSERT_MSG_BT(result == 0,
656 "Failed to drop root privileges. Result: " << result << "uid = " << getuid());
658 result = security_server_app_give_access(subject, getpid());
659 RUNNER_ASSERT_MSG_BT(result == SECURITY_SERVER_API_SUCCESS,
660 "Error in security_server_app_give_access. Result: " << result);
662 result = smack_have_access(subject, object, "rwxat");
663 RUNNER_ASSERT_MSG_BT(result == -1,
664 "smack_have_access should return error when SMACK is off. Result: " << result);
668 * NOSMACK version of tc02 test.
670 * check_privilege_by_pid should always return success when SMACK is off, no matter if label is
673 RUNNER_TEST_NOSMACK(tc02_check_privilege_by_pid_nosmack)
680 //we checking existing rule, it should return positive
681 ret = security_server_check_privilege_by_pid(pid, "_", "rx");
682 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
683 "check_privilege_by_pid for existing label failed. Result: " << ret);
685 //we checking rule with label that not exist
686 ret = security_server_check_privilege_by_pid(pid, "thislabelisnotreal", "rwxat");
687 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
688 "check_privilege_by_pid for nonexisting label failed. Result: " << ret);
692 * NOSMACK version of clear_password function.
694 * Compared to SMACK version of this function, this one skips adding rules and setting label.
696 int clear_password_nosmack()
699 unsigned int attempt, max_attempt, expire_sec;
702 reset_security_server();
704 attempt = max_attempt = expire_sec = UINT_MAX;
705 ret = security_server_is_pwd_valid(&attempt, &max_attempt, &expire_sec);
707 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD,
708 "is_pwd_faild should return no password error. Result: " << ret);
709 RUNNER_ASSERT_MSG_BT(expire_sec == 0, "expire_sec = " << expire_sec << ", should be 0.");
710 RUNNER_ASSERT_MSG_BT(max_attempt == 0, "max_attempt = " << max_attempt << ", should be 0.");
711 RUNNER_ASSERT_MSG_BT(attempt == 0, "attempt = " << attempt << ", should be 0.");
721 * NOSMACK version of tc03 test.
723 * Just as tc01a/tc01c NOSMACK replacement, we don't need to do anything with SMACK because most
724 * important functions will return errors (that is smack_accesses_apply/smack_have_access etc.).
725 * First clear password, then drop privileges and proceed to regular testing.
728 RUNNER_CHILD_TEST_NOSMACK(tc03_check_API_passwd_allow_nosmack)
731 unsigned int attempt, max_attempt, expire_sec;
733 attempt = max_attempt = expire_sec = 0;
735 clear_password_nosmack();
737 // drop root privileges
738 ret = drop_root_privileges();
739 RUNNER_ASSERT_MSG_BT(ret == 0,
740 "Failed to drop root privileges. Result: " << ret << "uid = " << getuid());
742 ret = security_server_set_pwd_validity(10);
743 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD,
744 "set_pwd_validity should return no password error. Result: " << ret);
746 ret = security_server_set_pwd_max_challenge(5);
747 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD,
748 "set_pwd_max_challenge should return no password error. Result: " << ret);
750 ret = security_server_is_pwd_valid(&attempt, &max_attempt, &expire_sec);
751 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD,
752 "is_pwd_valid should return no password error. Result: " << ret);
755 ret = security_server_set_pwd(NULL, "12345", 0, 0);
756 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
757 "set_pwd failed. Result: " << ret);
760 ret = security_server_reset_pwd("12345",0, 0);
761 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
762 "reset_pwd failed. Result: " << ret);
765 ret = security_server_chk_pwd("12345", &attempt, &max_attempt, &expire_sec);
766 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
767 "chk_pwd failed. Result: " << ret);
770 ret = security_server_set_pwd_history(10);
771 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
772 "set_pwd_history failed. Result: " << ret);
776 * NOSMACK version of tc05 test.
778 * This test assumes similar information as previous NOSMACK tests. SMACK off = no need to
779 * set accesses and apply them in SMACK before dropping privileges.
782 RUNNER_CHILD_TEST_NOSMACK(tc05_check_API_middleware_allow_nosmack)
785 size_t cookie_size = security_server_get_cookie_size();
787 char* ss_label = NULL;
789 add_process_group(PROC_AUDIO_GROUP_NAME);
791 // drop root privileges
792 ret = drop_root_privileges();
793 RUNNER_ASSERT_MSG_BT(ret == 0,
794 "Failed to drop root privileges. Result: " << ret << "uid = " << getuid());
796 ret = security_server_request_cookie(cookie, cookie_size);
797 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
798 "request_cookie failed. Result: " << ret);
800 ret = security_server_get_gid(PROC_AUDIO_GROUP_NAME);
801 RUNNER_ASSERT_MSG_BT(ret > -1, "Failed to get \"" << PROC_AUDIO_GROUP_NAME << "\" gid. Result: "
804 ret = security_server_check_privilege(cookie, ret);
805 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
806 "check_privilege failed. Result: " << ret);
808 ret = security_server_get_gid("root");
809 RUNNER_ASSERT_MSG_BT(ret > -1,
810 "Failed to get \"root\" gid. Result: " << ret);
812 ret = security_server_get_cookie_pid(cookie);
813 RUNNER_ASSERT_MSG_BT(ret == getpid(),
814 "get_cookie_pid returned different pid than it should. Result: " << ret);
816 ss_label = security_server_get_smacklabel_cookie(cookie);
817 RUNNER_ASSERT_MSG_BT(ss_label != NULL, "get_smacklabel_cookie failed.");
819 ret = security_server_check_privilege_by_pid(getpid(), "_", "rx");
820 if(ret != SECURITY_SERVER_API_SUCCESS) {
822 RUNNER_ASSERT_MSG_BT(false, "check_privilege_by_pid failed. Result: " << ret);
827 * NOSMACK version of tc07 test.
829 * Similarily to previous tests - no need to set self label because SMACK is off. Just as
830 * tc01a/tc01c replacement, security_server_app_give_access should return only success. Hence the
831 * NOSMACK version of tc08 test is skipped.
833 RUNNER_CHILD_TEST_NOSMACK(tc07_check_API_data_share_allow_nosmack)
837 // drop root privileges
838 ret = drop_root_privileges();
839 RUNNER_ASSERT_MSG_BT(ret == 0,
840 "Failed to drop root privileges. Result: " << ret << "uid = " << getuid());
842 ret = security_server_app_give_access(TEST07_SUBJECT, getpid());
843 RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS,
844 "app_give_access failed. Result: " << ret);
847 int main(int argc, char *argv[]) {
849 printf("Error: %s must be executed by root\n", argv[0]);
852 SummaryCollector::Register();
853 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);