2 * Copyright (c) 2014 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>
32 #include <access_provider.h>
33 #include "tests_common.h"
36 #define PROPER_COOKIE_SIZE 20
39 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_CLIENT_SMACK)
42 * test: tc04_security_server_get_gid
43 * description: Checking for security_server_get_gid
44 * with nonexisting gid and existing one
45 * expected: security_server_get_gid should return
46 * SECURITY_SERVER_ERROR_NO_SUCH_OBJECT with first call
47 * and group id with second call
49 RUNNER_CHILD_TEST_SMACK(tc04_security_server_get_gid)
51 SecurityServer::AccessProvider provider("tc04mylabel");
53 provider.applyAndSwithToUser(APP_UID, APP_GID);
55 int ret = security_server_get_gid("abc123xyz_pysiaczek");
56 LogDebug("ret = " << ret);
57 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT == ret, "Ret: " << ret);
58 ret = security_server_get_gid("root");
59 LogDebug("ret = " << ret);
60 RUNNER_ASSERT_MSG(0 == ret, "Ret: " << ret);
64 * test: tc05_check_privilege_by_cookie
65 * description: Function security_server_check_privilege_by_cookie should
66 * return status of access rights of cookie owner. In this case cookie owner
67 * is the same process that ask for the rights.
68 * expected: Function call with access rights set to "r" should return SUCCESS,
69 * with "rw" should return ACCESS DENIED.
71 RUNNER_CHILD_TEST_SMACK(tc05_check_privilege_by_cookie)
73 RUNNER_IGNORED_MSG("security_server_check_privilege_by_cookie is temporarily disabled: always returns success");
75 const char *object_label = "tc05objectlabel";
76 const char *access_rights = "r";
77 const char *access_rights_ext = "rw";
78 const char *subject_label = "tc05subjectlabel";
80 SecurityServer::AccessProvider provider(subject_label);
82 provider.addObjectRule(object_label, access_rights);
83 provider.applyAndSwithToUser(APP_UID, APP_GID);
85 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
86 security_server_request_cookie(cookie,20));
88 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
89 security_server_check_privilege_by_cookie(
94 RUNNER_ASSERT(SECURITY_SERVER_API_ERROR_ACCESS_DENIED ==
95 security_server_check_privilege_by_cookie(
102 * test: security_server_check_privilege_by_sockfd
103 * description: This test will create dummy server that will accept connection
104 * and die. The client will try to check access rights using connection descriptor.
105 * expected: Function call with access rights set to "r" should return SUCCESS,
106 * with "rw" should return ACCESS DENIED.
108 RUNNER_MULTIPROCESS_TEST_SMACK(tc06_check_privilege_by_sockfd)
110 RUNNER_IGNORED_MSG("security_server_check_privilege_by_sockfd is temporarily disabled: always returns success");
111 const char *object_label = "tc06objectlabel";
112 const char *access_rights = "r";
113 const char *access_rights_ext = "rw";
114 const char *subject_label = "tc06subjectlabel";
119 smack_accesses *handle;
120 RUNNER_ASSERT(0 == smack_accesses_new(&handle));
121 RUNNER_ASSERT(0 == smack_accesses_add(handle,
125 RUNNER_ASSERT(0 == smack_accesses_apply(handle));
126 smack_accesses_free(handle);
130 RUNNER_ASSERT_ERRNO(-1 != pid);
134 RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(subject_label), "child label " << subject_label << " not set");
136 int sockfd = create_new_socket();
137 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
139 SockUniquePtr sockfd_ptr(&sockfd);
141 label = security_server_get_smacklabel_sockfd(sockfd);
142 RUNNER_ASSERT_MSG(label != nullptr, "security_server_get_smacklabel_sockfd failed");
143 RUNNER_ASSERT_MSG(strcmp(label,"") == 0, "label is \"" << label << "\"");
146 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
148 RUNNER_ASSERT_ERRNO_MSG(listen(sockfd, 5) >= 0, "child listen failed");
150 label = security_server_get_smacklabel_sockfd(sockfd);
151 RUNNER_ASSERT_MSG(label != nullptr, "security_server_get_smacklabel_sockfd failed");
152 RUNNER_ASSERT_MSG(strcmp(label,"") == 0, "label is \"" << label << "\"");
155 struct sockaddr_un client_addr;
156 socklen_t client_len = sizeof(client_addr);
158 RUNNER_ASSERT_ERRNO_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0,
159 "child accept failed");
168 int sockfd = connect_to_testserver();
169 RUNNER_ASSERT_MSG(sockfd >= 0, "connect_to_testserver() failed");
171 SockUniquePtr sockfd_ptr(&sockfd);
173 label = security_server_get_smacklabel_sockfd(sockfd);
174 RUNNER_ASSERT_MSG(label != nullptr, "security_server_get_smacklabel_sockfd failed");
175 RUNNER_ASSERT_MSG(strcmp(label,subject_label) == 0, "label is \"" << label << "\"" << ", subject_label is \"" << subject_label << "\"" );
178 result1 = security_server_check_privilege_by_sockfd(
182 result2 = security_server_check_privilege_by_sockfd(
188 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result = " << result1);
189 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_ACCESS_DENIED == result2, "result = " << result2);
193 * test: security_server_check_privilege_by_sockfd
194 * description: This test will create dummy server that will accept connection
195 * and die. The client will try to check access rights using connection descriptor.
196 * Because we read a smack label not from socket directly, but from from pid of process
197 * on the other end of socket - that's why smack label will be updated.
198 * In this test client is running under root and server is not - to test the extreme case.
199 * expected: Function call with access rights set to "r" should return SUCCESS,
200 * with "rw" should return ACCESS DENIED.
202 RUNNER_MULTIPROCESS_TEST_SMACK(tc07_check_privilege_by_sockfd)
204 RUNNER_IGNORED_MSG("security_server_check_privilege_by_sockfd is temporarily disabled: always returns success");
205 const char *object_label = "tc07objectlabel";
206 const char *access_rights = "r";
207 const char *access_rights_ext = "rw";
208 const char *subject_label = "tc07subjectlabel";
214 access.add(subject_label, object_label, access_rights);
218 RUNNER_ASSERT_ERRNO(-1 != pid);
223 RUNNER_ASSERT_ERRNO(-1 != pid);
227 int sockfd = create_new_socket();
228 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
230 SockUniquePtr sockfd_ptr(&sockfd);
232 RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(subject_label), "child label " << subject_label << " not set");
234 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
236 RUNNER_ASSERT_ERRNO_MSG(listen(sockfd, 5) >= 0, "child listen failed");
238 struct sockaddr_un client_addr;
239 socklen_t client_len = sizeof(client_addr);
240 int csockfd = TEMP_FAILURE_RETRY(accept(sockfd,(struct sockaddr*)&client_addr, &client_len));
248 int sockfd = connect_to_testserver();
249 RUNNER_ASSERT_MSG(sockfd >= 0, "connect_to_testserver() failed");
251 result1 = security_server_check_privilege_by_sockfd(
255 result2 = security_server_check_privilege_by_sockfd(
262 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result1 = " << result1);
263 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_ERROR_ACCESS_DENIED == result2, " result2 = " << result2);
268 ///////////////////////////
269 /////NOSMACK ENV TESTS/////
270 ///////////////////////////
272 RUNNER_CHILD_TEST_NOSMACK(tc04_security_server_get_gid_nosmack)
276 ret = drop_root_privileges();
277 RUNNER_ASSERT_MSG(ret == 0,
278 "Failed to drop root privileges. Result: " << ret << "uid = " << getuid());
280 ret = security_server_get_gid("definitely_not_existing_object");
281 RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT, "ret = " << ret);
282 ret = security_server_get_gid("root");
283 RUNNER_ASSERT_MSG(ret == 0, "ret = " << ret);
287 * NOSMACK version of tc05 test.
289 * Correct behaviour of smack_accesses_apply and smack_set_label_for_self was checked by libsmack
290 * tests. We assume, that those tests pass. Additionally security_server_check_privilege_by_cookie
291 * should return SUCCESS no matter what access_rights we give to this function.
293 RUNNER_CHILD_TEST_NOSMACK(tc05_check_privilege_by_cookie_nosmack)
295 RUNNER_IGNORED_MSG("security_server_check_privilege_by_cookie is temporarily disabled: always returns success");
297 const char* object_label = "tc05objectlabel";
299 RUNNER_ASSERT(security_server_request_cookie(cookie,20) == SECURITY_SERVER_API_SUCCESS);
301 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
303 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
304 security_server_check_privilege_by_cookie(cookie, object_label, "r"));
306 //On NOSMACK env security server should return success on any accesses, even those that are
308 RUNNER_ASSERT(SECURITY_SERVER_API_SUCCESS ==
309 security_server_check_privilege_by_cookie(cookie, object_label, "rw"));
313 * NOSMACK version of tc06 test.
315 * Differences between this and SMACK version (server):
316 * - Skipped setting access_rights
317 * - Skipped setting label for server
318 * - get_smacklabel_sockfd is called only once for server, almost right after fork and creation
319 * of socket (because it should do nothing when SMACK is off)
320 * - After get_smacklabel_sockfd privileges are dropped and server is prepared to accept connections
323 * For client the only difference are expected results from check_privilege_by_sockfd - both should
326 RUNNER_MULTIPROCESS_TEST_NOSMACK(tc06_check_privilege_by_sockfd_nosmack)
328 RUNNER_IGNORED_MSG("security_server_check_privilege_by_sockfd is temporarily disabled: always returns success");
329 const char* object_label = "tc06objectlabel";
336 RUNNER_ASSERT_ERRNO(pid >= 0);
340 if (pid == 0) { //child process - server
342 int sockfd = create_new_socket();
343 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
345 SockUniquePtr sockfd_ptr(&sockfd);
347 //check if get_smacklabel_sockfd works correctly
348 label = security_server_get_smacklabel_sockfd(sockfd);
349 RUNNER_ASSERT_MSG(label != nullptr, "security_server_get_smacklabel_sockfd failed");
350 ret = strcmp(label, "");
352 RUNNER_ASSERT_MSG(ret == 0, "label is \"" << label << "\"");
354 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
356 RUNNER_ASSERT_ERRNO_MSG(listen(sockfd, 5) >= 0, "child listen failed");
358 struct sockaddr_un client_addr;
359 socklen_t client_len = sizeof(client_addr);
362 RUNNER_ASSERT_ERRNO_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0,
363 "child accept failed");
365 //wait a little bit for parent to do it's job
368 //if everything works, cleanup and return 0
374 int sockfd = connect_to_testserver();
375 RUNNER_ASSERT_MSG(sockfd >= 0, "Failed to connect to server.");
377 SockUniquePtr sockfd_ptr(&sockfd);
379 label = security_server_get_smacklabel_sockfd(sockfd);
380 RUNNER_ASSERT_MSG(label != nullptr, "get_smacklabel_sockfd failed.");
381 ret = strcmp(label, "");
383 RUNNER_ASSERT_MSG(ret == 0, "label is \"" << label << "\"");
385 result1 = security_server_check_privilege_by_sockfd(sockfd, object_label, "r");
386 result2 = security_server_check_privilege_by_sockfd(sockfd, object_label, "rw");
389 RUNNER_ASSERT_MSG(result1 == SECURITY_SERVER_API_SUCCESS, "result = " << result1);
390 RUNNER_ASSERT_MSG(result2 == SECURITY_SERVER_API_SUCCESS, "result = " << result2);
394 * NOSMACK version of tc07 test.
396 RUNNER_MULTIPROCESS_TEST_NOSMACK(tc07_check_privilege_by_sockfd_nosmack)
398 RUNNER_IGNORED_MSG("security_server_check_privilege_by_sockfd is temporarily disabled: always returns success");
399 const char* object_label = "tc07objectlabel";
405 RUNNER_ASSERT_ERRNO(-1 != pid);
410 RUNNER_ASSERT_ERRNO(-1 != pid);
412 if (pid == 0) { //child process
414 int sockfd = create_new_socket();
415 RUNNER_ASSERT_MSG(sockfd >= 0, "create_new_socket() failed");
417 SockUniquePtr sockfd_ptr(&sockfd);
420 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
422 //Prepare for accepting
423 RUNNER_ASSERT_ERRNO_MSG(listen(sockfd, 5) >= 0, "child listen failed");
425 struct sockaddr_un client_addr;
426 socklen_t client_len = sizeof(client_addr);
430 RUNNER_ASSERT_ERRNO_MSG((csockfd = accept(sockfd,(struct sockaddr*)&client_addr, &client_len)) > 0,
431 "child accept failed");
433 //wait a little bit for parent to do it's job
436 //cleanup and kill child
439 } else { //parent process
440 //Drop root privileges
441 RUNNER_ASSERT_MSG(drop_root_privileges() == 0, "uid = " << getuid());
443 //Wait for server to set up
446 //Connect and check privileges
447 int sockfd = connect_to_testserver();
448 RUNNER_ASSERT_MSG(sockfd >= 0, "Failed to create socket fd.");
450 result1 = security_server_check_privilege_by_sockfd(sockfd, object_label, "r");
451 result2 = security_server_check_privilege_by_sockfd(sockfd, object_label, "rw");
455 //Both results (just like in the previous test case) should return success.
456 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result1, "result1 = " << result1);
457 RUNNER_ASSERT_MSG(SECURITY_SERVER_API_SUCCESS == result2, "result2 = " << result2);
462 RUNNER_TEST_SMACK(tc18_security_server_get_smacklabel_cookie) {
465 char *label_smack = nullptr;
466 char *label_ss = nullptr;
467 char *cookie = nullptr;
469 int cookie_size = security_server_get_cookie_size();
470 RUNNER_ASSERT_MSG(PROPER_COOKIE_SIZE == cookie_size, "Wrong cookie size from security-server");
472 cookie = (char*) calloc(cookie_size, 1);
473 RUNNER_ASSERT_MSG(nullptr != cookie, "Memory allocation error");
475 res = security_server_request_cookie(cookie, cookie_size);
476 if (res != SECURITY_SERVER_API_SUCCESS) {
478 RUNNER_ASSERT_MSG(res == SECURITY_SERVER_API_SUCCESS, "Error in requesting cookie from security-server");
481 label_ss = security_server_get_smacklabel_cookie(cookie);
483 RUNNER_ASSERT_MSG(label_ss != nullptr, "Error in getting label by cookie");
486 std::string label_cookie(label_ss);
489 res = smack_new_label_from_self(&label_smack);
492 RUNNER_ASSERT_MSG(res == 0, "Error in getting self SMACK label");
494 std::string label_self(label_smack ? label_smack : "");
497 RUNNER_ASSERT_MSG(label_self == label_cookie, "No match in SMACK labels");
500 //TODO: here could be label change using SMACK API and checking if it
501 //is changed using security-server API function based on the same cookie
505 * NOSMACK version of tc_security_server_get_smacklabel_cookie test.
507 * Most of this test goes exactly as the original one. The only difference are the labels:
508 * - We assume that libsmack tests passed and smack_new_label_from_self will return -1 and nullptr
509 * label - there is no need to re-check it.
510 * - Label acquired from security_server_get_smacklabel_cookie should be an empty string.
512 RUNNER_TEST_NOSMACK(tc18_security_server_get_smacklabel_cookie_nosmack) {
515 char* label_ss = nullptr;
516 char* cookie = nullptr;
518 int cookie_size = security_server_get_cookie_size();
519 RUNNER_ASSERT_MSG(PROPER_COOKIE_SIZE == cookie_size,
520 "Wrong cookie size from security-server. Size: " << cookie_size);
522 cookie = (char*) calloc(cookie_size, sizeof(char));
523 RUNNER_ASSERT_MSG(nullptr != cookie, "Memory allocation error");
525 //Request cookie from SS
526 res = security_server_request_cookie(cookie, cookie_size);
527 CookieUniquePtr cookie_ptr(cookie);
529 RUNNER_ASSERT_MSG(res == SECURITY_SERVER_API_SUCCESS,
530 "Error in requesting cookie from security-server. Result: " << res);
532 label_ss = security_server_get_smacklabel_cookie(cookie_ptr.get());
533 RUNNER_ASSERT_MSG(label_ss != nullptr, "Error in getting label by cookie");
535 std::string label(label_ss);
537 RUNNER_ASSERT_MSG(label.empty(), "label_ss is not an empty string.");
545 int main(int argc, char *argv[])
547 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);