
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
Klf;              	   @   s   d Z ddlZddlZddljZyddlT W n   Y nX ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ dZdZdZG dd dZdefddZdd ZG dd dZdd ZdS )z>
classes and algorithms for the generation of SELinux policy.
    N)*   )	refpolicy)objectmodel)access)
interfaces)matching)util   c               @   s   e Zd ZdZdddZd ddZd!dd	Zefd
dZdd Z	dd Z
dd Zd"ddZdd Zdd Zdd Zdd Zdd ZdS )#PolicyGeneratora  Generate a reference policy module from access vectors.

    PolicyGenerator generates a new reference policy module
    or updates an existing module based on requested access
    in the form of access vectors.

    It generates allow rules and optionally module require
    statements, reference policy interfaces, and extended
    permission access vector rules. By default only allow rules
    are generated. The methods .set_gen_refpol, .set_gen_requires
    and .set_gen_xperms turns on interface generation,
    requires generation, and xperms rules genration respectively.

    PolicyGenerator can also optionally add comments explaining
    why a particular access was allowed based on the audit
    messages that generated the access. The access vectors
    passed in must have the .audit_msgs field set correctly
    and .explain set to SHORT|LONG_EXPLANATION to enable this
    feature.

    The module created by PolicyGenerator can be passed to
    output.ModuleWriter to output a text representation.
    Nc             C   s>   d| _ t| _d| _|r|| _n
tj | _d| _d| _d| _	dS )zInitialize a PolicyGenerator with an optional
        existing module.

        If the module paramater is not None then access
        will be added to the passed in module. Otherwise
        a new reference policy module will be created.
        NF)
ifgenNO_EXPLANATIONexplaingen_requiresmoduler   ZModule	dontauditxpermsdomains)selfr    r   /usr/lib/python3.6/policygen.py__init__E   s    
zPolicyGenerator.__init__c             C   s*   |rt ||| _d| _nd| _| j  dS )a?  Set whether reference policy interfaces are generated.

        To turn on interface generation pass in an interface set
        to use for interface generation. To turn off interface
        generation pass in None.

        If interface generation is enabled requires generation
        will also be enabled.
        TN)InterfaceGeneratorr   r   "_PolicyGenerator__set_module_style)r   Zif_set	perm_mapsr   r   r   set_gen_refpolY   s
    
zPolicyGenerator.set_gen_refpolTc             C   s
   || _ dS )a&  Set whether module requires are generated.

        Passing in true will turn on requires generation and
        False will disable generation. If requires generation is
        disabled interface generation will also be disabled and
        can only be re-enabled via .set_gen_refpol.
        N)r   )r   Zstatusr   r   r   set_gen_requiresk   s    z PolicyGenerator.set_gen_requiresc             C   s
   || _ dS )z)Set whether access is explained.
        N)r   )r   r   r   r   r   set_gen_explainu   s    zPolicyGenerator.set_gen_explainc             C   s
   || _ d S )N)r   )r   r   r   r   r   set_gen_dontauditz   s    z!PolicyGenerator.set_gen_dontauditc             C   s
   || _ dS )zSSet whether extended permission access vector rules
        are generated.
        N)r   )r   r   r   r   r   set_gen_xperms}   s    zPolicyGenerator.set_gen_xpermsc             C   s.   | j rd}nd}x| jj D ]
}||_qW d S )NTF)r   r   module_declarationsr   )r   r   modr   r   r   Z__set_module_style   s
    z"PolicyGenerator.__set_module_style1.0c             C   s\   d}x| j j D ]}|}qW |s8tj }| j jjd| ||_||_| jrRd|_nd|_dS )z?Set the name of the module and optionally the version.
        Nr   TF)	r   r    r   ZModuleDeclarationchildreninsertnameversionr   )r   r%   r&   mr!   r   r   r   set_module_name   s    zPolicyGenerator.set_module_namec             C   s   | j rt | j | jS )N)r   r   )r   r   r   r   
get_module   s    
zPolicyGenerator.get_modulec             C   sv  t j|}| jr|j|_d|_| jr>tt jt	|| jd|_|j
tjkrl| jd7  _|jrl| jd7  _|j
tjkr| jd7  _|j
tjkrt|jdkr| jddjd	d
 |jD  7  _n| jd|jd d  7  _|j
tjkrP| jd7  _| jd7  _| jd|jd  7  _x*|jdd D ]}| jd| 7  _q4W y|j
tjkrTd|jkrTd|jksd|jkrT| jsttddd d | _g }xHdd
 ttgt|jt|jt|jiD D ]}|| jkr|j| qW t|dkr$| jd|j|jdj|f 7  _n0t|dkrT| jd|j|jdj|f 7  _W n   Y nX | jj j| dS )z Add access vector rule.
         )	verbosityz0
#!!!! This avc is allowed in the current policyzN
#!!!! This av rule may have been overridden by an extended permission av rulez:
#!!!! This avc has a dontaudit rule in the current policyr   zH
#!!!! This avc can be allowed using one of the these booleans:
#     %sz, c             S   s   g | ]}|d  qS )r   r   ).0xr   r   r   
<listcomp>   s    z1PolicyGenerator.__add_av_rule.<locals>.<listcomp>z5
#!!!! This avc can be allowed using the boolean '%s'r   z
#!!!! This avc is a constraint violation.  You would need to modify the attributes of either the source or target types to allow this access.z
#Constraint rule: z
#	Nz?
#	Possible cause is the source %s and target %s are different.writediropenZdomain)r%   typesc             S   s   g | ]}|t  qS r   )ZTCONTEXT)r,   r-   r   r   r   r.      s    zL
#!!!! The source type '%s' can write to a '%s' of the following type:
# %s
zM
#!!!! The source type '%s' can write to a '%s' of the following types:
# %s
)!r   ZAVRuler   Z	DONTAUDIT	rule_typecommentr   strCommentexplain_accesstype	audit2whyZALLOWr   ZBOOLEANlendatajoinZ
CONSTRAINTZTERULEperms	obj_classr   ZseinfoZ	ATTRIBUTEZsesearchZSCONTEXTsrc_typeZCLASSZPERMSappendr   r#   )r   avZrulereasonr2   ir   r   r   Z__add_av_rule   sN    
&.$&zPolicyGenerator.__add_av_rulec             C   s@   x:|j j D ],}tj||}| jr*|j|_| jjj	| qW dS )z5Add extended permission access vector rules.
        N)
r   keysr   Z	AVExtRuler   ZDONTAUDITXPERMr3   r   r#   r@   )r   rA   opZextruler   r   r   Z__add_ext_av_rules   s
    z"PolicyGenerator.__add_ext_av_rulesc             C   s`   | j r*| j j|| j\}}| jjj| n|}x,|D ]$}| j| | jr4|jr4| j| q4W dS )zJAdd the access from the access vector set to this
        module.
        N)	r   genr   r   r#   extend_PolicyGenerator__add_av_ruler   "_PolicyGenerator__add_ext_av_rules)r   Zav_setZ	raw_allowifcallsrA   r   r   r   
add_access   s    	

zPolicyGenerator.add_accessc             C   s    x|D ]}| j jj| qW d S )N)r   r#   r@   )r   Zrole_type_set	role_typer   r   r   add_role_types   s    
zPolicyGenerator.add_role_types)N)NN)T)r"   )__name__
__module____qualname____doc__r   r   r   SHORT_EXPLANATIONr   r   r   r   r(   r)   rH   rI   rK   rM   r   r   r   r   r   -   s   




5r   c                s  g  fdd}|t krx| jD ]}jd|j  jdt|jt|jf  jd|jtj	|j
f  jd|j|j|jf  jtjd|j d d	d
dd q"W |  nb|rjd| j| j| j| jj f  t| jdkr| jd }jd|j|j|jf  |  S )a  Explain why a policy statement was generated.

    Return a string containing a text explanation of
    why a policy statement was generated. The string is
    commented and wrapped and can be directly inserted
    into a policy.

    Params:
      av - access vector representing the access. Should
       have .audit_msgs set appropriately.
      verbosity - the amount of explanation provided. Should
       be set to NO_EXPLANATION, SHORT_EXPLANATION, or
       LONG_EXPLANATION.
    Returns:
      list of strings - strings explaining the access or an empty
       string if verbosity=NO_EXPLANATION or there is not sufficient
       information to provide an explanation.
    c                 sN    sd S j d x6 j D ]*} t| j j}j d|j | jf  qW d S )Nz Interface options:z   %s # [%d])r@   allcall_interface	interfacerA   Z	to_stringZdist)matchifcall)mlsr   r   explain_interfaces  s    
z*explain_access.<locals>.explain_interfacesz %sz  scontext="%s" tcontext="%s"z  class="%s" perms="%s"z  comm="%s" exe="%s" path="%s"z	message=""P   z  z   )Zinitial_indentZsubsequent_indentz) src="%s" tgt="%s" class="%s", perms="%s"r   z comm="%s" exe="%s" path="%s")LONG_EXPLANATIONZ
audit_msgsr@   headerr5   ZscontextZtcontextZtclassr   Zlist_to_space_strZaccessesZcommZexepathrG   textwrapZwrapmessager?   tgt_typer>   r=   Zto_space_strr:   )rA   rX   r+   rZ   msgr   )rX   rY   r   r7      s*    
r7   c             C   s   g }g }|j | jj  |jdd dd tj }| j|_xtt	|D ]z}|| j
tjkrl|jj|j qH|| j
tjkr|jj|j qH|| j
tjkr|jj|j qHt|| j
 dsHtqHW t	|jdkst|S )Nc             S   s   | j S )N)num)paramr   r   r   <lambda>9  s    z call_interface.<locals>.<lambda>T)keyreverser   )rG   paramsvaluessortr   ZInterfaceCallr%   Zifnameranger:   r8   SRC_TYPEargsr@   r?   TGT_TYPErb   	OBJ_CLASSr>   printAssertionError)rU   rA   ri   rn   rW   rC   r   r   r   rT   4  s"    rT   c               @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r   Nc             C   s&   || _ | j| tj|| _g | _d S )N)ifshack_check_ifsr   ZAccessMatchermatchercalls)r   rs   r   r   r   r   r   N  s    
zInterfaceGenerator.__init__c             C   s   x|j j D ]|}g }|j|jj  |jdd dd xPtt|D ]@}|d || jkrbd|_P || j	t
jt
jt
jgkrDd|_P qDW qW d S )Nc             S   s   | j S )N)rd   )re   r   r   r   rf   \  s    z3InterfaceGenerator.hack_check_ifs.<locals>.<lambda>T)rg   rh   r   F)r   rj   rG   ri   rk   rl   r:   rd   Zenabledr8   r   rm   ro   rp   )r   rs   r-   ri   rC   r   r   r   rt   T  s    
z!InterfaceGenerator.hack_check_ifsc             C   s   | j |}g }xH| jD ]>}t|j j|j}|rFtjt|j|||_	|j
||f qW g }xX|D ]P\}}d}	x4|D ],}
|
j|rt|
j	r|j	r|
j	j|j	 d}	qtW |	sb|j
| qbW ||fS )NFT)rV   rv   rT   ZbestrU   rA   r   r6   r7   r4   r@   Zmatchesmerge)r   avsr+   raw_avrJ   rX   rW   drs   foundZo_ifcallr   r   r   rF   k  s$    


zInterfaceGenerator.genc             C   sP   g }xF|D ]>}t j }| jj| j|| t|r>| jj| q
|j| q
W |S )N)r   Z	MatchListru   Z
search_ifsrs   r:   rv   r@   )r   rx   ry   rA   Zansr   r   r   rV     s    
zInterfaceGenerator.match)N)rN   rO   rP   r   rt   rF   rV   r   r   r   r   r   M  s   
r   c             C   s&   dd }x| j  D ]}|| qW dS )z*Add require statements to the module.
    c             S   s   t j }xJ| j D ]>}|jj|j |jj|j x|jD ]}|j||j	 q:W qW x,| j
 D ] }x|jD ]}|jj| qjW q^W x,| j D ] }|jj|j |jj|j qW |jjd | jjd| d S )Nr   r   )r   ZRequireZavrulesr2   updateZ	src_typesZ	tgt_typesZobj_classesZadd_obj_classr=   Zinterface_callsrn   addZ
role_typesZrolesZrolediscardr#   r$   )noderZavruleobjrW   argrL   r   r   r   collect_requires  s    z&gen_requires.<locals>.collect_requiresN)Znodes)r   r   r   r   r   r   r     s    r   )rQ   	itertoolsr`   Zselinux.audit2whyr9   Zsetoolsr*   r   r   r   r   r   r	   r   rR   r]   r   r7   rT   r   r   r   r   r   r   <module>   s,   
 Q7B