92c54e6c62855e752db15bdfdb386de3a6b4b287
[platform/core/test/security-tests.git] / tests / libsmack-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      Pawel Polawski (p.polawski@samsung.com)
20  * @author      Jan Olszak (j.olszak@samsung.com)
21  * @version     1.0
22  * @brief       libprivilege test runer
23  */
24
25 #include <string>
26 #include <sstream>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/test/test_runner_multiprocess.h>
31 #include <dpl/log/log.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/smack.h>
36 #include <sys/xattr.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <sys/file.h>
40 #include <sys/wait.h>
41 #include "tests_common.h"
42 #include <access_provider.h>
43
44 #define TEST_SUBJECT  "test_subject"
45 #define TEST_OBJECT   "test_oject"
46 #define TEST_OBJECT_2 "test_oject_2"
47
48 #define SOCK_PATH "/tmp/test-smack-socket"
49
50 std::string testDir = "/opt/home/app/";
51 std::vector<std::string> accessesBasic = { "r", "w", "x", "wx", "rx", "rw", "rwx", "rwxat" };
52
53 int files_compare(int fd1, int fd2)
54 {
55     int result = 0;
56
57     //for getting files sizes
58     struct stat fs1, fs2;
59
60     //handlers for mmap()
61     void *h1 = MAP_FAILED;
62     void *h2 = MAP_FAILED;
63
64     //getting files information
65     if (fstat(fd1, &fs1) == -1) {
66         perror("fstat");
67         return -1;
68     }
69     if (fstat(fd2, &fs2) == -1) {
70         perror("fstat");
71         return -1;
72     }
73
74     if (fs1.st_size != fs2.st_size) //if files are identical size will be the same
75         return -1;
76
77     //mapping files to process memory
78     if ((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) == MAP_FAILED) {
79         result = -1;
80         goto end;
81     }
82     if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
83         result = -1;
84         goto end;
85     }
86
87     result = memcmp(h1, h2, fs1.st_size);
88
89     //cleaning after mmap()
90 end:
91     if (h2 != MAP_FAILED)
92         munmap(h2, fs2.st_size);
93     if (h1 != MAP_FAILED)
94         munmap(h1, fs1.st_size);
95
96     return result;
97 }
98
99
100 RUNNER_TEST_GROUP_INIT(libsmack)
101 /**
102  * Helper method to reset privileges at the begginning of tests.
103  */
104 void clean_up()
105 {
106     struct smack_accesses *rules = NULL;
107     int result = smack_accesses_new(&rules);
108     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
109
110     // CLEAN UP
111     smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","rwxat");
112     smack_accesses_apply(rules);
113     smack_accesses_free(rules);
114
115     // PREINIT CHECK.
116     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") != 1, "Rule has previous privileges after cleaning up!");
117     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"w") != 1, "Rule has previous privileges after cleaning up!");
118     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"x") != 1, "Rule has previous privileges after cleaning up!");
119     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"a") != 1, "Rule has previous privileges after cleaning up!");
120     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"t") != 1, "Rule has previous privileges after cleaning up!");
121 }
122
123 /**
124  * Checking if subject has any access to object
125  */
126 bool checkNoAccesses(const char *subject, const char *object)
127 {
128     int result;
129     result = smack_have_access(subject, object,"r");
130     if (result == 1) {
131         return false;
132     }
133     result = smack_have_access(subject, object,"w");
134     if (result == 1) {
135         return false;
136     }
137     result = smack_have_access(subject, object,"x");
138     if (result == 1) {
139         return false;
140     }
141     result = smack_have_access(subject, object,"a");
142     if (result == 1) {
143         return false;
144     }
145     result = smack_have_access(subject, object,"t");
146     if (result == 1) {
147         return false;
148     }
149     return true;
150 }
151
152 void removeAccessesAll()
153 {
154     struct smack_accesses *rules = NULL;
155     int result = smack_accesses_new(&rules);
156     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
157
158     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_01", "", "rxwat");
159     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_02", "", "rxwat");
160     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_03", "", "rxwat");
161     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_01", "", "rxwat");
162     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_02", "", "rxwat");
163     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_03", "", "rxwat");
164     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_01", "", "rxwat");
165     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_02", "", "rxwat");
166     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_03", "", "rxwat");
167
168     smack_accesses_apply(rules);
169     RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
170     smack_accesses_free(rules);
171 }
172
173
174 /**
175  * Add a new access with smack_accesses_add_modify()
176  */
177 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_1){
178     int result;
179
180     clean_up();
181
182     struct smack_accesses *rules = NULL;
183     result = smack_accesses_new(&rules);
184
185     // THE TEST
186     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"xr","");
187     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify by empty rules");
188     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
189
190     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"xr");
191     RUNNER_ASSERT_MSG_BT(result == 1, "Rule modified (added 'xr'), but no change made.");
192
193     // CLEAN UP
194     clean_up();
195     smack_accesses_free(rules);
196 }
197
198
199 /**
200  * Test if rules are applied in the right order, and modification works.
201  */
202 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_2){
203     int result;
204     struct smack_accesses *rules = NULL;
205     result = smack_accesses_new(&rules);
206     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
207
208     clean_up();
209
210     // THE TEST
211     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"r","");
212     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
213
214     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
215     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
216
217     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
218     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") == 0, "Modification didn't work");
219
220     // CLEAN UP
221     clean_up();
222     smack_accesses_free(rules);
223 }
224
225
226 /**
227  * Test if rules are applied in the right order, and modification works.
228  * Using different smack_accesses list to add and delete.
229  */
230 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_3){
231     int result;
232     struct smack_accesses *rules = NULL;
233     result = smack_accesses_new(&rules);
234     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
235
236     clean_up();
237
238     // THE TEST
239     // Add r privilage
240     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"r","");
241     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
242     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
243     RUNNER_ASSERT_MSG_BT(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") == 1, "Adding privileges didn't work");
244     smack_accesses_free(rules);
245
246     // Revoke r privilege
247     result = smack_accesses_new(&rules);
248     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
249     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
250     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
251     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
252
253     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
254     RUNNER_ASSERT_MSG_BT(result == 0, "Modification didn't work, rule has still 'r' privileges.");
255
256     // CLEAN UP
257     clean_up();
258     smack_accesses_free(rules);
259 }
260
261 /**
262  * Add a list of privileges and then revoke just ONE of them.
263  */
264 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_4){
265     int result;
266     struct smack_accesses *rules = NULL;
267     result = smack_accesses_new(&rules);
268     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
269
270     clean_up();
271
272     // THE TEST
273     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwxat","");
274     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
275     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
276
277     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
278     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
279     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
280
281     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"awxt");
282     RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule should have 'awxt' privileges.");
283     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
284     RUNNER_ASSERT_MSG_BT(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
285
286     // CLEAN UP
287     clean_up();
288     smack_accesses_free(rules);
289 }
290
291 /**
292  * Add a list of privileges and then revoke just ONE of them.
293  * Without applying privileges in between those actions.
294  */
295 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_5){
296     int result;
297     struct smack_accesses *rules = NULL;
298     result = smack_accesses_new(&rules);
299     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
300
301     clean_up();
302
303     // THE TEST
304     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwxat","");
305     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
306
307     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
308     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
309     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
310
311     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"awxt");
312     RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule should have 'awxt' privileges.");
313     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
314     RUNNER_ASSERT_MSG_BT(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
315
316     // CLEAN UP
317     clean_up();
318     smack_accesses_free(rules);
319 }
320
321
322 /**
323  * Add a list of privileges and then revoke just TWO of them.
324  */
325 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_6){
326     int result;
327     struct smack_accesses *rules = NULL;
328     result = smack_accesses_new(&rules);
329     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
330
331     clean_up();
332
333     // THE TEST
334     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwt","");
335     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
336     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
337
338     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"ax","rt");
339     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule.");
340     RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
341
342     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"wax");
343     RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule should have 'wax' privileges.");
344     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
345     RUNNER_ASSERT_MSG_BT(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
346
347     // CLEAN UP
348     clean_up();
349     smack_accesses_free(rules);
350 }
351
352 /**
353  * Run smack_accesses_add_modify with the same accesses_add and accesses_del.
354  */
355 RUNNER_TEST_SMACK(smack_accesses_add_modify_test_7){
356     unsigned int i;
357     int result;
358
359     struct smack_accesses *rules = NULL;
360
361     for (i = 0; i < accessesBasic.size(); ++i) {
362         result = smack_accesses_new(&rules);
363         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
364
365         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str(),accessesBasic[i].c_str());
366         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
367         RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
368
369         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
370             " Error while checking smack access. Accesses exist.");
371
372         // CLEAN UP
373         clean_up();
374         smack_accesses_free(rules);
375     }
376 }
377
378 /**
379  * Revoke subject with previously added rules and revoke it again.
380  */
381 RUNNER_TEST_SMACK(smack_revoke_subject_test_1){
382     unsigned int i;
383     int result;
384
385     struct smack_accesses *rules = NULL;
386
387     for (i = 0; i < accessesBasic.size(); ++i) {
388         // Creating and adding rules with TEST_OBJECT and TEST_OBJECT_2
389         result = smack_accesses_new(&rules);
390         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
391         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str(),"");
392         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str(),"");
393         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
394         RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
395         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
396         RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
397         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
398         RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
399
400         // Revoking subject
401         result = smack_revoke_subject(TEST_SUBJECT);
402         RUNNER_ASSERT_MSG_BT(result == 0, "Revoking subject didn't work.");
403
404         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
405             " Revoke didn't work. Accesses exist.");
406         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
407             " Revoke didn't work. Accesses exist.");
408
409
410         // Revoking subject again
411         result = smack_revoke_subject(TEST_SUBJECT);
412         RUNNER_ASSERT_MSG_BT(result == 0, "Revoking subject didn't work.");
413
414         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
415             " Revoke didn't work. Accesses exist.");
416         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
417             " Revoke didn't work. Accesses exist.");
418
419         smack_accesses_free(rules);
420     }
421 }
422
423 /**
424  * Clearing accesses
425  */
426 RUNNER_TEST_SMACK(smack_accesses_clear_test_1){
427     unsigned int i;
428     int result;
429
430     struct smack_accesses *rules = NULL;
431
432     for (i = 0; i < accessesBasic.size(); ++i) {
433         // Creating and adding rules with TEST_OBJECT and TEST_OBJECT_2
434         result = smack_accesses_new(&rules);
435         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
436         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
437         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
438         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
439         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
440         RUNNER_ASSERT_MSG_BT(smack_accesses_apply(rules) == 0, "Unable to apply rules");
441
442         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
443         RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
444         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
445         RUNNER_ASSERT_MSG_BT(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
446
447         smack_accesses_free(rules);
448
449         // Creating and clearing rules with TEST_OBJECT
450         result = smack_accesses_new(&rules);
451         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
452         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
453         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
454         result = smack_accesses_clear(rules);
455         RUNNER_ASSERT_MSG_BT(result == 0, "Clearing rules didn't work.");
456
457         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
458         RUNNER_ASSERT_MSG_BT(result == 0, "Clearing rules didn't work. Rule " << accessesBasic[i].c_str() << " does exist.");
459         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
460         RUNNER_ASSERT_MSG_BT(result == 1, "Clearing rules didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
461
462         smack_accesses_free(rules);
463
464         // Creating and clearing rules with TEST_OBJECT_2
465         result = smack_accesses_new(&rules);
466         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
467
468         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
469         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify accesses instance");
470         result = smack_accesses_clear(rules);
471         RUNNER_ASSERT_MSG_BT(result == 0, "Clearing rules didn't work.");
472
473         smack_accesses_free(rules);
474
475         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
476             " Clear didn't work. Accesses exist.");
477         RUNNER_ASSERT_MSG_BT(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
478             " Clear didn't work. Accesses exist.");
479     }
480 }
481
482 RUNNER_TEST(smack01_storing_and_restoring_rules)
483 {
484     /*
485      * author: Pawel Polawski
486      * test: smack_accesses_new, smack_accesses_add, smack_accesses_add_modify, smack_accesses_add_from_file,
487      *       smack_accesses_free, smack_accesses_save
488      * description: This test case will create structure holding SMACK rules and add new one to it. Next rules will be
489      *              stored and restored from file.
490      * expect: Rules created and stored in file should be identical to predefined template.
491      */
492
493     struct smack_accesses *rules = NULL;        //rules prepared in this test case
494     struct smack_accesses *import_test = NULL;  //rules imported from file
495
496     int result;             //result of each operation to be tested by RUNNER_ASSERT
497     int fd, tmp, sample;    //file descripptors for save / restore rules tests
498
499     //int smack_accesses_new(struct smack_accesses **accesses);
500     result = smack_accesses_new(&rules);        //rules struct init
501     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
502     result = smack_accesses_new(&import_test);  //rules struct init
503     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
504
505     //opening files
506     fd = open("/tmp/smack01_rules", O_RDWR | O_CREAT | O_TRUNC, 0644);  //for export prepared rules
507     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to create /tmp/smack01_rules");
508     tmp = open("/tmp/smack01_tmp", O_RDWR | O_CREAT | O_TRUNC, 0644);   //for import rules exported before
509     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to create /tmp/smack01_tmp");
510     sample = open("/etc/smack/test_smack_rules", O_RDONLY, 0644);             //reference preinstalled rules
511     RUNNER_ASSERT_MSG_BT(sample >= 0, "Unable to open /etc/smack/test_smack_rules");
512
513     //int smack_accesses_add(struct smack_accesses *handle, const char *subject,
514     //               const char *object, const char *access_type);
515     result = smack_accesses_add(rules, "writer", "book", "rw");
516     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
517     result = smack_accesses_add(rules, "reader", "book", "wx");
518     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
519
520     //int smack_accesses_add_modify(struct smack_accesses *handle, const char *subject,
521     //               const char *object, const char *access_add, const char *access_del);
522     result = smack_accesses_add_modify(rules, "reader", "book", "r", "wx");
523     RUNNER_ASSERT_MSG_BT(0 == result, "Unable to modify smack rules");
524
525     //int smack_accesses_save(struct smack_accesses *handle, int fd);
526     result = smack_accesses_save(rules, fd);
527     RUNNER_ASSERT_MSG_BT(0 == result, "Unable to save smack_accesses instance in file");
528
529     //int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd);
530     result = lseek(fd, 0, SEEK_SET);
531     RUNNER_ASSERT_MSG_BT(result == 0, "lseek() error");
532     result = smack_accesses_add_from_file(import_test, fd);
533     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to import rules from file");
534
535     result = smack_accesses_save(import_test, tmp);
536     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to save smack_accesses instance in file");
537
538     result = files_compare(fd, tmp);    //comparing rules saved in file, restored from it and stored one more time
539     RUNNER_ASSERT_MSG_BT(result == 0, "No match in stored and restored rules");
540
541     result = files_compare(tmp, sample);    //comparing rules stored in file with reference preinstalled rules
542     RUNNER_ASSERT_MSG_BT(result == 0, "No match in stored rules and pattern file");
543
544     //void smack_accesses_free(struct smack_accesses *handle);
545     smack_accesses_free(rules);
546     smack_accesses_free(import_test);
547
548     //closing file descriptors
549     close(fd);
550     close(tmp);
551     close(sample);
552 }
553
554 RUNNER_TEST_SMACK(smack02_aplying_rules_into_kernel)
555 {
556     /*
557      * author: Pawel Polawski
558      * test: smack_accesses_apply, smack_have_access, smack_revoke_subject, smack_accesses_clear, smack_accesses_new,
559      *       smack_accesses_add, smack_accesses_free
560      * description: In this test case aplying rules to kernel will be tested. After that function for test
561      *              accesses will be used.
562      * expect: In case of correct rules access should be granted.
563      */
564
565     //CAP_MAC_ADMIN needed for process to be able to change rules in kernel (apllying, removing)
566
567     struct smack_accesses *rules = NULL;        //rules prepared in this test case
568     int result;                                 //for storing functions results
569
570     result = smack_accesses_new(&rules);        //rules struct init
571     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
572
573     //adding test rules to struct
574     result = smack_accesses_add(rules, "writer", "book", "rwx");
575     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
576     result = smack_accesses_add(rules, "reader", "book", "r");
577     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
578     result = smack_accesses_add(rules, "spy", "book", "rwx");
579     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
580
581     //int smack_accesses_apply(struct smack_accesses *handle);
582     result = smack_accesses_apply(rules);       //applying rules to kernel
583     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to apply rules into kernel");
584
585     //int smack_have_access(const char *subject, const char *object,
586     //                  const char *access_type);
587     result = smack_have_access("spy", "book", "rwx");       //should have access - rule exist
588     RUNNER_ASSERT_MSG_BT(result == 1, "Error while checking Smack access");
589     result = smack_have_access("reader", "book", "rwx");    //should have no access - wrong rule, should be "r" only
590     RUNNER_ASSERT_MSG_BT(result == 0, "Error while checking Smack access");
591     result = smack_have_access("s02badsubjectlabel", "book", "rwx"); //should fail - rule not exist
592     RUNNER_ASSERT_MSG_BT(result == -1, "Error while checking Smack access");
593
594     //int smack_revoke_subject(const char *subject);
595     result = smack_revoke_subject("s02nonexistinglabel"); //this subject do not exist in kernel rules
596     RUNNER_ASSERT_MSG_BT(result == 0, "Error in removing not existing subject from kernel");
597     result = smack_revoke_subject("spy");       //this subject exist in kernel rules
598     RUNNER_ASSERT_MSG_BT(result == 0, "Error in removing existing subject from kernel");
599
600     result = smack_have_access("spy", "book", "rwx");                   //testing access after revoke_subject() from kernel
601     RUNNER_ASSERT_MSG_BT(result == 0, "Error in acces aplied to kernel");  //now spy should have no access
602
603     //for create new rule as a consequence of use accesses_clear() below
604     result = smack_accesses_add(rules, "s02subjectlabel", "book", "rwx");
605     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
606
607     //int smack_accesses_clear(struct smack_accesses *handle);
608     result = smack_accesses_clear(rules);    //"spy" removed before by using smack_revoke_subject()
609     RUNNER_ASSERT_MSG_BT(result == 0, "Error in clearing rules in kernel");
610
611     result = smack_have_access("writer", "book", "rwx");                //testing acces after acces_clear()
612     RUNNER_ASSERT_MSG_BT(result == 0, "Error in acces aplied to kernel");  //now writer also should have no access
613
614     //free resources
615     smack_accesses_free(rules);
616 }
617
618 //pairs of rules for test with mixed cases, different length and mixed order
619 const char *rules_tab[] = {
620     "reader1",  "-",                "-----",
621     "reader2",  "--------",         "-----",
622     "reader3",  "RwXaT",            "rwxat",
623     "reader4",  "RrrXXXXTTT",       "r-x-t",
624     "reader5",  "-r-w-a-t",         "rw-at",
625     "reader6",  "",                 "-----",
626     "reader7",  "xa--Rt---W",       "rwxat",
627     "reader8",  "#Ax[T].!~W@1}",    "-wxat"
628 };
629
630 RUNNER_TEST_SMACK(smack03_mixed_rule_string_add)
631 {
632     /*
633      * author: Pawel Polawski
634      * test: smack_have_access, smack_accesses_new, smack_accesses_add, smack_accesses_apply, smack_accesses_free
635      * description: In thist test case rules based on mixed string are added to kernel.
636      *              Strings are presented above and contains lower / upper case alpha, numbers and special signs.
637      * expect: Rules should be parsed correct and aplied to kernel.
638      */
639
640     //In thist test case mixed string are used as rules applied to kernel, next they are
641     //readed and compared with correct form of rules
642
643     struct smack_accesses *rules = NULL;        //rules prepared in this test case
644     int result;                                 //for storing functions results
645     int i;
646     int expected;
647
648     result = smack_accesses_new(&rules);        //rules struct init
649     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
650
651     //adding test rules with mixed string
652     for (i = 0; i < (3 * 8); i += 3) {
653         result = smack_accesses_add(rules, rules_tab[i], "book", rules_tab[i + 1]); //using mixed rules from table
654         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
655     }
656
657     //clearing
658     //FIXME: Using clear() here can cover error in accesses_apply() function
659     //result = smack_accesses_clear(rules);
660     //RUNNER_ASSERT_MSG_BT(result == 0, "Error in clearing rules in kernel");
661
662     //applying rules to kernel
663     result = smack_accesses_apply(rules);
664     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to apply rules into kernel");
665
666     //checking accesses using normal rules
667     for (i = 0; i < (3 * 8); i += 3) {
668         if (!strcmp(rules_tab[i + 2], "-----"))
669             expected = 0;
670         else
671             expected = 1;
672         result = smack_have_access(rules_tab[i], "book", rules_tab[i + 2]); //using normal rules from table
673         RUNNER_ASSERT_MSG_BT(result == expected, "Error while checking Smack access");
674     }
675
676     //free resources
677     smack_accesses_free(rules);
678 }
679
680 RUNNER_TEST_SMACK(smack04_mixed_rule_string_have_access)
681 {
682     /*
683      * author: Pawel Polawski
684      * test: smack_have_access
685      * description: In this test case we testing aplied before SMACK rules and comparing them using mixed strings.
686      * expect: Subjects should have accesses to the objects.
687      */
688
689     //In this test case we checking previous aplied rules but for compare mixed strings are used
690
691     int result;
692     int i;
693     int expected;
694
695     //rules were added in previous RUNNER_TEST section
696     //checking accesses using mixed rules
697     for (i = 0; i < (3 * 8); i += 3) {
698         if (!strcmp(rules_tab[i + 2], "-----"))
699             expected = 0;
700         else
701             expected = 1;
702         result = smack_have_access(rules_tab[i], "book", rules_tab[i + 1]); //using mixed rules from table
703         RUNNER_ASSERT_MSG_BT(result == expected, "Error while checking Smack access");
704     }
705 }
706
707 //RUNNER_TEST(smackXX_accesses_add_modify)
708 //{
709 //IDEAS FOR TESTS
710 // - what if we want to apply rule that is already in kernel?
711 // - tests for smack_accesses_add_modify() + smack_have_access() (check if add_modify sets the proper rule)
712 // - smack_accesses_add_modify("subject", "object", "rwx", "rwx") should create empty rule
713 //}
714
715 RUNNER_TEST_SMACK(smack05_self_label)
716 {
717     /*
718      * author: Pawel Polawski
719      * test: smack_set_label_for_self, smack_new_label_from_self
720      * description: In this test case process test it own default label. Next label is changed
721      *              and tested one more time if change was successfull.
722      * expect: Proces should have default "-" label and can change it to the oter one.
723      */
724
725     //In this test case process will manipulate it own label
726
727     char *label = NULL;
728     int result;
729     int fd;
730
731     const int B_SIZE = 8;
732     char buff[B_SIZE];
733
734     const char *def_rule = "_";
735
736     //int smack_new_label_from_self(char **label);
737     result = smack_new_label_from_self(&label);
738     RUNNER_ASSERT_MSG_BT(result >= 0, "Error in getting self label");
739
740     //comparing this label with default one "_"
741     result = strcmp(label, def_rule);
742     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong default process label");
743
744     //comparing this rule with received from /proc/self/attr/current
745     fd = open("/proc/self/attr/current", O_RDONLY, 0644);
746     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /proc/self/attr/current");
747     result = read(fd, buff, B_SIZE);
748     RUNNER_ASSERT_MSG_BT(result >= 0, "Error in reading from file /proc/self/attr/current");
749     result = strncmp(buff, def_rule, result);
750     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong default process rule");
751
752     free(label);
753
754     //now time for setting labels:
755
756     //int smack_set_label_for_self(const char *label);
757     result = smack_set_label_for_self("cola");
758     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting self label");
759
760     //checking new label using smack function
761     result = smack_new_label_from_self(&label);
762     RUNNER_ASSERT_MSG_BT(result >= 0, "Error in getting self label");
763     result = strcmp(label, "cola");
764     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong process label");
765
766     //checking new label using /proc/self/attr/current
767     result = lseek(fd, 0, SEEK_SET);    //going to the file beginning
768     RUNNER_ASSERT_MSG_BT(result == 0, "lseek() error");
769     result = read(fd, buff, B_SIZE);
770     RUNNER_ASSERT_MSG_BT(result >= 0, "Error in reading from file /proc/self/attr/current");
771     result = strncmp(buff, "cola", result);
772     RUNNER_ASSERT_MSG_BT(result == 0, "Proces rule in /proc/self/attr/current other than set");
773
774     free(label);
775     close(fd);
776 }
777
778 //RUNNER_TEST(smackXX_parent_child_label)
779 //{
780 //In this test case parent process and child labels will be tested
781 //Parent will fork and check child's label. First fork will be with default "_" parent label,
782 //second one witch changed label.
783 //}
784
785 //bellow function is from libsmack.c witch changed name
786 const char *xattr(enum smack_label_type type)
787 {
788     switch (type) {
789         case SMACK_LABEL_ACCESS:
790             return "security.SMACK64";
791         case SMACK_LABEL_EXEC:
792             return "security.SMACK64EXEC";
793         case SMACK_LABEL_MMAP:
794             return "security.SMACK64MMAP";
795         case SMACK_LABEL_TRANSMUTE:
796             return "security.SMACK64TRANSMUTE";
797         case SMACK_LABEL_IPIN:
798             return "security.SMACK64IPIN";
799         case SMACK_LABEL_IPOUT:
800             return "security.SMACK64IPOUT";
801         default:
802             /*  Should not reach this point */
803             return NULL;
804     }
805 }
806
807 //TODO: In bellow RUNNER_TEST add  lget / lset functions to be testet the same way as normal get / set
808 RUNNER_TEST(smack06_get_set_label)
809 {
810     /*
811      * author: Pawel Polawski
812      * test: smack_getlabel, smack_setlabel
813      * description: In this test case file label is tested using SMACK API functions and system xattr functions.
814      *              Functions tested here is used for normal files.
815      * expect: Function should return default label, and the new one after change it.
816      */
817
818     //In this test case will be tested setting and getting file label
819     //If file is symbolic link functions should follow it
820
821     //SMACK xattr from libsmack.c:
822     //
823     //case SMACK_LABEL_ACCESS:
824     //    return "security.SMACK64";
825     //case SMACK_LABEL_EXEC:
826     //    return "security.SMACK64EXEC";
827     //case SMACK_LABEL_MMAP:
828     //    return "security.SMACK64MMAP";
829     //case SMACK_LABEL_TRANSMUTE:
830     //    return "security.SMACK64TRANSMUTE";
831     //case SMACK_LABEL_IPIN:
832     //    return "security.SMACK64IPIN";
833     //case SMACK_LABEL_IPOUT:
834     //    return "security.SMACK64IPOUT";
835
836     int result;
837     char *label = NULL;
838
839     char buff[SMACK_LABEL_LEN+1];
840     const char* s06testlabel = "s06testlabel";
841
842     const char *file_path = "/etc/smack/test_smack_rules";
843
844
845     //preparing environment by restoring default "_" label
846     result = smack_setlabel(file_path, "_", SMACK_LABEL_ACCESS);
847     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
848
849
850     //int smack_getlabel(const char *path, char** label,
851     //          enum smack_label_type type);
852     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
853     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
854     //get label, should be default "_"
855     result = strcmp(label, "_");
856     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
857     free(label);
858     //get label using xattr function
859     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
860     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
861     //check label, should match the one readed by smack function
862     result = strncmp(buff, "_", result);
863     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
864
865
866     //int smack_setlabel(const char *path, const char* label,
867     //          enum smack_label_type type);
868     result = smack_setlabel(file_path, s06testlabel, SMACK_LABEL_ACCESS);
869     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
870
871
872     //get label using smack function
873     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
874     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
875     //get label, should be default s06testlabel
876     result = strcmp(label, s06testlabel);
877     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
878     free(label);
879     //get label using xattr function
880     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
881     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
882     //check label, should match the one readed by smack function
883     result = strncmp(buff, s06testlabel, result);
884     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
885 }
886
887 //RUNNER_TEST(smackXX_get_label_exec)
888 //{
889 //In this test case EXEC label will be tested
890 //by setting this type of label, reading it and testing executed binary exit status
891 //}
892
893 RUNNER_TEST(smack07_l_get_set_label)
894 {
895     /*
896      * author: Pawel Polawski
897      * test: smack_lgetlabel, smack_lsetlabel, smack_getlabel
898      * description: Functions tested here are similar to one from previous test case. The difference
899      *              is that in case of symbolic link they follows it and operates on file pointed by it.
900      * expect: All label manipulations should affect file pointed by symbolic link.
901      */
902
903     int result;
904     char *label = NULL;
905
906     char buff[SMACK_LABEL_LEN+1];
907     const char* s07testlabel1 = "s07testlabel1";
908     const char* s07testlabel2 = "s07testlabel2";
909
910     const char *file_path = "/etc/smack/test_smack_rules_lnk";
911
912
913     //preparing environment by restoring default "_" label
914     result = smack_lsetlabel(file_path, "_", SMACK_LABEL_ACCESS);
915     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
916     result = smack_setlabel(file_path, "_", SMACK_LABEL_ACCESS);
917     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
918
919
920     //int smack_lgetlabel(const char *path, char** label,
921     //          enum smack_label_type type);
922     result = smack_lgetlabel(file_path, &label, SMACK_LABEL_ACCESS);
923     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
924     //get label of symbolic link, should be default "_"
925     result = strcmp(label, "_");
926     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
927     free(label);
928     //get label using xattr function
929     result = lgetxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
930     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
931     //check label, should match the one readed by smack function
932     result = strncmp(buff, "_", result);
933     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
934
935
936     //int smack_lsetlabel(const char *path, const char* label,
937     //          enum smack_label_type type);
938     result = smack_lsetlabel(file_path, s07testlabel1, SMACK_LABEL_ACCESS);
939     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
940     //and set label for file pointed by link
941     result = smack_setlabel(file_path, s07testlabel2, SMACK_LABEL_ACCESS);
942     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
943
944
945     //get label using smack function
946     result = smack_lgetlabel(file_path, &label, SMACK_LABEL_ACCESS);
947     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
948     //check label, should be s07testlabel1
949     result = strcmp(label, s07testlabel1);
950     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
951     free(label);
952     //get label using xattr function
953     result = lgetxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
954     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
955     //check label, should match the one readed by smack function
956     result = strncmp(buff, s07testlabel1, result);
957     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
958
959
960     //now similar to above, but folowing symbolic link set before to s07testlabel2
961     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
962     RUNNER_ASSERT_MSG_BT(result == 0, "Error gettin label of file pointed by symbolic link");
963     //now label should be s07testlabel2 for file instead of s07testlabel1 set for link
964     result = strcmp(label, s07testlabel2);
965     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong label of file pointed by symbolic link");
966     free(label);
967     //get label using xattr function
968     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
969     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
970     //check label, should match the one readed by smack function
971     result = strncmp(buff, s07testlabel2, result);
972     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
973 }
974
975 RUNNER_TEST(smack08_f_get_set_label)
976 {
977     /*
978      * author: Pawel Polawski
979      * test: smack_fgetlabel, smack_fsetlabel
980      * description: This test case is similar to test case smack06 above. The difference
981      *              is that argument is file descriptor instead of file path.
982      *              Function not follow symbolic link and operates directly on it.
983      * expect: All label manipulations should affect symbolic link itself.
984      */
985
986     int result;
987     char *label = NULL;
988
989     char buff[SMACK_LABEL_LEN+1];
990     const char* s08testlabel = "s08testlabel";
991
992     int fd;
993     const char *file_path = "/etc/smack/test_smack_rules";
994
995     fd = open(file_path, O_RDWR, 0644);             //reference preinstalled rules
996     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /etc/smack/test_smack_rules");
997
998     //preparing environment by restoring default "_" label
999     result = smack_fsetlabel(fd, "_", SMACK_LABEL_ACCESS);
1000     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
1001
1002
1003     //int smack_fgetlabel(int fd, char** label,
1004     //          enum smack_label_type type);
1005     result = smack_fgetlabel(fd, &label, SMACK_LABEL_ACCESS);
1006     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
1007     //check label, should be "_"
1008     result = strcmp(label, "_");
1009     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
1010     free(label);
1011     //get label using xattr function
1012     result = fgetxattr(fd, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
1013     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
1014     //check label, should match the one readed by smack function
1015     result = strncmp(buff, "_", result);
1016     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file default label");
1017
1018
1019     //int smack_fsetlabel(int fd, const char* label,
1020     //          enum smack_label_type type);
1021     result = smack_fsetlabel(fd, s08testlabel, SMACK_LABEL_ACCESS);
1022     RUNNER_ASSERT_MSG_BT(result == 0, "Error in setting ACCESS label for file");
1023
1024
1025     //get label using smack function
1026     result = smack_fgetlabel(fd, &label, SMACK_LABEL_ACCESS);
1027     RUNNER_ASSERT_MSG_BT(result == 0, "Error in getting smack ACCESS label from file");
1028     //check label, should be s08testlabel
1029     result = strcmp(label, s08testlabel);
1030     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
1031     free(label);
1032     //get label using xattr function
1033     result = fgetxattr(fd, xattr(SMACK_LABEL_ACCESS), buff, SMACK_LABEL_LEN);
1034     RUNNER_ASSERT_MSG_BT(result > 0, "Error in getting xattr from file");
1035     //check label, should match the one readed by smack function
1036     result = strncmp(buff, s08testlabel, result);
1037     RUNNER_ASSERT_MSG_BT(result == 0, "Wrong file label");
1038
1039
1040     close(fd);
1041 }
1042
1043 RUNNER_TEST_SMACK(smack10_adding_removing_rules)
1044 {
1045     unsigned int i;
1046     int result;
1047
1048     struct smack_accesses *rulesBasic = NULL;
1049
1050     for (i = 0; i < accessesBasic.size(); ++i)
1051     {
1052         // Creating rules
1053         result = smack_accesses_new(&rulesBasic);
1054         RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1055
1056         // Adding accesses
1057         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1058         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1059
1060         // Applying rules
1061         result = smack_accesses_apply(rulesBasic);
1062         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1063
1064         // Checking if accesses were created
1065         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1066         RUNNER_ASSERT_MSG_BT(result == 1,
1067             " Error while checking smack access. Result: " << result);
1068
1069         smack_accesses_free(rulesBasic);
1070         rulesBasic = NULL;
1071
1072         // Deleting all rules
1073         clean_up();
1074     }
1075
1076     for (i = 0; i < 3; ++i)
1077     {
1078         // --- Creating rules (r or w or x)
1079         result = smack_accesses_new(&rulesBasic);
1080         RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1081
1082         // Adding accesses
1083         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1084         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add rulesBasic. Result: " << result);
1085
1086         // Applying rules
1087         result = smack_accesses_apply(rulesBasic);
1088         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1089         // Checking if accesses were created
1090         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1091         RUNNER_ASSERT_MSG_BT(result == 1,
1092             " Error while checking smack access. Result: " << result);
1093
1094         // Checking if wrong accesses were not created
1095         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i + 3].c_str());
1096         RUNNER_ASSERT_MSG_BT(result == 0,
1097             " Error while checking smack access. Result: " << result);
1098
1099         // --- Modifying accesses (r for wx or w for rx or x for rw)
1100         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i + 3].c_str(),accessesBasic[i].c_str());
1101         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1102
1103         // Applying rules
1104         result = smack_accesses_apply(rulesBasic);
1105         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1106
1107         // Checking if accesses were created
1108         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i + 3].c_str());
1109         RUNNER_ASSERT_MSG_BT(result == 1,
1110             " Error while checking smack access. Result: " << result);
1111
1112         // Checking if wrong accesses were not created
1113         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1114         RUNNER_ASSERT_MSG_BT(result == 0,
1115             " Error while checking smack access. Result: " << result);
1116
1117         smack_accesses_free(rulesBasic);
1118         rulesBasic = NULL;
1119
1120         // --- Creating complementary rules (r or w or x)
1121         result = smack_accesses_new(&rulesBasic);
1122         RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1123
1124         // Adding accesses
1125         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1126         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add rulesBasic. Result: " << result);
1127
1128         // Checking if accesses were created
1129         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i + 3].c_str());
1130         RUNNER_ASSERT_MSG_BT(result == 1,
1131             " Error while checking smack access. Result: " << result);
1132
1133         // Applying rules
1134         result = smack_accesses_apply(rulesBasic);
1135         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1136
1137         // Checking if accesses were created
1138         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1139         RUNNER_ASSERT_MSG_BT(result == 1,
1140             " Error while checking smack access. Result: " << result);
1141
1142         // --- Modifying accesses (adding rwx and removing r or w or x)
1143         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,"rwx",accessesBasic[i].c_str());
1144         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1145
1146         // Applying rules
1147         result = smack_accesses_apply(rulesBasic);
1148         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1149
1150         // Checking if accesses were created
1151         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i + 3].c_str());
1152         RUNNER_ASSERT_MSG_BT(result == 1,
1153             " Error while checking smack access. Result: " << result);
1154
1155         // Checking if wrong accesses were not created
1156         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1157         RUNNER_ASSERT_MSG_BT(result == 0,
1158             " Error while checking smack access. Result: " << result);
1159
1160         // --- Adding crossing accesses (rx or rw or wx)
1161         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,accessesBasic[3 + ((i + 1) % 3)].c_str(),"");
1162         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1163
1164         // Applying rules
1165         result = smack_accesses_apply(rulesBasic);
1166         RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1167
1168         // Checking if accesses were created
1169         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[3 + ((i + 1) % 3)].c_str());
1170         RUNNER_ASSERT_MSG_BT(result == 1,
1171             " Error while checking smack access. Result: " << result);
1172
1173         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, "rwx");
1174         RUNNER_ASSERT_MSG_BT(result == 1,
1175             " Error while checking smack access. Result: " << result);
1176
1177         // Deleting all rules
1178         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,"","rwx");
1179         RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1180
1181         result = smack_accesses_apply(rulesBasic);
1182         RUNNER_ASSERT_MSG_BT(result == 0, "Error while checking smack access. Result: " << result);
1183
1184         smack_accesses_free(rulesBasic);
1185         rulesBasic = NULL;
1186
1187         // Deleting all rules
1188         clean_up();
1189     }
1190 }
1191
1192 RUNNER_TEST_SMACK(smack11_saving_loading_rules)
1193 {
1194     int result;
1195     int fd;
1196
1197     struct smack_accesses *rulesBasic = NULL;
1198
1199     // Pre-cleanup
1200     removeAccessesAll();
1201
1202     // Creating rules
1203     result = smack_accesses_new(&rulesBasic);
1204     RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1205
1206     // Loading file with rwxat rules - test_smack_rules_full
1207     fd = open("/etc/smack/test_smack_rules_full", O_RDONLY, 0644);
1208     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /etc/smack/test_smack_rules_full");
1209
1210     // Adding rules from file
1211     result = smack_accesses_add_from_file(rulesBasic, fd);
1212     close(fd);
1213     RUNNER_ASSERT_MSG_BT(result == 0, "Error importing accesses from file");
1214
1215     // Applying rules
1216     result = smack_accesses_apply(rulesBasic);
1217     RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1218
1219     // Checking rules
1220     result = smack_have_access("test_subject_01", "test_object_01", "rwxat");
1221     RUNNER_ASSERT_MSG_BT(result == 1,
1222         " Error while checking smack accesses.");
1223     result = smack_have_access("test_subject_01", "test_object_02", "rwxat");
1224     RUNNER_ASSERT_MSG_BT(result == 1,
1225         " Error while checking smack accesses.");
1226     result = smack_have_access("test_subject_01", "test_object_03", "rwxat");
1227     RUNNER_ASSERT_MSG_BT(result == 1,
1228         " Error while checking smack accesses.");
1229     result = smack_have_access("test_subject_02", "test_object_01", "rwxat");
1230     RUNNER_ASSERT_MSG_BT(result == 1,
1231         " Error while checking smack accesses.");
1232     result = smack_have_access("test_subject_02", "test_object_02", "rwxat");
1233     RUNNER_ASSERT_MSG_BT(result == 1,
1234         " Error while checking smack accesses.");
1235     result = smack_have_access("test_subject_02", "test_object_03", "rwxat");
1236     RUNNER_ASSERT_MSG_BT(result == 1,
1237         " Error while checking smack accesses.");
1238     result = smack_have_access("test_subject_03", "test_object_01", "rwxat");
1239     RUNNER_ASSERT_MSG_BT(result == 1,
1240         " Error while checking smack accesses.");
1241     result = smack_have_access("test_subject_03", "test_object_02", "rwxat");
1242     RUNNER_ASSERT_MSG_BT(result == 1,
1243         " Error while checking smack accesses.");
1244     result = smack_have_access("test_subject_03", "test_object_03", "rwxat");
1245     RUNNER_ASSERT_MSG_BT(result == 1,
1246         " Error while checking smack accesses.");
1247
1248     // Removing rules
1249     removeAccessesAll();
1250
1251     smack_accesses_free(rulesBasic);
1252
1253     // Creating rules
1254     result = smack_accesses_new(&rulesBasic);
1255     RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1256
1257     // Loading file with partial wrong rules - test_smack_rules2
1258     fd = open("/etc/smack/test_smack_rules2", O_RDONLY, 0644);
1259     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /etc/smack/test_smack_rules2");
1260
1261     // Adding rules from file
1262     result = smack_accesses_add_from_file(rulesBasic, fd);
1263     close(fd);
1264     RUNNER_ASSERT_MSG_BT(result == 0, "Accesses were loaded from file");
1265
1266     // Applying rules
1267     result = smack_accesses_apply(rulesBasic);
1268     RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1269
1270     // Checking rules
1271     RUNNER_ASSERT_MSG_BT(checkNoAccesses("test_subject_01", "test_object_01"),
1272         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Accesses exist.");
1273     result = smack_have_access("test_subject_01", "test_object_02", "rwat");
1274     RUNNER_ASSERT_MSG_BT(result == 1,
1275         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1276     result = smack_have_access("test_subject_01", "test_object_03", "wat");
1277     RUNNER_ASSERT_MSG_BT(result == 1,
1278         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1279     RUNNER_ASSERT_MSG_BT(checkNoAccesses("test_subject_02", "test_object_01"),
1280         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Accesses exist.");
1281     result = smack_have_access("test_subject_02", "test_object_02", "wa-ft");
1282     RUNNER_ASSERT_MSG_BT(result == 1,
1283         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1284     result = smack_have_access("test_subject_02", "test_object_03", "wr");
1285     RUNNER_ASSERT_MSG_BT(result == 1,
1286         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1287     result = smack_have_access("test_subject_03", "test_object_01", "a");
1288     RUNNER_ASSERT_MSG_BT(result == 1,
1289         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1290     result = smack_have_access("test_subject_03", "test_object_02", "rwat");
1291     RUNNER_ASSERT_MSG_BT(result == 1,
1292         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1293     result = smack_have_access("test_subject_03", "test_object_03", "w");
1294     RUNNER_ASSERT_MSG_BT(result == 1,
1295         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1296
1297     // Removing rules
1298     removeAccessesAll();
1299
1300     smack_accesses_free(rulesBasic);
1301
1302     // Creating rules
1303     result = smack_accesses_new(&rulesBasic);
1304     RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1305
1306     // Loading file with partial wrong rules - test_smack_rules3
1307     fd = open("/etc/smack/test_smack_rules3", O_RDONLY, 0644);
1308     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /etc/smack/test_smack_rules3");
1309
1310     // Adding rules from file
1311     result = smack_accesses_add_from_file(rulesBasic, fd);
1312     close(fd);
1313     RUNNER_ASSERT_MSG_BT(result != 0, "Accesses were loaded from file");
1314
1315     // Applying rules
1316     result = smack_accesses_apply(rulesBasic);
1317     RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1318
1319     // Checking rules
1320     result = smack_have_access("test_subject_01", "test_object_01", "rwat");
1321     RUNNER_ASSERT_MSG_BT(result == 1,
1322         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Result: " << result );
1323     RUNNER_ASSERT_MSG_BT(checkNoAccesses("test_subject_01", "test_object_02"),
1324         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Accesses exist.");
1325     result = smack_have_access("test_subject_01", "test_object_03", "x");
1326     RUNNER_ASSERT_MSG_BT(result == 0,
1327         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Result: " << result );
1328
1329     // Removing rules
1330     removeAccessesAll();
1331
1332     smack_accesses_free(rulesBasic);
1333
1334     // Creating rules
1335     result = smack_accesses_new(&rulesBasic);
1336     RUNNER_ASSERT_MSG_BT(result == 0, "Error while creating new accesses. Result: " << result);
1337
1338     // Loading file with partial wrong rules - test_smack_rules4
1339     fd = open("/etc/smack/test_smack_rules4", O_RDONLY, 0644);
1340     RUNNER_ASSERT_MSG_BT(fd >= 0, "Unable to open /etc/smack/test_smack_rules4");
1341
1342     // Adding rules from file
1343     result = smack_accesses_add_from_file(rulesBasic, fd);
1344     close(fd);
1345     RUNNER_ASSERT_MSG_BT(result != 0, "Accesses were loaded from file");
1346
1347     // Applying rules
1348     result = smack_accesses_apply(rulesBasic);
1349     RUNNER_ASSERT_MSG_BT(result == 0, "Error while applying accesses. Result: " << result);
1350
1351     // Checking rules
1352     result = smack_have_access("test_subject_01", "test_object_01", "rxwat");
1353     RUNNER_ASSERT_MSG_BT(result == 1,
1354         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Result: " << result );
1355     RUNNER_ASSERT_MSG_BT(checkNoAccesses("test_subject_01", "test_object_02"),
1356         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Accesses exist.");
1357     result = smack_have_access("test_subject_01", "test_object_03", "a");
1358     RUNNER_ASSERT_MSG_BT(result == 0,
1359         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Result: " << result );
1360
1361     // Removing rules
1362     removeAccessesAll();
1363
1364     smack_accesses_free(rulesBasic);
1365 }
1366
1367 //int smack_new_label_from_socket(int fd, char **label);
1368
1369
1370 static void smack_set_another_label_for_self(void)
1371 {
1372     static int number = time(NULL);
1373     int result;
1374     char *smack_label;
1375
1376     number++;
1377     result = asprintf(&smack_label, "s%d", number);
1378     RUNNER_ASSERT_MSG_BT(result > 0, "asprintf failed");
1379     result = smack_set_label_for_self(smack_label);
1380     RUNNER_ASSERT_MSG_BT(result == 0, "smack_set_label_for_self(" << smack_label << ") failed");
1381     free(smack_label);
1382 }
1383
1384 static void smack_unix_sock_server(int sock)
1385 {
1386     int fd, result;
1387     char *smack_label;
1388
1389     alarm(2);
1390     fd = accept(sock, NULL, NULL);
1391     alarm(0);
1392     if (fd < 0)
1393         return;
1394     result = smack_new_label_from_self(&smack_label);
1395     RUNNER_ASSERT_MSG_BT(result >= 0, "smack_new_label_from_self() failed");
1396     result = write(fd, smack_label, strlen(smack_label));
1397     RUNNER_ASSERT_MSG_BT(result == (int)strlen(smack_label), "write() failed");
1398     close(fd);
1399     free(smack_label);
1400 }
1401
1402 RUNNER_MULTIPROCESS_TEST_SMACK(smack09_new_label_from_socket)
1403 {
1404     int pid;
1405     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
1406     unlink(SOCK_PATH);
1407     smack_set_another_label_for_self();
1408     pid = fork();
1409     RUNNER_ASSERT_MSG_BT(pid >= 0, "Fork failed");
1410     if (!pid) { /* child process, server */
1411         int sock, result;
1412
1413
1414         sock = socket(AF_UNIX, SOCK_STREAM, 0);
1415         RUNNER_ASSERT_MSG_BT(sock >= 0, "socket failed: " << strerror(errno));
1416         result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
1417         RUNNER_ASSERT_MSG_BT(result == 0, "bind failed: " << strerror(errno));
1418         result = listen(sock, 1);
1419         RUNNER_ASSERT_MSG_BT(result == 0, "listen failed: " << strerror(errno));
1420         smack_unix_sock_server(sock);
1421
1422         pid = fork();
1423         RUNNER_ASSERT_MSG_BT(pid >= 0, "Fork failed");
1424         /*  Test if socket label was unaffected by fork() */
1425         smack_unix_sock_server(sock);
1426         if (!pid) {
1427             usleep (100);
1428             smack_set_another_label_for_self();
1429             smack_unix_sock_server(sock);
1430         }
1431         close(sock);
1432
1433         exit(0);
1434     } else { /* parent process, client */
1435         sleep(1); /* Give server some time to setup listening socket */
1436         for (int i = 0; i < 4; ++i) {
1437             int sock, result;
1438             char smack_label1[SMACK_LABEL_LEN + 1];
1439             char *smack_label2;
1440
1441             sock = socket(AF_UNIX, SOCK_STREAM, 0);
1442             RUNNER_ASSERT_MSG_BT(sock >= 0, "socket failed: " << strerror(errno));
1443             result = connect(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
1444             RUNNER_ASSERT_MSG_BT(result == 0, "connect failed: " << strerror(errno));
1445             alarm(2);
1446             result = read(sock, smack_label1, SMACK_LABEL_LEN);
1447             RUNNER_ASSERT_MSG_BT(result >= 0, "read failed: " << strerror(errno));
1448             alarm(0);
1449             smack_label1[result] = '\0';
1450             result = smack_new_label_from_socket(sock, &smack_label2);
1451             RUNNER_ASSERT_MSG_BT(result >= 0, "smack_label_from_socket failed");
1452             result = strcmp(smack_label1, smack_label2);
1453             if (i < 3)
1454                 RUNNER_ASSERT_MSG_BT(result == 0, "smack labels differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i);
1455             else
1456                 RUNNER_ASSERT_MSG_BT(result != 0, "smack labels do not differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i);
1457             close(sock);
1458         }
1459     }
1460 }
1461
1462 void createFileWithLabel(const std::string &filePath, const std::string &fileLabel)
1463 {
1464     //create temporary file and set label for it
1465     mode_t systemMask;
1466
1467     unlink(filePath.c_str());
1468     //allow to create file with 777 rights
1469     systemMask = umask(0000);
1470     int fd = open(filePath.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
1471     //restore system mask
1472     umask(systemMask);
1473     RUNNER_ASSERT_MSG_BT(fd > -1, "Unable to create file for tests: " << strerror(errno));
1474
1475     //for descriptor protection
1476     FDUniquePtr fp(&fd, closeFdPtr);
1477
1478     //change owner and group to user APP
1479     int ret = chown(filePath.c_str(), APP_UID, APP_GID);
1480     RUNNER_ASSERT_MSG_BT(ret == 0, "Unable to change file owner: " << strerror(errno));
1481
1482     //set smack label on file
1483     ret = smack_setlabel(filePath.c_str(), fileLabel.c_str(), SMACK_LABEL_ACCESS);
1484     RUNNER_ASSERT_MSG_BT(ret == 0, "Unable to set label for file: " << ret);
1485
1486     char *label = NULL;
1487     ret = smack_getlabel(filePath.c_str(), &label, SMACK_LABEL_ACCESS);
1488     RUNNER_ASSERT_MSG_BT(ret == 0, "Unable to get label from file");
1489     std::string label_str(label ? label : "");
1490     free(label);
1491     RUNNER_ASSERT_MSG_BT(label_str == fileLabel, "File label not match set label");
1492 }
1493
1494 void prepareEnvironment(const std::string &subject, const std::string &object, const std::string &access)
1495 {
1496     const std::string ruleAll = "x";
1497
1498     SecurityServer::AccessProvider provider(subject);
1499     provider.allowAPI("system::homedir", ruleAll);
1500     provider.allowAPI(object, access);
1501     provider.applyAndSwithToUser(APP_UID, APP_GID);
1502 }
1503
1504 //- Add "l" rule to system
1505 //
1506 //Should be able to add "l" rule to system
1507 RUNNER_CHILD_TEST_SMACK(smack13_0_checking_laccess_mode_enabled_on_device)
1508 {
1509     std::string selfLabel = "smack13_0";
1510     std::string filename = "smack13_0_file";
1511
1512     //function inside checks if rule exist after add it
1513     SecurityServer::AccessProvider provider(selfLabel);
1514     provider.allowAPI(filename, "l");
1515     provider.apply();
1516
1517     int ret = smack_have_access(selfLabel.c_str(), filename.c_str(), "l");
1518     RUNNER_ASSERT_MSG_BT(ret == 1, "Error in adding laccess rule - l");
1519 }
1520
1521 //- Create file
1522 //- Set label for file and self
1523 //- Drop privileges
1524 //
1525 //Should have no access due to missing SMACK rule
1526 RUNNER_CHILD_TEST_SMACK(smack13_1_checking_laccess_mode)
1527 {
1528     std::string selfLabel = "smack13_1";
1529     std::string filename = "smack13_1_file";
1530     std::string filePath = testDir + filename;
1531
1532     createFileWithLabel(filePath, filename);
1533     int fd = open(filePath.c_str(), O_RDWR, 0);
1534     FDUniquePtr fp(&fd, closeFdPtr);
1535
1536     SecurityServer::AccessProvider provider(selfLabel);
1537     provider.applyAndSwithToUser(APP_UID, APP_GID);
1538
1539     int ret = flock(fd, LOCK_EX | LOCK_NB);
1540     RUNNER_ASSERT_MSG_BT(ret < 0, "Error, able to lock file: " << strerror(errno));
1541     ret = flock(fd, LOCK_UN | LOCK_NB);
1542     RUNNER_ASSERT_MSG_BT(ret < 0, "Error, able to lock file: " << strerror(errno));
1543     ret = flock(fd, LOCK_SH | LOCK_NB);
1544     RUNNER_ASSERT_MSG_BT(ret < 0, "Error, able to lock file: " << strerror(errno));
1545 }
1546
1547 //- Create file
1548 //- Set label for file and self
1549 //- Add SMACK rule "l"
1550 //- Drop privileges
1551 //
1552 //Should be able to lock file even without "w" rule
1553 RUNNER_CHILD_TEST_SMACK(smack13_2_checking_laccess_mode_with_l_rule)
1554 {
1555     std::string selfLabel = "smack13_2";
1556     std::string filename = "smack13_2_file";
1557     std::string filePath = testDir + filename;
1558
1559     createFileWithLabel(filePath, filename);
1560     int fd = open(filePath.c_str(), O_RDWR, 0);
1561     FDUniquePtr fp(&fd, closeFdPtr);
1562
1563     prepareEnvironment(selfLabel, filename, "l");
1564
1565     int ret = flock(fd, LOCK_EX | LOCK_NB);
1566     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to exclusive lock file: " << strerror(errno));
1567     ret = flock(fd, LOCK_UN | LOCK_NB);
1568     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to unlock file: " << strerror(errno));
1569     ret = flock(fd, LOCK_SH | LOCK_NB);
1570     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to shared lock file: " << strerror(errno));
1571 }
1572
1573 //- Create file
1574 //- Set label for file and self
1575 //- Add SMACK rule "w"
1576 //- Drop privileges
1577 //
1578 //Should be able to lock file even without "l" rule
1579 RUNNER_CHILD_TEST_SMACK(smack13_3_checking_laccess_mode_with_w_rule)
1580 {
1581     std::string selfLabel = "smack13_3";
1582     std::string filename = "smack13_3_file";
1583     std::string filePath = testDir + filename;
1584
1585     createFileWithLabel(filePath, filename);
1586     int fd = open(filePath.c_str(), O_RDWR, 0);
1587     FDUniquePtr fp(&fd, closeFdPtr);
1588
1589     prepareEnvironment(selfLabel, filename, "w");
1590
1591     int ret = flock(fd, LOCK_EX | LOCK_NB);
1592     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to exclusive lock file: " << strerror(errno));
1593     ret = flock(fd, LOCK_UN | LOCK_NB);
1594     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to unlock file: " << strerror(errno));
1595     ret = flock(fd, LOCK_SH | LOCK_NB);
1596     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to shared lock file: " << strerror(errno));
1597 }
1598
1599 //- Create file
1600 //- Set label for file and self
1601 //- Add SMACK rule "rw"
1602 //- Drop privileges
1603 //- Lock file (shared lock)
1604 //- Spawn child process
1605 //- Child tries to lock file (shared)
1606 //
1607 //Child should be able to lock file due to shared lock
1608 RUNNER_MULTIPROCESS_TEST_SMACK(smack13_4_0_checking_laccess_mode_w_rule_child)
1609 {
1610     std::string selfLabel = "smack13_4_0";
1611     std::string filename = "smack13_4_0_file";
1612     std::string filePath = testDir + filename;
1613
1614     createFileWithLabel(filePath, filename);
1615     int fd = open(filePath.c_str(), O_RDWR);
1616     FDUniquePtr fp(&fd, closeFdPtr);
1617     int ret = flock(fd, LOCK_SH | LOCK_NB);
1618     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to shared lock file: " << strerror(errno));
1619
1620     pid_t pid = fork();
1621     if (pid == 0) {
1622         //child process
1623         prepareEnvironment(selfLabel, filename, "rw");
1624
1625         int child_fd = open(filePath.c_str(), O_RDWR);
1626         RUNNER_ASSERT_MSG_BT(child_fd > -1, "Unable to open created file: " << strerror(errno));
1627
1628         //for descriptor protection
1629         FDUniquePtr fp(&child_fd, closeFdPtr);
1630
1631         ret = flock(child_fd, LOCK_SH | LOCK_NB);
1632         RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to lock file with shared lock: "
1633                           << strerror(errno));
1634     }
1635 }
1636
1637 //- Create file
1638 //- Set label for file and self
1639 //- Add SMACK rule "l"
1640 //- Drop privileges
1641 //- Lock file (shared lock)
1642 //- Spawn child process
1643 //- Child tries to lock file (shared)
1644 //
1645 //Child should be able to lock file due to shared lock
1646 RUNNER_MULTIPROCESS_TEST_SMACK(smack13_4_1_checking_laccess_mode_l_rule_child)
1647 {
1648     std::string selfLabel = "smack13_4_1";
1649     std::string filename = "smack13_4_1_file";
1650     std::string filePath = testDir + filename;
1651
1652     createFileWithLabel(filePath, filename);
1653     int fd = open(filePath.c_str(), O_RDWR);
1654     FDUniquePtr fp(&fd, closeFdPtr);
1655     int ret = flock(fd, LOCK_SH | LOCK_NB);
1656     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to shared lock file: " << strerror(errno));
1657
1658     pid_t pid = fork();
1659     if (pid == 0) {
1660         //child process
1661         //"r" is only for open in O_RDONLY mode
1662         prepareEnvironment(selfLabel, filename, "rl");
1663
1664         int child_fd = open(filePath.c_str(), O_RDONLY, 0);
1665         RUNNER_ASSERT_MSG_BT(child_fd > -1, "Unable to open created file: " << strerror(errno));
1666
1667         //for descriptor protection
1668         FDUniquePtr fp(&child_fd, closeFdPtr);
1669
1670         ret = flock(child_fd, LOCK_SH | LOCK_NB);
1671         RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to lock file with shared lock: "
1672                           << strerror(errno));
1673     }
1674 }
1675
1676 //- Create file
1677 //- Set label for file and self
1678 //- Add SMACK rule "rw"
1679 //- Drop privileges
1680 //- Lock file (exclusive lock)
1681 //- Spawn child process
1682 //- Child tries to lock file (exclusive / shared)
1683 //
1684 //Child should not be able to lock file due to exclusive lock
1685 RUNNER_MULTIPROCESS_TEST_SMACK(smack13_4_2_checking_laccess_mode_w_rule_child)
1686 {
1687     std::string selfLabel = "smack13_4_2";
1688     std::string filename = "smack13_4_2_file";
1689     std::string filePath = testDir + filename;
1690
1691     createFileWithLabel(filePath, filename);
1692     int fd = open(filePath.c_str(), O_RDWR);
1693     FDUniquePtr fp(&fd, closeFdPtr);
1694     int ret = flock(fd, LOCK_EX | LOCK_NB);
1695     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to exclusive lock file: " << strerror(errno));
1696
1697     pid_t pid = fork();
1698     if (pid == 0) {
1699         //child process
1700         prepareEnvironment(selfLabel, filename, "rw");
1701
1702         int child_fd = open(filePath.c_str(), O_RDWR, 0);
1703         RUNNER_ASSERT_MSG_BT(child_fd > -1, "Unable to open created file: " << strerror(errno));
1704
1705         //for descriptor protection
1706         FDUniquePtr fp(&child_fd, closeFdPtr);
1707
1708         ret = flock(child_fd, LOCK_EX | LOCK_NB);
1709         RUNNER_ASSERT_MSG_BT(ret < 0, "Error, able to lock file with exclusive lock");
1710     }
1711 }
1712
1713 //- Create file
1714 //- Set label for file and self
1715 //- Add SMACK rule "l"
1716 //- Drop privileges
1717 //- Lock file (exclusive lock)
1718 //- Spawn child process
1719 //- Child tries to lock file (exclusive / shared)
1720 //
1721 //Child should not be able to lock file due to exclusive lock
1722 RUNNER_MULTIPROCESS_TEST_SMACK(smack13_4_3_checking_laccess_mode_l_rule_child)
1723 {
1724     std::string selfLabel = "smack13_4_3";
1725     std::string filename = "smack13_4_3_file";
1726     std::string filePath = testDir + filename;
1727
1728     createFileWithLabel(filePath, filename);
1729     int fd = open(filePath.c_str(), O_RDWR, 0);
1730     FDUniquePtr fp(&fd, closeFdPtr);
1731     int ret = flock(fd, LOCK_EX | LOCK_NB);
1732     RUNNER_ASSERT_MSG_BT(ret == 0, "Error, unable to exclusive lock file: " << strerror(errno));
1733
1734     pid_t pid = fork();
1735     if (pid == 0) {
1736         //child process
1737         //"r" is only for open in O_RDONLY mode
1738         prepareEnvironment(selfLabel, filename, "rl");
1739
1740         int child_fd = open(filePath.c_str(), O_RDONLY, 0);
1741         RUNNER_ASSERT_MSG_BT(child_fd > -1, "Unable to open created file: " << strerror(errno));
1742
1743         //for descriptor protection
1744         FDUniquePtr fp(&child_fd, closeFdPtr);
1745
1746         ret = flock(child_fd, LOCK_EX | LOCK_NB);
1747         RUNNER_ASSERT_MSG_BT(ret < 0, "Error, able to lock file with eclusive lock");
1748     }
1749 }
1750
1751
1752 /////////////////////////////////////////
1753 //////NOSMACK ENVIRONMENT TESTS//////////
1754 /////////////////////////////////////////
1755
1756 /**
1757  * NOSMACK version of smack02 test. Functions, that should return error instead of success:
1758  * - smack_accesses_apply
1759  * - smack_have_access
1760  * - smack_revoke_subject
1761  * - smack_acceesses_clear
1762  *
1763  * Tests smack03, smack04, smack10, smack_accesses_clear, smack_revoke_subject all use functions
1764  * tested in smack02 test. Results from those functions (smack_have_access, smack_accesses_apply,
1765  * smack_accesses_clear, smack_revoke_subject) would be the same as in this test. Tests mentioned
1766  * above doesn't make much sense on NOSMACK environment when test smack02 exists and passes
1767  * correctly, thus those tests are are not implemented.
1768  */
1769 RUNNER_TEST_NOSMACK(smack02_aplying_rules_into_kernel_nosmack)
1770 {
1771
1772     smack_accesses *tmp = NULL;
1773     int result;
1774
1775     //init rules
1776     result = smack_accesses_new(&tmp);
1777     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to create smack_accesses instance");
1778
1779     //pass rules to unique_ptr
1780     AccessesUniquePtr rules(tmp, smack_accesses_free);
1781
1782     //adding test rules to struct (same as SMACK version of smack02 test)
1783     result = smack_accesses_add(rules.get(), "writer", "book", "rwx");
1784     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
1785     result = smack_accesses_add(rules.get(), "reader", "book", "r");
1786     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
1787     result = smack_accesses_add(rules.get(), "spy", "book", "rwx");
1788     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
1789
1790     //applying rules to kernel (should fail)
1791     result = smack_accesses_apply(rules.get());
1792     RUNNER_ASSERT_MSG_BT(result == -1, "Unable to apply rules into kernel");
1793
1794     //calls from SMACK version of this test - all should fail because of SMACK being turned off
1795     result = smack_have_access("spy", "book", "rwx");
1796     RUNNER_ASSERT_MSG_BT(result == -1, "smack_have_access should return error (SMACK is off)");
1797     result = smack_have_access("reader", "book", "rwx");
1798     RUNNER_ASSERT_MSG_BT(result == -1, "smack_have_access should return error (SMACK is off)");
1799     result = smack_have_access("s02badsubjectlabel", "book", "rwx");
1800     RUNNER_ASSERT_MSG_BT(result == -1, "smack_have_access should return error (SMACK is off)");
1801
1802     //testing subject revoking - should return error (no accesses applied = no subjects to revoke)
1803     result = smack_revoke_subject("s02nonexistinglabel");
1804     RUNNER_ASSERT_MSG_BT(result == -1, "smack_revoke_subject error - subject doesn't exist.");
1805     result = smack_revoke_subject("spy");
1806     RUNNER_ASSERT_MSG_BT(result == -1, "smack_revoke_subject error - subject doesn't exist.");
1807
1808     //after revoking smack_have_access still should return error
1809     result = smack_have_access("spy", "book", "rwx");
1810     RUNNER_ASSERT_MSG_BT(result == -1, "smack_have_access should return error (SMACK is off).");
1811
1812     result = smack_accesses_add(rules.get(), "s02subjectlabel", "book", "rwx");
1813     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to add smack rules");
1814
1815     //smack_accesses_clear should return error aswell
1816     result = smack_accesses_clear(rules.get());
1817     RUNNER_ASSERT_MSG_BT(result == -1, "Clearing rules should return error - no SMACK on system.");
1818
1819     result = smack_have_access("writer", "book", "rwx");
1820     RUNNER_ASSERT_MSG_BT(result == -1, "smack_have_access should return error (SMACK is off).");
1821 }
1822
1823 /**
1824  * NOSMACK version of smack11 test. Tests functions:
1825  * - smack_accesses_add_from_file
1826  *
1827  * Since other SMACK functions were tested in smack02 test, the only function needed to be checked
1828  * is applying rules loaded from file.
1829  */
1830 RUNNER_TEST_NOSMACK(smack03_saving_loading_rules_nosmack)
1831 {
1832     int result;
1833     int fd;
1834
1835     smack_accesses* tmp = NULL;
1836
1837     result = smack_accesses_new(&tmp);
1838     RUNNER_ASSERT_MSG_BT(result == 0, "Error during rules creation.");
1839
1840     AccessesUniquePtr rules(tmp, smack_accesses_free);
1841
1842     //open file with rules
1843     fd = open("/etc/smack/test_smack_rules_full", O_RDONLY, 0644);
1844     RUNNER_ASSERT_MSG_BT(fd >= 0,
1845             "Unable to open /etc/smack/test_smack_rules_full. Errno: " << strerror(errno));
1846
1847     //load accesses from file
1848     result = smack_accesses_add_from_file(rules.get(), fd);
1849     close(fd);
1850     RUNNER_ASSERT_MSG_BT(result == 0, "Error while importing accesses from file. Result: " << result);
1851 }
1852
1853 /**
1854  * NOSMACK version of smack05 test. Tests if functions getting, or
1855  * setting self label work correctly (that is, return error).
1856  */
1857 RUNNER_TEST_NOSMACK(smack04_self_label_nosmack)
1858 {
1859     char* label = NULL;
1860     int result;
1861     int fd;
1862
1863     char buff[SMACK_LABEL_LEN+1];
1864
1865     //smack_new_label_from_self should fail
1866     result = smack_new_label_from_self(&label);
1867     RUNNER_ASSERT_MSG_BT(result == -1, "new_label_from_self should return error (SMACK is off).");
1868     RUNNER_ASSERT_MSG_BT(label == NULL, "new_label_from_self shouldn't allocate memory to label.");
1869
1870     //We don't need to remember about freeing label - smack_new_label_from_self must return NULL
1871     //label if it's working properly.
1872
1873     // /proc/self/attr/current shouldn't keep any rules inside
1874     fd = open("/proc/self/attr/current", O_RDONLY, 0644);   //file exists, so it should open
1875     RUNNER_ASSERT_MSG_BT(fd >= 0, "/proc/self/attr/current failed to open.");
1876
1877     result = read(fd, buff, SMACK_LABEL_LEN);   //however reading it should return error
1878     if(result >= 0) {
1879         close(fd);
1880         RUNNER_ASSERT_MSG_BT(false, "Reading /proc/self/attr/current should return error.");
1881     }
1882
1883     //setting label for self should fail
1884     result = smack_set_label_for_self("s04testlabel");
1885     if(result != -1) {
1886         close(fd);
1887         RUNNER_ASSERT_MSG_BT(false, "set_label_for_self should return error (SMACK is off).");
1888     }
1889
1890     //getting previously set label should also fail
1891     result = smack_new_label_from_self(&label);
1892     if(result != -1) {
1893         close(fd);
1894         RUNNER_ASSERT_MSG_BT(false, "new_label_from_self should return error (SMACK is off).");
1895     }
1896     if(label != NULL) {
1897         close(fd);
1898         RUNNER_ASSERT_MSG_BT(false, "new_label_from_self shouldn't allocate memory to label.");
1899     }
1900
1901     // /proc/self/attr/current still shouldn't keep any rules inside
1902     result = lseek(fd, 0, SEEK_SET);    //going to the file beginning
1903     if(result != 0) {
1904         close(fd);
1905         RUNNER_ASSERT_MSG_BT(false, "lseek() error.");
1906     }
1907
1908     result = read(fd, buff, SMACK_LABEL_LEN);   //however it should return error
1909     if(result >= 0) {
1910         close(fd);
1911         RUNNER_ASSERT_MSG_BT(false, "Reading /proc/self/attr/current should return error.");
1912     }
1913
1914     close(fd);
1915 }
1916
1917 /**
1918  * NOSMACK version of smack_accesses_add_modify_x tests.
1919  *
1920  * Because all smack_accesses_add_modify tests are basically the same (all use smack_accesses_apply
1921  * and smack_have_access, which return -1 when SMACK is turned off), it makes much more sense to
1922  * write one test which will create rules using smack_accesses_add_modify and then check if
1923  * smack_accesses_apply and smack_have_access indeed return -1 when SMACK is turned off.
1924  */
1925 RUNNER_TEST_NOSMACK(smack05_accesses_add_modify_nosmack)
1926 {
1927     int result;
1928     smack_accesses* tmp = NULL;
1929
1930     result = smack_accesses_new(&tmp);
1931     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to allocate memory for rules. Result: " << result);
1932
1933     AccessesUniquePtr rules(tmp, smack_accesses_free);
1934
1935     //Not doing clean_up() every RUNNER_ASSERT_MSG - what clean_up does is just a creation of new
1936     //rule struct and removal of currenctly added and applied rules. clean_up() must be done only
1937     //after smack_accesses_apply().
1938     result = smack_accesses_add_modify(rules.get(), TEST_SUBJECT, TEST_OBJECT, "rwx", "");
1939     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule. Result: " << result);
1940
1941     result = smack_accesses_add_modify(rules.get(), TEST_SUBJECT, TEST_OBJECT, "rwx", "");
1942     RUNNER_ASSERT_MSG_BT(result == 0, "Unable to modify rule. Result: " << result);
1943
1944     result = smack_accesses_apply(rules.get());
1945     RUNNER_ASSERT_MSG_BT(result == -1,
1946             "smack_accesses_apply should return error (SMACK is off). Result: " << result);
1947
1948     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, "rwx");
1949     if(result != -1) {
1950         clean_up();
1951         RUNNER_ASSERT_MSG_BT(false,
1952                 "smack_have_access should return error (SMACK is off). Result: " << result);
1953     }
1954
1955     clean_up();
1956 }
1957
1958 /**
1959  * NOSMACK version of smack09 test.
1960  *
1961  * This test checks if smack_new_label_from_socket reacts correctly. Since label should be
1962  * acquired from getsockopt, and it should fail, we must only set up socket and call
1963  * smack_new_label_from_socket. It should return error.
1964  */
1965 RUNNER_MULTIPROCESS_TEST_NOSMACK(smack09_new_label_from_socket_nosmack)
1966 {
1967     int pid;
1968     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
1969     unlink(SOCK_PATH);
1970     char* smack_label;
1971
1972     pid = fork();
1973     RUNNER_ASSERT_MSG_BT(pid >= 0, "Fork failed");
1974     if (!pid) { //child (server)
1975         int sock, result;
1976         int fd;
1977
1978         //Create new socket
1979         sock = socket(AF_UNIX, SOCK_STREAM, 0);
1980         RUNNER_ASSERT_MSG_BT(sock >= 0, "socket failed: " << strerror(errno));
1981
1982         //Bind it to sockaddr
1983         result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
1984         if (result != 0) {
1985             close(sock);
1986             RUNNER_ASSERT_MSG_BT(false, "bind failed: " << strerror(errno));
1987         }
1988
1989         //Prepare for listening
1990         result = listen(sock, 1);
1991         if (result != 0) {
1992             close(sock);
1993             RUNNER_ASSERT_MSG_BT(false, "listen failed: " << strerror(errno));
1994         }
1995
1996         //Accept client
1997         alarm(2);
1998         fd = accept(sock, NULL, NULL);
1999         alarm(0);
2000         if (fd < 0) {
2001             close(sock);
2002             RUNNER_ASSERT_MSG_BT(false, "Failed when accepting connection from client.");
2003         }
2004
2005         //wait for smack_new_label_from_socket execution
2006         usleep(200);
2007
2008         //Close socket and server
2009         close(sock);
2010         exit(0);
2011     }
2012     else { //parent (client)
2013         //Wait a little bit until server is set up
2014         sleep(1);
2015         int sock, result;
2016
2017         //Create socket
2018         sock = socket(AF_UNIX, SOCK_STREAM, 0);
2019         RUNNER_ASSERT_MSG_BT(sock >= 0, "socket failed: " << strerror(errno));
2020
2021         //Connect to sockaddr
2022         result = connect(sock, (struct sockaddr*) &sockaddr,
2023                 sizeof(struct sockaddr_un));
2024         if (result != 0) {
2025             close(sock);
2026             RUNNER_ASSERT_MSG_BT(false, "connect failed: " << strerror(errno));
2027         }
2028
2029         //Try getting label, should fail beacuse getsockopt won't get anything
2030         result = smack_new_label_from_socket(sock, &smack_label);
2031         close(sock);
2032         RUNNER_ASSERT_MSG_BT(result == -1, "smack_new_label_from_socket should fail.");
2033     }
2034 }