Your IP : 13.59.214.148


Current Path : /lib64/python2.7/site-packages/sepolgen/
Upload File :
Current File : //lib64/python2.7/site-packages/sepolgen/audit.pyo

�
��^c@sLddlZddlZddlmZddlmZddlmZd�Zd�Zd�Zd	fd
��YZ	de	fd��YZ
d
e	fd��YZddlj
Z
iZde	fd��YZde	fd��YZde	fd��YZde	fd��YZdfd��YZdfd��YZdfd��YZdS(i����Ni(t	refpolicy(taccess(tutilcCs�ddl}ddl}tdd�}t|j�j�d�}|j|j|j�|�}|jd|�}|jd|�}|j	dd	d
d||gd|j
�j�d}tj
r�tj|�}n|S(
s
Obtain all of the avc and policy load messages from the audit
    log. This function uses ausearch and requires that the current
    process have sufficient rights to run ausearch.

    Returns:
       string contain all of the audit messages returned by ausearch.
    i����Ns/proc/uptimetris%xs%Xs/sbin/ausearchs-ms5AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERRs-tststdout(t
subprocessttimetopentfloattreadtsplittcloset	localtimetstrftimetPopentPIPEtcommunicateRtPY3tdecode_input(RRtfdtofftstbootdatetboottimetoutput((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytget_audit_boot_msgss	cCsVddl}|jdddgd|j�j�d}tjrRtj|�}n|S(s
Obtain all of the avc and policy load messages from the audit
    log. This function uses ausearch and requires that the current
    process have sufficient rights to run ausearch.

    Returns:
       string contain all of the audit messages returned by ausearch.
    i����Ns/sbin/ausearchs-ms5AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERRRi(RRRRRRR(RR((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytget_audit_msgs2s	cCsPddl}|jdgd|j�j�d}tjrLtj|�}n|S(s�Obtain all of the avc and policy load messages from /bin/dmesg.

    Returns:
       string contain all of the audit messages returned by dmesg.
    i����Ns
/bin/dmesgRi(RRRRRRR(RR((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytget_dmesg_msgsAs	tAuditMessagecBs eZdZd�Zd�ZRS(s�Base class for all objects representing audit messages.

    AuditMessage is a base class for all audit messages and only
    provides storage for the raw message (as a string) and a
    parsing function that does nothing.
    cCs||_d|_dS(Nt(tmessagetheader(tselfR((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__init__Ws	cCszxs|D]k}|jd�}t|�dkrQ|d dkr||_dSqn|ddkr|d|_dSqWdS(	s�Parse a string that has been split into records by space into
        an audit message.

        This method should be overridden by subclasses. Error reporting
        should be done by raise ValueError exceptions.
        t=iisaudit(Nitmsgi(R
tlenR(R trecsR#tfields((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytfrom_split_string[s
	
(t__name__t
__module__t__doc__R!R'(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyRPs	tInvalidMessagecBseZdZd�ZRS(s�Class representing invalid audit messages. This is used to differentiate
    between audit messages that aren't recognized (that should return None from
    the audit message parser) and a message that is recognized but is malformed
    in some way.
    cCstj||�dS(N(RR!(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!vs(R(R)R*R!(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR+pstPathMessagecBs eZdZd�Zd�ZRS(s!Class representing a path messagecCstj||�d|_dS(NR(RR!tpath(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!{scCsttj||�x]|D]U}|jd�}t|�dkrDqn|ddkr|ddd!|_dSqWdS(NR"iiR-ii����(RR'R
R$R-(R R%R#R&((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR's
(R(R)R*R!R'(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR,ys	t
AVCMessagecBs2eZdZd�Zd�Zd�Zd�ZRS(skAVC message representing an access denial or granted message.

    This is a very basic class and does not represent all possible fields
    in an avc message. Currently the fields are:
       scontext - context for the source (process) that generated the message
       tcontext - context for the target
       tclass - object class for the target (only one)
       comm - the process name
       exe - the on-disc binary
       path - the path of the target
       access - list of accesses that were allowed or denied
       denial - boolean indicating whether this was a denial (True) or granted
          (False) message.

    An example audit message generated from the audit daemon looks like (line breaks
    added):
       'type=AVC msg=audit(1155568085.407:10877): avc:  denied  { search } for
       pid=677 comm="python" name="modules" dev=dm-0 ino=13716388
       scontext=user_u:system_r:setroubleshootd_t:s0
       tcontext=system_u:object_r:modules_object_t:s0 tclass=dir'

    An example audit message stored in syslog (not processed by the audit daemon - line
    breaks added):
       'Sep 12 08:26:43 dhcp83-5 kernel: audit(1158064002.046:4): avc:  denied  { read }
       for  pid=2 496 comm="bluez-pin" name=".gdm1K3IFT" dev=dm-0 ino=3601333
       scontext=user_u:system_r:bluetooth_helper_t:s0-s0:c0
       tcontext=system_u:object_r:xdm_tmp_t:s0 tclass=file
    cCs�tj||�tj�|_tj�|_d|_d|_d|_d|_	d|_
d|_g|_t
|_tj|_dS(NR(RR!RtSecurityContexttscontextttcontextttclasstcommtexeR-tnametinotaccessestTruetdenialt	audit2whytTERULEttype(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!�s								cCs�t}|}|t|�dkr8td|j��nxN|t|�kr�||dkrgt}Pn|jj||�|d}q;W|s�td|j��n|dS(Nis#AVC message in invalid format [%s]
t}(tFalseR$t
ValueErrorRR8R7tappend(R R%tstarttfound_closeti((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__parse_access�scCs>tj||�t}t}t}t}x�tt|��D]�}||dkrs|j||d�}t}q;n||dkr�t|_n||jd�}t|�dkr�q;n|ddkr�t	j
|d�|_t}q;|ddkrt	j
|d�|_t}q;|dd	kr>|d|_
t}q;|dd
kre|ddd!|_q;|ddkr�|ddd!|_q;|dd
kr�|ddd!|_q;|ddkr�|ddd!|_q;|ddkr;|d|_q;q;W|s|s|s|r0td|j��n|j�dS(Nt{itgrantedR"iiR0R1R2R3i����R4R5R-R6s#AVC message in invalid format [%s]
(RR'R>trangeR$t_AVCMessage__parse_accessR8R9R
RR/R0R1R2R3R4R5R-R6R?Rtanalyze(R R%t	found_srct	found_tgttfound_classtfound_accessRCR&((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR'�sJ		
	cCs�|jj�}|jj�}t|j�}g|_|||j|ftj�krt|||j|f\|_	|_n9t
j|||j|j�\|_	|_|j	t
jkr�t
j
|_	n|j	t
jkr�td|��n|j	t
jkrtd|��n|j	t
jkr<td|j��n|j	t
jkrmtddj|j���n|j	t
jkr�td��n|j	t
jkr�|jg|_|jj|jjkr�|jjd|jjd|jjf�n|jj|jjkrK|jjdkrK|jjd	|jjd	|jjf�n|jj|jjkr�|jjd
|jjd
|jjf�q�n|j	|jft|||j|f<dS(NsInvalid Target Context %s
sInvalid Source Context %s
sInvalid Type Class %s
sInvalid permission %s
t s&Error during access vector computations	user (%s)tobject_rs	role (%s)s
level (%s)(R1t	to_stringR0ttupleR7tdataR2tavcdicttkeysR<R:RItNOPOLICYR;tBADTCONR?tBADSCONtBADPERMtjoint
BADCOMPUTEt
CONSTRAINTtuserR@troletlevel(R R1R0taccess_tuple((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyRI�s8	!(*-*-0(R(R)R*R!RHR'RI(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR.�s
			,tPolicyLoadMessagecBseZdZd�ZRS(s6Audit message indicating that the policy was reloaded.cCstj||�dS(N(RR!(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!s(R(R)R*R!(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR`stDaemonStartMessagecBs eZdZd�Zd�ZRS(s3Audit message indicating that a daemon was started.cCstj||�t|_dS(N(RR!R>tauditd(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!#scCs,tj||�d|kr(t|_ndS(NRb(RR'R8Rb(R R%((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR''s(R(R)R*R!R'(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyRa!s	tComputeSidMessagecBs)eZdZd�Zd�Zd�ZRS(s�Audit message indicating that a sid was not valid.

    Compute sid messages are generated on attempting to create a security
    context that is not valid. Security contexts are invalid if the role is
    not authorized for the user or the type is not authorized for the role.

    This class does not store all of the fields from the compute sid message -
    just the type and role.
    cCsJtj||�tj�|_tj�|_tj�|_d|_dS(NR(RR!RR/tinvalid_contextR0R1R2(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!7s
cCs�tj||�t|�dkr1td��nyztj|d�|_tj|djd�d�|_tj|djd�d�|_	|djd�d|_
Wntd��nXdS(	Ni
s;Split string does not represent a valid compute sid messageiiR"iii	(RR'R$R?RR/RdR
R0R1R2(R R%((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR'>s##cCsd|j|jfS(Nsrole %s types %s;
(R]R<(R ((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyRJs(R(R)R*R!R'R(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyRc-s			tAuditParsercBs�eZdZed�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d
d�Zd	�Z
d
�Zd�Zd
ed�ZRS(s�Parser for audit messages.

    This class parses audit messages and stores them according to their message
    type. This is not a general purpose audit message parser - it only extracts
    selinux related messages.

    Each audit messages are stored in one of four lists:
       avc_msgs - avc denial or granted messages. Messages are stored in
          AVCMessage objects.
       comput_sid_messages - invalid sid messages. Messages are stored in
          ComputSidMessage objects.
       invalid_msgs - selinux related messages that are not valid. Messages
          are stored in InvalidMessageObjects.
       policy_load_messages - policy load messages. Messages are stored in
          PolicyLoadMessage objects.

    These lists will be reset when a policy load message is seen if
    AuditParser.last_load_only is set to true. It is assumed that messages
    are fed to the parser in chronological order - time stamps are not
    parsed.
    cCs|j�||_dS(N(t_AuditParser__initializetlast_load_only(R Rg((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!es
cCsVg|_g|_g|_g|_g|_i|_t|_i|_|j	�dS(N(
tavc_msgstcompute_sid_msgstinvalid_msgstpolicy_load_msgst	path_msgst	by_headerR>tcheck_input_filet
inode_dictt_AuditParser__store_base_types(R ((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__initializeis								c	CsPg|j�D]}|jd�^q
}x!|D]}t}|dks_|dks_|dkrtt|�}t}n�|dkr�t|�}t}no|dks�|dkr�t|�}t}nB|dkr�t|�}t}n!|d	krtt	�}t}n|r/t|_
y|j|�Wntk
rCt
|�}nX|Sq/WdS(
Ns�savc:smessage=avc:s	msg='avc:ssecurity_compute_sid:stype=MAC_POLICY_LOADs	type=1403s
type=AVC_PATHstype=DAEMON_START(R
tstripR>R.R8RcR`R,RatlistRnR'R?R+tNone(R tlinetxtrecRCtfoundR#((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__parse_line�s4(
$						
cCse|j|�}|dkrdSt|t�rG|jr|j�qn�t|t�r�|jru|jru|j�n|jj	|�n�t|t
�r�|jj	|�nft|t�r�|j
j	|�nDt|t�r�|jj	|�n"t|t�r|jj	|�n|jdkra|j|jkrK|j|jj	|�qa|g|j|j<ndS(NR(t_AuditParser__parse_lineRtt
isinstanceR`RgRfRaRbRkR@R.RhRcRiR+RjR,RlRRm(R RuR#((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__parse�s,	
cCs�x�|jj�D]�}g}d}xE|D]=}t|t�rG|}q)t|t�r)|j|�q)q)Wt|�dkr|rx|D]}|j|_q�WqqWdS(Ni(	RmtvaluesRtR{R,R.R@R$R-(R tvaluetavcR-R#ta((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__post_process�s
	
cCsi|j�}x#|r1|j|�|j�}qW|js[tjjd�tjd�n|j�dS(spParse the contents of a file object. This method can be called
        multiple times (along with parse_string).sNothing to do
iN(treadlinet_AuditParser__parseRntsyststderrtwritetexitt_AuditParser__post_process(R tinputRu((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt
parse_file�s	
	cCs;|jd�}x|D]}|j|�qW|j�dS(s�Parse a string containing audit messages - messages should
        be separated by new lines. This method can be called multiple
        times (along with parse_file).s
N(R
R�R�(R R�tlinestl((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytparse_string�s
cCsYtj�}xF|jD];}|s2|j|�r|j|jj|jj�qqW|S(soReturn RoleAllowSet statements matching the specified filter

        Filter out types that match the filer, or all roles

        Params:
           role_filter - [optional] Filter object used to filter the
              output.
        Returns:
           Access vector set representing the denied access in the
           audit logs parsed by this object.
        (RtRoleTypeSetRitfiltertaddRdR]R<(R trole_filtert
role_typestcs((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pytto_role�s
#c	Cs�ddl}ddl}d}|dks6|dkr:|Sx^|jD]S}||krm|j||krm|S||krD|j||krD|j|SqDW||jj�kr�||j|<nd|}y�|j|d|jdtdt�}yt|�}	Wntk
rnXxX|j	d�D]G}
y7t|j
|
�j�|	kra|
|j|<}|SWq%q%Xq%WWn|jk
r�}nX|S(Ni����Rslocate -b '\%s'R�tshelltuniversal_newliness
(
RtosRoRTtcheck_outputtSTDOUTR8tintR?R
tlstattst_inotCalledProcessError(R R5tinodeRR�R-tdtcommandRR6tfilete((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__restore_path�s@
		
cCs"ddl}|jd�|_dS(Ni����tbase_file_type(tsepolicytget_types_from_attributet
base_types(R R�((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__store_base_types#scCs�ddl}|j|kr.|j|kr.dS||_||_xf|jD][}||krJxB|j|�D]1}|j|�rl|j|jd��rl|SqlWdSqJWdS(Ni����t_ti(R�told_scontexttold_tcontextR�tget_writable_filestendswitht
startswithtrstrip(R R1R0R�tbtypetwritable((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt__get_base_type's		'cCsCtj�}d|_d|_x|jD]}|jtkrI|rIq(n|j|jj	|j
j	�}|jdkr�|j|j
|j�|_n|r�|j|�r;|j|j
j	|jj	|j|j|j||d|j	d|j�q;q(|j|j
j	|jj	|j|j|j||d|j	d|j�q(W|S(s�Convert the audit logs access into a an access vector set.

        Convert the audit logs into an access vector set, optionally
        filtering the restults with the passed in filter object.

        Filter objects are object instances with a .filter method
        that takes and access vector and returns True if the message
        should be included in the final output and False otherwise.

        Params:
           avc_filter - [optional] Filter object used to filter the
              output.
        Returns:
           Access vector set representing the denied access in the
           audit logs parsed by this object.
        Rtavc_typeRR(RtAccessVectorSetR�R�RhR9R8t_AuditParser__get_base_typeR1R<R0R-t_AuditParser__restore_pathR5R6R�R�R2R7RR(R t
avc_filtertonly_denialstav_setRt	base_type((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt	to_access6s$		N(R(R)R*R>R!RfRzR�R�R�R�RtR�R�RpR�R8R�(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyReOs		$	%	
				$		t
AVCTypeFiltercBseZd�Zd�ZRS(cCstj|�|_dS(N(tretcompiletregex(R R�((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!\scCs<|jj|jj�rtS|jj|jj�r8tStS(N(R�tmatchR0R<R8R1R>(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR�_s
(R(R)R!R�(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR�[s	tComputeSidTypeFiltercBseZd�Zd�ZRS(cCstj|�|_dS(N(R�R�R�(R R�((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR!gscCsX|jj|jj�rtS|jj|jj�r8tS|jj|jj�rTtStS(N(R�R�RdR<R8R0R1R>(R R((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR�js(R(R)R!R�(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyR�fs	(R�R�RRRRRRRRR+R,tselinux.audit2whyR:RSR.R`RaRcReR�R�(((s4/usr/lib64/python2.7/site-packages/sepolgen/audit.pyt<module>s(			 	�"�