
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
KlfQ@                 @   s   d 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mZ ddlm	Z	 G d	d
 d
Z
dd Zdd Zdd Zdd Zdd ZG dd dZG dd dZG dd dZG dd dZdS )z7
Classes for representing and manipulating interfaces.
    N   )access)	refpolicy)objectmodel)matching)_c               @   sH   e Zd ZdZdd Zdd Zdd ZeeeZedd	 d
Z	dd Z
dS )Paramz;
    Object representing a paramater for an interface.
    c             C   s"   d| _ tj| _tj | _d| _d S )N T)_Param__namer   SRC_TYPEtypeIdSetobj_classesZrequired)self r    /usr/lib/python3.6/interfaces.py__init__&   s    
zParam.__init__c             C   s    t j|std| || _d S )NzName [%s] is not a param)r   
is_idparam
ValueErrorr
   )r   namer   r   r   set_name,   s    
zParam.set_namec             C   s   | j S )N)r
   )r   r   r   r   get_name1   s    zParam.get_namec             C   s   t | jdd  S )Nr   )intr   )r   r   r   r   <lambda>6   s    zParam.<lambda>)fgetc             C   s    d| j tj| j dj| jf S )Nz0<sepolgen.policygen.Param instance [%s, %s, %s]> )r   r   field_to_strr   joinr   )r   r   r   r   __repr__8   s    zParam.__repr__N)__name__
__module____qualname____doc__r   r   r   propertyr   numr   r   r   r   r   r   "   s   
r   c             C   s   d}| |kr||  }||j kr"d S |tjks6|tjkr|j tjksN|j tjkrd}|r`|jg}ng }x&tj|j|D ]}|tj	krtd}P qtW tj|_ qd}nt
 }| |_||_ |||j< |r|jj|j |S )Nr   r   )r   r   r   TGT_TYPE	obj_class	itertoolschainr   r   Zimplicitly_typed_objectsr   r   add)r   r   avparamsretpZavobjsobjr   r   r   __param_insert>   s0    




r/   c             C   s~   d}d}t j| jr.t| jtj| |dkr.d}t j| jrTt| jtj| |dkrTd}t j| jrzt| jtj	| |dkrzd}|S )aj  Extract the paramaters from an access vector.

    Extract the paramaters (in the form $N) from an access
    vector, storing them as Param objects in a dictionary.
    Some attempt is made at resolving conflicts with other
    entries in the dict, but if an unresolvable conflict is
    found it is reported to the caller.

    The goal here is to figure out how interface parameters are
    actually used in the interface - e.g., that $1 is a domain used as
    a SRC_TYPE. In general an interface will look like this:

    interface(`foo', `
       allow $1 foo : file read;
    ')

    This is simple to figure out - $1 is a SRC_TYPE. A few interfaces
    are more complex, for example:

    interface(`foo_trans',`
       domain_auto_trans($1,fingerd_exec_t,fingerd_t)

       allow $1 fingerd_t:fd use;
       allow fingerd_t $1:fd use;
       allow fingerd_t $1:fifo_file rw_file_perms;
       allow fingerd_t $1:process sigchld;
    ')

    Here the usage seems ambigious, but it is not. $1 is still domain
    and therefore should be returned as a SRC_TYPE.

    Returns:
      0  - success
      1  - conflict found
    r   Fr   )
r   r   src_typer/   r   r   tgt_typer%   r&   	OBJ_CLASS)r*   r+   r,   Z	found_srcr   r   r   av_extract_paramsj   s    $r3   c             C   s"   t j| jrt| jtjd |S d S )N)r   r   roler/   r   ZROLE)r4   r+   r   r   r   role_extract_params   s    r5   c                sl    fdd}d}|| j tjr"d}|| jtjr4d}|| jtjrFd}tj| j	rht
| j	tjd  rhd}|S )Nc                s2   d}x(| D ] }t j|r
t||d  r
d}q
W |S )Nr   r   )r   r   r/   )setr   r,   x)r+   r   r   extract_from_set   s    

z2type_rule_extract_params.<locals>.extract_from_setr   r   )	src_typesr   r   	tgt_typesr%   r   r2   r   r   Z	dest_typer/   Z	DEST_TYPE)ruler+   r8   r,   r   )r+   r   type_rule_extract_params   s    r<   c             C   s6   d}x,| j D ]"}tj|rt|tjd |rd}qW |S )Nr   r   )argsr   r   r/   r   r   )ifcallr+   r,   argr   r   r   ifcall_extract_params   s    
r@   c               @   s   e Zd Zdd Zdd ZdS )AttributeVectorc             C   s   d| _ tj | _d S )Nr	   )r   r   AccessVectorSet)r   r   r   r   r      s    zAttributeVector.__init__c             C   s   | j j| d S )N)r   add_av)r   r*   r   r   r   rC      s    zAttributeVector.add_avN)r   r    r!   r   rC   r   r   r   r   rA      s   rA   c               @   s$   e Zd Zdd Zdd Zdd ZdS )AttributeSetc             C   s
   i | _ d S )N)
attributes)r   r   r   r   r      s    zAttributeSet.__init__c             C   s   || j |j< d S )N)rE   r   )r   attrr   r   r   add_attr   s    zAttributeSet.add_attrc             C   s~   dd }d }x^|D ]V}|d d }|d dkrF|r<| j | ||}q|r|jd}tj|}|j| qW |rz| j | d S )Nc             S   sH   | dd j  }t|dks(|d dkr4td|  t }|d |_|S )Nr      r   Z	Attributez#Syntax error Attribute statement %s)splitlenSyntaxErrorrA   r   )linefieldsar   r   r   
parse_attr   s    
z*AttributeSet.from_file.<locals>.parse_attrr   r   [,rI   )rG   rJ   r   AccessVectorrC   )r   fdrP   rO   rM   lr*   r   r   r   	from_file   s    	




zAttributeSet.from_fileN)r   r    r!   r   rG   rV   r   r   r   r   rD      s   rD   c               @   sF   e Zd Zdi fddZi fddZdd Zdd	 Zd
d Zdd ZdS )InterfaceVectorNc             C   s6   d| _ d| _tj | _i | _|r,| j|| d| _d S )NTr	   F)enabledr   r   rB   r+   from_interfaceexpanded)r   	interfacerE   r   r   r   r      s    
zInterfaceVector.__init__c             C   sF  |j | _ xN|j D ]B}|jtjjkr&qd|j kr2qtj|}x|D ]}| j| qBW qW |rx|j	 D ]v}xp|j
D ]f}||j
krqr|j
| }xJ|jD ]@}	tj|	}|j|j kr|j|_|j|j kr|j|_| j| qW qrW qfW x|j D ]}
t|
| jrqW x |j D ]}t|| jrqW x |j D ]}t|| jr*q*W d S )NZ	dontaudit)r   ZavrulesZ	rule_typer   ZAVRuleZALLOWr   Zavrule_to_access_vectorsrC   ZtypeattributesrE   copyr0   r   r1   Zrolesr5   r+   Z	typerulesr<   interface_callsr@   )r   r[   rE   ZavruleZavsr*   ZtypeattributerF   Zattr_vecrO   r4   r;   r>   r   r   r   rY      s>    





zInterfaceVector.from_interfacec             C   s    t || jdkr| jj| d S )Nr   )r3   r+   r   rC   )r   r*   r   r   r   rC   3  s    zInterfaceVector.add_avc             C   s<   g }|j d| j  x| jD ]}|j t| qW dj|S )Nz[InterfaceVector %s]
)appendr   r   strr   )r   sr*   r   r   r   	to_string9  s
    zInterfaceVector.to_stringc             C   s   | j  S )N)r   )r   r   r   r   __str__@  s    zInterfaceVector.__str__c             C   s   d| j | jf S )Nz<InterfaceVector %s:%s>)r   rX   )r   r   r   r   r   C  s    zInterfaceVector.__repr__)	r   r    r!   r   rY   rC   rb   rc   r   r   r   r   r   rW      s   4rW   c               @   sx   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zi fddZ	di fddZ
dd Zdd Zdd Zdd ZdS )InterfaceSetNc             C   s   i | _ i | _g | _|| _d S )N)
interfacestgt_type_maptgt_type_alloutput)r   rh   r   r   r   r   H  s    zInterfaceSet.__init__c             C   s   | j r| j j|d  d S )Nr^   )rh   write)r   r`   r   r   r   oN  s    zInterfaceSet.oc             C   s   xt | jj dd dD ]}|jd|j  x:t |jj dd dD ] }|jd|jtj|j f  qDW |jd t |j	j
 }x&|D ]}|jdj| |jd	 qW qW d S )
Nc             S   s   | j S )N)r   )r7   r   r   r   r   S  s    z&InterfaceSet.to_file.<locals>.<lambda>)keyz[InterfaceVector %s c             S   s   | j S )N)r   )r7   r   r   r   r   U  s    z%s:%s z]
rR   r^   )sortedre   valuesri   r   r+   r   r   r   r   Zto_listr   )r   rT   ZivparamZavlr*   r   r   r   to_fileR  s     

zInterfaceSet.to_filec             C   s   dd }d }x^|D ]V}|d d }|d dkrF|r<| j | ||}q|r|jd}tj|}|j| qW |rz| j | | j  d S )Nc             S   s   | dd j  }t|dk s(|d dkr4td|  t }|d |_t|dkrTd S xb|dd  D ]R}|j d}t|dkrtd|  t }|d |_tj|d  |_||j	|j< qbW |S )	Nr   rH   r   rW   z)Syntax error InterfaceVector statement %s:z-Invalid param in InterfaceVector statement %srI   )
rJ   rK   rL   rW   r   r   r   Zstr_to_fieldr   r+   )rM   rN   ifvZfieldr-   rn   r   r   r   	parse_ifv^  s     


z)InterfaceSet.from_file.<locals>.parse_ifvr   r   rQ   rR   rI   )add_ifvrJ   r   rS   rC   index)r   rT   rr   rq   rM   rU   r*   r   r   r   rV   ]  s    





zInterfaceSet.from_filec             C   s   || j |j< d S )N)re   r   )r   rq   r   r   r   rs     s    zInterfaceSet.add_ifvc             C   s   xz| j j D ]l}t }x:|jD ]0}tj|jrB| jj| t }P |j|j qW x$|D ]}| j	j
|g }|j| qXW qW d S )N)re   rm   r6   r   r   r1   rg   r_   r)   rf   
setdefault)r   rq   r:   r*   r   rU   r   r   r   rt     s    
zInterfaceSet.indexc             C   s   t ||}| j| d S )N)rW   rs   )r   r[   rE   rq   r   r   r   r)     s    
zInterfaceSet.addc             C   s@   x(t j|j |j D ]}| j|| qW | j| | j  d S )N)r'   r(   re   	templatesr)   expand_ifcallsrt   )r   headersrh   rE   ir   r   r   add_headers  s    
zInterfaceSet.add_headersc             C   sZ   t j|rPt|dd  }|t|jkr,d S |j|d  }t|trH|S |gS n|gS d S )Nr   )r   r   r   rK   r=   
isinstancelist)r   idr>   r$   r?   r   r   r   	map_param  s    

zInterfaceSet.map_paramc             C   s   | j |j|}|d krd S | j |j|}|d kr4d S | j |j|}|d krNd S tj }x0|jD ]&}| j ||}	|	d krzq^q^|j|	 q^W t|dkrd S x:|D ]2}
x,|D ]$}x|D ]}|j	j
|
||| qW qW qW d S )Nr   )r~   r0   r1   r&   r   r   ZpermsupdaterK   r   r)   )r   rq   r*   r>   r9   r:   r   Z	new_permsZpermr-   r0   r1   r&   r   r   r   
map_add_av  s*    


zInterfaceSet.map_add_avc             C   s   |d fg}| j |j }d|_xt|dkr|jd\}}| j |j }||krrx|jD ]}| j||| qTW |jrrqxv|j D ]j}	|	j|jkr| j	t
d d S y||	j }
W n* tk
r   | j	t
d|	j  w|Y nX |j|
|	f q|W qW d S )NTr   r   zFound circular interface classz#Missing interface definition for %srI   )re   r   rZ   rK   popr   r   r]   Zifnamerj   r   KeyErrorr_   )r   r[   
if_by_namestackrq   ZcurZ
cur_ifcallZcur_ifvr*   r>   Znewifr   r   r   do_expand_ifcalls  s*    
zInterfaceSet.do_expand_ifcallsc             C   sZ   i }x&t j|j |j D ]}|||j< qW x(t j|j |j D ]}| j|| qBW d S )N)r'   r(   re   rv   r   r   )r   rx   r   ry   r[   r   r   r   rw     s
    zInterfaceSet.expand_ifcalls)N)r   r    r!   r   rj   ro   rV   rs   rt   r)   rz   r~   r   r   rw   r   r   r   r   rd   G  s   
#$rd   )r"   r\   r'   r	   r   r   r   r   Zsepolgeni18nr   r   r/   r3   r5   r<   r@   rA   rD   rW   rd   r   r   r   r   <module>   s"   ,4Z