Fix set_app_privilege tests
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases_nosmack.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15 */
16
17 /*
18  * @file        test_cases.cpp
19  * @author      Jan Olszak (j.olszak@samsung.com)
20  * @author      Rafal Krypa (r.krypa@samsung.com)
21  * @version     1.0
22  * @brief       libprivilege-control test runner
23  */
24
25 #include <memory>
26 #include <functional>
27 #include <fstream>
28
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36
37 #include <dpl/test/test_runner.h>
38 #include <dpl/test/test_runner_multiprocess.h>
39 #include <sys/smack.h>
40 #include <privilege-control.h>
41 #include <tests_common.h>
42 #include <libprivilege-control_test_common.h>
43
44 #define APP_GID       5000
45 #define APP_UID       5000
46
47 #define APP_USER_NAME "app"
48 #define APP_HOME_DIR  "/opt/home/app"
49
50
51 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
52
53 namespace {
54 typedef std::unique_ptr<smack_accesses,std::function<void (smack_accesses*)> > SmackUniquePtr;
55
56 void closefdptr(int* fd) { close(*fd); }
57 typedef std::unique_ptr<int, std::function<void (int*)> > FDUniquePtr;
58 }
59
60 /////////////////////////////////////////
61 //////NOSMACK ENVIRONMENT TESTS//////////
62 /////////////////////////////////////////
63
64 /**
65  * NOSMACK version of nftw_check_labels_app_shared_dir function.
66  *
67  * This function used with nftw should expect -1 result from smack_have_access instead of 1.
68  */
69 int nftw_check_labels_app_shared_dir_nosmack(const char *fpath, const struct stat *sb,
70                                              int /*typeflag*/, struct FTW* /*ftwbuf*/)
71 {
72     int result;
73     char* label;
74
75     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
76     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path. Result: " << result);
77     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
78
79     result = strcmp(APPID_SHARED_DIR, label);
80     RUNNER_ASSERT_MSG(result == 0,
81             "ACCESS label on " << fpath << " is incorrect. Result: " << result);
82
83     //The only exception in nftw_check_labels_app_shared_dir
84     //smack_have_access returns -1 because of no SMACK.
85     result = smack_have_access(APP_ID, APPID_SHARED_DIR, "rwxat");
86     RUNNER_ASSERT_MSG(result == -1,
87             "smack_have_access should return error (SMACK is off). Result: " << result);
88
89     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
90     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path. Result: " << result);
91     RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
92
93     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
94     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path. Result: " << result);
95     if (S_ISDIR(sb->st_mode)) {
96         RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
97         result = strcmp("TRUE", label);
98         RUNNER_ASSERT_MSG(result == 0,
99                 "TRANSMUTE label on " << fpath << " is not set. Result: " << result);
100     } else
101         RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
102
103     return 0;
104 }
105
106 RUNNER_TEST_GROUP_INIT(libprivilegecontrol_nosmack)
107
108 /**
109  * NOSMACK version of privilege_control03 test.
110  *
111  * Uses nosmack version of nftw_check_labels_app_shared_dir (defined above).
112  */
113 RUNNER_TEST_NOSMACK(privilege_control03_app_label_shared_dir_nosmack)
114 {
115     int result;
116
117     DB_BEGIN
118
119     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APP_ID);
120     RUNNER_ASSERT_MSG(result != PC_OPERATION_SUCCESS,
121             "perm_app_setup_path should fail here. Result: " << result);
122
123     DB_END
124
125     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
126     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
127             "Unable to clean up Smack labels in " << TEST_APP_DIR);
128
129     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
130     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
131             "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
132
133     DB_BEGIN
134
135     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APPID_SHARED_DIR);
136     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
137             "perm_app_setup_path() failed. Result: " << result);
138
139     DB_END
140
141     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir_nosmack, FTW_MAX_FDS, FTW_PHYS);
142     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
143             "Unable to check Smack labels for shared app dir");
144
145     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
146     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
147             "Unable to check Smack labels for non-app dir");
148 }
149
150 /**
151  * NOSMACK version of privilege_control04 test.
152  *
153  * Tries to add permisions from test_privilege_control_rules template and checks if
154  * smack_have_access returns -1 on check between every rule.
155  */
156 RUNNER_TEST_NOSMACK(privilege_control04_add_permissions_nosmack)
157 {
158     int result;
159
160     DB_BEGIN
161
162     result = perm_app_uninstall(APP_ID);
163     RUNNER_ASSERT_MSG(result == 0,
164             "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
165
166     result = perm_app_install(APP_ID);
167     RUNNER_ASSERT_MSG(result == 0,
168             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
169
170     //Add permissions
171     result = perm_app_enable_permissions(APP_ID, APP_TYPE_EFL, PRIVS_EFL, TRUE);
172     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
173             "Error adding app permissions. Result: " << result);
174
175     DB_END
176
177     //Check if smack_have_access always fails on every rule
178     result = test_have_nosmack_accesses(rules_efl);
179     RUNNER_ASSERT_MSG(result == -1,
180             "Despite SMACK being off some accesses were added. Result: " << result);
181
182     // TODO check entry in database
183 }
184
185 void set_app_privilege_nosmack(int line_no,
186                                const char* app_id, app_type_t app_type,
187                                const char** privileges, const char* type,
188                                const char* app_path, const char* dac_file,
189                                const rules_t &rules)
190 {
191     check_app_installed(line_no, app_path);
192
193     int result;
194
195     DB_BEGIN
196
197     result = perm_app_uninstall(app_id);
198     RUNNER_ASSERT_MSG(result == 0,
199             "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
200
201     result = perm_app_install(app_id);
202     RUNNER_ASSERT_MSG(result == 0,
203             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
204
205     result = perm_app_enable_permissions(app_id, app_type, privileges, 1);
206     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
207             " Error enabling app permissions. Result: " << result);
208
209     DB_END
210
211     result = test_have_nosmack_accesses(rules);
212     RUNNER_ASSERT_MSG(result == -1, "Line: " << line_no <<
213             " Permissions shouldn't be added. Result: " << result);
214
215     result = perm_app_set_privilege(app_id, type, app_path);
216     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
217             " Error in perm_app_set_privilege. Error: " << result);
218
219     //Even though app privileges are set, no smack label should be extracted.
220     char* label = NULL;
221     result = smack_new_label_from_self(&label);
222     RUNNER_ASSERT_MSG(result == -1, "Line: " << line_no <<
223             " new_label_from_self should return error (SMACK is off). Result: " << result);
224     RUNNER_ASSERT_MSG(label == NULL, "Line: " << line_no <<
225             " new_label_from_self shouldn't allocate memory for label.");
226
227     check_groups(dac_file);
228 }
229
230 /**
231  * NOSMACK version of privilege_control05_set_app_privilege test.
232  *
233  * Another very similar test to it's SMACK version, this time smack_new_label_from_self is
234  * expected to return different result.
235  */
236 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_nosmack)
237 {
238     int result;
239
240     check_app_installed(__LINE__, APP_SET_PRIV_PATH);
241
242     //Preset exec label
243     smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
244     smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
245
246     //Set app privileges
247     result = perm_app_set_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
248     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
249             "Error in perm_app_set_privilege. Error: " << result);
250
251     //Even though app privileges are set, no smack label should be extracted.
252     char* label = NULL;
253     result = smack_new_label_from_self(&label);
254     RUNNER_ASSERT_MSG(result == -1,
255             "new_label_from_self should return error (SMACK is off). Result: " << result);
256     RUNNER_ASSERT_MSG(label == NULL, "new_label_from_self shouldn't allocate memory for label.");
257
258     //Check if DAC privileges really set
259     RUNNER_ASSERT_MSG(getuid() == APP_UID, "Wrong UID");
260     RUNNER_ASSERT_MSG(getgid() == APP_GID, "Wrong GID");
261
262     result = strcmp(getenv("HOME"), APP_HOME_DIR);
263     RUNNER_ASSERT_MSG(result == 0, "Wrong HOME DIR. Result: " << result);
264
265     result = strcmp(getenv("USER"), APP_USER_NAME);
266     RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME. Result: " << result);
267
268     check_groups(LIBPRIVILEGE_TEST_DAC_FILE);
269 }
270
271 /**
272  * NOSMACK version of privilege_control05_set_app_privilege_wgt test.
273  *
274  * Same as the above, plus uses test_have_nosmack_accesses instead of test_have_all_accesses.
275  */
276 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_wgt_nosmack)
277 {
278     set_app_privilege_nosmack(__LINE__, WGT_APP_ID, APP_TYPE_WGT, PRIVS_WGT, "wgt", WGT_APP_PATH,
279             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt);
280 }
281
282 /**
283  * NOSMACK version of privilege_control05_set_app_privilege_wgt_partner test.
284  *
285  * Same as the above.
286  */
287 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_wgt_partner_nosmack)
288 {
289     set_app_privilege_nosmack(__LINE__, WGT_PARTNER_APP_ID, APP_TYPE_WGT_PARTNER, PRIVS_WGT,
290             "wgt_partner", WGT_PARTNER_APP_PATH,
291             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt_partner);
292 }
293
294 /**
295  * NOSMACK version of privilege_control05_set_app_privilege_wgt_platform test.
296  *
297  * Same as the above.
298  */
299 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_wgt_platform_nosmack)
300 {
301     set_app_privilege_nosmack(__LINE__, WGT_PLATFORM_APP_ID, APP_TYPE_WGT_PLATFORM, PRIVS_WGT,
302             "wgt_platform", WGT_PLATFORM_APP_PATH,
303             LIBPRIVILEGE_TEST_DAC_FILE_WGT, rules_wgt_platform);
304 }
305
306 /**
307  * NOSMACK version of privilege_control05_set_app_privilege_osp test.
308  *
309  * Same as the above.
310  */
311 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_osp_nosmack)
312 {
313     set_app_privilege_nosmack(__LINE__, OSP_APP_ID, APP_TYPE_OSP, PRIVS_OSP, NULL, OSP_APP_PATH,
314             LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp);
315 }
316
317 /**
318  * NOSMACK version of privilege_control05_set_app_privilege_osp_partner test.
319  *
320  * Same as the above.
321  */
322 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_osp_partner_nosmack)
323 {
324     set_app_privilege_nosmack(__LINE__, OSP_PARTNER_APP_ID, APP_TYPE_OSP_PARTNER, PRIVS_OSP,
325             NULL, OSP_PARTNER_APP_PATH, LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp_partner);
326 }
327
328 /**
329  * NOSMACK version of privilege_control05_set_app_privilege_osp_platform test.
330  *
331  * Same as the above.
332  */
333 RUNNER_CHILD_TEST_NOSMACK(privilege_control05_set_app_privilege_osp_platform_nosmack)
334 {
335     set_app_privilege_nosmack(__LINE__, OSP_PLATFORM_APP_ID, APP_TYPE_OSP_PLATFORM, PRIVS_OSP,
336             NULL, OSP_PLATFORM_APP_PATH,
337             LIBPRIVILEGE_TEST_DAC_FILE_OSP, rules_osp_platform);
338 }
339
340 /**
341  * Revoke permissions from the list. Should be executed as privileged user.
342  */
343 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_wgt_nosmack)
344 {
345     test_revoke_permissions(__LINE__, WGT_APP_ID, rules_wgt, false);
346 }
347
348 /**
349  * Revoke permissions from the list. Should be executed as privileged user.
350  */
351 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_wgt_partner_nosmack)
352 {
353     test_revoke_permissions(__LINE__, WGT_PARTNER_APP_ID, rules_wgt_partner, false);
354 }
355
356 /**
357  * Revoke permissions from the list. Should be executed as privileged user.
358  */
359 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_wgt_platform_nosmack)
360 {
361     test_revoke_permissions(__LINE__, WGT_PLATFORM_APP_ID, rules_wgt_platform, false);
362 }
363
364 /**
365  * Revoke permissions from the list. Should be executed as privileged user.
366  */
367 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_osp_nosmack)
368 {
369     test_revoke_permissions(__LINE__, OSP_APP_ID, rules_osp, false);
370 }
371
372 /**
373  * Revoke permissions from the list. Should be executed as privileged user.
374  */
375 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_osp_partner_nosmack)
376 {
377     test_revoke_permissions(__LINE__, OSP_PARTNER_APP_ID, rules_osp_partner, false);
378 }
379
380 /**
381  * Revoke permissions from the list. Should be executed as privileged user.
382  */
383 RUNNER_CHILD_TEST_NOSMACK(privilege_control06_revoke_permissions_osp_platform_nosmack)
384 {
385     test_revoke_permissions(__LINE__, OSP_PLATFORM_APP_ID, rules_osp_platform, false);
386 }
387
388 /*
389  * NOSMACK version of privilege_control10_app_register_av test.
390  *
391  * Uses NOSMACK version of checkOnlyAvAccess (mentioned above), rest of the test is identical to
392  * it's SMACK version.
393  */
394 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
395 RUNNER_TEST_NOSMACK(privilege_control10_app_register_av_nosmack)
396 {
397     RUNNER_IGNORED_MSG("app_register_av is not implemented");
398     int result;
399
400     // cleaning
401     smack_revoke_subject(APP_TEST_AV_1);
402     smack_revoke_subject(APP_TEST_AV_2);
403
404     cleaning_smack_app_files();
405
406     DB_BEGIN
407
408     // Adding two apps before antivir
409     result = perm_app_install(APP_TEST_APP_1);
410     RUNNER_ASSERT_MSG(result == 0,
411             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
412
413     result = perm_app_install(APP_TEST_APP_2);
414     RUNNER_ASSERT_MSG(result == 0,
415             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
416
417     DB_END
418
419     // Adding antivir
420     result = app_register_av(APP_TEST_AV_1);
421     RUNNER_ASSERT_MSG(result == 0,
422             "app_register_av returned " << result << ". Errno: " << strerror(errno));
423
424     // Checking added apps accesses
425     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_1)");
426     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_1)");
427
428     DB_BEGIN
429
430     // Adding third app
431     result = perm_app_install(APP_TEST_APP_3);
432     RUNNER_ASSERT_MSG(result == 0,
433             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
434
435     DB_END
436
437     // Checking app accesses
438     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_1, "perm_app_install(APP_TEST_APP_3)");
439     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_2, "perm_app_install(APP_TEST_APP_3)");
440     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_3, "perm_app_install(APP_TEST_APP_3)");
441
442     // Adding second antivir
443     result = app_register_av(APP_TEST_AV_2);
444     RUNNER_ASSERT_MSG(result == 0,
445             "app_register_av returned " << result << ". Errno: " << strerror(errno));
446
447     // Checking app accesses
448     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
449     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
450     checkOnlyAvAccessNosmack(APP_TEST_AV_1, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
451     checkOnlyAvAccessNosmack(APP_TEST_AV_2, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
452     checkOnlyAvAccessNosmack(APP_TEST_AV_2, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
453     checkOnlyAvAccessNosmack(APP_TEST_AV_2, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
454
455     // cleaning
456     smack_revoke_subject(APP_TEST_AV_1);
457     smack_revoke_subject(APP_TEST_AV_2);
458
459     cleaning_smack_app_files();
460
461 }
462 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
463
464 /**
465  * NOSMACK version of privilege_control11_app_enable_permissions test.
466  *
467  * Since the original test did the same thing around five times, there is no need to redo the
468  * same test for perm_app_enable_permissions. perm_app_enable_permissions will be called once,
469  * test_have_nosmack_accesses will check if smack_have_access still returns error and then
470  * we will check if SMACK file was correctly created.
471  */
472 RUNNER_TEST_NOSMACK(privilege_control11_app_enable_permissions_nosmack)
473 {
474     int result;
475
476     DB_BEGIN
477
478     result = perm_app_uninstall(WGT_APP_ID);
479     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
480             "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
481
482     result = perm_app_install(WGT_APP_ID);
483     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
484             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
485
486     result = perm_app_revoke_permissions(WGT_APP_ID);
487     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
488             "Error revoking app permissions. Result: " << result);
489
490     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
491     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
492             "Error enabling app permissions. Result: " << result);
493
494     DB_END
495
496     //Check if accesses aren't added
497     result = test_have_nosmack_accesses(rules2);
498     RUNNER_ASSERT_MSG(result == -1, "Permissions shouldn't be added. Result: " << result);
499
500     // TODO check entry in database
501
502     DB_BEGIN
503
504     //Clean up
505     result = perm_app_revoke_permissions(WGT_APP_ID);
506     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
507             "Error revoking app permissions. Result: " << result);
508
509     DB_END
510 }
511
512 RUNNER_CHILD_TEST_NOSMACK(privilege_control11_app_enable_permissions_efl_nosmack)
513 {
514     test_app_enable_permissions_efl(false);
515 }
516
517 /*
518  * Check perm_app_install function
519  */
520 RUNNER_CHILD_TEST_NOSMACK(privilege_control12_app_disable_permissions_efl_nosmack)
521 {
522     test_app_disable_permissions_efl(false);
523 }
524
525 /**
526  * Remove previously granted SMACK permissions based on permissions list.
527  */
528 RUNNER_TEST_NOSMACK(privilege_control12_app_disable_permissions_nosmack)
529 {
530     test_app_disable_permissions(false);
531 }
532
533 /**
534  * NOSMACK version of privilege_control13 test.
535  *
536  * Uses perm_app_reset_permissions and checks with test_have_nosmack_accesses if nothing has
537  * changed.
538  */
539 RUNNER_TEST_NOSMACK(privilege_control13_app_reset_permissions_nosmack)
540 {
541     int result;
542
543     DB_BEGIN
544
545     result = perm_app_uninstall(WGT_APP_ID);
546     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
547             "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
548
549     result = perm_app_install(WGT_APP_ID);
550     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
551             "perm_app_install returned " << result << ". Errno: " << strerror(errno));
552
553     // Prepare permissions to reset
554     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, 1);
555     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
556             " Error adding app permissions. Result: " << result);
557
558     // Reset permissions
559     result = perm_app_reset_permissions(WGT_APP_ID);
560     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
561             "Error reseting app permissions. Result: " << result);
562
563     DB_END
564
565     result = test_have_nosmack_accesses(rules2);
566     RUNNER_ASSERT_MSG(result == -1, "Permissions shouldn't be changed. Result: " << result);
567
568     DB_BEGIN
569
570     // Disable permissions
571     result = perm_app_revoke_permissions(WGT_APP_ID);
572     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
573             "Error disabling app permissions. Result: " << result);
574
575     DB_END
576 }
577
578 /**
579  * NOSMACK version of privilege_control15_app_id_from_socket.
580  *
581  * SMACK version of this test case utilized smack_new_label_from_self and smack_set_label_for_self.
582  * Those functions rely on /proc/self/attr/current file, which is unreadable and has no contents on
583  * NOSMACK environment. Functions mentioned above were tested during libsmack tests, so they are
584  * assumed to react correctly and are not tested in this test case.
585  *
586  * This test works similarly to libsmack test smack09_new_label_from_socket. At first server and
587  * client are created then sockets are set up and perm_app_id_from_socket is used. On NOSMACK env
588  * correct behavior for perm_app_id_from_socket would be returning NULL label.
589  */
590 RUNNER_MULTIPROCESS_TEST_NOSMACK(privilege_control15_app_id_from_socket_nosmack)
591 {
592     int pid;
593     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
594
595     //Clean up before creating socket
596     unlink(SOCK_PATH);
597
598     //Create our server and client with fork
599     pid = fork();
600     RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
601
602     if (!pid) { //child (server)
603         int sock, result, fd;
604
605         //Create a socket
606         sock = socket(AF_UNIX, SOCK_STREAM, 0);
607         RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
608
609         //Bind socket to address
610         result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
611         if (result != 0) {
612             close(sock);
613             RUNNER_ASSERT_MSG(false, "bind failed: " << strerror(errno));
614         }
615
616         //Prepare for listening
617         result = listen(sock, 1);
618         if (result != 0) {
619             close(sock);
620             RUNNER_ASSERT_MSG(false, "listen failed: " << strerror(errno));
621         }
622
623         //Accept connection
624         alarm(2);
625         fd = accept(sock, NULL, NULL);
626         alarm(0);
627         RUNNER_ASSERT_MSG(fd >= 0, "accept failed: " << strerror(errno));
628
629         //Wait a little bit for client to use perm_app_id_from_socket
630         usleep(200);
631
632         //cleanup
633         close(sock);
634         exit(0);
635     } else { //parent (client)
636         // Give server some time to setup listening socket
637         sleep(1);
638         int sock, result;
639         char* smack_label = NULL;
640
641         //Create socket
642         sock = socket(AF_UNIX, SOCK_STREAM, 0);
643         RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
644
645         //Try connecting to address
646         result = connect(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
647         if (result != 0) {
648             close(sock);
649             RUNNER_ASSERT_MSG(0, "connect failed: " << strerror(errno));
650         }
651
652         //Use perm_app_id_from_socket. Should fail and return NULL smack_label.
653         smack_label = perm_app_id_from_socket(sock);
654         if (smack_label != NULL) {
655             close(sock);
656             RUNNER_ASSERT_MSG(0, "perm_app_id_from_socket should fail.");
657         }
658
659         //cleanup
660         close(sock);
661         RUNNER_ASSERT_MSG(smack_label == NULL, "perm_app_id_from_socket should fail.");
662     }
663 }
664
665 /**
666  * Next three functions are defined only because of NOSMACK environment.
667  *
668  * Inside check_labels_dir_nosmack, smack_have_access should expect error, not access granted.
669  */
670 int check_labels_dir_nosmack(const char *fpath, const struct stat *sb,
671                              const char *labels_db_path, const char *dir_db_path,
672                              const char *access)
673 {
674     int result;
675     char* label;
676     char* label_gen;
677     char label_temp[SMACK_LABEL_LEN + 1];
678     std::fstream fs_db;
679
680     /* ACCESS */
681     result = smack_lgetlabel(fpath, &label_gen, SMACK_LABEL_ACCESS);
682     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path. Result: " << result);
683     RUNNER_ASSERT_MSG(label_gen != NULL, "ACCESS label on " << fpath << " is not set");
684
685     /* EXEC */
686     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
687     if (result != 0) {
688         free(label_gen);
689         RUNNER_ASSERT_MSG(false, "Could not get label for the path. Result: " << result);
690     }
691     if (label != NULL) {
692         free(label_gen);
693         free(label);
694         RUNNER_ASSERT_MSG(false, "EXEC label on " << fpath << " is set.");
695     }
696
697     /* TRANSMUTE */
698     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
699     if (result != 0) {
700         free(label_gen);
701         free(label);
702         RUNNER_ASSERT_MSG(false, "Could not get label for the path. Result: " << result);
703     }
704     if (S_ISDIR(sb->st_mode)) {
705         if (label == NULL) {
706             free(label_gen);
707             free(label);
708             RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set");
709         }
710         result = strcmp("TRUE", label);
711         if (result != 0) {
712             free(label_gen);
713             free(label);
714             RUNNER_ASSERT_MSG(false,
715                     "TRANSMUTE label on " << fpath << " is not set to TRUE Result: " << result);
716         }
717     } else if (label != NULL) {
718         free(label_gen);
719         free(label);
720         RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is set");
721     }
722
723     free(label);
724
725     fs_db.open(labels_db_path, std::ios_base::in);
726     if (!(fs_db.good())) {
727         free(label_gen);
728         RUNNER_ASSERT_MSG(false, "Can not open database for apps");
729     }
730
731     while(!fs_db.eof()) {
732         fs_db.getline(label_temp, 255);
733         result = smack_have_access(label_temp, label_gen, access);
734         if (result != -1) {  //expect error, not access granted
735             free(label_gen);
736             RUNNER_ASSERT_MSG(false, "smack_have_access should fail. Result: " << result);
737         }
738     }
739
740     fs_db.close();
741
742     fs_db.open(dir_db_path, std::ios_base::in);
743     if (!fs_db.good()) {
744         free(label_gen);
745         RUNNER_ASSERT_MSG(false, "Can not open database for dirs");
746     }
747
748     bool is_dir = false;
749     while(!fs_db.eof()) {
750         fs_db.getline(label_temp, 255);
751         if (strcmp(label_gen, label_temp) == 0) {
752             is_dir = true;
753             break;
754         }
755     }
756
757     free(label_gen);
758
759     RUNNER_ASSERT_MSG(is_dir, "Error autogenerated label is not in dirs db.");
760
761     return 0;
762 }
763
764 RUNNER_TEST_NOSMACK(privilege_control17_appsettings_privilege_nosmack)
765 {
766     test_appsettings_privilege(false);
767 }
768
769 /**
770  * NOSMACK version of privilege_control18 test.
771  *
772  * Uses NOSMACK version of nftw_check_labels_app_public_dir.
773  */
774 RUNNER_TEST_NOSMACK(privilege_control18_app_setup_path_public_nosmack)
775 {
776     int result;
777
778     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
779     RUNNER_ASSERT_MSG(result == 0,
780             "Unable to clean up Smack labels in " << TEST_APP_DIR << ". Result: " << result);
781
782     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
783     RUNNER_ASSERT_MSG(result == 0,
784             "Unable to clean up Smack labels in " << TEST_NON_APP_DIR << ". Result: " << result);
785
786     DB_BEGIN
787
788     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_PUBLIC_RO);
789     RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed. Result: " << result);
790
791     DB_END
792
793     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
794     RUNNER_ASSERT_MSG(result == 0,
795             "Unable to check Smack labels for non-app dir. Result: " << result);
796
797 }
798
799 /**
800  * NOSMACK version of privilege_control19 test.
801  *
802  * Uses NOSMACK version of nftw_check_labels_app_settings_dir.
803  */
804 RUNNER_TEST_NOSMACK(privilege_control19_app_setup_path_settings_nosmack)
805 {
806     int result;
807
808     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
809     RUNNER_ASSERT_MSG(result == 0,
810             "Unable to clean up Smack labels in " << TEST_APP_DIR << ". Result: " << result);
811
812     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
813     RUNNER_ASSERT_MSG(result == 0,
814             "Unable to clean up Smack labels in " << TEST_NON_APP_DIR << ". Result: " << result);
815
816     DB_BEGIN
817
818     result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_SETTINGS_RW);
819     RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed. Result: " << result);
820
821     DB_END
822
823     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
824     RUNNER_ASSERT_MSG(result == 0,
825             "Unable to check Smack labels for non-app dir. Result: " << result);
826
827 }
828
829 /**
830  * NOSMACK version of privilege_control20 test.
831  *
832  * Uses NOSMACK version of test_have_nosmack_accesses.
833  */
834 RUNNER_TEST_NOSMACK(privilege_control20_app_setup_path_npruntime_nosmack)
835 {
836     int result = 0;
837     CStringPtr labelPtr;
838     std::string nptargetlabel = std::string(APP_NPRUNTIME) + ".npruntime";
839     char *label = NULL;
840
841     DB_BEGIN
842
843     result = perm_app_uninstall(APP_NPRUNTIME);
844     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_uninstall. " << result);
845
846     result = perm_app_install(APP_NPRUNTIME);
847     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_install. " << result);
848
849     result = perm_app_setup_path(APP_NPRUNTIME, APP_NPRUNTIME_FILE, PERM_APP_PATH_NPRUNTIME);
850     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path. " << result);
851
852     DB_END
853
854     RUNNER_ASSERT(0 == smack_lgetlabel(APP_NPRUNTIME_FILE, &label, SMACK_LABEL_EXEC));
855     labelPtr.reset(label);
856     label = NULL;
857     RUNNER_ASSERT(0 == strcmp(labelPtr.get(), nptargetlabel.c_str()));
858
859     // Rules to test
860     const std::vector< std::vector<std::string> > np_rules = {
861         { APP_NPRUNTIME,   nptargetlabel,       "rw"    },
862         { nptargetlabel,   APP_NPRUNTIME,       "rxat"  },
863         { nptargetlabel,   "system::homedir",   "rxat"  },
864         { nptargetlabel,   "xorg",              "rw"    },
865         { nptargetlabel,   "crash-worker",      "rwxa"  },
866         { nptargetlabel,   "sys-assert::core",  "rwxat" },
867         { nptargetlabel,   "syslogd",           "rw"    },
868     };
869
870     // Check if accesses aren't added
871     result = test_have_nosmack_accesses(np_rules);
872     RUNNER_ASSERT_MSG(result == -1, "Accesses shouldn't be added. Result: " << result);
873
874     DB_BEGIN
875
876     // Uninstall app runtime
877     result = perm_app_uninstall(APP_NPRUNTIME);
878     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_uninstall. " << result);
879
880     DB_END
881 }
882
883 /**
884  * NOSMACK version of privielge_control21b test.
885  *
886  * Instead of error caused by incorrect params expect access granted, becuase SMACK is off.
887  */
888 RUNNER_TEST_NOSMACK(privilege_control21b_incorrect_params_smack_pid_have_access_nosmack)
889 {
890     int result = smack_pid_have_access(PID_CORRECT, "some_object", NULL);
891     RUNNER_ASSERT_MSG(result == 1,
892             "smack_pid_have_access should return access granted. Result: " << result);
893
894     result = smack_pid_have_access(PID_CORRECT, NULL, "rw");
895     RUNNER_ASSERT_MSG(result == 1,
896             "smack_pid_have_access should return access granted. Result: " << result);
897
898     result = smack_pid_have_access(PID_CORRECT, NULL, "rw");
899     RUNNER_ASSERT_MSG(result == 1,
900             "smack_pid_have_access should return access granted. Result: " << result);
901
902     result = smack_pid_have_access(PID_INCORRECT, "some_object", "rw");
903     RUNNER_ASSERT_MSG(result == 1,
904             "smack_pid_have_access should return access granted. Result: " << result);
905 }
906
907