509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!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ƒ  �xêt|dd�D �]Ø\}}|jƒ }	t|	ƒdksJ|	d d dkrzqJ|dkröyt	|	d ƒ}W n< t
k
rÎ }
 z tjdj|||	d ƒƒ|
‚W Y dd}
~
X nX |dk rðtjd	j|||	d ƒƒ‚d
}qJ|d
k�rêt|	ƒ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 �r®tjdj|||	d
 ƒƒ‚|d7 }||k�rÔt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�o�tkn  �s´t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o”t	kn  s¬t
dj|||ƒƒ‚|tksÆt
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r‚t||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