Add parameters to security-manager tests functions.
[platform/core/test/security-tests.git] / tests / security-manager-tests / security_manager_tests.cpp
1 #include <dpl/test/test_runner.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <memory.h>
5 #include <summary_collector.h>
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/xattr.h>
10 #include <linux/xattr.h>
11
12 #include <libprivilege-control_test_common.h>
13 #include <tests_common.h>
14
15 #include <security-manager.h>
16 #include <sm_db.h>
17
18 DEFINE_SMARTPTR(security_manager_app_inst_req_free, app_inst_req, AppInstReqUniquePtr);
19
20 static const char *const SM_APP_ID1 = "sm_test_app_id_double";
21 static const char *const SM_PKG_ID1 = "sm_test_pkg_id_double";
22
23 static const char *const SM_APP_ID2 = "sm_test_app_id_full";
24 static const char *const SM_PKG_ID2 = "sm_test_pkg_id_full";
25
26 static const privileges_t SM_ALLOWED_PRIVILEGES = {
27     "security_manager_test_rules2_r",
28     "security_manager_test_rules2_no_r"
29 };
30
31 static const privileges_t SM_DENIED_PRIVILEGES  = {
32     "security_manager_test_rules1",
33     "security_manager_test_rules2"
34 };
35
36 static const char *const XATTR_NAME_TIZENEXEC =  XATTR_SECURITY_PREFIX "TIZEN_EXEC_LABEL";
37
38 static const rules_t SM_ALLOWED_RULES = {
39     { USER_APP_ID, "test_sm_book_8", "r" },
40     { USER_APP_ID, "test_sm_book_9", "w" },
41     { USER_APP_ID, "test_sm_book_10", "x" },
42     { USER_APP_ID, "test_sm_book_11", "rw" },
43     { USER_APP_ID, "test_sm_book_12", "rx" },
44     { USER_APP_ID, "test_sm_book_13", "wx" },
45     { USER_APP_ID, "test_sm_book_14", "rwx" },
46     { USER_APP_ID, "test_sm_book_15", "rwxat" },
47     { "test_sm_subject_8", USER_APP_ID, "r" },
48     { "test_sm_subject_9", USER_APP_ID, "w" },
49     { "test_sm_subject_10", USER_APP_ID, "x" },
50     { "test_sm_subject_11", USER_APP_ID, "rw" },
51     { "test_sm_subject_12", USER_APP_ID, "rx" },
52     { "test_sm_subject_13", USER_APP_ID, "wx" },
53     { "test_sm_subject_14", USER_APP_ID, "rwx" },
54     { "test_sm_subject_15", USER_APP_ID, "rwxat" }
55 };
56 static const rules_t SM_DENIED_RULES = {
57     { USER_APP_ID, "test_sm_book_1", "r" },
58     { USER_APP_ID, "test_sm_book_2", "w" },
59     { USER_APP_ID, "test_sm_book_3", "x" },
60     { USER_APP_ID, "test_sm_book_4", "rw" },
61     { USER_APP_ID, "test_sm_book_5", "rx" },
62     { USER_APP_ID, "test_sm_book_6", "wx" },
63     { USER_APP_ID, "test_sm_book_7", "rwx" },
64     { "test_sm_subject_1", USER_APP_ID, "r" },
65     { "test_sm_subject_2", USER_APP_ID, "w" },
66     { "test_sm_subject_3", USER_APP_ID, "x" },
67     { "test_sm_subject_4", USER_APP_ID, "rw" },
68     { "test_sm_subject_5", USER_APP_ID, "rx" },
69     { "test_sm_subject_6", USER_APP_ID, "wx" },
70     { "test_sm_subject_7", USER_APP_ID, "rwx" }
71 };
72
73 static const char *const SM_PRIVATE_PATH = "/etc/smack/test_DIR/app_dir";
74 static const char *const SM_PUBLIC_PATH = "/etc/smack/test_DIR/app_dir_public";
75 static const char *const SM_PUBLIC_RO_PATH = "/etc/smack/test_DIR/app_dir_public_ro";
76 static const char *const SM_DENIED_PATH = "/etc/smack/test_DIR/non_app_dir";
77
78
79 static bool isLinkToExec(const char *fpath, const struct stat *sb)
80 {
81
82     struct stat buf;
83     char *target;
84     int ret;
85
86     // check if it's a link
87     if ( !S_ISLNK(sb->st_mode))
88         return false;
89
90     target = realpath(fpath, NULL);
91     RUNNER_ASSERT_MSG_BT(target != 0, "Could not obtain real path from link.");
92
93     ret = stat(target, &buf);
94     RUNNER_ASSERT_MSG_BT(ret == 0, "Could not obtain real path's stat from link.");
95
96     if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG))
97         return false;
98
99
100     return true;
101 }
102
103 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
104                               const char* correctLabel, bool transmute_test, bool exec_test)
105 {
106     int result;
107     CStringPtr labelPtr;
108     char* label = NULL;
109
110     /* ACCESS */
111     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
112     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
113     labelPtr.reset(label);
114     RUNNER_ASSERT_MSG_BT(label != NULL, "ACCESS label on " << fpath << " is not set");
115     result = strcmp(correctLabel, label);
116     RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is incorrect"
117             " (should be '" << correctLabel << "' and is '" << label << "')");
118
119
120     /* EXEC */
121     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
122     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
123     labelPtr.reset(label);
124
125     if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
126         RUNNER_ASSERT_MSG_BT(label != NULL, "EXEC label on " << fpath << " is not set");
127         result = strcmp(correctLabel, label);
128         RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect EXEC label on executable file " << fpath);
129     } else
130         RUNNER_ASSERT_MSG_BT(label == NULL, "EXEC label on " << fpath << " is set");
131
132
133     /* LINK TO EXEC */
134     if (isLinkToExec(fpath, sb) && exec_test) {
135         char buf[SMACK_LABEL_LEN+1];
136         result = lgetxattr(fpath, XATTR_NAME_TIZENEXEC, buf, sizeof(buf));
137         RUNNER_ASSERT_MSG_BT(result != -1, "Could not get label for the path "
138                 << fpath << "("<<strerror(errno)<<")");
139         buf[result]='\0';
140         result = strcmp(correctLabel, buf);
141         RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect TIZEN_EXEC_LABEL attribute"
142                 " on link to executable " << fpath);
143     }
144
145
146
147     /* TRANSMUTE */
148     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
149     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
150     labelPtr.reset(label);
151
152     if (S_ISDIR(sb->st_mode) && transmute_test == true) {
153         RUNNER_ASSERT_MSG_BT(label != NULL, "TRANSMUTE label on " << fpath << " is not set at all");
154         RUNNER_ASSERT_MSG_BT(strcmp(label,"TRUE") == 0,
155                 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
156     } else {
157         RUNNER_ASSERT_MSG_BT(label == NULL, "TRANSMUTE label on " << fpath << " is set");
158     }
159
160     return 0;
161 }
162
163
164 static int nftw_check_sm_labels_app_private_dir(const char *fpath, const struct stat *sb,
165                                int /*typeflag*/, struct FTW* /*ftwbuf*/)
166 {
167     return nftw_check_sm_labels_app_dir(fpath, sb, USER_APP_ID, false, true);
168 }
169
170 static int nftw_check_sm_labels_app_public_dir(const char *fpath, const struct stat *sb,
171                                int /*typeflag*/, struct FTW* /*ftwbuf*/)
172 {
173
174     return nftw_check_sm_labels_app_dir(fpath, sb, "User", true, false);
175 }
176
177 static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct stat *sb,
178                                int /*typeflag*/, struct FTW* /*ftwbuf*/)
179 {
180
181     return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
182 }
183
184 static app_inst_req* do_app_inst_req_new()
185 {
186     int result;
187     app_inst_req *req = NULL;
188
189     result = security_manager_app_inst_req_new(&req);
190     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
191             "creation of new request failed. Result: " << result);
192     RUNNER_ASSERT_MSG_BT(req != NULL, "creation of new request did not allocate memory");
193     return req;
194 }
195
196 static void prepare_app_path()
197 {
198     int result;
199
200     result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
201     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
202
203     result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
204     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
205
206     result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
207     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
208
209     result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
210     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
211 }
212
213 static void prepare_app_env()
214 {
215     prepare_app_path();
216 }
217
218 /* TODO: add parameters to this function */
219 static void check_app_path_after_install()
220 {
221     int result;
222
223     result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
224     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
225
226     result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
227     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
228
229     result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
230     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
231
232     result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
233     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
234 }
235
236 static void check_app_permissions(const char *const app_id, const char *const pkg_id,
237                                   const privileges_t &allowed_privs, const privileges_t &denied_privs,
238                                   const rules_t &allowed_rules, const rules_t &denied_rules)
239 {
240     bool result;
241
242     result = check_all_accesses(smack_check(), allowed_rules);
243     RUNNER_ASSERT_MSG_BT(result, "Permissions not added.");
244     result = check_no_accesses(smack_check(), denied_rules);
245     RUNNER_ASSERT_MSG_BT(result, "Permissions added.");
246
247     /* TODO: USER_APP_ID is hardcoded in the following checks, because libprivilege always generate
248      *       label "User" for all installed apps. Adjust it when libprivilege is upgraded. */
249     (void)app_id; // unused parameter
250     (void)pkg_id; // unused parameter
251
252     for (auto it = allowed_privs.begin(); it != allowed_privs.end(); ++it)
253         check_perm_app_has_permission(USER_APP_ID, (*it).c_str(), true);
254
255     for (auto it = denied_privs.begin(); it != denied_privs.end(); ++it)
256         check_perm_app_has_permission(USER_APP_ID, (*it).c_str(), false);
257 }
258
259 static void check_app_after_install(const char *const app_id, const char *const pkg_id,
260                                     const privileges_t &allowed_privs, const privileges_t &denied_privs,
261                                     const rules_t &allowed_rules, const rules_t &denied_rules)
262 {
263     check_app_permissions(app_id, pkg_id,
264                           allowed_privs, denied_privs,
265                           allowed_rules, denied_rules);
266 }
267
268 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
269
270
271 RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
272 {
273     int result;
274     AppInstReqUniquePtr request;
275
276     request.reset(do_app_inst_req_new());
277
278     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
279     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
280             "setting app id failed. Result: " << result);
281
282     result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID1);
283     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
284             "setting pkg id failed. Result: " << result);
285
286     result = security_manager_app_install(request.get());
287     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
288             "installing app failed. Result: " << result);
289
290     result = security_manager_app_install(request.get());
291     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
292             "installing already installed app failed. Result: " << result);
293
294     request.reset(do_app_inst_req_new());
295
296     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
297     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
298             "setting app id failed. Result: " << result);
299
300     result = security_manager_app_uninstall(request.get());
301     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
302             "uninstalling app failed. Result: " << result);
303
304     result = security_manager_app_uninstall(request.get());
305     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
306             "uninstalling already uninstalled app failed. Result: " << result);
307 }
308
309 RUNNER_TEST(security_manager_02_app_install_uninstall_full)
310 {
311     int result;
312     AppInstReqUniquePtr request;
313
314     prepare_app_env();
315
316     request.reset(do_app_inst_req_new());
317
318     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
319     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
320             "setting app id failed. Result: " << result);
321
322     result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
323     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
324             "setting pkg id failed. Result: " << result);
325
326     result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[0].c_str());
327     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
328             "setting allowed permission failed. Result: " << result);
329     result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[1].c_str());
330     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
331             "setting allowed permission failed. Result: " << result);
332
333     result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
334                                                     SECURITY_MANAGER_PATH_PRIVATE);
335     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
336             "setting allowed path failed. Result: " << result);
337
338     result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
339                                                     SECURITY_MANAGER_PATH_PUBLIC);
340     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
341             "setting allowed path failed. Result: " << result);
342
343     result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
344                                                     SECURITY_MANAGER_PATH_PUBLIC_RO);
345     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
346             "setting allowed path failed. Result: " << result);
347
348     result = security_manager_app_install(request.get());
349     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
350             "installing app failed. Result: " << result);
351
352     check_app_after_install(SM_APP_ID2, SM_PKG_ID2,
353                             SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES,
354                             SM_ALLOWED_RULES, SM_DENIED_RULES);
355
356     /* TODO: add parameters to this function */
357     check_app_path_after_install();
358
359     request.reset(do_app_inst_req_new());
360
361     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
362     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
363             "setting app id failed. Result: " << result);
364
365     result = security_manager_app_uninstall(request.get());
366     RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
367             "uninstalling app failed. Result: " << result);
368
369 }
370
371 int main(int argc, char *argv[])
372 {
373     SummaryCollector::Register();
374     return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
375 }