
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
^j@                 @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	Z	ddl
mZ ddl
mZ ddlmZ dd	lmZ d
ddddgZdZdZG dd dZdd Zdd Zdd ZG dd dZdS )    N)OrderedDict)ENOENT)suppress   )	exception)	policyrep)PermissionMapDescriptor)
TERuletyperwbnu
   c               @   s   e Zd ZdZd%ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS )&PermissionMapz-Permission Map for information flow analysis.Nc             C   sN   t jt| _t | _d| _|r*| j| n tj	d}dj
|j}| j| dS )z\
        Parameter:
        permmapfile     The path to the permission map to load.
        NZsetoolsz{0}/setools/perm_map)loggingZ	getLogger__name__logr   permmappermmapfileloadpkg_resourcesZget_distributionformatlocation)selfr   Zdistropath r   /usr/lib64/python3.6/permmap.py__init__*   s    
zPermissionMap.__init__c             C   s   | j S )N)r   )r   r   r   r   __str__;   s    zPermissionMap.__str__c             C   s8   t jt }| j|_tj| j|_| j|_||t| < |S )N)r   __new__r   copydeepcopyr   r   id)r   memoZnewobjr   r   r   __deepcopy__>   s    
zPermissionMap.__deepcopy__c             c   s0   x*| j  D ]}x| j|D ]
}|V  qW q
W d S )N)classesperms)r   clsmappingr   r   r   __iter__F   s    zPermissionMap.__iter__c             C   sb  | j jdj| t|d}d}d}d}d}| jj  xt|ddD ]\}}|j }	t|	dksJ|	d d dkrzqJ|dkryt	|	d }W n< t
k
r }
 z tjdj|||	d |
W Y dd}
~
X nX |dk rtjd	j|||	d d
}qJ|d
krt|	dks|	d dkr0tjdj|||	t|	d }yt	|	d
 }W n> t
k
r }
 z tjdj|||	d
 |
W Y dd}
~
X nX |dk rtjdj|||	d
 |d7 }||krtjdj|||t | j|< d}d}qJ|dkrJt|	d }t|	d }|tkr,tjdj|||	d yt	|	d
 }W n> t
k
rz }
 z tjdj|||	d
 |
W Y dd}
~
X nX t|  kotkn  stjdj|||	d
 tt| j jdj|||| |dkr| j jdj|| t| j||dd}||_||_|d7 }|d7 }||krJd
}qJW W dQ R X || _| j jdj| | j jdj|| dS )z\
        Parameter:
        permmapfile     The path to the permission map to load.
        zOpening permission map "{0}"r
   r   r   )start#z&{0}:{1}:Invalid number of classes: {2}Nz/{0}:{1}:Number of classes must be positive: {2}      classz&{0}:{1}:Invalid class declaration: {2}z*{0}:{1}:Invalid number of permissions: {2}z3{0}:{1}:Number of permissions must be positive: {2}z{0}:{1}:Extra class found: {2}z/{0}:{1}:Invalid information flow direction: {2}z&{0}:{1}:Invalid permission weight: {2}z.{0}:{1}:Permission weight must be {3}-{4}: {2}zRead {0}:{1} {2} {3}r   zPermission {0}:{1} is unmapped.T)createz(Successfully opened permission map "{0}"z+Read {0} classes and {1} total permissions.)r   infor   openr   clear	enumeratesplitlenint
ValueErrorr   ZPermissionMapParseErrorstrr   infoflow_directions
min_weight
max_weightdebugMapping	directionweightr   )r   r   mapfileZtotal_permsZclass_countZnum_classesstateZline_numlineentryex
class_nameZ	num_permsZ
perm_count	perm_nameZflow_directionr@   r)   r   r   r   r   K   s    
$
$


$






zPermissionMap.loadc       	      C   s(  t |d}| jjdj| |jdjt| j x| jj D ]\}}|jdj|t| x|j D ]\}}|d }|d }t|  kot	kn  st
dj||||tkst
dj||||d	kr| jjd
j|| |jdj||| qjW |jd qBW | jjdj| W dQ R X dS )z
        Save the permission map to the specified path.  Existing files
        will be overwritten.

        Parameter:
        permmapfile         The path to write the permission map.
        r   zWriting permission map to "{0}"z{0}

zclass {0} {1}
r?   r@   z={0}:{1} weight is out of range ({2}). This is an SETools bug.z@{0}:{1} flow direction ({2}) is invalid. This is an SETools bug.r   z1Warning: permission {0} in class {1} is unmapped.z{0:>20} {1:>9} {2:>9}

z*Successfully wrote permission map to "{0}"N)r2   r   r1   r   writer6   r   itemsr;   r<   AssertionErrorr:   Zwarning)	r   r   rA   	classnamer'   ZpermnameZsettingsr?   r@   r   r   r   save   s(    


zPermissionMap.savec             c   s   | j j E dH  dS )zw
        Generate class names in the permission map.

        Yield:
        class       An object class name.
        N)r   keys)r   r   r   r   r&      s    zPermissionMap.classesc             c   sf   y,x&| j | j D ]}t| j ||V  qW W n4 tk
r` } ztjdj||W Y dd}~X nX dS )z
        Generate permission mappings for the specified class.

        Parameter:
        class_      An object class name.

        Yield:
        Mapping     A permission's complete map (weight, direction, enabled)
        z{0} is not mapped.N)r   rN   r>   KeyErrorr   UnmappedClassr   )r   class_permrE   r   r   r   r'      s
    
zPermissionMap.permsc             C   s   t | j||S )z)Retrieve a specific permission's mapping.)r>   r   )r   rQ   rR   r   r   r   r)      s    zPermissionMap.mappingc             C   s   x| j |D ]
}d|_qW dS )a  
        Exclude all permissions in an object class for calculating rule weights.

        Parameter:
        class_              The object class to exclude.

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        FN)r'   enabled)r   rQ   rR   r   r   r   exclude_class   s    
zPermissionMap.exclude_classc             C   s   dt | j||_dS )a  
        Exclude a permission for calculating rule weights.

        Parameter:
        class_              The object class of the permission.
        permission          The permission name to exclude.

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        UnmappedPermission  The specified permission is not mapped for the object class.
        FN)r>   r   rS   )r   rQ   
permissionr   r   r   exclude_permission  s    z PermissionMap.exclude_permissionc             C   s   x| j |D ]
}d|_qW dS )a  
        Include all permissions in an object class for calculating rule weights.

        Parameter:
        class_              The object class to include.

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        TN)r'   rS   )r   rQ   rR   r   r   r   include_class  s    zPermissionMap.include_classc             C   s   dt | j||_dS )a  
        Include a permission for calculating rule weights.

        Parameter:
        class_              The object class of the permission.
        permission          The permission name to include.

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        UnmappedPermission  The specified permission is not mapped for the object class.
        TN)r>   r   rS   )r   rQ   rU   r   r   r   include_permission'  s    z PermissionMap.include_permissionc             C   s   x|j  D ]}t|}|| jkr@| jjdj|| t | j|< |j}tt	j
 ||jjO }W dQ R X xB|D ]:}|| j| krn| jjdj||| t| j||dd qnW q
W dS )zHCreate mappings for all classes and permissions in the specified policy.z"Adding unmapped class {0} from {1}Nz.Adding unmapped permission {0} in {1} from {2}T)r0   )r&   r9   r   r   r=   r   r   r'   r   r   ZNoCommoncommonr>   )r   ZpolicyrQ   rF   r'   rG   r   r   r   
map_policy6  s    


zPermissionMap.map_policyc             C   s   d}d}t |j}|jtjkr0tjdj|jxv|jD ]l}t	| j
||}|jsRq8|jdkrjt||j}q8|jdkrt||j}q8|jdkr8t||j}t||j}q8W ||fS )aT  
        Get the type enforcement rule's information flow read and write weights.

        Parameter:
        rule            A type enforcement rule.

        Return: Tuple(read_weight, write_weight)
        read_weight     The type enforcement rule's read weight.
        write_weight    The type enforcement rule's write weight.
        r   z1{0} rules cannot be used for calculating a weightr
   r   r   )r9   ZtclassZruletyper	   Zallowr   ZRuleTypeErrorr   r'   r>   r   rS   r?   maxr@   )r   ZruleZwrite_weightZread_weightrF   rG   r)   r   r   r   rule_weightJ  s$    




zPermissionMap.rule_weightc             C   s   |t | j||_dS )a  
        Set the information flow direction of a permission.

        Parameter:
        class_              The object class of the permission.
        permission          The permission name.
        direction           The information flow direction the permission (r/w/b/n).

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        UnmappedPermission  The specified permission is not mapped for the object class.
        N)r>   r   r?   )r   rQ   rU   r?   r   r   r   set_directionq  s    zPermissionMap.set_directionc             C   s   |t | j||_dS )a  
        Set the weight of a permission.

        Parameter:
        class_              The object class of the permission.
        permission          The permission name.
        weight              The weight of the permission (1-10).

        Exceptions:
        UnmappedClass       The specified object class is not mapped.
        UnmappedPermission  The specified permission is not mapped for the object class.
        N)r>   r   r@   )r   rQ   rU   r@   r   r   r   
set_weight  s    zPermissionMap.set_weight)N)r   
__module____qualname____doc__r   r   r%   r*   r   rM   r&   r'   r)   rT   rV   rW   rX   rZ   r\   r]   r^   r   r   r   r   r   &   s$   
q%	'r   c             C   s*   t |   kotkn  s&tdj| | S )Nz$Permission weights must be 1-10: {0})r;   r<   r8   r   )r@   r   r   r   validate_weight  s    rb   c             C   s   | t krtdj| | S )Nz'Invalid information flow direction: {0})r:   r8   r   )r?   r   r   r   validate_direction  s    rc   c             C   s   t | S )N)bool)rS   r   r   r   validate_enabled  s    re   c               @   s@   e Zd ZdZedeZedeZede	Z
dddZdd	 Zd
S )r>   z1A mapping for a permission in the permission map.r@   r?   rS   Fc             C   s   || _ || _|| _|rD|| j kr,t | j |< dddd| j | |< n:|| j kr^tjdj||| j | kr~tjdj||d S )Nr   r   T)r?   r@   rS   z{0} is not mapped.z{0}:{1} is not mapped.)perm_maprQ   rR   r   r   rP   r   ZUnmappedPermission)r   rf   rL   rU   r0   r   r   r   r     s    

zMapping.__init__c             C   s(   | j |j kr| j|jk S | j |j k S d S )N)rQ   rR   )r   otherr   r   r   __lt__  s    zMapping.__lt__N)F)r   r_   r`   ra   r   rb   r@   rc   r?   re   rS   r   rh   r   r   r   r   r>     s   



r>   )sysr   r!   collectionsr   errnor   
contextlibr   r    r   r   Zdescriptorsr   r	   r:   r;   r<   r   rb   rc   re   r>   r   r   r   r   <module>   s(     o