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