1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
/**
* @file
* Implementation of the sefs_fcfile class.
*
* @author Jeremy A. Mowery jmowery@tresys.com
* @author Jason Tang jtang@tresys.com
*
* Copyright (C) 2007 Tresys Technology, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include "sefs_internal.hh"
#include <sefs/entry.hh>
#include <sefs/fcfile.hh>
#include <apol/util.h>
#include <qpol/genfscon_query.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
/******************** public functions below ********************/
static void fcfile_entry_free(void *elem)
{
if (elem != NULL)
{
sefs_entry *entry = static_cast < sefs_entry * >(elem);
delete entry;
}
}
sefs_fcfile::sefs_fcfile(sefs_callback_fn_t msg_callback, void *varg) throw(std::bad_alloc):sefs_fclist(SEFS_FCLIST_TYPE_FCFILE,
msg_callback, varg)
{
_files = _entries = NULL;
_mls_set = false;
try
{
if ((_files = apol_vector_create(free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
if ((_entries = apol_vector_create(fcfile_entry_free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
}
catch(...)
{
apol_vector_destroy(&_files);
apol_vector_destroy(&_entries);
throw;
}
}
sefs_fcfile::sefs_fcfile(const char *file, sefs_callback_fn_t msg_callback, void *varg) throw(std::bad_alloc, std::invalid_argument,
std::
runtime_error):sefs_fclist
(SEFS_FCLIST_TYPE_FCFILE, msg_callback, varg)
{
_files = _entries = NULL;
_mls_set = false;
try
{
if ((_files = apol_vector_create_with_capacity(1, free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
if ((_entries = apol_vector_create(fcfile_entry_free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
if (appendFile(file) < 0)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error("Could not construct fcfile with the given file.");
}
}
catch(...)
{
apol_vector_destroy(&_files);
apol_vector_destroy(&_entries);
throw;
}
}
sefs_fcfile::sefs_fcfile(const apol_vector_t * files, sefs_callback_fn_t msg_callback, void *varg) throw(std::bad_alloc,
std::invalid_argument,
std::
runtime_error):sefs_fclist
(SEFS_FCLIST_TYPE_FCFILE, msg_callback, varg)
{
_files = _entries = NULL;
_mls_set = false;
try
{
if (files == NULL)
{
SEFS_ERR(this, "%s", strerror(EINVAL));
errno = EINVAL;
throw std::invalid_argument(strerror(EINVAL));
}
if ((_files = apol_vector_create_with_capacity(apol_vector_get_size(files), free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
if ((_entries = apol_vector_create(fcfile_entry_free)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::bad_alloc();
}
if (appendFileList(files) != apol_vector_get_size(files))
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error("Could not construct fcfile with the given vector.");
}
}
catch(...)
{
apol_vector_destroy(&_files);
apol_vector_destroy(&_entries);
throw;
}
}
sefs_fcfile::~sefs_fcfile()
{
apol_vector_destroy(&_files);
apol_vector_destroy(&_entries);
}
int sefs_fcfile::runQueryMap(sefs_query * query, sefs_fclist_map_fn_t fn, void *data) throw(std::runtime_error,
std::invalid_argument)
{
apol_vector_t *type_list = NULL;
apol_mls_range_t *range = NULL;
int retval = 0;
try
{
if (query != NULL)
{
query->compile();
if (policy != NULL)
{
if (query->_type != NULL && query->_indirect &&
(type_list =
query_create_candidate_type(policy, query->_type, query->_retype, query->_regex,
query->_indirect)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(errno));
}
if (query->_range != NULL && query->_rangeMatch != 0 &&
(range = apol_mls_range_create_from_string(policy, query->_range)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(errno));
}
}
}
for (size_t i = 0; i < apol_vector_get_size(_entries); i++)
{
sefs_entry *e = static_cast < sefs_entry * >(apol_vector_get_element(_entries, i));
if (query != NULL)
{
const struct sefs_context_node *context = e->_context;
if (!query_str_compare(context->user, query->_user, query->_reuser, query->_regex))
{
continue;
}
if (!query_str_compare(context->role, query->_role, query->_rerole, query->_regex))
{
continue;
}
bool str_matched = false, pol_matched = false;
str_matched = query_str_compare(context->type, query->_type, query->_retype, query->_regex);
if (type_list != NULL && !str_matched)
{
size_t index;
pol_matched =
(apol_vector_get_index(type_list, context->type, apol_str_strcmp, NULL, &index) <
0);
}
if (!str_matched && !pol_matched)
{
continue;
}
if (isMLS())
{
if (range == NULL)
{
if (!query_str_compare
(context->range, query->_range, query->_rerange, query->_regex))
{
continue;
}
}
else
{
const apol_mls_range_t *context_range = apol_context_get_range(context->context);
int ret;
ret = apol_mls_range_compare(policy, context_range, range, query->_rangeMatch);
if (ret <= 0)
{
continue;
}
}
}
if (e->_objectClass != QPOL_CLASS_ALL && query->_objclass != QPOL_CLASS_ALL &&
e->_objectClass != query->_objclass)
{
continue;
}
bool path_matched;
if (query->_path == NULL || query->_path[0] == '\0')
{
path_matched = true;
}
else
{
path_matched = false;
char *anchored_path = NULL;
if (asprintf(&anchored_path, "^%s$", e->_path) < 0)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(errno));
}
regex_t regex;
if (regcomp(®ex, anchored_path, REG_EXTENDED | REG_NOSUB) != 0)
{
free(anchored_path);
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(errno));
}
bool compval = query_str_compare(query->_path, anchored_path, ®ex, true);
free(anchored_path);
regfree(®ex);
if (compval)
{
path_matched = true;
}
}
if (!path_matched)
{
continue;
}
}
// if reached this point, then all criteria passed, so
// invoke the mapping function
if ((retval = fn(this, e, data)) < 0)
{
return retval;
}
}
}
catch(...)
{
apol_vector_destroy(&type_list);
apol_mls_range_destroy(&range);
throw;
}
apol_vector_destroy(&type_list);
return retval;
}
bool sefs_fcfile::isMLS() const
{
if (_mls_set)
{
return _mls;
}
return false;
}
int sefs_fcfile::appendFile(const char *file) throw(std::bad_alloc, std::invalid_argument, std::runtime_error)
{
FILE *fc_file = NULL;
char *line = NULL, *name_dup = NULL;
size_t line_len = 0;
size_t last_entry = apol_vector_get_size(_entries);
int retval, error = 0;
regex_t line_regex, context_regex;
bool is_line_compiled = false;
bool is_context_compiled = false;
try
{
if (file == NULL)
{
errno = EINVAL;
SEFS_ERR(this, "%s", strerror(EINVAL));
throw std::invalid_argument(strerror(EINVAL));
}
fc_file = fopen(file, "r");
if (!fc_file)
{
SEFS_ERR(this, "Unable to open file %s", file);
throw std::runtime_error(strerror(error));
}
if ((name_dup = strdup(file)) == NULL)
{
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
if (regcomp(&line_regex, "^([^[:blank:]]+)[[:blank:]]+(-.[[:blank:]]+)?([^-].+)$", REG_EXTENDED) != 0)
{
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
is_line_compiled = true;
if (regcomp(&context_regex, "^([^:]+):([^:]+):([^:]+):?(.*)$", REG_EXTENDED) != 0)
{
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
is_context_compiled = true;
while (!feof(fc_file))
{
if (getline(&line, &line_len, fc_file) == -1)
{
if (feof(fc_file))
{
break;
}
else
{
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
}
parse_line(name_dup, line, &line_regex, &context_regex);
}
if (apol_vector_append(_files, name_dup) < 0)
{
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
name_dup = NULL;
retval = 0;
}
catch(...)
{
error = errno;
// discard all entries that were read from this file_contexts
size_t i = apol_vector_get_size(_entries);
for (; i > last_entry; i--)
{
sefs_entry *e = static_cast < sefs_entry * >(apol_vector_get_element(_entries, i - 1));
fcfile_entry_free(e);
apol_vector_remove(_entries, i - 1);
}
retval = -1;
}
if (fc_file != NULL)
{
fclose(fc_file);
}
if (is_line_compiled)
{
regfree(&line_regex);
}
if (is_context_compiled)
{
regfree(&context_regex);
}
free(name_dup);
free(line);
errno = error;
return retval;
}
size_t sefs_fcfile::appendFileList(const apol_vector_t * files)throw(std::bad_alloc, std::invalid_argument, std::runtime_error)
{
size_t i;
if (files == NULL)
{
SEFS_ERR(this, "%s", strerror(EINVAL));
errno = EINVAL;
throw new std::invalid_argument(strerror(EINVAL));
}
for (i = 0; i < apol_vector_get_size(files); i++)
{
if (appendFile(static_cast < char *>(apol_vector_get_element(files, i))) < 0)
{
return i;
}
}
return i;
}
const apol_vector_t *sefs_fcfile::fileList() const
{
return _files;
}
/******************** private functions below ********************/
void sefs_fcfile::parse_line(const char *origin, const char *line, regex_t * line_regex,
regex_t * context_regex) throw(std::bad_alloc, std::runtime_error)
{
int error = 0;
char *s = strdup(line);
char *path;
if (s == NULL)
{
error = errno;
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
apol_str_trim(s);
if (s[0] == '#' || s[0] == '\0')
{
free(s);
return;
}
try
{
const size_t nmatch = 5;
regmatch_t pmatch[nmatch];
if (regexec(line_regex, s, nmatch, pmatch, 0) != 0)
{
error = EIO;
SEFS_ERR(this, "fcfile line is not legal:\n%s", s);
throw std::runtime_error(strerror(error));
}
assert(pmatch[1].rm_so == 0);
s[pmatch[1].rm_eo] = '\0';
if ((path = strdup(s)) == NULL)
{
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(error));
}
if (apol_bst_insert_and_get(path_tree, (void **)&path, NULL) < 0)
{
free(path);
SEFS_ERR(this, "%s", strerror(errno));
throw std::runtime_error(strerror(error));
}
uint32_t objclass;
if (pmatch[2].rm_so != -1)
{
switch (s[pmatch[2].rm_so + 1])
{
case '-':
objclass = QPOL_CLASS_FILE;
break;
case 'd':
objclass = QPOL_CLASS_DIR;
break;
case 'c':
objclass = QPOL_CLASS_CHR_FILE;
break;
case 'b':
objclass = QPOL_CLASS_BLK_FILE;
break;
case 'p':
objclass = QPOL_CLASS_FIFO_FILE;
break;
case 'l':
objclass = QPOL_CLASS_LNK_FILE;
break;
case 's':
objclass = QPOL_CLASS_SOCK_FILE;
break;
default:
error = EIO;
SEFS_ERR(this, "%s", "Invalid file context object class.");
throw std::runtime_error(strerror(error));
}
}
else
{
// no object class explicitly given
objclass = QPOL_CLASS_ALL;
}
assert(pmatch[3].rm_so != -1);
char *context_str = s + pmatch[3].rm_so;
char *user, *role, *type, *range;
if (strcmp(context_str, "<<none>>") == 0)
{
user = role = type = range = "";
}
else
{
if (regexec(context_regex, context_str, nmatch, pmatch, 0) != 0)
{
error = EIO;
SEFS_ERR(this, "fcfile context is not legal:\n%s", context_str);
throw std::runtime_error(strerror(error));
}
assert(pmatch[1].rm_so == 0);
context_str[pmatch[1].rm_eo] = '\0';
user = context_str;
assert(pmatch[2].rm_so != -1);
context_str[pmatch[2].rm_eo] = '\0';
role = context_str + pmatch[2].rm_so;
assert(pmatch[3].rm_so != -1);
context_str[pmatch[3].rm_eo] = '\0';
type = context_str + pmatch[3].rm_so;
range = NULL;
if (pmatch[4].rm_so != -1)
{
range = context_str + pmatch[4].rm_so;
}
}
if (range != NULL & range[0] != '\0')
{
if (_mls_set && !_mls)
{
error = EIO;
SEFS_ERR(this, "fcfile context is MLS, but fcfile is not:\n%s", context_str);
throw std::runtime_error(strerror(error));
}
_mls = true;
_mls_set = true;
}
else
{
if (_mls_set && !_mls && strcmp(context_str, "<<none>>") != 0)
{
error = EIO;
SEFS_ERR(this, "fcfile context is not MLS, but fcfile is:\n%s", context_str);
throw std::runtime_error(strerror(error));
}
_mls = true;
_mls_set = false;
}
struct sefs_context_node *context = getContext(user, role, type, range);
sefs_entry *entry = new sefs_entry(this, context, objclass, path, origin);
if (apol_vector_append(_entries, static_cast < void *>(entry)) < 0)
{
error = errno;
delete entry;
SEFS_ERR(this, "%s", strerror(error));
throw std::bad_alloc();
}
}
catch(...)
{
free(s);
errno = error;
throw;
}
free(s);
}
/******************** C functions below ********************/
sefs_fclist_t *sefs_fcfile_create(sefs_callback_fn_t msg_callback, void *varg)
{
sefs_fclist *fclist;
try
{
fclist = new sefs_fcfile(msg_callback, varg);
}
catch(...)
{
errno = ENOMEM;
return NULL;
}
return fclist;
}
sefs_fclist_t *sefs_fcfile_create_from_file(const char *file, sefs_callback_fn_t msg_callback, void *varg)
{
sefs_fclist *fclist;
try
{
fclist = new sefs_fcfile(file, msg_callback, varg);
}
catch(...)
{
errno = ENOMEM;
return NULL;
}
return fclist;
}
sefs_fclist_t *sefs_fcfile_create_from_file_list(const apol_vector_t * files, sefs_callback_fn_t msg_callback, void *varg)
{
sefs_fclist *fclist;
try
{
fclist = new sefs_fcfile(files, msg_callback, varg);
}
catch(...)
{
errno = ENOMEM;
return NULL;
}
return fclist;
}
int sefs_fcfile_append_file(sefs_fcfile_t * fcfile, const char *file)
{
if (fcfile == NULL)
{
SEFS_ERR(fcfile, "%s", strerror(EINVAL));
errno = EINVAL;
return -1;
}
try
{
fcfile->appendFile(file);
}
catch(...)
{
return -1;
}
return 0;
}
size_t sefs_fcfile_append_file_list(sefs_fcfile_t * fcfile, const apol_vector_t * files)
{
if (fcfile == NULL)
{
SEFS_ERR(fcfile, "%s", strerror(EINVAL));
errno = EINVAL;
return 0;
}
return fcfile->appendFileList(files);
}
const apol_vector_t *sefs_fcfile_get_file_list(const sefs_fcfile_t * fcfile)
{
if (fcfile == NULL)
{
SEFS_ERR(fcfile, "%s", strerror(EINVAL));
errno = EINVAL;
return NULL;
}
return fcfile->fileList();
}
#if defined(__ARM_EABI__) && defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 4)
// va_list mangling has been changed in g++ 4.4.
// Add ABI <= g++-4.3 compatibility aliases.
__asm__(".globl _ZN11sefs_fcfileC1EPFvPvPK11sefs_fclistiPKcS0_ES0_\n.set _ZN11sefs_fcfileC1EPFvPvPK11sefs_fclistiPKcS0_ES0_,_ZN11sefs_fcfileC1EPFvPvPK11sefs_fclistiPKcSt9__va_listES0_");
__asm__(".globl _ZN11sefs_fcfileC1EPK11apol_vectorPFvPvPK11sefs_fclistiPKcS3_ES3_\n.set _ZN11sefs_fcfileC1EPK11apol_vectorPFvPvPK11sefs_fclistiPKcS3_ES3_,_ZN11sefs_fcfileC1EPK11apol_vectorPFvPvPK11sefs_fclistiPKcSt9__va_listES3_");
__asm__(".globl _ZN11sefs_fcfileC1EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_\n.set _ZN11sefs_fcfileC1EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_,_ZN11sefs_fcfileC1EPKcPFvPvPK11sefs_fclistiS1_St9__va_listES2_");
__asm__(".globl _ZN11sefs_fcfileC2EPFvPvPK11sefs_fclistiPKcS0_ES0_\n.set _ZN11sefs_fcfileC2EPFvPvPK11sefs_fclistiPKcS0_ES0_,_ZN11sefs_fcfileC2EPFvPvPK11sefs_fclistiPKcSt9__va_listES0_");
__asm__(".globl _ZN11sefs_fcfileC2EPK11apol_vectorPFvPvPK11sefs_fclistiPKcS3_ES3_\n.set _ZN11sefs_fcfileC2EPK11apol_vectorPFvPvPK11sefs_fclistiPKcS3_ES3_,_ZN11sefs_fcfileC2EPK11apol_vectorPFvPvPK11sefs_fclistiPKcSt9__va_listES3_");
__asm__(".globl _ZN11sefs_fcfileC2EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_\n.set _ZN11sefs_fcfileC2EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_,_ZN11sefs_fcfileC2EPKcPFvPvPK11sefs_fclistiS1_St9__va_listES2_");
__asm__(".globl _ZN15sefs_filesystemC1EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_\n.set _ZN15sefs_filesystemC1EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_,_ZN15sefs_filesystemC1EPKcPFvPvPK11sefs_fclistiS1_St9__va_listES2_");
__asm__(".globl _ZN15sefs_filesystemC2EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_\n.set _ZN15sefs_filesystemC2EPKcPFvPvPK11sefs_fclistiS1_S2_ES2_,_ZN15sefs_filesystemC2EPKcPFvPvPK11sefs_fclistiS1_St9__va_listES2_");
#endif
|