관리-도구
편집 파일: audit_data.cpython-39.opt-1.pyc
a ����U+e�����������������������@���sl��d�dl�mZ�d�dlZd�dlmZ�d�dlZg�d�Zd�dlZd�dlZd�dlZd�dl Z d�dl Z d�dlZd�dlZd�dl Z d�dlT�d�dlmZ�d�dlT�d�dlT�d�dlT�d�dlT�dZdd��Zd d ��Zdd��Ze�d �Zdd��Ze�d�Zdd��Zd�dlZdd��ZG�dd��de �Z!G�dd��de �Z"G�dd��de �Z#G�dd��d�Z$G�dd��de �Z%G�dd ��d e&�Z'G�d!d"��d"�Z(d#d$��Z)dS�)%�����)�absolute_importN)�range) �derive_record_format�parse_audit_record_text� AvcContext�AVC�AuditEventID� AuditEvent�AuditRecord�AuditRecordReader�compute_avcs)�*����c�����������������C���s���|�|k|�|k��S��N��)�x�yr���r����=/usr/lib/python3.9/site-packages/setroubleshoot/audit_data.py�<lambda>7��������r���c�����������������C���s ���t�|��\}}}}t|||�}|S�r���)r���r ���)�text�parse_succeeded�record_type�event_id� body_text�audit_recordr���r���r����audit_record_from_text=���s����r���c�����������������C���s*���t��d|��rtjS�t��d|��r$tjS�tjS�)Nz/audispd_events$z/audit_events$)�re�searchr����TEXT_FORMAT� BINARY_FORMAT)Zsocket_pathr���r���r���r���E���s ����r���zL(node=(\S+)\s+)?(type=(\S+)\s+)?(msg=)?audit\(((\d+)\.(\d+):(\d+))\):\s*(.*)c����������� ������C���s����d}d�}d�}d�}d�}t��|��}|d�ur�d}|�d�r>|�d�}|�d�rR|�d�}|�d�r�t|�d��}t|�d��}t|�d��} t||| |�}|�d �}||||fS�) NFT��������������������� ���� ���)�audit_input_rer����group�intr���) �inputr����hostr���r���r����match�seconds�milli�serialr���r���r���r���^���s&���� r���z%audit\(((\d+)\.(\d+):(\d+))\):\s*(.*)c�����������������C���sv���d}d�}d�}t��|��}|d�urld}|�d�rbt|�d��}t|�d��}t|�d��}t|||�}|�d�}|||fS�)NFT����r!���r���r"�������)�audit_binary_input_rer���r)���r*���r���)r+���r���r���r���r-���r.���r/���r0���r���r���r����parse_audit_binary_text|���s���� r4���c�����������������C���s"���|�rdd��|�D��}||�krdS�dS�)Nc�����������������S���s���g�|�]}|t�jv�r|�qS�r���)�string� printable��.0r���r���r���r���� <listcomp>����r���zprintable.<locals>.<listcomp>TFr���)�sZ filtered_pathr���r���r���r6�������s ����r6���c�����������������������sZ���e�Zd�Zddiddiddiddid�Z��fdd�Zdd��Zdd ��Zd d��Zdd ��Z���Z S�)r����XMLForm� attribute��user�role�type�mlsc��������������������sz���t�t|������t|tj�rv|�d�}t|�dkrv|d�|�_|d�|�_ |d�|�_ t|�dkrpd�|dd����|�_nd|�_d�S�)N�:r���r���r1���r!���Zs0) �superr����__init__� isinstance�sixZstring_types�split�lenr>���r?���r@����joinrA���)�self�data�fields�� __class__r���r���rD�������s���� zAvcContext.__init__c�����������������C���s���d|�j�|�j|�j|�jf�S�)Nz%s:%s:%s:%sr=����rJ���r���r���r����__str__����s����zAvcContext.__str__c�����������������C���s���t��t|���\}}|S�r���)�selinuxZselinux_raw_to_trans_context�str)rJ����rcZtransr���r���r����format����s����zAvcContext.formatc�����������������C���s���|���|��S�r���)�__eq__�rJ����otherr���r���r����__ne__����s����zAvcContext.__ne__c�����������������C���s2���t�|�j����D�]}t|�|�t||�kr�dS�qdS��NFT)�list� _xml_info�keys�getattr)rJ���rW����namer���r���r���rU�������s����zAvcContext.__eq__) �__name__� __module__�__qualname__r[���rD���rP���rT���rX���rU���� __classcell__r���r���rM���r���r�������s���� r���c�����������������������sv���e�Zd�Zded�ded�ded�ddid�Zd��fdd� Zdd ��Zd d��Zdd ��Ze dd���Z dd��Zdd��Z���Z S�)r���r<����r;����import_typecastr;���)r.���r/���r0���r,���Nc��������������������s2���t�t|������||�_||�_||�_|d�ur.||�_d�S�r���)rC���r���rD���r.���r/���r0���r,���)rJ���r.���r/���r0���r,���rM���r���r���rD�������s����zAuditEventID.__init__c�����������������C���sD���|�j�|j�krdS�|�j|jkr dS�|�j|jkr0dS�|�j|jkr@dS�dS�rY���)r,���r.���r/���r0���rV���r���r���r���rU�������s����zAuditEventID.__eq__c�����������������C���sb���|�j�|j�kr&td|�jj|�j�|j�f���|�j|jkr>|�j|jk�S�|�j|jkrV|�j|jk�S�|�j|jk�S�)Nz?cannot compare two %s objects whose host values differ (%s!=%s))r,���� ValueErrorrN���r_���r.���r/���r0���rV���r���r���r����__lt__����s�����zAuditEventID.__lt__c�����������������C���s���dd�l�}|��|��S��Nr���)�copy)rJ���rh���r���r���r���rh�������s����zAuditEventID.copyc�����������������C���s���t�|�j�|�jd��S�)N������@�@)�floatZsecr/���rO���r���r���r���r�������r���zAuditEventID.<lambda>c�����������������C���s���d|�j�|�j|�jf�S�)Nzaudit(%d.%d:%d)�r.���r/���r0���rO���r���r���r���rP�������s����zAuditEventID.__str__c�����������������C���s.���|�j�d�u�rdS�|�jd�u�rdS�|�jd�u�r*dS�dS�rY���rk���rO���r���r���r����is_valid����s���� zAuditEventID.is_valid)N)r_���r`���ra���r*���r[���rD���rU���rf���rh����property�timerP���rl���rb���r���r���rM���r���r�������s����r���c�����������������������s����e�Zd�Zddided�ddided�d�ZdZdZe� e�Z e�d�Z e�d �Ze�d �Zd*��fdd � Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zd d!��Zd"d#��Zd$d%��Zd&d'��Zd(d)��Z���ZS�)+r ���r;���r<����elementrc���)r���r���r����line_numberr���Ziiiiz([^ \t]+)\s*=\s*([^ \t]+)z$avc:\s+([^\s]+)\s+{([^}]+)}\s+for\s+z^a\d+$Nc��������������������s8���t�t|������||�_||�_||�_||�_||�_|�����d�S�r���) rC���r ���rD���r���r���r���rL���rp����_init_postprocess)rJ���r���r���r���rL���rp���rM���r���r���rD�����s����zAuditRecord.__init__c�����������������C���sr���t�|�dd��d�u�r|��|�j��|�jdv�rnd|�jvrntj�|�j�}|rn|�d�}||�jd<�|�d�}|� ��|�jd<�d�S�)NrL����r���ZUSER_AVCZ1400Z1107�seresultr1���r!����seperms) r]����set_fields_from_textr���r���rL���r ����avc_rer���r)���rG���)rJ���r-���rs���rt���r���r���r���rq�����s���� zAuditRecord._init_postprocessc�����������������C���s���|�����S�r���)�to_host_textrO���r���r���r���rP���(��s����zAuditRecord.__str__c�����������������C���s ���d�|�_�|�jjd�u�rt��|�j_d�S�r���)rp���r���r,���Zget_hostnamerO���r���r���r����audispd_rectify+��s����zAuditRecord.audispd_rectifyc�����������������C���s.���|�j����sdS�|�jd�u�rdS�|�jd�u�r*dS�dS�rY���)r���rl���r����messagerO���r���r���r���rl���0��s���� zAuditRecord.is_validc�����������������C���s4���|�j�d�}|dkrdS�|dkr"dS�tj�d|��dS�)Nrs���ZdeniedFZgrantedTz!unknown value for seresult ('%s'))rL����logZavc�warn)rJ���rs���r���r���r���� is_granted9��s���� zAuditRecord.is_grantedc�����������������C���s����g�d�}|D�]>}||�j�v�r|�jdkr.|dkr.q|�j�|�}t|�}||�j�|<�q|�jdkr�t|�j�����D�]0\}}|�j�|�rd|�j�|�}t|�}||�j�|<�qdd�S�)N)Zacct�cmd�comm�cwdrK����dir�exe�filer,����key�msgr^����newZocommold�pathZwatchr���ZsaddrZEXECVE)rL���r���Zaudit_msg_decoderZ����items�exec_arg_rer���)rJ���Zencoded_fieldsZfield�valueZ decoded_valuer���r���r���� decode_fieldsB��s���� zAuditRecord.decode_fieldsc�����������������C���sF���z0t�jd�dk�r|�d�W�S�t�|��d�W�S�W�n���|�Y�S�0�d�S�)Nr���r����hexzutf-8)�sys�version_info�decode� bytearray�fromhex�rJ���r����r���r���r���� translate_hexV��s����zAuditRecord.translate_hexc�������������� ���C���s��g�|�_�i�|�_tj�|�D�]�}|�d�}|�d�}|�d�}z�|dkr^t�t |d��}t� |�}|dv�r�|�d��d�s�|��|�}|dkr�zt jtt |���}W�n���Y�n0�|dkr�t�t |�t����}|r�|}W�n�ty����Y�n0�||�j|<�|�j��|��qd�S�) Nr1���r!����"�arch����)r^���r����r~���r}���r����r����exit�syscall)� fields_ordrL���r ����key_value_pair_re�finditerr)����strip�auditZaudit_elf_to_machiner*���Zaudit_machine_to_name� startswithr�����errno� errorcode�absZaudit_syscall_to_nameZaudit_detect_machinere����append)rJ���r���r-���r����r�����iZsyscall_namer���r���r���ru���e��s4���� z AuditRecord.set_fields_from_textc�����������������C���s���|�j��|�S�r���)rL����get)rJ���r^���r���r���r���� get_field���s����zAuditRecord.get_fieldc�����������������C���s"���t�|�}t�tjtjtj|�j|�S�r���)rH����struct�packr ����binary_header_format�binary_version�binary_header_sizer���)rJ���r����� msg_lengthr���r���r����get_binary_header���s���� �zAuditRecord.get_binary_headerc��������������������sj�����j�d�u�rdS���jdkr4d��j��jd���j�f�}nd��j��jf�}|d���fdd���jD���d�7�}|S�) N��r���z#type=%s msg=%s: avc: denied { %s } � ztype=%s msg=%s: c��������������������s���g�|�]}d�|��j�|�f��qS�)z%s=%s)rL���)r8����krO���r���r���r9������r���z.AuditRecord.fields_to_text.<locals>.<listcomp>� )rL���r���r���rI����accessr����)rJ����bufr���rO���r����fields_to_text���s���� "zAuditRecord.fields_to_textc�����������������C���s���d|�j�|�j|�jf�S�)Nztype=%s msg=%s: %s )r���r���r���rO���r���r���r����to_text���s����zAuditRecord.to_textc�����������������C���s2���|�j�jd�ur&d|�j�j|�j|�j�|�jf�S�|����S�d�S�)Nznode=%s type=%s msg=%s: %s )r���r,���r���r���r����rO���r���r���r���rw������s �����zAuditRecord.to_host_textc�����������������C���s���d|�j�|�jf�}|��|�|�S�)Nz%s: %s)r���r���r�����rJ����recordr���r���r���� to_binary���s����zAuditRecord.to_binary)NN) r_���r`���ra���r���r*���r[���r����r����r�����calcsizer����r����compiler����rv���r����rD���rq���rP���rx���rl���r|���r����r����ru���r����r����r����r����rw���r����rb���r���r���rM���r���r �����s4���� " r ���c�������������������@���s,���e�Zd�ZdZdZdd��Zdd��Zdd��Zd S�) r���r1���r!���c�����������������C���sV���||�_�d|�_d|�_|�j�|�jkr(|�j|�_n*|�j�|�jkr>|�j|�_ntd||�j j f���d�S�)Nr����r���z unknown record format (%s) in %s)� record_format� _input_bufferrp���r���� feed_textZfeedr ����feed_binaryre���rN���r_���)rJ���r����r���r���r���rD������s���� zAuditRecordReader.__init__c�����������������c���s����t�|�dkrd�S�|��j|7��_t�|�j�tjk�r2d�S�t�tj|�jdtj���\}}}}tj|�}t�|�j�|k�rpd�S�|�jtj|��}t|�\}} } |�j|d���|�_|rt� |�| | d�dfV��qd�S�rg���) rH���r����r ���r����r�����unpackr����r4���r����Zaudit_msg_type_to_name)rJ����new_datar����r����r���r����Z total_lenr���r���r���r���r���r���r���r�������s$���� �� zAuditRecordReader.feed_binaryc����������� ������c���s����t�|�dkrd�S�|��j|7��_d}|�j�d|�}|dkr�|��jd7��_|d7�}|�j||��}t|�\}}}}|r�|||d�|�jfV��|}|�j�d|�}q0|�j|d���|�_d�S�)Nr���r����r1���)rH���r�����findrp���r���) rJ���r�����start�end�liner���r���r���r���r���r���r���r�������s ����zAuditRecordReader.feed_textN)r_���r`���ra���r ���r���rD���r����r����r���r���r���r���r������s ���!r���c�����������������������s����e�Zd�Zdded�ded�d�Z��fdd�Zdd ��Zd d��Zd$d d�Z dd��Z edd���Zdd��Z dd��Zd%dd�Zdd��Zdd��Zdd��Zd d!��Zd"d#��Z���ZS�)&r ���ro���r���)r;���rZ���rd���rc���)�recordsr���c��������������������s*���t�t|������d�|�_g�|�_i�|�_d�|�_d�S�r���)rC���r ���rD���r���r�����record_types� timestamprO���rM���r���r���rD������s ����zAuditEvent.__init__c�����������������C���s0���t�|�dd��d�u�ri�|�_|�jD�]}|��|��qd�S�)Nr����)r]���r����r�����process_recordr����r���r���r���rq�����s���� zAuditEvent._init_postprocessc�������������� ���C���sL���|�j�}|����d|�j|����|����d�dd��|D���d�dd��|�jD���f�S�)Nz2%s: is_avc=%s, is_granted=%s: line_numbers=[%s] %s�,c�����������������S���s���g�|�]}t�|��qS�r����rR���r7���r���r���r���r9�����r���z&AuditEvent.__str__.<locals>.<listcomp>r����c�����������������S���s���g�|�]}d�|��qS�)z %sr����r8���r����r���r���r���r9�����r���)�line_numbers�sortr����is_avcr|���rI���r����)rJ���r����r���r���r���rP�����s������zAuditEvent.__str__r����c�����������������C���s���|��dd��|�jD���S�)Nc�����������������S���s���g�|�]}t�|��qS�r���r����r����r���r���r���r9�����r���z%AuditEvent.format.<locals>.<listcomp>)rI���r����)rJ���� separatorr���r���r���rT�����s����zAuditEvent.formatc�����������������C���s ���t�|�j�S�r���)rH���r����rO���r���r���r����num_records��s����zAuditEvent.num_recordsc�����������������C���s���dd��|�j�D��S�)Nc�����������������S���s���g�|�]}|j�r|j��qS�r���)rp���r����r���r���r���r9�����r���z'AuditEvent.<lambda>.<locals>.<listcomp>)r����rO���r���r���r���r�����r���zAuditEvent.<lambda>c�����������������C���s���|�j��|��|��|��d�S�r���)r����r����r����r����r���r���r���� add_record��s����zAuditEvent.add_recordc�����������������C���sp���|�j�d�u�r2|j����|�_�t|�j�j�|�j�jd��|�_n |�j�|j�ksRtd|�j�|j�f���|�j�|j g��}|� |��d�S�)Nri���zBcannot add audit record to audit event, event_id mismatch %s != %s)r���rh���rj���r.���r/���r����re���r����� setdefaultr���r����)rJ���r����Zrecord_listr���r���r���r������s���� �zAuditEvent.process_recordNc�����������������C���sR���g�}|du�r|�j�}n |��|�}|D�]*}|j�|�}|du�r<q"|�||jf��q"|S�)aN��Return list of (value, record_type) tuples. In other words return the value matching name for every record_type. If record_type is not specified then all records are searched. Note: it is possible to have more than one record of a given type thus it is always possible to have multiple values returned.N)r�����get_records_of_typerL���r����r����r@���)rJ���r^���r���r����r����r����r����r���r���r���r����+��s���� zAuditEvent.get_fieldc�����������������C���s ���d�}|�j��|�}|r|d�}|S�rg����r����r����)rJ���r@���r����r����r���r���r����get_record_of_type?��s ����zAuditEvent.get_record_of_typec�����������������C���s���|�j��|g��S�r���r����)rJ���r@���r���r���r���r����F��s����zAuditEvent.get_records_of_typec�����������������C���s$���dD�]}|���|�}|r|��S�qd�S�)Nrr���)r����)rJ���r���r����r���r���r����get_avc_recordI��s���� zAuditEvent.get_avc_recordc�����������������C���s���|�����d�uS�r���)r����rO���r���r���r���r����O��s����zAuditEvent.is_avcc�����������������C���s:���d}dD�],}|���|�}|D�]}|���s.��dS�d}qq|S�)NFrr���T)r����r|���)rJ���Zall_grantedr���r����r����r���r���r���r|���R��s���� zAuditEvent.is_granted)r����)N)r_���r`���ra���r ���r���r[���rD���rq���rP���rT���r����rm���r����r����r����r����r����r����r����r����r|���rb���r���r���rM���r���r ������s"��� � r ���c�������������������@���s���e�Zd�ZdS�)�AVCErrorN)r_���r`���ra���r���r���r���r���r����a��s���r����c�������������������@���s���e�Zd�ZdgZddgZg�d�Zg�d�Zg�d�Zg�d�Zg�d�Z g�d�Z g�d �Zg�d �Zg�d�Z g�d�Zg�d �ZddgZdgZdgZg�d�Zg�d�Zg�d�Zg�d�ZdgZdgZg�d�Zg�d�Zg�d�Zg�d�ZddgZg�d�Ze� d�Z!e� d�Z"dFdd�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+d0d1��Z,d2d3��Z-d4d5��Z.d6d7��Z/d8d9��Z0d:d;��Z1d<d=��Z2d>d?��Z3d@dA��Z4dBdC��Z5dDdE��Z6dS�)Gr���r]����execute)�open�readr]����lock�ioctl)r����r����r]���r����r����r����)r����r����r����r]���r����r����)r]����link�unlink�rename)�creater����r]����setattrr����r����r����)r����r����r����r����r]���r�����writer����r����r����r����r����)r����r����r]���r����r���r����) r����r����r]���r����r���r�����add_name�remove_namer����)r����r����r]���r����r���r����r����r����)r����r����r����r]���r����r����r����r����r����r����r���r����r�����reparentr�����rmdir)ZmountZremountZunmountr]���r���r����)r����r]���r���r����r����r����)r����r]���r���r����r����r����r����)r����r]���r���r����r����r����r����)r����r����r]���r����r����r����r����r����r����r���r����r����r����r����r����r����)r����r]���r����r����r����)r����r]���r����r����r����)r����r]���r����r����r����r����)r����r]���r����r����r����r����r����r����)r����r����r]���r����r����r����r����r����r����r����r����r����z^(\w+):\[([^\]]*)\]z^(/proc/)(\d+)(.*)NTc�����������������C���s����||�_�||�_i�|�_d�|�_d�|�_d�|�_d�|�_d�|�_d�|�_d�|�_ d�|�_ d�|�_g�|�_g�|�_ d�|�_d�|�_d�|�_d�|�_d�|�_g�|�_|��|��d�S�r���)�audit_event�query_environment�template_substitutions�tpath�spath�source� source_pkgr�����scontext�tcontext�tclass�port�src_rpms�tgt_rpmsr,����pid�kmodr�����why�bools� derive_avc_info_from_audit_event)rJ���r����� avc_recordr����r���r���r���rD������s*����zAVC.__init__c�����������������C���s���|�����S�r���)� format_avcrO���r���r���r���rP������s����zAVC.__str__c�����������������C���sN���d}|d|�j��7�}|d|�j�7�}|d|�j�7�}|d|�j�7�}|d|�j�7�}|S�)Nr����zscontext=%s ztcontext=%s z access=%s z tclass=%s z tpath=%s )r����r����r����r����r����)rJ���r���r���r���r���r�������s����zAVC.format_avcc�����������������C���s,���|�j�du�rdS�|�j�D�]}||v�r�dS�qdS�)zMReturns true if the AVC contains _any_ of the permissions in the access list.NFT�r�����rJ���Zaccess_list�ar���r���r����has_any_access_in���s���� zAVC.has_any_access_inc�����������������C���s,���|�j�du�rdS�|�j�D�]}||vr�dS�qdS�)zmReturns true if _every_ access in the AVC matches at least one of the permissions in the access list.NFTr����r����r���r���r����all_accesses_are_in���s���� zAVC.all_accesses_are_inc�������������� ���C���s����t���t���}|����t��}|����g�}dd��dd��ttgt|�jjt |�j t|�ji�D��D��}|}|D�]$}||v�rb|� ttt|��d���qb|D�]}||v�r�||vr�|�|��q�|����|S�)Nc�����������������S���s���g�|�]}|t���qS�r���)ZTARGETr7���r���r���r���r9������r���z,AVC.allowed_target_types.<locals>.<listcomp>c�����������������S���s���g�|�]}|d��r|�qS�)Zenabledr���)r8���r���r���r���r���r9������r����types)Zget_all_file_typesZget_all_port_typesr����Zget_all_attributesr����ALLOW�SOURCEr����r@���ZCLASSr����ZPERMSr�����extend�next�infoZ ATTRIBUTEr����)rJ���Z all_typesZall_attributesZ allowed_typesZwtypesr�����tr���r���r����allowed_target_types���s ����4zAVC.allowed_target_typesc�����������������C���sB���|���dg�r>z$|�jr.t|�j�t@�tjkr.W�dS�W�n���Y�n0�dS�)Nr����TF)r�����a1r*���� O_ACCMODE�os�O_RDONLYrO���r���r���r����open_with_write���s���� zAVC.open_with_writec�����������������C���s"���|D�]}t��||j�r�dS�qdS�)NTF)r���r-���r@���)rJ����context� type_listr@���r���r���r���Z__typeMatch���s����zAVC.__typeMatchc�����������������C���s���|�j�du�rdS�|��|�j�|�S�)zReturns true if the type in the source context of the avc regular expression matches any of the types in the type list.NF)r�����_AVC__typeMatch�rJ���r��r���r���r����matches_source_types���s���� zAVC.matches_source_typesc�����������������C���s���|�j�du�rdS�|��|�j�|�S�)zReturns true if the type in the target context of the avc regular expression matches any of the types in the type list.NF)r����r ��r��r���r���r����matches_target_types���s���� zAVC.matches_target_typesc�����������������C���s���|�j�d�u�rdS�|�j�|v�S�)NF)r����)rJ���Ztclass_listr���r���r���� has_tclass_in���s���� zAVC.has_tclass_inc�����������������C���s���|������|�����d�S�r���)�derive_environmental_info�%update_derived_template_substitutionsrO���r���r���r����update��s����z AVC.updatec�����������������C���s���t�|�j��S�r���)�is_standard_directoryr����rO���r���r���r����path_is_not_standard_directory��s����z"AVC.path_is_not_standard_directoryc�������������� ���C���s���|�j��d�}|�j��d�}|�j��d�}|du�r�|�j�d�}|D�]D}|�d�}|rX|dkrXq<|�d�}|r<|r<|dkr<|�|�r<�q�q<|r�tj�|�s�|dur�tj�|�r�tj�tj� ||��}|du��r"|�j��d�}|du�r"|�j��d�}|d kr�d |�}n(|dk�r|dk�r|}nd |�}n|}|du�r�|dk�r|�rg�} �z�d } |�j��d�}tj� d|���rvt�d|��j} t |�}tdd�} | ����d�D�]�}|���}t|��r�|d�d �dk�r�zP| d k�s�t�|d ��j| k�rt t�|d��j�|k�r| �|dd����W�n�t�y(���Y��q�Y�n0��q�| ����t| �dk�rR| d �d�}n�t| �dk�r�| D�]�}|d �d|�k�s�|d�|k�r�|d�}��q�nJz4| d k�r�t�|d ��j| k�r�|d�}W���q�W�n�t�y����Y�n0��qdW�n.�t�y����d}Y�n�t�y���d}Y�n0�n�|�d�dk�r�|�r�d dl}ddd|�g}zl|j||jdd�}t |�}|�d�D�]B}z&t t�|�j�|k�r�|}W���q�W�n�t�y����Y�n0��qlW�n�t�y����Y�n0�|du�r$tj�|��r�|�j�d|�}n4|�j� |�}|�r|�j!}|d �dk�r$d |�"d��}||�_#|�j#du��r~|�j!d!k�rJd"|�_#n4|�j!d#k�sb|�j!d$k�rtt$d%�|�j%�|�_#n t$d&�|�_#dS�)'a���Derive the target path. If path information is available the avc record will have a path field and no name field because the path field is more specific and supersedes name. The name field is typically the directory entry. For some special files the kernel embeds instance information into the file name. For example 'pipe:[1234]' or 'socket:[1234]' where the number inside the brackets is the inode number. The proc pseudo file system has the process pid embedded in the name, for example '/proc/1234/mem'. These numbers are ephemeral and do not contribute meaningful information for our reports. Plus we may use the path information to decide if an alert is identical to a previous alert, we coalesce them if they are. The presence of an instance specific number in the path confuses this comparison. For these reasons we strip any instance information out of the path, Example input and output: pipe:[1234] --> pipe socket:[1234] --> socket /proc/1234/fd --> /proc/<pid>/fd ./foo --> ./foo /etc/sysconfig --> /etc/sysconfig r^���r�����inoN�PATHZnametypeZPARENTr����r�����%sr�����/r����devz/dev/z/proc/mounts�rr����r1���r���z/dev/%sr!���zunknown mountpointFZlocatez-bz\%sT)�stderrZuniversal_newlinesz \1<pid>\3���@Z filesystemr����Z udp_socketZ tcp_socketzport %sZUnknown)&r����r����r����r�����endswithr��r�����isabs�normpathrI����exists�lstat�st_rdevr*���r����r����rG���rH����stat�st_inor�����OSError�close� TypeErrorr����� subprocessZcheck_outputZSTDOUT� Exception�proc_pid_instance_re�sub�pipe_instance_path_rer���r����r����r�����_r����)rJ���r���r^���r����ZinodestrZavc_path_recordsZavc_path_recordr���r�����matchesZdev_rdevr��r���fdr����r���r+��Zcommand�outputr����r-���r���r���r���� _set_tpath��s����� $ : � zAVC._set_tpathc����������� ������C���s���d�|�_�d�|�_d�|�_d�|�_d|�_g�|�_d��}�}�}}|r@||�_n|�j���|�_|�j� d�}|�j� d�|�_t|�j t�s�t|�j� d��|�_ t|�jt�s�t|�j� d��|�_|�j� d�|�_|�j� d�d�u�r�|�j� d�|�_n|�j� d�|�_|�j� d �|�_|�j� d �|�_|�r<|� d�}|� d�}|� d �|�_|� d�dk|�_|� d�|�_|d�u��rR|�j� d�}|d�u��rh|�j� d�}||�_|�r|||�_n|�r�|�j|�_|�j�s�|�j|�_|�j�s�|�j j|�_|�j� d�}|�r�|� d�}nd�}|��|��|�j�d�} | D�]H} | � d�}tj�|��s |�s|�j�|��n|�j�tj�||����q�g�|�_g�|�_|�jjj |�_ t!�"t#|�j �t#|�j�t#|�j�|�j�\|�_$}|�j$t!j%k�r�t&t'd�|�j���|�j$t!j(k�r�t&t'd�|�j���|�j$t!j)k�r�t&t'd���|�j$t!j*k�r�t&t'd�|�j���|�j$t!j+k�rt&t'd�|�j���|�j$t!j,k�r.t&t'd�|�j���|�j$t!j-k�rNt&t'd�|�j���|�j$t!j.k�rht&t'd���|�j$t!j/k�r|||�_0d�S�)NFZSYSCALLrt���r����r����r�����dest�srcr����r����r����r~���r�����successZyesr��ZCWDr���r��r^���z8%s **** Recorded AVC is allowed in current policy **** zh%s **** Recorded AVC is dontaudited in current policy. 'semodule -B' will turn on dontaudit rules **** zMust call policy_init firstz.%s **** Invalid AVC: bad target context **** z.%s **** Invalid AVC: bad source context **** z*%s **** Invalid AVC: bad type class **** z*%s **** Invalid AVC: bad permission **** z&Error during access vector computation)1r����r����r����r��r7��Z syscall_pathsr����r����r����r����r����r����rE���r����r���r����r����r����r����r����r����r@���r4��r����r��r����r!��r����rI���r����r����r���r,���� audit2whyZanalyzerR���r����r����r����r0��Z DONTAUDITZNOPOLICYZBADTCONZBADSCONZ BADTCLASSZBADPERMZ BADCOMPUTEZBOOLEANr����) rJ���r����r����r~���r����r����Zsyscall_recordZ cwd_recordr���Zpath_recordsZpath_recordr����r����r���r���r���r�������s����� *z$AVC.derive_avc_info_from_audit_eventc�����������������C���sT���|�j�rP|�jr,t|�j�|�_|�jr,|�j�|�j��t|�j�sPt|�j�}|rP|�j�|��d�S�r���) r����r����Zget_package_nvr_by_file_pathr����r����r����r��r����r����)rJ���Zrpmr���r���r���r����s���� zAVC.derive_environmental_infoc�����������������C���s���|�j�d�u�r||�_�d�S�r���)r����r����r���r���r����set_alt_path��s���� zAVC.set_alt_pathc�����������������K���s(���t�|����D�]\}}|r||�j|<�qd�S�r���)rZ���r����r����)rJ����kwdsr����r����r���r���r����set_template_substitutions��s����zAVC.set_template_substitutionsc�����������������C���sV��t�|�jj�|�jd<�t�|�jj�|�jd<�t�|�j�|�jd<�t�|�j�|�jd<�|�jrdt�ddt�|�j��|�jd<�t�|�j �|�jd<�|�j r�t�ddt�|�j ��|�jd <�|�j d�u�r�d�|�jd <�nJ|�j dkr�t�|�j �|�jd <�n.|�j dkr�t�tj� |�j ��|�jd <�n d�|�jd <�t�|�j �|�jd <�|�jd�u��rd�|�jd<�nt�d�|�j��|�jd<�t�|�j�|�jd<�t�|�j�|�jd<�d�S�)NZSOURCE_TYPEZTARGET_TYPEr���ZSOURCE_PATHr�����.ZFIX_SOURCE_PATHZTARGET_PATHZFIX_TARGET_PATHZ TARGET_DIRr����r����ZTARGET_CLASSZACCESSZSOURCE_PACKAGEZPORT_NUMBER)�escape_htmlr����r@���r����r����r����r����r���r.��r����r����r��r�����dirnamer����rI���r����r����rO���r���r���r���r��$��s,���� z)AVC.update_derived_template_substitutionsc�����������������C���s6���t�|�j����D�]"\}}|d�u�rtt|��|�j|<�qd�S�r���)rZ���r����r����r=��Zdefault_text)rJ���r����r����r���r���r����validate_template_substitutionsC��s����z#AVC.validate_template_substitutions)NT)7r_���r`���ra���Zstat_file_permsZx_file_permsZr_file_permsZ rx_file_permsZ ra_file_permsZlink_file_permsZcreate_lnk_permsZcreate_file_permsZr_dir_permsZrw_dir_permsZra_dir_permsZcreate_dir_permsZmount_fs_permsZsearch_dir_permsZgetattr_dir_permsZsetattr_dir_permsZlist_dir_permsZadd_entry_dir_permsZdel_entry_dir_permsZmanage_dir_permsZgetattr_file_permsZsetattr_file_permsZread_file_permsZappend_file_permsZwrite_file_permsZ rw_file_permsZdelete_file_permsZmanage_file_permsr���r����r/��r-��rD���rP���r����r����r����r��r ��r ��r��r��r��r��r��r4��r����r��r9��r;��r��r?��r���r���r���r���r���e��sf��� �"fr���c�����������������C���s����g�}zndD�]d}|���|�}|D�]P}z|�t|�|���W�q�tyj�}�z|�|jd|���W�Y�d�}~qd�}~0�0�qq W�nR�ty��}�z:dd�l}dd�l}|�|jd|���t|� ����W�Y�d�}~n d�}~0�0�|S�)Nrr���r��r���z!Unable to process audit event: %s) r����r����r���r�����syslogZLOG_ERRr,��� tracebackZsyslog_trace� format_exc)r����Zavcsr���r����r�����er@��rA��r���r���r���r���J��s���� 0"r���)*Z __future__r���rF���Z six.movesr���r�����__all__r����r����r��r����r���rQ����base64r����Zselinux.audit2whyr8��Zsetroubleshoot.utilZsetroubleshoot.html_utilZsetroubleshoot.xml_serializeZsepolicyr���cmpr���r���r����r(���r���r3���r4���r5���r6���ZXmlSerializer���r���r ���r���r ���r,��r����r���r���r���r���r���r����<module>���sP��� )<�*Mj���h