2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5 * @file security_server_tests_client_smack.cpp
6 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
8 * @brief Test cases for security-server-client-smack.
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/smack.h>
25 #include <dpl/log/log.h>
26 #include <dpl/test/test_runner.h>
27 #include <dpl/test/test_runner_child.h>
28 #include <dpl/test/test_runner_multiprocess.h>
29 #include "security_server_mockup.h"
31 #include <security-server.h>
33 #include "tests_common.h"
35 #define PROPER_COOKIE_SIZE 20
39 const char *subject_label = "mylabel"; \
40 RUNNER_ASSERT_MSG(-1 != system("touch /opt/home/root/pid_cycle"), \
41 "Cannot prepare environment for test."); \
42 RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(subject_label), \
43 "Cannot prepare environment for test."); \
44 RUNNER_ASSERT_MSG(-1 != setgid(1), \
45 "Cannot prepare environment for test."); \
46 RUNNER_ASSERT_MSG(-1 != setuid(1), \
47 "Cannot prepare environment for test"); \
52 * Environment preparation should only differ in setting label. On NOSMACK system
53 * smack_set_label_for_self returns error because of no access to /proc/self/attr/current.
55 #define ENVIRONMENT_NOSMACK \
57 int fd = open("/opt/home/root/pid_cycle", O_CREAT|O_APPEND, 0444);\
58 RUNNER_ASSERT_MSG(fd >= 0, \
59 "Couldn't create pid_cycle file. errno: " << strerror(errno));\
61 RUNNER_ASSERT_MSG(-1 != setgid(1), \
62 "Cannot prepare environment for test."); \
63 RUNNER_ASSERT_MSG(-1 != setuid(1), \
64 "Cannot prepare environment for test"); \
69 * Unique_ptr typedef for NOSMACK version of tc06 test
71 void closesockfdptr(int* sockfd_ptr)
75 typedef std::unique_ptr<int, std::function<void(int*)> > SockFDUniquePtr;
78 * Dropping root privileges
79 * returns 0 on success, 1 on error
81 int drop_root_privileges()
84 /* process is running as root, drop privileges */
85 if (setgid(5000) != 0)
87 if (setuid(5000) != 0)
97 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_CLIENT_SMACK)
100 * test: Check cookie size returned by security_server_get_cookie_size.
101 * description: Cookie used by security-server is 20 bytes long.
102 * Any other size of cookies should be treated as error.
103 * expected: Function security_server_get_cookie_size returns 20.
105 RUNNER_CHILD_TEST_SMACK(tc01_security_server_get_cookie_size)
109 int ret = security_server_get_cookie_size();
110 RUNNER_ASSERT_MSG(20 == ret, "ret = " << ret);
114 * test: security_server_request_cookie
115 * description: Function security_server_request_cookie will return
116 * 20 bytes long cookie.
117 * expected: function will set up cookie in the array and return
118 * SECURITY_SERVER_API_SUCCESS.
120 RUNNER_CHILD_TEST_SMACK(tc02_security_server_request_cookie_normal_case)
125 int ret = security_server_request_cookie(cookie, 20);
126 LogDebug("ret = " << ret);
127 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS == ret);
131 * test: security_server_request_cookie
132 * description: Function security_server_request_cookie will return
133 * 20 bytes long cookie.
134 * expected: function will set up cookie in the array and return
135 * SECURITY_SERVER_API_SUCCESS.
137 RUNNER_CHILD_TEST_SMACK(tc03_security_server_request_cookie_too_small_buffer_size)
142 int ret = security_server_request_cookie(cookie, 10);
143 LogDebug("ret = " << ret);
144 RUNNER_ASSERT(SECURITY_SERVER_API_ERROR_BUFFER_TOO_SMALL == ret);
148 * test: tc04_security_server_get_gid
149 * description: Checking for security_server_get_gid
150 * with nonexisting gid and existing one
151 * expected: security_server_get_gid should return
152 * SECURITY_SERVER_ERROR_NO_SUCH_OBJECT with first call
153 * and group id with second call
155 RUNNER_CHILD_TEST_SMACK(tc04_security_server_get_gid)
159 int ret = security_server_get_gid("abc123xyz_pysiaczek");
160 LogDebug("ret = " << ret);
161 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT == ret, "Ret: " << ret);
162 ret = security_server_get_gid("root");
163 LogDebug("ret = " << ret);
164 RUNNER_ASSERT_MSG(0 == ret, "Ret: " << ret);
168 * test: tc05_check_privilege_by_cookie
169 * description: Function security_server_check_privilege_by_cookie should
170 * return status of access rights of cookie owner. In this case cookie owner
171 * is the same process that ask for the rights.
172 * expected: Function call with access rights set to "r" should return SUCCESS,
173 * with "rw" should return ACCESS DENIED.
175 RUNNER_CHILD_TEST_SMACK(tc05_check_privilege_by_cookie)
178 const char *object_label = "tc05objectlabel";
179 const char *access_rights = "r";
180 const char *access_rights_ext = "rw";
181 const char *subject_label = "tc05subjectlabel";
183 smack_accesses *handle;
185 RUNNER_ASSERT(0 == smack_accesses_new(&handle));
187 RUNNER_ASSERT(0 == smack_accesses_add(handle,
192 RUNNER_ASSERT(0 == smack_accesses_apply(handle));
194 smack_accesses_free(handle);
196 RUNNER_ASSERT(0 == smack_set_label_for_self(subject_label));
198 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
199 security_server_request_cookie(cookie,20));
201 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
203 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
204 security_server_check_privilege_by_cookie(
209 RUNNER_ASSERT(SECURITY_SERVER_API_ERROR_ACCESS_DENIED ==
210 security_server_check_privilege_by_cookie(
217 * test: security_server_check_privilege_by_sockfd
218 * description: This test will create dummy server that will accept connection
219 * and die. The client will try to check access rights using connection descriptor.
220 * expected: Function call with access rights set to "r" should return SUCCESS,
221 * with "rw" should return ACCESS DENIED.
223 RUNNER_MULTIPROCESS_TEST_SMACK(tc06_check_privilege_by_sockfd)
225 const char *object_label = "tc06objectlabel";
226 const char *access_rights = "r";
227 const char *access_rights_ext = "rw";
228 const char *subject_label = "tc06subjectlabel";
233 smack_accesses *handle;
234 RUNNER_ASSERT(0 == smack_accesses_new(&handle));
235 RUNNER_ASSERT(0 == smack_accesses_add(handle,
239 RUNNER_ASSERT(0 == smack_accesses_apply(handle));
240 smack_accesses_free(handle);
244 RUNNER_ASSERT(-1 != pid);
248 RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(subject_label), "child label " << subject_label << " not set");
250 int sockfd = create_new_socket();
251 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
253 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
255 label = security_server_get_smacklabel_sockfd(sockfd);
256 RUNNER_ASSERT_MSG(label != NULL, "security_server_get_smacklabel_sockfd failed");
257 RUNNER_ASSERT_MSG(strcmp(label,"") == 0, "label is \"" << label << "\"");
260 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
262 RUNNER_ASSERT_MSG(listen(sockfd, 5) >= 0, "child listen failed");
264 label = security_server_get_smacklabel_sockfd(sockfd);
265 RUNNER_ASSERT_MSG(label != NULL, "security_server_get_smacklabel_sockfd failed");
266 RUNNER_ASSERT_MSG(strcmp(label,"") == 0, "label is \"" << label << "\"");
269 struct sockaddr_un client_addr;
270 socklen_t client_len = sizeof(client_addr);
272 RUNNER_ASSERT_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0, "child accept failed");
281 int sockfd = connect_to_testserver();
282 RUNNER_ASSERT_MSG(sockfd >= 0, "connect_to_testserver() failed");
284 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
286 label = security_server_get_smacklabel_sockfd(sockfd);
287 RUNNER_ASSERT_MSG(label != NULL, "security_server_get_smacklabel_sockfd failed");
288 RUNNER_ASSERT_MSG(strcmp(label,subject_label) == 0, "label is \"" << label << "\"" << ", subject_label is \"" << subject_label << "\"" );
291 result1 = security_server_check_privilege_by_sockfd(
295 result2 = security_server_check_privilege_by_sockfd(
301 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result = " << result1);
302 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_ACCESS_DENIED == result2, "result = " << result2);
306 * test: security_server_check_privilege_by_sockfd
307 * description: This test will create dummy server that will accept connection
308 * and die. The client will try to check access rights using connection descriptor.
309 * Because we read a smack label not from socket directly, but from from pid of process
310 * on the other end of socket - that's why smack label will be updated.
311 * In this test client is running under root and server is not - to test the extreme case.
312 * expected: Function call with access rights set to "r" should return SUCCESS,
313 * with "rw" should return ACCESS DENIED.
315 RUNNER_MULTIPROCESS_TEST_SMACK(tc07_check_privilege_by_sockfd)
317 RUNNER_IGNORED_MSG("This test drop privileges");
318 const char *object_label = "tc07objectlabel";
319 const char *access_rights = "r";
320 const char *access_rights_ext = "rw";
321 const char *subject_label = "tc07subjectlabel";
326 smack_accesses *handle;
327 RUNNER_ASSERT(0 == smack_accesses_new(&handle));
328 RUNNER_ASSERT(0 == smack_accesses_add(handle,
332 RUNNER_ASSERT(0 == smack_accesses_apply(handle));
333 smack_accesses_free(handle);
336 RUNNER_ASSERT(-1 != pid);
340 int sockfd = create_new_socket();
341 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
343 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
345 RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(subject_label), "child label " << subject_label << " not set");
347 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
349 RUNNER_ASSERT_MSG(listen(sockfd, 5) >= 0, "child listen failed");
351 struct sockaddr_un client_addr;
352 socklen_t client_len = sizeof(client_addr);
353 int csockfd = TEMP_FAILURE_RETRY(accept(sockfd,(struct sockaddr*)&client_addr, &client_len));
361 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
364 int sockfd = connect_to_testserver();
365 RUNNER_ASSERT_MSG(sockfd >= 0, "connect_to_testserver() failed");
367 result1 = security_server_check_privilege_by_sockfd(
371 result2 = security_server_check_privilege_by_sockfd(
379 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result1 = " << result1);
380 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_ACCESS_DENIED == result2, " result2 = " << result2);
383 ///////////////////////////
384 /////NOSMACK ENV TESTS/////
385 ///////////////////////////
388 * First four test cases are the same as their SMACK versions. The only difference is environment
389 * preparation (described near ENVIRONMENT_NOSMACK macro).
391 RUNNER_CHILD_TEST_NOSMACK(tc01_security_server_get_cookie_size_nosmack)
395 int ret = security_server_get_cookie_size();
396 RUNNER_ASSERT_MSG(ret == 20, "ret = " << ret);
399 RUNNER_CHILD_TEST_NOSMACK(tc02_security_server_request_cookie_normal_case_nosmack)
404 int ret = security_server_request_cookie(cookie, 20);
405 RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "ret = " << ret);
408 RUNNER_CHILD_TEST_NOSMACK(tc03_security_server_request_cookie_too_small_buffer_size_nosmack)
413 int ret = security_server_request_cookie(cookie, 10);
414 RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_ERROR_BUFFER_TOO_SMALL, "ret = " << ret);
417 RUNNER_CHILD_TEST_NOSMACK(tc04_security_server_get_gid_nosmack)
421 int ret = security_server_get_gid("definitely_not_existing_object");
422 RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT, "ret = " << ret);
423 ret = security_server_get_gid("root");
424 RUNNER_ASSERT_MSG(ret == 0, "ret = " << ret);
428 * NOSMACK version of tc05 test.
430 * Correct behaviour of smack_accesses_apply and smack_set_label_for_self was checked by libsmack
431 * tests. We assume, that those tests pass. Additionally security_server_check_privilege_by_cookie
432 * should return SUCCESS no matter what access_rights we give to this function.
434 RUNNER_CHILD_TEST_NOSMACK(tc05_check_privilege_by_cookie_nosmack)
437 const char* object_label = "tc05objectlabel";
439 RUNNER_ASSERT(security_server_request_cookie(cookie,20) == SECURITY_SERVER_API_SUCCESS);
441 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
443 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
444 security_server_check_privilege_by_cookie(cookie, object_label, "r"));
446 //On NOSMACK env security server should return success on any accesses, even those that are
448 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
449 security_server_check_privilege_by_cookie(cookie, object_label, "rw"));
453 * NOSMACK version of tc06 test.
455 * Differences between this and SMACK version (server):
456 * - Skipped setting access_rights
457 * - Skipped setting label for server
458 * - get_smacklabel_sockfd is called only once for server, almost right after fork and creation
459 * of socket (because it should do nothing when SMACK is off)
460 * - After get_smacklabel_sockfd privileges are dropped and server is prepared to accept connections
463 * For client the only difference are expected results from check_privilege_by_sockfd - both should
466 RUNNER_MULTIPROCESS_TEST_NOSMACK(tc06_check_privilege_by_sockfd_nosmack)
468 const char* object_label = "tc06objectlabel";
475 RUNNER_ASSERT(pid >= 0);
479 if (pid == 0) { //child process - server
481 int sockfd = create_new_socket();
482 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
484 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
486 //check if get_smacklabel_sockfd works correctly
487 label = security_server_get_smacklabel_sockfd(sockfd);
488 RUNNER_ASSERT_MSG(label != NULL, "security_server_get_smacklabel_sockfd failed");
489 ret = strcmp(label, "");
491 RUNNER_ASSERT_MSG(ret == 0, "label is \"" << label << "\"");
493 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
495 RUNNER_ASSERT_MSG(listen(sockfd, 5) >= 0, "child listen failed");
497 struct sockaddr_un client_addr;
498 socklen_t client_len = sizeof(client_addr);
501 RUNNER_ASSERT_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0, "child accept failed");
503 //wait a little bit for parent to do it's job
506 //if everything works, cleanup and return 0
512 int sockfd = connect_to_testserver();
513 RUNNER_ASSERT_MSG(sockfd >= 0, "Failed to connect to server.");
515 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
517 label = security_server_get_smacklabel_sockfd(sockfd);
518 RUNNER_ASSERT_MSG(label != NULL, "get_smacklabel_sockfd failed.");
519 ret = strcmp(label, "");
521 RUNNER_ASSERT_MSG(ret == 0, "label is \"" << label << "\"");
523 result1 = security_server_check_privilege_by_sockfd(sockfd, object_label, "r");
524 result2 = security_server_check_privilege_by_sockfd(sockfd, object_label, "rw");
527 RUNNER_ASSERT_MSG(result1 == SECURITY_SERVER_API_SUCCESS, "result = " << result1);
528 RUNNER_ASSERT_MSG(result2 == SECURITY_SERVER_API_SUCCESS, "result = " << result2);
532 * NOSMACK version of tc07 test.
534 RUNNER_MULTIPROCESS_TEST_NOSMACK(tc07_check_privilege_by_sockfd_nosmack)
536 RUNNER_IGNORED_MSG("This test drop privileges");
537 const char* object_label = "tc07objectlabel";
543 RUNNER_ASSERT(-1 != pid);
545 if (pid == 0) { //child process
547 int sockfd = create_new_socket();
548 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
550 SockFDUniquePtr sockfd_ptr(&sockfd, closesockfdptr);
553 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
555 //Prepare for accepting
556 RUNNER_ASSERT_MSG(listen(sockfd, 5) >= 0, "child listen failed");
558 struct sockaddr_un client_addr;
559 socklen_t client_len = sizeof(client_addr);
563 RUNNER_ASSERT_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0, "child accept failed");
565 //wait a little bit for parent to do it's job
568 //cleanup and kill child
571 } else { //parent process
572 //Drop root privileges
573 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
575 //Wait for server to set up
578 //Connect and check privileges
579 int sockfd = connect_to_testserver();
580 RUNNER_ASSERT_MSG(sockfd >= 0, "Failed to create socket fd.");
582 result1 = security_server_check_privilege_by_sockfd(sockfd, object_label, "r");
583 result2 = security_server_check_privilege_by_sockfd(sockfd, object_label, "rw");
588 //Both results (just like in the previous test case) should return success.
589 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result1 = " << result1);
590 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result2, "result2 = " << result2);
593 int apply_smack_rule(const char *subject, const char *object, const char *rule)
595 struct smack_accesses *ruleHandler = NULL;
596 if (smack_accesses_new(&ruleHandler) != 0)
598 if (smack_accesses_add(ruleHandler, subject, object, rule) != 0)
600 if (smack_accesses_apply(ruleHandler) != 0)
603 smack_accesses_free(ruleHandler);
607 smack_accesses_free(ruleHandler);
611 RUNNER_TEST(tc10_security_server_get_uid_by_cookie)
613 int cookieSize = security_server_get_cookie_size();
614 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
616 std::vector<char> cookie(cookieSize);
617 int retval = security_server_request_cookie(&cookie[0], cookieSize);
618 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
621 uid_t cookieUid, realUid;
623 retval = security_server_get_uid_by_cookie(&cookie[0], &cookieUid);
624 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get UID from cookie. My uid: " << realUid << " Server error: " << retval);
625 RUNNER_ASSERT_MSG(realUid == cookieUid, "No match in received UID");
627 //checking for input parameters
628 retval = security_server_get_uid_by_cookie(NULL, &cookieUid);
629 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "Error in checking input parameters by function");
630 retval = security_server_get_uid_by_cookie(&cookie[0], NULL);
631 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "Error in checking input parameters by function");
634 RUNNER_CHILD_TEST(tc11_security_server_get_uid_by_cookie)
636 int cookieSize = security_server_get_cookie_size();
637 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
639 std::vector<char> cookie(cookieSize);
640 int retval = security_server_request_cookie(&cookie[0], cookieSize);
641 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
643 //preapare SMACK environment
644 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
645 RUNNER_ASSERT_MSG(smack_revoke_subject("BialyMis") == 0, "Error in smack_revoke_subject");
647 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
651 retval = security_server_get_uid_by_cookie(&cookie[0], &cookieUid);
652 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "Socket not protected by smack");
655 RUNNER_CHILD_TEST(tc12_security_server_get_uid_by_cookie)
657 int cookieSize = security_server_get_cookie_size();
658 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
660 uid_t realUid = getuid();
662 std::vector<char> cookie(cookieSize);
663 int retval = security_server_request_cookie(&cookie[0], cookieSize);
664 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
666 //preapare SMACK environment
667 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
668 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-check", "w") == 0, "Error in adding rule");
670 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
674 retval = security_server_get_uid_by_cookie(&cookie[0], &cookieUid);
675 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get UID from cookie");
676 RUNNER_ASSERT_MSG(realUid == cookieUid, "No match in received UID");
679 RUNNER_CHILD_TEST(tc13_security_server_get_uid_by_cookie)
681 int cookieSize = security_server_get_cookie_size();
682 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
684 //preapare SMACK environment
685 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
686 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-check", "w") == 0, "Error in adding rule");
687 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-get", "w") == 0, "Error in adding rule");
689 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
691 std::vector<char> cookie(cookieSize);
692 int retval = security_server_request_cookie(&cookie[0], cookieSize);
693 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
696 uid_t cookieUid, realUid = getuid();
697 retval = security_server_get_uid_by_cookie(&cookie[0], &cookieUid);
698 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get UID from cookie");
699 RUNNER_ASSERT_MSG(realUid == cookieUid, "No match in received UID");
702 RUNNER_TEST(tc14_security_server_get_gid_by_cookie)
704 int cookieSize = security_server_get_cookie_size();
705 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
707 std::vector<char> cookie(cookieSize);
708 int retval = security_server_request_cookie(&cookie[0], cookieSize);
709 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
712 gid_t cookieGid, realGid;
714 retval = security_server_get_gid_by_cookie(&cookie[0], &cookieGid);
715 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get GID from cookie");
716 RUNNER_ASSERT_MSG(realGid == cookieGid, "No match in received GID");
718 //checking for input parameters
719 retval = security_server_get_gid_by_cookie(NULL, &cookieGid);
720 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "Error in checking input parameters by function");
721 retval = security_server_get_gid_by_cookie(&cookie[0], NULL);
722 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "Error in checking input parameters by function");
726 RUNNER_CHILD_TEST(tc15_security_server_get_gid_by_cookie)
728 int cookieSize = security_server_get_cookie_size();
729 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
731 std::vector<char> cookie(cookieSize);
732 int retval = security_server_request_cookie(&cookie[0], cookieSize);
733 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
735 //preapare SMACK environment
736 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
737 RUNNER_ASSERT_MSG(smack_revoke_subject("BialyMis") == 0, "Error in smack_revoke_subject");
739 RUNNER_ASSERT_MSG(setgid(5000) == 0, "Unable to drop privileges");
740 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
743 gid_t cookieGid, realGid;
745 retval = security_server_get_gid_by_cookie(&cookie[0], &cookieGid);
746 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED, "Socket not protected by smack");
749 RUNNER_CHILD_TEST(tc16_security_server_get_gid_by_cookie)
751 int cookieSize = security_server_get_cookie_size();
752 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
754 std::vector<char> cookie(cookieSize);
756 gid_t realGid = getgid();
757 int retval = security_server_request_cookie(&cookie[0], cookieSize);
758 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
760 //preapare SMACK environment
761 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
762 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-check", "w") == 0, "Error in adding rule");
764 RUNNER_ASSERT_MSG(setgid(5000) == 0, "Unable to drop privileges");
765 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
769 retval = security_server_get_gid_by_cookie(&cookie[0], &cookieGid);
770 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get GID from cookie");
771 RUNNER_ASSERT_MSG(realGid == cookieGid, "No match in received GID. ReadGid: " << realGid << " CookieGid: " << cookieGid);
774 RUNNER_CHILD_TEST(tc17_security_server_get_gid_by_cookie)
776 int cookieSize = security_server_get_cookie_size();
777 RUNNER_ASSERT_MSG(cookieSize == 20, "Wrong cookie size");
779 //preapare SMACK environment
780 RUNNER_ASSERT_MSG(smack_set_label_for_self("BialyMis") == 0, "Unable to set label for self");
781 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-check", "w") == 0, "Error in adding rule");
782 RUNNER_ASSERT_MSG(apply_smack_rule("BialyMis", "security-server::api-cookie-get", "w") == 0, "Error in adding rule");
784 RUNNER_ASSERT_MSG(setgid(5000) == 0, "Unable to drop privileges");
785 RUNNER_ASSERT_MSG(setuid(5000) == 0, "Unable to drop privileges");
787 std::vector<char> cookie(cookieSize);
788 int retval = security_server_request_cookie(&cookie[0], cookieSize);
789 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get cookie");
792 gid_t cookieGid, realGid = getgid();
793 retval = security_server_get_gid_by_cookie(&cookie[0], &cookieGid);
794 RUNNER_ASSERT_MSG(retval == SECURITY_SERVER_API_SUCCESS, "Unable to get GID from cookie");
795 RUNNER_ASSERT_MSG(realGid == cookieGid, "No match in received GID. ReadGid: " << realGid << " CookieGid: " << cookieGid);
799 RUNNER_TEST_SMACK(tc18_security_server_get_smacklabel_cookie) {
804 char *label_smack = NULL;
805 char *label_ss = NULL;
808 int cookie_size = security_server_get_cookie_size();
809 RUNNER_ASSERT_MSG(PROPER_COOKIE_SIZE == cookie_size, "Wrong cookie size from security-server");
813 cookie = (char*) calloc(cookie_size, 1);
814 RUNNER_ASSERT_MSG(NULL != cookie, "Memory allocation error");
816 res = security_server_request_cookie(cookie, cookie_size);
817 if (res != SECURITY_SERVER_API_SUCCESS) {
819 RUNNER_ASSERT_MSG(res == SECURITY_SERVER_API_SUCCESS, "Error in requesting cookie from security-server");
822 label_ss = security_server_get_smacklabel_cookie(cookie);
824 RUNNER_ASSERT_MSG(label_ss != NULL, "Error in getting label by cookie");
827 std::string label_cookie(label_ss);
830 res = smack_new_label_from_self(&label_smack);
833 RUNNER_ASSERT_MSG(res == 0, "Error in getting self SMACK label");
835 std::string label_self(label_smack ? label_smack : "");
838 RUNNER_ASSERT_MSG(label_self == label_cookie, "No match in SMACK labels");
841 //TODO: here could be label change using SMACK API and checking if it
842 //is changed using security-server API function based on the same cookie
846 * NOSMACK version of tc_security_server_get_smacklabel_cookie test.
848 * Most of this test goes exactly as the original one. The only difference are the labels:
849 * - We assume that libsmack tests passed and smack_new_label_from_self will return -1 and NULL
850 * label - there is no need to re-check it.
851 * - Label acquired from security_server_get_smacklabel_cookie should be an empty string.
853 RUNNER_TEST_NOSMACK(tc_security_server_get_smacklabel_cookie_nosmack) {
858 char* label_ss = NULL;
861 int cookie_size = security_server_get_cookie_size();
862 RUNNER_ASSERT_MSG(PROPER_COOKIE_SIZE == cookie_size,
863 "Wrong cookie size from security-server. Size: " << cookie_size);
865 cookie = (char*) calloc(cookie_size, sizeof(char));
866 RUNNER_ASSERT_MSG(NULL != cookie, "Memory allocation error");
870 //Request cookie from SS
871 res = security_server_request_cookie(cookie, cookie_size);
872 std::unique_ptr<char, std::function<void(char*)> > cookie_ptr(cookie, free);
874 if (res != SECURITY_SERVER_API_SUCCESS) {
875 RUNNER_ASSERT_MSG(res == SECURITY_SERVER_API_SUCCESS,
876 "Error in requesting cookie from security-server. Result: " << res);
879 label_ss = security_server_get_smacklabel_cookie(cookie_ptr.get());
880 RUNNER_ASSERT_MSG(label_ss != NULL, "Error in getting label by cookie");
882 //Check if label_ss is correct, that is only one NULL character.
883 if (label_ss[0] != '\0') {
885 RUNNER_ASSERT_MSG(label_ss[0] == '\0', "label_ss was not an empty string.");
895 int main(int argc, char *argv[])
898 DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);