3 n`ǝ@sdddlmZddlZddlmZddlZdddddd d d gZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z dd lTddljZdd lTdd lTdd lTdd lTd ZddZeZddZddZe jdZddZe jdZddZddl Z ddZ!Gddde"Z#Gddde"Z$Gdd d e"Z%Gdd d Z&Gdd d e"Z'GdddZ(dS) )absolute_importN)rangederive_record_formatparse_audit_record_text AvcContextAVC AuditEventID AuditEvent AuditRecordAuditRecordReader)*cCs||k||kS)N)xyrr /usr/lib/python3.6/audit_data.py6srcCs t|\}}}}t|||}|S)N)rr )textparse_succeeded record_typeevent_id body_text audit_recordrrraudit_record_from_text@s rcCs*tjd|rtjStjd|r$tjStjS)Nz/audispd_events$z/audit_events$)researchr TEXT_FORMAT BINARY_FORMAT)Z socket_pathrrrrHs   zL(node=(\S+)\s+)?(type=(\S+)\s+)?(msg=)?audit\(((\d+)\.(\d+):(\d+))\):\s*(.*)c Csd}d}d}d}d}tj|}|dk rd}|jdr>|jd}|jdrR|jd}|jdrt|jd}t|jd}t|jd} t||| |}|jd }||||fS) NFT )audit_input_rergroupintr) inputrhostrrrmatchsecondsmilliserialrrrras&       z%audit\(((\d+)\.(\d+):(\d+))\):\s*(.*)cCsvd}d}d}tj|}|dk rld}|jdrbt|jd}t|jd}t|jd}t|||}|jd}|||fS)NFTrr r)audit_binary_input_rerr&r'r)r(rrrr*r+r,r-rrrparse_audit_binary_texts    r1cCs"|rdd|D}||krdSdS)NcSsg|]}|tjkr|qSr)string printable).0rrrr szprintable..TFr)sZ filtered_pathrrrr3s r3csZeZdZddiddiddiddidZfddZddZdd Zd d Zd d ZZ S)rXMLForm attribute)userroletypemlscsztt|jt|tjrv|jd}t|dkrv|d|_|d|_ |d|_ t|dkrpdj |dd|_ nd|_ dS)N:r rr.rZs0) superr__init__ isinstancesixZ string_typessplitlenr9r:r;joinr<)selfdatafields) __class__rrr?s       zAvcContext.__init__cCsd|j|j|j|jfS)Nz %s:%s:%s:%s)r9r:r;r<)rErrr__str__szAvcContext.__str__cCstjt|\}}|S)N)selinuxZselinux_raw_to_trans_contextstr)rEZrcZtransrrrformatszAvcContext.formatcCs |j| S)N)__eq__)rEotherrrr__ne__szAvcContext.__ne__cCs4x.t|jjD]}t||t||krdSqWdS)NFT)list _xml_infokeysgetattr)rErNnamerrrrMszAvcContext.__eq__) __name__ __module__ __qualname__rQr?rIrLrOrM __classcell__rr)rHrrs  csveZdZdeddeddedddidZdfdd Zdd Zd d Zd d Ze ddZ ddZ ddZ Z S)rr8)r7import_typecastr7)r+r,r-r)Ncs2tt|j||_||_||_|dk r.||_dS)N)r>rr?r+r,r-r))rEr+r,r-r))rHrrr?s zAuditEventID.__init__cCsD|j|jkrdS|j|jkr dS|j|jkr0dS|j|jkr@dSdS)NFT)r)r+r,r-)rErNrrrrMs    zAuditEventID.__eq__cCsb|j|jkr&td|jj|j|jf|j|jkr>|j|jkS|j|jkrV|j|jkS|j|jkS)Nz?cannot compare two %s objects whose host values differ (%s!=%s))r) ValueErrorrHrUr+r,r-)rErNrrr__lt__s     zAuditEventID.__lt__cCsddl}|j|S)Nr)copy)rEr\rrrr\szAuditEventID.copycCst|j|jdS)Ng@@)floatZsecr,)rErrrrszAuditEventID.cCsd|j|j|jfS)Nzaudit(%d.%d:%d))r+r,r-)rErrrrIszAuditEventID.__str__cCs.|jdkrdS|jdkrdS|jdkr*dSdS)NFT)r+r,r-)rErrris_valids   zAuditEventID.is_valid)N)rUrVrWr'rQr?rMr[r\propertyZtimerIr^rXrr)rHrrs    cseZdZddidedddideddZdZdZej eZ e j dZ e j d Ze j d Zd(fd d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'ZZS))r r7r8element)r7rY)rrr line_numberrZiiiiz([^ \t]+)\s*=\s*([^ \t]+)z$avc:\s+([^\s]+)\s+{([^}]+)}\s+for\s+z^a\d+$Ncs8tt|j||_||_||_||_||_|jdS)N) r>r r?rrrrGra_init_postprocess)rErrrrGra)rHrrr?szAuditRecord.__init__cCsrt|dddkr|j|j|jd krnd|jkrntjj|j}|rn|jd}||jd<|jd}|j |jd <dS) NrGrUSER_AVC14001107seresultr.rseperms)rrcrdre) rSset_fields_from_textrrrGr avc_rerr&rB)rEr*rfrgrrrrbs      zAuditRecord._init_postprocesscCs|jS)N) to_host_text)rErrrrI+szAuditRecord.__str__cCs d|_|jjdkrt|j_dS)N)rarr)Z get_hostname)rErrraudispd_rectify.s zAuditRecord.audispd_rectifycCs.|jjsdS|jdkrdS|jdkr*dSdS)NFT)rr^rmessage)rErrrr^3s   zAuditRecord.is_validcCsddddddddd d d d d dddg}xF|D]>}||jkr*|jdkrL|dkrLq*|j|}t|}||j|<q*W|jdkrxBt|jjD]0\}}|jj|r|j|}t|}||j|<qWdS)NZacctcmdcommcwdrFdirexefiler)keymsgrTnewZocommoldpathZwatchrZsaddrZEXECVE)rGrZaudit_msg_decoderPitems exec_arg_rer)rEZencoded_fieldsZfieldvalueZ decoded_valuerrr decode_fields<s       zAuditRecord.decode_fieldsc Cs<y,tjddkr|jdStj|jdSWn |SdS)Nrr hexzutf-8)sys version_infodecode bytearrayfromhex)rErvrrr translate_hexPs  zAuditRecord.translate_hexcCs g|_i|_xtjj|D]}|jd}|jd}|jd}y|dkrbtjt |d}tj |}|dkr|jdj ds|j |}|d kryt jtt |}Wn YnX|d krtjt |tj}|r|}Wntk rYnX||j|<|jj|qWdS)Nr.r"archrTrvrnrmrqroexitsyscall)rTrvrnrmrqro) fields_ordrGr key_value_pair_refinditerr&stripauditZaudit_elf_to_machiner'Zaudit_machine_to_name startswithrerrno errorcodeabsZaudit_syscall_to_nameZaudit_detect_machinerZappend)rErr*rsryiZ syscall_namerrrrh_s4      z AuditRecord.set_fields_from_textcCs |jj|S)N)rGget)rErTrrr get_fieldszAuditRecord.get_fieldcCs"t|}tjtjtjtj|j|S)N)rCstructpackr binary_header_formatbinary_versionbinary_header_sizer)rErt msg_lengthrrrget_binary_headers zAuditRecord.get_binary_headercsjjdkrdSjdkr4djjdjjf}ndjjf}|djfddjDd7}|S) Nrz#type=%s msg=%s: avc: denied { %s }  ztype=%s msg=%s: csg|]}d|j|fqS)z%s=%s)rG)r4k)rErrr5sz.AuditRecord.fields_to_text.. )rGrrrDaccessr)rEZbufr)rErfields_to_texts  "zAuditRecord.fields_to_textcCsd|j|j|jfS)Nztype=%s msg=%s: %s )rrr)rErrrto_textszAuditRecord.to_textcCs2|jjdk r&d|jj|j|j|jfS|jSdS)Nznode=%s type=%s msg=%s: %s )rr)rrr)rErrrrjs zAuditRecord.to_host_textcCsd|j|jf}|j||S)Nz%s: %s)rrr)rErecordrrr to_binaryszAuditRecord.to_binary)NN)rUrVrWrr'rQrrrcalcsizerrcompilerrirxr?rbrIrkr^rzrrhrrrrrjrrXrr)rHrr s0      " c@s,eZdZdZdZddZddZddZd S) r r.rcCsV||_d|_d|_|j|jkr(|j|_n*|j|jkr>|j|_ntd||j j fdS)Nrrz unknown record format (%s) in %s) record_format _input_bufferrar feed_textZfeedr feed_binaryrZrHrU)rErrrrr?s    zAuditRecordReader.__init__c cst|dkrdS|j|7_xt|jtjkr4dStjtj|jdtj\}}}}tj|}t|j|krrdS|jtj|}t|\}} } |j|d|_|r tj || | ddfVq WdS)Nr) rCrr rrunpackrr1rZaudit_msg_type_to_name) rEnew_datarrrrZ total_lenrrrrrrrrs"  zAuditRecordReader.feed_binaryc cst|dkrdS|j|7_d}|jjd|}xh|dkr|jd7_|d7}|j||}t|\}}}}|r|||d|jfV|}|jjd|}q2W|j|d|_dS)Nrrr.)rCrfindrar) rErstartendlinerrrrrrrrs   zAuditRecordReader.feed_textN)rUrVrWrrr?rrrrrrr s  !cseZdZddeddeddZfddZdd Zd d Zd$d dZ ddZ e ddZ ddZ ddZd%ddZddZddZddZd d!Zd"d#ZZS)&r r`r)r7rPrY)r7rY)recordsrcs*tt|jd|_g|_i|_d|_dS)N)r>r r?rr record_types timestamp)rE)rHrrr?s zAuditEvent.__init__cCs4t|dddkri|_x|jD]}|j|qWdS)Nr)rSrrprocess_record)rErrrrrbs zAuditEvent._init_postprocesscCsL|j}|jd|j|j|jdjdd|Ddjdd|jDfS)Nz2%s: is_avc=%s, is_granted=%s: line_numbers=[%s] %s,cSsg|] }t|qSr)rK)r4rrrrr5 sz&AuditEvent.__str__..rcSsg|] }d|qS)z %sr)r4rrrrr5 s) line_numberssortris_avc is_grantedrDr)rErrrrrIs zAuditEvent.__str__rcCs|jdd|jDS)NcSsg|] }t|qSr)rK)r4rrrrr5sz%AuditEvent.format..)rDr)rEZ separatorrrrrL szAuditEvent.formatcCs t|jS)N)rCr)rErrr num_recordsszAuditEvent.num_recordscCsdd|jDS)NcSsg|]}|jr|jqSr)ra)r4rrrrr5sz'AuditEvent...)r)rErrrrszAuditEvent.cCs|jj||j|dS)N)rrr)rErrrr add_records zAuditEvent.add_recordcCsp|jdkr2|jj|_t|jj|jjd|_n |j|jksRtd|j|jf|jj|j g}|j |dS)Ng@@zBcannot add audit record to audit event, event_id mismatch %s != %s) rr\r]r+r,rrZr setdefaultrr)rErZ record_listrrrrs   zAuditEvent.process_recordNcCsVg}|dkr|j}n |j|}x2|D]*}|jj|}|dkr>q$|j||jfq$W|S)aNReturn 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)rget_records_of_typerGrrr;)rErTrrwrrryrrrr%s   zAuditEvent.get_fieldcCs d}|jj|}|r|d}|S)Nr)rr)rEr;rrrrrget_record_of_type9s  zAuditEvent.get_record_of_typecCs|jj|gS)N)rr)rEr;rrrr@szAuditEvent.get_records_of_typecCs$xdD]}|j|}|r|SqWdS)Nrrcrdre)rrcrdre)r)rErrrrrget_avc_recordCs  zAuditEvent.get_avc_recordcCs |jdk S)N)r)rErrrrIszAuditEvent.is_avccCsH|j}|dkrdS|jd}|dkr*dS|dkr6dStjjd|dS)NFrfZdeniedZgrantedTz!unknown value for seresult ('%s'))rrGlogZavcwarn)rE avc_recordrfrrrrLs zAuditEvent.is_granted)r)N)rUrVrWr rrQr?rbrIrLrr_rrrrrrrrrrXrr)rHrr s      c@s|eZdZdgZddgZdddddgZddddddgZddddddgZddd d gZd ddd dd d gZ dd ddddd d ddd d g Z ddddddgZ ddddddddd g Z dddddddd gZ dd dddd ddd d ddddd dgZddddgZddgZdgZd gZddddddgZdddddd dgZdddddd dgZdd dd dd dd d dddddddgZdgZd gZdddddgZdddddgZddd dddgZdddd dddgZdd gZdd dd dd dd dd ddg Zej dZ!ej dZ"dBddZ#ddZ$ddZ%dd Z&d!d"Z'd#d$Z(d%d&Z)d'd(Z*d)d*Z+d+d,Z,d-d.Z-d/d0Z.d1d2Z/d3d4Z0d5d6Z1d7d8Z2d9d:Z3d;d<Z4d=d>Z5d?d@Z6dAS)CrrSZexecuteopenreadlockZioctlrlinkunlinkrenameZcreatesetattrwriterZadd_nameZ remove_nameZreparentrmdirZmountZremountZunmountz^(\w+):\[([^\]]*)\]z^(/proc/)(\d+)(.*)TcCs||_||_i|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ g|_ g|_ d|_d|_d|_d|_d|_g|_|jdS)N) audit_eventquery_environmenttemplate_substitutionstpathspathsource source_pkgrscontexttcontexttclassportsrc_rpmstgt_rpmsr)pidkmodrwhybools derive_avc_info_from_audit_event)rErrrrrr?s*z AVC.__init__cCs|jS)N) format_avc)rErrrrIsz AVC.__str__cCsNd}|d|j7}|d|j7}|d|j7}|d|j7}|d|j7}|S)Nrz scontext=%s z tcontext=%s z access=%s z tclass=%s z tpath=%s )rrrrr)rErrrrrszAVC.format_avccCs.|jdkrdSx|jD]}||krdSqWdS)zMReturns true if the AVC contains _any_ of the permissions in the access list.NFT)r)rE access_listarrrhas_any_access_ins   zAVC.has_any_access_incCs.|jdkrdSx|jD]}||krdSqWdS)zmReturns true if _every_ access in the AVC matches at least one of the permissions in the access list.NFT)r)rErrrrrall_accesses_are_ins   zAVC.all_accesses_are_inc Cstt}|jt}|jg}ddddttgt|jjt |j t |j iDD}|}x,|D]$}||krd|j ttt|dqdWx&|D]}||kr||kr|j|qW|j|S)NcSsg|] }|tqSr)ZTARGET)r4rrrrr5sz,AVC.allowed_target_types..cSsg|]}|dr|qS)Zenabledr)r4rrrrr5stypes)Zget_all_file_typesZget_all_port_typesrZget_all_attributesrALLOWSOURCErr;ZCLASSrZPERMSrextendnextinfoZ ATTRIBUTEr)rEZ all_typesZall_attributesZ allowed_typesZwtypesrtrrrallowed_target_typess  4  zAVC.allowed_target_typesc Cs@|jdgr pipe socket:[1234] --> socket /proc/1234/fd --> /proc//fd ./foo --> ./foo /etc/sysconfig --> /etc/sysconfig NrTrvinoPATHZnametypeZPARENTrrrz%srp/rdevz/dev/z /proc/mountsrrr.r z/dev/%srzunknown mountpointFZlocatez-bz\%sT)stderrZuniversal_newlinesz \1\3@Z filesystemrZ udp_socketZ tcp_socketzport %sZUnknown)"rrrrendswithrrvexistslstatst_rdevr'rrrBrCstatst_inorOSErrorclose TypeErrorr subprocessZ check_outputZSTDOUTproc_pid_instance_resubpipe_instance_path_rerrrr_r)rErvrTZinodestrZavc_path_recordsZavc_path_recordrrZmatchesZdev_rdevrrfdrrr Zcommandoutputrrr*rrr _set_tpaths               :        zAVC._set_tpathc Csxd|_d|_d|_d|_d|_g|_d}}}}|jj|_|jj d}|jj d|_ t |j tsxt|jj d|_ t |jtst|jj d|_|jj d|_|jj ddkr|jj d|_n|jj d|_|j|jj d |_|jj d |_|r8|j d }|j d }|j d |_|j ddk|_|j d|_|dkrN|jj d }|dkrd|jj d }||_|rx||_n|r|j|_|js|j|_|js|j j|_|jj d}|r|j d}nd}|jjd}xR|D]J} | j d} tjj| s| r|jj| n|jjtjj|| qWg|_g|_|jjj |_ t!j"t#|j t#|jt#|j|j \|_$} |j$t!j%krt&t'd|j|j$t!j(krt&t'd|j|j$t!j)krt&t'd|j$t!j*krt&t'd|j|j$t!j+krt&t'd|j|j$t!j,kr&t&t'd|j|j$t!j-krFt&t'd|j|j$t!j.kr`t&t'd|j$t!j/krt| |_0dS)NFZSYSCALLrgrrrdestsrcrrrqrnrsuccessyesrZCWDrorrTz8%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)1rrrrrZ syscall_pathsrrrrrrr@rrrrrrrrrr;rrrvisabsrrDrrrr) audit2whyZanalyzerKrrrZrZ DONTAUDITZNOPOLICYZBADTCONZBADSCONZ BADTCLASSZBADPERMZ BADCOMPUTEZBOOLEANr) rErqrnrrZsyscall_recordZ cwd_recordroZ path_recordsZ path_recordrvrrrrrs                   *  z$AVC.derive_avc_info_from_audit_eventcCsP|jrL|jr,t|j|_|jr,|jj|j|jrLt|j}|rL|jj|dS)N)rrZget_package_nvr_by_file_pathrrrrr)rEZrpmrrrrs  zAVC.derive_environmental_infocCs|jdkr||_dS)N)r)rErvrrr set_alt_path s zAVC.set_alt_pathcKs,x&t|jD]\}}|r||j|<qWdS)N)rPrwr)rEkwdsrsryrrrset_template_substitutionsszAVC.set_template_substitutionscCsVt|jj|jd<t|jj|jd<t|j|jd<t|j|jd<|jrdtjddt|j|jd<t|j |jd<|j rtjddt|j |jd <|j dkrd|jd <nJ|j d krt|j |jd <n.|j d krtt j j |j |jd <n d|jd <t|j |jd <|jdkrd|jd<ntdj|j|jd<t|j|jd<t|j|jd<dS)NZ SOURCE_TYPEZ TARGET_TYPErZ SOURCE_PATHr.ZFIX_SOURCE_PATHZ TARGET_PATHZFIX_TARGET_PATHZ TARGET_DIRrprrZ TARGET_CLASSZACCESSZSOURCE_PACKAGEZ PORT_NUMBER) escape_htmlrr;rrrrrr rrrrvdirnamerrDrr)rErrrrs,       z)AVC.update_derived_template_substitutionscCs:x4t|jjD]"\}}|dkrtt||j|<qWdS)N)rPrrwrZ default_text)rErsryrrrvalidate_template_substitutions5sz#AVC.validate_template_substitutionsN)T)7rUrVrWZstat_file_permsZ x_file_permsZ r_file_permsZ rx_file_permsZ ra_file_permsZlink_file_permsZcreate_lnk_permsZcreate_file_permsZ r_dir_permsZ rw_dir_permsZ ra_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_permsrrrr r?rIrrrrrrrrrrrrrrrrrrrrrrr[sn            b ))Z __future__rrAZ six.movesrr|__all__rrrrrrJbase64rZselinux.audit2whyrZsetroubleshoot.utilZsetroubleshoot.html_utilZsetroubleshoot.xml_serializeZsepolicyrZcmpZget_standard_directoriesrrrrr%rr0r1r2r3Z XmlSerializerrr r r rrrrrsV     )<!Mj