• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Property Service contexts backend for labeling Android
3  * property keys
4  */
5 
6 #include <stdarg.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <limits.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include "callbacks.h"
14 #include "label_internal.h"
15 
16 /* A property security context specification. */
17 typedef struct spec {
18 	struct selabel_lookup_rec lr;	/* holds contexts for lookup result */
19 	char *property_key;		/* property key string */
20 } spec_t;
21 
22 /* Our stored configuration */
23 struct saved_data {
24 	/*
25 	 * The array of specifications is sorted for longest
26 	 * prefix match
27 	 */
28 	spec_t *spec_arr;
29 	unsigned int nspec;	/* total number of specifications */
30 };
31 
cmp(const void * A,const void * B)32 static int cmp(const void *A, const void *B)
33 {
34 	const struct spec *sp1 = A, *sp2 = B;
35 
36 	if (strncmp(sp1->property_key, "*", 1) == 0)
37 		return 1;
38 	if (strncmp(sp2->property_key, "*", 1) == 0)
39 		return -1;
40 
41 	size_t L1 = strlen(sp1->property_key);
42 	size_t L2 = strlen(sp2->property_key);
43 
44 	return (L1 < L2) - (L1 > L2);
45 }
46 
47 /*
48  * Warn about duplicate specifications.
49  */
nodups_specs(struct saved_data * data,const char * path)50 static int nodups_specs(struct saved_data *data, const char *path)
51 {
52 	int rc = 0;
53 	unsigned int ii, jj;
54 	struct spec *curr_spec, *spec_arr = data->spec_arr;
55 
56 	for (ii = 0; ii < data->nspec; ii++) {
57 		curr_spec = &spec_arr[ii];
58 		for (jj = ii + 1; jj < data->nspec; jj++) {
59 			if (!strcmp(spec_arr[jj].property_key,
60 					    curr_spec->property_key)) {
61 				rc = -1;
62 				errno = EINVAL;
63 				if (strcmp(spec_arr[jj].lr.ctx_raw,
64 						    curr_spec->lr.ctx_raw)) {
65 					selinux_log
66 						(SELINUX_ERROR,
67 						 "%s: Multiple different specifications for %s  (%s and %s).\n",
68 						 path, curr_spec->property_key,
69 						 spec_arr[jj].lr.ctx_raw,
70 						 curr_spec->lr.ctx_raw);
71 				} else {
72 					selinux_log
73 						(SELINUX_ERROR,
74 						 "%s: Multiple same specifications for %s.\n",
75 						 path, curr_spec->property_key);
76 				}
77 			}
78 		}
79 	}
80 	return rc;
81 }
82 
process_line(struct selabel_handle * rec,const char * path,char * line_buf,int pass,unsigned lineno)83 static int process_line(struct selabel_handle *rec,
84 			const char *path, char *line_buf,
85 			int pass, unsigned lineno)
86 {
87 	int items;
88 	char *prop = NULL, *context = NULL;
89 	struct saved_data *data = (struct saved_data *)rec->data;
90 	spec_t *spec_arr = data->spec_arr;
91 	unsigned int nspec = data->nspec;
92 	const char *errbuf = NULL;
93 
94 	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
95 	if (items < 0) {
96 		items = errno;
97 		if (errbuf) {
98 			selinux_log(SELINUX_ERROR,
99 				    "%s:  line %u error due to: %s\n", path,
100 				    lineno, errbuf);
101 		} else {
102 			selinux_log(SELINUX_ERROR,
103 				    "%s:  line %u error due to: %m\n", path,
104 				    lineno);
105 		}
106 		errno = items;
107 		return -1;
108 	}
109 
110 	if (items == 0)
111 		return items;
112 
113 	if (items != 2) {
114 		selinux_log(SELINUX_ERROR,
115 			    "%s:  line %u is missing fields\n", path,
116 			    lineno);
117 		free(prop);
118 		errno = EINVAL;
119 		return -1;
120 	}
121 
122 	if (pass == 0) {
123 		free(prop);
124 		free(context);
125 	} else if (pass == 1) {
126 		/* On the second pass, process and store the specification in spec. */
127 		spec_arr[nspec].property_key = prop;
128 		spec_arr[nspec].lr.ctx_raw = context;
129 
130 		if (rec->validating) {
131 			if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) {
132 				selinux_log(SELINUX_ERROR,
133 					    "%s:  line %u has invalid context %s\n",
134 					    path, lineno, spec_arr[nspec].lr.ctx_raw);
135 				errno = EINVAL;
136 				return -1;
137 			}
138 		}
139 	}
140 
141 	data->nspec = ++nspec;
142 	return 0;
143 }
144 
init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned n)145 static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
146 		unsigned n)
147 {
148 	struct saved_data *data = (struct saved_data *)rec->data;
149 	const char *path = NULL;
150 	FILE *fp;
151 	char line_buf[BUFSIZ];
152 	unsigned int lineno, maxnspec, pass;
153 	int status = -1;
154 	struct stat sb;
155 
156 	/* Process arguments */
157 	while (n--)
158 		switch (opts[n].type) {
159 		case SELABEL_OPT_PATH:
160 			path = opts[n].value;
161 			break;
162 		}
163 
164 	if (!path)
165 		return -1;
166 
167 	/* Open the specification file. */
168 	if ((fp = fopen(path, "re")) == NULL)
169 		return -1;
170 
171 	if (fstat(fileno(fp), &sb) < 0)
172 		goto finish;
173 	errno = EINVAL;
174 	if (!S_ISREG(sb.st_mode))
175 		goto finish;
176 
177 	/*
178 	 * Two passes of the specification file. First is to get the size.
179 	 * After the first pass, the spec array is malloced to the appropriate
180 	 * size. Second pass is to populate the spec array and check for
181 	 * dups.
182 	 */
183 	maxnspec = UINT_MAX / sizeof(spec_t);
184 	for (pass = 0; pass < 2; pass++) {
185 		data->nspec = 0;
186 		lineno = 0;
187 
188 		while (fgets(line_buf, sizeof(line_buf) - 1, fp)
189 		       && data->nspec < maxnspec) {
190 			if (process_line(rec, path, line_buf, pass, ++lineno)
191 									  != 0)
192 				goto finish;
193 		}
194 
195 		if (pass == 1) {
196 			status = nodups_specs(data, path);
197 
198 			if (status)
199 				goto finish;
200 		}
201 
202 		if (pass == 0) {
203 			if (data->nspec == 0) {
204 				status = 0;
205 				goto finish;
206 			}
207 
208 			if (NULL == (data->spec_arr =
209 				     calloc(data->nspec, sizeof(spec_t))))
210 				goto finish;
211 
212 			maxnspec = data->nspec;
213 			rewind(fp);
214 		}
215 	}
216 
217 	qsort(data->spec_arr, data->nspec, sizeof(struct spec), cmp);
218 
219 	status = digest_add_specfile(rec->digest, fp, NULL, sb.st_size, path);
220 	if (status)
221 		goto finish;
222 
223 	digest_gen_hash(rec->digest);
224 
225 finish:
226 	fclose(fp);
227 	return status;
228 }
229 
230 /*
231  * Backend interface routines
232  */
closef(struct selabel_handle * rec)233 static void closef(struct selabel_handle *rec)
234 {
235 	struct saved_data *data = (struct saved_data *)rec->data;
236 	struct spec *spec;
237 	unsigned int i;
238 
239 	for (i = 0; i < data->nspec; i++) {
240 		spec = &data->spec_arr[i];
241 		free(spec->property_key);
242 		free(spec->lr.ctx_raw);
243 		free(spec->lr.ctx_trans);
244 	}
245 
246 	if (data->spec_arr)
247 		free(data->spec_arr);
248 
249 	free(data);
250 }
251 
property_lookup(struct selabel_handle * rec,const char * key,int type)252 static struct selabel_lookup_rec *property_lookup(struct selabel_handle *rec,
253 					 const char *key,
254 					 int __attribute__((unused)) type)
255 {
256 	struct saved_data *data = (struct saved_data *)rec->data;
257 	spec_t *spec_arr = data->spec_arr;
258 	unsigned int i;
259 	struct selabel_lookup_rec *ret = NULL;
260 
261 	if (!data->nspec) {
262 		errno = ENOENT;
263 		goto finish;
264 	}
265 
266 	for (i = 0; i < data->nspec; i++) {
267 		if (strncmp(spec_arr[i].property_key, key,
268 			    strlen(spec_arr[i].property_key)) == 0) {
269 			break;
270 		}
271 		if (strncmp(spec_arr[i].property_key, "*", 1) == 0)
272 			break;
273 	}
274 
275 	if (i >= data->nspec) {
276 		/* No matching specification. */
277 		errno = ENOENT;
278 		goto finish;
279 	}
280 
281 	ret = &spec_arr[i].lr;
282 
283 finish:
284 	return ret;
285 }
286 
service_lookup(struct selabel_handle * rec,const char * key,int type)287 static struct selabel_lookup_rec *service_lookup(struct selabel_handle *rec,
288 		const char *key, int __attribute__((unused)) type)
289 {
290 	struct saved_data *data = (struct saved_data *)rec->data;
291 	spec_t *spec_arr = data->spec_arr;
292 	unsigned int i;
293 	struct selabel_lookup_rec *ret = NULL;
294 
295 	if (!data->nspec) {
296 		errno = ENOENT;
297 		goto finish;
298 	}
299 
300 	for (i = 0; i < data->nspec; i++) {
301 		if (strcmp(spec_arr[i].property_key, key) == 0)
302 			break;
303 		if (strcmp(spec_arr[i].property_key, "*") == 0)
304 			break;
305 	}
306 
307 	if (i >= data->nspec) {
308 		/* No matching specification. */
309 		errno = ENOENT;
310 		goto finish;
311 	}
312 
313 	ret = &spec_arr[i].lr;
314 
315 finish:
316 	return ret;
317 }
318 
stats(struct selabel_handle * rec)319 static void stats(struct selabel_handle __attribute__((unused)) *rec)
320 {
321 	selinux_log(SELINUX_WARNING, "'stats' functionality not implemented.\n");
322 }
323 
selabel_property_init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned nopts)324 int selabel_property_init(struct selabel_handle *rec,
325 			  const struct selinux_opt *opts,
326 			  unsigned nopts)
327 {
328 	struct saved_data *data;
329 
330 	data = (struct saved_data *)calloc(1, sizeof(*data));
331 	if (!data)
332 		return -1;
333 
334 	rec->data = data;
335 	rec->func_close = &closef;
336 	rec->func_stats = &stats;
337 	rec->func_lookup = &property_lookup;
338 
339 	return init(rec, opts, nopts);
340 }
341 
selabel_service_init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned nopts)342 int selabel_service_init(struct selabel_handle *rec,
343 		const struct selinux_opt *opts, unsigned nopts)
344 {
345 	struct saved_data *data;
346 
347 	data = (struct saved_data *)calloc(1, sizeof(*data));
348 	if (!data)
349 		return -1;
350 
351 	rec->data = data;
352 	rec->func_close = &closef;
353 	rec->func_stats = &stats;
354 	rec->func_lookup = &service_lookup;
355 
356 	return init(rec, opts, nopts);
357 }
358