Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * Access vector cache interface for object managers.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Author : Eamon Walsh <ewalsh@epoch.ncsc.mil>
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
#ifndef _SELINUX_AVC_H_
Chris PeBenito 473ea7
#define _SELINUX_AVC_H_
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#include <sys/types.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <selinux/selinux.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#ifdef __cplusplus
Chris PeBenito 473ea7
extern "C"
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
#endif
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * SID format and operations
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
struct security_id {
Chris PeBenito 473ea7
  security_context_t ctx;
Chris PeBenito 473ea7
  unsigned int refcnt;
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
typedef struct security_id *security_id_t;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#define SECSID_WILD (security_id_t)NULL /* unspecified SID */
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_sid_to_context - get copy of context corresponding to SID.
Chris PeBenito 473ea7
 * @sid: input SID
Chris PeBenito 473ea7
 * @ctx: pointer to context reference
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Return a copy of the security context corresponding to the input
Chris PeBenito 473ea7
 * @sid in the memory referenced by @ctx.  The caller is expected to 
Chris PeBenito 473ea7
 * free the context with freecon().  Return %0 on success, -%1 on
Chris PeBenito 473ea7
 * failure, with @errno set to %ENOMEM if insufficient memory was
Chris PeBenito 473ea7
 * available to make the copy, or %EINVAL if the input SID is invalid.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_sid_to_context(security_id_t sid, security_context_t *ctx);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_context_to_sid - get SID for context.
Chris PeBenito 473ea7
 * @ctx: input security context
Chris PeBenito 473ea7
 * @sid: pointer to SID reference
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Look up security context @ctx in SID table, making
Chris PeBenito 473ea7
 * a new entry if @ctx is not found.  Increment the
Chris PeBenito 473ea7
 * reference counter for the SID.  Store a pointer
Chris PeBenito 473ea7
 * to the SID structure into the memory referenced by @sid, 
Chris PeBenito 473ea7
 * returning %0 on success or -%1 on error with @errno set.  
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_context_to_sid(security_context_t ctx, security_id_t *sid);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * sidget - increment SID reference counter.
Chris PeBenito 473ea7
 * @sid: SID reference
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Increment the reference counter for @sid, indicating that
Chris PeBenito 473ea7
 * @sid is in use by an (additional) object.  Return the
Chris PeBenito 473ea7
 * new reference count, or zero if @sid is invalid (has zero
Chris PeBenito 473ea7
 * reference count).  Note that avc_context_to_sid() also
Chris PeBenito 473ea7
 * increments reference counts.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int sidget(security_id_t sid);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * sidput - decrement SID reference counter.
Chris PeBenito 473ea7
 * @sid: SID reference
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Decrement the reference counter for @sid, indicating that
Chris PeBenito 473ea7
 * a reference to @sid is no longer in use.  Return the 
Chris PeBenito 473ea7
 * new reference count.  When the reference count reaches
Chris PeBenito 473ea7
 * zero, the SID is invalid, and avc_context_to_sid() must
Chris PeBenito 473ea7
 * be called to obtain a new SID for the security context.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int sidput(security_id_t sid);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * AVC entry
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
struct avc_entry;
Chris PeBenito 473ea7
struct avc_entry_ref {
Chris PeBenito 473ea7
	struct avc_entry *ae;
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_entry_ref_init - initialize an AVC entry reference.
Chris PeBenito 473ea7
 * @aeref: pointer to avc entry reference structure
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Use this macro to initialize an avc entry reference structure
Chris PeBenito 473ea7
 * before first use.  These structures are passed to avc_has_perm(),
Chris PeBenito 473ea7
 * which stores cache entry references in them.  They can increase
Chris PeBenito 473ea7
 * performance on repeated queries.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
#define avc_entry_ref_init(aeref) ((aeref)->ae = NULL)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * User-provided callbacks for memory, auditing, and locking
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/* These structures are passed by reference to avc_init().  Passing
Chris PeBenito 473ea7
 * a NULL reference will cause the AVC to use a default.  The default
Chris PeBenito 473ea7
 * memory callbacks are malloc() and free().  The default logging method
Chris PeBenito 473ea7
 * is to print on stderr.  If no thread callbacks are passed, a separate
Chris PeBenito 473ea7
 * listening thread won't be started for kernel policy change messages.
Chris PeBenito 473ea7
 * If no locking callbacks are passed, no locking will take place.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
struct avc_memory_callback {
Chris PeBenito 473ea7
  /* malloc() equivalent. */
Chris PeBenito 473ea7
  void *(*func_malloc)(size_t size);
Chris PeBenito 473ea7
  /* free() equivalent. */
Chris PeBenito 473ea7
  void  (*func_free)  (void *ptr);
Chris PeBenito 473ea7
  /* Note that these functions should set errno on failure.
Chris PeBenito 473ea7
     If not, some avc routines may return -1 without errno set. */
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
struct avc_log_callback {
Chris PeBenito 473ea7
  /* log the printf-style format and arguments. */
Chris PeBenito 473ea7
  void (*func_log)(const char *fmt, ...);
Chris PeBenito 473ea7
  /* store a string representation of auditdata (corresponding
Chris PeBenito 473ea7
     to the given security class) into msgbuf. */
Chris PeBenito 473ea7
  void (*func_audit)(void *auditdata, security_class_t class,
Chris PeBenito 473ea7
		     char *msgbuf, size_t msgbufsize);
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
struct avc_thread_callback {
Chris PeBenito 473ea7
  /* create and start a thread, returning an opaque pointer to it; 
Chris PeBenito 473ea7
     the thread should run the given function. */
Chris PeBenito 473ea7
  void *(*func_create_thread)(void (*run)(void));
Chris PeBenito 473ea7
  /* cancel a given thread and free its resources. */
Chris PeBenito 473ea7
  void  (*func_stop_thread)(void *thread);
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
struct avc_lock_callback {
Chris PeBenito 473ea7
  /* create a lock and return an opaque pointer to it. */
Chris PeBenito 473ea7
  void *(*func_alloc_lock)(void);
Chris PeBenito 473ea7
  /* obtain a given lock, blocking if necessary. */
Chris PeBenito 473ea7
  void  (*func_get_lock)(void *lock);
Chris PeBenito 473ea7
  /* release a given lock. */
Chris PeBenito 473ea7
  void  (*func_release_lock)(void *lock);
Chris PeBenito 473ea7
  /* destroy a given lock (free memory, etc.) */
Chris PeBenito 473ea7
  void  (*func_free_lock)(void *lock);
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * AVC operations
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_init - Initialize the AVC.
Chris PeBenito 473ea7
 * @msgprefix: prefix for log messages
Chris PeBenito 473ea7
 * @mem_callbacks: user-supplied memory callbacks
Chris PeBenito 473ea7
 * @log_callbacks: user-supplied logging callbacks
Chris PeBenito 473ea7
 * @thread_callbacks: user-supplied threading callbacks
Chris PeBenito 473ea7
 * @lock_callbacks: user-supplied locking callbacks
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Initialize the access vector cache.  Return %0 on
Chris PeBenito 473ea7
 * success or -%1 with @errno set on failure.  
Chris PeBenito 473ea7
 * If @msgprefix is NULL, use "uavc".  If any callback 
Chris PeBenito 473ea7
 * structure references are NULL, use default methods 
Chris PeBenito 473ea7
 * for those callbacks (see the definition of the callback
Chris PeBenito 473ea7
 * structures above).
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_init(const char *msgprefix,
Chris PeBenito 473ea7
	     const struct avc_memory_callback *mem_callbacks,
Chris PeBenito 473ea7
	     const struct avc_log_callback *log_callbacks,
Chris PeBenito 473ea7
	     const struct avc_thread_callback *thread_callbacks,
Chris PeBenito 473ea7
	     const struct avc_lock_callback *lock_callbacks);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_cleanup - Remove unused SIDs and AVC entries.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Search the SID table for SID structures with zero
Chris PeBenito 473ea7
 * reference counts, and remove them along with all
Chris PeBenito 473ea7
 * AVC entries that reference them.  This can be used
Chris PeBenito 473ea7
 * to return memory to the system.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_cleanup(void);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_reset - Flush the cache and reset statistics.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Remove all entries from the cache and reset all access
Chris PeBenito 473ea7
 * statistics (as returned by avc_cache_stats()) to zero.
Chris PeBenito 473ea7
 * The SID mapping is not affected.  Return %0 on success, 
Chris PeBenito 473ea7
 * -%1 with @errno set on error.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_reset(void);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_destroy - Free all AVC structures.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Destroy all AVC structures and free all allocated
Chris PeBenito 473ea7
 * memory.  User-supplied locking, memory, and audit
Chris PeBenito 473ea7
 * callbacks will be retained, but security-event
Chris PeBenito 473ea7
 * callbacks will not.  All SID's will be invalidated.
Chris PeBenito 473ea7
 * User must call avc_init() if further use of AVC is desired.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_destroy(void);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_has_perm_noaudit - Check permissions but perform no auditing.
Chris PeBenito 473ea7
 * @ssid: source security identifier
Chris PeBenito 473ea7
 * @tsid: target security identifier
Chris PeBenito 473ea7
 * @tclass: target security class
Chris PeBenito 473ea7
 * @requested: requested permissions, interpreted based on @tclass
Chris PeBenito 473ea7
 * @aeref:  AVC entry reference
Chris PeBenito 473ea7
 * @avd: access vector decisions
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Check the AVC to determine whether the @requested permissions are granted
Chris PeBenito 473ea7
 * for the SID pair (@ssid, @tsid), interpreting the permissions
Chris PeBenito 473ea7
 * based on @tclass, and call the security server on a cache miss to obtain
Chris PeBenito 473ea7
 * a new decision and add it to the cache.  Update @aeref to refer to an AVC
Chris PeBenito 473ea7
 * entry with the resulting decisions, and return a copy of the decisions
Chris PeBenito 473ea7
 * in @avd.  Return %0 if all @requested permissions are granted, -%1 with
Chris PeBenito 473ea7
 * @errno set to %EACCES if any permissions are denied, or to another value
Chris PeBenito 473ea7
 * upon other errors.  This function is typically called by avc_has_perm(),
Chris PeBenito 473ea7
 * but may also be called directly to separate permission checking from
Chris PeBenito 473ea7
 * auditing, e.g. in cases where a lock must be held for the check but
Chris PeBenito 473ea7
 * should be released for the auditing.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_has_perm_noaudit(security_id_t ssid, 
Chris PeBenito 473ea7
			    security_id_t tsid,
Chris PeBenito 473ea7
			    security_class_t tclass,
Chris PeBenito 473ea7
			    access_vector_t requested,
Chris PeBenito 473ea7
			 struct avc_entry_ref *aeref,
Chris PeBenito 473ea7
			    struct av_decision *avd);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_has_perm - Check permissions and perform any appropriate auditing.
Chris PeBenito 473ea7
 * @ssid: source security identifier
Chris PeBenito 473ea7
 * @tsid: target security identifier
Chris PeBenito 473ea7
 * @tclass: target security class
Chris PeBenito 473ea7
 * @requested: requested permissions, interpreted based on @tclass
Chris PeBenito 473ea7
 * @aeref:  AVC entry reference
Chris PeBenito 473ea7
 * @auditdata: auxiliary audit data
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Check the AVC to determine whether the @requested permissions are granted
Chris PeBenito 473ea7
 * for the SID pair (@ssid, @tsid), interpreting the permissions
Chris PeBenito 473ea7
 * based on @tclass, and call the security server on a cache miss to obtain
Chris PeBenito 473ea7
 * a new decision and add it to the cache.  Update @aeref to refer to an AVC
Chris PeBenito 473ea7
 * entry with the resulting decisions.  Audit the granting or denial of
Chris PeBenito 473ea7
 * permissions in accordance with the policy.  Return %0 if all @requested
Chris PeBenito 473ea7
 * permissions are granted, -%1 with @errno set to %EACCES if any permissions
Chris PeBenito 473ea7
 * are denied or to another value upon other errors.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_has_perm(security_id_t ssid, security_id_t tsid,
Chris PeBenito 473ea7
		    security_class_t tclass, access_vector_t requested,
Chris PeBenito 473ea7
		    struct avc_entry_ref *aeref, void *auditdata);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_audit - Audit the granting or denial of permissions.
Chris PeBenito 473ea7
 * @ssid: source security identifier
Chris PeBenito 473ea7
 * @tsid: target security identifier
Chris PeBenito 473ea7
 * @tclass: target security class
Chris PeBenito 473ea7
 * @requested: requested permissions
Chris PeBenito 473ea7
 * @avd: access vector decisions
Chris PeBenito 473ea7
 * @result: result from avc_has_perm_noaudit
Chris PeBenito 473ea7
 * @auditdata:  auxiliary audit data
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Audit the granting or denial of permissions in accordance
Chris PeBenito 473ea7
 * with the policy.  This function is typically called by
Chris PeBenito 473ea7
 * avc_has_perm() after a permission check, but can also be
Chris PeBenito 473ea7
 * called directly by callers who use avc_has_perm_noaudit()
Chris PeBenito 473ea7
 * in order to separate the permission check from the auditing.
Chris PeBenito 473ea7
 * For example, this separation is useful when the permission check must
Chris PeBenito 473ea7
 * be performed under a lock, to allow the lock to be released
Chris PeBenito 473ea7
 * before calling the auditing code.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_audit(security_id_t ssid, security_id_t tsid,
Chris PeBenito 473ea7
	       security_class_t tclass, access_vector_t requested,
Chris PeBenito 473ea7
	       struct av_decision *avd, int result, void *auditdata);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/* 
Chris PeBenito 473ea7
 * security event callback facility
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/* security events */
Chris PeBenito 473ea7
#define AVC_CALLBACK_GRANT		1
Chris PeBenito 473ea7
#define AVC_CALLBACK_TRY_REVOKE		2
Chris PeBenito 473ea7
#define AVC_CALLBACK_REVOKE		4
Chris PeBenito 473ea7
#define AVC_CALLBACK_RESET		8
Chris PeBenito 473ea7
#define AVC_CALLBACK_AUDITALLOW_ENABLE	16
Chris PeBenito 473ea7
#define AVC_CALLBACK_AUDITALLOW_DISABLE	32
Chris PeBenito 473ea7
#define AVC_CALLBACK_AUDITDENY_ENABLE	64
Chris PeBenito 473ea7
#define AVC_CALLBACK_AUDITDENY_DISABLE	128
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_add_callback - Register a callback for security events.
Chris PeBenito 473ea7
 * @callback: callback function
Chris PeBenito 473ea7
 * @events: bitwise OR of desired security events
Chris PeBenito 473ea7
 * @ssid: source security identifier or %SECSID_WILD
Chris PeBenito 473ea7
 * @tsid: target security identifier or %SECSID_WILD
Chris PeBenito 473ea7
 * @tclass: target security class
Chris PeBenito 473ea7
 * @perms: permissions
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Register a callback function for events in the set @events
Chris PeBenito 473ea7
 * related to the SID pair (@ssid, @tsid) and
Chris PeBenito 473ea7
 * and the permissions @perms, interpreting
Chris PeBenito 473ea7
 * @perms based on @tclass.  Returns %0 on success or
Chris PeBenito 473ea7
 * -%1 if insufficient memory exists to add the callback.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
int avc_add_callback(int (*callback)(u_int32_t event, security_id_t ssid,
Chris PeBenito 473ea7
				     security_id_t tsid,
Chris PeBenito 473ea7
                                     security_class_t tclass,
Chris PeBenito 473ea7
				     access_vector_t perms,
Chris PeBenito 473ea7
				     access_vector_t *out_retained),
Chris PeBenito 473ea7
		     u_int32_t events, security_id_t ssid,
Chris PeBenito 473ea7
		     security_id_t tsid,
Chris PeBenito 473ea7
		     security_class_t tclass, access_vector_t perms);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/*
Chris PeBenito 473ea7
 * AVC statistics 
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/* If set, cache statistics are tracked.  This may
Chris PeBenito 473ea7
 * become a compile-time option in the future.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
#define AVC_CACHE_STATS     1
Chris PeBenito 473ea7
Chris PeBenito 473ea7
struct avc_cache_stats {
Chris PeBenito 473ea7
  unsigned entry_lookups;
Chris PeBenito 473ea7
  unsigned entry_hits;
Chris PeBenito 473ea7
  unsigned entry_misses;
Chris PeBenito 473ea7
  unsigned entry_discards;
Chris PeBenito 473ea7
  unsigned cav_lookups;
Chris PeBenito 473ea7
  unsigned cav_hits;
Chris PeBenito 473ea7
  unsigned cav_probes;
Chris PeBenito 473ea7
  unsigned cav_misses;
Chris PeBenito 473ea7
};
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_cache_stats - get cache access statistics.
Chris PeBenito 473ea7
 * @stats: reference to statistics structure
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Fill the supplied structure with information about AVC 
Chris PeBenito 473ea7
 * activity since the last call to avc_init() or
Chris PeBenito 473ea7
 * avc_reset().  See the structure definition for
Chris PeBenito 473ea7
 * details.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_cache_stats(struct avc_cache_stats *stats);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_av_stats - log av table statistics.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Log a message with information about the size and
Chris PeBenito 473ea7
 * distribution of the access vector table.  The audit
Chris PeBenito 473ea7
 * callback is used to print the message.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_av_stats(void);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/**
Chris PeBenito 473ea7
 * avc_sid_stats - log SID table statistics.
Chris PeBenito 473ea7
 *
Chris PeBenito 473ea7
 * Log a message with information about the size and
Chris PeBenito 473ea7
 * distribution of the SID table.  The audit callback
Chris PeBenito 473ea7
 * is used to print the message.
Chris PeBenito 473ea7
 */
Chris PeBenito 473ea7
void avc_sid_stats(void);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#ifdef __cplusplus
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
#endif
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#endif /* _SELINUX_AVC_H_ */