2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file test_cases.cpp
19 * @author Jan Olszak (j.olszak@samsung.com)
20 * @author Rafal Krypa (r.krypa@samsung.com)
22 * @brief libprivilege-control test runer
34 #include <dpl/test/test_runner.h>
35 #include <dpl/test/test_runner_child.h>
36 #include <dpl/log/log.h>
37 #include <sys/types.h>
40 #include <sys/xattr.h>
41 #include <sys/smack.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <privilege-control.h>
51 #define SMACK_RULES_DIR "/opt/etc/smack-app/accesses.d/"
52 #define SMACK_STARTUP_RULES_FILE "/opt/etc/smack-app-early/accesses.d/rules"
53 #define SMACK_LOAD2 "/smack/load2"
54 #define TEST_APP_DIR "/etc/smack/test_privilege_control_DIR/app_dir"
55 #define TEST_NON_APP_DIR "/etc/smack/test_privilege_control_DIR/non_app_dir"
56 #define APPID_DIR "test_APP_ID_dir"
57 #define APPID_SHARED_DIR "test_APP_ID_shared_dir"
58 #define CANARY_LABEL "tiny_yellow_canary"
60 #define APP_ID "test_APP"
61 #define APP_SET_PRIV_PATH "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
62 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
64 #define WGT_APP_ID "QwCqJ0ttyS"
65 #define WGT_PARTNER_APP_ID "7btsV1Y0sX"
66 #define WGT_PLATFORM_APP_ID "G4DE3U2vmW"
67 #define WGT_APP_PATH "/opt/usr/apps/QwCqJ0ttyS/bin/QwCqJ0ttyS.TestMisiuPysiu123"
68 #define WGT_PARTNER_APP_PATH "/opt/usr/apps/7btsV1Y0sX/bin/7btsV1Y0sX.MisiuPysiu123Partner"
69 #define WGT_PLATFORM_APP_PATH "/opt/usr/apps/G4DE3U2vmW/bin/G4DE3U2vmW.MisiuPysiu123Platform"
70 #define OSP_APP_ID "uqNfgEjqc7"
71 #define OSP_PARTNER_APP_ID "j4RuPsZrNt"
72 #define OSP_PLATFORM_APP_ID "V5LKqDFBXm"
73 #define OSP_APP_PATH "/opt/usr/apps/uqNfgEjqc7/bin/PysiuMisiu123Osp"
74 #define OSP_PARTNER_APP_PATH "/opt/usr/apps/j4RuPsZrNt/bin/PysiuMisiu123OspPartner"
75 #define OSP_PLATFORM_APP_PATH "/opt/usr/apps/V5LKqDFBXm/bin/PysiuMisiu123OspPlatform"
76 #define EARLY_RULE_SUBJECT "livebox.web-provider"
77 #define EARLY_RULE_RIGHTS "rwx--"
79 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
80 const char *PRIVS2[] = { "test_privilege_control_rules2", NULL };
81 const char *PRIVS2_NO_R[] = { "test_privilege_control_rules2_no_r", NULL };
82 const char *PRIVS2_R[] = { "test_privilege_control_rules2_r", NULL };
83 const char *PRIVS2_R_AND_NO_R[] = { "test_privilege_control_rules2_r", "test_privilege_control_rules2_no_r", NULL };
84 const char *PRIVS_WGT[] = { "test_privilege_control_rules_wgt", NULL };
85 const char *PRIVS_OSP[] = { "test_privilege_control_rules_osp", NULL };
87 #define LIBPRIVILEGE_APP_GROUP_LIST "/usr/share/privilege-control/app_group_list"
88 #define LIBPRIVILEGE_TEST_DAC_FILE "/usr/share/privilege-control/test_privilege_control_rules.dac"
89 #define LIBPRIVILEGE_TEST_DAC_FILE_WGT "/usr/share/privilege-control/WRT_test_privilege_control_rules_wgt.dac"
90 #define LIBPRIVILEGE_TEST_DAC_FILE_OSP "/usr/share/privilege-control/OSP_test_privilege_control_rules_osp.dac"
92 #define APP_TEST_APP_1 "test-application1"
93 #define APP_TEST_APP_2 "test-application_2"
94 #define APP_TEST_APP_3 "test-app-3"
95 #define APP_TEST_AV_1 "test-antivirus1"
96 #define APP_TEST_AV_2 "test-antivirus_2"
97 #define APP_TEST_AV_3 "test-av-3"
99 #define SMACK_APPS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_apps_id.db"
100 #define SMACK_AVS_LABELS_DATABASE "/opt/dbspace/.privilege_control_all_avs_id.db"
101 #define SMACK_PUBLIC_DIRS_DATABASE "/opt/dbspace/.privilege_control_public_dirs.db"
102 #define SMACK_APPS_SETTINGS_LABELS_DATABASE "/opt/dbspace/.privilege_control_app_setting.db"
103 #define SMACK_SETTINGS_DIRS_DATABASE "/opt/dbspace/.privilege_control_setting_dir.db"
105 #define APP_TEST_SETTINGS_ASP1 "test-app-settings-asp1"
106 #define APP_TEST_SETTINGS_ASP2 "test-app-settings-asp2"
107 #define APP_TEST_AV_ASP1 "test-app-av-asp1"
108 #define APP_TEST_AV_ASP2 "test-app-av-asp2"
110 #define SOCK_PATH "/tmp/test-smack-socket"
114 #define APP_USER_NAME "app"
115 #define APP_HOME_DIR "/opt/home/app"
117 #define APP_FRIEND_1 "app_friend_1"
118 #define APP_FRIEND_2 "app_friend_2"
120 // How many open file descriptors should ftw() function use?
121 #define FTW_MAX_FDS 16
123 // Rules from test_privilege_control_rules.smack
124 const std::vector< std::vector<std::string> > rules = {
125 { APP_ID, "test_book_1", "r" },
126 { APP_ID, "test_book_2", "w" },
127 { APP_ID, "test_book_3", "x" },
128 { APP_ID, "test_book_4", "rw" },
129 { APP_ID, "test_book_5", "rx" },
130 { APP_ID, "test_book_6", "wx" },
131 { APP_ID, "test_book_7", "rwx" },
132 { "test_subject_1", APP_ID, "r" },
133 { "test_subject_2", APP_ID, "w" },
134 { "test_subject_3", APP_ID, "x" },
135 { "test_subject_4", APP_ID, "rw" },
136 { "test_subject_5", APP_ID, "rx" },
137 { "test_subject_6", APP_ID, "wx" },
138 { "test_subject_7", APP_ID, "rwx" },
139 { APP_ID, APPID_SHARED_DIR, "rwxat"}
142 // Rules from test_privilege_control_rules2.smack
143 const std::vector< std::vector<std::string> > rules2 = {
144 { APP_ID, "test_book_8", "r" },
145 { APP_ID, "test_book_9", "w" },
146 { APP_ID, "test_book_10", "x" },
147 { APP_ID, "test_book_11", "rw" },
148 { APP_ID, "test_book_12", "rx" },
149 { APP_ID, "test_book_13", "wx" },
150 { APP_ID, "test_book_14", "rwx" },
151 { APP_ID, "test_book_15", "rwxat" },
152 { "test_subject_8", APP_ID, "r" },
153 { "test_subject_9", APP_ID, "w" },
154 { "test_subject_10", APP_ID, "x" },
155 { "test_subject_11", APP_ID, "rw" },
156 { "test_subject_12", APP_ID, "rx" },
157 { "test_subject_13", APP_ID, "wx" },
158 { "test_subject_14", APP_ID, "rwx" },
159 { "test_subject_15", APP_ID, "rwxat" }
162 // Rules from test_privilege_control_rules_no_r.smack
163 const std::vector< std::vector<std::string> > rules2_no_r = {
164 { APP_ID, "test_book_9", "w" },
165 { APP_ID, "test_book_10", "x" },
166 { APP_ID, "test_book_11", "w" },
167 { APP_ID, "test_book_12", "x" },
168 { APP_ID, "test_book_13", "wx" },
169 { APP_ID, "test_book_14", "wx" },
170 { APP_ID, "test_book_15", "wxat" },
171 { "test_subject_9", APP_ID, "w" },
172 { "test_subject_10", APP_ID, "x" },
173 { "test_subject_11", APP_ID, "w" },
174 { "test_subject_12", APP_ID, "x" },
175 { "test_subject_13", APP_ID, "wx" },
176 { "test_subject_14", APP_ID, "wx" },
177 { "test_subject_15", APP_ID, "wxat" }
180 // Rules from test_privilege_control_rules.smack
181 // minus test_privilege_control_rules_no_r.smack
182 const std::vector< std::vector<std::string> > rules2_r = {
183 { APP_ID, "test_book_8", "r" },
184 { APP_ID, "test_book_11", "r" },
185 { APP_ID, "test_book_12", "r" },
186 { APP_ID, "test_book_14", "r" },
187 { APP_ID, "test_book_15", "r" },
188 { "test_subject_8", APP_ID, "r" },
189 { "test_subject_11", APP_ID, "r" },
190 { "test_subject_12", APP_ID, "r" },
191 { "test_subject_14", APP_ID, "r" },
192 { "test_subject_15", APP_ID, "r" }
195 // Rules from test_privilege_control_rules_wgt.smack for wgt
196 const std::vector< std::vector<std::string> > rules_wgt = {
197 { WGT_APP_ID, "test_book_8", "r" },
198 { WGT_APP_ID, "test_book_9", "w" },
199 { WGT_APP_ID, "test_book_10", "x" },
200 { WGT_APP_ID, "test_book_11", "rw" },
201 { WGT_APP_ID, "test_book_12", "rx" },
202 { WGT_APP_ID, "test_book_13", "wx" },
203 { WGT_APP_ID, "test_book_14", "rwx" },
204 { WGT_APP_ID, "test_book_15", "rwxat" },
205 { "test_subject_8", WGT_APP_ID, "r" },
206 { "test_subject_9", WGT_APP_ID, "w" },
207 { "test_subject_10", WGT_APP_ID, "x" },
208 { "test_subject_11", WGT_APP_ID, "rw" },
209 { "test_subject_12", WGT_APP_ID, "rx" },
210 { "test_subject_13", WGT_APP_ID, "wx" },
211 { "test_subject_14", WGT_APP_ID, "rwx" },
212 { "test_subject_15", WGT_APP_ID, "rwxat" }
215 // Rules from test_privilege_control_rules_wgt.smack for wgt_partner
216 const std::vector< std::vector<std::string> > rules_wgt_partner = {
217 { WGT_PARTNER_APP_ID, "test_book_8", "r" },
218 { WGT_PARTNER_APP_ID, "test_book_9", "w" },
219 { WGT_PARTNER_APP_ID, "test_book_10", "x" },
220 { WGT_PARTNER_APP_ID, "test_book_11", "rw" },
221 { WGT_PARTNER_APP_ID, "test_book_12", "rx" },
222 { WGT_PARTNER_APP_ID, "test_book_13", "wx" },
223 { WGT_PARTNER_APP_ID, "test_book_14", "rwx" },
224 { WGT_PARTNER_APP_ID, "test_book_15", "rwxat" },
225 { "test_subject_8", WGT_PARTNER_APP_ID, "r" },
226 { "test_subject_9", WGT_PARTNER_APP_ID, "w" },
227 { "test_subject_10", WGT_PARTNER_APP_ID, "x" },
228 { "test_subject_11", WGT_PARTNER_APP_ID, "rw" },
229 { "test_subject_12", WGT_PARTNER_APP_ID, "rx" },
230 { "test_subject_13", WGT_PARTNER_APP_ID, "wx" },
231 { "test_subject_14", WGT_PARTNER_APP_ID, "rwx" },
232 { "test_subject_15", WGT_PARTNER_APP_ID, "rwxat" }
235 // Rules from test_privilege_control_rules_wgt.smack for wgt_platform
236 const std::vector< std::vector<std::string> > rules_wgt_platform = {
237 { WGT_PLATFORM_APP_ID, "test_book_8", "r" },
238 { WGT_PLATFORM_APP_ID, "test_book_9", "w" },
239 { WGT_PLATFORM_APP_ID, "test_book_10", "x" },
240 { WGT_PLATFORM_APP_ID, "test_book_11", "rw" },
241 { WGT_PLATFORM_APP_ID, "test_book_12", "rx" },
242 { WGT_PLATFORM_APP_ID, "test_book_13", "wx" },
243 { WGT_PLATFORM_APP_ID, "test_book_14", "rwx" },
244 { WGT_PLATFORM_APP_ID, "test_book_15", "rwxat" },
245 { "test_subject_8", WGT_PLATFORM_APP_ID, "r" },
246 { "test_subject_9", WGT_PLATFORM_APP_ID, "w" },
247 { "test_subject_10", WGT_PLATFORM_APP_ID, "x" },
248 { "test_subject_11", WGT_PLATFORM_APP_ID, "rw" },
249 { "test_subject_12", WGT_PLATFORM_APP_ID, "rx" },
250 { "test_subject_13", WGT_PLATFORM_APP_ID, "wx" },
251 { "test_subject_14", WGT_PLATFORM_APP_ID, "rwx" },
252 { "test_subject_15", WGT_PLATFORM_APP_ID, "rwxat" }
255 // Rules from test_privilege_control_rules_osp.smack for osp
256 const std::vector< std::vector<std::string> > rules_osp = {
257 { OSP_APP_ID, "test_book_8", "r" },
258 { OSP_APP_ID, "test_book_9", "w" },
259 { OSP_APP_ID, "test_book_10", "x" },
260 { OSP_APP_ID, "test_book_11", "rw" },
261 { OSP_APP_ID, "test_book_12", "rx" },
262 { OSP_APP_ID, "test_book_13", "wx" },
263 { OSP_APP_ID, "test_book_14", "rwx" },
264 { OSP_APP_ID, "test_book_15", "rwxat" },
265 { "test_subject_8", OSP_APP_ID, "r" },
266 { "test_subject_9", OSP_APP_ID, "w" },
267 { "test_subject_10", OSP_APP_ID, "x" },
268 { "test_subject_11", OSP_APP_ID, "rw" },
269 { "test_subject_12", OSP_APP_ID, "rx" },
270 { "test_subject_13", OSP_APP_ID, "wx" },
271 { "test_subject_14", OSP_APP_ID, "rwx" },
272 { "test_subject_15", OSP_APP_ID, "rwxat" }
275 // Rules from test_privilege_control_rules_osp.smack for osp_partner
276 const std::vector< std::vector<std::string> > rules_osp_partner = {
277 { OSP_PARTNER_APP_ID, "test_book_8", "r" },
278 { OSP_PARTNER_APP_ID, "test_book_9", "w" },
279 { OSP_PARTNER_APP_ID, "test_book_10", "x" },
280 { OSP_PARTNER_APP_ID, "test_book_11", "rw" },
281 { OSP_PARTNER_APP_ID, "test_book_12", "rx" },
282 { OSP_PARTNER_APP_ID, "test_book_13", "wx" },
283 { OSP_PARTNER_APP_ID, "test_book_14", "rwx" },
284 { OSP_PARTNER_APP_ID, "test_book_15", "rwxat" },
285 { "test_subject_8", OSP_PARTNER_APP_ID, "r" },
286 { "test_subject_9", OSP_PARTNER_APP_ID, "w" },
287 { "test_subject_10", OSP_PARTNER_APP_ID, "x" },
288 { "test_subject_11", OSP_PARTNER_APP_ID, "rw" },
289 { "test_subject_12", OSP_PARTNER_APP_ID, "rx" },
290 { "test_subject_13", OSP_PARTNER_APP_ID, "wx" },
291 { "test_subject_14", OSP_PARTNER_APP_ID, "rwx" },
292 { "test_subject_15", OSP_PARTNER_APP_ID, "rwxat" }
295 // Rules from test_privilege_control_rules_osp.smack for osp_platform
296 const std::vector< std::vector<std::string> > rules_osp_platform = {
297 { OSP_PLATFORM_APP_ID, "test_book_8", "r" },
298 { OSP_PLATFORM_APP_ID, "test_book_9", "w" },
299 { OSP_PLATFORM_APP_ID, "test_book_10", "x" },
300 { OSP_PLATFORM_APP_ID, "test_book_11", "rw" },
301 { OSP_PLATFORM_APP_ID, "test_book_12", "rx" },
302 { OSP_PLATFORM_APP_ID, "test_book_13", "wx" },
303 { OSP_PLATFORM_APP_ID, "test_book_14", "rwx" },
304 { OSP_PLATFORM_APP_ID, "test_book_15", "rwxat" },
305 { "test_subject_8", OSP_PLATFORM_APP_ID, "r" },
306 { "test_subject_9", OSP_PLATFORM_APP_ID, "w" },
307 { "test_subject_10", OSP_PLATFORM_APP_ID, "x" },
308 { "test_subject_11", OSP_PLATFORM_APP_ID, "rw" },
309 { "test_subject_12", OSP_PLATFORM_APP_ID, "rx" },
310 { "test_subject_13", OSP_PLATFORM_APP_ID, "wx" },
311 { "test_subject_14", OSP_PLATFORM_APP_ID, "rwx" },
312 { "test_subject_15", OSP_PLATFORM_APP_ID, "rwxat" }
316 typedef std::unique_ptr<smack_accesses,std::function<void (smack_accesses*)> > SmackUniquePtr;
318 const char *OSP_BLAHBLAH = "/usr/share/privilege-control/OSP_feature.blah.blahblah.smack";
319 const char *WRT_BLAHBLAH = "/usr/share/privilege-control/WGT_blahblah.smack";
320 const char *OTHER_BLAHBLAH = "/usr/share/privilege-control/blahblah.smack";
321 const char *OSP_BLAHBLAH_DAC = "/usr/share/privilege-control/OSP_feature.blah.blahblah.dac";
322 const char *WRT_BLAHBLAH_DAC = "/usr/share/privilege-control/WGT_blahblah.dac";
323 const char *OTHER_BLAHBLAH_DAC = "/usr/share/privilege-control/blahblah.dac";
324 const char *BLAHBLAH_FEATURE = "http://feature/blah/blahblah";
327 * Check if every rule is true.
328 * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't
330 int test_have_all_accesses(const std::vector< std::vector<std::string> > &rules)
333 for (uint i = 0; i < rules.size(); ++i) {
334 result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
342 * Check if every rule is true.
343 * @return 1 if ANY rule in SMACK, 0 if
345 int test_have_any_accesses(const std::vector< std::vector<std::string> > &rules)
348 for (uint i = 0; i < rules.size(); ++i) {
349 result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
356 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
357 int /*typeflag*/, struct FTW* /*ftwbuf*/)
359 smack_lsetlabel(fpath, NULL, SMACK_LABEL_ACCESS);
360 smack_lsetlabel(fpath, NULL, SMACK_LABEL_EXEC);
361 smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
366 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
367 int /*typeflag*/, struct FTW* /*ftwbuf*/)
369 smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_ACCESS);
370 smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_EXEC);
371 smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
376 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
377 int /*typeflag*/, struct FTW* /*ftwbuf*/)
383 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
384 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
385 result = strcmp(CANARY_LABEL, label);
386 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is overwritten");
389 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
390 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
391 result = strcmp(CANARY_LABEL, label);
392 RUNNER_ASSERT_MSG(result == 0, "EXEC label on " << fpath << " is overwritten");
395 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
396 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
397 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
402 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
403 int /*typeflag*/, struct FTW* /*ftwbuf*/)
409 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
410 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
411 RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
412 result = strcmp(APPID_DIR, label);
413 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
416 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
417 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
418 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR)) {
419 RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
420 result = strcmp(APPID_DIR, label);
421 RUNNER_ASSERT_MSG(result == 0, "EXEC label on executable file " << fpath << " is incorrect");
422 } else if (S_ISLNK(sb->st_mode)) {
424 char *target = realpath(fpath, NULL);
425 RUNNER_ASSERT_MSG(0 == stat(target, &buf),"Stat failed for " << fpath);
427 if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG)) {
428 RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
430 RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
431 result = strcmp(APPID_DIR, label);
432 RUNNER_ASSERT_MSG(result == 0, "EXEC label on link to executable file " << fpath << " is incorrect");
435 RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
438 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
439 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
440 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
445 int nftw_check_labels_app_shared_dir(const char *fpath, const struct stat *sb,
446 int /*typeflag*/, struct FTW* /*ftwbuf*/)
452 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
453 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
454 RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
455 result = strcmp(APPID_SHARED_DIR, label);
456 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
458 result = smack_have_access(APP_ID, APPID_SHARED_DIR, "rwxat");
459 RUNNER_ASSERT_MSG(result == 1,
460 "Error rwxat access was not given shared dir. Subject: " <<
461 APP_ID << ". Object: " << APPID_SHARED_DIR << ". Result: " << result);
463 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
464 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
465 RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
468 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
469 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
470 if (S_ISDIR(sb->st_mode)) {
471 RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
472 result = strcmp("TRUE", label);
473 RUNNER_ASSERT_MSG(result == 0, "TRANSMUTE label on " << fpath << " is not set");
475 RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
480 int check_labels_dir(const char *fpath, const struct stat *sb,
481 const char *labels_db_path, const char *dir_db_path,
487 char *scanf_label_format;
488 char label_temp[SMACK_LABEL_LEN + 1];
492 result = smack_lgetlabel(fpath, &label_gen, SMACK_LABEL_ACCESS);
493 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
494 RUNNER_ASSERT_MSG(label_gen != NULL, "ACCESS label on " << fpath << " is not set");
497 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
500 RUNNER_ASSERT_MSG(false, "Could not get label for the path");
505 RUNNER_ASSERT_MSG(false, "EXEC label on " << fpath << " is set.");
509 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
513 RUNNER_ASSERT_MSG(false, "Could not get label for the path");
515 if (S_ISDIR(sb->st_mode)) {
519 RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set");
521 result = strcmp("TRUE", label);
525 RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set to TRUE");
527 } else if (label != NULL) {
530 RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is set");
535 if (0 > asprintf(&scanf_label_format, "%%%ds\\n", SMACK_LABEL_LEN)) {
537 RUNNER_ASSERT_MSG(false, "asprintf failed");
540 file_db = fopen(labels_db_path, "r");
541 if (file_db == NULL) {
543 free(scanf_label_format);
544 RUNNER_ASSERT_MSG(false, "Can not open database for apps");
546 while (fscanf(file_db, scanf_label_format, label_temp) == 1) {
547 result = smack_have_access(label_temp, label_gen, access);
551 free(scanf_label_format);
552 RUNNER_ASSERT_MSG(false,
553 "Error " << access << " access was not given for subject: "
554 << label_temp << ". Result: " << result);
559 file_db = fopen(dir_db_path, "r");
560 if (file_db == NULL) {
562 free(scanf_label_format);
563 RUNNER_ASSERT_MSG(false, "Can not open database for dirs");
566 while (fscanf(file_db, scanf_label_format, label_temp) == 1) {
567 if (strcmp(label_gen, label_temp) == 0) {
572 free(scanf_label_format);
576 RUNNER_ASSERT_MSG(is_dir, "Error autogenerated label is not in dirs db.");
581 int nftw_check_labels_app_public_dir(const char *fpath, const struct stat *sb,
582 int /*typeflag*/, struct FTW* /*ftwbuf*/)
584 return check_labels_dir(fpath, sb,
585 SMACK_APPS_LABELS_DATABASE,
586 SMACK_PUBLIC_DIRS_DATABASE, "rx");
589 int nftw_check_labels_app_settings_dir(const char *fpath, const struct stat *sb,
590 int /*typeflag*/, struct FTW* /*ftwbuf*/)
592 return check_labels_dir(fpath, sb,
593 SMACK_APPS_SETTINGS_LABELS_DATABASE,
594 SMACK_SETTINGS_DIRS_DATABASE, "rwx");
597 int file_exists(const char *path)
599 FILE *file = fopen(path, "r");
607 void osp_blahblah_check(int line_no, const std::vector<std::string> &rules)
609 std::ifstream smack_file(OSP_BLAHBLAH);
610 RUNNER_ASSERT_MSG(smack_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH);
612 auto it = rules.begin();
614 while (std::getline(smack_file,line)) {
615 RUNNER_ASSERT_MSG(it != rules.end(), "Line: " << line_no << "Additional line in file: " << line);
616 RUNNER_ASSERT_MSG(*it == line, "Line: " << line_no << " " << *it << "!=" << line);
620 RUNNER_ASSERT_MSG(it == rules.end(), "Line: " << line_no << " Missing line in file: " << *it);
625 void osp_blahblah_dac_check(int line_no, const std::vector<unsigned> &gids)
627 std::ifstream dac_file(OSP_BLAHBLAH_DAC);
628 RUNNER_ASSERT_MSG(dac_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH_DAC);
630 auto it = gids.begin();
632 while (std::getline(dac_file,line)) {
633 std::istringstream is(line);
636 RUNNER_ASSERT_MSG(it != gids.end(), "Line: " << line_no << "Additional line in file: " << gid);
637 RUNNER_ASSERT_MSG(*it == gid, "Line: " << line_no << " " << *it << "!=" << gid);
641 RUNNER_ASSERT_MSG(it == gids.end(), "Line: " << line_no << " Missing line in file: " << *it);
646 void remove_smack_files()
649 unlink(OSP_BLAHBLAH);
650 unlink(WRT_BLAHBLAH);
651 unlink(OTHER_BLAHBLAH);
652 unlink(OSP_BLAHBLAH_DAC);
653 unlink(WRT_BLAHBLAH_DAC);
654 unlink(OTHER_BLAHBLAH_DAC);
657 int cleaning_smack_app_files (void)
659 unlink(SMACK_RULES_DIR APP_TEST_APP_1);
661 unlink(SMACK_RULES_DIR APP_TEST_APP_2);
663 unlink(SMACK_RULES_DIR APP_TEST_APP_3);
665 unlink(SMACK_RULES_DIR APP_TEST_AV_1);
667 unlink(SMACK_RULES_DIR APP_TEST_AV_2);
669 unlink(SMACK_RULES_DIR APP_TEST_AV_3);
674 int cleaning_smack_database_files (void)
679 unlink(SMACK_APPS_LABELS_DATABASE);
680 fd = open(SMACK_APPS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
686 unlink(SMACK_AVS_LABELS_DATABASE);
687 fd = open(SMACK_AVS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
692 //clean app settings database
693 unlink(SMACK_APPS_SETTINGS_LABELS_DATABASE);
694 fd = open(SMACK_APPS_SETTINGS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
699 //clean public dirs database
700 unlink(SMACK_PUBLIC_DIRS_DATABASE);
701 fd = open(SMACK_PUBLIC_DIRS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
706 //clean settings dirs database
707 unlink(SMACK_SETTINGS_DIRS_DATABASE);
708 fd = open(SMACK_SETTINGS_DIRS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
716 void add_lables_to_db()
720 file_db = fopen(SMACK_AVS_LABELS_DATABASE, "a");
721 RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
722 << SMACK_AVS_LABELS_DATABASE << " can not be opened to apend!");
723 if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP1)) {
725 RUNNER_ASSERT_MSG(false, "Error writing to database file");
727 if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP2)) {
729 RUNNER_ASSERT_MSG(false, "Error writing to database file");
733 file_db = fopen(SMACK_APPS_SETTINGS_LABELS_DATABASE, "a");
734 RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
735 << SMACK_APPS_SETTINGS_LABELS_DATABASE << " can not be opened to apend!");
736 if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP1)) {
738 RUNNER_ASSERT_MSG(false, "Error writing to database file");
740 if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP2)) {
742 RUNNER_ASSERT_MSG(false, "Error writing to database file");
746 file_db = fopen(SMACK_APPS_LABELS_DATABASE, "a");
747 RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
748 << SMACK_APPS_LABELS_DATABASE << " can not be opened to apend!");
749 if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP1)) {
751 RUNNER_ASSERT_MSG(false, "Error writing to database file");
753 if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP2)) {
755 RUNNER_ASSERT_MSG(false, "Error writing to database file");
757 if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP1)) {
759 RUNNER_ASSERT_MSG(false, "Error writing to database file");
761 if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP2)) {
763 RUNNER_ASSERT_MSG(false, "Error writing to database file");
769 RUNNER_TEST_GROUP_INIT(libprivilegecontrol)
772 * Test setting labels for all files and folders in given path.
774 RUNNER_TEST(privilege_control02_app_label_dir)
778 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
779 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
781 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
782 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
784 result = perm_app_setup_path(APPID_DIR, TEST_APP_DIR, APP_PATH_PRIVATE);
785 RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
787 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_dir, FTW_MAX_FDS, FTW_PHYS);
788 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for app dir");
790 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
791 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
794 RUNNER_TEST(privilege_control03_app_label_shared_dir)
798 result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APP_ID);
799 RUNNER_ASSERT_MSG(result != 0, "perm_app_setup_path(APP_ID, APP_ID) didn't fail");
801 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
802 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
804 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
805 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
807 result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APPID_SHARED_DIR);
808 RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
810 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
811 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
813 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
814 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
818 * Add permisions from test_privilege_control_rules template
820 RUNNER_TEST(privilege_control04_add_permissions)
822 int result = app_add_permissions(APP_ID, PRIVS);
823 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
824 " Error adding app permissions. Result: " << result);
826 // Check if the accesses are realy applied..
827 result = test_have_all_accesses(rules);
828 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
831 FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
832 RUNNER_ASSERT_MSG(pFile != NULL,
833 "SMACK file NOT created!. Errno: " << errno);
836 fseek(pFile, 0L, SEEK_END);
837 int smack_file_length = ftell(pFile);
838 RUNNER_ASSERT_MSG(smack_file_length > 0,
839 "SMACK file empty, but privileges list was not empty.. Errno: " << errno);
846 * Revoke permissions from the list. Should be executed as privileged user.
848 RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
852 // Revoke permissions
853 result = perm_app_revoke_permissions(APP_ID);
854 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
855 "Error revoking app permissions. Result: " << result);
857 result = perm_app_revoke_permissions(WGT_APP_ID);
858 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
859 "Error revoking app permissions. Result: " << result);
860 result = perm_app_revoke_permissions(WGT_PARTNER_APP_ID);
861 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
862 "Error revoking app permissions. Result: " << result);
863 result = perm_app_revoke_permissions(WGT_PLATFORM_APP_ID);
864 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
865 "Error revoking app permissions. Result: " << result);
867 result = perm_app_revoke_permissions(OSP_APP_ID);
868 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
869 "Error revoking app permissions. Result: " << result);
870 result = perm_app_revoke_permissions(OSP_PARTNER_APP_ID);
871 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
872 "Error revoking app permissions. Result: " << result);
873 result = perm_app_revoke_permissions(OSP_PLATFORM_APP_ID);
874 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
875 "Error revoking app permissions. Result: " << result);
877 // Are all the permissions revoked?
878 result = test_have_any_accesses(rules);
879 RUNNER_ASSERT_MSG(result != 1, "Not all permisions revoked.");
880 result = test_have_any_accesses(rules_wgt);
881 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
882 result = test_have_any_accesses(rules_wgt_partner);
883 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
884 result = test_have_any_accesses(rules_wgt_platform);
885 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
887 result = test_have_any_accesses(rules);
888 RUNNER_ASSERT_MSG(result != 1, "Not all permisions revoked.");
889 result = test_have_any_accesses(rules_osp);
890 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
891 result = test_have_any_accesses(rules_osp_partner);
892 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
893 result = test_have_any_accesses(rules_osp_platform);
894 RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
896 FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
897 RUNNER_ASSERT_MSG(pFile != NULL,
898 "SMACK file removed!. Errno: " << errno);
900 fseek(pFile, 0L, SEEK_END);
901 int smack_file_length = ftell(pFile);
904 RUNNER_ASSERT_MSG(smack_file_length == 0,
905 "SMACK file not empty.. Errno: " << errno);
907 pFile = fopen(SMACK_RULES_DIR WGT_APP_ID, "rb");
908 RUNNER_ASSERT_MSG(pFile != NULL,
909 "SMACK file removed!. Errno: " << errno);
911 fseek(pFile, 0L, SEEK_END);
912 smack_file_length = ftell(pFile);
915 RUNNER_ASSERT_MSG(smack_file_length == 0,
916 "SMACK file not empty.. Errno: " << errno);
918 pFile = fopen(SMACK_RULES_DIR WGT_PARTNER_APP_ID, "rb");
919 RUNNER_ASSERT_MSG(pFile != NULL,
920 "SMACK file removed!. Errno: " << errno);
922 fseek(pFile, 0L, SEEK_END);
923 smack_file_length = ftell(pFile);
926 RUNNER_ASSERT_MSG(smack_file_length == 0,
927 "SMACK file not empty.. Errno: " << errno);
929 pFile = fopen(SMACK_RULES_DIR WGT_PLATFORM_APP_ID, "rb");
930 RUNNER_ASSERT_MSG(pFile != NULL,
931 "SMACK file removed!. Errno: " << errno);
933 fseek(pFile, 0L, SEEK_END);
934 smack_file_length = ftell(pFile);
937 RUNNER_ASSERT_MSG(smack_file_length == 0,
938 "SMACK file not empty.. Errno: " << errno);
940 pFile = fopen(SMACK_RULES_DIR OSP_APP_ID, "rb");
941 RUNNER_ASSERT_MSG(pFile != NULL,
942 "SMACK file removed!. Errno: " << errno);
944 fseek(pFile, 0L, SEEK_END);
945 smack_file_length = ftell(pFile);
948 RUNNER_ASSERT_MSG(smack_file_length == 0,
949 "SMACK file not empty.. Errno: " << errno);
951 pFile = fopen(SMACK_RULES_DIR OSP_PARTNER_APP_ID, "rb");
952 RUNNER_ASSERT_MSG(pFile != NULL,
953 "SMACK file removed!. Errno: " << errno);
955 fseek(pFile, 0L, SEEK_END);
956 smack_file_length = ftell(pFile);
959 RUNNER_ASSERT_MSG(smack_file_length == 0,
960 "SMACK file not empty.. Errno: " << errno);
962 pFile = fopen(SMACK_RULES_DIR OSP_PLATFORM_APP_ID, "rb");
963 RUNNER_ASSERT_MSG(pFile != NULL,
964 "SMACK file removed!. Errno: " << errno);
966 fseek(pFile, 0L, SEEK_END);
967 smack_file_length = ftell(pFile);
970 RUNNER_ASSERT_MSG(smack_file_length == 0,
971 "SMACK file not empty.. Errno: " << errno);
974 static void read_gids(std::set<unsigned> &set, const char *file_path)
976 FILE *f = fopen(file_path, "r");
977 RUNNER_ASSERT_MSG(f != NULL, "Unable to open file " << file_path);
979 while (fscanf(f, "%u\n", &gid) == 1) {
984 RUNNER_TEST(privilege_control05_add_shared_dir_readers)
986 #define TEST_OBJ "TEST_OBJECT"
987 #define TEST_OBJ_SOME_OTHER "TEST_OBJA"
988 #define test_string_01 "TEST_raz TEST_OBJECT r-x-- -----"
989 #define test_string_21 "TEST_trzy TEST_OBJA -wx--\n"
990 #define test_string_22 "TEST_trzy TEST_OBJECT r-x-- -----\n"
997 const char *app_labels_wrong[] = {"-TEST_raz", NULL};
998 const char *app_labels[] = {"TEST_raz", "TEST_dwa", "TEST_trzy", NULL};
999 const int READ_BUF_SIZE = 1000;
1000 char buf[READ_BUF_SIZE];
1002 struct smack_accesses *rules = NULL;
1004 //test environment cleaning
1005 cleaning_smack_app_files();
1006 cleaning_smack_database_files();
1008 //test what happens when the label is not correct SMACK label
1009 result = smack_accesses_new(&rules);
1010 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new.");
1012 result = add_shared_dir_readers(TEST_OBJ,app_labels_wrong);
1013 RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "add_shared_dir_readers should fail here");
1015 result = smack_have_access(app_labels_wrong[0],TEST_OBJ,"rx");
1016 RUNNER_ASSERT_MSG(result != 1, "add_shared_dir_readers should not grant permission here");
1018 smack_accesses_free(rules);
1020 //ok, now the correct list of apps
1021 result = smack_accesses_new(&rules);
1022 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new.");
1024 for (i = 0; i < 3; i++) {
1026 result = perm_app_revoke_permissions(app_labels[i]);
1027 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_revoke_permissions.");
1028 result = perm_app_uninstall(app_labels[i]);
1029 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
1030 result = perm_app_install(app_labels[i]);
1031 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
1033 RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[i]));
1034 fd = open(path, O_WRONLY, 0644);
1035 RUNNER_ASSERT_MSG(fd != -1, "Error in opening file " << path);
1038 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ,"wt");
1039 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
1042 smack_accesses_free(rules);
1043 result = smack_accesses_new(&rules);
1044 result = smack_accesses_add(rules,app_labels[i],TEST_OBJ_SOME_OTHER,"wx");
1045 RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
1047 result = smack_accesses_apply(rules);
1048 RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
1050 result = smack_accesses_save(rules, fd);
1051 RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
1057 smack_accesses_free(rules);
1059 // THE TEST - accesses
1061 result = add_shared_dir_readers(TEST_OBJ,app_labels);
1062 RUNNER_ASSERT_MSG(result == 0, "add_shared_dir_readers failed");
1064 result = smack_have_access(app_labels[0],TEST_OBJ,"rx");
1065 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1067 result = smack_have_access(app_labels[1],TEST_OBJ,"rx");
1068 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1070 result = smack_have_access(app_labels[2],TEST_OBJ,"rx");
1071 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1073 result = smack_have_access(app_labels[1],TEST_OBJ,"rwxt");
1074 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1076 result = smack_have_access(app_labels[2],TEST_OBJ_SOME_OTHER,"wx");
1077 RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1080 //TEST the operations on empty files
1082 RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[0]));
1083 file = fopen(path, "r");
1085 RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
1087 RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1088 result = strcmp(buf, test_string_01);
1089 RUNNER_ASSERT_MSG( result != 0, "add_shared_dir_readers ERROR, file not formatted" << path);
1094 //TEST the operations on non empty files
1095 RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[2]));
1097 file = fopen(path, "r");
1098 RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
1100 RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1101 result = strcmp(buf, test_string_21);
1102 RUNNER_ASSERT_MSG( result == 0, "add_shared_dir_readers ERROR, file not formatted" << path);
1104 RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1105 result = strcmp(buf, test_string_22);
1106 RUNNER_ASSERT_MSG( result == 0, "add_shared_dir_readers ERROR, file not formatted" << path);
1114 * Set APP privileges.
1117 void check_groups(const char *dac_file)
1119 std::set<unsigned> groups_check;
1120 read_gids(groups_check, LIBPRIVILEGE_APP_GROUP_LIST);
1121 read_gids(groups_check, dac_file);
1123 int groups_cnt = getgroups(0, NULL);
1124 RUNNER_ASSERT_MSG(groups_cnt > 0, "Wrong number of supplementary groupsCnt");
1125 gid_t *groups_list = (gid_t*) calloc(groups_cnt, sizeof(gid_t));
1126 RUNNER_ASSERT_MSG(groups_list != NULL, "Memory allocation failed");
1127 RUNNER_ASSERT(-1 != getgroups(groups_cnt, groups_list));
1129 for (int i = 0; i < groups_cnt; ++i) {
1130 //getgroups() can return multiple number of the same group
1131 //they are returned in sequence, so we will given number when last
1132 //element of this number is reached
1133 if ((i < groups_cnt - 1) && (groups_list[i + 1] == groups_list[i]))
1135 if (groups_check.erase(groups_list[i]) == 0) {
1136 // getgroups() may also return process' main group
1137 if (groups_list[i] != getgid())
1138 RUNNER_ASSERT_MSG(false, "Application belongs to unknown group (GID=" << groups_list[i] << ")");
1142 std::string groups_left;
1143 for (std::set<unsigned>::iterator it = groups_check.begin(); it != groups_check.end(); it++) {
1144 groups_left.append(std::to_string(*it)).append(" ");
1146 RUNNER_ASSERT_MSG(groups_check.empty(), "Application doesn't belong to some required groups: " << groups_left);
1149 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
1153 // Preset exec label
1154 smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
1155 smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
1158 * TODO This test should also verify perm_app_set_privilege behavior for OSP and
1159 * WRT apps. To do that we'll have to install real apps on device as a
1163 // Set APP privileges
1164 result = perm_app_set_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
1165 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1167 // Check if SMACK label really set
1169 result = smack_new_label_from_self(&label);
1170 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1171 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1172 result = strcmp(APP_ID, label);
1173 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1175 // Check if DAC privileges really set
1176 RUNNER_ASSERT_MSG(getuid() == APP_UID, "Wrong UID");
1177 RUNNER_ASSERT_MSG(getgid() == APP_GID, "Wrong GID");
1179 result = strcmp(getenv("HOME"), APP_HOME_DIR);
1180 RUNNER_ASSERT_MSG(result == 0, "Wrong HOME DIR");
1182 result = strcmp(getenv("USER"), APP_USER_NAME);
1183 RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME");
1185 check_groups(LIBPRIVILEGE_TEST_DAC_FILE);
1189 * Set APP privileges. wgt.
1191 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt)
1195 result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS_WGT, 1);
1196 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1197 " Error enabling app permissions. Result: " << result);
1199 result = test_have_all_accesses(rules_wgt);
1200 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1202 result = perm_app_set_privilege(WGT_APP_ID, "wgt", WGT_APP_PATH);
1203 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1205 // Check if SMACK label really set
1207 result = smack_new_label_from_self(&label);
1208 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1209 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1210 result = strcmp(WGT_APP_ID, label);
1211 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1214 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1218 * Set APP privileges. wgt_partner.
1220 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt_partner)
1224 result = perm_app_enable_permissions(WGT_PARTNER_APP_ID, APP_TYPE_WGT_PARTNER, PRIVS_WGT, 1);
1225 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1226 " Error enabling app permissions. Result: " << result);
1228 result = test_have_all_accesses(rules_wgt_partner);
1229 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1231 result = perm_app_set_privilege(WGT_PARTNER_APP_ID, "wgt_partner", WGT_PARTNER_APP_PATH);
1232 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1234 // Check if SMACK label really set
1236 result = smack_new_label_from_self(&label);
1237 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1238 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1239 result = strcmp(WGT_PARTNER_APP_ID, label);
1240 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1242 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1246 * Set APP privileges. wgt_platform.
1248 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt_platform)
1252 result = perm_app_enable_permissions(WGT_PLATFORM_APP_ID, APP_TYPE_WGT_PLATFORM, PRIVS_WGT, 1);
1253 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1254 " Error enabling app permissions. Result: " << result);
1256 result = test_have_all_accesses(rules_wgt_platform);
1257 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1259 result = perm_app_set_privilege(WGT_PLATFORM_APP_ID, "wgt_platform", WGT_PLATFORM_APP_PATH);
1260 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1262 // Check if SMACK label really set
1264 result = smack_new_label_from_self(&label);
1265 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1266 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1267 result = strcmp(WGT_PLATFORM_APP_ID, label);
1268 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1270 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1274 * Set APP privileges. osp app.
1276 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp)
1280 result = perm_app_enable_permissions(OSP_APP_ID, APP_TYPE_OSP, PRIVS_OSP, 1);
1281 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1282 " Error enabling app permissions. Result: " << result);
1284 result = test_have_all_accesses(rules_osp);
1285 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1287 result = perm_app_set_privilege(OSP_APP_ID, NULL, OSP_APP_PATH);
1288 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1290 // Check if SMACK label really set
1292 result = smack_new_label_from_self(&label);
1293 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1294 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1295 result = strcmp(OSP_APP_ID, label);
1296 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1298 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1302 * Set APP privileges. partner osp app.
1304 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp_partner)
1308 result = perm_app_enable_permissions(OSP_PARTNER_APP_ID, APP_TYPE_OSP_PARTNER, PRIVS_OSP, 1);
1309 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1310 " Error enabling app permissions. Result: " << result);
1312 result = test_have_all_accesses(rules_osp_partner);
1313 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1315 result = perm_app_set_privilege(OSP_PARTNER_APP_ID, NULL, OSP_PARTNER_APP_PATH);
1316 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1318 // Check if SMACK label really set
1320 result = smack_new_label_from_self(&label);
1321 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1322 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1323 result = strcmp(OSP_PARTNER_APP_ID, label);
1324 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1326 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1330 * Set APP privileges. platform osp app.
1332 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp_platform)
1336 result = perm_app_enable_permissions(OSP_PLATFORM_APP_ID, APP_TYPE_OSP_PLATFORM, PRIVS_OSP, 1);
1337 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1338 " Error enabling app permissions. Result: " << result);
1340 result = test_have_all_accesses(rules_osp_platform);
1341 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1343 result = perm_app_set_privilege(OSP_PLATFORM_APP_ID, NULL, OSP_PLATFORM_APP_PATH);
1344 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in perm_app_set_privilege. Error: " << result);
1346 // Check if SMACK label really set
1348 result = smack_new_label_from_self(&label);
1349 RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1350 RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1351 result = strcmp(OSP_PLATFORM_APP_ID, label);
1352 RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1354 check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1358 * Add new API feature
1360 RUNNER_TEST(privilege_control11_add_api_feature)
1364 remove_smack_files();
1367 // argument validation
1368 result = perm_add_api_feature(APP_TYPE_OSP, NULL, NULL, NULL, 0);
1369 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1371 result = perm_add_api_feature(APP_TYPE_OSP,"", NULL, NULL, 0);
1372 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1375 // already existing features
1376 result = perm_add_api_feature(APP_TYPE_OSP,"http://tizen.org/privilege/messaging.read", NULL, NULL, 0);
1377 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1379 result = perm_add_api_feature(APP_TYPE_WGT,"http://tizen.org/privilege/messaging.sms", NULL, NULL, 0);
1380 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1382 result = perm_add_api_feature(APP_TYPE_OTHER,"http://tizen.org/privilege/messaging", NULL, NULL, 0);
1383 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1385 result = perm_add_api_feature(APP_TYPE_OTHER,"http://tizen.org/messaging", NULL, NULL, 0);
1386 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1388 result = perm_add_api_feature(APP_TYPE_OTHER,"http://messaging", NULL, NULL, 0);
1389 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1391 result = perm_add_api_feature(APP_TYPE_OTHER,"messaging.read", NULL, NULL, 0);
1392 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1396 result = perm_add_api_feature(APP_TYPE_OSP,"blahblah", NULL, NULL, 0);
1397 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1399 result = perm_add_api_feature(APP_TYPE_WGT,"blahblah", NULL, NULL, 0);
1400 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1402 result = perm_add_api_feature(APP_TYPE_OTHER,"blahblah", NULL, NULL, 0);
1403 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1406 // smack files existence
1407 result = file_exists(OSP_BLAHBLAH);
1408 RUNNER_ASSERT(result == -1);
1410 result = file_exists(WRT_BLAHBLAH);
1411 RUNNER_ASSERT(result == -1);
1413 result = file_exists(OTHER_BLAHBLAH);
1414 RUNNER_ASSERT(result == -1);
1418 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, { NULL }, NULL, 0);
1419 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1420 result = file_exists(OSP_BLAHBLAH);
1421 RUNNER_ASSERT(result == -1);
1423 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "", NULL }, NULL, 0);
1424 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1425 result = file_exists(OSP_BLAHBLAH);
1426 RUNNER_ASSERT(result == 0);
1427 remove_smack_files();
1429 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { " \t\n", "\t \n", "\n\t ", NULL }, NULL, 0);
1430 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1431 result = file_exists(OSP_BLAHBLAH);
1432 RUNNER_ASSERT(result == 0);
1433 remove_smack_files();
1437 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed", NULL }, NULL, 0);
1438 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1439 result = file_exists(OSP_BLAHBLAH);
1440 RUNNER_ASSERT(result == -1);
1442 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed malformed", NULL }, NULL, 0);
1443 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1444 result = file_exists(OSP_BLAHBLAH);
1445 RUNNER_ASSERT(result == -1);
1447 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "-malformed malformed rwxat", NULL }, NULL, 0);
1448 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1449 result = file_exists(OSP_BLAHBLAH);
1450 RUNNER_ASSERT(result == -1);
1452 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "~/\"\\ malformed rwxat", NULL }, NULL, 0);
1453 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1454 result = file_exists(OSP_BLAHBLAH);
1455 RUNNER_ASSERT(result == -1);
1457 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "subject object rwxat something else", NULL }, NULL, 0);
1458 RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1459 result = file_exists(OSP_BLAHBLAH);
1460 RUNNER_ASSERT(result == -1);
1464 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed malformed maaaaaalformed", NULL }, NULL, 0);
1465 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1466 osp_blahblah_check(__LINE__, { "malformed malformed r--a- -----" });
1467 remove_smack_files();
1469 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "subject object foo", NULL }, NULL, 0);
1470 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1471 osp_blahblah_check(__LINE__, { "subject object ----- -----" });
1472 remove_smack_files();
1474 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1475 "subject object\t rwxat",
1477 "subject2\tobject2 txarw",
1481 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1482 osp_blahblah_check(__LINE__, { "subject object rwxat -----", "subject2 object2 rwxat -----"});
1483 remove_smack_files();
1485 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1486 "Sub::jE,ct object a-RwX",
1489 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1490 osp_blahblah_check(__LINE__, { "Sub::jE,ct object rwxa- -----"});
1491 remove_smack_files();
1493 // TODO For now identical/complementary rules are not merged.
1494 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1495 "subject object rwxat",
1497 "subject object txarw",
1501 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1502 osp_blahblah_check(__LINE__, { "subject object rwxat -----", "subject object rwxat -----"});
1503 remove_smack_files();
1507 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},0);
1508 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1509 osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1510 result = file_exists(OSP_BLAHBLAH_DAC);
1511 RUNNER_ASSERT(result == -1);
1512 remove_smack_files();
1516 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},3);
1517 printf("%d \n", result);
1518 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1519 osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1520 osp_blahblah_dac_check(__LINE__, {0,1,2});
1521 remove_smack_files();
1523 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},1);
1524 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1525 osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1526 osp_blahblah_dac_check(__LINE__, {0});
1527 remove_smack_files();
1529 result = perm_add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {1,1,1},3);
1530 RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1531 osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1532 osp_blahblah_dac_check(__LINE__, {1,1,1});
1533 remove_smack_files();
1537 * Check perm_app_install function
1539 RUNNER_TEST(privilege_control01_app_install)
1544 unlink(SMACK_RULES_DIR APP_ID);
1546 perm_app_uninstall(APP_ID);
1548 result = perm_app_install(APP_ID);
1549 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1551 // checking if file really exists
1552 fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
1553 RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
1556 // try install second time app with the same ID - it should pass.
1557 result = perm_app_install(APP_ID);
1558 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1562 * Check perm_app_install function
1564 RUNNER_TEST(privilege_control07_app_uninstall)
1569 result = perm_app_uninstall(APP_ID);
1570 RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
1572 // checking if file really exists
1573 fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
1574 RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after perm_app_uninstall");
1578 void checkOnlyAvAccess(const char *av_id, const char *app_id, const char *comment)
1581 result = smack_have_access(av_id, app_id, "rwx");
1582 RUNNER_ASSERT_MSG(result == 1,
1583 "Error while checking " << av_id << " rwx access to "
1584 << app_id << " " << comment << " Result: " << result);
1585 result = smack_have_access(av_id, app_id, "a");
1586 RUNNER_ASSERT_MSG(result == 0,
1587 "Error while checking " << av_id << " a access to "
1588 << app_id << " " << comment << " Result: " << result);
1589 result = smack_have_access(av_id, app_id, "t");
1590 RUNNER_ASSERT_MSG(result == 0,
1591 "Error while checking " << av_id << " t access to "
1592 << app_id << " " << comment << " Result: " << result);
1596 * Check app_register_av function
1597 * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
1599 RUNNER_TEST(privilege_control10_app_register_av)
1604 smack_revoke_subject(APP_TEST_AV_1);
1605 smack_revoke_subject(APP_TEST_AV_2);
1607 cleaning_smack_app_files();
1608 cleaning_smack_database_files();
1610 // Adding two apps before antivir
1611 result = perm_app_install(APP_TEST_APP_1);
1612 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1614 result = perm_app_install(APP_TEST_APP_2);
1615 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1618 result = app_register_av(APP_TEST_AV_1);
1619 RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
1621 // Checking added apps accesses
1622 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_1)");
1623 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_1)");
1626 result = perm_app_install(APP_TEST_APP_3);
1627 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
1629 // Checking app accesses
1630 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "perm_app_install(APP_TEST_APP_3)");
1631 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "perm_app_install(APP_TEST_APP_3)");
1632 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "perm_app_install(APP_TEST_APP_3)");
1634 // Adding second antivir
1635 result = app_register_av(APP_TEST_AV_2);
1636 RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
1638 // Checking app accesses
1639 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
1640 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
1641 checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
1642 checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
1643 checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
1644 checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
1647 smack_revoke_subject(APP_TEST_AV_1);
1648 smack_revoke_subject(APP_TEST_AV_2);
1650 cleaning_smack_app_files();
1651 cleaning_smack_database_files();
1655 * Grant SMACK permissions based on permissions list.
1657 RUNNER_TEST(privilege_control11_app_enable_permissions)
1660 int smack_file_length;
1664 * Test - Enabling all permissions with persistant mode enabled
1667 result = perm_app_revoke_permissions(APP_ID);
1668 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1669 "Error revoking app permissions. Result: " << result);
1671 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1672 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1673 " Error enabling app permissions. Result: " << result);
1675 // Check if the accesses are realy applied..
1676 result = test_have_all_accesses(rules2);
1677 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1680 pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1681 RUNNER_ASSERT_MSG(pFile != NULL,
1682 "SMACK file NOT created!. Errno: " << errno);
1685 fseek(pFile, 0L, SEEK_END);
1686 smack_file_length = ftell(pFile);
1687 RUNNER_ASSERT_MSG(smack_file_length > 0,
1688 "SMACK file empty with persistant mode 1. Errno: " << errno);
1694 result = perm_app_revoke_permissions(APP_ID);
1695 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1696 "Error revoking app permissions. Result: " << result);
1699 * Test - Enabling all permissions with persistant mode disabled
1702 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 0);
1703 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1704 " Error enabling app permissions. Result: " << result);
1706 // Check if the accesses are realy applied..
1707 result = test_have_all_accesses(rules2);
1708 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1711 pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1712 RUNNER_ASSERT_MSG(pFile != NULL,
1713 "SMACK file NOT created!. Errno: " << errno);
1716 fseek(pFile, 0L, SEEK_END);
1717 smack_file_length = ftell(pFile);
1718 RUNNER_ASSERT_MSG(smack_file_length == 0,
1719 "SMACK file not empty with persistant mode 0. Errno: " << errno);
1725 result = perm_app_revoke_permissions(APP_ID);
1726 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1727 "Error revoking app permissions. Result: " << result);
1730 * Test - Enabling all permissions in two complementary files
1733 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_R_AND_NO_R, 1);
1734 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1735 " Error enabling app permissions. Result: " << result);
1737 // Check if the accesses are realy applied..
1738 result = test_have_all_accesses(rules2);
1739 RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1742 pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1743 RUNNER_ASSERT_MSG(pFile != NULL,
1744 "SMACK file NOT created!. Errno: " << errno);
1747 fseek(pFile, 0L, SEEK_END);
1748 smack_file_length = ftell(pFile);
1749 RUNNER_ASSERT_MSG(smack_file_length > 0,
1750 "SMACK file empty with persistant mode 1. Errno: " << errno);
1756 result = perm_app_revoke_permissions(APP_ID);
1757 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1758 "Error revoking app permissions. Result: " << result);
1761 * Test - Enabling some permissions and then enabling complementary permissions
1764 // Enable permission for rules 2 no r
1765 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1766 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1767 " Error enabling app permissions without r. Result: " << result);
1769 // Check if the accesses are realy applied..
1770 result = test_have_all_accesses(rules2_no_r);
1771 RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
1774 pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1775 RUNNER_ASSERT_MSG(pFile != NULL,
1776 "SMACK file NOT created!. Errno: " << errno);
1779 fseek(pFile, 0L, SEEK_END);
1780 smack_file_length = ftell(pFile);
1781 RUNNER_ASSERT_MSG(smack_file_length > 0,
1782 "SMACK file empty with persistant mode 1. Errno: " << errno);
1787 // Enable permission for rules 2
1788 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1789 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1790 " Error enabling app all permissions. Result: " << result);
1792 // Check if the accesses are realy applied..
1793 result = test_have_all_accesses(rules2);
1794 RUNNER_ASSERT_MSG(result == 1, "Permissions all not added.");
1797 result = perm_app_revoke_permissions(APP_ID);
1798 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1799 "Error revoking app permissions. Result: " << result);
1802 * Test - Enabling some permissions and then enabling all permissions
1805 // Enable permission for rules 2 no r
1806 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1807 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1808 " Error enabling app permissions without r. Result: " << result);
1810 // Check if the accesses are realy applied..
1811 result = test_have_all_accesses(rules2_no_r);
1812 RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
1815 pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1816 RUNNER_ASSERT_MSG(pFile != NULL,
1817 "SMACK file NOT created!. Errno: " << errno);
1820 fseek(pFile, 0L, SEEK_END);
1821 smack_file_length = ftell(pFile);
1822 RUNNER_ASSERT_MSG(smack_file_length > 0,
1823 "SMACK file empty with persistant mode 1. Errno: " << errno);
1828 // Enable permission for rules 2
1829 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_R, 1);
1830 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1831 " Error enabling app permissions with only r. Result: " << result);
1833 // Check if the accesses are realy applied..
1834 result = test_have_all_accesses(rules2);
1835 RUNNER_ASSERT_MSG(result == 1, "Permissions with only r not added.");
1838 result = perm_app_revoke_permissions(APP_ID);
1839 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1840 "Error revoking app permissions. Result: " << result);
1844 * Until perm_app_disable_permissions is not fixed this test should remain
1848 * Remove previously granted SMACK permissions based on permissions list.
1850 /*RUNNER_TEST(privilege_control12_app_disable_permissions)
1854 * Test - disable all granted permissions.
1858 // Prepare permissions that we want to disable
1859 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1860 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1861 " Error enabling app permissions. Result: " << result);
1863 // Disable permissions
1864 result = perm_app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1865 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1866 "Error disabling app permissions. Result: " << result);
1868 // Are all the permissions disabled?
1869 result = test_have_any_accesses(rules2);
1870 RUNNER_ASSERT_MSG(result!=1, "Not all permisions disabled.");
1873 * Test - disable some granted permissions leaving non complementary and then disabling those too.
1876 // Prepare permissions that will not be disabled
1877 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS, 1);
1878 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1879 " Error adding app first permissions. Result: " << result);
1881 // Prepare permissions that we want to disable
1882 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1883 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1884 " Error adding app second permissions. Result: " << result);
1886 // Disable second permissions
1887 result = perm_app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1888 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1889 "Error disabling app second permissions. Result: " << result);
1891 // Are all second permissions disabled?
1892 result = test_have_any_accesses(rules2);
1893 RUNNER_ASSERT_MSG(result!=1, "Not all first permisions disabled.");
1895 // Are all first permissions not disabled?
1896 result = test_have_all_accesses(rules);
1897 RUNNER_ASSERT_MSG(result==1, "Some of second permissions disabled.");
1899 // Disable first permissions
1900 result = perm_app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS);
1901 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1902 "Error disabling app first permissions. Result: " << result);
1904 // Are all second permissions disabled?
1905 result = test_have_any_accesses(rules);
1906 RUNNER_ASSERT_MSG(result!=1, "Not all second permisions disabled.");
1909 * Test - disable only no r granted permissions.
1912 // Prepare permissions
1913 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1914 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1915 " Error adding app permissions. Result: " << result);
1917 // Disable same permissions without r
1918 result = perm_app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R);
1919 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1920 "Error disabling app no r permissions. Result: " << result);
1922 // Is any r permissions disabled?
1923 result = test_have_all_accesses(rules2_r);
1924 RUNNER_ASSERT_MSG(result==1, "Some of r permissions disabled.");
1925 // Are all no r permissions disabled?
1926 result = test_have_any_accesses(rules2_no_r);
1927 RUNNER_ASSERT_MSG(result!=1, "Not all no r permissions disabled.");
1929 // Prepare permissions
1930 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1931 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1932 " Error adding app no r permissions. Result: " << result);
1934 // Disable all permissions
1935 result = perm_app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1936 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1937 "Error disabling app permissions. Result: " << result);
1941 * Reset SMACK permissions for an application by revoking all previously
1942 * granted rules and enabling them again from a rules file from disk.
1945 RUNNER_TEST(privilege_control13_app_reset_permissions)
1950 * Test - doing reset and checking if rules exist again.
1953 // Prepare permissions to reset
1954 result = perm_app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1955 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1956 " Error adding app permissions. Result: " << result);
1958 // Reset permissions
1959 result = perm_app_reset_permissions(APP_ID);
1960 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1961 "Error reseting app permissions. Result: " << result);
1963 // Are all second permissions not disabled?
1964 result = test_have_all_accesses(rules2);
1965 RUNNER_ASSERT_MSG(result == 1, "Not all permissions added.");
1967 // Disable permissions
1968 result = perm_app_revoke_permissions(APP_ID);
1969 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1970 "Error disabling app permissions. Result: " << result);
1974 * Make two applications "friends", by giving them both full permissions on
1977 RUNNER_TEST(privilege_control14_app_add_friend)
1982 * Test - making friends with no permissions on each other
1985 result = perm_app_revoke_permissions(APP_FRIEND_1);
1986 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1987 "Error revoking app permissions. Result: " << result);
1988 result = perm_app_revoke_permissions(APP_FRIEND_2);
1989 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1990 "Error revoking app permissions. Result: " << result);
1992 perm_app_uninstall(APP_FRIEND_1);
1993 perm_app_uninstall(APP_FRIEND_2);
1995 // Installing friends to be
1996 result = perm_app_install(APP_FRIEND_1);
1997 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1998 " Error installing first app. Result: " << result);
1999 result = perm_app_install(APP_FRIEND_2);
2000 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2001 " Error installing second app. Result: " << result);
2004 result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2005 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2006 " Error making friends. Errno: " << result);
2008 // Checking if friends were made
2009 result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
2010 RUNNER_ASSERT_MSG(result == 1,
2011 " Error first one sided friednship failed. Result: " << result);
2012 result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
2013 RUNNER_ASSERT_MSG(result == 1,
2014 " Error second one sided friednship failed. Result: " << result);
2017 result = perm_app_revoke_permissions(APP_FRIEND_1);
2018 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2019 "Error revoking app permissions. Result: " << result);
2020 result = perm_app_revoke_permissions(APP_FRIEND_2);
2021 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2022 "Error revoking app permissions. Result: " << result);
2024 perm_app_uninstall(APP_FRIEND_1);
2025 perm_app_uninstall(APP_FRIEND_2);
2028 * Test - making friends with nonexisting friend
2031 // Installing one friend
2032 result = perm_app_install(APP_FRIEND_1);
2033 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2034 " Error installing first app. Errno: " << result);
2036 // Adding imaginairy friend as second
2037 result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2038 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2039 " Error making friends (first) with imaginairy friend failed. Result: "
2041 // Adding imaginairy friend as first
2042 result = perm_app_add_friend(APP_FRIEND_2, APP_FRIEND_1);
2043 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2044 " Error making friends (second) with imaginairy friend failed. Result: "
2047 result = perm_app_revoke_permissions(APP_FRIEND_1);
2048 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2049 "Error revoking app permissions. Result: " << result);
2050 result = perm_app_revoke_permissions(APP_FRIEND_2);
2051 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2052 "Error revoking app permissions. Result: " << result);
2054 perm_app_uninstall(APP_FRIEND_1);
2055 perm_app_uninstall(APP_FRIEND_2);
2058 * Test - making friends with some permissions already added
2063 struct smack_accesses *rulesFriend = NULL;
2065 std::vector<std::string> accessesFriend =
2066 { "r", "w", "x", "rw", "rx", "wx", "rwx", "rwxat" };
2068 // Installing friends to be
2069 result = perm_app_install(APP_FRIEND_1);
2070 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2071 " Error installing first app. Result: " << result);
2072 result = perm_app_install(APP_FRIEND_2);
2073 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2074 " Error installing second app. Result: " << result);
2076 for (i = 0; i < accessesFriend.size(); ++i)
2078 for (j = 0; j < accessesFriend.size(); ++j)
2080 // Adding rules before making friends
2081 result = smack_accesses_new(&rulesFriend);
2082 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2083 "Error in smack_accesses_new. Result: " << result);
2085 result = smack_accesses_add(rulesFriend,
2086 APP_FRIEND_1, APP_FRIEND_2, accessesFriend[i].c_str());
2087 RUNNER_ASSERT_MSG(result == 0,
2088 "Unable to add modify rulesFirend (first). Result: " << result);
2089 result = smack_accesses_add(rulesFriend, APP_FRIEND_2,
2090 APP_FRIEND_1, accessesFriend[j].c_str());
2091 RUNNER_ASSERT_MSG(result == 0,
2092 "Unable to add modify rulesFirend (second). Result: " << result);
2094 result = smack_accesses_apply(rulesFriend);
2095 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2096 "Error in smack_accesses_apply. Result: " << result);
2099 result = perm_app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2100 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2101 " Error making friends. Result: " << result);
2103 // Checking if friends were made
2104 result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
2105 RUNNER_ASSERT_MSG(result == 1,
2106 " Error first one sided friednship failed. Result: " << result);
2107 result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
2108 RUNNER_ASSERT_MSG(result == 1,
2109 " Error second one sided friednship failed. Result: " << result);
2111 // Deleting all rules between friends
2112 smack_accesses_add_modify(rulesFriend,
2113 APP_FRIEND_1, APP_FRIEND_2,"","rwxat");
2114 smack_accesses_add_modify(rulesFriend,
2115 APP_FRIEND_2, APP_FRIEND_1,"","rwxat");
2117 result = smack_accesses_apply(rulesFriend);
2119 smack_accesses_free(rulesFriend);
2125 result = perm_app_revoke_permissions(APP_FRIEND_1);
2126 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2127 "Error revoking app permissions. Result: " << result);
2128 result = perm_app_revoke_permissions(APP_FRIEND_2);
2129 RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2130 "Error revoking app permissions. Result: " << result);
2132 perm_app_uninstall(APP_FRIEND_1);
2133 perm_app_uninstall(APP_FRIEND_2);
2136 static void smack_set_random_label_based_on_pid_on_self(void)
2139 std::stringstream ss;
2141 ss << "s-" << getpid() << "-" << getppid();
2142 result = smack_set_label_for_self(ss.str().c_str());
2143 RUNNER_ASSERT_MSG(result == 0, "smack_set_label_for_self("
2144 << ss.str().c_str() << ") failed");
2147 static void smack_unix_sock_server(int sock)
2153 fd = accept(sock, NULL, NULL);
2157 result = smack_new_label_from_self(&smack_label);
2162 RUNNER_ASSERT_MSG(0, "smack_new_label_from_self() failed");
2164 result = write(fd, smack_label, strlen(smack_label));
2165 if (result != (int)strlen(smack_label)) {
2169 RUNNER_ASSERT_MSG(0, "write() failed: " << strerror(errno));
2175 RUNNER_TEST(privilege_control15_app_id_from_socket)
2178 struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
2182 RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
2184 smack_set_random_label_based_on_pid_on_self();
2186 if (!pid) { /* child process, server */
2189 /* Set the process label before creating a socket */
2190 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2191 RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
2193 (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
2196 RUNNER_ASSERT_MSG(0, "bind failed: " << strerror(errno));
2198 result = listen(sock, 1);
2201 RUNNER_ASSERT_MSG(0, "listen failed: " << strerror(errno));
2203 smack_unix_sock_server(sock);
2205 /* Change the process label with listening socket */
2206 smack_unix_sock_server(sock);
2209 RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
2210 /* Now running two concurrent servers.
2211 Test if socket label was unaffected by fork() */
2212 smack_unix_sock_server(sock);
2213 /* Let's give the two servers different labels */
2214 smack_unix_sock_server(sock);
2216 waitpid(pid, NULL, 0);
2218 } else { /* parent process, client */
2219 sleep(1); /* Give server some time to setup listening socket */
2221 for (i = 0; i < 4; ++i) {
2224 char smack_label1[SMACK_LABEL_LEN + 1];
2227 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2228 RUNNER_ASSERT_MSG(sock >= 0,
2229 "socket failed: " << strerror(errno));
2230 result = connect(sock,
2231 (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
2234 RUNNER_ASSERT_MSG(0, "connect failed: " << strerror(errno));
2238 result = read(sock, smack_label1, SMACK_LABEL_LEN);
2242 RUNNER_ASSERT_MSG(0, "read failed: " << strerror(errno));
2244 smack_label1[result] = '\0';
2245 smack_label2 = perm_app_id_from_socket(sock);
2246 if (smack_label2 == NULL) {
2248 RUNNER_ASSERT_MSG(0, "perm_app_id_from_socket failed");
2250 result = strcmp(smack_label1, smack_label2);
2253 RUNNER_ASSERT_MSG(0, "smack labels differ: '" << smack_label1
2254 << "' != '" << smack_label2 << "-" << random() << "'");
2258 waitpid(pid, NULL, 0);
2262 RUNNER_TEST(privilege_control16_app_setup_path){
2263 const char *path1 = "/usr/share/privilege-control/app_setup_access_test";
2264 const char *path2 = "/usr/share/privilege-control/app_setup_access_test/directory";
2265 const char *path3 = "/usr/share/privilege-control/app_setup_access_test/one";
2266 const char *path4 = "/usr/share/privilege-control/app_setup_access_test/directory/two";
2267 const char *label1 = "qwert123456za";
2268 const char *label2 = "trewq654123az";
2270 std::unique_ptr<char, std::function<void(void*)> > labelPtr(NULL,free);
2275 int fd = creat(path3, S_IRWXU);
2278 fd = creat(path4, S_IRWXU);
2284 RUNNER_ASSERT(PC_OPERATION_SUCCESS == perm_app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label1));
2285 RUNNER_ASSERT(0 == smack_lgetlabel(path3, &label, SMACK_LABEL_ACCESS));
2286 labelPtr.reset(label);
2288 RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label1));
2290 RUNNER_ASSERT(PC_OPERATION_SUCCESS == perm_app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label2));
2291 RUNNER_ASSERT(0 == smack_lgetlabel(path4, &label, SMACK_LABEL_EXEC));
2292 labelPtr.reset(label);
2294 RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label2));
2296 RUNNER_ASSERT(0 == smack_lgetlabel(path1, &label, SMACK_LABEL_EXEC));
2297 labelPtr.reset(label);
2299 RUNNER_ASSERT(labelPtr.get() == NULL);
2302 RUNNER_TEST(privilege_control17_appsettings_privilege)
2304 #define APP_1 "app_1"
2305 #define APP_1_DIR "/tmp/app_1"
2307 #define APP_2 "app_2"
2308 #define APP_2_DIR "/tmp/app_2"
2310 #define APP_TEST "app_test"
2312 #define PRIV_APPSETTING (const char*[]) {"http://tizen.org/privilege/appsetting", NULL}
2315 char *app1_dir_label;
2316 char *app2_dir_label;
2320 (void)perm_app_uninstall(APP_TEST);
2321 (void)perm_app_uninstall(APP_1);
2322 (void)perm_app_uninstall(APP_2);
2324 //install some app 1
2325 ret = perm_app_install(APP_1);
2326 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install." << ret);
2328 mkdir(APP_1_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
2330 //register settings folder for app 1
2331 ret = perm_app_setup_path(APP_1, APP_1_DIR, APP_PATH_SETTINGS_RW );
2332 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
2334 //install "app_test" and give it appsettings privilege
2335 ret = perm_app_install(APP_TEST);
2336 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
2339 ret = perm_app_enable_permissions(APP_TEST, APP_TYPE_OSP, PRIV_APPSETTING, true);
2341 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,
2342 " Error enabling app permissions. Result: " << ret);
2344 //check if "app_test" has an RX access to the app "app_1"
2345 ret = smack_have_access(APP_TEST, APP_1, "rx");
2346 RUNNER_ASSERT_MSG(ret,"access denies");
2349 //check if "app_test" has an RWX access to a folder registered by "app_1"
2350 ret = smack_getlabel(APP_1_DIR, &app1_dir_label, SMACK_LABEL_ACCESS );
2351 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
2352 ret = smack_have_access(APP_TEST, app1_dir_label, "rwx");
2353 RUNNER_ASSERT_MSG(ret,"access denies");
2356 //intstall another app: "app_2"
2357 ret = perm_app_install(APP_2);
2358 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
2360 mkdir(APP_2_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
2361 //register settings folder for that "app_2"
2362 ret = perm_app_setup_path(APP_2, APP_2_DIR, APP_PATH_SETTINGS_RW );
2363 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
2365 //check if "app_test" has an RX access to the app "app_2"
2366 ret = smack_have_access(APP_TEST, APP_2, "rx");
2367 RUNNER_ASSERT_MSG(ret,"access denies");
2369 //check if "app_test" has an RWX access to a folder registered by "app_2"
2370 ret = smack_getlabel(APP_2_DIR, &app2_dir_label, SMACK_LABEL_ACCESS );
2371 RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
2372 ret = smack_have_access(APP_TEST, app2_dir_label, "rwx");
2373 RUNNER_ASSERT_MSG(ret,"access denies");
2375 free (app1_dir_label);
2376 free (app2_dir_label);
2380 (void)perm_app_uninstall(APP_TEST);
2381 (void)perm_app_uninstall(APP_1);
2382 (void)perm_app_uninstall(APP_2);
2385 RUNNER_TEST(privilege_control18_app_setup_path_public)
2389 cleaning_smack_database_files();
2392 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
2393 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
2395 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2396 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
2398 result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_PUBLIC_RO);
2399 RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
2401 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
2402 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
2404 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2405 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
2407 cleaning_smack_database_files();
2410 RUNNER_TEST(privilege_control19_app_setup_path_settings)
2414 cleaning_smack_database_files();
2417 result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
2418 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
2420 result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2421 RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
2423 result = perm_app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_SETTINGS_RW);
2424 RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path() failed");
2426 result = nftw(TEST_APP_DIR, &nftw_check_labels_app_settings_dir, FTW_MAX_FDS, FTW_PHYS);
2427 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
2429 result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2430 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
2432 cleaning_smack_database_files();
2435 RUNNER_TEST(privilege_control20_early_rules)
2441 char *single_line_format = NULL;
2444 char subject[SMACK_LABEL_LEN + 1];
2445 char object[SMACK_LABEL_LEN + 1];
2446 char rule_add[6]; // "rwxat" + '\0'
2447 char rule_remove[6]; // "rwxat" + '\0'
2449 subject[SMACK_LABEL_LEN] = '\0';
2450 object[SMACK_LABEL_LEN] = '\0';
2452 rule_remove[5] = '\0';
2454 unlink(SMACK_RULES_DIR APP_ID);
2456 perm_app_uninstall(APP_ID);
2458 result = perm_app_install(APP_ID);
2459 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
2460 result = perm_app_install(APP_TEST_APP_1);
2461 RUNNER_ASSERT_MSG(result == 0, "perm_app_install returned " << result << ". Errno: " << strerror(errno));
2463 // checking if file really exists
2464 fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
2466 RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
2469 result = perm_app_enable_permissions(APP_ID, APP_TYPE_WGT, (const char**) &perm, 1);
2470 RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
2471 result = perm_app_enable_permissions(APP_TEST_APP_1, APP_TYPE_WGT, (const char**) &perm, 1);
2472 RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
2474 file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2475 RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2477 result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2479 while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2480 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2481 pass_1 = 1; // Found rule for APP_ID
2484 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2485 pass_2 = 1; // Found rule for APP_TEST_APP_1
2489 free(single_line_format);
2490 single_line_format = NULL;
2494 RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " not found");
2495 RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
2497 // Checking if "early rule" for APP_ID was really removed
2498 // We also should make sure that "early rules" for other apps wasn't removed
2499 result = perm_app_uninstall(APP_ID);
2500 RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
2504 file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2505 RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2507 result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2509 while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2510 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2511 pass_1 = 0; // Found rule for APP_ID - it should NOT be here
2514 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2515 pass_2 = 1; // Found rule for APP_TEST_APP_1
2519 free(single_line_format);
2520 single_line_format = NULL;
2524 RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
2525 RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
2527 // Removing and checking "early rule" for APP_TEST_APP_1
2528 result = perm_app_uninstall(APP_TEST_APP_1);
2529 RUNNER_ASSERT_MSG(result == 0, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
2533 file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2534 RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2536 result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2538 while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2539 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2540 pass_1 = 0; // Found rule for APP_ID - it should NOT be here
2543 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2544 pass_2 = 0; // Found rule for APP_TEST_APP_1 - it should NOT be here
2548 free(single_line_format);
2549 single_line_format = NULL;
2553 RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
2554 RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " found");