
    l
j'                     D   d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ddlZddlmZ dZdZdZdZd	Z ed
      Z G d dej*                        Z G d dej.                        Z G d dej.                        Zde	e   fdZdede	e   fdZ	 d#dedej<                  de
de	e   def
dZdedej<                  de
defdZ dedej<                  de
defdZ!dedej<                  de
defdZ" ejF                  d      dejH                  fd       Z% ejF                  d      defd       Z& ejF                  d      defd       Z' ejF                  d      defd        Z( ejF                  d      defd!       Z) ejF                  d      defd"       Z*y)$z Landlock constants and syscalls.    N)CallableOptionalTupleTypeVar)SyscallError   i  i  i  &   Tc                       e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 d	Z	 d
Z	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 ed        Zed        Zed        Zy)FSAccessa  Flags representing types of file system actions.

    These flags enable one to restrict a sandboxed process
    to a set of actions on files and directories.

    Files or directories opened before the sandboxing
    are not subject to these restrictions.
    r         i @            @         i   i   i   i   i    i   c                 D    | j                         | j                         z  S N)all_fileall_dir)clss    ,/usr/libexec/warpinator/landlock/plumbing.pyallzFSAccess.allY   s    ||~--    c                     | j                   | j                  z  | j                  z  }t               dk\  r|| j                  z  }t               dk\  r|| j
                  z  }|S )N      )EXECUTE
WRITE_FILE	READ_FILElandlock_abi_versionTRUNCATE	IOCTL_DEVr   flagss     r   r   zFSAccess.all_file]   sT    cnn,s}}<!Q&S\\!E!Q&S]]"Er   c                 x   | j                   | j                  z  | j                  z  | j                  z  | j                  z  | j
                  z  | j                  z  | j                  z  | j                  z  | j                  z  }t               dk\  r|| j                  z  }t               dk\  r|| j                  z  }|S )Nr   r   )READ_DIR
REMOVE_DIRREMOVE_FILE	MAKE_CHARMAKE_DIRMAKE_REG	MAKE_SOCK	MAKE_FIFO
MAKE_BLOCKMAKE_SYMr#   REFERr%   r&   s     r   r   zFSAccess.all_dirh   s     LLnnoo mm ll	
 ll mm mm nn ll	 	  !Q&SYYE!Q&S]]"Er   N)__name__
__module____qualname____doc__r    r!   r"   r$   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r%   classmethodr   r   r    r   r   r   r      s     GJ(I'H H/ J2K$I8H)H4I:I2J4H5E I
 . .    r   r   c                   *    e Zd Zdej                  fgZy)RulesetAttrhandled_access_fsN)r4   r5   r6   ctypesc_uint64_fields_r9   r   r   r;   r;      s    $foo67Hr   r;   c                   D    e Zd Zdej                  fdej
                  fgZy)PathBeneathAttrallowed_access	parent_fdN)r4   r5   r6   r=   r>   c_int32r?   r9   r   r   rA   rA      s!    	6??+	fnn%Hr   rA   returnc                      t        j                         } | dk7  rd|  S t        j                         }t        t	        t
        |j                  d      d   j                  d                  }|dk  rd| S y )NLinuxzDLandlock is a only available on Linux, it looks like you're running -r   .)r      zQLandlock is only available in kernel 5.13 or newer, it looks like you're running )platformsystemreleasetuplemapintsplit)rL   kernel_versionkernel_version_tuples      r   !find_generic_reason_from_platformrT      s    __F--3H6	
 %%'N S.*>*>s*CA*F*L*LS*Q!RSg%--;,<>	

 r   errc                 h    t         j                  dt         j                  di}|j                  |       S )NzbCannot find Landlock syscalls - perhaps the kernel was not built with 'CONFIG_SECURITY_LANDLOCK=y'zLandlock has been disabled at boot time. The CONFIG_LSM configuration item should list 'landlock', or, 'lsm=landlock' needs to be added the kernel's command-line arguments (usually via your bootloader).)errnoENOSYS
EOPNOTSUPPget)rU   errno_to_reasons     r   find_generic_reason_from_errnor\      s7     - AO s##r   resultfunc	argumentsreasonc                 
   t        j                         }|dk(  r| S t        |d|j                        }t	               xs t        |      xs |}t        |t        j                  |      d| d|j                   | d|  |      )Nr   namezError calling z: z = r`   )	r=   	get_errnogetattrr4   rT   r\   r   osstrerror)r]   r^   r_   r`   rU   rb   s         r   syscall_errcheckrh      s     


C ax 4/D)+ 	)#.	 
 
C
b	{#fXF	 r   c           	          t         j                  dt         j                  dt         j                  dt         j                  di}t        | |||j                  t        j                                     S )Nz7Unknown 'flags', or unknown access, or too small 'size'z 'attr' or 'size' inconsistenciesz/Empty 'landlock_ruleset_attr.handled_access_fs')	rW   EINVALE2BIGEFAULTENOMSGrh   rZ   r=   rd   r]   r^   r_   r[   s       r   create_ruleset_errcheckro      s]    O78G	O i!4!4V5E5E5G!H r   c                 $   t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  dt         j                  di}t        | |||j                  t        j                                     S )Nz'flags' is not 0, or inconsistent access in the rule (i.e. 'landlock_path_beneath_attr.allowed_access' is not a subset of the ruleset handled accesses)zAEmpty accesses (e.g. 'landlock_path_beneath_attr.allowed_access')z}'ruleset_fd' is not a file descriptor for the current thread, or a member of 'rule_attr' is not a file descriptor as expectedzr'ruleset_fd' is not a ruleset file descriptor, or a member of 'rule_attr' is not the expected file descriptor typez:'ruleset_fd' has no write access to the underlying rulesetz'rule_attr' inconsistency)rW   rj   rm   EBADFEBADFDEPERMrl   rh   rZ   r=   rd   rn   s       r   add_rule_errcheckrt      s}     < 	 > K OQ1O i!4!4V5E5E5G!H r   c           
         t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  di}t        | |||j                  t        j                                     S )Nz'flags' is not 0z<'ruleset_fd' is not a file descriptor for the current threadz-'ruleset_fd' is not a ruleset file descriptoru   'ruleset_fd' has no read access to the underlying ruleset, or the current thread is not running with no_new_privs, or it doesn’t have 'CAP_SYS_ADMIN' in its namespacezSThe maximum number of stacked rulesets (16) has been reached for the current thread)
rW   rj   rq   rr   rs   rk   rh   rZ   r=   rd   rn   s       r   restrict_self_errcheckrv      sn    (SE A 	 3	O i!4!4V5E5E5G!H r   c                      	 t        j                  d d      S # t        $ r2} t        j                         dk(  rt        t                     |  d } ~ ww xY w)NT)	use_errnoWindowsrc   )r=   CDLL	TypeErrorrK   rL   r   rT   )es    r   get_libcr}      sL    {{4400 ??	)&G&IJPQQ	s    	A-AAc                     t               } t        j                  | d   t              }d|j                  _        t        j                  t        j                  t              t        j                  t        j                  f|j                  _        t        |j                  _        t        j                  |j                  _        |S )Nsyscalllandlock_create_ruleset)r}   	functoolspartialSYSCALL_CREATE_RULESETr^   rb   r=   c_longPOINTERr;   c_size_tc_uint32argtypesro   errcheckrestype)libccreate_rulesets     r   get_create_rulesetr      s    :D&&tI8NON8N{#	$N  $;N "(--Nr   c                     t               } t        j                  | d   t              }d|j                  _        t        j                  t        j                  t        j                  t        j                  t              t        j                  f|j                  _        t        |j                  _        t        j                  |j                  _        |S )Nr   landlock_add_rule)r}   r   r   SYSCALL_ADD_RULEr^   rb   r=   r   c_intc_uintr   rA   r   r   rt   r   r   )r   add_rules     r   get_add_ruler     s    :D  i2BCH,HMM'HMM /HMM"MMHMMOr   c                  Z   t               } t        j                  | d   t              }d|j                  _        t        j                  t        j                  t        j                  f|j                  _
        t        |j                  _        t        j                  |j                  _        |S )Nr   landlock_restrict_self)r}   r   r   SYSCALL_RESTRICT_SELFr^   rb   r=   r   r   r   r   rv   r   r   )r   restrict_selfs     r   get_restrict_selfr   "  s{    :D%%d9o7LMM6M#M
 #9M!'Mr   c                  8   t               } | j                  }d|_        t        j                  t        j
                  t        j
                  t        j
                  t        j
                  f|_        t        |_        t        j                  |_	        | j                  S )Nprctl)
r}   r   rb   r=   r   c_ulongr   rh   r   r   )r   r   s     r   	get_prctlr   3  se     :DJJEEJEN &ENLLEM::r   c                  .     t               d dt              S )Nr   )r   CREATE_RULESET_VERSIONr9   r   r   r#   r#   G  s    a)?@@r   r   )+r7   r=   enumrW   r   rf   rK   typingr   r   r   r   _ctypeslandlockr   r   r   r   r   PR_SET_NO_NEW_PRIVSr
   IntFlagr   	Structurer;   rA   strrT   rP   r\   CFuncPtrrh   ro   rt   rv   	lru_cacherz   r}   r   r   r   r   r#   r9   r   r   <module>r      sW   &     	  5 5  !     CLct|| cL8&"" 8f&& 8C= *	$ 	$ 	$ RV%%27AI#0	A 	W-=-= 	% 	TU 	a w'7'7 E a &1 G,<,<  ST   Q&++   QH  " Qh  $ Q8    Q8  & QAc A Ar   