xref: /aosp_15_r20/external/selinux/libselinux/src/matchpathcon.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker #include <sys/stat.h>
2*2d543d20SAndroid Build Coastguard Worker #include <string.h>
3*2d543d20SAndroid Build Coastguard Worker #include <errno.h>
4*2d543d20SAndroid Build Coastguard Worker #include <stdio.h>
5*2d543d20SAndroid Build Coastguard Worker #include "selinux_internal.h"
6*2d543d20SAndroid Build Coastguard Worker #include "label_internal.h"
7*2d543d20SAndroid Build Coastguard Worker #include "callbacks.h"
8*2d543d20SAndroid Build Coastguard Worker #include <limits.h>
9*2d543d20SAndroid Build Coastguard Worker 
10*2d543d20SAndroid Build Coastguard Worker static int (*myinvalidcon) (const char *p, unsigned l, char *c) = NULL;
11*2d543d20SAndroid Build Coastguard Worker static int (*mycanoncon) (const char *p, unsigned l, char **c) =  NULL;
12*2d543d20SAndroid Build Coastguard Worker 
13*2d543d20SAndroid Build Coastguard Worker static void
14*2d543d20SAndroid Build Coastguard Worker #ifdef __GNUC__
15*2d543d20SAndroid Build Coastguard Worker     __attribute__ ((format(printf, 1, 2)))
16*2d543d20SAndroid Build Coastguard Worker #endif
default_printf(const char * fmt,...)17*2d543d20SAndroid Build Coastguard Worker     default_printf(const char *fmt, ...)
18*2d543d20SAndroid Build Coastguard Worker {
19*2d543d20SAndroid Build Coastguard Worker 	va_list ap;
20*2d543d20SAndroid Build Coastguard Worker 	va_start(ap, fmt);
21*2d543d20SAndroid Build Coastguard Worker 	vfprintf(stderr, fmt, ap);
22*2d543d20SAndroid Build Coastguard Worker 	va_end(ap);
23*2d543d20SAndroid Build Coastguard Worker }
24*2d543d20SAndroid Build Coastguard Worker 
25*2d543d20SAndroid Build Coastguard Worker void
26*2d543d20SAndroid Build Coastguard Worker #ifdef __GNUC__
27*2d543d20SAndroid Build Coastguard Worker     __attribute__ ((format(printf, 1, 2)))
28*2d543d20SAndroid Build Coastguard Worker #endif
29*2d543d20SAndroid Build Coastguard Worker     (*myprintf) (const char *fmt,...) = &default_printf;
30*2d543d20SAndroid Build Coastguard Worker int myprintf_compat = 0;
31*2d543d20SAndroid Build Coastguard Worker 
set_matchpathcon_printf(void (* f)(const char * fmt,...))32*2d543d20SAndroid Build Coastguard Worker void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
33*2d543d20SAndroid Build Coastguard Worker {
34*2d543d20SAndroid Build Coastguard Worker 	myprintf = f ? f : &default_printf;
35*2d543d20SAndroid Build Coastguard Worker 	myprintf_compat = 1;
36*2d543d20SAndroid Build Coastguard Worker }
37*2d543d20SAndroid Build Coastguard Worker 
compat_validate(const struct selabel_handle * rec,struct selabel_lookup_rec * contexts,const char * path,unsigned lineno)38*2d543d20SAndroid Build Coastguard Worker int compat_validate(const struct selabel_handle *rec,
39*2d543d20SAndroid Build Coastguard Worker 		    struct selabel_lookup_rec *contexts,
40*2d543d20SAndroid Build Coastguard Worker 		    const char *path, unsigned lineno)
41*2d543d20SAndroid Build Coastguard Worker {
42*2d543d20SAndroid Build Coastguard Worker 	int rc;
43*2d543d20SAndroid Build Coastguard Worker 	char **ctx = &contexts->ctx_raw;
44*2d543d20SAndroid Build Coastguard Worker 
45*2d543d20SAndroid Build Coastguard Worker 	if (myinvalidcon)
46*2d543d20SAndroid Build Coastguard Worker 		rc = myinvalidcon(path, lineno, *ctx);
47*2d543d20SAndroid Build Coastguard Worker 	else if (mycanoncon)
48*2d543d20SAndroid Build Coastguard Worker 		rc = mycanoncon(path, lineno, ctx);
49*2d543d20SAndroid Build Coastguard Worker 	else if (rec->validating) {
50*2d543d20SAndroid Build Coastguard Worker 		rc = selabel_validate(contexts);
51*2d543d20SAndroid Build Coastguard Worker 		if (rc < 0) {
52*2d543d20SAndroid Build Coastguard Worker 			if (lineno) {
53*2d543d20SAndroid Build Coastguard Worker 				COMPAT_LOG(SELINUX_WARNING,
54*2d543d20SAndroid Build Coastguard Worker 					    "%s: line %u has invalid context %s\n",
55*2d543d20SAndroid Build Coastguard Worker 						path, lineno, *ctx);
56*2d543d20SAndroid Build Coastguard Worker 			} else {
57*2d543d20SAndroid Build Coastguard Worker 				COMPAT_LOG(SELINUX_WARNING,
58*2d543d20SAndroid Build Coastguard Worker 					    "%s: has invalid context %s\n", path, *ctx);
59*2d543d20SAndroid Build Coastguard Worker 			}
60*2d543d20SAndroid Build Coastguard Worker 		}
61*2d543d20SAndroid Build Coastguard Worker 	} else
62*2d543d20SAndroid Build Coastguard Worker 		rc = 0;
63*2d543d20SAndroid Build Coastguard Worker 
64*2d543d20SAndroid Build Coastguard Worker 	return rc ? -1 : 0;
65*2d543d20SAndroid Build Coastguard Worker }
66*2d543d20SAndroid Build Coastguard Worker 
67*2d543d20SAndroid Build Coastguard Worker #ifndef BUILD_HOST
68*2d543d20SAndroid Build Coastguard Worker 
69*2d543d20SAndroid Build Coastguard Worker static __thread struct selabel_handle *hnd;
70*2d543d20SAndroid Build Coastguard Worker 
71*2d543d20SAndroid Build Coastguard Worker /*
72*2d543d20SAndroid Build Coastguard Worker  * An array for mapping integers to contexts
73*2d543d20SAndroid Build Coastguard Worker  */
74*2d543d20SAndroid Build Coastguard Worker static __thread char **con_array;
75*2d543d20SAndroid Build Coastguard Worker static __thread int con_array_size;
76*2d543d20SAndroid Build Coastguard Worker static __thread int con_array_used;
77*2d543d20SAndroid Build Coastguard Worker 
78*2d543d20SAndroid Build Coastguard Worker static pthread_once_t once = PTHREAD_ONCE_INIT;
79*2d543d20SAndroid Build Coastguard Worker static pthread_key_t destructor_key;
80*2d543d20SAndroid Build Coastguard Worker static int destructor_key_initialized = 0;
81*2d543d20SAndroid Build Coastguard Worker 
free_array_elts(void)82*2d543d20SAndroid Build Coastguard Worker static void free_array_elts(void)
83*2d543d20SAndroid Build Coastguard Worker {
84*2d543d20SAndroid Build Coastguard Worker 	int i;
85*2d543d20SAndroid Build Coastguard Worker 	for (i = 0; i < con_array_used; i++)
86*2d543d20SAndroid Build Coastguard Worker 		free(con_array[i]);
87*2d543d20SAndroid Build Coastguard Worker 	free(con_array);
88*2d543d20SAndroid Build Coastguard Worker 
89*2d543d20SAndroid Build Coastguard Worker 	con_array_size = con_array_used = 0;
90*2d543d20SAndroid Build Coastguard Worker 	con_array = NULL;
91*2d543d20SAndroid Build Coastguard Worker }
92*2d543d20SAndroid Build Coastguard Worker 
add_array_elt(char * con)93*2d543d20SAndroid Build Coastguard Worker static int add_array_elt(char *con)
94*2d543d20SAndroid Build Coastguard Worker {
95*2d543d20SAndroid Build Coastguard Worker 	char **tmp;
96*2d543d20SAndroid Build Coastguard Worker 	if (con_array_size) {
97*2d543d20SAndroid Build Coastguard Worker 		while (con_array_used >= con_array_size) {
98*2d543d20SAndroid Build Coastguard Worker 			con_array_size *= 2;
99*2d543d20SAndroid Build Coastguard Worker 			tmp = (char **)reallocarray(con_array, con_array_size,
100*2d543d20SAndroid Build Coastguard Worker 						    sizeof(char*));
101*2d543d20SAndroid Build Coastguard Worker 			if (!tmp) {
102*2d543d20SAndroid Build Coastguard Worker 				free_array_elts();
103*2d543d20SAndroid Build Coastguard Worker 				return -1;
104*2d543d20SAndroid Build Coastguard Worker 			}
105*2d543d20SAndroid Build Coastguard Worker 			con_array = tmp;
106*2d543d20SAndroid Build Coastguard Worker 		}
107*2d543d20SAndroid Build Coastguard Worker 	} else {
108*2d543d20SAndroid Build Coastguard Worker 		con_array_size = 1000;
109*2d543d20SAndroid Build Coastguard Worker 		con_array = (char **)malloc(sizeof(char*) * con_array_size);
110*2d543d20SAndroid Build Coastguard Worker 		if (!con_array) {
111*2d543d20SAndroid Build Coastguard Worker 			con_array_size = con_array_used = 0;
112*2d543d20SAndroid Build Coastguard Worker 			return -1;
113*2d543d20SAndroid Build Coastguard Worker 		}
114*2d543d20SAndroid Build Coastguard Worker 	}
115*2d543d20SAndroid Build Coastguard Worker 
116*2d543d20SAndroid Build Coastguard Worker 	con_array[con_array_used] = strdup(con);
117*2d543d20SAndroid Build Coastguard Worker 	if (!con_array[con_array_used])
118*2d543d20SAndroid Build Coastguard Worker 		return -1;
119*2d543d20SAndroid Build Coastguard Worker 	return con_array_used++;
120*2d543d20SAndroid Build Coastguard Worker }
121*2d543d20SAndroid Build Coastguard Worker 
set_matchpathcon_invalidcon(int (* f)(const char * p,unsigned l,char * c))122*2d543d20SAndroid Build Coastguard Worker void set_matchpathcon_invalidcon(int (*f) (const char *p, unsigned l, char *c))
123*2d543d20SAndroid Build Coastguard Worker {
124*2d543d20SAndroid Build Coastguard Worker 	myinvalidcon = f;
125*2d543d20SAndroid Build Coastguard Worker }
126*2d543d20SAndroid Build Coastguard Worker 
default_canoncon(const char * path,unsigned lineno,char ** context)127*2d543d20SAndroid Build Coastguard Worker static int default_canoncon(const char *path, unsigned lineno, char **context)
128*2d543d20SAndroid Build Coastguard Worker {
129*2d543d20SAndroid Build Coastguard Worker 	char *tmpcon;
130*2d543d20SAndroid Build Coastguard Worker 	if (security_canonicalize_context_raw(*context, &tmpcon) < 0) {
131*2d543d20SAndroid Build Coastguard Worker 		if (errno == ENOENT)
132*2d543d20SAndroid Build Coastguard Worker 			return 0;
133*2d543d20SAndroid Build Coastguard Worker 		if (lineno)
134*2d543d20SAndroid Build Coastguard Worker 			myprintf("%s:  line %u has invalid context %s\n", path,
135*2d543d20SAndroid Build Coastguard Worker 				 lineno, *context);
136*2d543d20SAndroid Build Coastguard Worker 		else
137*2d543d20SAndroid Build Coastguard Worker 			myprintf("%s:  invalid context %s\n", path, *context);
138*2d543d20SAndroid Build Coastguard Worker 		return 1;
139*2d543d20SAndroid Build Coastguard Worker 	}
140*2d543d20SAndroid Build Coastguard Worker 	free(*context);
141*2d543d20SAndroid Build Coastguard Worker 	*context = tmpcon;
142*2d543d20SAndroid Build Coastguard Worker 	return 0;
143*2d543d20SAndroid Build Coastguard Worker }
144*2d543d20SAndroid Build Coastguard Worker 
set_matchpathcon_canoncon(int (* f)(const char * p,unsigned l,char ** c))145*2d543d20SAndroid Build Coastguard Worker void set_matchpathcon_canoncon(int (*f) (const char *p, unsigned l, char **c))
146*2d543d20SAndroid Build Coastguard Worker {
147*2d543d20SAndroid Build Coastguard Worker 	if (f)
148*2d543d20SAndroid Build Coastguard Worker 		mycanoncon = f;
149*2d543d20SAndroid Build Coastguard Worker 	else
150*2d543d20SAndroid Build Coastguard Worker 		mycanoncon = &default_canoncon;
151*2d543d20SAndroid Build Coastguard Worker }
152*2d543d20SAndroid Build Coastguard Worker 
153*2d543d20SAndroid Build Coastguard Worker static __thread struct selinux_opt options[SELABEL_NOPT];
154*2d543d20SAndroid Build Coastguard Worker static __thread int notrans;
155*2d543d20SAndroid Build Coastguard Worker 
set_matchpathcon_flags(unsigned int flags)156*2d543d20SAndroid Build Coastguard Worker void set_matchpathcon_flags(unsigned int flags)
157*2d543d20SAndroid Build Coastguard Worker {
158*2d543d20SAndroid Build Coastguard Worker 	int i;
159*2d543d20SAndroid Build Coastguard Worker 	memset(options, 0, sizeof(options));
160*2d543d20SAndroid Build Coastguard Worker 	i = SELABEL_OPT_BASEONLY;
161*2d543d20SAndroid Build Coastguard Worker 	options[i].type = i;
162*2d543d20SAndroid Build Coastguard Worker 	options[i].value = (flags & MATCHPATHCON_BASEONLY) ? (char*)1 : NULL;
163*2d543d20SAndroid Build Coastguard Worker 	i = SELABEL_OPT_VALIDATE;
164*2d543d20SAndroid Build Coastguard Worker 	options[i].type = i;
165*2d543d20SAndroid Build Coastguard Worker 	options[i].value = (flags & MATCHPATHCON_VALIDATE) ? (char*)1 : NULL;
166*2d543d20SAndroid Build Coastguard Worker 	notrans = flags & MATCHPATHCON_NOTRANS;
167*2d543d20SAndroid Build Coastguard Worker }
168*2d543d20SAndroid Build Coastguard Worker 
169*2d543d20SAndroid Build Coastguard Worker /*
170*2d543d20SAndroid Build Coastguard Worker  * An association between an inode and a
171*2d543d20SAndroid Build Coastguard Worker  * specification.
172*2d543d20SAndroid Build Coastguard Worker  */
173*2d543d20SAndroid Build Coastguard Worker typedef struct file_spec {
174*2d543d20SAndroid Build Coastguard Worker 	ino_t ino;		/* inode number */
175*2d543d20SAndroid Build Coastguard Worker 	int specind;		/* index of specification in spec */
176*2d543d20SAndroid Build Coastguard Worker 	char *file;		/* full pathname for diagnostic messages about conflicts */
177*2d543d20SAndroid Build Coastguard Worker 	struct file_spec *next;	/* next association in hash bucket chain */
178*2d543d20SAndroid Build Coastguard Worker } file_spec_t;
179*2d543d20SAndroid Build Coastguard Worker 
180*2d543d20SAndroid Build Coastguard Worker /*
181*2d543d20SAndroid Build Coastguard Worker  * The hash table of associations, hashed by inode number.
182*2d543d20SAndroid Build Coastguard Worker  * Chaining is used for collisions, with elements ordered
183*2d543d20SAndroid Build Coastguard Worker  * by inode number in each bucket.  Each hash bucket has a dummy
184*2d543d20SAndroid Build Coastguard Worker  * header.
185*2d543d20SAndroid Build Coastguard Worker  */
186*2d543d20SAndroid Build Coastguard Worker #define HASH_BITS 16
187*2d543d20SAndroid Build Coastguard Worker #define HASH_BUCKETS (1 << HASH_BITS)
188*2d543d20SAndroid Build Coastguard Worker #define HASH_MASK (HASH_BUCKETS-1)
189*2d543d20SAndroid Build Coastguard Worker static file_spec_t *fl_head;
190*2d543d20SAndroid Build Coastguard Worker 
191*2d543d20SAndroid Build Coastguard Worker /*
192*2d543d20SAndroid Build Coastguard Worker  * Try to add an association between an inode and
193*2d543d20SAndroid Build Coastguard Worker  * a specification.  If there is already an association
194*2d543d20SAndroid Build Coastguard Worker  * for the inode and it conflicts with this specification,
195*2d543d20SAndroid Build Coastguard Worker  * then use the specification that occurs later in the
196*2d543d20SAndroid Build Coastguard Worker  * specification array.
197*2d543d20SAndroid Build Coastguard Worker  */
matchpathcon_filespec_add(ino_t ino,int specind,const char * file)198*2d543d20SAndroid Build Coastguard Worker int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
199*2d543d20SAndroid Build Coastguard Worker {
200*2d543d20SAndroid Build Coastguard Worker 	file_spec_t *prevfl, *fl;
201*2d543d20SAndroid Build Coastguard Worker 	int h, ret;
202*2d543d20SAndroid Build Coastguard Worker 	struct stat sb;
203*2d543d20SAndroid Build Coastguard Worker 
204*2d543d20SAndroid Build Coastguard Worker 	if (!fl_head) {
205*2d543d20SAndroid Build Coastguard Worker 		fl_head = malloc(sizeof(file_spec_t) * HASH_BUCKETS);
206*2d543d20SAndroid Build Coastguard Worker 		if (!fl_head)
207*2d543d20SAndroid Build Coastguard Worker 			goto oom;
208*2d543d20SAndroid Build Coastguard Worker 		memset(fl_head, 0, sizeof(file_spec_t) * HASH_BUCKETS);
209*2d543d20SAndroid Build Coastguard Worker 	}
210*2d543d20SAndroid Build Coastguard Worker 
211*2d543d20SAndroid Build Coastguard Worker 	h = (ino + (ino >> HASH_BITS)) & HASH_MASK;
212*2d543d20SAndroid Build Coastguard Worker 	for (prevfl = &fl_head[h], fl = fl_head[h].next; fl;
213*2d543d20SAndroid Build Coastguard Worker 	     prevfl = fl, fl = fl->next) {
214*2d543d20SAndroid Build Coastguard Worker 		if (ino == fl->ino) {
215*2d543d20SAndroid Build Coastguard Worker 			ret = lstat(fl->file, &sb);
216*2d543d20SAndroid Build Coastguard Worker 			if (ret < 0 || sb.st_ino != ino) {
217*2d543d20SAndroid Build Coastguard Worker 				fl->specind = specind;
218*2d543d20SAndroid Build Coastguard Worker 				free(fl->file);
219*2d543d20SAndroid Build Coastguard Worker 				fl->file = strdup(file);
220*2d543d20SAndroid Build Coastguard Worker 				if (!fl->file)
221*2d543d20SAndroid Build Coastguard Worker 					goto oom;
222*2d543d20SAndroid Build Coastguard Worker 				return fl->specind;
223*2d543d20SAndroid Build Coastguard Worker 
224*2d543d20SAndroid Build Coastguard Worker 			}
225*2d543d20SAndroid Build Coastguard Worker 
226*2d543d20SAndroid Build Coastguard Worker 			if (!strcmp(con_array[fl->specind],
227*2d543d20SAndroid Build Coastguard Worker 				    con_array[specind]))
228*2d543d20SAndroid Build Coastguard Worker 				return fl->specind;
229*2d543d20SAndroid Build Coastguard Worker 
230*2d543d20SAndroid Build Coastguard Worker 			myprintf
231*2d543d20SAndroid Build Coastguard Worker 			    ("%s:  conflicting specifications for %s and %s, using %s.\n",
232*2d543d20SAndroid Build Coastguard Worker 			     __FUNCTION__, file, fl->file,
233*2d543d20SAndroid Build Coastguard Worker 			     con_array[fl->specind]);
234*2d543d20SAndroid Build Coastguard Worker 			free(fl->file);
235*2d543d20SAndroid Build Coastguard Worker 			fl->file = strdup(file);
236*2d543d20SAndroid Build Coastguard Worker 			if (!fl->file)
237*2d543d20SAndroid Build Coastguard Worker 				goto oom;
238*2d543d20SAndroid Build Coastguard Worker 			return fl->specind;
239*2d543d20SAndroid Build Coastguard Worker 		}
240*2d543d20SAndroid Build Coastguard Worker 
241*2d543d20SAndroid Build Coastguard Worker 		if (ino > fl->ino)
242*2d543d20SAndroid Build Coastguard Worker 			break;
243*2d543d20SAndroid Build Coastguard Worker 	}
244*2d543d20SAndroid Build Coastguard Worker 
245*2d543d20SAndroid Build Coastguard Worker 	fl = malloc(sizeof(file_spec_t));
246*2d543d20SAndroid Build Coastguard Worker 	if (!fl)
247*2d543d20SAndroid Build Coastguard Worker 		goto oom;
248*2d543d20SAndroid Build Coastguard Worker 	fl->ino = ino;
249*2d543d20SAndroid Build Coastguard Worker 	fl->specind = specind;
250*2d543d20SAndroid Build Coastguard Worker 	fl->file = strdup(file);
251*2d543d20SAndroid Build Coastguard Worker 	if (!fl->file)
252*2d543d20SAndroid Build Coastguard Worker 		goto oom_freefl;
253*2d543d20SAndroid Build Coastguard Worker 	fl->next = prevfl->next;
254*2d543d20SAndroid Build Coastguard Worker 	prevfl->next = fl;
255*2d543d20SAndroid Build Coastguard Worker 	return fl->specind;
256*2d543d20SAndroid Build Coastguard Worker       oom_freefl:
257*2d543d20SAndroid Build Coastguard Worker 	free(fl);
258*2d543d20SAndroid Build Coastguard Worker       oom:
259*2d543d20SAndroid Build Coastguard Worker 	myprintf("%s:  insufficient memory for file label entry for %s\n",
260*2d543d20SAndroid Build Coastguard Worker 		 __FUNCTION__, file);
261*2d543d20SAndroid Build Coastguard Worker 	return -1;
262*2d543d20SAndroid Build Coastguard Worker }
263*2d543d20SAndroid Build Coastguard Worker 
264*2d543d20SAndroid Build Coastguard Worker /*
265*2d543d20SAndroid Build Coastguard Worker  * Evaluate the association hash table distribution.
266*2d543d20SAndroid Build Coastguard Worker  */
matchpathcon_filespec_eval(void)267*2d543d20SAndroid Build Coastguard Worker void matchpathcon_filespec_eval(void)
268*2d543d20SAndroid Build Coastguard Worker {
269*2d543d20SAndroid Build Coastguard Worker 	file_spec_t *fl;
270*2d543d20SAndroid Build Coastguard Worker 	int h, used, nel, len, longest;
271*2d543d20SAndroid Build Coastguard Worker 
272*2d543d20SAndroid Build Coastguard Worker 	if (!fl_head)
273*2d543d20SAndroid Build Coastguard Worker 		return;
274*2d543d20SAndroid Build Coastguard Worker 
275*2d543d20SAndroid Build Coastguard Worker 	used = 0;
276*2d543d20SAndroid Build Coastguard Worker 	longest = 0;
277*2d543d20SAndroid Build Coastguard Worker 	nel = 0;
278*2d543d20SAndroid Build Coastguard Worker 	for (h = 0; h < HASH_BUCKETS; h++) {
279*2d543d20SAndroid Build Coastguard Worker 		len = 0;
280*2d543d20SAndroid Build Coastguard Worker 		for (fl = fl_head[h].next; fl; fl = fl->next) {
281*2d543d20SAndroid Build Coastguard Worker 			len++;
282*2d543d20SAndroid Build Coastguard Worker 		}
283*2d543d20SAndroid Build Coastguard Worker 		if (len)
284*2d543d20SAndroid Build Coastguard Worker 			used++;
285*2d543d20SAndroid Build Coastguard Worker 		if (len > longest)
286*2d543d20SAndroid Build Coastguard Worker 			longest = len;
287*2d543d20SAndroid Build Coastguard Worker 		nel += len;
288*2d543d20SAndroid Build Coastguard Worker 	}
289*2d543d20SAndroid Build Coastguard Worker 
290*2d543d20SAndroid Build Coastguard Worker 	myprintf
291*2d543d20SAndroid Build Coastguard Worker 	    ("%s:  hash table stats: %d elements, %d/%d buckets used, longest chain length %d\n",
292*2d543d20SAndroid Build Coastguard Worker 	     __FUNCTION__, nel, used, HASH_BUCKETS, longest);
293*2d543d20SAndroid Build Coastguard Worker }
294*2d543d20SAndroid Build Coastguard Worker 
295*2d543d20SAndroid Build Coastguard Worker /*
296*2d543d20SAndroid Build Coastguard Worker  * Destroy the association hash table.
297*2d543d20SAndroid Build Coastguard Worker  */
matchpathcon_filespec_destroy(void)298*2d543d20SAndroid Build Coastguard Worker void matchpathcon_filespec_destroy(void)
299*2d543d20SAndroid Build Coastguard Worker {
300*2d543d20SAndroid Build Coastguard Worker 	file_spec_t *fl, *tmp;
301*2d543d20SAndroid Build Coastguard Worker 	int h;
302*2d543d20SAndroid Build Coastguard Worker 
303*2d543d20SAndroid Build Coastguard Worker 	free_array_elts();
304*2d543d20SAndroid Build Coastguard Worker 
305*2d543d20SAndroid Build Coastguard Worker 	if (!fl_head)
306*2d543d20SAndroid Build Coastguard Worker 		return;
307*2d543d20SAndroid Build Coastguard Worker 
308*2d543d20SAndroid Build Coastguard Worker 	for (h = 0; h < HASH_BUCKETS; h++) {
309*2d543d20SAndroid Build Coastguard Worker 		fl = fl_head[h].next;
310*2d543d20SAndroid Build Coastguard Worker 		while (fl) {
311*2d543d20SAndroid Build Coastguard Worker 			tmp = fl;
312*2d543d20SAndroid Build Coastguard Worker 			fl = fl->next;
313*2d543d20SAndroid Build Coastguard Worker 			free(tmp->file);
314*2d543d20SAndroid Build Coastguard Worker 			free(tmp);
315*2d543d20SAndroid Build Coastguard Worker 		}
316*2d543d20SAndroid Build Coastguard Worker 		fl_head[h].next = NULL;
317*2d543d20SAndroid Build Coastguard Worker 	}
318*2d543d20SAndroid Build Coastguard Worker 	free(fl_head);
319*2d543d20SAndroid Build Coastguard Worker 	fl_head = NULL;
320*2d543d20SAndroid Build Coastguard Worker }
321*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_fini_internal(void)322*2d543d20SAndroid Build Coastguard Worker static void matchpathcon_fini_internal(void)
323*2d543d20SAndroid Build Coastguard Worker {
324*2d543d20SAndroid Build Coastguard Worker 	free_array_elts();
325*2d543d20SAndroid Build Coastguard Worker 
326*2d543d20SAndroid Build Coastguard Worker 	if (hnd) {
327*2d543d20SAndroid Build Coastguard Worker 		selabel_close(hnd);
328*2d543d20SAndroid Build Coastguard Worker 		hnd = NULL;
329*2d543d20SAndroid Build Coastguard Worker 	}
330*2d543d20SAndroid Build Coastguard Worker }
331*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_thread_destructor(void * ptr)332*2d543d20SAndroid Build Coastguard Worker static void matchpathcon_thread_destructor(void __attribute__((unused)) *ptr)
333*2d543d20SAndroid Build Coastguard Worker {
334*2d543d20SAndroid Build Coastguard Worker 	matchpathcon_fini_internal();
335*2d543d20SAndroid Build Coastguard Worker }
336*2d543d20SAndroid Build Coastguard Worker 
337*2d543d20SAndroid Build Coastguard Worker void __attribute__((destructor)) matchpathcon_lib_destructor(void);
338*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_lib_destructor(void)339*2d543d20SAndroid Build Coastguard Worker void  __attribute__((destructor)) matchpathcon_lib_destructor(void)
340*2d543d20SAndroid Build Coastguard Worker {
341*2d543d20SAndroid Build Coastguard Worker 	if (destructor_key_initialized)
342*2d543d20SAndroid Build Coastguard Worker 		__selinux_key_delete(destructor_key);
343*2d543d20SAndroid Build Coastguard Worker }
344*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_init_once(void)345*2d543d20SAndroid Build Coastguard Worker static void matchpathcon_init_once(void)
346*2d543d20SAndroid Build Coastguard Worker {
347*2d543d20SAndroid Build Coastguard Worker 	if (__selinux_key_create(&destructor_key, matchpathcon_thread_destructor) == 0)
348*2d543d20SAndroid Build Coastguard Worker 		destructor_key_initialized = 1;
349*2d543d20SAndroid Build Coastguard Worker }
350*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_init_prefix(const char * path,const char * subset)351*2d543d20SAndroid Build Coastguard Worker int matchpathcon_init_prefix(const char *path, const char *subset)
352*2d543d20SAndroid Build Coastguard Worker {
353*2d543d20SAndroid Build Coastguard Worker 	if (!mycanoncon)
354*2d543d20SAndroid Build Coastguard Worker 		mycanoncon = default_canoncon;
355*2d543d20SAndroid Build Coastguard Worker 
356*2d543d20SAndroid Build Coastguard Worker 	__selinux_once(once, matchpathcon_init_once);
357*2d543d20SAndroid Build Coastguard Worker 	__selinux_setspecific(destructor_key, /* some valid address to please GCC */ &selinux_page_size);
358*2d543d20SAndroid Build Coastguard Worker 
359*2d543d20SAndroid Build Coastguard Worker 	options[SELABEL_OPT_SUBSET].type = SELABEL_OPT_SUBSET;
360*2d543d20SAndroid Build Coastguard Worker 	options[SELABEL_OPT_SUBSET].value = subset;
361*2d543d20SAndroid Build Coastguard Worker 	options[SELABEL_OPT_PATH].type = SELABEL_OPT_PATH;
362*2d543d20SAndroid Build Coastguard Worker 	options[SELABEL_OPT_PATH].value = path;
363*2d543d20SAndroid Build Coastguard Worker 
364*2d543d20SAndroid Build Coastguard Worker 	hnd = selabel_open(SELABEL_CTX_FILE, options, SELABEL_NOPT);
365*2d543d20SAndroid Build Coastguard Worker 	return hnd ? 0 : -1;
366*2d543d20SAndroid Build Coastguard Worker }
367*2d543d20SAndroid Build Coastguard Worker 
368*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_init(const char * path)369*2d543d20SAndroid Build Coastguard Worker int matchpathcon_init(const char *path)
370*2d543d20SAndroid Build Coastguard Worker {
371*2d543d20SAndroid Build Coastguard Worker 	return matchpathcon_init_prefix(path, NULL);
372*2d543d20SAndroid Build Coastguard Worker }
373*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_fini(void)374*2d543d20SAndroid Build Coastguard Worker void matchpathcon_fini(void)
375*2d543d20SAndroid Build Coastguard Worker {
376*2d543d20SAndroid Build Coastguard Worker 	matchpathcon_fini_internal();
377*2d543d20SAndroid Build Coastguard Worker }
378*2d543d20SAndroid Build Coastguard Worker 
379*2d543d20SAndroid Build Coastguard Worker /*
380*2d543d20SAndroid Build Coastguard Worker  * We do not want to resolve a symlink to a real path if it is the final
381*2d543d20SAndroid Build Coastguard Worker  * component of the name.  Thus we split the pathname on the last "/" and
382*2d543d20SAndroid Build Coastguard Worker  * determine a real path component of the first portion.  We then have to
383*2d543d20SAndroid Build Coastguard Worker  * copy the last part back on to get the final real path.  Wheww.
384*2d543d20SAndroid Build Coastguard Worker  */
realpath_not_final(const char * name,char * resolved_path)385*2d543d20SAndroid Build Coastguard Worker int realpath_not_final(const char *name, char *resolved_path)
386*2d543d20SAndroid Build Coastguard Worker {
387*2d543d20SAndroid Build Coastguard Worker 	char *last_component;
388*2d543d20SAndroid Build Coastguard Worker 	char *tmp_path, *p;
389*2d543d20SAndroid Build Coastguard Worker 	size_t len = 0;
390*2d543d20SAndroid Build Coastguard Worker 	int rc = 0;
391*2d543d20SAndroid Build Coastguard Worker 
392*2d543d20SAndroid Build Coastguard Worker 	tmp_path = strdup(name);
393*2d543d20SAndroid Build Coastguard Worker 	if (!tmp_path) {
394*2d543d20SAndroid Build Coastguard Worker 		myprintf("symlink_realpath(%s) strdup() failed: %m\n",
395*2d543d20SAndroid Build Coastguard Worker 			name);
396*2d543d20SAndroid Build Coastguard Worker 		rc = -1;
397*2d543d20SAndroid Build Coastguard Worker 		goto out;
398*2d543d20SAndroid Build Coastguard Worker 	}
399*2d543d20SAndroid Build Coastguard Worker 
400*2d543d20SAndroid Build Coastguard Worker 	last_component = strrchr(tmp_path, '/');
401*2d543d20SAndroid Build Coastguard Worker 
402*2d543d20SAndroid Build Coastguard Worker 	if (last_component == tmp_path) {
403*2d543d20SAndroid Build Coastguard Worker 		last_component++;
404*2d543d20SAndroid Build Coastguard Worker 		p = strcpy(resolved_path, "");
405*2d543d20SAndroid Build Coastguard Worker 	} else if (last_component) {
406*2d543d20SAndroid Build Coastguard Worker 		*last_component = '\0';
407*2d543d20SAndroid Build Coastguard Worker 		last_component++;
408*2d543d20SAndroid Build Coastguard Worker 		p = realpath(tmp_path, resolved_path);
409*2d543d20SAndroid Build Coastguard Worker 	} else {
410*2d543d20SAndroid Build Coastguard Worker 		last_component = tmp_path;
411*2d543d20SAndroid Build Coastguard Worker 		p = realpath("./", resolved_path);
412*2d543d20SAndroid Build Coastguard Worker 	}
413*2d543d20SAndroid Build Coastguard Worker 
414*2d543d20SAndroid Build Coastguard Worker 	if (!p) {
415*2d543d20SAndroid Build Coastguard Worker 		myprintf("symlink_realpath(%s) realpath() failed: %m\n",
416*2d543d20SAndroid Build Coastguard Worker 			name);
417*2d543d20SAndroid Build Coastguard Worker 		rc = -1;
418*2d543d20SAndroid Build Coastguard Worker 		goto out;
419*2d543d20SAndroid Build Coastguard Worker 	}
420*2d543d20SAndroid Build Coastguard Worker 
421*2d543d20SAndroid Build Coastguard Worker 	len = strlen(p);
422*2d543d20SAndroid Build Coastguard Worker 	if (len + strlen(last_component) + 2 > PATH_MAX) {
423*2d543d20SAndroid Build Coastguard Worker 		myprintf("symlink_realpath(%s) failed: Filename too long \n",
424*2d543d20SAndroid Build Coastguard Worker 			name);
425*2d543d20SAndroid Build Coastguard Worker 		errno = ENAMETOOLONG;
426*2d543d20SAndroid Build Coastguard Worker 		rc = -1;
427*2d543d20SAndroid Build Coastguard Worker 		goto out;
428*2d543d20SAndroid Build Coastguard Worker 	}
429*2d543d20SAndroid Build Coastguard Worker 
430*2d543d20SAndroid Build Coastguard Worker 	resolved_path += len;
431*2d543d20SAndroid Build Coastguard Worker 	strcpy(resolved_path, "/");
432*2d543d20SAndroid Build Coastguard Worker 	resolved_path += 1;
433*2d543d20SAndroid Build Coastguard Worker 	strcpy(resolved_path, last_component);
434*2d543d20SAndroid Build Coastguard Worker out:
435*2d543d20SAndroid Build Coastguard Worker 	free(tmp_path);
436*2d543d20SAndroid Build Coastguard Worker 	return rc;
437*2d543d20SAndroid Build Coastguard Worker }
438*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_internal(const char * path,mode_t mode,char ** con)439*2d543d20SAndroid Build Coastguard Worker static int matchpathcon_internal(const char *path, mode_t mode, char ** con)
440*2d543d20SAndroid Build Coastguard Worker {
441*2d543d20SAndroid Build Coastguard Worker 	char stackpath[PATH_MAX + 1];
442*2d543d20SAndroid Build Coastguard Worker 	char *p = NULL;
443*2d543d20SAndroid Build Coastguard Worker 	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
444*2d543d20SAndroid Build Coastguard Worker 			return -1;
445*2d543d20SAndroid Build Coastguard Worker 
446*2d543d20SAndroid Build Coastguard Worker 	if (S_ISLNK(mode)) {
447*2d543d20SAndroid Build Coastguard Worker 		if (!realpath_not_final(path, stackpath))
448*2d543d20SAndroid Build Coastguard Worker 			path = stackpath;
449*2d543d20SAndroid Build Coastguard Worker 	} else {
450*2d543d20SAndroid Build Coastguard Worker 		p = realpath(path, stackpath);
451*2d543d20SAndroid Build Coastguard Worker 		if (p)
452*2d543d20SAndroid Build Coastguard Worker 			path = p;
453*2d543d20SAndroid Build Coastguard Worker 	}
454*2d543d20SAndroid Build Coastguard Worker 
455*2d543d20SAndroid Build Coastguard Worker 	return notrans ?
456*2d543d20SAndroid Build Coastguard Worker 		selabel_lookup_raw(hnd, con, path, mode) :
457*2d543d20SAndroid Build Coastguard Worker 		selabel_lookup(hnd, con, path, mode);
458*2d543d20SAndroid Build Coastguard Worker }
459*2d543d20SAndroid Build Coastguard Worker 
matchpathcon(const char * path,mode_t mode,char ** con)460*2d543d20SAndroid Build Coastguard Worker int matchpathcon(const char *path, mode_t mode, char ** con) {
461*2d543d20SAndroid Build Coastguard Worker 	return matchpathcon_internal(path, mode, con);
462*2d543d20SAndroid Build Coastguard Worker }
463*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_index(const char * name,mode_t mode,char ** con)464*2d543d20SAndroid Build Coastguard Worker int matchpathcon_index(const char *name, mode_t mode, char ** con)
465*2d543d20SAndroid Build Coastguard Worker {
466*2d543d20SAndroid Build Coastguard Worker 	int i = matchpathcon_internal(name, mode, con);
467*2d543d20SAndroid Build Coastguard Worker 
468*2d543d20SAndroid Build Coastguard Worker 	if (i < 0)
469*2d543d20SAndroid Build Coastguard Worker 		return -1;
470*2d543d20SAndroid Build Coastguard Worker 
471*2d543d20SAndroid Build Coastguard Worker 	return add_array_elt(*con);
472*2d543d20SAndroid Build Coastguard Worker }
473*2d543d20SAndroid Build Coastguard Worker 
matchpathcon_checkmatches(char * str)474*2d543d20SAndroid Build Coastguard Worker void matchpathcon_checkmatches(char *str __attribute__((unused)))
475*2d543d20SAndroid Build Coastguard Worker {
476*2d543d20SAndroid Build Coastguard Worker 	selabel_stats(hnd);
477*2d543d20SAndroid Build Coastguard Worker }
478*2d543d20SAndroid Build Coastguard Worker 
479*2d543d20SAndroid Build Coastguard Worker /* Compare two contexts to see if their differences are "significant",
480*2d543d20SAndroid Build Coastguard Worker  * or whether the only difference is in the user. */
selinux_file_context_cmp(const char * a,const char * b)481*2d543d20SAndroid Build Coastguard Worker int selinux_file_context_cmp(const char * a,
482*2d543d20SAndroid Build Coastguard Worker 			     const char * b)
483*2d543d20SAndroid Build Coastguard Worker {
484*2d543d20SAndroid Build Coastguard Worker 	const char *rest_a, *rest_b;	/* Rest of the context after the user */
485*2d543d20SAndroid Build Coastguard Worker 	if (!a && !b)
486*2d543d20SAndroid Build Coastguard Worker 		return 0;
487*2d543d20SAndroid Build Coastguard Worker 	if (!a)
488*2d543d20SAndroid Build Coastguard Worker 		return -1;
489*2d543d20SAndroid Build Coastguard Worker 	if (!b)
490*2d543d20SAndroid Build Coastguard Worker 		return 1;
491*2d543d20SAndroid Build Coastguard Worker 	rest_a = strchr(a, ':');
492*2d543d20SAndroid Build Coastguard Worker 	rest_b = strchr(b, ':');
493*2d543d20SAndroid Build Coastguard Worker 	if (!rest_a && !rest_b)
494*2d543d20SAndroid Build Coastguard Worker 		return 0;
495*2d543d20SAndroid Build Coastguard Worker 	if (!rest_a)
496*2d543d20SAndroid Build Coastguard Worker 		return -1;
497*2d543d20SAndroid Build Coastguard Worker 	if (!rest_b)
498*2d543d20SAndroid Build Coastguard Worker 		return 1;
499*2d543d20SAndroid Build Coastguard Worker 	return strcmp(rest_a, rest_b);
500*2d543d20SAndroid Build Coastguard Worker }
501*2d543d20SAndroid Build Coastguard Worker 
selinux_file_context_verify(const char * path,mode_t mode)502*2d543d20SAndroid Build Coastguard Worker int selinux_file_context_verify(const char *path, mode_t mode)
503*2d543d20SAndroid Build Coastguard Worker {
504*2d543d20SAndroid Build Coastguard Worker 	char * con = NULL;
505*2d543d20SAndroid Build Coastguard Worker 	char * fcontext = NULL;
506*2d543d20SAndroid Build Coastguard Worker 	int rc = 0;
507*2d543d20SAndroid Build Coastguard Worker 	char stackpath[PATH_MAX + 1];
508*2d543d20SAndroid Build Coastguard Worker 	char *p = NULL;
509*2d543d20SAndroid Build Coastguard Worker 
510*2d543d20SAndroid Build Coastguard Worker 	if (S_ISLNK(mode)) {
511*2d543d20SAndroid Build Coastguard Worker 		if (!realpath_not_final(path, stackpath))
512*2d543d20SAndroid Build Coastguard Worker 			path = stackpath;
513*2d543d20SAndroid Build Coastguard Worker 	} else {
514*2d543d20SAndroid Build Coastguard Worker 		p = realpath(path, stackpath);
515*2d543d20SAndroid Build Coastguard Worker 		if (p)
516*2d543d20SAndroid Build Coastguard Worker 			path = p;
517*2d543d20SAndroid Build Coastguard Worker 	}
518*2d543d20SAndroid Build Coastguard Worker 
519*2d543d20SAndroid Build Coastguard Worker 	rc = lgetfilecon_raw(path, &con);
520*2d543d20SAndroid Build Coastguard Worker 	if (rc == -1) {
521*2d543d20SAndroid Build Coastguard Worker 		if (errno != ENOTSUP)
522*2d543d20SAndroid Build Coastguard Worker 			return -1;
523*2d543d20SAndroid Build Coastguard Worker 		else
524*2d543d20SAndroid Build Coastguard Worker 			return 0;
525*2d543d20SAndroid Build Coastguard Worker 	}
526*2d543d20SAndroid Build Coastguard Worker 
527*2d543d20SAndroid Build Coastguard Worker 	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0)){
528*2d543d20SAndroid Build Coastguard Worker 			freecon(con);
529*2d543d20SAndroid Build Coastguard Worker 			return -1;
530*2d543d20SAndroid Build Coastguard Worker 	}
531*2d543d20SAndroid Build Coastguard Worker 
532*2d543d20SAndroid Build Coastguard Worker 	if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
533*2d543d20SAndroid Build Coastguard Worker 		if (errno != ENOENT)
534*2d543d20SAndroid Build Coastguard Worker 			rc = -1;
535*2d543d20SAndroid Build Coastguard Worker 		else
536*2d543d20SAndroid Build Coastguard Worker 			rc = 0;
537*2d543d20SAndroid Build Coastguard Worker 	} else {
538*2d543d20SAndroid Build Coastguard Worker 		/*
539*2d543d20SAndroid Build Coastguard Worker 		 * Need to set errno to 0 as it can be set to ENOENT if the
540*2d543d20SAndroid Build Coastguard Worker 		 * file_contexts.subs file does not exist (see selabel_open in
541*2d543d20SAndroid Build Coastguard Worker 		 * label.c), thus causing confusion if errno is checked on return.
542*2d543d20SAndroid Build Coastguard Worker 		 */
543*2d543d20SAndroid Build Coastguard Worker 		errno = 0;
544*2d543d20SAndroid Build Coastguard Worker 		rc = (selinux_file_context_cmp(fcontext, con) == 0);
545*2d543d20SAndroid Build Coastguard Worker 	}
546*2d543d20SAndroid Build Coastguard Worker 
547*2d543d20SAndroid Build Coastguard Worker 	freecon(con);
548*2d543d20SAndroid Build Coastguard Worker 	freecon(fcontext);
549*2d543d20SAndroid Build Coastguard Worker 	return rc;
550*2d543d20SAndroid Build Coastguard Worker }
551*2d543d20SAndroid Build Coastguard Worker 
selinux_lsetfilecon_default(const char * path)552*2d543d20SAndroid Build Coastguard Worker int selinux_lsetfilecon_default(const char *path)
553*2d543d20SAndroid Build Coastguard Worker {
554*2d543d20SAndroid Build Coastguard Worker 	struct stat st;
555*2d543d20SAndroid Build Coastguard Worker 	int rc = -1;
556*2d543d20SAndroid Build Coastguard Worker 	char * scontext = NULL;
557*2d543d20SAndroid Build Coastguard Worker 	if (lstat(path, &st) != 0)
558*2d543d20SAndroid Build Coastguard Worker 		return rc;
559*2d543d20SAndroid Build Coastguard Worker 
560*2d543d20SAndroid Build Coastguard Worker 	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
561*2d543d20SAndroid Build Coastguard Worker 			return -1;
562*2d543d20SAndroid Build Coastguard Worker 
563*2d543d20SAndroid Build Coastguard Worker 	/* If there's an error determining the context, or it has none,
564*2d543d20SAndroid Build Coastguard Worker 	   return to allow default context */
565*2d543d20SAndroid Build Coastguard Worker 	if (selabel_lookup_raw(hnd, &scontext, path, st.st_mode)) {
566*2d543d20SAndroid Build Coastguard Worker 		if (errno == ENOENT)
567*2d543d20SAndroid Build Coastguard Worker 			rc = 0;
568*2d543d20SAndroid Build Coastguard Worker 	} else {
569*2d543d20SAndroid Build Coastguard Worker 		rc = lsetfilecon_raw(path, scontext);
570*2d543d20SAndroid Build Coastguard Worker 		freecon(scontext);
571*2d543d20SAndroid Build Coastguard Worker 	}
572*2d543d20SAndroid Build Coastguard Worker 	return rc;
573*2d543d20SAndroid Build Coastguard Worker }
574*2d543d20SAndroid Build Coastguard Worker 
575*2d543d20SAndroid Build Coastguard Worker #endif
576