Add test privilege_control05_add_shared_dir_readers
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases.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 runer
23  */
24
25 #include <string>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <vector>
30 #include <errno.h>
31 #include <memory>
32 #include <ftw.h>
33 #include <dpl/test/test_runner.h>
34 #include <dpl/test/test_runner_child.h>
35 #include <dpl/log/log.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/mman.h>
39 #include <sys/xattr.h>
40 #include <sys/smack.h>
41 #include <privilege-control.h>
42 #include <fstream>
43
44
45 #define SMACK_RULES_DIR  "/etc/smack/accesses.d/"
46 #define SMACK_LOAD2 "/smack/load2"
47 #define TEST_APP_DIR "/etc/smack/test_privilege_control_DIR/app_dir"
48 #define TEST_NON_APP_DIR "/etc/smack/test_privilege_control_DIR/non_app_dir"
49 #define APPID_DIR  "test_APP_ID_dir"
50 #define APPID_SHARED_DIR  "test_APP_ID_shared_dir"
51 #define CANARY_LABEL "tiny_yellow_canary"
52
53 #define APP_ID  "test_APP"
54 #define APP_SET_PRIV_PATH "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
55 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
56
57 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
58
59 #define LIBPRIVILEGE_APP_GROUP_LIST "/usr/share/privilege-control/app_group_list"
60 #define LIBPRIVILEGE_TEST_DAC_FILE "/usr/share/privilege-control/test_privilege_control_rules.dac"
61
62 #define APP_TEST_APP_1 "test-application1"
63 #define APP_TEST_APP_2 "test-application_2"
64 #define APP_TEST_APP_3 "test-app-3"
65 #define APP_TEST_AV_1 "test-antivirus1"
66 #define APP_TEST_AV_2 "test-antivirus_2"
67 #define APP_TEST_AV_3 "test-av-3"
68 #define SMACK_APPS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_apps_id.db"
69 #define SMACK_AVS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_avs_id.db"
70
71 #define APP_GID 5000
72 #define APP_UID 5000
73 #define APP_USER_NAME "app"
74 #define APP_HOME_DIR "/opt/home/app"
75
76 // How many open file descriptors should ftw() function use?
77 #define FTW_MAX_FDS 16
78
79 // Rules from test_privilege_control_rules.smack
80 const std::vector< std::vector<std::string> > rules = {
81         { APP_ID, "test_book_1", "r" },
82         { APP_ID, "test_book_2", "w" },
83         { APP_ID, "test_book_3", "x" },
84         { APP_ID, "test_book_4", "rw" },
85         { APP_ID, "test_book_5", "rx" },
86         { APP_ID, "test_book_6", "wx" },
87         { APP_ID, "test_book_7", "rwx" },
88         { "test_subject_1", APP_ID, "r" },
89         { "test_subject_2", APP_ID, "w" },
90         { "test_subject_3", APP_ID, "x" },
91         { "test_subject_4", APP_ID, "rw" },
92         { "test_subject_5", APP_ID, "rx" },
93         { "test_subject_6", APP_ID, "wx" },
94         { "test_subject_7", APP_ID, "rwx" },
95         { APP_ID, APPID_SHARED_DIR, "rwxat"}};
96
97
98 namespace {
99
100 const char* OSP_BLAHBLAH = "/usr/share/privilege-control/OSP_blahblah.smack";
101 const char* WRT_BLAHBLAH = "/usr/share/privilege-control/WGT_blahblah.smack";
102 const char* OTHER_BLAHBLAH = "/usr/share/privilege-control/blahblah.smack";
103 const char* BLAHBLAH_FEATURE = "http://feature/blah/blahblah";
104
105 /**
106  * Check if every rule is true.
107  * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't
108  */
109 int test_have_all_accesses(const std::vector< std::vector<std::string> >& rules){
110     int result;
111     for(uint i =0; i<rules.size();++i ){
112         result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
113         if (result !=1)
114             return result;
115     }
116     return 1;
117 }
118
119 /**
120  * Check if every rule is true.
121  * @return 1 if ANY rule in SMACK, 0 if
122  */
123 int test_have_any_accesses(const std::vector< std::vector<std::string> >& rules){
124     int result;
125     for(uint i =0; i<rules.size();++i ){
126         result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
127         if (result ==1)
128             return 1;
129     }
130     return 0;
131 }
132
133 RUNNER_TEST_GROUP_INIT(libprivilegecontrol)
134
135 int nftw_remove_labels(const char *fpath, const struct stat *sb,
136                                 int typeflag, struct FTW *ftwbuf)
137 {
138         smack_lsetlabel(fpath, NULL, SMACK_LABEL_ACCESS);
139         smack_lsetlabel(fpath, NULL, SMACK_LABEL_EXEC);
140         smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
141
142         return 0;
143 }
144
145 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat *sb,
146                                 int typeflag, struct FTW *ftwbuf)
147 {
148         smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_ACCESS);
149         smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_EXEC);
150         smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
151
152         return 0;
153 }
154
155 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat *sb,
156                                 int typeflag, struct FTW *ftwbuf)
157 {
158     int result;
159     char* label;
160
161     /* ACCESS */
162     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
163     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
164     result = strcmp(CANARY_LABEL, label);
165     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is overwritten");
166
167     /* EXEC */
168     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
169     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
170     result = strcmp(CANARY_LABEL, label);
171     RUNNER_ASSERT_MSG(result == 0, "EXEC label on " << fpath << " is overwritten");
172
173     /* TRANSMUTE */
174     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
175     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
176     RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
177
178     return 0;
179 }
180
181 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
182                                 int typeflag, struct FTW *ftwbuf)
183 {
184     int result;
185     char* label;
186
187     /* ACCESS */
188     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
189     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
190     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
191     result = strcmp(APPID_DIR, label);
192     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
193
194     /* EXEC */
195     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
196     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
197     if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR)) {
198         RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
199         result = strcmp(APPID_DIR, label);
200         RUNNER_ASSERT_MSG(result == 0, "EXEC label on executable file " << fpath << " is incorrect");
201     } else
202         RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
203
204     /* TRANSMUTE */
205     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
206     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
207     RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
208
209     return 0;
210 }
211
212 int nftw_check_labels_app_shared_dir(const char *fpath, const struct stat *sb,
213                                 int typeflag, struct FTW *ftwbuf)
214 {
215     int result;
216     char* label;
217
218     /* ACCESS */
219     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
220     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
221     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
222     result = strcmp(APPID_SHARED_DIR, label);
223     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
224
225     /* EXEC */
226     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
227     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
228     RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
229
230     /* TRANSMUTE */
231     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
232     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
233     if (S_ISDIR(sb->st_mode)) {
234         RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
235         result = strcmp("TRUE", label);
236         RUNNER_ASSERT_MSG(result == 0, "TRANSMUTE label on " << fpath << " is not set");
237     } else
238         RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
239
240     return 0;
241 }
242
243 int file_exists(const char* path)
244 {
245     FILE* file = fopen(path, "r");
246     if (file) {
247         fclose(file);
248         return 0;
249     }
250     return -1;
251 }
252
253 void osp_blahblah_check(int line_no, const std::vector<std::string>& rules)
254 {
255     std::ifstream smack_file(OSP_BLAHBLAH);
256     RUNNER_ASSERT_MSG(smack_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH);
257
258     auto it = rules.begin();
259     std::string line;
260     while(std::getline(smack_file,line)) {
261         RUNNER_ASSERT_MSG(it != rules.end(), "Line: " << line_no << "Additional line in file: " << line);
262         RUNNER_ASSERT_MSG(*it == line, "Line: " << line_no << " " << *it << "!=" << line);
263         it++;
264     }
265
266     RUNNER_ASSERT_MSG(it == rules.end(), "Line: " << line_no << " Missing line in file: " << *it);
267
268     smack_file.close();
269 }
270
271 void remove_smack_files()
272 {
273     unlink(OSP_BLAHBLAH);
274     unlink(WRT_BLAHBLAH);
275     unlink(OTHER_BLAHBLAH);
276 }
277
278 int smack_file_name(const char* app_id, char** path)
279 {
280     if (asprintf(path, SMACK_RULES_DIR "/%s", app_id) == -1) {
281         RUNNER_ASSERT_MSG(false, "asprint failed");
282         *path = NULL;
283     }
284
285     return 0;
286 }
287
288 int cleaning_smack_app_files (void)
289 {
290     char *path = NULL;
291     int fd = -1;
292
293     smack_file_name(APP_TEST_APP_1, &path);
294     unlink(path);
295     free(path);
296
297     smack_file_name(APP_TEST_APP_2, &path);
298     unlink(path);
299     free(path);
300
301     smack_file_name(APP_TEST_APP_3, &path);
302     unlink(path);
303     free(path);
304
305     smack_file_name(APP_TEST_AV_1, &path);
306     unlink(path);
307     free(path);
308
309     smack_file_name(APP_TEST_AV_2, &path);
310     unlink(path);
311     free(path);
312
313     smack_file_name(APP_TEST_AV_3, &path);
314     unlink(path);
315     free(path);
316
317     return 0;
318 }
319
320 int cleaning_smack_database_files (void)
321 {
322     int fd = -1;
323
324     //clean app database
325     unlink(SMACK_APPS_LABELS_DATABASE);
326     fd = open(SMACK_APPS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
327     if (fd == -1) {
328         return -1;
329     }
330
331     //clean av database
332     unlink(SMACK_AVS_LABELS_DATABASE);
333     fd = open(SMACK_AVS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
334     if (fd == -1) {
335         return -1;
336     }
337
338     return 0;
339 }
340 } // namespace
341
342 /**
343  * Test setting labels for all files and folders in given path.
344  */
345 RUNNER_TEST(privilege_control02_app_label_dir)
346 {
347     int result;
348
349     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
350     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
351
352     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
353     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
354
355     result = app_label_dir(APPID_DIR, TEST_APP_DIR);
356     RUNNER_ASSERT_MSG(result == 0, "app_label_dir() failed");
357
358     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_dir, FTW_MAX_FDS, FTW_PHYS);
359     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for app dir");
360
361     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
362     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
363 }
364
365 RUNNER_TEST(privilege_control03_app_label_shared_dir)
366 {
367     int result;
368
369     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
370     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
371
372     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
373     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
374
375     result = app_label_shared_dir(APP_ID, APPID_SHARED_DIR, TEST_APP_DIR);
376     RUNNER_ASSERT_MSG(result == 0, "app_label_shared_dir() failed");
377
378     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
379     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
380
381     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
382     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
383 }
384
385
386 /**
387  * Add permisions from  test_privilege_control_rules template
388  */
389 RUNNER_TEST(privilege_control04_add_permissions)
390 {
391     int result = app_add_permissions(APP_ID, PRIVS);
392     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
393             " Error adding app permissions. Errno: " << result);
394
395     // Check if the accesses are realy applied..
396     result = test_have_all_accesses(rules);
397     RUNNER_ASSERT_MSG(result==1, "Permissions not added.");
398
399     //// File exists?
400     FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
401     RUNNER_ASSERT_MSG(pFile != NULL,
402             "SMACK file NOT created!. Errno: " << errno);
403
404     //// Is it empty?
405     fseek(pFile, 0L, SEEK_END);
406     int smack_file_length = ftell(pFile);
407     RUNNER_ASSERT_MSG(smack_file_length>0,
408             "SMACK file empty, but privileges list was not empty.. Errno: " << errno);
409
410     if (pFile != NULL)
411         fclose(pFile);
412
413 }
414
415 /**
416  * Revoke permissions from the list. Should be executed as privileged user.
417  */
418 RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
419 {
420     int result;
421     char* path = NULL;
422     int fd;
423
424     // Revoke permissions
425     result = app_revoke_permissions(APP_ID);
426     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
427             "Error revoking app permissions. Errno: " << result);
428
429     // Are all the permissions revoked?
430     result = test_have_all_accesses(rules);
431     RUNNER_ASSERT_MSG(result!=1, "Not all permisions revoked.");
432
433     smack_file_name(APP_ID, &path);
434     fd = open(path, O_RDONLY);
435     RUNNER_ASSERT_MSG(fd >= 0, "SMACK file deleted after app_revoke_permissions");
436     RUNNER_ASSERT_MSG(lseek(fd, 0, SEEK_END) == 0, "SMACK file not empty after app_revoke_permissions");
437     free(path);
438     close(fd);
439 }
440
441 static void read_gids(std::set<unsigned> &set, const char* file_path)
442 {
443         FILE *f = fopen(file_path, "r");
444         RUNNER_ASSERT_MSG(f != NULL, "Unable to open file " << file_path);
445         unsigned gid;
446         while (fscanf(f, "%u\n", &gid) == 1) {
447                 set.insert(gid);
448         }
449 }
450
451 RUNNER_TEST(privilege_control05_add_shared_dir_readers)
452 {
453
454 #define  TEST_OBJ "TEST_OBJECT"
455 #define  TEST_OBJ_SOME_OTHER "TEST_OBJA"
456 #define test_string_01 "TEST_raz TEST_OBJECT r-x-- -----"
457 #define test_string_21 "TEST_trzy TEST_OBJA -wx--\n"
458 #define test_string_22 "TEST_trzy TEST_OBJECT r-x-- -----\n"
459
460     int result;
461     int i;
462     int fd = -1;
463     char *path;
464     const char *app_labels[] = {"TEST_raz", "TEST_dwa", "TEST_trzy", ""};
465     const int READ_BUF_SIZE = 1000;
466     char buf[READ_BUF_SIZE];
467     FILE *file = NULL;
468     struct smack_accesses * rules = NULL;
469
470     result = smack_accesses_new(&rules);
471     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new. Error: " << result);
472
473     for (i = 0; i < 3; i++) {
474
475         (void)app_uninstall(app_labels[i]);
476         result = app_install(app_labels[i]);
477         RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in app_install.");
478
479         asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[i]);
480         fd = open(path, O_WRONLY, 0644);
481         RUNNER_ASSERT_MSG(fd != -1, "Error in opening file " << path);
482
483         if (i == 1) {
484                 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ,"wt");
485                 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
486         }
487         if (i == 2) {
488                 smack_accesses_free(rules);
489                 result = smack_accesses_new(&rules);
490                 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ_SOME_OTHER,"wx");
491                 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
492         }
493         result = smack_accesses_apply(rules);
494         RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
495
496         result = smack_accesses_save(rules, fd);
497         RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
498
499         free(path);
500         close(fd);
501
502     }
503
504     smack_accesses_free(rules);
505
506     // THE TEST - accesses
507
508     result = add_shared_dir_readers(TEST_OBJ,app_labels);
509     RUNNER_ASSERT_MSG(result == 0, "add_shared_dir_readers failed");
510
511     result = smack_have_access(app_labels[0],TEST_OBJ,"rx");
512     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
513
514     result = smack_have_access(app_labels[1],TEST_OBJ,"rx");
515     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
516
517     result = smack_have_access(app_labels[2],TEST_OBJ,"rx");
518     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
519
520     result = smack_have_access(app_labels[1],TEST_OBJ,"rwxt");
521     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
522
523     result = smack_have_access(app_labels[2],TEST_OBJ_SOME_OTHER,"wx");
524     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
525
526
527     //TEST the operations on empty files
528
529     asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[0]);
530     file = fopen(path, "r");
531
532     RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
533
534     fgets(buf, READ_BUF_SIZE, file);
535     result = strcmp(buf, test_string_01);
536     RUNNER_ASSERT_MSG( result!=0, "add_shared_dir_readers ERROR, file not formatted" << path );
537
538     free(path);
539     fclose(file);
540
541     //TEST the operations on non empty files
542     asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[2]);
543     file = NULL;
544     file = fopen(path, "r");
545     RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
546
547     fgets(buf, READ_BUF_SIZE, file);
548     result = strcmp(buf, test_string_21);
549     RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" );
550
551     fgets(buf, READ_BUF_SIZE, file);
552     result = strcmp(buf, test_string_22);
553     RUNNER_ASSERT_MSG( result==0, "add_shared_dir_readers ERROR, file not formatted" );
554
555     free(path);
556     fclose(file);
557 }
558
559
560 /**
561  * Set APP privileges.
562  */
563 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
564 {
565     int result;
566
567     // Preset exec label
568     smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
569     smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
570
571     // Set APP privileges
572     result = set_app_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
573     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
574
575     // Check if SMACK label really set
576     char * label;
577     result = smack_new_label_from_self(&label);
578     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
579     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
580     result = strcmp(APP_ID, label);
581     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
582
583     // Check if DAC privileges really set
584     RUNNER_ASSERT_MSG(getuid() == APP_UID, "Wrong UID");
585     RUNNER_ASSERT_MSG(getgid() == APP_GID, "Wrong GID");
586
587     result = strcmp(getenv("HOME"), APP_HOME_DIR);
588     RUNNER_ASSERT_MSG(result == 0, "Wrong HOME DIR");
589
590     result = strcmp(getenv("USER"), APP_USER_NAME);
591     RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME");
592
593     std::set<unsigned> groups_check;
594     read_gids(groups_check, LIBPRIVILEGE_APP_GROUP_LIST);
595     read_gids(groups_check, LIBPRIVILEGE_TEST_DAC_FILE);
596
597     int groups_cnt = getgroups(0, NULL);
598     RUNNER_ASSERT_MSG(groups_cnt > 0, "Wrong number of supplementary groupsCnt");
599     gid_t *groups_list = (gid_t *) calloc(groups_cnt, sizeof(gid_t));
600     RUNNER_ASSERT_MSG(groups_list != NULL, "Memory allocation failed");
601     getgroups(groups_cnt, groups_list);
602
603     for (int i = 0; i < groups_cnt; ++i) {
604         if (groups_check.erase(groups_list[i]) == 0) {
605             // getgroups() may also return process' main group
606             if (groups_list[i] == getgid())
607                 RUNNER_ASSERT_MSG(false, "Application belongs to unknown group (GID=" << groups_list[i] << ")");
608         }
609     }
610     std::string groups_left;
611     for (std::set<unsigned>::iterator it = groups_check.begin(); it != groups_check.end(); it++) {
612         groups_left.append(std::to_string(*it)).append(" ");
613     }
614     RUNNER_ASSERT_MSG(groups_check.empty(), "Application doesn't belong to some required groups: " << groups_left);
615 }
616
617 RUNNER_TEST(privilege_control08_app_give_access)
618 {
619     const char *subject = "lkjq345v34sfa";
620     const char *object = "lk9290f92lkjz";
621     smack_accesses *tmp = NULL;
622
623     RUNNER_ASSERT(0 == smack_accesses_new(&tmp));
624
625     std::unique_ptr<smack_accesses,std::function<void(smack_accesses*)>>
626         smack(tmp, smack_accesses_free);
627
628     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "r--a-"));
629     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
630
631     app_give_access(subject, object, "wt");
632
633     RUNNER_ASSERT(1 == smack_have_access(subject, object, "rwat"));
634     RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
635
636     app_revoke_access(subject, object);
637
638     RUNNER_ASSERT(1 == smack_have_access(subject, object, "ra"));
639     RUNNER_ASSERT(0 == smack_have_access(subject, object, "w"));
640     RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
641     RUNNER_ASSERT(0 == smack_have_access(subject, object, "t"));
642
643     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "-"));
644     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
645 }
646
647 /**
648  * Add new API feature
649  */
650 RUNNER_TEST(privilege_control09_add_api_feature)
651 {
652     int result;
653
654     remove_smack_files();
655
656
657     // argument validation
658     result = add_api_feature(APP_TYPE_OSP, NULL, NULL, NULL);
659     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
660
661     result = add_api_feature(APP_TYPE_OSP,"" , NULL, NULL);
662     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
663
664
665     // already existing features
666     result = add_api_feature(APP_TYPE_OSP,"messaging" , NULL, NULL);
667     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
668
669     result = add_api_feature(APP_TYPE_OSP,"blahblah/messaging" , NULL, NULL);
670     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
671
672     result = add_api_feature(APP_TYPE_WGT,"blahblahblah/messaging" , NULL, NULL);
673     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
674
675     result = add_api_feature(APP_TYPE_OTHER,"blah/messaging" , NULL, NULL);
676     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
677
678
679     // empty features
680     result = add_api_feature(APP_TYPE_OSP,"blahblah" , NULL, NULL);
681     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
682
683     result = add_api_feature(APP_TYPE_WGT,"blahblah" , NULL, NULL);
684     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
685
686     result = add_api_feature(APP_TYPE_OTHER,"blahblah" , NULL, NULL);
687     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
688
689
690     // smack files existence
691     result = file_exists(OSP_BLAHBLAH);
692     RUNNER_ASSERT(result == -1);
693
694     result = file_exists(WRT_BLAHBLAH);
695     RUNNER_ASSERT(result == -1);
696
697     result = file_exists(OTHER_BLAHBLAH);
698     RUNNER_ASSERT(result == -1);
699
700
701     // empty rules
702     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , { NULL }, NULL);
703     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
704     result = file_exists(OSP_BLAHBLAH);
705     RUNNER_ASSERT(result == -1);
706
707     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "", NULL }, NULL);
708     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
709     result = file_exists(OSP_BLAHBLAH);
710     RUNNER_ASSERT(result == 0);
711     remove_smack_files();
712
713     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ " \t\n", "\t \n", "\n\t  ", NULL }, NULL);
714     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
715     result = file_exists(OSP_BLAHBLAH);
716     RUNNER_ASSERT(result == 0);
717     remove_smack_files();
718
719
720     // malformed rules
721     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed", NULL }, NULL);
722     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
723     result = file_exists(OSP_BLAHBLAH);
724     RUNNER_ASSERT(result == -1);
725
726     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed malformed", NULL }, NULL);
727     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
728     result = file_exists(OSP_BLAHBLAH);
729     RUNNER_ASSERT(result == -1);
730
731     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "-malformed malformed rwxat", NULL }, NULL);
732     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
733     result = file_exists(OSP_BLAHBLAH);
734     RUNNER_ASSERT(result == -1);
735
736     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "~/\"\\\ malformed rwxat", NULL }, NULL);
737     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
738     result = file_exists(OSP_BLAHBLAH);
739     RUNNER_ASSERT(result == -1);
740
741     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "subject object rwxat something else", NULL }, NULL);
742     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
743     result = file_exists(OSP_BLAHBLAH);
744     RUNNER_ASSERT(result == -1);
745
746
747     // correct rules
748     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "malformed malformed maaaaaalformed", NULL }, NULL);
749     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
750     osp_blahblah_check(__LINE__, { "malformed malformed r--a-" });
751     remove_smack_files();
752
753     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){ "subject object foo", NULL }, NULL);
754     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
755     osp_blahblah_check(__LINE__, { "subject object -----" });
756     remove_smack_files();
757
758     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
759         "subject    object\t rwxat",
760         " \t \n",
761         "subject2\tobject2 txarw",
762         "",
763         NULL }, NULL);
764     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
765     osp_blahblah_check(__LINE__, { "subject object rwxat", "subject2 object2 rwxat"});
766     remove_smack_files();
767
768     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
769         "Sub::jE,ct object a-RwX",
770         NULL }, NULL);
771     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
772     osp_blahblah_check(__LINE__, { "Sub::jE,ct object rwxa-"});
773     remove_smack_files();
774
775     // TODO For now identical/complementary rules are not merged.
776     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE , (const char*[]){
777         "subject object rwxat",
778         " \t \n",
779         "subject object txarw",
780         "",
781         NULL }, NULL);
782     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
783     osp_blahblah_check(__LINE__, { "subject object rwxat", "subject object rwxat"});
784     remove_smack_files();
785
786
787     // TODO database group ids
788 }
789
790 /*
791  * Check app_install function
792  */
793 RUNNER_TEST(privilege_control01_app_install)
794 {
795     int result;
796     char *path = NULL;
797     int fd = -1;
798
799     smack_file_name(APP_ID, &path);
800     unlink(path);
801
802     result = app_install(APP_ID);
803     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
804
805     // checking if file really exists
806     fd = open(path, O_RDONLY);
807     RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << path << " : " << result << ". Errno: " << strerror(errno));
808     close(fd);
809     free(path);
810
811     // try install second time app with the same ID - it should failed with -1 (Errno: File exists).
812     result = app_install(APP_ID);
813     RUNNER_ASSERT_MSG(result == -1, "app_install returned " << result <<". Errno: " << strerror(errno));
814 }
815
816 /*
817  * Check app_install function
818  */
819 RUNNER_TEST(privilege_control07_app_uninstall)
820 {
821     int result;
822     char *path = NULL;
823     int fd = -1;
824
825     smack_file_name(APP_ID, &path);
826     result = app_uninstall(APP_ID);
827     RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result <<". Errno: " << strerror(errno));
828
829     // checking if file really exists
830     smack_file_name(APP_ID, &path);
831     fd = open(path, O_RDONLY);
832     RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after app_uninstall");
833     close(fd);
834     free(path);
835 }
836
837 /*
838  * Check app_register_av function
839  * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
840  */
841 RUNNER_TEST(privilege_control10_app_register_av)
842 {
843     int result;
844     //FILE* file_av = NULL;
845     //FILE* file_app = NULL;
846     //int fd_app = -1;
847     int fd  = -1;
848     char *path = NULL;
849     char *buff;
850     int len;
851     int i;
852     //char label1[SMACK_LABEL_LEN +1];
853     //char label2[SMACK_LABEL_LEN +1];
854     //char acces_rights[6 +1];
855     //char row[2 * SMACK_LABEL_LEN + 20] //
856     const char* correct_antivirus1_rules = "test-antivirus1 test-application1 rwx--\n"
857                                             "test-antivirus1 test-application_2 rwx--\n"
858                                             "test-antivirus1 test-app-3 rwx--";
859     const char* correct_antivirus2_rules = "test-antivirus_2 test-application1 rwx--\n"
860                                            "test-antivirus_2 test-application_2 rwx--\n"
861                                            "test-antivirus_2 test-app-3 rwx--";
862
863     // cleaning
864     cleaning_smack_app_files();
865     cleaning_smack_database_files();
866
867     result = app_install(APP_TEST_APP_1);
868     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
869
870     result = app_install(APP_TEST_APP_2);
871     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
872
873     result = app_register_av(APP_TEST_AV_1);
874     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
875
876     result = app_install(APP_TEST_APP_3);
877     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result <<". Errno: " << strerror(errno));
878
879     result = app_register_av(APP_TEST_AV_2);
880     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result <<". Errno: " << strerror(errno));
881
882     // checking rules for anti virus 1
883     // compare between file in /etc/smack/access.d/ and correct value (correct_antivirus1_rules).
884     len = strlen(correct_antivirus1_rules);
885     buff = (char *) malloc((len+1) * sizeof(char));
886     smack_file_name(APP_TEST_AV_1, &path);
887     fd = open(path, O_RDONLY);
888     free(path);
889     RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
890     result = read(fd, buff, len);
891     close(fd);
892     fd = -1;
893     buff[len] = '\0';
894     RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
895     result = strncmp(buff, correct_antivirus1_rules, len);
896     RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
897     free(buff);
898
899     // checking rules for anti virus 2
900     len = strlen(correct_antivirus2_rules);
901     buff = (char *) malloc((len+1) * sizeof(char));
902     smack_file_name(APP_TEST_AV_2, &path);
903     fd = open(path, O_RDONLY);
904     free (path);
905     RUNNER_ASSERT_MSG(fd > -1, "file open failed " << result <<". Errno: " << strerror(errno));
906     result = read(fd, buff, len);
907     close (fd);
908     fd = -1;
909     buff[len] = '\0';
910     RUNNER_ASSERT_MSG(result > -1, "read from file descriptor failed. Errno: " << strerror(errno));
911     result = strncmp(buff, correct_antivirus2_rules, len);
912     RUNNER_ASSERT_MSG(result == 0, "Rules do not match: " << result << "\n\"" << buff << "\"\n\"" << correct_antivirus1_rules << "\"\n" << len);
913     free(buff);
914
915     // cleaning
916     cleaning_smack_app_files();
917     cleaning_smack_database_files();
918 }