xref: /aosp_15_r20/external/selinux/libsepol/include/sepol/policydb/services.h (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker 
2*2d543d20SAndroid Build Coastguard Worker /* -*- linux-c -*- */
3*2d543d20SAndroid Build Coastguard Worker 
4*2d543d20SAndroid Build Coastguard Worker /*
5*2d543d20SAndroid Build Coastguard Worker  * Author : Stephen Smalley, <[email protected]>
6*2d543d20SAndroid Build Coastguard Worker  */
7*2d543d20SAndroid Build Coastguard Worker 
8*2d543d20SAndroid Build Coastguard Worker #ifndef _SEPOL_POLICYDB_SERVICES_H_
9*2d543d20SAndroid Build Coastguard Worker #define _SEPOL_POLICYDB_SERVICES_H_
10*2d543d20SAndroid Build Coastguard Worker 
11*2d543d20SAndroid Build Coastguard Worker /*
12*2d543d20SAndroid Build Coastguard Worker  * Security server interface.
13*2d543d20SAndroid Build Coastguard Worker  */
14*2d543d20SAndroid Build Coastguard Worker 
15*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/flask_types.h>
16*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
17*2d543d20SAndroid Build Coastguard Worker #include <stddef.h>
18*2d543d20SAndroid Build Coastguard Worker 
19*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus
20*2d543d20SAndroid Build Coastguard Worker extern "C" {
21*2d543d20SAndroid Build Coastguard Worker #endif
22*2d543d20SAndroid Build Coastguard Worker 
23*2d543d20SAndroid Build Coastguard Worker /* Set the policydb and sidtab structures to be used by
24*2d543d20SAndroid Build Coastguard Worker    the service functions.  If not set, then these default
25*2d543d20SAndroid Build Coastguard Worker    to private structures within libsepol that can only be
26*2d543d20SAndroid Build Coastguard Worker    initialized and accessed via the service functions themselves.
27*2d543d20SAndroid Build Coastguard Worker    Setting the structures explicitly allows a program to directly
28*2d543d20SAndroid Build Coastguard Worker    manipulate them, e.g. checkpolicy populates the structures directly
29*2d543d20SAndroid Build Coastguard Worker    from a source policy rather than from a binary policy. */
30*2d543d20SAndroid Build Coastguard Worker extern int sepol_set_policydb(policydb_t * p);
31*2d543d20SAndroid Build Coastguard Worker extern int sepol_set_sidtab(sidtab_t * s);
32*2d543d20SAndroid Build Coastguard Worker 
33*2d543d20SAndroid Build Coastguard Worker /* Load the security policy. This initializes the policydb
34*2d543d20SAndroid Build Coastguard Worker    and sidtab based on the provided binary policy. */
35*2d543d20SAndroid Build Coastguard Worker extern int sepol_load_policy(void *data, size_t len);
36*2d543d20SAndroid Build Coastguard Worker 
37*2d543d20SAndroid Build Coastguard Worker /*
38*2d543d20SAndroid Build Coastguard Worker  * Compute access vectors based on a SID pair for
39*2d543d20SAndroid Build Coastguard Worker  * the permissions in a particular class.
40*2d543d20SAndroid Build Coastguard Worker  */
41*2d543d20SAndroid Build Coastguard Worker extern int sepol_compute_av(sepol_security_id_t ssid,	/* IN */
42*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t tsid,	/* IN */
43*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_class_t tclass,	/* IN */
44*2d543d20SAndroid Build Coastguard Worker 			    sepol_access_vector_t requested,	/* IN */
45*2d543d20SAndroid Build Coastguard Worker 			    struct sepol_av_decision *avd);	/* OUT */
46*2d543d20SAndroid Build Coastguard Worker 
47*2d543d20SAndroid Build Coastguard Worker /* Same as above, but also return the reason(s) for any
48*2d543d20SAndroid Build Coastguard Worker    denials of the requested permissions. */
49*2d543d20SAndroid Build Coastguard Worker #define SEPOL_COMPUTEAV_TE     0x1U
50*2d543d20SAndroid Build Coastguard Worker #define SEPOL_COMPUTEAV_CONS   0x2U
51*2d543d20SAndroid Build Coastguard Worker #define SEPOL_COMPUTEAV_RBAC   0x4U
52*2d543d20SAndroid Build Coastguard Worker #define SEPOL_COMPUTEAV_BOUNDS 0x8U
53*2d543d20SAndroid Build Coastguard Worker extern int sepol_compute_av_reason(sepol_security_id_t ssid,
54*2d543d20SAndroid Build Coastguard Worker 				   sepol_security_id_t tsid,
55*2d543d20SAndroid Build Coastguard Worker 				   sepol_security_class_t tclass,
56*2d543d20SAndroid Build Coastguard Worker 				   sepol_access_vector_t requested,
57*2d543d20SAndroid Build Coastguard Worker 				   struct sepol_av_decision *avd,
58*2d543d20SAndroid Build Coastguard Worker 				   unsigned int *reason);
59*2d543d20SAndroid Build Coastguard Worker 
60*2d543d20SAndroid Build Coastguard Worker /*
61*2d543d20SAndroid Build Coastguard Worker  * Same as above, but also returns the constraint expression calculations
62*2d543d20SAndroid Build Coastguard Worker  * whether allowed or denied in a buffer. This buffer is allocated by
63*2d543d20SAndroid Build Coastguard Worker  * this call and must be free'd by the caller using free(3). The constraint
64*2d543d20SAndroid Build Coastguard Worker  * buffer will contain any constraints in infix notation.
65*2d543d20SAndroid Build Coastguard Worker  * If the SHOW_GRANTED flag is set it will show granted and denied
66*2d543d20SAndroid Build Coastguard Worker  * constraints. The default is to show only denied constraints.
67*2d543d20SAndroid Build Coastguard Worker  */
68*2d543d20SAndroid Build Coastguard Worker #define SHOW_GRANTED 1
69*2d543d20SAndroid Build Coastguard Worker extern int sepol_compute_av_reason_buffer(sepol_security_id_t ssid,
70*2d543d20SAndroid Build Coastguard Worker 				   sepol_security_id_t tsid,
71*2d543d20SAndroid Build Coastguard Worker 				   sepol_security_class_t tclass,
72*2d543d20SAndroid Build Coastguard Worker 				   sepol_access_vector_t requested,
73*2d543d20SAndroid Build Coastguard Worker 				   struct sepol_av_decision *avd,
74*2d543d20SAndroid Build Coastguard Worker 				   unsigned int *reason,
75*2d543d20SAndroid Build Coastguard Worker 				   char **reason_buf,
76*2d543d20SAndroid Build Coastguard Worker 				   unsigned int flags);
77*2d543d20SAndroid Build Coastguard Worker 
78*2d543d20SAndroid Build Coastguard Worker /*
79*2d543d20SAndroid Build Coastguard Worker  * Returns the mls/validatetrans constraint expression calculations in
80*2d543d20SAndroid Build Coastguard Worker  * a buffer that must be free'd by the caller using free(3).
81*2d543d20SAndroid Build Coastguard Worker  * If the SHOW_GRANTED flag is set it will show granted and denied
82*2d543d20SAndroid Build Coastguard Worker  * mls/validatetrans (the default is to show only those denied).
83*2d543d20SAndroid Build Coastguard Worker  */
84*2d543d20SAndroid Build Coastguard Worker extern int sepol_validate_transition_reason_buffer(sepol_security_id_t oldsid,
85*2d543d20SAndroid Build Coastguard Worker 					sepol_security_id_t newsid,
86*2d543d20SAndroid Build Coastguard Worker 					sepol_security_id_t tasksid,
87*2d543d20SAndroid Build Coastguard Worker 					sepol_security_class_t tclass,
88*2d543d20SAndroid Build Coastguard Worker 					char **reason_buf,
89*2d543d20SAndroid Build Coastguard Worker 					unsigned int flags);
90*2d543d20SAndroid Build Coastguard Worker 
91*2d543d20SAndroid Build Coastguard Worker /*
92*2d543d20SAndroid Build Coastguard Worker  * Return a class ID associated with the class string representation
93*2d543d20SAndroid Build Coastguard Worker  * specified by `class_name'.
94*2d543d20SAndroid Build Coastguard Worker  */
95*2d543d20SAndroid Build Coastguard Worker extern int sepol_string_to_security_class(const char *class_name,
96*2d543d20SAndroid Build Coastguard Worker 					sepol_security_class_t  *tclass);
97*2d543d20SAndroid Build Coastguard Worker 
98*2d543d20SAndroid Build Coastguard Worker /*
99*2d543d20SAndroid Build Coastguard Worker  * Return a permission av bit associated with tclass and the string
100*2d543d20SAndroid Build Coastguard Worker  * representation of the `perm_name'.
101*2d543d20SAndroid Build Coastguard Worker  */
102*2d543d20SAndroid Build Coastguard Worker extern int sepol_string_to_av_perm(sepol_security_class_t tclass,
103*2d543d20SAndroid Build Coastguard Worker 					const char *perm_name,
104*2d543d20SAndroid Build Coastguard Worker 					sepol_access_vector_t *av);
105*2d543d20SAndroid Build Coastguard Worker 
106*2d543d20SAndroid Build Coastguard Worker /*
107*2d543d20SAndroid Build Coastguard Worker  * Return a string representation of the permission av bit associated with
108*2d543d20SAndroid Build Coastguard Worker  * tclass.
109*2d543d20SAndroid Build Coastguard Worker  * Returns a pointer to an internal buffer, overridden by the next call to
110*2d543d20SAndroid Build Coastguard Worker  * this function or sepol_av_to_string().
111*2d543d20SAndroid Build Coastguard Worker  */
112*2d543d20SAndroid Build Coastguard Worker  extern const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
113*2d543d20SAndroid Build Coastguard Worker 					sepol_access_vector_t av);
114*2d543d20SAndroid Build Coastguard Worker 
115*2d543d20SAndroid Build Coastguard Worker /*
116*2d543d20SAndroid Build Coastguard Worker  * Compute a SID to use for labeling a new object in the
117*2d543d20SAndroid Build Coastguard Worker  * class `tclass' based on a SID pair.
118*2d543d20SAndroid Build Coastguard Worker  */
119*2d543d20SAndroid Build Coastguard Worker extern int sepol_transition_sid(sepol_security_id_t ssid,	/* IN */
120*2d543d20SAndroid Build Coastguard Worker 				sepol_security_id_t tsid,	/* IN */
121*2d543d20SAndroid Build Coastguard Worker 				sepol_security_class_t tclass,	/* IN */
122*2d543d20SAndroid Build Coastguard Worker 				sepol_security_id_t * out_sid);	/* OUT */
123*2d543d20SAndroid Build Coastguard Worker 
124*2d543d20SAndroid Build Coastguard Worker /*
125*2d543d20SAndroid Build Coastguard Worker  * Compute a SID to use when selecting a member of a
126*2d543d20SAndroid Build Coastguard Worker  * polyinstantiated object of class `tclass' based on
127*2d543d20SAndroid Build Coastguard Worker  * a SID pair.
128*2d543d20SAndroid Build Coastguard Worker  */
129*2d543d20SAndroid Build Coastguard Worker extern int sepol_member_sid(sepol_security_id_t ssid,	/* IN */
130*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t tsid,	/* IN */
131*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_class_t tclass,	/* IN */
132*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t * out_sid);	/* OUT */
133*2d543d20SAndroid Build Coastguard Worker 
134*2d543d20SAndroid Build Coastguard Worker /*
135*2d543d20SAndroid Build Coastguard Worker  * Compute a SID to use for relabeling an object in the
136*2d543d20SAndroid Build Coastguard Worker  * class `tclass' based on a SID pair.
137*2d543d20SAndroid Build Coastguard Worker  */
138*2d543d20SAndroid Build Coastguard Worker extern int sepol_change_sid(sepol_security_id_t ssid,	/* IN */
139*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t tsid,	/* IN */
140*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_class_t tclass,	/* IN */
141*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t * out_sid);	/* OUT */
142*2d543d20SAndroid Build Coastguard Worker 
143*2d543d20SAndroid Build Coastguard Worker /*
144*2d543d20SAndroid Build Coastguard Worker  * Write the security context string representation of
145*2d543d20SAndroid Build Coastguard Worker  * the context associated with `sid' into a dynamically
146*2d543d20SAndroid Build Coastguard Worker  * allocated string of the correct size.  Set `*scontext'
147*2d543d20SAndroid Build Coastguard Worker  * to point to this string and set `*scontext_len' to
148*2d543d20SAndroid Build Coastguard Worker  * the length of the string.
149*2d543d20SAndroid Build Coastguard Worker  */
150*2d543d20SAndroid Build Coastguard Worker extern int sepol_sid_to_context(sepol_security_id_t sid,	/* IN */
151*2d543d20SAndroid Build Coastguard Worker 				sepol_security_context_t * scontext,	/* OUT */
152*2d543d20SAndroid Build Coastguard Worker 				size_t * scontext_len);	/* OUT */
153*2d543d20SAndroid Build Coastguard Worker 
154*2d543d20SAndroid Build Coastguard Worker /*
155*2d543d20SAndroid Build Coastguard Worker  * Return a SID associated with the security context that
156*2d543d20SAndroid Build Coastguard Worker  * has the string representation specified by `scontext'.
157*2d543d20SAndroid Build Coastguard Worker  */
158*2d543d20SAndroid Build Coastguard Worker extern int sepol_context_to_sid(sepol_const_security_context_t scontext,	/* IN */
159*2d543d20SAndroid Build Coastguard Worker 				size_t scontext_len,	/* IN */
160*2d543d20SAndroid Build Coastguard Worker 				sepol_security_id_t * out_sid);	/* OUT */
161*2d543d20SAndroid Build Coastguard Worker 
162*2d543d20SAndroid Build Coastguard Worker /*
163*2d543d20SAndroid Build Coastguard Worker  * Generate the set of SIDs for legal security contexts
164*2d543d20SAndroid Build Coastguard Worker  * for a given user that can be reached by `fromsid'.
165*2d543d20SAndroid Build Coastguard Worker  * Set `*sids' to point to a dynamically allocated
166*2d543d20SAndroid Build Coastguard Worker  * array containing the set of SIDs.  Set `*nel' to the
167*2d543d20SAndroid Build Coastguard Worker  * number of elements in the array.
168*2d543d20SAndroid Build Coastguard Worker  */
169*2d543d20SAndroid Build Coastguard Worker extern int sepol_get_user_sids(sepol_security_id_t callsid,
170*2d543d20SAndroid Build Coastguard Worker 			       char *username,
171*2d543d20SAndroid Build Coastguard Worker 			       sepol_security_id_t ** sids, uint32_t * nel);
172*2d543d20SAndroid Build Coastguard Worker 
173*2d543d20SAndroid Build Coastguard Worker /*
174*2d543d20SAndroid Build Coastguard Worker  * Return the SIDs to use for an unlabeled file system
175*2d543d20SAndroid Build Coastguard Worker  * that is being mounted from the device with the
176*2d543d20SAndroid Build Coastguard Worker  * the kdevname `name'.  The `fs_sid' SID is returned for
177*2d543d20SAndroid Build Coastguard Worker  * the file system and the `file_sid' SID is returned
178*2d543d20SAndroid Build Coastguard Worker  * for all files within that file system.
179*2d543d20SAndroid Build Coastguard Worker  */
180*2d543d20SAndroid Build Coastguard Worker extern int sepol_fs_sid(char *dev,	/* IN */
181*2d543d20SAndroid Build Coastguard Worker 			sepol_security_id_t * fs_sid,	/* OUT  */
182*2d543d20SAndroid Build Coastguard Worker 			sepol_security_id_t * file_sid);	/* OUT */
183*2d543d20SAndroid Build Coastguard Worker 
184*2d543d20SAndroid Build Coastguard Worker /*
185*2d543d20SAndroid Build Coastguard Worker  * Return the SID of the port specified by
186*2d543d20SAndroid Build Coastguard Worker  * `domain', `type', `protocol', and `port'.
187*2d543d20SAndroid Build Coastguard Worker  */
188*2d543d20SAndroid Build Coastguard Worker extern int sepol_port_sid(uint16_t domain,
189*2d543d20SAndroid Build Coastguard Worker 			  uint16_t type,
190*2d543d20SAndroid Build Coastguard Worker 			  uint8_t protocol,
191*2d543d20SAndroid Build Coastguard Worker 			  uint16_t port, sepol_security_id_t * out_sid);
192*2d543d20SAndroid Build Coastguard Worker 
193*2d543d20SAndroid Build Coastguard Worker /*
194*2d543d20SAndroid Build Coastguard Worker  * Return the SID of the ibpkey specified by
195*2d543d20SAndroid Build Coastguard Worker  * `subnet prefix', and `pkey'.
196*2d543d20SAndroid Build Coastguard Worker  */
197*2d543d20SAndroid Build Coastguard Worker extern int sepol_ibpkey_sid(uint64_t subnet_prefix_p,
198*2d543d20SAndroid Build Coastguard Worker 			    uint16_t pkey,
199*2d543d20SAndroid Build Coastguard Worker 			    sepol_security_id_t *out_sid);
200*2d543d20SAndroid Build Coastguard Worker 
201*2d543d20SAndroid Build Coastguard Worker /*
202*2d543d20SAndroid Build Coastguard Worker  * Return the SID of the ibendport specified by
203*2d543d20SAndroid Build Coastguard Worker  * `dev_name', and `port'.
204*2d543d20SAndroid Build Coastguard Worker  */
205*2d543d20SAndroid Build Coastguard Worker extern int sepol_ibendport_sid(char *dev_name,
206*2d543d20SAndroid Build Coastguard Worker 			       uint8_t port,
207*2d543d20SAndroid Build Coastguard Worker 			       sepol_security_id_t *out_sid);
208*2d543d20SAndroid Build Coastguard Worker 
209*2d543d20SAndroid Build Coastguard Worker /*
210*2d543d20SAndroid Build Coastguard Worker  * Return the SIDs to use for a network interface
211*2d543d20SAndroid Build Coastguard Worker  * with the name `name'.  The `if_sid' SID is returned for
212*2d543d20SAndroid Build Coastguard Worker  * the interface and the `msg_sid' SID is returned as
213*2d543d20SAndroid Build Coastguard Worker  * the default SID for messages received on the
214*2d543d20SAndroid Build Coastguard Worker  * interface.
215*2d543d20SAndroid Build Coastguard Worker  */
216*2d543d20SAndroid Build Coastguard Worker extern int sepol_netif_sid(char *name,
217*2d543d20SAndroid Build Coastguard Worker 			   sepol_security_id_t * if_sid,
218*2d543d20SAndroid Build Coastguard Worker 			   sepol_security_id_t * msg_sid);
219*2d543d20SAndroid Build Coastguard Worker 
220*2d543d20SAndroid Build Coastguard Worker /*
221*2d543d20SAndroid Build Coastguard Worker  * Return the SID of the node specified by the address
222*2d543d20SAndroid Build Coastguard Worker  * `addr' where `addrlen' is the length of the address
223*2d543d20SAndroid Build Coastguard Worker  * in bytes and `domain' is the communications domain or
224*2d543d20SAndroid Build Coastguard Worker  * address family in which the address should be interpreted.
225*2d543d20SAndroid Build Coastguard Worker  */
226*2d543d20SAndroid Build Coastguard Worker extern int sepol_node_sid(uint16_t domain,
227*2d543d20SAndroid Build Coastguard Worker 			  void *addr,
228*2d543d20SAndroid Build Coastguard Worker 			  size_t addrlen, sepol_security_id_t * out_sid);
229*2d543d20SAndroid Build Coastguard Worker 
230*2d543d20SAndroid Build Coastguard Worker /*
231*2d543d20SAndroid Build Coastguard Worker  * Return a value indicating how to handle labeling for the
232*2d543d20SAndroid Build Coastguard Worker  * the specified filesystem type, and optionally return a SID
233*2d543d20SAndroid Build Coastguard Worker  * for the filesystem object.
234*2d543d20SAndroid Build Coastguard Worker  */
235*2d543d20SAndroid Build Coastguard Worker #define SECURITY_FS_USE_XATTR 1	/* use xattr */
236*2d543d20SAndroid Build Coastguard Worker #define SECURITY_FS_USE_TRANS 2	/* use transition SIDs, e.g. devpts/tmpfs */
237*2d543d20SAndroid Build Coastguard Worker #define SECURITY_FS_USE_TASK  3	/* use task SIDs, e.g. pipefs/sockfs */
238*2d543d20SAndroid Build Coastguard Worker #define SECURITY_FS_USE_GENFS 4	/* use the genfs support */
239*2d543d20SAndroid Build Coastguard Worker #define SECURITY_FS_USE_NONE  5	/* no labeling support */
240*2d543d20SAndroid Build Coastguard Worker extern int sepol_fs_use(const char *fstype,	/* IN */
241*2d543d20SAndroid Build Coastguard Worker 			unsigned int *behavior,	/* OUT */
242*2d543d20SAndroid Build Coastguard Worker 			sepol_security_id_t * sid);	/* OUT  */
243*2d543d20SAndroid Build Coastguard Worker 
244*2d543d20SAndroid Build Coastguard Worker /*
245*2d543d20SAndroid Build Coastguard Worker  * Return the SID to use for a file in a filesystem
246*2d543d20SAndroid Build Coastguard Worker  * that cannot support a persistent label mapping or use another
247*2d543d20SAndroid Build Coastguard Worker  * fixed labeling behavior like transition SIDs or task SIDs.
248*2d543d20SAndroid Build Coastguard Worker  */
249*2d543d20SAndroid Build Coastguard Worker extern int sepol_genfs_sid(const char *fstype,	/* IN */
250*2d543d20SAndroid Build Coastguard Worker 			   const char *name,	/* IN */
251*2d543d20SAndroid Build Coastguard Worker 			   sepol_security_class_t sclass,	/* IN */
252*2d543d20SAndroid Build Coastguard Worker 			   sepol_security_id_t * sid);	/* OUT  */
253*2d543d20SAndroid Build Coastguard Worker 
254*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus
255*2d543d20SAndroid Build Coastguard Worker }
256*2d543d20SAndroid Build Coastguard Worker #endif
257*2d543d20SAndroid Build Coastguard Worker 
258*2d543d20SAndroid Build Coastguard Worker #endif
259