+
    	:iU  c                   s0   ^ RI t ^ RIt^RIHt ^RIHt ^RIHt R tR tR t ! R R	4      t	 ! R
 R]	4      t
 ! R R]	4      t^ RIHt / t ! R R]	4      t ! R R]	4      t ! R R]	4      t ! R R]	4      t ! R R4      t ! R R4      t ! R R4      tR# )i    N)	refpolicy)access)utilc                 s   ^ RI p ^ RIp\        RR4      p\        VP	                  4       P                  4       ^ ,          4      pVP                   VP                  VP                  4       V,
          4      pVP                  RV4      pVP                  RV4      pV P                  RRRR	WV.V P                  R
7      P                  4       ^ ,          p\        P                  '       d   \        P                  ! V4      pV# )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.
Nz/proc/uptimeZrz%xz%X/sbin/ausearch-m5AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERRz-tsZstdout)
subprocesstimeZopenZfloatZreadsplitZcloseZ	localtimeZstrftimePopenPIPEcommunicater   PY3decode_input)r   r	   ZfdZoffZsZbootdateZboottimeoutputs           3/usr/lib/python3.14/site-packages/sepolgen/audit.pyget_audit_boot_msgsr      s     NC Bbggioo"#CHHtyy{S()A}}T1%H}}T1%H/7npuw  K%/__  66AkmAGFxxx""6*M    c                 s    ^ RI p V P                  . ROV P                  R7      P                  4       ^ ,          p\        P
                  '       d   \        P                  ! V4      pV# )r   Nr   )r   r   r   r   r   r   r   r   r   r   r   r        r   get_audit_msgsr   2   sS     o%/__  66AkmAGFxxx""6*Mr   c                 s    ^ RI p V P                  R.V P                  R7      P                  4       ^ ,          p\        P
                  '       d   \        P                  ! V4      pV# )zObtain all of the avc and policy load messages from /bin/dmesg.

Returns:
   string contain all of the audit messages returned by dmesg.
Nz
/bin/dmesgr   r   r   r   r   get_dmesg_msgsr   A   sT     |n%/__  66AkmAGFxxx""6*Mr   c                   s0   a  ] tR t^Pt o RtR tR tRtV tR# )AuditMessagezBase 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.
c                s     Wn         R V n        R#  N)messageheaderselfr      &&r   __init__ZAuditMessage.__init__W   s    r   c                s    V F\  pVP                  R4      p\        V4      ^8w  d   VR,          R8X  d
   W n         R# K=  V^ ,          R8X  g   KM  V^,          V n         R# 	  R# )zParse 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.
=:Ni   Nzaudit(Nmsg)r
   lenr   r   recsr#   fields   &&  r   from_split_stringZAuditMessage.from_split_string[   sX     CYYs^F6{ar7h&"%KayE!$Qi r   )r   r   N	__name__
__module____qualname____firstlineno____doc__r!   r)   __static_attributes____classdictcell__Z__classdict__   @r   r   r   P   s      r   r   c                   s*   a  ] tR t^pt o RtR tRtV tR# )InvalidMessagezClass 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.
c                0    \         P                  W4       R # Nr   r!   r   r    r   r!   ZInvalidMessage.__init__v       d,r    Nr+   r,   r-   r.   r/   r!   r0   r1   r2   r3   r   r4   r4   p   s     
- -r   r4   c                   s0   a  ] tR t^yt o RtR tR tRtV tR# )PathMessagez!Class representing a path messagec                >    \         P                  W4       R V n        R# r   )r   r!   pathr   r    r   r!   ZPathMessage.__init__{   s    d,	r   c                s    \         P                  W4       V FH  pVP                  R 4      p\        V4      ^8w  d   K&  V^ ,          R8X  g   K6  V^,          ^R V n         R# 	  R# )r"   r=   N)r   r)   r
   r$   r=   r%   r(   r   r)   ZPathMessage.from_split_string   sV    &&t2CYYs^F6{aayF""1IaO	 r   )r=   Nr*   r2   r3   r   r;   r;   y   s     +	 	r   r;   c                   s<   a  ] tR t^t o RtR tR tR tR tRt	V t
R# )
AVCMessagea3  AVC 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.
   ioctlcmd - ioctl 'request' parameter

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
c                s2   \         P                  W4       \        P                  ! 4       V n        \        P                  ! 4       V n        R V n        R V n        R V n        R V n	        R V n
        . V n        RV n        RV n        \        P                  V n        R# )r   TN)r   r!   r    SecurityContextscontexttcontexttclasscommexer=   nameaccessesdenialioctlcmd	audit2whyTERULEtyper   r    r   r!   ZAVCMessage.__init__   su    d,!113!113			$$	r   c                sb   R pTpV\        V4      ^,
          8X  d   \        RV P                  ,          4      hV\        V4      8  d=   W,          R8X  d   RpM,V P                  P	                  W,          4       V^,           pKL  V'       g   \        RV P                  ,          4      hV^,           # )F#AVC message in invalid format [%s]
Z}T)r$   
ValueErrorr   rG   append)r   r&   ZstartZfound_closeis   &&&  r   Z__parse_accessZAVCMessage.__parse_access   s     TQCdllRSS#d)mw#~"MM  )AACdllRSS1ur   c                s4   \         P                  W4       R pR pR pR p\        \        V4      4       EF  pW,          R8X  d   V P	                  W^,           4      pRpK-  W,          R8X  d   R V n        W,          P                  R4      p\        V4      ^8w  d   Kj  V^ ,          R8X  d'   \        P                  ! V^,          4      V n	        RpK  V^ ,          R8X  d'   \        P                  ! V^,          4      V n
        RpK  V^ ,          R8X  d   V^,          V n        RpK  V^ ,          R8X  d   V^,          ^R V n        EK  V^ ,          R	8X  d   V^,          ^R V n        EK6  V^ ,          R
8X  d   V^,          ^R V n        EKX  V^ ,          R8X  g   EKi   \        V^,          ^4      V n        EK  	  V'       d   V'       d   V'       d	   V'       g   \#        RV P$                  ,          4      hV P'                  4        R#   \"         d     EK  i ; i)FZ{TZgrantedr"   rA   rB   rC   rD   rE   rF   rI   rM   Nr>   )r   r)   Zranger$   _AVCMessage__parse_accessrH   r
   r    r@   rA   rB   rC   rD   rE   rF   ZintrI   rN   r   analyze)r   r&   Z	found_srcZ	found_tgtZfound_classZfound_accessrP   r'   s   &&      r   r)   ZAVCMessage.from_split_string   s   &&t2 		s4y!Aw#~''!e4#I%#W]]3'F6{aayJ& ) 9 9&) D 	j( ) 9 9&) D 	h&$Qi"f$"1IaO	e#!!9Qr?f$"1IaO	j($'q	2$6DM9 "@ 	LCdllRSS " s   HHHc                s>   V P                   P                  4       pV P                  P                  4       p\        V P                  4      p. V n        W!V P                  V3\        P                  4       9   d*   \        W!V P                  V3,          w  V n	        V n        R
# \        P                  ! W!V P                  V P                  4      w  V n	        V n        V P                  \        P                  8X  d   \        P                  V n	        V P                  \        P                  8X  d   \        R V,          4      hV P                  \        P                   8X  d   \        RV,          4      hV P                  \        P                   8X  d   \        RV P                  ,          4      hV P                  \        P"                  8X  d,   \        RRP%                  V P                  4      ,          4      hV P                  \        P&                  8X  d   \        R4      hV P                  \        P(                  8X  Ed   V P
                  .V n        V P                  P*                  V P                   P*                  8w  dT   V P
                  P-                  RV P                  P*                  ,          RV P                   P*                  ,          34       V P                  P.                  V P                   P.                  8w  do   V P                   P.                  R8w  dT   V P
                  P-                  RV P                  P.                  ,          RV P                   P.                  ,          34       V P                  P0                  V P                   P0                  8w  dT   V P
                  P-                  R	V P                  P0                  ,          R	V P                   P0                  ,          34       V P                  V P
                  3\        W!V P                  V3&   R
# )zInvalid Target Context %s
zInvalid Source Context %s
zInvalid Type Class %s
zInvalid permission %s
Z z&Error during access vector computationz	user (%s)Zobject_rz	role (%s)z
level (%s)N)rB   Z	to_stringrA   ZtuplerG   datarC   avcdictZkeysrL   rJ   rR   ZNOPOLICYrK   ZBADTCONrN   ZBADSCONZBADPERMZjoinZ
BADCOMPUTEZ
CONSTRAINTZuserrO   roleZlevel)r   rB   rA   Zaccess_tuples   &   r   rR   ZAVCMessage.analyze   s   ==**,==**,dmm,	\:gllnL#*H\+Z#[ DIty#,#4#4XVZVcVc#d DItyyyI...%,,	yyI--- !>!IJJyyI--- !>!IJJyyI--- !:T[[!HIIyyI--- !:SXXdmm=T!TUUyyI000 !IJJyyI000"iiM	==%%););;II$$kDMM4F4F&FVZVcVcVhVhHh%ij==%%););;@R@RV`@`II$$kDMM4F4F&FVZVcVcVhVhHh%ij==&&$--*=*==II$$lT]]5H5H&H,Y]YfYfYlYlJl%mnHL		SWS\S\G]GXlCDr   )rG   rD   rS   rH   rE   rI   rF   r=   rA   rC   rB   rL   N)r+   r,   r-   r.   r/   r!   rQ   r)   rR   r0   r1   r2   r3   r   r?   r?      s&     :%,+Z ^  ^r   r?   c                   s*   a  ] tR tRt o RtR tRtV tR# )PolicyLoadMessagei  z6Audit message indicating that the policy was reloaded.c                r5   r6   r7   r   r    r   r!   ZPolicyLoadMessage.__init__   r8   r   r9   Nr:   r2   r3   r   rV   rV     s     @- -r   rV   c                   s0   a  ] tR tRt o RtR tR tRtV tR# )DaemonStartMessagei#  z3Audit message indicating that a daemon was started.c                r<   FN)r   r!   auditdr   r    r   r!   ZDaemonStartMessage.__init__%  s    d,r   c                sP    \         P                  W4       R V9   d
   RV n        R# R# )rY   TN)r   r)   rY   r   r&   r    r   r)   Z$DaemonStartMessage.from_split_string)  s$    &&t2tDK r   )rY   Nr*   r2   r3   r   rW   rW   #  s     = r   rW   c                   s6   a  ] tR tRt o RtR tR tR tRtV t	R# )ComputeSidMessagei/  an  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.
c                s    \         P                  W4       \        P                  ! 4       V n        \        P                  ! 4       V n        \        P                  ! 4       V n        R V n        R# r   )r   r!   r    r@   invalid_contextrA   rB   rC   r   r    r   r!   ZComputeSidMessage.__init__9  sH    d,(88:!113!113r   c                s   \         P                  W4       \        V4      ^
8  d   \        R4      h \        P
                  ! V^,          4      V n        \        P
                  ! V^,          P                  R4      ^,          4      V n        \        P
                  ! V^,          P                  R4      ^,          4      V n	        V^	,          P                  R4      ^,          V n
        R#    \        R4      h; i)i
   z;Split string does not represent a valid compute sid messager"   N)r   r)   r$   rN   r    r@   r\   r
   rA   rB   rC   rZ   r    r   r)   Z#ComputeSidMessage.from_split_string@  s    &&t2t9r>Z[[	\#,#<#<T!W#ED %55d1gmmC6H6KLDM%55d1gmmC6H6KLDMq'--,Q/DK	\Z[[s   B6C* *C7c                s@    R V P                   : RV P                  : R2# )zrole z types z;
)rU   rL   r      &r   r   ZComputeSidMessage.outputL  s    (,		499==r   )r\   rA   rC   rB   N)
r+   r,   r-   r.   r/   r!   r)   r   r0   r1   r2   r3   r   r[   r[   /  s     \> >r   r[   c                   sf   a  ] tR tRt o RtRR ltR tR tR tR t	R t
R	 tRR ltRR ltRtV tR
# )AuditParseriQ  aw  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.
c                s2    V P                  4        Wn        R # r6   )_AuditParser__initializelast_load_only)r   ra   r    r   r!   ZAuditParser.__init__g  s    ,r   c                sh    . V n         . V n        . V n        . V n        . V n        / V n        R V n        R# rX   )avc_msgscompute_sid_msgsinvalid_msgspolicy_load_msgs	path_msgs	by_headercheck_input_filer]   r^   r   Z__initializeZAuditParser.__initializek  s7     " " %r   c                s
   VP                  4        Uu. uF  q"P                  R 4      NK  	  ppV F  pRpVR8X  g   VR8X  g   VR8X  d   \        V4      pRpM^VR8X  d   \        V4      pRpMIVR8X  g   VR8X  d   \	        V4      pRpM-VR	8X  d   \        V4      pRpMVR
8X  d   \        \        4      pRpV'       g   K  RV n         XP                  V4       Vu # 	  R# u upi   \         d    \        T4      p Tu # i ; i)u   Fzavc:zmessage=avc:z	msg='avc:Tzsecurity_compute_sid:ztype=MAC_POLICY_LOADz	type=1403ztype=AVC_PATHztype=DAEMON_STARTN)r
   Zstripr?   r[   rV   r;   rW   Zlistrh   r)   rN   r4   )r   lineZxZrecrP   Zfoundr#   s   &&     r   Z__parse_lineZAuditParser.__parse_line  s    59JJLALqww)*LAAEF{a>1Q+5E &--'-,,[0@'-o%!$'))(.u(,%/))#. 
1 2 5 B. " /(.C
/s   C!C&&DDc                s   V P                  V4      pVf   R # \        V\        4      '       d%   V P                  '       d   V P	                  4        EM-\        V\
        4      '       dQ   VP                  '       d#   V P                  '       d   V P	                  4        V P                  P                  V4       M\        V\        4      '       d   V P                  P                  V4       M\        V\        4      '       d   V P                  P                  V4       Mc\        V\        4      '       d   V P                  P                  V4       M1\        V\        4      '       d   V P                   P                  V4       VP"                  R8w  df   VP"                  V P$                  9   d/   V P$                  VP"                  ,          P                  V4       R # V.V P$                  VP"                  &   R # R # )Nr   )_AuditParser__parse_line
isinstancerV   ra   r`   rW   rY   re   rO   r?   rb   r[   rc   r4   rd   r;   rf   r   rg   )r   ri   r#      && r   Z__parseZAuditParser.__parse  sZ   %; c,--"""!!#/00
 zzzd111!!#!!((-Z((MM  %.//!!((-^,,$$S)[))NN!!#& ::zzT^^+szz*11#6.1Uszz*	 r   c                sT   V P                   P                  4        F  p. pR pV FF  p\        V\        4      '       d   TpK  \        V\        4      '       g   K5  VP                  V4       KH  	  \        V4      ^ 8  g   Ke  V'       g   Ko  V F  pVP                  Vn        K  	  K  	  R # r6   )rg   Zvaluesrk   r;   r?   rO   r$   r=   )r   Zvalueavcr=   r#   Zas   &     r   Z__post_processZAuditParser.__post_process  s}    ^^**,ECDc;//DZ00JJsO	 
 3x!|A!YYAF  -r   c                s*   VP                  4       pV'       d$   V P                  V4       VP                  4       pK+  V P                  '       g6   \        P                  P                  R4       \        P                  ! ^ 4       V P                  4        R# )zhParse the contents of a file object. This method can be called
multiple times (along with parse_string).zNothing to do
N)Zreadline_AuditParser__parserh   sysZstderrZwriteZexit_AuditParser__post_process)r   inputri   rl   r   
parse_fileZAuditParser.parse_file  sa     ~~LL>>#D$$$JJ./HHQKr   c                s|    VP                  R4      pV F  pV P                  V4       K  	  V P                  4        R# )zParse a string containing audit messages - messages should
be separated by new lines. This method can be called multiple
times (along with parse_file).Z
N)r
   rn   rp   )r   rq   ZlinesZlr(   r   parse_stringZAuditParser.parse_string  s3     D!ALLO r   Nc                s   \         P                  ! 4       pV P                   F^  pV'       d   VP                  V4      '       g   K$  VP	                  VP
                  P                  VP
                  P                  4       K`  	  V# )a/  Return 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.
)r   ZRoleTypeSetrc   filteraddr\   rU   rL   )r   Zrole_filterZ
role_typesZcsr(   r   to_roleZAuditParser.to_role  sc     '')
''B+"4"4R"8"8r11668J8J8O8OP ( r   c                s   \         P                  ! 4       pV P                   EF   pVP                  R8w  d   V'       d   K  V'       d   VP	                  V4      '       g   K@  \         P
                  ! VP                  P                  VP                  P                  VP                  .VP                  ,           4      pVP                  Vn        VP                  Vn        VP                  '       d?   \        P                  ! 4       pVP                  VP                  4       WeP                   R&   VP#                  WTR7       EK#  	  V# )a"  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.
TZioctl)Z	audit_msg)r   ZAccessVectorSetrb   rH   rt   ZAccessVectorrA   rL   rB   rC   rG   rS   rI   r    ZXpermSetru   ZxpermsZadd_av)r   Z
avc_filterZonly_denialsZav_setrm   ZavZ	xperm_sets   &&&    r   	to_accessZAuditParser.to_access  s    " '')==CzzT!l!2!23!7!7((#,,*;*;S\\=N=N),*57:||*D E((((<<< ) 2 2 4IMM#,,/)2IIg&b0 !" r   )rb   rg   rh   rc   rd   ra   rf   re   )Fr6   )NT)r+   r,   r-   r.   r/   r!   r`   rj   rn   rp   rr   rs   rv   rw   r0   r1   r2   r3   r   r_   r_   Q  s>     *-&,H3J'
&# #r   r_   c                   ,   a  ] tR tRt o R tR tRtV tR# )AVCTypeFilteri$  c                <    \         P                  ! V4      V n        R # r6   reZcompileregexr   r}   r    r   r!   ZAVCTypeFilter.__init__%      ZZ&
r   c                s    V P                   P                  VP                  P                  4      '       d   R # V P                   P                  VP                  P                  4      '       d   R # R# TF)r}   matchrA   rL   rB   r   rm   r    r   rt   ZAVCTypeFilter.filter(  sI    ::CLL--..::CLL--..r   r}   Nr+   r,   r-   r.   r!   rt   r0   r1   r2   r3   r   ry   ry   $  s     ' r   ry   c                   rx   )ComputeSidTypeFilteri/  c                rz   r6   r{   r~   r    r   r!   ZComputeSidTypeFilter.__init__0  r   r   c                sP   V P                   P                  VP                  P                  4      '       d   R # V P                   P                  VP                  P                  4      '       d   R # V P                   P                  VP
                  P                  4      '       d   R # R# r   )r}   r   r\   rL   rA   rB   r   r    r   rt   ZComputeSidTypeFilter.filter3  sm    ::C//4455::CLL--..::CLL--..r   r   Nr   r2   r3   r   r   r   /  s     ' r   r   )r|   ro   r   r    r   r   r   r   r   r   r4   r;   Zselinux.audit2whyrJ   rT   r?   rV   rW   r[   r_   ry   r   r9   r   r   <module>r      s   ( 
 
   , @-\ -,   &
O^ O^b- -
	 	> >DQ Qf	 	 r   