lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
#include <sepol/policydb/policydb.h>
#include <sepol/policydb/services.h>
#include "context_internal.h"
 
#include "debug.h"
#include "context.h"
#include "handle.h"
#include "mls.h"
#include "private.h"
 
/* ----- Compatibility ---- */
int policydb_context_isvalid(const policydb_t * p, const context_struct_t * c)
{
 
   return context_is_valid(p, c);
}
 
int sepol_check_context(const char *context)
{
 
   return sepol_context_to_sid((const sepol_security_context_t)context,
                   strlen(context) + 1, NULL);
}
 
/* ---- End compatibility --- */
 
/*
 * Return 1 if the fields in the security context
 * structure `c' are valid.  Return 0 otherwise.
 */
int context_is_valid(const policydb_t * p, const context_struct_t * c)
{
 
   role_datum_t *role;
   user_datum_t *usrdatum;
   ebitmap_t types, roles;
   int ret = 1;
 
   ebitmap_init(&types);
   ebitmap_init(&roles);
   if (!c->role || c->role > p->p_roles.nprim)
       return 0;
 
   if (!c->user || c->user > p->p_users.nprim)
       return 0;
 
   if (!c->type || c->type > p->p_types.nprim)
       return 0;
 
   if (c->role != OBJECT_R_VAL) {
       /*
        * Role must be authorized for the type.
        */
       role = p->role_val_to_struct[c->role - 1];
       if (!role || !ebitmap_get_bit(&role->cache, c->type - 1))
           /* role may not be associated with type */
           return 0;
 
       /*
        * User must be authorized for the role.
        */
       usrdatum = p->user_val_to_struct[c->user - 1];
       if (!usrdatum)
           return 0;
 
       if (!ebitmap_get_bit(&usrdatum->cache, c->role - 1))
           /* user may not be associated with role */
           return 0;
   }
 
   if (!mls_context_isvalid(p, c))
       return 0;
 
   return ret;
}
 
/*
 * Write the security context string representation of
 * the context structure `context' into a dynamically
 * allocated string of the correct size.  Set `*scontext'
 * to point to this string and set `*scontext_len' to
 * the length of the string.
 */
int context_to_string(sepol_handle_t * handle,
             const policydb_t * policydb,
             const context_struct_t * context,
             char **result, size_t * result_len)
{
 
   char *scontext = NULL;
   size_t scontext_len = 0;
   char *ptr;
 
   /* Compute the size of the context. */
   scontext_len +=
       strlen(policydb->p_user_val_to_name[context->user - 1]) + 1;
   scontext_len +=
       strlen(policydb->p_role_val_to_name[context->role - 1]) + 1;
   scontext_len += strlen(policydb->p_type_val_to_name[context->type - 1]);
   scontext_len += mls_compute_context_len(policydb, context);
 
   /* We must null terminate the string */
   scontext_len += 1;
 
   /* Allocate space for the context; caller must free this space. */
   scontext = malloc(scontext_len);
   if (!scontext)
       goto omem;
   scontext[scontext_len - 1] = '\0';
 
   /*
    * Copy the user name, role name and type name into the context.
    */
   ptr = scontext;
   sprintf(ptr, "%s:%s:%s",
       policydb->p_user_val_to_name[context->user - 1],
       policydb->p_role_val_to_name[context->role - 1],
       policydb->p_type_val_to_name[context->type - 1]);
 
   ptr +=
       strlen(policydb->p_user_val_to_name[context->user - 1]) + 1 +
       strlen(policydb->p_role_val_to_name[context->role - 1]) + 1 +
       strlen(policydb->p_type_val_to_name[context->type - 1]);
 
   mls_sid_to_context(policydb, context, &ptr);
 
   *result = scontext;
   *result_len = scontext_len;
   return STATUS_SUCCESS;
 
      omem:
   ERR(handle, "out of memory, could not convert " "context to string");
   free(scontext);
   return STATUS_ERR;
}
 
/*
 * Create a context structure from the given record
 */
int context_from_record(sepol_handle_t * handle,
           const policydb_t * policydb,
           context_struct_t ** cptr,
           const sepol_context_t * record)
{
 
   context_struct_t *scontext = NULL;
   user_datum_t *usrdatum;
   role_datum_t *roldatum;
   type_datum_t *typdatum;
 
   /* Hashtab keys are not constant - suppress warnings */
   char *user = strdup(sepol_context_get_user(record));
   char *role = strdup(sepol_context_get_role(record));
   char *type = strdup(sepol_context_get_type(record));
   const char *mls = sepol_context_get_mls(record);
 
   scontext = (context_struct_t *) malloc(sizeof(context_struct_t));
   if (!user || !role || !type || !scontext) {
       ERR(handle, "out of memory");
       goto err;
   }
   context_init(scontext);
 
   /* User */
   usrdatum = (user_datum_t *) hashtab_search(policydb->p_users.table,
                          (hashtab_key_t) user);
   if (!usrdatum) {
       ERR(handle, "user %s is not defined", user);
       goto err_destroy;
   }
   scontext->user = usrdatum->s.value;
 
   /* Role */
   roldatum = (role_datum_t *) hashtab_search(policydb->p_roles.table,
                          (hashtab_key_t) role);
   if (!roldatum) {
       ERR(handle, "role %s is not defined", role);
       goto err_destroy;
   }
   scontext->role = roldatum->s.value;
 
   /* Type */
   typdatum = (type_datum_t *) hashtab_search(policydb->p_types.table,
                          (hashtab_key_t) type);
   if (!typdatum || typdatum->flavor == TYPE_ATTRIB) {
       ERR(handle, "type %s is not defined", type);
       goto err_destroy;
   }
   scontext->type = typdatum->s.value;
 
   /* MLS */
   if (mls && !policydb->mls) {
       ERR(handle, "MLS is disabled, but MLS context \"%s\" found",
           mls);
       goto err_destroy;
   } else if (!mls && policydb->mls) {
       ERR(handle, "MLS is enabled, but no MLS context found");
       goto err_destroy;
   }
   if (mls && (mls_from_string(handle, policydb, mls, scontext) < 0))
       goto err_destroy;
 
   /* Validity check */
   if (!context_is_valid(policydb, scontext)) {
       if (mls) {
           ERR(handle,
               "invalid security context: \"%s:%s:%s:%s\"",
               user, role, type, mls);
       } else {
           ERR(handle,
               "invalid security context: \"%s:%s:%s\"",
               user, role, type);
       }
       goto err_destroy;
   }
 
   *cptr = scontext;
   free(user);
   free(type);
   free(role);
   return STATUS_SUCCESS;
 
      err_destroy:
   errno = EINVAL;
   context_destroy(scontext);
 
      err:
   free(scontext);
   free(user);
   free(type);
   free(role);
   ERR(handle, "could not create context structure");
   return STATUS_ERR;
}
 
/*
 * Create a record from the given context structure
 */
int context_to_record(sepol_handle_t * handle,
             const policydb_t * policydb,
             const context_struct_t * context,
             sepol_context_t ** record)
{
 
   sepol_context_t *tmp_record = NULL;
   char *mls = NULL;
 
   if (sepol_context_create(handle, &tmp_record) < 0)
       goto err;
 
   if (sepol_context_set_user(handle, tmp_record,
                  policydb->p_user_val_to_name[context->user -
                               1]) < 0)
       goto err;
 
   if (sepol_context_set_role(handle, tmp_record,
                  policydb->p_role_val_to_name[context->role -
                               1]) < 0)
       goto err;
 
   if (sepol_context_set_type(handle, tmp_record,
                  policydb->p_type_val_to_name[context->type -
                               1]) < 0)
       goto err;
 
   if (policydb->mls) {
       if (mls_to_string(handle, policydb, context, &mls) < 0)
           goto err;
 
       if (sepol_context_set_mls(handle, tmp_record, mls) < 0)
           goto err;
   }
 
   free(mls);
   *record = tmp_record;
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not create context record");
   sepol_context_free(tmp_record);
   free(mls);
   return STATUS_ERR;
}
 
/*
 * Create a context structure from the provided string.
 */
int context_from_string(sepol_handle_t * handle,
           const policydb_t * policydb,
           context_struct_t ** cptr,
           const char *con_str, size_t con_str_len)
{
 
   char *con_cpy = NULL;
   sepol_context_t *ctx_record = NULL;
 
   if (zero_or_saturated(con_str_len)) {
       ERR(handle, "Invalid context length");
       goto err;
   }
 
   /* sepol_context_from_string expects a NULL-terminated string */
   con_cpy = malloc(con_str_len + 1);
   if (!con_cpy) {
       ERR(handle, "out of memory");
       goto err;
   }
 
   memcpy(con_cpy, con_str, con_str_len);
   con_cpy[con_str_len] = '\0';
 
   if (sepol_context_from_string(handle, con_cpy, &ctx_record) < 0)
       goto err;
 
   /* Now create from the data structure */
   if (context_from_record(handle, policydb, cptr, ctx_record) < 0)
       goto err;
 
   free(con_cpy);
   sepol_context_free(ctx_record);
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not create context structure");
   free(con_cpy);
   sepol_context_free(ctx_record);
   return STATUS_ERR;
}
 
int sepol_context_check(sepol_handle_t * handle,
           const sepol_policydb_t * policydb,
           const sepol_context_t * context)
{
 
   context_struct_t *con = NULL;
   int ret = context_from_record(handle, &policydb->p, &con, context);
   context_destroy(con);
   free(con);
   return ret;
}