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