
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
O+eU              	   @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlZd dl	m
Z
mZ ddlmZmZ ddlmZ dgZed	d
ddddddgZedddddgZG dd dZG dd dZdS )    N)defaultdict
namedtuple)suppress)NetworkXErrorNetworkXNoPath   )EdgeAttrDictEdgeAttrList)
TERuletypeDomainTransitionAnalysisstepsourcetarget
transitionentrypointssetexecdyntransition
setcurrentname
entrypointexecutetype_transitionc               @   s   e Zd ZdZd!ddZedd Zejdd Zed	d
 Zejdd
 Zdd Z	d"ddZ
dd Zdd Zdd Zedd Zdd Zdd Zdd Zdd  ZdS )#r   zDomain transition analysis.FNc             C   s>   t jt| _|| _|| _|| _d| _d| _t	j
 | _d| _dS )zD
        Parameter:
        policy   The policy to analyze.
        TN)loggingZ	getLogger__name__logpolicyexcludereverserebuildgraphrebuildsubgraphnxZDiGraphGsubG)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 fdd|D  _ ng  _ d _d S )Nc                s   g | ]} j j|qS r$   )r   lookup_type).0t)r#   r$   r%   
<listcomp>U   s    z4DomainTransitionAnalysis.exclude.<locals>.<listcomp>T)r*   r   )r#   typesr$   )r#   r%   r   R   s    c             c   sj   | j j|}| j j|}| jr&| j  | jjdj|| tt | j	t
j| j||V  W dQ R X dS )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   infoformatr   r   )_DomainTransitionAnalysis__generate_stepsr    shortest_pathr"   )r#   r   r   sr-   r$   r$   r%   r4   [   s    
z&DomainTransitionAnalysis.shortest_path   c             c   s   |dk rt d| jj|}| jj|}| jr6| j  | jjdj||| tt	. x&t
j| j|||D ]}| j|V  qjW W dQ R X dS )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    Zall_simple_pathsr"   r3   )r#   r   r   maxlenr5   r-   pathr$   r$   r%   	all_pathsx   s    

z"DomainTransitionAnalysis.all_pathsc             c   sx   | j j|}| j j|}| jr&| j  | jjdj|| tt, x$t	j
| j||D ]}| j|V  qVW W dQ R X dS )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|}| jr| j  | jjdj|| jr0dnd tt	n xf| j
j|D ]V\}}t| j
||}| jrx|| }}n
|| }}t|||j| j||j|j|jV  qPW W dQ R X dS )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}zin tozout fromN)r   r+   r   r0   r   r1   r2   r   r   r   r"   Z	out_edgesEdgestep_outputr   /_DomainTransitionAnalysis__generate_entrypointsr   r   r   )r#   Ztype_r5   r   r   edgereal_sourcereal_targetr$   r$   r%   transitions   s"    


z$DomainTransitionAnalysis.transitionsc             C   s   | j r| j  tj| jS )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    fdd 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| qS r$   )entrypoint_outputr   r   r   )r,   e)r?   r$   r%   r.      s   zCDomainTransitionAnalysis.__generate_entrypoints.<locals>.<listcomp>)r   )r?   r$   )r?   r%   Z__generate_entrypoints   s    
z/DomainTransitionAnalysis.__generate_entrypointsc          	   c   s~   xxt dt|D ]f}||d  }|| }t| j||}| jrH|| }}n
|| }}t|||j| j||j|j	|j
V  qW dS )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)rangelenr<   r"   r   r=   r   r>   r   r   r   )r#   r9   r5   r   r   r?   r@   rA   r$   r$   r%   Z__generate_steps  s    
z)DomainTransitionAnalysis.__generate_stepsc             C   s  | j j  dj| j| j _| jjdj| j tt}tt}tdd }tdd }tdd }x4| jj	 D ]$}|j
tjkr8|jdkrqp|j}|jdkrd	|krxJtj|jj |jj D ].\}}	||	krt| j ||	d
d}
|
jj| qW d|krPxNtj|jj |jj D ]2\}}	||	krt| j ||	d
d}|jj| qW d|kr~x"|jj D ]}|| j| qfW d|kr6x|jj D ]}|| j| qW nd|krx8tj|jj |jj D ]\}}	|| |	 j| qW d|krxtj|jj |jj D ]\}}	|| |	 j| qW qp|j
tjkrp|jdkrRqp|j}x<tj|jj |jj D ] \}}	|| |	 | j| qrW qpW g }g }g }x| j j D ]\}}	t| j ||	}
d}d}|
jrt||	 j }t|| j }|j|}|sd
}nx|D ]}||ks4|| | rh|
j|  ||	 | 7  < |
j|  || | 7  < || | |	 r|
j|  || | |	 7  < qW ||kr|
j j!||  |
j  r|
j rd
}nd
}|
jr ||kr|
j"j!||  nd
}nd
}|r|r|j|
 n"|r.|j|
 n|r|j|
 qW | j j#| x"|D ]}
|
`|
`|
`|
`|
` qVW x|D ]}
|
`|
`"qzW d| _$d
| _%| jjd | jj&djt'j(| j t'j)| j  d S )Nz Domain transition graph for {0}.z,Building domain transition graph from {0}...c               S   s   t tS )N)r   listr$   r$   r$   r%   <lambda>n  s    z7DomainTransitionAnalysis._build_graph.<locals>.<lambda>c               S   s   t tS )N)r   rI   r$   r$   r$   r%   rJ   o  s    c               S   s   t dd S )Nc               S   s   t tS )N)r   rI   r$   r$   r$   r%   rJ   r  s    zIDomainTransitionAnalysis._build_graph.<locals>.<lambda>.<locals>.<lambda>)r   r$   r$   r$   r%   rJ   r  s    processfiler   T)creater   r   r   r   r   Fz+Completed building domain transition graph.z$Graph stats: nodes: {0}, edges: {1}.)rK   rL   )*r!   clearr2   r   r   r   r1   r   rI   ZterulesZruletyper
   ZallowZtclassperms	itertoolsproductr   expandr   r<   r   appendr   r   defaultedgessetkeysintersectionr   r   r   extendr   remove_edges_fromr   r   debugr    number_of_nodesnumber_of_edges)r#   r   r   r   r   Z
type_transZrulerO   r5   r-   r?   rF   dZinvalid_edgeZclear_transitionZclear_dyntransitionZinvalid_transZinvalid_dyntransentryZexematchmr$   r$   r%   rC   a  s    

"
"




"" 

$







z%DomainTransitionAnalysis._build_graphc             C   s   g }x| j j D ]\}}t| j ||}t|j}|j| j |sBqx8|D ]0}|j|= |j|= tt	 |j
|= W d Q R X qHW |j r|j r|j| qW | j j| d S )N)r"   rU   r<   rV   r   intersection_updater   r   r   KeyErrorr   r   rS   rZ   )r#   Zinvalid_edgesr   r   r?   r   rF   r$   r$   r%   Z__remove_excluded_entrypoints  s    


z6DomainTransitionAnalysis.__remove_excluded_entrypointsc             C   s   | j r| j  | jjd | jjdj| j | jjdj| j | jrZ| jjdd| _	n| jj
 | _	| jr| j	j| j | j  d| _| jjd | jjdjtj| j	tj| j	 d S )	Nz$Building domain transition subgraph.zExcluding {0}zReverse {0}T)copyFz.Completed building domain transition subgraph.z'Subgraph stats: nodes: {0}, edges: {1}.)r   rC   r   r1   r[   r2   r   r   r!   r"   rd   Zremove_nodes_from6_DomainTransitionAnalysis__remove_excluded_entrypointsr   r    r\   r]   )r#   r$   r$   r%   r0     s     

z(DomainTransitionAnalysis._build_subgraph)FN)r6   )r   
__module____qualname____doc__r&   propertyr   setterr   r4   r:   r;   rB   rD   staticmethodr>   r3   rC   re   r0   r$   r$   r$   r%   r   2   s"   
	
$'^ c               @   sb   e Zd ZdZedZedZedZedZe	dZ
e	dZe	dZdd
dZdd Zdd ZdS )r<   aS  
    A graph edge.  Also used for returning domain transition steps.

    Parameters:
    graph       The NetworkX graph.
    source      The source type of the edge.
    target      The target tyep of the edge.

    Keyword Parameters:
    create      (T/F) create the edge if it does not exist.
                The default is False.
    r   r   r   r   r   r   r   Fc             C   sj   || _ || _|| _| j j||sf|s.tdn8| j j|| d | _d | _d | _d | _	d | _
d | _d | _d S )NzEdge does not exist in graph)r!   r   r   Zhas_edger7   Zadd_edger   r   r   r   r   r   r   )r#   Zgraphr   r   rM   r$   r$   r%   r&   E  s    
zEdge.__init__c                s4   t |tr& fddt|jd D S  j|S d S )Nc                s   g | ]} j |qS r$   )_index_to_item)r,   i)r#   r$   r%   r.   [  s    z$Edge.__getitem__.<locals>.<listcomp>r6   )
isinstanceslicerG   indicesrl   )r#   keyr$   )r#   r%   __getitem__W  s    
zEdge.__getitem__c             C   s.   |dkr| j S |dkr| jS tdj|dS )z'Return source or target based on index.r   r   z,Invalid index (edges only have 2 items): {0}N)r   r   
IndexErrorr2   )r#   indexr$   r$   r%   rl   _  s
    zEdge._index_to_itemN)F)r   rf   rg   rh   r	   r   r   r   r   r   r   r   r   r&   rr   rl   r$   r$   r$   r%   r<   .  s   
r<   )rP   r   collectionsr   r   
contextlibr   Znetworkxr    Znetworkx.exceptionr   r   Zdescriptorsr   r	   Z	policyrepr
   __all__r=   rE   r   r<   r$   r$   r$   r%   <module>   s2        