a ‰ÄÏhÊ.ã@szdZddlmZddlmZddlmZdd„ZGdd „d ejƒZd d „Z Gd d „d ƒZ dd„Z dd„Z Gdd„dƒZ dS)aW Classes representing basic access. SELinux - at the most basic level - represents access as the 4-tuple subject (type or context), target (type or context), object class, permission. The policy language elaborates this basic access to facilitate more concise rules (e.g., allow rules can have multiple source or target types - see refpolicy for more information). This module has objects for representing the most basic access (AccessVector) and sets of that access (AccessVectorSet). These objects are used in Madison in a variety of ways, but they are the fundamental representation of access. é)Ú refpolicy)Úutilé)Ú audit2whycCsNt|ƒdkrF|ddkrFzt|dd…ƒWnty@YdS0dSdSdS)z¬Determine if an id is a parameter in the form $N, where N is an integer. Returns: True if the id is a parameter False if the id is not a parameter rrú$NFT)ÚlenÚintÚ ValueError)Úid©r ú3/usr/lib/python3.9/site-packages/sepolgen/access.pyÚ is_idparam's r c@sJeZdZdZddd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dS)Ú AccessVectora An access vector is the basic unit of access in SELinux. Access vectors are the most basic representation of access within SELinux. It represents the access a source type has to a target type in terms of an object class and a set of permissions. Access vectors are distinct from AVRules in that they can only store a single source type, target type, and object class. The simplicity of AccessVectors makes them useful for storing access in a form that is easy to search and compare. The source, target, and object are stored as string. No checking done to verify that the strings are valid SELinux identifiers. Identifiers in the form $N (where N is an integer) are reserved as interface parameters and are treated as wild cards in many circumstances. Properties: .src_type - The source type allowed access. [String or None] .tgt_type - The target type to which access is allowed. [String or None] .obj_class - The object class to which access is allowed. [String or None] .perms - The permissions allowed to the object class. [IdSet] .audit_msgs - The audit messages that generated this access vector [List of strings] .xperms - Extended permissions attached to the AV. [Dictionary {operation: xperm set}] NcCsV|r| |¡nd|_d|_d|_t ¡|_g|_tj |_ g|_ i|_ d|_ d|_dS©N)Ú from_listÚsrc_typeÚtgt_typeÚ obj_classrÚIdSetÚpermsÚ audit_msgsrÚTERULEÚtypeÚdataÚxpermsÚ__hash__Z info_flow_dir)ÚselfZ init_listr r r Ú__init__Ss  zAccessVector.__init__cCsRt|ƒdkrtdt|ƒƒ‚|d|_|d|_|d|_t |dd…¡|_dS)axInitialize an access vector from a list. Initialize an access vector from a list treating the list as positional arguments - i.e., 0 = src_type, 1 = tgt_type, etc. All of the list elements 3 and greater are treated as perms. For example, the list ['foo_t', 'bar_t', 'file', 'read', 'write'] would create an access vector list with the source type 'foo_t', target type 'bar_t', object class 'file', and permissions 'read' and 'write'. This format is useful for very simple storage to strings or disc (see to_list) and for initializing access vectors. éz+List must contain at least four elements %srrééN) rr Ústrrrrrrr)rÚlistr r r rhs     zAccessVector.from_listcCs$|j|j|jg}| t|jƒ¡|S)zî Convert an access vector to a list. Convert an access vector to a list treating the list as positional values. See from_list for more information on how an access vector is represented in a list. )rrrÚextendÚsortedr)rÚlr r r Úto_list}szAccessVector.to_listcCsL|j |j¡|jD]2}||jvr0t ¡|j|<|j| |j|¡qdS)z0Add permissions and extended permissions from AVN)rÚupdaterrZXpermSetr#)rÚavÚopr r r Úmerge‰s   zAccessVector.mergecCs| ¡Sr)Ú to_string©rr r r Ú__str__’szAccessVector.__str__cCsd|j|j|j|j ¡fS)Nzallow %s %s:%s %s;)rrrrZ to_space_strr,r r r r+•s  ÿzAccessVector.to_stringc CstzTt|jƒ}|j|j|j|f}t|jƒ}| ¡| ¡|j|j|j|f}|||ƒWSttfyntYS0dSr) r"rrrrÚsortÚAttributeErrorÚ TypeErrorÚNotImplemented)rÚotherÚmethodÚxÚaÚyÚbr r r Ú_compare™s   zAccessVector._compare)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr&r*r-r+r8r r r r r8s   rcCsjt|tƒr|gSg}|jD]J}|jD]>}|jD]2}tƒ}||_||_||_|j  ¡|_|  |¡q.q$q|S)aRConvert an avrule into a list of access vectors. AccessVectors and AVRules are similarly, but differ in that an AVRule can more than one source type, target type, and object class. This function expands a single avrule into a list of one or more AccessVectors representing the access defined in the AVRule. ) Ú isinstancerZ src_typesZ tgt_typesZ obj_classesrrrrÚcopyÚappend)Zavruler5rrrÚaccessr r r Úavrule_to_access_vectors§s     rAc@sTeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd e j gfd d„Z ddd„Z d S)ÚAccessVectorSeta/A non-overlapping set of access vectors. An AccessVectorSet is designed to store one or more access vectors that are non-overlapping. Access can be added to the set incrementally and access vectors will be added or merged as necessary. For example, adding the following access vectors using add_av: allow $1 etc_t : read; allow $1 etc_t : write; allow $1 var_log_t : read; Would result in an access vector set with the access vectors: allow $1 etc_t : { read write}; allow $1 var_log_t : read; cCsi|_d|_dS)z)Initialize an access vector set. N)ÚsrcZinfo_dirr,r r r rÏszAccessVectorSet.__init__ccs6|j ¡D]&}| ¡D]}| ¡D] }|Vq"qq dS)z9Iterate over all of the unique access vectors in the set.N)rCÚvalues)rÚtgtsÚobjsr(r r r Ú__iter__Øs  zAccessVectorSet.__iter__cCs2d}|j ¡D]}| ¡D]}|t|ƒ7}qq|S)a6Return the number of unique access vectors in the set. Because of the internal representation of the access vector set, __len__ is not a constant time operation. Worst case is O(N) where N is the number of unique access vectors, but the common case is probably better. r)rCrDr)rr%rErFr r r Ú__len__ßs  zAccessVectorSet.__len__cCs g}|D]}| | ¡¡q|S)abReturn the unique access vectors in the set as a list. The format of the returned list is a set of nested lists, each access vector represented by a list. This format is designed to be simply serializable to a file. For example, consider an access vector set with the following access vectors: allow $1 user_t : file read; allow $1 etc_t : file { read write}; to_list would return the following: [[$1, user_t, file, read] [$1, etc_t, file, read, write]] See AccessVector.to_list for more information. )r?r&©rr%r(r r r r&íszAccessVectorSet.to_listcCs|D]}| t|ƒ¡qdS)aAdd access vectors stored in a list. See to list for more information on the list format that this method accepts. This will add all of the access from the list. Any existing access vectors in the set will be retained. N)Úadd_avrrIr r r rs zAccessVectorSet.from_listNc Cs:tƒ}||_||_||_||_||_||_| ||¡dS)z)Add an access vector to the set. N)rrrrrrrrJ) rrrrrÚ audit_msgZavc_typerr(r r r ÚaddszAccessVectorSet.addcCsv|j |ji¡}| |ji¡}|j|jf|vrF||j|jf |¡n|||j|jf<|rr||j|jfj |¡dS)z Add an access vector to the set.N) rCÚ setdefaultrrrrr*rr?)rr(rKZtgtÚclsr r r rJszAccessVectorSet.add_av)N) r9r:r;r<rrGrHr&rrrrLrJr r r r rBÀs   rBcCs.t ¡}|D]}| |j¡| |j¡q |Sr)rrrLrr)ÚavsÚtypesr(r r r Úavs_extract_types*s  rQcCsFi}|D]8}|j|vr"||j}nt ¡}|||j<| |j¡q|Sr)rrrr'r)rOrr(Úsr r r Úavs_extract_obj_perms2s   rSc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú RoleTypeSetz³A non-overlapping set of role type statements. This class allows the incremental addition of role type statements and maintains a non-overlapping list of statements. cCs i|_dS)z Initialize an access vector set.N)Ú role_typesr,r r r rCszRoleTypeSet.__init__ccs|j ¡D] }|Vq dS)zAIterate over all of the unique role allows statements in the set.N)rUrD)rÚ role_typer r r rGGszRoleTypeSet.__iter__cCst|j ¡ƒS)z2Return the unique number of role allow statements.)rrUÚkeysr,r r r rHLszRoleTypeSet.__len__cCs>||jvr|j|}nt ¡}||_||j|<|j |¡dSr)rUrZRoleTypeÚrolerPrL)rrXrrVr r r rLPs    zRoleTypeSet.addN)r9r:r;r<rrGrHrLr r r r rT=s rTN)r<ÚrrZselinuxrr Z ComparisonrrArBrQrSrTr r r r Ús   oj