/usr/lib/python3/dist-packages/setools/policyrep/__init__.py is in python3-setools 4.0.1-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2014-2016, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools 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.
#
# SETools 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 SETools. If not, see
# <http://www.gnu.org/licenses/>.
#
# pylint: disable=too-many-public-methods
#
# Create a Python representation of the policy.
# The idea is that this is module provides convenient
# abstractions and methods for accessing the policy
# structures.
import logging
from itertools import chain
from errno import ENOENT
try:
import selinux
except ImportError:
pass
from . import qpol
# The libqpol SWIG class is not quite natural for
# Python the policy is repeatedly referenced in the
# function calls, which makes sense for C code
# but not for python code, so each object keeps
# a reference to the policy for internal use.
# This also makes sense since an object would only
# be valid for the policy it comes from.
# Components
from . import boolcond
from . import bounds
from . import default
from . import mls
from . import objclass
from . import polcap
from . import role
from . import typeattr
from . import user
# Rules
from . import mlsrule
from . import rbacrule
from . import terule
# Constraints
from . import constraint
# In-policy Labeling
from . import fscontext
from . import initsid
from . import netcontext
# Xen
from . import xencontext
# Classes useful for policyrep users:
from . import exception
from .netcontext import PortconProtocol, port_range
from .terule import ioctlSet
class SELinuxPolicy(object):
"""The complete SELinux policy."""
def __init__(self, policyfile=None):
"""
Parameter:
policyfile Path to a policy to open.
"""
self.log = logging.getLogger(__name__)
self.policy = None
self.filename = None
if policyfile:
self._load_policy(policyfile)
else:
try:
self._load_running_policy()
except NameError:
raise RuntimeError("Loading the running policy requires libselinux Python bindings")
def __repr__(self):
return "<SELinuxPolicy(\"{0}\")>".format(self.filename)
def __str__(self):
return self.filename
def __deepcopy__(self, memo):
# shallow copy as all of the members are immutable
newobj = SELinuxPolicy.__new__(SELinuxPolicy)
newobj.policy = self.policy
newobj.filename = self.filename
memo[id(self)] = newobj
return newobj
#
# Policy loading functions
#
def _load_policy(self, filename):
"""Load the specified policy."""
self.log.info("Opening SELinux policy \"{0}\"".format(filename))
try:
self.policy = qpol.qpol_policy_factory(str(filename))
except SyntaxError as err:
raise exception.InvalidPolicy("Error opening policy file \"{0}\": {1}".
format(filename, err))
self.log.info("Successfully opened SELinux policy \"{0}\"".format(filename))
self.filename = filename
@staticmethod
def _potential_policies():
"""Generate a list of potential policies to use."""
# try libselinux for current policy
if selinux.selinuxfs_exists():
yield selinux.selinux_current_policy_path()
# otherwise look through the supported policy versions
base_policy_path = selinux.selinux_binary_policy_path()
for version in range(qpol.QPOL_POLICY_MAX_VERSION, qpol.QPOL_POLICY_MIN_VERSION-1, -1):
yield "{0}.{1}".format(base_policy_path, version)
def _load_running_policy(self):
"""Try to load the current running policy."""
self.log.info("Attempting to locate current running policy.")
for filename in self._potential_policies():
try:
self._load_policy(filename)
except OSError as err:
if err.errno != ENOENT:
raise
else:
break
else:
raise RuntimeError("Unable to locate an SELinux policy to load.")
#
# Policy properties
#
@property
def handle_unknown(self):
"""The handle unknown permissions setting (allow,deny,reject)"""
return self.policy.handle_unknown()
@property
def mls(self):
"""(T/F) The policy has MLS enabled."""
return mls.enabled(self.policy)
@property
def version(self):
"""The policy database version (e.g. v29)"""
return self.policy.version()
@property
def target_platform(self):
"""The policy platform (selinux or xen)"""
return self.policy.target_platform()
#
# Policy statistics
#
@property
def allow_count(self):
"""The number of (type) allow rules."""
return self.policy.avrule_allow_count()
@property
def allowxperm_count(self):
"""The number of allowxperm rules."""
return self.policy.avrule_allowx_count()
@property
def auditallow_count(self):
"""The number of auditallow rules."""
return self.policy.avrule_auditallow_count()
@property
def auditallowxperm_count(self):
"""The number of auditallowxperm rules."""
return self.policy.avrule_auditallowx_count()
@property
def boolean_count(self):
"""The number of Booleans."""
return self.policy.bool_count()
@property
def category_count(self):
"""The number of categories."""
return sum(1 for _ in self.categories())
@property
def class_count(self):
"""The number of object classes."""
return self.policy.class_count()
@property
def common_count(self):
"""The number of common permission sets."""
return self.policy.common_count()
@property
def conditional_count(self):
"""The number of conditionals."""
return self.policy.cond_count()
@property
def constraint_count(self):
"""The number of standard constraints."""
return sum(1 for c in self.constraints() if c.ruletype == "constrain")
@property
def default_count(self):
"""The number of default_* rules."""
return sum(1 for d in self.defaults())
@property
def devicetreecon_count(self):
"""The number of Xen devicetreecon statements."""
return self.policy.devicetreecon_count()
@property
def dontaudit_count(self):
"""The number of dontaudit rules."""
return self.policy.avrule_dontaudit_count()
@property
def dontauditxperm_count(self):
"""The number of dontauditxperm rules."""
return self.policy.avrule_dontauditx_count()
@property
def fs_use_count(self):
"""fs_use_* statements."""
return self.policy.fs_use_count()
@property
def genfscon_count(self):
"""The number of genfscon statements."""
return self.policy.genfscon_count()
@property
def initialsids_count(self):
"""The number of initial sid statements."""
return self.policy.isid_count()
@property
def iomemcon_count(self):
"""The number of Xen iomemcon statements."""
return self.policy.iomemcon_count()
@property
def ioportcon_count(self):
"""The number of Xen ioportcon statements."""
return self.policy.ioportcon_count()
@property
def level_count(self):
"""The number of levels."""
return sum(1 for _ in self.levels())
@property
def mlsconstraint_count(self):
"""The number of MLS constraints."""
return sum(1 for c in self.constraints() if c.ruletype == "mlsconstrain")
@property
def mlsvalidatetrans_count(self):
"""The number of MLS validatetrans."""
return sum(1 for v in self.constraints() if v.ruletype == "mlsvalidatetrans")
@property
def netifcon_count(self):
"""The number of netifcon statements."""
return self.policy.netifcon_count()
@property
def neverallow_count(self):
"""The number of neverallow rules."""
return self.policy.avrule_neverallow_count()
@property
def neverallowxperm_count(self):
"""The number of neverallowxperm rules."""
return self.policy.avrule_neverallowx_count()
@property
def nodecon_count(self):
"""The number of nodecon statements."""
return self.policy.nodecon_count()
@property
def pcidevicecon_count(self):
"""The number of Xen pcidevicecon statements."""
return self.policy.pcidevicecon_count()
@property
def permission_count(self):
"""The number of permissions."""
return sum(len(c.perms) for c in chain(self.commons(), self.classes()))
@property
def permissives_count(self):
"""The number of permissive types."""
return self.policy.permissive_count()
@property
def pirqcon_count(self):
"""The number of Xen pirqcon statements."""
return self.policy.pirqcon_count()
@property
def polcap_count(self):
"""The number of policy capabilities."""
return self.policy.polcap_count()
@property
def portcon_count(self):
"""The number of portcon statements."""
return self.policy.portcon_count()
@property
def range_transition_count(self):
"""The number of range_transition rules."""
return self.policy.range_trans_count()
@property
def role_count(self):
"""The number of roles."""
return self.policy.role_count()
@property
def role_allow_count(self):
"""The number of (role) allow rules."""
return self.policy.role_allow_count()
@property
def role_transition_count(self):
"""The number of role_transition rules."""
return self.policy.role_trans_count()
@property
def type_attribute_count(self):
"""The number of (type) attributes."""
return sum(1 for _ in self.typeattributes())
@property
def type_count(self):
"""The number of types."""
return sum(1 for _ in self.types())
@property
def type_change_count(self):
"""The number of type_change rules."""
return self.policy.terule_change_count()
@property
def type_member_count(self):
"""The number of type_member rules."""
return self.policy.terule_member_count()
@property
def type_transition_count(self):
"""The number of type_transition rules."""
return self.policy.terule_trans_count() + self.policy.filename_trans_count()
@property
def typebounds_count(self):
"""The number of typebounds rules."""
return sum(1 for b in self.bounds() if b.ruletype == "typebounds")
@property
def user_count(self):
"""The number of users."""
return self.policy.user_count()
@property
def validatetrans_count(self):
"""The number of validatetrans."""
return sum(1 for v in self.constraints() if v.ruletype == "validatetrans")
#
# Policy components lookup functions
#
def lookup_boolean(self, name):
"""Look up a Boolean."""
return boolcond.boolean_factory(self.policy, name)
def lookup_class(self, name):
"""Look up an object class."""
return objclass.class_factory(self.policy, name)
def lookup_common(self, name):
"""Look up a common permission set."""
return objclass.common_factory(self.policy, name)
def lookup_initialsid(self, name):
"""Look up an initial sid."""
return initsid.initialsid_factory(self.policy, name)
def lookup_level(self, level):
"""Look up a MLS level."""
return mls.level_factory(self.policy, level)
def lookup_sensitivity(self, name):
"""Look up a MLS sensitivity by name."""
return mls.sensitivity_factory(self.policy, name)
def lookup_range(self, range_):
"""Look up a MLS range."""
return mls.range_factory(self.policy, range_)
def lookup_role(self, name):
"""Look up a role by name."""
return role.role_factory(self.policy, name)
def lookup_type(self, name):
"""Look up a type by name."""
return typeattr.type_factory(self.policy, name, deref=True)
def lookup_type_or_attr(self, name):
"""Look up a type or type attribute by name."""
return typeattr.type_or_attr_factory(self.policy, name, deref=True)
def lookup_typeattr(self, name):
"""Look up a type attribute by name."""
return typeattr.attribute_factory(self.policy, name)
def lookup_user(self, name):
"""Look up a user by name."""
return user.user_factory(self.policy, name)
#
# Policy components generators
#
def bools(self):
"""Generator which yields all Booleans."""
for bool_ in self.policy.bool_iter():
yield boolcond.boolean_factory(self.policy, bool_)
def bounds(self):
"""Generator which yields all *bounds statements (typebounds, etc.)"""
for bound in self.policy.typebounds_iter():
yield bounds.bounds_factory(self.policy, bound)
def categories(self):
"""Generator which yields all MLS categories."""
for cat in self.policy.cat_iter():
try:
yield mls.category_factory(self.policy, cat)
except TypeError:
# libqpol unfortunately iterates over aliases too
pass
def classes(self):
"""Generator which yields all object classes."""
for class_ in self.policy.class_iter():
yield objclass.class_factory(self.policy, class_)
def commons(self):
"""Generator which yields all commons."""
for common in self.policy.common_iter():
yield objclass.common_factory(self.policy, common)
def defaults(self):
"""Generator which yields all default_* statements."""
for default_ in self.policy.default_iter():
try:
for default_obj in default.default_factory(self.policy, default_):
yield default_obj
except exception.NoDefaults:
# qpol iterates over all classes. Handle case
# where a class has no default_* settings.
pass
def levels(self):
"""Generator which yields all level declarations."""
for level in self.policy.level_iter():
try:
yield mls.level_decl_factory(self.policy, level)
except TypeError:
# libqpol unfortunately iterates over levels and sens aliases
pass
def polcaps(self):
"""Generator which yields all policy capabilities."""
for cap in self.policy.polcap_iter():
yield polcap.polcap_factory(self.policy, cap)
def roles(self):
"""Generator which yields all roles."""
for role_ in self.policy.role_iter():
yield role.role_factory(self.policy, role_)
def sensitivities(self):
"""Generator which yields all sensitivities."""
# see mls.py for more info on why level_iter is used here.
for sens in self.policy.level_iter():
try:
yield mls.sensitivity_factory(self.policy, sens)
except TypeError:
# libqpol unfortunately iterates over sens and aliases
pass
def types(self):
"""Generator which yields all types."""
for type_ in self.policy.type_iter():
try:
yield typeattr.type_factory(self.policy, type_)
except TypeError:
# libqpol unfortunately iterates over attributes and aliases
pass
def typeattributes(self):
"""Generator which yields all (type) attributes."""
for type_ in self.policy.type_iter():
try:
yield typeattr.attribute_factory(self.policy, type_)
except TypeError:
# libqpol unfortunately iterates over attributes and aliases
pass
def users(self):
"""Generator which yields all users."""
for user_ in self.policy.user_iter():
yield user.user_factory(self.policy, user_)
#
# Policy rules generators
#
def mlsrules(self):
"""Generator which yields all MLS rules."""
for rule in self.policy.range_trans_iter():
yield mlsrule.mls_rule_factory(self.policy, rule)
def rbacrules(self):
"""Generator which yields all RBAC rules."""
for rule in chain(self.policy.role_allow_iter(),
self.policy.role_trans_iter()):
yield rbacrule.rbac_rule_factory(self.policy, rule)
def terules(self):
"""Generator which yields all type enforcement rules."""
for rule in chain(self.policy.avrule_iter(),
self.policy.avrulex_iter(),
self.policy.terule_iter(),
self.policy.filename_trans_iter()):
yield terule.te_rule_factory(self.policy, rule)
#
# Policy rule type validators
#
@staticmethod
def validate_bounds_ruletype(types):
"""Validate constraint types."""
return bounds.validate_ruletype(types)
@staticmethod
def validate_constraint_ruletype(types):
"""Validate constraint types."""
return constraint.validate_ruletype(types)
@staticmethod
def validate_default_ruletype(types):
"""Validate default_* types."""
return default.validate_ruletype(types)
@staticmethod
def validate_default_value(value):
"""Validate default_* values."""
return default.validate_default_value(value)
@staticmethod
def validate_default_range(value):
"""Validate default_range range."""
return default.validate_default_range(value)
@staticmethod
def validate_fs_use_ruletype(types):
"""Validate fs_use_* rule types."""
return fscontext.validate_ruletype(types)
@staticmethod
def validate_mls_ruletype(types):
"""Validate MLS rule types."""
return mlsrule.validate_ruletype(types)
@staticmethod
def validate_rbac_ruletype(types):
"""Validate RBAC rule types."""
return rbacrule.validate_ruletype(types)
@staticmethod
def validate_te_ruletype(types):
"""Validate type enforcement rule types."""
return terule.validate_ruletype(types)
#
# Constraints generators
#
def constraints(self):
"""Generator which yields all constraints (regular and MLS)."""
for constraint_ in chain(self.policy.constraint_iter(),
self.policy.validatetrans_iter()):
yield constraint.constraint_factory(self.policy, constraint_)
#
# In-policy Labeling statement generators
#
def fs_uses(self):
"""Generator which yields all fs_use_* statements."""
for fs_use in self.policy.fs_use_iter():
yield fscontext.fs_use_factory(self.policy, fs_use)
def genfscons(self):
"""Generator which yields all genfscon statements."""
for fscon in self.policy.genfscon_iter():
yield fscontext.genfscon_factory(self.policy, fscon)
def initialsids(self):
"""Generator which yields all initial SID statements."""
for sid in self.policy.isid_iter():
yield initsid.initialsid_factory(self.policy, sid)
def netifcons(self):
"""Generator which yields all netifcon statements."""
for ifcon in self.policy.netifcon_iter():
yield netcontext.netifcon_factory(self.policy, ifcon)
def nodecons(self):
"""Generator which yields all nodecon statements."""
for node in self.policy.nodecon_iter():
yield netcontext.nodecon_factory(self.policy, node)
def portcons(self):
"""Generator which yields all portcon statements."""
for port in self.policy.portcon_iter():
yield netcontext.portcon_factory(self.policy, port)
#
# Xen labeling statements
#
def iomemcons(self):
"""Generator which yields all iomemcon statements."""
for mem_addr in self.policy.iomemcon_iter():
yield xencontext.iomemcon_factory(self.policy, mem_addr)
def ioportcons(self):
"""Generator which yields all ioportcon statements."""
for port in self.policy.ioportcon_iter():
yield xencontext.ioportcon_factory(self.policy, port)
def pcidevicecons(self):
"""Generator which yields all pcidevicecon statements."""
for device in self.policy.pcidevicecon_iter():
yield xencontext.pcidevicecon_factory(self.policy, device)
def pirqcons(self):
"""Generator which yields all pirqcon statements."""
for irq in self.policy.pirqcon_iter():
yield xencontext.pirqcon_factory(self.policy, irq)
def devicetreecons(self):
"""Generator which yields all devicetreecon statements."""
for path in self.policy.devicetreecon_iter():
yield xencontext.devicetreecon_factory(self.policy, path)
|