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
93 items = read_spec_entries(line_buf, 2, &prop, &context);
94 if (items <= 0)
95 return items;
96 if (items != 2) {
97 selinux_log(SELINUX_ERROR,
98 "%s: line %u is missing fields\n", path,
99 lineno);
100 free(prop);
101 errno = EINVAL;
102 return -1;
103 }
104
105 if (pass == 0) {
106 free(prop);
107 free(context);
108 } else if (pass == 1) {
109 /* On the second pass, process and store the specification in spec. */
110 spec_arr[nspec].property_key = prop;
111 spec_arr[nspec].lr.ctx_raw = context;
112
113 if (rec->validating) {
114 if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) {
115 selinux_log(SELINUX_ERROR,
116 "%s: line %u has invalid context %s\n",
117 path, lineno, spec_arr[nspec].lr.ctx_raw);
118 errno = EINVAL;
119 return -1;
120 }
121 }
122 }
123
124 data->nspec = ++nspec;
125 return 0;
126 }
127
init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned n)128 static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
129 unsigned n)
130 {
131 struct saved_data *data = (struct saved_data *)rec->data;
132 const char *path = NULL;
133 FILE *fp;
134 char line_buf[BUFSIZ];
135 unsigned int lineno, maxnspec, pass;
136 int status = -1;
137 struct stat sb;
138
139 /* Process arguments */
140 while (n--)
141 switch (opts[n].type) {
142 case SELABEL_OPT_PATH:
143 path = opts[n].value;
144 break;
145 }
146
147 if (!path)
148 return -1;
149
150 /* Open the specification file. */
151 if ((fp = fopen(path, "r")) == NULL)
152 return -1;
153
154 if (fstat(fileno(fp), &sb) < 0)
155 goto finish;
156 errno = EINVAL;
157 if (!S_ISREG(sb.st_mode))
158 goto finish;
159
160 /*
161 * Two passes of the specification file. First is to get the size.
162 * After the first pass, the spec array is malloced to the appropriate
163 * size. Second pass is to populate the spec array and check for
164 * dups.
165 */
166 maxnspec = UINT_MAX / sizeof(spec_t);
167 for (pass = 0; pass < 2; pass++) {
168 data->nspec = 0;
169 lineno = 0;
170
171 while (fgets(line_buf, sizeof(line_buf) - 1, fp)
172 && data->nspec < maxnspec) {
173 if (process_line(rec, path, line_buf, pass, ++lineno)
174 != 0)
175 goto finish;
176 }
177
178 if (pass == 1) {
179 status = nodups_specs(data, path);
180
181 if (status)
182 goto finish;
183 }
184
185 if (pass == 0) {
186 if (data->nspec == 0) {
187 status = 0;
188 goto finish;
189 }
190
191 if (NULL == (data->spec_arr =
192 malloc(sizeof(spec_t) * data->nspec)))
193 goto finish;
194
195 memset(data->spec_arr, 0, sizeof(spec_t) * data->nspec);
196 maxnspec = data->nspec;
197 rewind(fp);
198 }
199 }
200
201 qsort(data->spec_arr, data->nspec, sizeof(struct spec), cmp);
202
203 status = digest_add_specfile(rec->digest, fp, NULL, sb.st_size, path);
204 if (status)
205 goto finish;
206
207 digest_gen_hash(rec->digest);
208
209 finish:
210 fclose(fp);
211 return status;
212 }
213
214 /*
215 * Backend interface routines
216 */
closef(struct selabel_handle * rec)217 static void closef(struct selabel_handle *rec)
218 {
219 struct saved_data *data = (struct saved_data *)rec->data;
220 struct spec *spec;
221 unsigned int i;
222
223 for (i = 0; i < data->nspec; i++) {
224 spec = &data->spec_arr[i];
225 free(spec->property_key);
226 free(spec->lr.ctx_raw);
227 free(spec->lr.ctx_trans);
228 }
229
230 if (data->spec_arr)
231 free(data->spec_arr);
232
233 free(data);
234 }
235
lookup(struct selabel_handle * rec,const char * key,int type)236 static struct selabel_lookup_rec *lookup(struct selabel_handle *rec,
237 const char *key,
238 int __attribute__((unused)) type)
239 {
240 struct saved_data *data = (struct saved_data *)rec->data;
241 spec_t *spec_arr = data->spec_arr;
242 unsigned int i;
243 struct selabel_lookup_rec *ret = NULL;
244
245 if (!data->nspec) {
246 errno = ENOENT;
247 goto finish;
248 }
249
250 for (i = 0; i < data->nspec; i++) {
251 if (strncmp(spec_arr[i].property_key, key,
252 strlen(spec_arr[i].property_key)) == 0) {
253 break;
254 }
255 if (strncmp(spec_arr[i].property_key, "*", 1) == 0)
256 break;
257 }
258
259 if (i >= data->nspec) {
260 /* No matching specification. */
261 errno = ENOENT;
262 goto finish;
263 }
264
265 ret = &spec_arr[i].lr;
266
267 finish:
268 return ret;
269 }
270
stats(struct selabel_handle * rec)271 static void stats(struct selabel_handle __attribute__((unused)) *rec)
272 {
273 selinux_log(SELINUX_WARNING, "'stats' functionality not implemented.\n");
274 }
275
selabel_property_init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned nopts)276 int selabel_property_init(struct selabel_handle *rec,
277 const struct selinux_opt *opts,
278 unsigned nopts)
279 {
280 struct saved_data *data;
281
282 data = (struct saved_data *)malloc(sizeof(*data));
283 if (!data)
284 return -1;
285 memset(data, 0, sizeof(*data));
286
287 rec->data = data;
288 rec->func_close = &closef;
289 rec->func_stats = &stats;
290 rec->func_lookup = &lookup;
291
292 return init(rec, opts, nopts);
293 }
294