| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "test-downgrade.h" |
| 22 |
#include "parse_util.h" |
| 23 |
#include "helpers.h" |
| 24 |
|
| 25 |
#include <sepol/debug.h> |
| 26 |
#include <sepol/handle.h> |
| 27 |
#include <sepol/policydb/policydb.h> |
| 28 |
#include <sepol/policydb/link.h> |
| 29 |
#include <sepol/policydb/expand.h> |
| 30 |
#include <sepol/policydb/conditional.h> |
| 31 |
#include <limits.h> |
| 32 |
#include <CUnit/Basic.h> |
| 33 |
|
| 34 |
#define POLICY_BIN_HI "policies/test-downgrade/policy.hi" |
| 35 |
#define POLICY_BIN_LO "policies/test-downgrade/policy.lo" |
| 36 |
|
| 37 |
static policydb_t policydb; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
int downgrade_test_init(void) |
| 49 |
{ |
| 50 |
|
| 51 |
if (policydb_init(&policydb)) { |
| 52 |
fprintf(stderr, "%s: Out of memory!\n", __FUNCTION__); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
int downgrade_test_cleanup(void) |
| 69 |
{ |
| 70 |
policydb_destroy(&policydb); |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
int downgrade_add_tests(CU_pSuite suite) |
| 85 |
{ |
| 86 |
if (CU_add_test(suite, "downgrade", test_downgrade) == NULL) |
| 87 |
return CU_get_error(); |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void test_downgrade(void) |
| 103 |
{ |
| 104 |
if (do_downgrade_test(0) < 0) |
| 105 |
fprintf(stderr, |
| 106 |
"\nError during downgrade testing of Non-MLS policy\n"); |
| 107 |
|
| 108 |
|
| 109 |
if (do_downgrade_test(1) < 0) |
| 110 |
fprintf(stderr, |
| 111 |
"\nError during downgrade testing of MLS policy\n"); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int do_downgrade_test(int mls) |
| 128 |
{ |
| 129 |
policydb_t policydb_tmp; |
| 130 |
int hi, lo, version; |
| 131 |
|
| 132 |
|
| 133 |
policydb_destroy(&policydb); |
| 134 |
downgrade_test_init(); |
| 135 |
|
| 136 |
|
| 137 |
if (read_binary_policy(POLICY_BIN_HI, &policydb) != 0) { |
| 138 |
fprintf(stderr, "error reading %spolicy binary\n", mls ? "mls " : ""); |
| 139 |
CU_FAIL("Unable to read the binary policy"); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
policydb.mls = mls ? 1 : 0; |
| 145 |
|
| 146 |
for (hi = policydb.policyvers; hi >= POLICYDB_VERSION_MIN; hi--) { |
| 147 |
|
| 148 |
version = policydb.policyvers; |
| 149 |
|
| 150 |
|
| 151 |
for (lo = hi - 1; lo >= POLICYDB_VERSION_MIN; lo--) { |
| 152 |
|
| 153 |
|
| 154 |
policydb.policyvers = lo; |
| 155 |
|
| 156 |
|
| 157 |
if (write_binary_policy(POLICY_BIN_LO, &policydb) != 0) { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if (mls && lo < POLICYDB_VERSION_MLS) |
| 163 |
continue; |
| 164 |
|
| 165 |
fprintf(stderr, "error writing %spolicy binary, version %d (downgraded from %d)\n", mls ? "mls " : "", lo, hi); |
| 166 |
CU_FAIL("Failed to write downgraded binary policy"); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if (policydb_init(&policydb_tmp)) { |
| 172 |
fprintf(stderr, "%s: Out of memory!\n", |
| 173 |
__FUNCTION__); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
if (read_binary_policy(POLICY_BIN_LO, &policydb_tmp) != 0) { |
| 177 |
fprintf(stderr, "error reading %spolicy binary, version %d (downgraded from %d)\n", mls ? "mls " : "", lo, hi); |
| 178 |
CU_FAIL("Unable to read downgraded binary policy"); |
| 179 |
return -1; |
| 180 |
} |
| 181 |
policydb_destroy(&policydb_tmp); |
| 182 |
} |
| 183 |
|
| 184 |
policydb.policyvers = version; |
| 185 |
} |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int read_binary_policy(const char *path, policydb_t *p) |
| 204 |
{ |
| 205 |
FILE *in_fp = NULL; |
| 206 |
struct policy_file f; |
| 207 |
int rc; |
| 208 |
|
| 209 |
|
| 210 |
if ((in_fp = fopen(path, "rb")) == NULL) { |
| 211 |
fprintf(stderr, "Unable to open %s: %s\n", path, |
| 212 |
strerror(errno)); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
memset(&f, 0, sizeof(struct policy_file)); |
| 218 |
f.type = PF_USE_STDIO; |
| 219 |
f.fp = in_fp; |
| 220 |
rc = policydb_read(p, &f, 0); |
| 221 |
|
| 222 |
fclose(in_fp); |
| 223 |
return rc; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int write_binary_policy(const char *path, policydb_t *p) |
| 239 |
{ |
| 240 |
FILE *out_fp = NULL; |
| 241 |
struct policy_file f; |
| 242 |
sepol_handle_t *handle; |
| 243 |
int rc; |
| 244 |
|
| 245 |
|
| 246 |
handle = sepol_handle_create(); |
| 247 |
if (handle == NULL) { |
| 248 |
fprintf(stderr, "Out of memory!\n"); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
sepol_msg_set_callback(handle, NULL, NULL); |
| 252 |
|
| 253 |
|
| 254 |
if ((out_fp = fopen(path, "w" )) == NULL) { |
| 255 |
fprintf(stderr, "Unable to open %s: %s\n", path, |
| 256 |
strerror(errno)); |
| 257 |
sepol_handle_destroy(handle); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
memset(&f, 0, sizeof(struct policy_file)); |
| 263 |
f.type = PF_USE_STDIO; |
| 264 |
f.fp = out_fp; |
| 265 |
f.handle = handle; |
| 266 |
rc = policydb_write(p, &f); |
| 267 |
|
| 268 |
sepol_handle_destroy(f.handle); |
| 269 |
fclose(out_fp); |
| 270 |
return rc; |
| 271 |
} |