19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#ifndef _SELINUX_H_
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#define _SELINUX_H_
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include <sys/types.h>
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include <stdarg.h>
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#ifdef __cplusplus
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern "C" {
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#endif
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Return 1 if we are running on a SELinux kernel, or 0 if not or -1 if we get an error. */
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int is_selinux_enabled(void);
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Return 1 if we are running on a SELinux MLS kernel, or 0 otherwise. */
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int is_selinux_mls_enabled(void);
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* No longer used; here for compatibility with legacy callers. */
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsontypedef char *security_context_t;
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Free the memory allocated for a context by any of the below get* calls. */
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern void freecon(char * con);
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Free the memory allocated for a context array by security_compute_user. */
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern void freeconary(char ** con);
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Wrappers for the /proc/pid/attr API. */
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get current context, and set *con to refer to it.
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Caller must free via freecon. */
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getcon(char ** con);
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set the current security context to con.
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Note that use of this function requires that the entire application
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   be trusted to maintain any desired separation between the old and new
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   security contexts, unlike exec-based transitions performed via setexeccon.
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   When possible, decompose your application and use setexeccon()+execve()
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   instead. Note that the application may lose access to its open descriptors
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   as a result of a setcon() unless policy allows it to use descriptors opened
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   by the old context. */
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setcon(const char * con);
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get context of process identified by pid, and
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   set *con to refer to it.  Caller must free via freecon. */
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getpidcon(pid_t pid, char ** con);
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get previous context (prior to last exec), and set *con to refer to it.
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Caller must free via freecon. */
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getprevcon(char ** con);
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get exec context, and set *con to refer to it.
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Sets *con to NULL if no exec context has been set, i.e. using default.
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   If non-NULL, caller must free via freecon. */
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getexeccon(char ** con);
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set exec security context for the next execve.
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Call with NULL if you want to reset to the default. */
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setexeccon(const char * con);
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get fscreate context, and set *con to refer to it.
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Sets *con to NULL if no fs create context has been set, i.e. using default.
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   If non-NULL, caller must free via freecon. */
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getfscreatecon(char ** con);
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set the fscreate security context for subsequent file creations.
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Call with NULL if you want to reset to the default. */
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setfscreatecon(const char * context);
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get keycreate context, and set *con to refer to it.
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Sets *con to NULL if no key create context has been set, i.e. using default.
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   If non-NULL, caller must free via freecon. */
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getkeycreatecon(char ** con);
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set the keycreate security context for subsequent key creations.
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Call with NULL if you want to reset to the default. */
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setkeycreatecon(const char * context);
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get sockcreate context, and set *con to refer to it.
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Sets *con to NULL if no socket create context has been set, i.e. using default.
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   If non-NULL, caller must free via freecon. */
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getsockcreatecon(char ** con);
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set the sockcreate security context for subsequent socket creations.
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Call with NULL if you want to reset to the default. */
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setsockcreatecon(const char * context);
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Wrappers for the xattr API. */
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get file context, and set *con to refer to it.
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Caller must free via freecon. */
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getfilecon(const char *path, char ** con);
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int lgetfilecon(const char *path, char ** con);
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int fgetfilecon(int fd, char ** con);
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Set file context */
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int setfilecon(const char *path, const char *con);
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int lsetfilecon(const char *path, const char *con);
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int fsetfilecon(int fd, const char *con);
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Wrappers for the socket API */
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Get context of peer socket, and set *con to refer to it.
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   Caller must free via freecon. */
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonextern int getpeercon(int fd, char ** con);
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* Wrappers for the selinuxfs (policy) API. */
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
106typedef unsigned int access_vector_t;
107typedef unsigned short security_class_t;
108
109struct av_decision {
110	access_vector_t allowed;
111	access_vector_t decided;
112	access_vector_t auditallow;
113	access_vector_t auditdeny;
114	unsigned int seqno;
115	unsigned int flags;
116};
117
118/* Definitions of av_decision.flags */
119#define SELINUX_AVD_FLAGS_PERMISSIVE	0x0001
120
121/* Structure for passing options, used by AVC and label subsystems */
122struct selinux_opt {
123	int type;
124	const char *value;
125};
126
127/* Callback facilities */
128union selinux_callback {
129	/* log the printf-style format and arguments,
130	   with the type code indicating the type of message */
131	int
132#ifdef __GNUC__
133__attribute__ ((format(printf, 2, 3)))
134#endif
135	(*func_log) (int type, const char *fmt, ...);
136	/* store a string representation of auditdata (corresponding
137	   to the given security class) into msgbuf. */
138	int (*func_audit) (void *auditdata, security_class_t cls,
139			   char *msgbuf, size_t msgbufsize);
140	/* validate the supplied context, modifying if necessary */
141	int (*func_validate) (char **ctx);
142	/* netlink callback for setenforce message */
143	int (*func_setenforce) (int enforcing);
144	/* netlink callback for policyload message */
145	int (*func_policyload) (int seqno);
146};
147
148#define SELINUX_CB_LOG		0
149#define SELINUX_CB_AUDIT	1
150#define SELINUX_CB_VALIDATE	2
151#define SELINUX_CB_SETENFORCE	3
152#define SELINUX_CB_POLICYLOAD	4
153
154extern union selinux_callback selinux_get_callback(int type);
155extern void selinux_set_callback(int type, union selinux_callback cb);
156
157	/* Logging type codes, passed to the logging callback */
158#define SELINUX_ERROR	        0
159#define SELINUX_WARNING		1
160#define SELINUX_INFO		2
161#define SELINUX_AVC		3
162
163/* Compute an access decision. */
164extern int security_compute_av(const char * scon,
165			       const char * tcon,
166			       security_class_t tclass,
167			       access_vector_t requested,
168			       struct av_decision *avd);
169
170/* Compute a labeling decision and set *newcon to refer to it.
171   Caller must free via freecon. */
172extern int security_compute_create(const char * scon,
173				   const char * tcon,
174				   security_class_t tclass,
175				   char ** newcon);
176
177/* Compute a relabeling decision and set *newcon to refer to it.
178   Caller must free via freecon. */
179extern int security_compute_relabel(const char * scon,
180				    const char * tcon,
181				    security_class_t tclass,
182				    char ** newcon);
183
184/* Compute a polyinstantiation member decision and set *newcon to refer to it.
185   Caller must free via freecon. */
186extern int security_compute_member(const char * scon,
187				   const char * tcon,
188				   security_class_t tclass,
189				   char ** newcon);
190
191/* Compute the set of reachable user contexts and set *con to refer to
192   the NULL-terminated array of contexts.  Caller must free via freeconary. */
193extern int security_compute_user(const char * scon,
194				 const char *username,
195				 char *** con);
196
197/* Load a policy configuration. */
198extern int security_load_policy(void *data, size_t len);
199
200/* Get the context of an initial kernel security identifier by name.
201   Caller must free via freecon */
202extern int security_get_initial_context(const char *name,
203					char ** con);
204
205/* Translate boolean strict to name value pair. */
206typedef struct {
207	const char *name;
208	int value;
209} SELboolean;
210/* save a list of booleans in a single transaction.  */
211extern int security_set_boolean_list(size_t boolcnt,
212				     SELboolean * const boollist, int permanent);
213
214/* Check the validity of a security context. */
215extern int security_check_context(const char * con);
216
217/* Canonicalize a security context. */
218extern int security_canonicalize_context(const char * con,
219					 char ** canoncon);
220
221/* Get the enforce flag value. */
222extern int security_getenforce(void);
223
224/* Set the enforce flag value. */
225extern int security_setenforce(int value);
226
227/* Get the behavior for undefined classes/permissions */
228extern int security_deny_unknown(void);
229
230/* Disable SELinux at runtime (must be done prior to initial policy load). */
231extern int security_disable(void);
232
233/* Get the policy version number. */
234extern int security_policyvers(void);
235
236/* Get the boolean names */
237extern int security_get_boolean_names(char ***names, int *len);
238
239/* Get the pending value for the boolean */
240extern int security_get_boolean_pending(const char *name);
241
242/* Get the active value for the boolean */
243extern int security_get_boolean_active(const char *name);
244
245/* Set the pending value for the boolean */
246extern int security_set_boolean(const char *name, int value);
247
248/* Commit the pending values for the booleans */
249extern int security_commit_booleans(void);
250
251/* Userspace class mapping support */
252struct security_class_mapping {
253	const char *name;
254	const char *perms[sizeof(access_vector_t) * 8 + 1];
255};
256
257extern int selinux_set_mapping(struct security_class_mapping *map);
258
259/* Common helpers */
260
261/* Convert between security class values and string names */
262extern security_class_t string_to_security_class(const char *name);
263extern const char *security_class_to_string(security_class_t cls);
264
265/* Convert between individual access vector permissions and string names */
266extern const char *security_av_perm_to_string(security_class_t tclass,
267					      access_vector_t perm);
268extern access_vector_t string_to_av_perm(security_class_t tclass,
269					 const char *name);
270
271/* Returns an access vector in a string representation.  User must free the
272 * returned string via free(). */
273extern int security_av_string(security_class_t tclass,
274			      access_vector_t av, char **result);
275
276/* Check permissions and perform appropriate auditing. */
277extern int selinux_check_access(const char * scon,
278				const char * tcon,
279				const char *tclass,
280				const char *perm, void *aux);
281
282/* Set the path to the selinuxfs mount point explicitly.
283   Normally, this is determined automatically during libselinux
284   initialization, but this is not always possible, e.g. for /sbin/init
285   which performs the initial mount of selinuxfs. */
286void set_selinuxmnt(const char *mnt);
287
288#ifdef __cplusplus
289}
290#endif
291#endif
292