xref: /aosp_15_r20/external/selinux/libselinux/include/selinux/restorecon.h (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker #ifndef _RESTORECON_H_
2*2d543d20SAndroid Build Coastguard Worker #define _RESTORECON_H_
3*2d543d20SAndroid Build Coastguard Worker 
4*2d543d20SAndroid Build Coastguard Worker #include <selinux/label.h>
5*2d543d20SAndroid Build Coastguard Worker 
6*2d543d20SAndroid Build Coastguard Worker #include <sys/types.h>
7*2d543d20SAndroid Build Coastguard Worker #include <stddef.h>
8*2d543d20SAndroid Build Coastguard Worker #include <stdarg.h>
9*2d543d20SAndroid Build Coastguard Worker 
10*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus
11*2d543d20SAndroid Build Coastguard Worker extern "C" {
12*2d543d20SAndroid Build Coastguard Worker #endif
13*2d543d20SAndroid Build Coastguard Worker 
14*2d543d20SAndroid Build Coastguard Worker /**
15*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon - Relabel files.
16*2d543d20SAndroid Build Coastguard Worker  * @pathname: specifies file/directory to relabel.
17*2d543d20SAndroid Build Coastguard Worker  * @restorecon_flags: specifies the actions to be performed when relabeling.
18*2d543d20SAndroid Build Coastguard Worker  *
19*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon(3) will automatically call
20*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_default_handle(3) and selinux_restorecon_set_sehandle(3)
21*2d543d20SAndroid Build Coastguard Worker  * first time through to set the selabel_open(3) parameters to use the
22*2d543d20SAndroid Build Coastguard Worker  * currently loaded policy file_contexts and request their computed digest.
23*2d543d20SAndroid Build Coastguard Worker  *
24*2d543d20SAndroid Build Coastguard Worker  * Should other selabel_open(3) parameters be required see
25*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_set_sehandle(3).
26*2d543d20SAndroid Build Coastguard Worker  */
27*2d543d20SAndroid Build Coastguard Worker extern int selinux_restorecon(const char *pathname,
28*2d543d20SAndroid Build Coastguard Worker 				    unsigned int restorecon_flags);
29*2d543d20SAndroid Build Coastguard Worker /**
30*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_parallel - Relabel files, optionally use more threads.
31*2d543d20SAndroid Build Coastguard Worker  * @pathname: specifies file/directory to relabel.
32*2d543d20SAndroid Build Coastguard Worker  * @restorecon_flags: specifies the actions to be performed when relabeling.
33*2d543d20SAndroid Build Coastguard Worker  * @nthreads: specifies the number of threads to use (0 = use number of CPUs
34*2d543d20SAndroid Build Coastguard Worker  *            currently online)
35*2d543d20SAndroid Build Coastguard Worker  *
36*2d543d20SAndroid Build Coastguard Worker  * Same as selinux_restorecon(3), but allows to use multiple threads to do
37*2d543d20SAndroid Build Coastguard Worker  * the work.
38*2d543d20SAndroid Build Coastguard Worker  */
39*2d543d20SAndroid Build Coastguard Worker extern int selinux_restorecon_parallel(const char *pathname,
40*2d543d20SAndroid Build Coastguard Worker 				       unsigned int restorecon_flags,
41*2d543d20SAndroid Build Coastguard Worker 				       size_t nthreads);
42*2d543d20SAndroid Build Coastguard Worker /*
43*2d543d20SAndroid Build Coastguard Worker  * restorecon_flags options
44*2d543d20SAndroid Build Coastguard Worker  */
45*2d543d20SAndroid Build Coastguard Worker /*
46*2d543d20SAndroid Build Coastguard Worker  * Force the checking of labels even if the stored SHA1 digest
47*2d543d20SAndroid Build Coastguard Worker  * matches the specfiles SHA1 digest (requires CAP_SYS_ADMIN).
48*2d543d20SAndroid Build Coastguard Worker  */
49*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_IGNORE_DIGEST		0x00001
50*2d543d20SAndroid Build Coastguard Worker /*
51*2d543d20SAndroid Build Coastguard Worker  * Do not change file labels.
52*2d543d20SAndroid Build Coastguard Worker  */
53*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_NOCHANGE			0x00002
54*2d543d20SAndroid Build Coastguard Worker /*
55*2d543d20SAndroid Build Coastguard Worker  * If set, change file label to that in spec file.
56*2d543d20SAndroid Build Coastguard Worker  * If not, only change type component to that in spec file.
57*2d543d20SAndroid Build Coastguard Worker  */
58*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_SET_SPECFILE_CTX		0x00004
59*2d543d20SAndroid Build Coastguard Worker /*
60*2d543d20SAndroid Build Coastguard Worker  * Recursively descend directories.
61*2d543d20SAndroid Build Coastguard Worker  */
62*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_RECURSE			0x00008
63*2d543d20SAndroid Build Coastguard Worker /*
64*2d543d20SAndroid Build Coastguard Worker  * Log changes to selinux log. Note that if VERBOSE and
65*2d543d20SAndroid Build Coastguard Worker  * PROGRESS are set, then PROGRESS will take precedence.
66*2d543d20SAndroid Build Coastguard Worker  */
67*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_VERBOSE			0x00010
68*2d543d20SAndroid Build Coastguard Worker /*
69*2d543d20SAndroid Build Coastguard Worker  * If SELINUX_RESTORECON_PROGRESS is true and
70*2d543d20SAndroid Build Coastguard Worker  * SELINUX_RESTORECON_MASS_RELABEL is true, then output approx % complete,
71*2d543d20SAndroid Build Coastguard Worker  * else output the number of files in 1k blocks processed to stdout.
72*2d543d20SAndroid Build Coastguard Worker  */
73*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_PROGRESS			0x00020
74*2d543d20SAndroid Build Coastguard Worker /*
75*2d543d20SAndroid Build Coastguard Worker  * Convert passed-in pathname to canonical pathname.
76*2d543d20SAndroid Build Coastguard Worker  */
77*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_REALPATH			0x00040
78*2d543d20SAndroid Build Coastguard Worker /*
79*2d543d20SAndroid Build Coastguard Worker  * Prevent descending into directories that have a different
80*2d543d20SAndroid Build Coastguard Worker  * device number than the pathname from which the descent began.
81*2d543d20SAndroid Build Coastguard Worker  */
82*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_XDEV				0x00080
83*2d543d20SAndroid Build Coastguard Worker /*
84*2d543d20SAndroid Build Coastguard Worker  * Attempt to add an association between an inode and a specification.
85*2d543d20SAndroid Build Coastguard Worker  * If there is already an association for the inode and it conflicts
86*2d543d20SAndroid Build Coastguard Worker  * with the specification, then use the last matching specification.
87*2d543d20SAndroid Build Coastguard Worker  */
88*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_ADD_ASSOC			0x00100
89*2d543d20SAndroid Build Coastguard Worker /*
90*2d543d20SAndroid Build Coastguard Worker  * Abort on errors during the file tree walk.
91*2d543d20SAndroid Build Coastguard Worker  */
92*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_ABORT_ON_ERROR		0x00200
93*2d543d20SAndroid Build Coastguard Worker /*
94*2d543d20SAndroid Build Coastguard Worker  * Log any label changes to syslog.
95*2d543d20SAndroid Build Coastguard Worker  */
96*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_SYSLOG_CHANGES		0x00400
97*2d543d20SAndroid Build Coastguard Worker /*
98*2d543d20SAndroid Build Coastguard Worker  * Log what spec matched each file.
99*2d543d20SAndroid Build Coastguard Worker  */
100*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_LOG_MATCHES			0x00800
101*2d543d20SAndroid Build Coastguard Worker /*
102*2d543d20SAndroid Build Coastguard Worker  * Ignore files that do not exist.
103*2d543d20SAndroid Build Coastguard Worker  */
104*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_IGNORE_NOENTRY		0x01000
105*2d543d20SAndroid Build Coastguard Worker /*
106*2d543d20SAndroid Build Coastguard Worker  * Do not read /proc/mounts to obtain a list of non-seclabel
107*2d543d20SAndroid Build Coastguard Worker  * mounts to be excluded from relabeling checks.
108*2d543d20SAndroid Build Coastguard Worker  */
109*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_IGNORE_MOUNTS		0x02000
110*2d543d20SAndroid Build Coastguard Worker /*
111*2d543d20SAndroid Build Coastguard Worker  * Set if there is a mass relabel required.
112*2d543d20SAndroid Build Coastguard Worker  * See SELINUX_RESTORECON_PROGRESS flag for details.
113*2d543d20SAndroid Build Coastguard Worker  */
114*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_MASS_RELABEL			0x04000
115*2d543d20SAndroid Build Coastguard Worker /*
116*2d543d20SAndroid Build Coastguard Worker  * Set if no digest is to be read or written (as only processes
117*2d543d20SAndroid Build Coastguard Worker  * running with CAP_SYS_ADMIN can read/write digests).
118*2d543d20SAndroid Build Coastguard Worker  */
119*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_SKIP_DIGEST			0x08000
120*2d543d20SAndroid Build Coastguard Worker 
121*2d543d20SAndroid Build Coastguard Worker /*
122*2d543d20SAndroid Build Coastguard Worker  * Set to treat conflicting specifications as errors.
123*2d543d20SAndroid Build Coastguard Worker  */
124*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_CONFLICT_ERROR		0x10000
125*2d543d20SAndroid Build Coastguard Worker 
126*2d543d20SAndroid Build Coastguard Worker /*
127*2d543d20SAndroid Build Coastguard Worker  * Count, but otherwise ignore, errors during the file tree walk.
128*2d543d20SAndroid Build Coastguard Worker  */
129*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_COUNT_ERRORS			0x20000
130*2d543d20SAndroid Build Coastguard Worker 
131*2d543d20SAndroid Build Coastguard Worker /**
132*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_set_sehandle - Set the global fc handle.
133*2d543d20SAndroid Build Coastguard Worker  * @hndl: specifies handle to set as the global fc handle.
134*2d543d20SAndroid Build Coastguard Worker  *
135*2d543d20SAndroid Build Coastguard Worker  * Called by a process that has already called selabel_open(3) with its
136*2d543d20SAndroid Build Coastguard Worker  * required parameters, or if selinux_restorecon_default_handle(3) has been
137*2d543d20SAndroid Build Coastguard Worker  * called to set the default selabel_open(3) parameters.
138*2d543d20SAndroid Build Coastguard Worker  */
139*2d543d20SAndroid Build Coastguard Worker extern void selinux_restorecon_set_sehandle(struct selabel_handle *hndl);
140*2d543d20SAndroid Build Coastguard Worker 
141*2d543d20SAndroid Build Coastguard Worker /**
142*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_default_handle - Sets default selabel_open(3) parameters
143*2d543d20SAndroid Build Coastguard Worker  *				       to use the currently loaded policy and
144*2d543d20SAndroid Build Coastguard Worker  *				       file_contexts.
145*2d543d20SAndroid Build Coastguard Worker  *
146*2d543d20SAndroid Build Coastguard Worker  * Return value is the created handle on success or NULL with @errno set on
147*2d543d20SAndroid Build Coastguard Worker  * failure.
148*2d543d20SAndroid Build Coastguard Worker  */
149*2d543d20SAndroid Build Coastguard Worker extern struct selabel_handle *selinux_restorecon_default_handle(void);
150*2d543d20SAndroid Build Coastguard Worker 
151*2d543d20SAndroid Build Coastguard Worker /**
152*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_set_exclude_list - Add a list of directories that are
153*2d543d20SAndroid Build Coastguard Worker  *					 to be excluded from relabeling.
154*2d543d20SAndroid Build Coastguard Worker  * @exclude_list: containing a NULL terminated list of one or more
155*2d543d20SAndroid Build Coastguard Worker  *		  directories not to be relabeled.
156*2d543d20SAndroid Build Coastguard Worker  */
157*2d543d20SAndroid Build Coastguard Worker extern void selinux_restorecon_set_exclude_list(const char **exclude_list);
158*2d543d20SAndroid Build Coastguard Worker 
159*2d543d20SAndroid Build Coastguard Worker /**
160*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_set_alt_rootpath - Use alternate rootpath.
161*2d543d20SAndroid Build Coastguard Worker  * @alt_rootpath: containing the alternate rootpath to be used.
162*2d543d20SAndroid Build Coastguard Worker  *
163*2d543d20SAndroid Build Coastguard Worker  * Return %0 on success, -%1 with @errno set on failure.
164*2d543d20SAndroid Build Coastguard Worker  */
165*2d543d20SAndroid Build Coastguard Worker extern int selinux_restorecon_set_alt_rootpath(const char *alt_rootpath);
166*2d543d20SAndroid Build Coastguard Worker 
167*2d543d20SAndroid Build Coastguard Worker /**
168*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_xattr - Read/remove security.sehash xattr entries.
169*2d543d20SAndroid Build Coastguard Worker  * @pathname: specifies directory path to check.
170*2d543d20SAndroid Build Coastguard Worker  * @xattr_flags: specifies the actions to be performed.
171*2d543d20SAndroid Build Coastguard Worker  * @xattr_list: a linked list of struct dir_xattr structures containing
172*2d543d20SAndroid Build Coastguard Worker  *              the directory, digest and result of the action on the
173*2d543d20SAndroid Build Coastguard Worker  *              security.sehash entry.
174*2d543d20SAndroid Build Coastguard Worker  *
175*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_xattr(3) will automatically call
176*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_default_handle(3) and selinux_restorecon_set_sehandle(3)
177*2d543d20SAndroid Build Coastguard Worker  * first time through to set the selabel_open(3) parameters to use the
178*2d543d20SAndroid Build Coastguard Worker  * currently loaded policy file_contexts and request their computed digest.
179*2d543d20SAndroid Build Coastguard Worker  *
180*2d543d20SAndroid Build Coastguard Worker  * Should other selabel_open(3) parameters be required see
181*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_set_sehandle(3), however note that a file_contexts
182*2d543d20SAndroid Build Coastguard Worker  * computed digest is required for selinux_restorecon_xattr().
183*2d543d20SAndroid Build Coastguard Worker  */
184*2d543d20SAndroid Build Coastguard Worker enum digest_result {
185*2d543d20SAndroid Build Coastguard Worker 	MATCH = 0,
186*2d543d20SAndroid Build Coastguard Worker 	NOMATCH,
187*2d543d20SAndroid Build Coastguard Worker 	DELETED_MATCH,
188*2d543d20SAndroid Build Coastguard Worker 	DELETED_NOMATCH,
189*2d543d20SAndroid Build Coastguard Worker 	ERROR
190*2d543d20SAndroid Build Coastguard Worker };
191*2d543d20SAndroid Build Coastguard Worker 
192*2d543d20SAndroid Build Coastguard Worker struct dir_xattr {
193*2d543d20SAndroid Build Coastguard Worker 	char *directory;
194*2d543d20SAndroid Build Coastguard Worker 	char *digest; /* A hex encoded string that can be printed. */
195*2d543d20SAndroid Build Coastguard Worker 	enum digest_result result;
196*2d543d20SAndroid Build Coastguard Worker 	struct dir_xattr *next;
197*2d543d20SAndroid Build Coastguard Worker };
198*2d543d20SAndroid Build Coastguard Worker 
199*2d543d20SAndroid Build Coastguard Worker extern int selinux_restorecon_xattr(const char *pathname,
200*2d543d20SAndroid Build Coastguard Worker 				    unsigned int xattr_flags,
201*2d543d20SAndroid Build Coastguard Worker 				    struct dir_xattr ***xattr_list);
202*2d543d20SAndroid Build Coastguard Worker 
203*2d543d20SAndroid Build Coastguard Worker /*
204*2d543d20SAndroid Build Coastguard Worker  * xattr_flags options
205*2d543d20SAndroid Build Coastguard Worker  */
206*2d543d20SAndroid Build Coastguard Worker /* Recursively descend directories. */
207*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_XATTR_RECURSE			0x0001
208*2d543d20SAndroid Build Coastguard Worker /* Delete non-matching digests from each directory in pathname. */
209*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_XATTR_DELETE_NONMATCH_DIGESTS	0x0002
210*2d543d20SAndroid Build Coastguard Worker /* Delete all digests found in pathname. */
211*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_XATTR_DELETE_ALL_DIGESTS		0x0004
212*2d543d20SAndroid Build Coastguard Worker /* Do not read /proc/mounts. */
213*2d543d20SAndroid Build Coastguard Worker #define SELINUX_RESTORECON_XATTR_IGNORE_MOUNTS			0x0008
214*2d543d20SAndroid Build Coastguard Worker 
215*2d543d20SAndroid Build Coastguard Worker /* selinux_restorecon_get_skipped_errors - Get the number of errors ignored
216*2d543d20SAndroid Build Coastguard Worker  * during re-labeling.
217*2d543d20SAndroid Build Coastguard Worker  *
218*2d543d20SAndroid Build Coastguard Worker  * If SELINUX_RESTORECON_COUNT_ERRORS was passed to selinux_restorecon(3) or
219*2d543d20SAndroid Build Coastguard Worker  * selinux_restorecon_parallel(3), and that function returned successfully
220*2d543d20SAndroid Build Coastguard Worker  * (i.e., with a zero return value), then this function returns the number of
221*2d543d20SAndroid Build Coastguard Worker  * errors ignored during the file tree walk.
222*2d543d20SAndroid Build Coastguard Worker  */
223*2d543d20SAndroid Build Coastguard Worker extern long unsigned selinux_restorecon_get_skipped_errors(void);
224*2d543d20SAndroid Build Coastguard Worker 
225*2d543d20SAndroid Build Coastguard Worker #ifdef __cplusplus
226*2d543d20SAndroid Build Coastguard Worker }
227*2d543d20SAndroid Build Coastguard Worker #endif
228*2d543d20SAndroid Build Coastguard Worker #endif
229