3
O+eU @ s d dl Z d dlZd dlmZmZ d dlmZ d dlZd dl m
Z
mZ ddlm
Z
mZ ddlmZ dgZed d
ddd
dddgZed
ddddgZG dd dZG dd dZdS ) N)defaultdict
namedtuple)suppress)
NetworkXErrorNetworkXNoPath )EdgeAttrDictEdgeAttrList)
TERuletypeDomainTransitionAnalysisstepsourcetarget
transitionentrypointssetexec
dyntransition
setcurrentname
entrypointexecutetype_transitionc @ s e Zd ZdZd!ddZedd Zejdd Zed d
Zejdd
Zdd
Z d"ddZ
dd Zdd Zdd Z
edd Zdd Zdd Zdd Zdd ZdS )#r zDomain transition analysis.FNc C s> t jt| _|| _|| _|| _d| _d| _t j
| _d| _dS )zD
Parameter:
policy The policy to analyze.
TN)
loggingZ getLogger__name__logpolicyexcludereverserebuildgraphrebuildsubgraphnxZDiGraphGsubG)selfr r r r$ /usr/lib64/python3.6/dta.py__init__6 s
z!DomainTransitionAnalysis.__init__c C s | j S )N)_reverse)r# r$ r$ r% r E s z DomainTransitionAnalysis.reversec C s t || _d| _d S )NT)boolr' r )r# directionr$ r$ r% r I s
c C s | j S )N)_exclude)r# r$ r$ r% r N s z DomainTransitionAnalysis.excludec s* |r fdd|D _ ng _ d _d S )Nc s g | ]} j j|qS r$ )r lookup_type).0t)r# r$ r%
U s z4DomainTransitionAnalysis.exclude..T)r* r )r# typesr$ )r# r% r R s c c sj | j j|}| j j|}| jr&| j | jjdj|| tt | j t
j| j||V W dQ R X dS )a
Generator which yields one shortest domain transition path
between the source and target types (there may be more).
Parameters:
source The source type.
target The target type.
Yield: generator(steps)
steps A generator that returns the tuple of
source, target, and rules for each
domain transition.
z8Generating one domain transition path from {0} to {1}...N)
r r+ r _build_subgraphr infoformatr r )_DomainTransitionAnalysis__generate_stepsr
shortest_pathr" )r# r
r sr- r$ r$ r% r4 [ s
z&DomainTransitionAnalysis.shortest_path c c s |dk rt d| jj|}| jj|}| jr6| j | jjdj||| tt . x&t
j| j|||D ]}| j
|V qjW W dQ R X dS )a
Generator which yields all domain transition paths between
the source and target up to the specified maximum path
length.
Parameters:
source The source type.
target The target type.
maxlen Maximum length of paths.
Yield: generator(steps)
steps A generator that returns the tuple of
source, target, and rules for each
domain transition.
r z%Maximum path length must be positive.zIGenerating all domain transition paths from {0} to {1}, max length {2}...N)
ValueErrorr r+ r r0 r r1 r2 r r r Zall_simple_pathsr" r3 )r# r
r maxlenr5 r- pathr$ r$ r% all_pathsx s
z"DomainTransitionAnalysis.all_pathsc c sx | j j|}| j j|}| jr&| j | jjdj|| tt, x$t j
| j||D ]}| j|V qVW W dQ R X dS )a
Generator which yields all shortest domain transition paths
between the source and target types.
Parameters:
source The source type.
target The target type.
Yield: generator(steps)
steps A generator that returns the tuple of
source, target, and rules for each
domain transition.
zBGenerating all shortest domain transition paths from {0} to {1}...N)
r r+ r r0 r r1 r2 r r r all_shortest_pathsr" r3 )r# r
r r5 r- r9 r$ r$ r% r; s
z+DomainTransitionAnalysis.all_shortest_pathsc c s | j j|}| jr| j | jjdj|| jr0dnd tt n xf| j
j|D ]V\}}t| j
||}| jrx|| }}n
|| }}t
|||j| j||j|j|jV qPW W dQ R X dS )aM
Generator which yields all domain transitions out of a
specified source type.
Parameters:
type_ The starting type.
Yield: generator(steps)
steps A generator that returns the tuple of
source, target, and rules for each
domain transition.
z)Generating all domain transitions {1} {0}zin tozout fromN)r r+ r r0 r r1 r2 r r r r" Z out_edgesEdgestep_outputr /_DomainTransitionAnalysis__generate_entrypointsr r r )r# Ztype_r5 r
r edgereal_sourcereal_targetr$ r$ r% transitions s"
z$DomainTransitionAnalysis.transitionsc C s | j r| j tj| jS )zR
Get the domain transition graph statistics.
Return: str
)r _build_graphr r1 r! )r# r$ r$ r% get_stats s z"DomainTransitionAnalysis.get_statsc s fdd j D S )a
Creates a list of entrypoint, execute, and
type_transition rules for each entrypoint.
Parameter:
data The dictionary of entrypoints.
Return: list of tuple(type, entry, exec, trans)
type The entrypoint type.
entry The list of entrypoint rules.
exec The list of execute rules.
trans The list of type_transition rules.
c s, g | ]$}t | j| j| j| qS r$ )entrypoint_outputr r r )r, e)r? r$ r% r. s zCDomainTransitionAnalysis.__generate_entrypoints..)r )r? r$ )r? r% Z__generate_entrypoints s
z/DomainTransitionAnalysis.__generate_entrypointsc c s~ xxt dt|D ]f}||d }|| }t| j||}| jrH|| }}n
|| }}t|||j| j||j|j |j
V qW dS )a
Generator which yields the source, target, and associated rules
for each domain transition.
Parameter:
path A list of graph node names representing an information flow path.
Yield: tuple(source, target, transition, entrypoints,
setexec, dyntransition, setcurrent)
source The source type for this step of the domain transition.
target The target type for this step of the domain transition.
transition The list of transition rules.
entrypoints Generator which yields entrypoint-related rules.
setexec The list of setexec rules.
dyntranstion The list of dynamic transition rules.
setcurrent The list of setcurrent rules.
r N)rangelenr<