freebsd-dev/sys/security/audit/audit_private.h
Pawel Jakub Dawidek 2609222ab4 Merge Capsicum overhaul:
- Capability is no longer separate descriptor type. Now every descriptor
  has set of its own capability rights.

- The cap_new(2) system call is left, but it is no longer documented and
  should not be used in new code.

- The new syscall cap_rights_limit(2) should be used instead of
  cap_new(2), which limits capability rights of the given descriptor
  without creating a new one.

- The cap_getrights(2) syscall is renamed to cap_rights_get(2).

- If CAP_IOCTL capability right is present we can further reduce allowed
  ioctls list with the new cap_ioctls_limit(2) syscall. List of allowed
  ioctls can be retrived with cap_ioctls_get(2) syscall.

- If CAP_FCNTL capability right is present we can further reduce fcntls
  that can be used with the new cap_fcntls_limit(2) syscall and retrive
  them with cap_fcntls_get(2).

- To support ioctl and fcntl white-listing the filedesc structure was
  heavly modified.

- The audit subsystem, kdump and procstat tools were updated to
  recognize new syscalls.

- Capability rights were revised and eventhough I tried hard to provide
  backward API and ABI compatibility there are some incompatible changes
  that are described in detail below:

	CAP_CREATE old behaviour:
	- Allow for openat(2)+O_CREAT.
	- Allow for linkat(2).
	- Allow for symlinkat(2).
	CAP_CREATE new behaviour:
	- Allow for openat(2)+O_CREAT.

	Added CAP_LINKAT:
	- Allow for linkat(2). ABI: Reuses CAP_RMDIR bit.
	- Allow to be target for renameat(2).

	Added CAP_SYMLINKAT:
	- Allow for symlinkat(2).

	Removed CAP_DELETE. Old behaviour:
	- Allow for unlinkat(2) when removing non-directory object.
	- Allow to be source for renameat(2).

	Removed CAP_RMDIR. Old behaviour:
	- Allow for unlinkat(2) when removing directory.

	Added CAP_RENAMEAT:
	- Required for source directory for the renameat(2) syscall.

	Added CAP_UNLINKAT (effectively it replaces CAP_DELETE and CAP_RMDIR):
	- Allow for unlinkat(2) on any object.
	- Required if target of renameat(2) exists and will be removed by this
	  call.

	Removed CAP_MAPEXEC.

	CAP_MMAP old behaviour:
	- Allow for mmap(2) with any combination of PROT_NONE, PROT_READ and
	  PROT_WRITE.
	CAP_MMAP new behaviour:
	- Allow for mmap(2)+PROT_NONE.

	Added CAP_MMAP_R:
	- Allow for mmap(PROT_READ).
	Added CAP_MMAP_W:
	- Allow for mmap(PROT_WRITE).
	Added CAP_MMAP_X:
	- Allow for mmap(PROT_EXEC).
	Added CAP_MMAP_RW:
	- Allow for mmap(PROT_READ | PROT_WRITE).
	Added CAP_MMAP_RX:
	- Allow for mmap(PROT_READ | PROT_EXEC).
	Added CAP_MMAP_WX:
	- Allow for mmap(PROT_WRITE | PROT_EXEC).
	Added CAP_MMAP_RWX:
	- Allow for mmap(PROT_READ | PROT_WRITE | PROT_EXEC).

	Renamed CAP_MKDIR to CAP_MKDIRAT.
	Renamed CAP_MKFIFO to CAP_MKFIFOAT.
	Renamed CAP_MKNODE to CAP_MKNODEAT.

	CAP_READ old behaviour:
	- Allow pread(2).
	- Disallow read(2), readv(2) (if there is no CAP_SEEK).
	CAP_READ new behaviour:
	- Allow read(2), readv(2).
	- Disallow pread(2) (CAP_SEEK was also required).

	CAP_WRITE old behaviour:
	- Allow pwrite(2).
	- Disallow write(2), writev(2) (if there is no CAP_SEEK).
	CAP_WRITE new behaviour:
	- Allow write(2), writev(2).
	- Disallow pwrite(2) (CAP_SEEK was also required).

	Added convinient defines:

	#define	CAP_PREAD		(CAP_SEEK | CAP_READ)
	#define	CAP_PWRITE		(CAP_SEEK | CAP_WRITE)
	#define	CAP_MMAP_R		(CAP_MMAP | CAP_SEEK | CAP_READ)
	#define	CAP_MMAP_W		(CAP_MMAP | CAP_SEEK | CAP_WRITE)
	#define	CAP_MMAP_X		(CAP_MMAP | CAP_SEEK | 0x0000000000000008ULL)
	#define	CAP_MMAP_RW		(CAP_MMAP_R | CAP_MMAP_W)
	#define	CAP_MMAP_RX		(CAP_MMAP_R | CAP_MMAP_X)
	#define	CAP_MMAP_WX		(CAP_MMAP_W | CAP_MMAP_X)
	#define	CAP_MMAP_RWX		(CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
	#define	CAP_RECV		CAP_READ
	#define	CAP_SEND		CAP_WRITE

	#define	CAP_SOCK_CLIENT \
		(CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
		 CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
	#define	CAP_SOCK_SERVER \
		(CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
		 CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
		 CAP_SETSOCKOPT | CAP_SHUTDOWN)

	Added defines for backward API compatibility:

	#define	CAP_MAPEXEC		CAP_MMAP_X
	#define	CAP_DELETE		CAP_UNLINKAT
	#define	CAP_MKDIR		CAP_MKDIRAT
	#define	CAP_RMDIR		CAP_UNLINKAT
	#define	CAP_MKFIFO		CAP_MKFIFOAT
	#define	CAP_MKNOD		CAP_MKNODAT
	#define	CAP_SOCK_ALL		(CAP_SOCK_CLIENT | CAP_SOCK_SERVER)

Sponsored by:	The FreeBSD Foundation
Reviewed by:	Christoph Mallon <christoph.mallon@gmx.de>
Many aspects discussed with:	rwatson, benl, jonathan
ABI compatibility discussed with:	kib
2013-03-02 00:53:12 +00:00

431 lines
13 KiB
C

/*-
* Copyright (c) 1999-2009 Apple Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* This include file contains function prototypes and type definitions used
* within the audit implementation.
*/
#ifndef _SECURITY_AUDIT_PRIVATE_H_
#define _SECURITY_AUDIT_PRIVATE_H_
#ifndef _KERNEL
#error "no user-serviceable parts inside"
#endif
#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/ucred.h>
#ifdef MALLOC_DECLARE
MALLOC_DECLARE(M_AUDITBSM);
MALLOC_DECLARE(M_AUDITDATA);
MALLOC_DECLARE(M_AUDITPATH);
MALLOC_DECLARE(M_AUDITTEXT);
MALLOC_DECLARE(M_AUDITGIDSET);
#endif
/*
* Audit control variables that are usually set/read via system calls and
* used to control various aspects of auditing.
*/
extern struct au_qctrl audit_qctrl;
extern struct audit_fstat audit_fstat;
extern struct au_mask audit_nae_mask;
extern int audit_panic_on_write_fail;
extern int audit_fail_stop;
extern int audit_argv;
extern int audit_arge;
/*
* Success/failure conditions for the conversion of a kernel audit record to
* BSM format.
*/
#define BSM_SUCCESS 0
#define BSM_FAILURE 1
#define BSM_NOAUDIT 2
/*
* Defines for the kernel audit record k_ar_commit field. Flags are set to
* indicate what sort of record it is, and which preselection mechanism
* selected it.
*/
#define AR_COMMIT_KERNEL 0x00000001U
#define AR_COMMIT_USER 0x00000010U
#define AR_PRESELECT_TRAIL 0x00001000U
#define AR_PRESELECT_PIPE 0x00002000U
#define AR_PRESELECT_USER_TRAIL 0x00004000U
#define AR_PRESELECT_USER_PIPE 0x00008000U
/*
* Audit data is generated as a stream of struct audit_record structures,
* linked by struct kaudit_record, and contain storage for possible audit so
* that it will not need to be allocated during the processing of a system
* call, both improving efficiency and avoiding sleeping at untimely moments.
* This structure is converted to BSM format before being written to disk.
*/
struct vnode_au_info {
mode_t vn_mode;
uid_t vn_uid;
gid_t vn_gid;
dev_t vn_dev;
long vn_fsid;
long vn_fileid;
long vn_gen;
};
struct groupset {
gid_t *gidset;
u_int gidset_size;
};
struct socket_au_info {
int so_domain;
int so_type;
int so_protocol;
in_addr_t so_raddr; /* Remote address if INET socket. */
in_addr_t so_laddr; /* Local address if INET socket. */
u_short so_rport; /* Remote port. */
u_short so_lport; /* Local port. */
};
/*
* The following is used for A_OLDSETQCTRL and AU_OLDGETQCTRL and a 64-bit
* userland.
*/
struct au_qctrl64 {
u_int64_t aq64_hiwater;
u_int64_t aq64_lowater;
u_int64_t aq64_bufsz;
u_int64_t aq64_delay;
u_int64_t aq64_minfree;
};
typedef struct au_qctrl64 au_qctrl64_t;
union auditon_udata {
char *au_path;
int au_cond;
int au_flags;
int au_policy;
int au_trigger;
int64_t au_cond64;
int64_t au_policy64;
au_evclass_map_t au_evclass;
au_mask_t au_mask;
auditinfo_t au_auinfo;
auditpinfo_t au_aupinfo;
auditpinfo_addr_t au_aupinfo_addr;
au_qctrl_t au_qctrl;
au_qctrl64_t au_qctrl64;
au_stat_t au_stat;
au_fstat_t au_fstat;
auditinfo_addr_t au_kau_info;
};
struct posix_ipc_perm {
uid_t pipc_uid;
gid_t pipc_gid;
mode_t pipc_mode;
};
struct audit_record {
/* Audit record header. */
u_int32_t ar_magic;
int ar_event;
int ar_retval; /* value returned to the process */
int ar_errno; /* return status of system call */
struct timespec ar_starttime;
struct timespec ar_endtime;
u_int64_t ar_valid_arg; /* Bitmask of valid arguments */
/* Audit subject information. */
struct xucred ar_subj_cred;
uid_t ar_subj_ruid;
gid_t ar_subj_rgid;
gid_t ar_subj_egid;
uid_t ar_subj_auid; /* Audit user ID */
pid_t ar_subj_asid; /* Audit session ID */
pid_t ar_subj_pid;
struct au_tid ar_subj_term;
struct au_tid_addr ar_subj_term_addr;
struct au_mask ar_subj_amask;
/* Operation arguments. */
uid_t ar_arg_euid;
uid_t ar_arg_ruid;
uid_t ar_arg_suid;
gid_t ar_arg_egid;
gid_t ar_arg_rgid;
gid_t ar_arg_sgid;
pid_t ar_arg_pid;
pid_t ar_arg_asid;
struct au_tid ar_arg_termid;
struct au_tid_addr ar_arg_termid_addr;
uid_t ar_arg_uid;
uid_t ar_arg_auid;
gid_t ar_arg_gid;
struct groupset ar_arg_groups;
int ar_arg_fd;
int ar_arg_atfd1;
int ar_arg_atfd2;
int ar_arg_fflags;
mode_t ar_arg_mode;
int ar_arg_dev;
long ar_arg_value;
void *ar_arg_addr;
int ar_arg_len;
int ar_arg_mask;
u_int ar_arg_signum;
char ar_arg_login[MAXLOGNAME];
int ar_arg_ctlname[CTL_MAXNAME];
struct socket_au_info ar_arg_sockinfo;
char *ar_arg_upath1;
char *ar_arg_upath2;
char *ar_arg_text;
struct au_mask ar_arg_amask;
struct vnode_au_info ar_arg_vnode1;
struct vnode_au_info ar_arg_vnode2;
int ar_arg_cmd;
int ar_arg_svipc_cmd;
struct ipc_perm ar_arg_svipc_perm;
int ar_arg_svipc_id;
void *ar_arg_svipc_addr;
struct posix_ipc_perm ar_arg_pipc_perm;
union auditon_udata ar_arg_auditon;
char *ar_arg_argv;
int ar_arg_argc;
char *ar_arg_envv;
int ar_arg_envc;
int ar_arg_exitstatus;
int ar_arg_exitretval;
struct sockaddr_storage ar_arg_sockaddr;
cap_rights_t ar_arg_rights;
uint32_t ar_arg_fcntl_rights;
char ar_jailname[MAXHOSTNAMELEN];
};
/*
* Arguments in the audit record are initially not defined; flags are set to
* indicate if they are present so they can be included in the audit log
* stream only if defined.
*/
#define ARG_EUID 0x0000000000000001ULL
#define ARG_RUID 0x0000000000000002ULL
#define ARG_SUID 0x0000000000000004ULL
#define ARG_EGID 0x0000000000000008ULL
#define ARG_RGID 0x0000000000000010ULL
#define ARG_SGID 0x0000000000000020ULL
#define ARG_PID 0x0000000000000040ULL
#define ARG_UID 0x0000000000000080ULL
#define ARG_AUID 0x0000000000000100ULL
#define ARG_GID 0x0000000000000200ULL
#define ARG_FD 0x0000000000000400ULL
#define ARG_POSIX_IPC_PERM 0x0000000000000800ULL
#define ARG_FFLAGS 0x0000000000001000ULL
#define ARG_MODE 0x0000000000002000ULL
#define ARG_DEV 0x0000000000004000ULL
#define ARG_ADDR 0x0000000000008000ULL
#define ARG_LEN 0x0000000000010000ULL
#define ARG_MASK 0x0000000000020000ULL
#define ARG_SIGNUM 0x0000000000040000ULL
#define ARG_LOGIN 0x0000000000080000ULL
#define ARG_SADDRINET 0x0000000000100000ULL
#define ARG_SADDRINET6 0x0000000000200000ULL
#define ARG_SADDRUNIX 0x0000000000400000ULL
#define ARG_TERMID_ADDR 0x0000000000400000ULL
#define ARG_UNUSED2 0x0000000001000000ULL
#define ARG_UPATH1 0x0000000002000000ULL
#define ARG_UPATH2 0x0000000004000000ULL
#define ARG_TEXT 0x0000000008000000ULL
#define ARG_VNODE1 0x0000000010000000ULL
#define ARG_VNODE2 0x0000000020000000ULL
#define ARG_SVIPC_CMD 0x0000000040000000ULL
#define ARG_SVIPC_PERM 0x0000000080000000ULL
#define ARG_SVIPC_ID 0x0000000100000000ULL
#define ARG_SVIPC_ADDR 0x0000000200000000ULL
#define ARG_GROUPSET 0x0000000400000000ULL
#define ARG_CMD 0x0000000800000000ULL
#define ARG_SOCKINFO 0x0000001000000000ULL
#define ARG_ASID 0x0000002000000000ULL
#define ARG_TERMID 0x0000004000000000ULL
#define ARG_AUDITON 0x0000008000000000ULL
#define ARG_VALUE 0x0000010000000000ULL
#define ARG_AMASK 0x0000020000000000ULL
#define ARG_CTLNAME 0x0000040000000000ULL
#define ARG_PROCESS 0x0000080000000000ULL
#define ARG_MACHPORT1 0x0000100000000000ULL
#define ARG_MACHPORT2 0x0000200000000000ULL
#define ARG_EXIT 0x0000400000000000ULL
#define ARG_IOVECSTR 0x0000800000000000ULL
#define ARG_ARGV 0x0001000000000000ULL
#define ARG_ENVV 0x0002000000000000ULL
#define ARG_ATFD1 0x0004000000000000ULL
#define ARG_ATFD2 0x0008000000000000ULL
#define ARG_RIGHTS 0x0010000000000000ULL
#define ARG_FCNTL_RIGHTS 0x0020000000000000ULL
#define ARG_NONE 0x0000000000000000ULL
#define ARG_ALL 0xFFFFFFFFFFFFFFFFULL
#define ARG_IS_VALID(kar, arg) ((kar)->k_ar.ar_valid_arg & (arg))
#define ARG_SET_VALID(kar, arg) do { \
(kar)->k_ar.ar_valid_arg |= (arg); \
} while (0)
#define ARG_CLEAR_VALID(kar, arg) do { \
(kar)->k_ar.ar_valid_arg &= ~(arg); \
} while (0)
/*
* In-kernel version of audit record; the basic record plus queue meta-data.
* This record can also have a pointer set to some opaque data that will be
* passed through to the audit writing mechanism.
*/
struct kaudit_record {
struct audit_record k_ar;
u_int32_t k_ar_commit;
void *k_udata; /* User data. */
u_int k_ulen; /* User data length. */
struct uthread *k_uthread; /* Audited thread. */
TAILQ_ENTRY(kaudit_record) k_q;
};
TAILQ_HEAD(kaudit_queue, kaudit_record);
/*
* Functions to manage the allocation, release, and commit of kernel audit
* records.
*/
void audit_abort(struct kaudit_record *ar);
void audit_commit(struct kaudit_record *ar, int error,
int retval);
struct kaudit_record *audit_new(int event, struct thread *td);
/*
* Functions relating to the conversion of internal kernel audit records to
* the BSM file format.
*/
struct au_record;
int kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
int bsm_rec_verify(void *rec);
/*
* Kernel versions of the libbsm audit record functions.
*/
void kau_free(struct au_record *rec);
void kau_init(void);
/*
* Return values for pre-selection and post-selection decisions.
*/
#define AU_PRS_SUCCESS 1
#define AU_PRS_FAILURE 2
#define AU_PRS_BOTH (AU_PRS_SUCCESS|AU_PRS_FAILURE)
/*
* Data structures relating to the kernel audit queue. Ideally, these might
* be abstracted so that only accessor methods are exposed.
*/
extern struct mtx audit_mtx;
extern struct cv audit_watermark_cv;
extern struct cv audit_worker_cv;
extern struct kaudit_queue audit_q;
extern int audit_q_len;
extern int audit_pre_q_len;
extern int audit_in_failure;
/*
* Flags to use on audit files when opening and closing.
*/
#define AUDIT_OPEN_FLAGS (FWRITE | O_APPEND)
#define AUDIT_CLOSE_FLAGS (FWRITE | O_APPEND)
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
/*
* Some of the BSM tokenizer functions take different parameters in the
* kernel implementations in order to save the copying of large kernel data
* structures. The prototypes of these functions are declared here.
*/
token_t *kau_to_socket(struct socket_au_info *soi);
/*
* audit_klib prototypes
*/
int au_preselect(au_event_t event, au_class_t class,
au_mask_t *mask_p, int sorf);
void au_evclassmap_init(void);
void au_evclassmap_insert(au_event_t event, au_class_t class);
au_class_t au_event_class(au_event_t event);
au_event_t audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
au_event_t audit_flags_and_error_to_openevent(int oflags, int error);
au_event_t audit_flags_and_error_to_openatevent(int oflags, int error);
au_event_t audit_msgctl_to_event(int cmd);
au_event_t audit_semctl_to_event(int cmr);
void audit_canon_path(struct thread *td, int dirfd, char *path,
char *cpath);
au_event_t auditon_command_event(int cmd);
/*
* Audit trigger events notify user space of kernel audit conditions
* asynchronously.
*/
void audit_trigger_init(void);
int audit_send_trigger(unsigned int trigger);
/*
* Accessor functions to manage global audit state.
*/
void audit_set_kinfo(struct auditinfo_addr *);
void audit_get_kinfo(struct auditinfo_addr *);
/*
* General audit related functions.
*/
struct kaudit_record *currecord(void);
void audit_free(struct kaudit_record *ar);
void audit_shutdown(void *arg, int howto);
void audit_rotate_vnode(struct ucred *cred,
struct vnode *vp);
void audit_worker_init(void);
/*
* Audit pipe functions.
*/
int audit_pipe_preselect(au_id_t auid, au_event_t event,
au_class_t class, int sorf, int trail_select);
void audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
int sorf, int trail_select, void *record, u_int record_len);
void audit_pipe_submit_user(void *record, u_int record_len);
#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */