Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 1 | |
| 2 | /* |
Stephen Smalley | 53bb2a1 | 2017-08-17 14:16:06 -0400 | [diff] [blame] | 3 | * Author : Stephen Smalley, <sds@tycho.nsa.gov> |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> |
| 8 | * |
| 9 | * Support for enhanced MLS infrastructure. |
| 10 | * |
| 11 | * Updated: Karl MacMillan <kmacmillan@tresys.com> |
| 12 | * |
| 13 | * Added conditional policy language extensions |
| 14 | * |
| 15 | * Updated: James Morris <jmorris@intercode.com.au> |
| 16 | * |
| 17 | * Added IPv6 support. |
| 18 | * |
| 19 | * Updated: Joshua Brindle <jbrindle@tresys.com> |
| 20 | * Karl MacMillan <kmacmillan@tresys.com> |
| 21 | * Jason Tang <jtang@tresys.com> |
| 22 | * |
| 23 | * Policy Module support. |
| 24 | * |
Daniel Jurgens | 9fbb311 | 2017-05-22 16:08:24 +0300 | [diff] [blame] | 25 | * Copyright (C) 2017 Mellanox Technologies Inc. |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 26 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
| 27 | * Copyright (C) 2003 - 2005 Tresys Technology, LLC |
| 28 | * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation, version 2. |
| 32 | */ |
| 33 | |
| 34 | /* FLASK */ |
| 35 | |
| 36 | /* |
| 37 | * checkpolicy |
| 38 | * |
| 39 | * Load and check a policy configuration. |
| 40 | * |
| 41 | * A policy configuration is created in a text format, |
| 42 | * and then compiled into a binary format for use by |
| 43 | * the security server. By default, checkpolicy reads |
| 44 | * the text format. If '-b' is specified, then checkpolicy |
| 45 | * reads the binary format instead. |
| 46 | * |
| 47 | * If '-o output_file' is specified, then checkpolicy |
| 48 | * writes the binary format version of the configuration |
| 49 | * to the specified output file. |
| 50 | * |
| 51 | * If '-d' is specified, then checkpolicy permits the user |
| 52 | * to interactively test the security server functions with |
| 53 | * the loaded policy configuration. |
| 54 | * |
| 55 | * If '-c' is specified, then the supplied parameter is used to |
| 56 | * determine which policy version to use for generating binary |
| 57 | * policy. This is for compatibility with older kernels. If any |
| 58 | * booleans or conditional rules are thrown away a warning is printed. |
| 59 | */ |
| 60 | |
Nicolas Iooss | 61f760b | 2017-01-08 19:45:52 +0100 | [diff] [blame] | 61 | #include <ctype.h> |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 62 | #include <getopt.h> |
| 63 | #include <unistd.h> |
| 64 | #include <stdlib.h> |
| 65 | #include <sys/types.h> |
| 66 | #include <sys/stat.h> |
| 67 | #include <sys/socket.h> |
| 68 | #include <netinet/in.h> |
Richard Haines | aac9360 | 2016-04-24 10:34:47 +0100 | [diff] [blame] | 69 | #ifndef IPPROTO_DCCP |
| 70 | #define IPPROTO_DCCP 33 |
| 71 | #endif |
Richard Haines via Selinux | cf0ab12 | 2018-03-11 16:22:55 +0000 | [diff] [blame^] | 72 | #ifndef IPPROTO_SCTP |
| 73 | #define IPPROTO_SCTP 132 |
| 74 | #endif |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 75 | #include <arpa/inet.h> |
| 76 | #include <fcntl.h> |
| 77 | #include <stdio.h> |
| 78 | #include <errno.h> |
| 79 | #include <sys/mman.h> |
| 80 | |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 81 | #include <sepol/module_to_cil.h> |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 82 | #include <sepol/kernel_to_cil.h> |
| 83 | #include <sepol/kernel_to_conf.h> |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 84 | #include <sepol/policydb/policydb.h> |
| 85 | #include <sepol/policydb/services.h> |
| 86 | #include <sepol/policydb/conditional.h> |
| 87 | #include <sepol/policydb/hierarchy.h> |
| 88 | #include <sepol/policydb/flask.h> |
| 89 | #include <sepol/policydb/expand.h> |
| 90 | #include <sepol/policydb/link.h> |
| 91 | |
| 92 | #include "queue.h" |
| 93 | #include "checkpolicy.h" |
| 94 | #include "parse_util.h" |
| 95 | |
| 96 | extern char *optarg; |
| 97 | extern int optind; |
| 98 | |
| 99 | static policydb_t policydb; |
| 100 | static sidtab_t sidtab; |
| 101 | |
| 102 | extern policydb_t *policydbp; |
| 103 | extern int mlspol; |
| 104 | |
| 105 | static int handle_unknown = SEPOL_DENY_UNKNOWN; |
Nicolas Iooss | 7dcb7a5 | 2014-09-14 23:41:46 +0200 | [diff] [blame] | 106 | static const char *txtfile = "policy.conf"; |
| 107 | static const char *binfile = "policy"; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 108 | |
| 109 | unsigned int policyvers = POLICYDB_VERSION_MAX; |
| 110 | |
Nicolas Iooss | ef61dd7 | 2017-03-05 18:13:01 +0100 | [diff] [blame] | 111 | static __attribute__((__noreturn__)) void usage(const char *progname) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 112 | { |
| 113 | printf |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 114 | ("usage: %s [-b[F]] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M]" |
Joshua Brindle | f830d96 | 2009-10-14 15:49:25 -0400 | [diff] [blame] | 115 | "[-c policyvers (%d-%d)] [-o output_file] [-t target_platform (selinux,xen)]" |
Paul Nuzzi | 79d10a8 | 2009-09-29 10:06:26 -0400 | [diff] [blame] | 116 | "[input_file]\n", |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 117 | progname, POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX); |
| 118 | exit(1); |
| 119 | } |
| 120 | |
| 121 | #define FGETS(out, size, in) \ |
| 122 | if (fgets(out,size,in)==NULL) { \ |
| 123 | fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,\ |
| 124 | strerror(errno)); \ |
| 125 | exit(1);\ |
| 126 | } |
| 127 | static int print_sid(sepol_security_id_t sid, |
| 128 | context_struct_t * context |
| 129 | __attribute__ ((unused)), void *data |
| 130 | __attribute__ ((unused))) |
| 131 | { |
| 132 | sepol_security_context_t scontext; |
| 133 | size_t scontext_len; |
| 134 | int rc; |
| 135 | |
| 136 | rc = sepol_sid_to_context(sid, &scontext, &scontext_len); |
| 137 | if (rc) |
| 138 | printf("sid %d -> error %d\n", sid, rc); |
| 139 | else { |
| 140 | printf("sid %d -> scontext %s\n", sid, scontext); |
| 141 | free(scontext); |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | struct val_to_name { |
| 147 | unsigned int val; |
| 148 | char *name; |
| 149 | }; |
| 150 | |
| 151 | static int find_perm(hashtab_key_t key, hashtab_datum_t datum, void *p) |
| 152 | { |
| 153 | struct val_to_name *v = p; |
| 154 | perm_datum_t *perdatum; |
| 155 | |
| 156 | perdatum = (perm_datum_t *) datum; |
| 157 | |
| 158 | if (v->val == perdatum->s.value) { |
| 159 | v->name = key; |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | #ifdef EQUIVTYPES |
| 167 | static int insert_type_rule(avtab_key_t * k, avtab_datum_t * d, |
| 168 | struct avtab_node *type_rules) |
| 169 | { |
| 170 | struct avtab_node *p, *c, *n; |
| 171 | |
| 172 | for (p = type_rules, c = type_rules->next; c; p = c, c = c->next) { |
| 173 | /* |
| 174 | * Find the insertion point, keeping the list |
| 175 | * ordered by source type, then target type, then |
| 176 | * target class. |
| 177 | */ |
| 178 | if (k->source_type < c->key.source_type) |
| 179 | break; |
| 180 | if (k->source_type == c->key.source_type && |
| 181 | k->target_type < c->key.target_type) |
| 182 | break; |
| 183 | if (k->source_type == c->key.source_type && |
| 184 | k->target_type == c->key.target_type && |
| 185 | k->target_class < c->key.target_class) |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | /* Insert the rule */ |
| 190 | n = malloc(sizeof(struct avtab_node)); |
| 191 | if (!n) { |
| 192 | fprintf(stderr, "out of memory\n"); |
| 193 | exit(1); |
| 194 | } |
| 195 | |
| 196 | n->key = *k; |
| 197 | n->datum = *d; |
| 198 | n->next = p->next; |
| 199 | p->next = n; |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int create_type_rules(avtab_key_t * k, avtab_datum_t * d, void *args) |
| 204 | { |
| 205 | struct avtab_node *type_rules = args; |
| 206 | |
| 207 | if (d->specified & AVTAB_ALLOWED) { |
| 208 | /* |
| 209 | * Insert the rule into the lists for both |
| 210 | * the source type and the target type. |
| 211 | */ |
| 212 | if (insert_type_rule(k, d, &type_rules[k->source_type - 1])) |
| 213 | return -1; |
| 214 | if (insert_type_rule(k, d, &type_rules[k->target_type - 1])) |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static void free_type_rules(struct avtab_node *l) |
| 222 | { |
| 223 | struct avtab_node *tmp; |
| 224 | |
| 225 | while (l) { |
| 226 | tmp = l; |
| 227 | l = l->next; |
| 228 | free(tmp); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static int identify_equiv_types(void) |
| 233 | { |
| 234 | struct avtab_node *type_rules, *l1, *l2; |
| 235 | int i, j; |
| 236 | |
| 237 | /* |
| 238 | * Create a list of access vector rules for each type |
| 239 | * from the access vector table. |
| 240 | */ |
| 241 | type_rules = malloc(sizeof(struct avtab_node) * policydb.p_types.nprim); |
| 242 | if (!type_rules) { |
| 243 | fprintf(stderr, "out of memory\n"); |
| 244 | exit(1); |
| 245 | } |
| 246 | memset(type_rules, 0, |
| 247 | sizeof(struct avtab_node) * policydb.p_types.nprim); |
| 248 | if (avtab_map(&policydb.te_avtab, create_type_rules, type_rules)) |
| 249 | exit(1); |
| 250 | |
| 251 | /* |
| 252 | * Compare the type lists and identify equivalent types. |
| 253 | */ |
| 254 | for (i = 0; i < policydb.p_types.nprim - 1; i++) { |
| 255 | if (!type_rules[i].next) |
| 256 | continue; |
| 257 | for (j = i + 1; j < policydb.p_types.nprim; j++) { |
| 258 | for (l1 = type_rules[i].next, l2 = type_rules[j].next; |
| 259 | l1 && l2; l1 = l1->next, l2 = l2->next) { |
| 260 | if (l2->key.source_type == (j + 1)) { |
| 261 | if (l1->key.source_type != (i + 1)) |
| 262 | break; |
| 263 | } else { |
| 264 | if (l1->key.source_type != |
| 265 | l2->key.source_type) |
| 266 | break; |
| 267 | } |
| 268 | if (l2->key.target_type == (j + 1)) { |
| 269 | if (l1->key.target_type != (i + 1)) |
| 270 | break; |
| 271 | } else { |
| 272 | if (l1->key.target_type != |
| 273 | l2->key.target_type) |
| 274 | break; |
| 275 | } |
| 276 | if (l1->key.target_class != l2->key.target_class |
| 277 | || l1->datum.allowed != l2->datum.allowed) |
| 278 | break; |
| 279 | } |
| 280 | if (l1 || l2) |
| 281 | continue; |
| 282 | free_type_rules(type_rules[j].next); |
| 283 | type_rules[j].next = NULL; |
| 284 | printf("Types %s and %s are equivalent.\n", |
| 285 | policydb.p_type_val_to_name[i], |
| 286 | policydb.p_type_val_to_name[j]); |
| 287 | } |
| 288 | free_type_rules(type_rules[i].next); |
| 289 | type_rules[i].next = NULL; |
| 290 | } |
| 291 | |
| 292 | free(type_rules); |
| 293 | return 0; |
| 294 | } |
| 295 | #endif |
| 296 | |
| 297 | extern char *av_to_string(uint32_t tclass, sepol_access_vector_t av); |
| 298 | |
Nicolas Iooss | c4a4a1a | 2014-09-14 23:41:49 +0200 | [diff] [blame] | 299 | int display_bools(void) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 300 | { |
Nicolas Iooss | 581d3eb | 2014-09-14 23:41:41 +0200 | [diff] [blame] | 301 | uint32_t i; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 302 | |
| 303 | for (i = 0; i < policydbp->p_bools.nprim; i++) { |
| 304 | printf("%s : %d\n", policydbp->p_bool_val_to_name[i], |
| 305 | policydbp->bool_val_to_struct[i]->state); |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | void display_expr(cond_expr_t * exp) |
| 311 | { |
| 312 | |
| 313 | cond_expr_t *cur; |
| 314 | for (cur = exp; cur != NULL; cur = cur->next) { |
| 315 | switch (cur->expr_type) { |
| 316 | case COND_BOOL: |
| 317 | printf("%s ", |
| 318 | policydbp->p_bool_val_to_name[cur->bool - 1]); |
| 319 | break; |
| 320 | case COND_NOT: |
| 321 | printf("! "); |
| 322 | break; |
| 323 | case COND_OR: |
| 324 | printf("|| "); |
| 325 | break; |
| 326 | case COND_AND: |
| 327 | printf("&& "); |
| 328 | break; |
| 329 | case COND_XOR: |
| 330 | printf("^ "); |
| 331 | break; |
| 332 | case COND_EQ: |
| 333 | printf("== "); |
| 334 | break; |
| 335 | case COND_NEQ: |
| 336 | printf("!= "); |
| 337 | break; |
| 338 | default: |
| 339 | printf("error!"); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Nicolas Iooss | c4a4a1a | 2014-09-14 23:41:49 +0200 | [diff] [blame] | 345 | int display_cond_expressions(void) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 346 | { |
| 347 | cond_node_t *cur; |
| 348 | |
| 349 | for (cur = policydbp->cond_list; cur != NULL; cur = cur->next) { |
| 350 | printf("expression: "); |
| 351 | display_expr(cur->expr); |
| 352 | printf("current state: %d\n", cur->cur_state); |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | int change_bool(char *name, int state) |
| 358 | { |
| 359 | cond_bool_datum_t *bool; |
| 360 | |
| 361 | bool = hashtab_search(policydbp->p_bools.table, name); |
| 362 | if (bool == NULL) { |
| 363 | printf("Could not find bool %s\n", name); |
| 364 | return -1; |
| 365 | } |
| 366 | bool->state = state; |
| 367 | evaluate_conds(policydbp); |
| 368 | return 0; |
| 369 | } |
| 370 | |
Nicolas Iooss | 5af8c5a | 2014-09-14 23:41:50 +0200 | [diff] [blame] | 371 | static int check_level(hashtab_key_t key, hashtab_datum_t datum, void *arg __attribute__ ((unused))) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 372 | { |
| 373 | level_datum_t *levdatum = (level_datum_t *) datum; |
| 374 | |
| 375 | if (!levdatum->isalias && !levdatum->defined) { |
| 376 | fprintf(stderr, |
| 377 | "Error: sensitivity %s was not used in a level definition!\n", |
| 378 | key); |
| 379 | return -1; |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int main(int argc, char **argv) |
| 385 | { |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 386 | policydb_t parse_policy; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 387 | sepol_security_class_t tclass; |
Richard Haines | ab9cbb1 | 2013-11-03 19:12:29 +0000 | [diff] [blame] | 388 | sepol_security_id_t ssid, tsid, *sids, oldsid, newsid, tasksid; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 389 | sepol_security_context_t scontext; |
| 390 | struct sepol_av_decision avd; |
| 391 | class_datum_t *cladatum; |
Nicolas Iooss | 7dcb7a5 | 2014-09-14 23:41:46 +0200 | [diff] [blame] | 392 | const char *file = txtfile; |
| 393 | char ans[80 + 1], *outfile = NULL, *path, *fstype; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 394 | size_t scontext_len, pathlen; |
| 395 | unsigned int i; |
| 396 | unsigned int protocol, port; |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 397 | unsigned int binary = 0, debug = 0, cil = 0, conf = 0; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 398 | struct val_to_name v; |
Paul Nuzzi | 79d10a8 | 2009-09-29 10:06:26 -0400 | [diff] [blame] | 399 | int ret, ch, fd, target = SEPOL_TARGET_SELINUX; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 400 | unsigned int nel, uret; |
| 401 | struct stat sb; |
| 402 | void *map; |
| 403 | FILE *outfp = NULL; |
| 404 | char *name; |
| 405 | int state; |
| 406 | int show_version = 0; |
Richard Haines | ab9cbb1 | 2013-11-03 19:12:29 +0000 | [diff] [blame] | 407 | char *reason_buf = NULL; |
| 408 | unsigned int reason; |
| 409 | int flags; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 410 | struct policy_file pf; |
Guido Trentalancia | bf57d23 | 2009-11-02 18:14:28 +0100 | [diff] [blame] | 411 | struct option long_options[] = { |
| 412 | {"output", required_argument, NULL, 'o'}, |
| 413 | {"target", required_argument, NULL, 't'}, |
| 414 | {"binary", no_argument, NULL, 'b'}, |
| 415 | {"debug", no_argument, NULL, 'd'}, |
| 416 | {"version", no_argument, NULL, 'V'}, |
Laurent Bigonville | f6a03f1 | 2013-07-06 14:32:33 +0200 | [diff] [blame] | 417 | {"handle-unknown", required_argument, NULL, 'U'}, |
Guido Trentalancia | bf57d23 | 2009-11-02 18:14:28 +0100 | [diff] [blame] | 418 | {"mls", no_argument, NULL, 'M'}, |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 419 | {"cil", no_argument, NULL, 'C'}, |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 420 | {"conf",no_argument, NULL, 'F'}, |
Guido Trentalancia | bf57d23 | 2009-11-02 18:14:28 +0100 | [diff] [blame] | 421 | {"help", no_argument, NULL, 'h'}, |
| 422 | {NULL, 0, NULL, 0} |
| 423 | }; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 424 | |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 425 | while ((ch = getopt_long(argc, argv, "o:t:dbU:MCFVc:h", long_options, NULL)) != -1) { |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 426 | switch (ch) { |
| 427 | case 'o': |
| 428 | outfile = optarg; |
| 429 | break; |
Paul Nuzzi | 79d10a8 | 2009-09-29 10:06:26 -0400 | [diff] [blame] | 430 | case 't': |
| 431 | if (!strcasecmp(optarg, "Xen")) |
| 432 | target = SEPOL_TARGET_XEN; |
| 433 | else if (!strcasecmp(optarg, "SELinux")) |
| 434 | target = SEPOL_TARGET_SELINUX; |
| 435 | else{ |
| 436 | fprintf(stderr, "%s: Unknown target platform:" |
| 437 | "%s\n", argv[0], optarg); |
| 438 | exit(1); |
| 439 | } |
| 440 | break; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 441 | case 'b': |
| 442 | binary = 1; |
| 443 | file = binfile; |
| 444 | break; |
| 445 | case 'd': |
| 446 | debug = 1; |
| 447 | break; |
| 448 | case 'V': |
| 449 | show_version = 1; |
| 450 | break; |
| 451 | case 'U': |
| 452 | if (!strcasecmp(optarg, "deny")) { |
| 453 | handle_unknown = DENY_UNKNOWN; |
| 454 | break; |
| 455 | } |
| 456 | if (!strcasecmp(optarg, "allow")) { |
| 457 | handle_unknown = ALLOW_UNKNOWN; |
| 458 | break; |
| 459 | } |
| 460 | if (!strcasecmp(optarg, "reject")) { |
| 461 | handle_unknown = REJECT_UNKNOWN; |
| 462 | break; |
| 463 | } |
| 464 | usage(argv[0]); |
| 465 | case 'M': |
| 466 | mlspol = 1; |
| 467 | break; |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 468 | case 'C': |
| 469 | cil = 1; |
| 470 | break; |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 471 | case 'F': |
| 472 | conf = 1; |
| 473 | break; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 474 | case 'c':{ |
Dan Albert | b1bbd30 | 2014-12-10 11:28:44 -0800 | [diff] [blame] | 475 | long int n; |
| 476 | errno = 0; |
| 477 | n = strtol(optarg, NULL, 10); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 478 | if (errno) { |
| 479 | fprintf(stderr, |
| 480 | "Invalid policyvers specified: %s\n", |
| 481 | optarg); |
| 482 | usage(argv[0]); |
| 483 | exit(1); |
| 484 | } |
| 485 | if (n < POLICYDB_VERSION_MIN |
| 486 | || n > POLICYDB_VERSION_MAX) { |
| 487 | fprintf(stderr, |
| 488 | "policyvers value %ld not in range %d-%d\n", |
| 489 | n, POLICYDB_VERSION_MIN, |
| 490 | POLICYDB_VERSION_MAX); |
| 491 | usage(argv[0]); |
| 492 | exit(1); |
| 493 | } |
| 494 | if (policyvers != n) |
| 495 | policyvers = n; |
| 496 | break; |
| 497 | } |
Guido Trentalancia | bf57d23 | 2009-11-02 18:14:28 +0100 | [diff] [blame] | 498 | case 'h': |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 499 | default: |
| 500 | usage(argv[0]); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (show_version) { |
| 505 | printf("%d (compatibility range %d-%d)\n", policyvers, |
| 506 | POLICYDB_VERSION_MAX, POLICYDB_VERSION_MIN); |
| 507 | exit(0); |
| 508 | } |
| 509 | |
| 510 | if (optind != argc) { |
| 511 | file = argv[optind++]; |
| 512 | if (optind != argc) |
| 513 | usage(argv[0]); |
| 514 | } |
| 515 | printf("%s: loading policy configuration from %s\n", argv[0], file); |
| 516 | |
| 517 | /* Set policydb and sidtab used by libsepol service functions |
| 518 | to my structures, so that I can directly populate and |
| 519 | manipulate them. */ |
| 520 | sepol_set_policydb(&policydb); |
| 521 | sepol_set_sidtab(&sidtab); |
| 522 | |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 523 | if (cil && conf) { |
| 524 | fprintf(stderr, "Can't convert to CIL and policy.conf at the same time\n"); |
| 525 | exit(1); |
| 526 | } |
| 527 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 528 | if (binary) { |
| 529 | fd = open(file, O_RDONLY); |
| 530 | if (fd < 0) { |
| 531 | fprintf(stderr, "Can't open '%s': %s\n", |
| 532 | file, strerror(errno)); |
| 533 | exit(1); |
| 534 | } |
| 535 | if (fstat(fd, &sb) < 0) { |
| 536 | fprintf(stderr, "Can't stat '%s': %s\n", |
| 537 | file, strerror(errno)); |
| 538 | exit(1); |
| 539 | } |
| 540 | map = |
| 541 | mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
| 542 | fd, 0); |
| 543 | if (map == MAP_FAILED) { |
| 544 | fprintf(stderr, "Can't map '%s': %s\n", |
| 545 | file, strerror(errno)); |
| 546 | exit(1); |
| 547 | } |
| 548 | policy_file_init(&pf); |
| 549 | pf.type = PF_USE_MEMORY; |
| 550 | pf.data = map; |
| 551 | pf.len = sb.st_size; |
| 552 | if (policydb_init(&policydb)) { |
| 553 | fprintf(stderr, "%s: policydb_init: Out of memory!\n", |
| 554 | argv[0]); |
| 555 | exit(1); |
| 556 | } |
| 557 | ret = policydb_read(&policydb, &pf, 1); |
| 558 | if (ret) { |
| 559 | fprintf(stderr, |
| 560 | "%s: error(s) encountered while parsing configuration\n", |
| 561 | argv[0]); |
| 562 | exit(1); |
| 563 | } |
| 564 | policydbp = &policydb; |
| 565 | |
| 566 | /* Check Policy Consistency */ |
| 567 | if (policydbp->mls) { |
| 568 | if (!mlspol) { |
| 569 | fprintf(stderr, "%s: MLS policy, but non-MLS" |
| 570 | " is specified\n", argv[0]); |
| 571 | exit(1); |
| 572 | } |
| 573 | } else { |
| 574 | if (mlspol) { |
| 575 | fprintf(stderr, "%s: non-MLS policy, but MLS" |
| 576 | " is specified\n", argv[0]); |
| 577 | exit(1); |
| 578 | } |
| 579 | } |
| 580 | } else { |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 581 | if (conf) { |
| 582 | fprintf(stderr, "Can only generate policy.conf from binary policy\n"); |
| 583 | exit(1); |
| 584 | } |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 585 | if (policydb_init(&parse_policy)) |
| 586 | exit(1); |
| 587 | /* We build this as a base policy first since that is all the parser understands */ |
| 588 | parse_policy.policy_type = POLICY_BASE; |
Paul Nuzzi | 79d10a8 | 2009-09-29 10:06:26 -0400 | [diff] [blame] | 589 | policydb_set_target_platform(&parse_policy, target); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 590 | |
| 591 | /* Let sepol know if we are dealing with MLS support */ |
| 592 | parse_policy.mls = mlspol; |
| 593 | parse_policy.handle_unknown = handle_unknown; |
| 594 | |
| 595 | policydbp = &parse_policy; |
| 596 | |
| 597 | if (read_source_policy(policydbp, file, "checkpolicy") < 0) |
| 598 | exit(1); |
| 599 | |
| 600 | if (hashtab_map(policydbp->p_levels.table, check_level, NULL)) |
| 601 | exit(1); |
| 602 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 603 | /* Linking takes care of optional avrule blocks */ |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 604 | if (link_modules(NULL, policydbp, NULL, 0, 0)) { |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 605 | fprintf(stderr, "Error while resolving optionals\n"); |
| 606 | exit(1); |
| 607 | } |
| 608 | |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 609 | if (!cil) { |
| 610 | if (policydb_init(&policydb)) { |
| 611 | fprintf(stderr, "%s: policydb_init failed\n", argv[0]); |
| 612 | exit(1); |
| 613 | } |
| 614 | if (expand_module(NULL, policydbp, &policydb, 0, 1)) { |
| 615 | fprintf(stderr, "Error while expanding policy\n"); |
| 616 | exit(1); |
| 617 | } |
| 618 | policydb_destroy(policydbp); |
| 619 | policydbp = &policydb; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 620 | } |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | if (policydb_load_isids(&policydb, &sidtab)) |
| 624 | exit(1); |
| 625 | |
| 626 | printf("%s: policy configuration loaded\n", argv[0]); |
| 627 | |
| 628 | if (outfile) { |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 629 | outfp = fopen(outfile, "w"); |
| 630 | if (!outfp) { |
| 631 | perror(outfile); |
| 632 | exit(1); |
| 633 | } |
| 634 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 635 | policydb.policyvers = policyvers; |
| 636 | |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 637 | if (!cil) { |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 638 | if (!conf) { |
| 639 | printf("%s: writing binary representation (version %d) to %s\n", argv[0], policyvers, outfile); |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 640 | |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 641 | policydb.policy_type = POLICY_KERN; |
| 642 | |
| 643 | policy_file_init(&pf); |
| 644 | pf.type = PF_USE_STDIO; |
| 645 | pf.fp = outfp; |
| 646 | ret = policydb_write(&policydb, &pf); |
| 647 | } else { |
| 648 | printf("%s: writing policy.conf to %s\n", |
| 649 | argv[0], outfile); |
| 650 | ret = sepol_kernel_policydb_to_conf(outfp, policydbp); |
| 651 | } |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 652 | if (ret) { |
| 653 | fprintf(stderr, "%s: error writing %s\n", |
| 654 | argv[0], outfile); |
| 655 | exit(1); |
| 656 | } |
| 657 | } else { |
| 658 | printf("%s: writing CIL to %s\n",argv[0], outfile); |
James Carter | 13c27d6 | 2017-03-21 16:00:30 -0400 | [diff] [blame] | 659 | if (binary) { |
| 660 | ret = sepol_kernel_policydb_to_cil(outfp, policydbp); |
| 661 | } else { |
| 662 | ret = sepol_module_policydb_to_cil(outfp, policydbp, 1); |
| 663 | } |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 664 | if (ret) { |
| 665 | fprintf(stderr, "%s: error writing %s\n", argv[0], outfile); |
| 666 | exit(1); |
| 667 | } |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 668 | } |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 669 | |
| 670 | if (outfile) { |
| 671 | fclose(outfp); |
| 672 | } |
| 673 | } else if (cil) { |
| 674 | fprintf(stderr, "%s: No file to write CIL was specified\n", argv[0]); |
| 675 | exit(1); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 676 | } |
James Carter | b1d9456 | 2015-04-01 10:05:04 -0400 | [diff] [blame] | 677 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 678 | if (!debug) { |
| 679 | policydb_destroy(&policydb); |
James Carter | dd11ab6 | 2017-03-17 15:58:29 -0400 | [diff] [blame] | 680 | sepol_sidtab_destroy(&sidtab); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 681 | exit(0); |
| 682 | } |
| 683 | |
| 684 | menu: |
| 685 | printf("\nSelect an option:\n"); |
| 686 | printf("0) Call compute_access_vector\n"); |
| 687 | printf("1) Call sid_to_context\n"); |
| 688 | printf("2) Call context_to_sid\n"); |
| 689 | printf("3) Call transition_sid\n"); |
| 690 | printf("4) Call member_sid\n"); |
| 691 | printf("5) Call change_sid\n"); |
| 692 | printf("6) Call list_sids\n"); |
| 693 | printf("7) Call load_policy\n"); |
| 694 | printf("8) Call fs_sid\n"); |
| 695 | printf("9) Call port_sid\n"); |
| 696 | printf("a) Call netif_sid\n"); |
| 697 | printf("b) Call node_sid\n"); |
| 698 | printf("c) Call fs_use\n"); |
| 699 | printf("d) Call genfs_sid\n"); |
| 700 | printf("e) Call get_user_sids\n"); |
| 701 | printf("f) display conditional bools\n"); |
| 702 | printf("g) display conditional expressions\n"); |
| 703 | printf("h) change a boolean value\n"); |
Richard Haines | ab9cbb1 | 2013-11-03 19:12:29 +0000 | [diff] [blame] | 704 | printf("i) display constraint expressions\n"); |
| 705 | printf("j) display validatetrans expressions\n"); |
Daniel Jurgens | 9fbb311 | 2017-05-22 16:08:24 +0300 | [diff] [blame] | 706 | printf("k) Call ibpkey_sid\n"); |
Daniel Jurgens | 118c0cd | 2017-05-22 16:08:27 +0300 | [diff] [blame] | 707 | printf("l) Call ibendport_sid\n"); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 708 | #ifdef EQUIVTYPES |
| 709 | printf("z) Show equivalent types\n"); |
| 710 | #endif |
| 711 | printf("m) Show menu again\n"); |
| 712 | printf("q) Exit\n"); |
| 713 | while (1) { |
| 714 | printf("\nChoose: "); |
| 715 | FGETS(ans, sizeof(ans), stdin); |
| 716 | switch (ans[0]) { |
| 717 | case '0': |
| 718 | printf("source sid? "); |
| 719 | FGETS(ans, sizeof(ans), stdin); |
| 720 | ssid = atoi(ans); |
| 721 | |
| 722 | printf("target sid? "); |
| 723 | FGETS(ans, sizeof(ans), stdin); |
| 724 | tsid = atoi(ans); |
| 725 | |
| 726 | printf("target class? "); |
| 727 | FGETS(ans, sizeof(ans), stdin); |
| 728 | if (isdigit(ans[0])) { |
| 729 | tclass = atoi(ans); |
| 730 | if (!tclass |
| 731 | || tclass > policydb.p_classes.nprim) { |
| 732 | printf("\nNo such class.\n"); |
| 733 | break; |
| 734 | } |
| 735 | cladatum = |
| 736 | policydb.class_val_to_struct[tclass - 1]; |
| 737 | } else { |
| 738 | ans[strlen(ans) - 1] = 0; |
| 739 | cladatum = |
| 740 | (class_datum_t *) hashtab_search(policydb. |
| 741 | p_classes. |
| 742 | table, |
| 743 | ans); |
| 744 | if (!cladatum) { |
| 745 | printf("\nNo such class\n"); |
| 746 | break; |
| 747 | } |
| 748 | tclass = cladatum->s.value; |
| 749 | } |
| 750 | |
| 751 | if (!cladatum->comdatum && !cladatum->permissions.nprim) { |
| 752 | printf |
| 753 | ("\nNo access vector definition for that class\n"); |
| 754 | break; |
| 755 | } |
| 756 | ret = sepol_compute_av(ssid, tsid, tclass, 0, &avd); |
| 757 | switch (ret) { |
| 758 | case 0: |
| 759 | printf("\nallowed {"); |
| 760 | for (i = 1; i <= sizeof(avd.allowed) * 8; i++) { |
| 761 | if (avd.allowed & (1 << (i - 1))) { |
| 762 | v.val = i; |
| 763 | ret = |
| 764 | hashtab_map(cladatum-> |
| 765 | permissions. |
| 766 | table, |
| 767 | find_perm, &v); |
| 768 | if (!ret && cladatum->comdatum) { |
| 769 | ret = |
| 770 | hashtab_map |
| 771 | (cladatum-> |
| 772 | comdatum-> |
| 773 | permissions.table, |
| 774 | find_perm, &v); |
| 775 | } |
| 776 | if (ret) |
| 777 | printf(" %s", v.name); |
| 778 | } |
| 779 | } |
| 780 | printf(" }\n"); |
| 781 | break; |
| 782 | case -EINVAL: |
| 783 | printf("\ninvalid sid\n"); |
| 784 | break; |
| 785 | default: |
| 786 | printf("return code 0x%x\n", ret); |
| 787 | } |
| 788 | break; |
| 789 | case '1': |
| 790 | printf("sid? "); |
| 791 | FGETS(ans, sizeof(ans), stdin); |
| 792 | ssid = atoi(ans); |
| 793 | ret = sepol_sid_to_context(ssid, |
| 794 | &scontext, &scontext_len); |
| 795 | switch (ret) { |
| 796 | case 0: |
| 797 | printf("\nscontext %s\n", scontext); |
| 798 | free(scontext); |
| 799 | break; |
| 800 | case -EINVAL: |
| 801 | printf("\ninvalid sid\n"); |
| 802 | break; |
| 803 | case -ENOMEM: |
| 804 | printf("\nout of memory\n"); |
| 805 | break; |
| 806 | default: |
| 807 | printf("return code 0x%x\n", ret); |
| 808 | } |
| 809 | break; |
| 810 | case '2': |
| 811 | printf("scontext? "); |
| 812 | FGETS(ans, sizeof(ans), stdin); |
| 813 | scontext_len = strlen(ans); |
| 814 | ans[scontext_len - 1] = 0; |
| 815 | ret = sepol_context_to_sid(ans, scontext_len, &ssid); |
| 816 | switch (ret) { |
| 817 | case 0: |
| 818 | printf("\nsid %d\n", ssid); |
| 819 | break; |
| 820 | case -EINVAL: |
| 821 | printf("\ninvalid context\n"); |
| 822 | break; |
| 823 | case -ENOMEM: |
| 824 | printf("\nout of memory\n"); |
| 825 | break; |
| 826 | default: |
| 827 | printf("return code 0x%x\n", ret); |
| 828 | } |
| 829 | break; |
| 830 | case '3': |
| 831 | case '4': |
| 832 | case '5': |
| 833 | ch = ans[0]; |
| 834 | |
| 835 | printf("source sid? "); |
| 836 | FGETS(ans, sizeof(ans), stdin); |
| 837 | ssid = atoi(ans); |
| 838 | printf("target sid? "); |
| 839 | FGETS(ans, sizeof(ans), stdin); |
| 840 | tsid = atoi(ans); |
| 841 | |
| 842 | printf("object class? "); |
| 843 | FGETS(ans, sizeof(ans), stdin); |
| 844 | if (isdigit(ans[0])) { |
| 845 | tclass = atoi(ans); |
| 846 | if (!tclass |
| 847 | || tclass > policydb.p_classes.nprim) { |
| 848 | printf("\nNo such class.\n"); |
| 849 | break; |
| 850 | } |
| 851 | } else { |
| 852 | ans[strlen(ans) - 1] = 0; |
| 853 | cladatum = |
| 854 | (class_datum_t *) hashtab_search(policydb. |
| 855 | p_classes. |
| 856 | table, |
| 857 | ans); |
| 858 | if (!cladatum) { |
| 859 | printf("\nNo such class\n"); |
| 860 | break; |
| 861 | } |
| 862 | tclass = cladatum->s.value; |
| 863 | } |
| 864 | |
| 865 | if (ch == '3') |
| 866 | ret = |
| 867 | sepol_transition_sid(ssid, tsid, tclass, |
| 868 | &ssid); |
| 869 | else if (ch == '4') |
| 870 | ret = |
| 871 | sepol_member_sid(ssid, tsid, tclass, &ssid); |
| 872 | else |
| 873 | ret = |
| 874 | sepol_change_sid(ssid, tsid, tclass, &ssid); |
| 875 | switch (ret) { |
| 876 | case 0: |
| 877 | printf("\nsid %d\n", ssid); |
| 878 | break; |
| 879 | case -EINVAL: |
| 880 | printf("\ninvalid sid\n"); |
| 881 | break; |
| 882 | case -ENOMEM: |
| 883 | printf("\nout of memory\n"); |
| 884 | break; |
| 885 | default: |
| 886 | printf("return code 0x%x\n", ret); |
| 887 | } |
| 888 | break; |
| 889 | case '6': |
| 890 | sepol_sidtab_map(&sidtab, print_sid, 0); |
| 891 | break; |
| 892 | case '7': |
| 893 | printf("pathname? "); |
| 894 | FGETS(ans, sizeof(ans), stdin); |
| 895 | pathlen = strlen(ans); |
| 896 | ans[pathlen - 1] = 0; |
| 897 | printf("%s: loading policy configuration from %s\n", |
| 898 | argv[0], ans); |
| 899 | fd = open(ans, O_RDONLY); |
| 900 | if (fd < 0) { |
| 901 | fprintf(stderr, "Can't open '%s': %s\n", |
| 902 | ans, strerror(errno)); |
| 903 | break; |
| 904 | } |
| 905 | if (fstat(fd, &sb) < 0) { |
| 906 | fprintf(stderr, "Can't stat '%s': %s\n", |
| 907 | ans, strerror(errno)); |
| 908 | break; |
| 909 | } |
| 910 | map = |
| 911 | mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, |
| 912 | MAP_PRIVATE, fd, 0); |
| 913 | if (map == MAP_FAILED) { |
| 914 | fprintf(stderr, "Can't map '%s': %s\n", |
| 915 | ans, strerror(errno)); |
| 916 | break; |
| 917 | } |
| 918 | ret = sepol_load_policy(map, sb.st_size); |
| 919 | switch (ret) { |
| 920 | case 0: |
| 921 | printf("\nsuccess\n"); |
| 922 | break; |
| 923 | case -EINVAL: |
| 924 | printf("\ninvalid policy\n"); |
| 925 | break; |
| 926 | case -ENOMEM: |
| 927 | printf("\nout of memory\n"); |
| 928 | break; |
| 929 | default: |
| 930 | printf("return code 0x%x\n", ret); |
| 931 | } |
| 932 | break; |
| 933 | case '8': |
| 934 | printf("fs kdevname? "); |
| 935 | FGETS(ans, sizeof(ans), stdin); |
| 936 | ans[strlen(ans) - 1] = 0; |
| 937 | sepol_fs_sid(ans, &ssid, &tsid); |
| 938 | printf("fs_sid %d default_file_sid %d\n", ssid, tsid); |
| 939 | break; |
| 940 | case '9': |
| 941 | printf("protocol? "); |
| 942 | FGETS(ans, sizeof(ans), stdin); |
| 943 | ans[strlen(ans) - 1] = 0; |
| 944 | if (!strcmp(ans, "tcp") || !strcmp(ans, "TCP")) |
| 945 | protocol = IPPROTO_TCP; |
| 946 | else if (!strcmp(ans, "udp") || !strcmp(ans, "UDP")) |
| 947 | protocol = IPPROTO_UDP; |
Richard Haines | 3895fbb | 2016-04-06 12:44:35 +0100 | [diff] [blame] | 948 | else if (!strcmp(ans, "dccp") || !strcmp(ans, "DCCP")) |
| 949 | protocol = IPPROTO_DCCP; |
Richard Haines via Selinux | cf0ab12 | 2018-03-11 16:22:55 +0000 | [diff] [blame^] | 950 | else if (!strcmp(ans, "sctp") || !strcmp(ans, "SCTP")) |
| 951 | protocol = IPPROTO_SCTP; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 952 | else { |
| 953 | printf("unknown protocol\n"); |
| 954 | break; |
| 955 | } |
| 956 | printf("port? "); |
| 957 | FGETS(ans, sizeof(ans), stdin); |
| 958 | port = atoi(ans); |
| 959 | sepol_port_sid(0, 0, protocol, port, &ssid); |
| 960 | printf("sid %d\n", ssid); |
| 961 | break; |
| 962 | case 'a': |
| 963 | printf("netif name? "); |
| 964 | FGETS(ans, sizeof(ans), stdin); |
| 965 | ans[strlen(ans) - 1] = 0; |
| 966 | sepol_netif_sid(ans, &ssid, &tsid); |
| 967 | printf("if_sid %d default_msg_sid %d\n", ssid, tsid); |
| 968 | break; |
| 969 | case 'b':{ |
| 970 | char *p; |
| 971 | int family, len; |
| 972 | struct in_addr addr4; |
| 973 | struct in6_addr addr6; |
| 974 | |
| 975 | printf("protocol family? "); |
| 976 | FGETS(ans, sizeof(ans), stdin); |
| 977 | ans[strlen(ans) - 1] = 0; |
| 978 | if (!strcasecmp(ans, "ipv4")) |
| 979 | family = AF_INET; |
| 980 | else if (!strcasecmp(ans, "ipv6")) |
| 981 | family = AF_INET6; |
| 982 | else { |
| 983 | printf("unknown protocol family\n"); |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | printf("node address? "); |
| 988 | FGETS(ans, sizeof(ans), stdin); |
| 989 | ans[strlen(ans) - 1] = 0; |
| 990 | |
| 991 | if (family == AF_INET) { |
| 992 | p = (char *)&addr4; |
| 993 | len = sizeof(addr4); |
| 994 | } else { |
| 995 | p = (char *)&addr6; |
| 996 | len = sizeof(addr6); |
| 997 | } |
| 998 | |
| 999 | if (inet_pton(family, ans, p) < 1) { |
| 1000 | printf("error parsing address\n"); |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | sepol_node_sid(family, p, len, &ssid); |
| 1005 | printf("sid %d\n", ssid); |
| 1006 | break; |
| 1007 | } |
| 1008 | case 'c': |
| 1009 | printf("fstype? "); |
| 1010 | FGETS(ans, sizeof(ans), stdin); |
| 1011 | ans[strlen(ans) - 1] = 0; |
| 1012 | sepol_fs_use(ans, &uret, &ssid); |
| 1013 | switch (uret) { |
| 1014 | case SECURITY_FS_USE_XATTR: |
| 1015 | printf("use xattr\n"); |
| 1016 | break; |
| 1017 | case SECURITY_FS_USE_TRANS: |
| 1018 | printf("use transition SIDs\n"); |
| 1019 | break; |
| 1020 | case SECURITY_FS_USE_TASK: |
| 1021 | printf("use task SIDs\n"); |
| 1022 | break; |
| 1023 | case SECURITY_FS_USE_GENFS: |
| 1024 | printf("use genfs\n"); |
| 1025 | break; |
| 1026 | case SECURITY_FS_USE_NONE: |
| 1027 | printf("no labeling support\n"); |
| 1028 | break; |
| 1029 | } |
| 1030 | printf("sid %d\n", ssid); |
| 1031 | break; |
| 1032 | case 'd': |
| 1033 | printf("fstype? "); |
| 1034 | FGETS(ans, sizeof(ans), stdin); |
| 1035 | ans[strlen(ans) - 1] = 0; |
| 1036 | fstype = strdup(ans); |
| 1037 | printf("path? "); |
| 1038 | FGETS(ans, sizeof(ans), stdin); |
| 1039 | ans[strlen(ans) - 1] = 0; |
| 1040 | path = strdup(ans); |
| 1041 | printf("object class? "); |
| 1042 | FGETS(ans, sizeof(ans), stdin); |
| 1043 | if (isdigit(ans[0])) { |
| 1044 | tclass = atoi(ans); |
| 1045 | if (!tclass |
| 1046 | || tclass > policydb.p_classes.nprim) { |
| 1047 | printf("\nNo such class.\n"); |
| 1048 | break; |
| 1049 | } |
| 1050 | } else { |
| 1051 | ans[strlen(ans) - 1] = 0; |
| 1052 | cladatum = |
| 1053 | (class_datum_t *) hashtab_search(policydb. |
| 1054 | p_classes. |
| 1055 | table, |
| 1056 | ans); |
| 1057 | if (!cladatum) { |
| 1058 | printf("\nNo such class\n"); |
| 1059 | break; |
| 1060 | } |
| 1061 | tclass = cladatum->s.value; |
| 1062 | } |
| 1063 | sepol_genfs_sid(fstype, path, tclass, &ssid); |
| 1064 | printf("sid %d\n", ssid); |
| 1065 | free(fstype); |
| 1066 | free(path); |
| 1067 | break; |
| 1068 | case 'e': |
| 1069 | printf("from SID? "); |
| 1070 | FGETS(ans, sizeof(ans), stdin); |
| 1071 | ans[strlen(ans) - 1] = 0; |
| 1072 | ssid = atoi(ans); |
| 1073 | |
| 1074 | printf("username? "); |
| 1075 | FGETS(ans, sizeof(ans), stdin); |
| 1076 | ans[strlen(ans) - 1] = 0; |
| 1077 | |
| 1078 | ret = sepol_get_user_sids(ssid, ans, &sids, &nel); |
| 1079 | switch (ret) { |
| 1080 | case 0: |
| 1081 | if (!nel) |
| 1082 | printf("\nnone\n"); |
| 1083 | for (i = 0; i < nel; i++) |
| 1084 | print_sid(sids[i], NULL, NULL); |
| 1085 | free(sids); |
| 1086 | break; |
| 1087 | case -ENOMEM: |
| 1088 | printf("\nout of memory\n"); |
| 1089 | break; |
| 1090 | case -EINVAL: |
| 1091 | printf("\ninvalid argument\n"); |
| 1092 | break; |
| 1093 | default: |
| 1094 | printf("\nerror\n"); |
| 1095 | break; |
| 1096 | } |
| 1097 | break; |
| 1098 | case 'f': |
| 1099 | display_bools(); |
| 1100 | break; |
| 1101 | case 'g': |
| 1102 | display_cond_expressions(); |
| 1103 | break; |
| 1104 | case 'h': |
| 1105 | printf("name? "); |
| 1106 | FGETS(ans, sizeof(ans), stdin); |
| 1107 | ans[strlen(ans) - 1] = 0; |
| 1108 | |
| 1109 | name = malloc((strlen(ans) + 1) * sizeof(char)); |
| 1110 | if (name == NULL) { |
| 1111 | fprintf(stderr, "couldn't malloc string.\n"); |
| 1112 | break; |
| 1113 | } |
| 1114 | strcpy(name, ans); |
| 1115 | |
| 1116 | printf("state? "); |
| 1117 | FGETS(ans, sizeof(ans), stdin); |
| 1118 | ans[strlen(ans) - 1] = 0; |
| 1119 | |
| 1120 | if (atoi(ans)) |
| 1121 | state = 1; |
| 1122 | else |
| 1123 | state = 0; |
| 1124 | |
| 1125 | change_bool(name, state); |
| 1126 | free(name); |
| 1127 | break; |
Richard Haines | ab9cbb1 | 2013-11-03 19:12:29 +0000 | [diff] [blame] | 1128 | case 'i': |
| 1129 | printf("source sid? "); |
| 1130 | FGETS(ans, sizeof(ans), stdin); |
| 1131 | ssid = atoi(ans); |
| 1132 | |
| 1133 | printf("target sid? "); |
| 1134 | FGETS(ans, sizeof(ans), stdin); |
| 1135 | tsid = atoi(ans); |
| 1136 | |
| 1137 | printf("target class? "); |
| 1138 | FGETS(ans, sizeof(ans), stdin); |
| 1139 | if (isdigit(ans[0])) { |
| 1140 | tclass = atoi(ans); |
| 1141 | if (!tclass |
| 1142 | || tclass > policydb.p_classes.nprim) { |
| 1143 | printf("\nNo such class.\n"); |
| 1144 | break; |
| 1145 | } |
| 1146 | cladatum = |
| 1147 | policydb.class_val_to_struct[tclass - 1]; |
| 1148 | } else { |
| 1149 | ans[strlen(ans) - 1] = 0; |
| 1150 | cladatum = |
| 1151 | (class_datum_t *) hashtab_search(policydb. |
| 1152 | p_classes. |
| 1153 | table, |
| 1154 | ans); |
| 1155 | if (!cladatum) { |
| 1156 | printf("\nNo such class\n"); |
| 1157 | break; |
| 1158 | } |
| 1159 | tclass = cladatum->s.value; |
| 1160 | } |
| 1161 | |
| 1162 | flags = SHOW_GRANTED; |
| 1163 | if (sepol_compute_av_reason_buffer(ssid, tsid, |
| 1164 | tclass, 0, &avd, &reason, |
| 1165 | &reason_buf, flags)) { |
| 1166 | printf("\nconstraint error\n"); |
| 1167 | break; |
| 1168 | } |
| 1169 | if (reason_buf) { |
| 1170 | printf("\nConstraint expressions:\n%s", |
| 1171 | reason_buf); |
| 1172 | free(reason_buf); |
| 1173 | } else { |
| 1174 | printf("\nNo constraints found.\n"); |
| 1175 | } |
| 1176 | break; |
| 1177 | case 'j': |
| 1178 | printf("old sid? "); |
| 1179 | FGETS(ans, sizeof(ans), stdin); |
| 1180 | oldsid = atoi(ans); |
| 1181 | |
| 1182 | printf("new sid? "); |
| 1183 | FGETS(ans, sizeof(ans), stdin); |
| 1184 | newsid = atoi(ans); |
| 1185 | |
| 1186 | printf("task sid? "); |
| 1187 | FGETS(ans, sizeof(ans), stdin); |
| 1188 | tasksid = atoi(ans); |
| 1189 | |
| 1190 | printf("target class? "); |
| 1191 | FGETS(ans, sizeof(ans), stdin); |
| 1192 | if (isdigit(ans[0])) { |
| 1193 | tclass = atoi(ans); |
| 1194 | if (!tclass |
| 1195 | || tclass > policydb.p_classes.nprim) { |
| 1196 | printf("\nNo such class.\n"); |
| 1197 | break; |
| 1198 | } |
| 1199 | cladatum = |
| 1200 | policydb.class_val_to_struct[tclass - 1]; |
| 1201 | } else { |
| 1202 | ans[strlen(ans) - 1] = 0; |
| 1203 | cladatum = |
| 1204 | (class_datum_t *) hashtab_search(policydb. |
| 1205 | p_classes. |
| 1206 | table, |
| 1207 | ans); |
| 1208 | if (!cladatum) { |
| 1209 | printf("\nNo such class\n"); |
| 1210 | break; |
| 1211 | } |
| 1212 | tclass = cladatum->s.value; |
| 1213 | } |
| 1214 | |
| 1215 | flags = SHOW_GRANTED; |
| 1216 | if (sepol_validate_transition_reason_buffer(oldsid, |
| 1217 | newsid, tasksid, tclass, |
| 1218 | &reason_buf, flags)) { |
| 1219 | printf("\nvalidatetrans error\n"); |
| 1220 | break; |
| 1221 | } |
| 1222 | if (reason_buf) { |
| 1223 | printf("\nValidatetrans expressions:\n%s", |
| 1224 | reason_buf); |
| 1225 | free(reason_buf); |
| 1226 | } else { |
| 1227 | printf( |
| 1228 | "\nNo validatetrans expressions found.\n"); |
| 1229 | } |
| 1230 | break; |
Daniel Jurgens | 9fbb311 | 2017-05-22 16:08:24 +0300 | [diff] [blame] | 1231 | case 'k': |
| 1232 | { |
| 1233 | char *p; |
| 1234 | struct in6_addr addr6; |
| 1235 | uint64_t subnet_prefix; |
| 1236 | unsigned int pkey; |
| 1237 | |
| 1238 | printf("subnet prefix? "); |
| 1239 | FGETS(ans, sizeof(ans), stdin); |
| 1240 | ans[strlen(ans) - 1] = 0; |
| 1241 | p = (char *)&addr6; |
| 1242 | |
| 1243 | if (inet_pton(AF_INET6, ans, p) < 1) { |
| 1244 | printf("error parsing subnet prefix\n"); |
| 1245 | break; |
| 1246 | } |
| 1247 | |
| 1248 | memcpy(&subnet_prefix, p, sizeof(subnet_prefix)); |
| 1249 | printf("pkey? "); |
| 1250 | FGETS(ans, sizeof(ans), stdin); |
| 1251 | pkey = atoi(ans); |
| 1252 | sepol_ibpkey_sid(subnet_prefix, pkey, &ssid); |
| 1253 | printf("sid %d\n", ssid); |
| 1254 | } |
| 1255 | break; |
Daniel Jurgens | 118c0cd | 2017-05-22 16:08:27 +0300 | [diff] [blame] | 1256 | case 'l': |
| 1257 | printf("device name (eg. mlx4_0)? "); |
| 1258 | FGETS(ans, sizeof(ans), stdin); |
| 1259 | ans[strlen(ans) - 1] = 0; |
| 1260 | |
| 1261 | name = malloc((strlen(ans) + 1) * sizeof(char)); |
| 1262 | if (!name) { |
| 1263 | fprintf(stderr, "couldn't malloc string.\n"); |
| 1264 | break; |
| 1265 | } |
| 1266 | strcpy(name, ans); |
| 1267 | |
| 1268 | printf("port? "); |
| 1269 | FGETS(ans, sizeof(ans), stdin); |
| 1270 | port = atoi(ans); |
| 1271 | sepol_ibendport_sid(name, port, &ssid); |
| 1272 | printf("sid %d\n", ssid); |
| 1273 | free(name); |
| 1274 | break; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 1275 | #ifdef EQUIVTYPES |
| 1276 | case 'z': |
| 1277 | identify_equiv_types(); |
| 1278 | break; |
| 1279 | #endif |
| 1280 | case 'm': |
| 1281 | goto menu; |
| 1282 | case 'q': |
| 1283 | exit(0); |
| 1284 | break; |
| 1285 | default: |
| 1286 | printf("\nUnknown option %s.\n", ans); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* FLASK */ |