15
15
16
16
#include " src/starnix/tests/selinux/userspace/util.h"
17
17
18
- // TODO: Pretty-print without polluting the global namespace!
19
- namespace fit {
20
- void PrintTo (const fit::result<int , std::string>& value, std::ostream* os) {
21
- if (value.is_ok ()) {
22
- *os << " Ok(\" " << value.value () << " \" )" ;
23
- } else {
24
- *os << " Errno(" << value.error_value () << " )" ;
25
- }
26
- }
27
- } // namespace fit
28
-
29
18
namespace {
30
19
31
20
using ValidateContextResult = fit::result<int , std::string>;
32
21
33
- ValidateContextResult validate_context (std::string_view context) {
22
+ ValidateContextResult ValidateContext (std::string_view context) {
34
23
constexpr char context_api_path[] = " /sys/fs/selinux/context" ;
35
24
fbl::unique_fd context_api (open (context_api_path, O_RDWR));
36
25
if (!context_api.is_valid ()) {
@@ -45,7 +34,7 @@ ValidateContextResult validate_context(std::string_view context) {
45
34
ssize_t result = read (context_api.get (), read_buf, sizeof (read_buf));
46
35
if (result == 0 ) {
47
36
// Use `c_str()` to strip the trailing NUL, if any, from the read context.
48
- return fit::ok (validated_context. c_str ( ));
37
+ return fit::ok (RemoveTrailingNul (validated_context ));
49
38
}
50
39
if (result < 0 ) {
51
40
return fit::error (errno);
@@ -60,14 +49,14 @@ TEST(SeLinuxFsContext, ValidatesRequiredFieldsPresent) {
60
49
LoadPolicy (" selinuxfs_policy.pp" );
61
50
62
51
// Contexts that have too few colons to provide user, role, type & sensitivity are rejected.
63
- EXPECT_EQ (validate_context (" test_selinuxfs_u" ), fit::failed ());
64
- EXPECT_EQ (validate_context (" test_selinuxfs_u:test_selinuxfs_r" ), fit::failed ());
65
- EXPECT_EQ (validate_context (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t" ), fit::failed ());
52
+ EXPECT_EQ (ValidateContext (" test_selinuxfs_u" ), fit::failed ());
53
+ EXPECT_EQ (ValidateContext (" test_selinuxfs_u:test_selinuxfs_r" ), fit::failed ());
54
+ EXPECT_EQ (ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t" ), fit::failed ());
66
55
67
56
// The minimum valid context has at least user, role, type and low/default sensitivity.
68
57
constexpr std::string_view kMinimumValidContext =
69
58
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0" ;
70
- EXPECT_EQ (validate_context (kMinimumValidContext ), expect_ok (kMinimumValidContext ));
59
+ EXPECT_EQ (ValidateContext (kMinimumValidContext ), expect_ok (kMinimumValidContext ));
71
60
}
72
61
73
62
TEST (SeLinuxFsContext, ValidatesFieldValues) {
@@ -76,34 +65,34 @@ TEST(SeLinuxFsContext, ValidatesFieldValues) {
76
65
// Valid contexts are successfully written, and can be read-back.
77
66
constexpr std::string_view kValidContext =
78
67
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.c2" ;
79
- EXPECT_EQ (validate_context (kValidContext ), expect_ok (kValidContext ));
68
+ EXPECT_EQ (ValidateContext (kValidContext ), expect_ok (kValidContext ));
80
69
81
70
// Context user must be defined by the policy.
82
- EXPECT_EQ (validate_context (" bad_value:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.c2" ),
71
+ EXPECT_EQ (ValidateContext (" bad_value:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.c2" ),
83
72
fit::failed ());
84
73
85
74
// Context role must be defined by the policy.
86
- EXPECT_EQ (validate_context (" test_selinuxfs_u:bad_value:test_selinuxfs_t:s0:c0-s2:c0.c2" ),
75
+ EXPECT_EQ (ValidateContext (" test_selinuxfs_u:bad_value:test_selinuxfs_t:s0:c0-s2:c0.c2" ),
87
76
fit::failed ());
88
77
89
78
// Context type/domain must be defined by the policy.
90
- EXPECT_EQ (validate_context (" test_selinuxfs_u:test_selinuxfs_r:bad_value:s0:c0-s2:c0.c2" ),
79
+ EXPECT_EQ (ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:bad_value:s0:c0-s2:c0.c2" ),
91
80
fit::failed ());
92
81
93
82
// Context low & high sensitivities must be defined by the policy.
94
83
EXPECT_EQ (
95
- validate_context (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:bad_value:c0-s2:c0.c2" ),
84
+ ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:bad_value:c0-s2:c0.c2" ),
96
85
fit::failed ());
97
86
EXPECT_EQ (
98
- validate_context (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-bad_value:c0.c2" ),
87
+ ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-bad_value:c0.c2" ),
99
88
fit::failed ());
100
89
101
90
// Context low & high categories must be defined by the policy.
102
91
EXPECT_EQ (
103
- validate_context (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:bad_value-s2:c0.c2" ),
92
+ ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:bad_value-s2:c0.c2" ),
104
93
fit::failed ());
105
94
EXPECT_EQ (
106
- validate_context (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.bad_value" ),
95
+ ValidateContext (" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.bad_value" ),
107
96
fit::failed ());
108
97
}
109
98
@@ -113,21 +102,21 @@ TEST(SeLinuxFsContext, ValidatesAllowedUserFieldValues) {
113
102
// The "test_selinuxfs_u" user is granted the full range of categories.
114
103
constexpr std::string_view kValidContext =
115
104
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0-s2:c0.c2" ;
116
- EXPECT_EQ (validate_context (kValidContext ), expect_ok (kValidContext ));
105
+ EXPECT_EQ (ValidateContext (kValidContext ), expect_ok (kValidContext ));
117
106
118
107
// The "test_selinuxfs_limited_u" user is granted only "s0" sensitivity, must have "c0" category
119
108
// and may have "c1" category.
120
109
constexpr std::string_view kLimitedContext_Valid =
121
110
" test_selinuxfs_limited_level_u:test_selinuxfs_r:test_selinuxfs_t:s0:c0" ;
122
- EXPECT_EQ (validate_context (kLimitedContext_Valid ), expect_ok (kLimitedContext_Valid ));
111
+ EXPECT_EQ (ValidateContext (kLimitedContext_Valid ), expect_ok (kLimitedContext_Valid ));
123
112
124
113
constexpr std::string_view kLimitedContext_MissingCategory =
125
114
" test_selinuxfs_limited_level_u:test_selinuxfs_r:test_selinuxfs_t:s0" ;
126
- EXPECT_EQ (validate_context (kLimitedContext_MissingCategory ), fit::failed ());
115
+ EXPECT_EQ (ValidateContext (kLimitedContext_MissingCategory ), fit::failed ());
127
116
128
117
constexpr std::string_view kLimitedContext_BadSensitivity =
129
118
" test_selinuxfs_limited_level_u:test_selinuxfs_r:test_selinuxfs_t:s1:c0" ;
130
- EXPECT_EQ (validate_context (kLimitedContext_BadSensitivity ), fit::failed ());
119
+ EXPECT_EQ (ValidateContext (kLimitedContext_BadSensitivity ), fit::failed ());
131
120
}
132
121
133
122
TEST (SeLinuxFsContext, NormalizeCategories) {
@@ -139,15 +128,15 @@ TEST(SeLinuxFsContext, NormalizeCategories) {
139
128
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s2:c0.c2" ;
140
129
constexpr std::string_view kThreeCategoryContextFormB =
141
130
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s2:c0,c1,c2" ;
142
- EXPECT_EQ (validate_context (kThreeCategoryContextFormA ),
143
- validate_context (kThreeCategoryContextFormB ));
131
+ EXPECT_EQ (ValidateContext (kThreeCategoryContextFormA ),
132
+ ValidateContext (kThreeCategoryContextFormB ));
144
133
145
134
// Using a pair of categories results in the same Security Context as a two-element range.
146
135
constexpr std::string_view kTwoCategoryContextFormA =
147
136
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s2:c0.c1" ;
148
137
constexpr std::string_view kTwoCategoryContextFormB =
149
138
" test_selinuxfs_u:test_selinuxfs_r:test_selinuxfs_t:s2:c0,c1" ;
150
- EXPECT_EQ (validate_context (kTwoCategoryContextFormA ), validate_context (kTwoCategoryContextFormB ));
139
+ EXPECT_EQ (ValidateContext (kTwoCategoryContextFormA ), ValidateContext (kTwoCategoryContextFormB ));
151
140
}
152
141
153
142
} // namespace
0 commit comments