SYSTRACE(4)

NAME
systrace - enforce and generate policies for system calls

SYNOPSIS
options SYSTRACE

DESCRIPTION
The systrace kernel facility provides a mechanism to manipulate and enforce access policies for system calls. Using the systrace facility it is possible to monitor and control a process's access to the kernel through system calls.

Access to the systrace facility is provided to userland processes, such as systrace(1), through an ioctl(2) interface on the pseudo-device /dev/systrace. This interface allows messages to be sent from the kernel to the userland process to request confirmation of an access policy.

The systrace facility can assign the following policies to system calls for a specific process:

SYSTR_POLICY_ASK
    Send a message on /dev/systrace requesting the access policy for the system call.

SYSTR_POLICY_PERMIT
    Immediately allow the system call.

SYSTR_POLICY_NEVER
    Immediately deny the system call and return an error code.

SYSTRACE MESSAGES
A read(2) operation on the systrace pseudo-device will block if there are no pending messages, or return the following structure:

    struct str_message {
    	int32_t msg_type;
    #define SYSTR_MSG_ASK		1
    #define SYSTR_MSG_RES		2
    #define SYSTR_MSG_EMUL		3
    #define SYSTR_MSG_CHILD		4
    #define SYSTR_MSG_UGID		5
    #define SYSTR_MSG_POLICYFREE	6
    #define SYSTR_MSG_EXECVE	7
    #define SYSTR_MSG_SCRIPTNAME	8
    	pid_t msg_pid;
    	uint16_t msg_seqnr;	/* answer has to match seqnr */
    	int16_t msg_policy;
    	union {
    		struct str_msg_emul msg_emul;
    		struct str_msg_ugid msg_ugid;
    		struct str_msg_ask msg_ask;
    		struct str_msg_child msg_child;
    		struct str_msg_execve msg_execve;
    	} msg_data;
    };
    struct str_msg_emul {
    	char emul[SYSTR_EMULEN];
    };
    struct str_msg_ugid {
    	uid_t uid;
    	gid_t gid;
    };
    struct str_msg_execve {
    	char path[MAXPATHLEN];
    };
    struct str_msg_ask {
    	int32_t code;
    	int32_t argsize;
    	register_t args[SYSTR_MAXARGS];
    	register_t rval[2];
    	int32_t result;
    };
    struct str_msg_child {
    	pid_t new_pid;
    };

These messages are all sent to the userland control process.

SYSTR_MSG_ASK
    This message is sent whenever the kernel does not have a cached simple policy for system call number code within the currently set emulation.

SYSTR_MSG_RES
    This message is sent whenever a system call is flagged with SYSTR_FLAGS_RESULT.

SYSTR_MSG_EMUL
    This message is sent whenever a process gains a child.

SYSTR_MSG_UGID
    This message is sent whenever the effective UID or GID has changed during the execution of a system call.

SYSTR_MSG_POLICYFREE
    This message is sent whenever the kernel frees the policy identified by msg_policy.

SYSTR_MSG_EXECVE
    This message is sent whenever, before a call to execve(2) a process is privileged (technically, the process has the ,Dv P_SUGID flag set), but after the call these privileges have been dropped. The new image name is specified in the path argument.

IOCTL INTERFACE
The systrace facility supports the following ioctl(2) operations:

STRIOCATTACH pid_t
    Attach to the process with the specified process ID. This operation will fail under the following conditions:

    1.  The process is trying to attach to itself.
    2.  The process is a system process.
    3.  The process is being traced already.
    4.  You do not own the process and you are not root.
    5.  The process is init(8), and the kernel was not compiled with option INSECURE.

STRIOCDETACH pid_t
    Wake up the process if it is waiting for an answer, and detach from it.

STRIOCANSWER struct systrace_answer
    Notify the systrace facility in response to a SYSTR_MSG_ASK message what to do with a system call that was assigned a policy of SYSTR_POLICY_ASK.

        struct systrace_answer {
        	pid_t stra_pid;		/* PID of process being traced */
        	uint16_t stra_seqnr;
        	int16_t reserved;
        	uid_t stra_seteuid;
        	gid_t stra_setegid;
        	int32_t stra_policy;    /* Policy to assign */
        	int32_t stra_error;     /* Return value of denied syscall
        	                       (will return EPERM if zero) */
        	int32_t stra_flags;
        #define	SYSTR_FLAGS_RESULT	0x0001	/* Report syscall result */
        #define SYSTR_FLAGS_SETEUID	0x002
        #define SYSTR_FLAGS_SETEGID	0x004
        };

    Valid return values for stra_policy are SYSTR_POLICY_PERMIT, SYSTR_POLICY_ASK, and SYSTR_POLICY_NEVER.

STRIOCIO struct systrace_io
    Copy data in/out of the process being traced.

        struct systrace_io {
        	pid_t strio_pid;    /* PID of process being traced */
        	int32_t strio_ops;
        #define	SYSTR_READ	1
        #define	SYSTR_WRITE	2
        	void *strio_offs;
        	void *strio_addr;
        	size_t strio_len;
        };

STRIOCPOLICY struct systrace_policy
    Manipulate the set of policies.

        struct systrace_policy {
        	int strp_op;
        #define	SYSTR_POLICY_NEW	1    /* Allocate a new policy */
        #define	SYSTR_POLICY_ASSIGN	2    /* Assign policy to process */
        #define	SYSTR_POLICY_MODIFY	3    /* Modify an entry */
        	int32_t strp_num;
        	union {
        		struct {
        			int16_t code;
        #define SYSTR_POLICY_ASK	0
        #define SYSTR_POLICY_PERMIT	1
        #define SYSTR_POLICY_NEVER	2
        			int16_t policy;
        		} assign;
        		pid_t pid;
        		int32_t maxents;
        	} strp_data;
        #define strp_pid	strp_data.pid
        #define strp_maxents	strp_data.maxents
        #define strp_code	strp_data.assign.code
        #define strp_policy	strp_data.assign.policy
        };

    The SYSTR_POLICY_NEW operation allocates a new policy with all entries initialized to SYSTR_POLICY_ASK, and returns the new policy number into strp_num. The SYSTR_POLICY_ASSIGN operation attaches the policy identified by strp_num to strp_pid, with a maximum of strp_maxents entries. The SYSTR_POLICY_MODIFY operation changes the entry indexed by strp_code to strp_policy.

STRIOCGETCWD pid_t
    Set the working directory of the current process to that of the specified process.

STRIOCRESCWD
    Restore the working directory of the current process.

STRIOCREPORT pid_t *
    Report the current emulation a process is using inside the msg_emul structure.

STRIOCREPLACE struct systrace_replace *
    Arrange for system call arguments to be replaced by arguments supplied by the monitoring process.

        struct systrace_replace {
        	pid_t strr_pid;
        	uint16_t strr_seqnr;
        	int16_t reserved;
        	int32_t strr_nrepl;	/* # of arguments to replace */
        	void *strr_base;	/* Base user memory */
        	size_t strr_len;	/* Length of memory */
        	int32_t strr_argind[SYSTR_MAXARGS]; /* Argument indexes */
        	size_t strr_off[SYSTR_MAXARGS];	/* Argumen offsets */
        	size_t strr_offlen[SYSTR_MAXARGS]; /* Argument sizes */
        	int32_t strr_flags[SYSTR_MAXARGS];
        };

STRIOCSCRIPTNAME struct systrace_scriptname *
    Set the path of executed scripts to sn_scriptname.

        struct systrace_scriptname {
        	pid_t sn_pid;
        	char  sn_scriptname[MAXPATHLEN];
        };

FILES
/dev/systrace
    system call tracing facility

SEE ALSO
systrace(1), ioctl(2), read(2), options(4), init(8)

HISTORY
The systrace facility first appeared in OpenBSD 3.2, and then in NetBSD 2.0.

CAVEATS
When creating new policies, if strp_maxents is not large enough to accommodate any system calls needed for fundamental process operations, the traced process will block forever.