xref: /aosp_15_r20/external/selinux/libselinux/src/context.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 #include "context_internal.h"
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 
7 #define COMP_USER  0
8 #define COMP_ROLE  1
9 #define COMP_TYPE  2
10 #define COMP_RANGE 3
11 
12 typedef struct {
13 	char *current_str;	/* This is made up-to-date only when needed */
14 	char *(component[4]);
15 } context_private_t;
16 
17 /*
18  * Allocate a new context, initialized from str.  There must be 3 or
19  * 4 colon-separated components and no whitespace in any component other
20  * than the MLS component.
21  */
context_new(const char * str)22 context_t context_new(const char *str)
23 {
24 	int i, count;
25 	errno = 0;
26 	context_private_t *n =
27 	    (context_private_t *) malloc(sizeof(context_private_t));
28 	context_t result = (context_t) malloc(sizeof(context_s_t));
29 	const char *p, *tok;
30 
31 	if (result)
32 		result->ptr = n;
33 	else
34 		free(n);
35 	if (n == 0 || result == 0) {
36 		goto err;
37 	}
38 	n->current_str = n->component[0] = n->component[1] = n->component[2] =
39 	    n->component[3] = 0;
40 	for (count = 0, p = str; *p; p++) {
41 		switch (*p) {
42 		case ':':
43 			count++;
44 			break;
45 		case '\n':
46 		case '\t':
47 		case '\r':
48 			goto err;	/* sanity check */
49 		case ' ':
50 			if (count < 3)
51 				goto err;	/* sanity check */
52 		}
53 	}
54 	/*
55 	 * Could be anywhere from 2 - 5
56 	 * e.g user:role:type to user:role:type:sens1:cata-sens2:catb
57 	 */
58 	if (count < 2 || count > 5) {	/* might not have a range */
59 		goto err;
60 	}
61 
62 	n->component[3] = 0;
63 	for (i = 0, tok = str; *tok; i++) {
64 		if (i < 3)
65 			for (p = tok; *p && *p != ':'; p++) {	/* empty */
66 		} else {
67 			/* MLS range is one component */
68 			for (p = tok; *p; p++) {	/* empty */
69 			}
70 		}
71 		n->component[i] = strndup(tok, p - tok);
72 		if (n->component[i] == 0)
73 			goto err;
74 		tok = *p ? p + 1 : p;
75 	}
76 	return result;
77       err:
78 	if (errno == 0) errno = EINVAL;
79 	context_free(result);
80 	return 0;
81 }
82 
83 
conditional_free(char ** v)84 static void conditional_free(char **v)
85 {
86 	if (*v) {
87 		free(*v);
88 	}
89 	*v = 0;
90 }
91 
92 /*
93  * free all storage used by a context.  Safe to call with
94  * null pointer.
95  */
context_free(context_t context)96 void context_free(context_t context)
97 {
98 	context_private_t *n;
99 	int i;
100 	if (context) {
101 		n = context->ptr;
102 		if (n) {
103 			conditional_free(&n->current_str);
104 			for (i = 0; i < 4; i++) {
105 				conditional_free(&n->component[i]);
106 			}
107 			free(n);
108 		}
109 		free(context);
110 	}
111 }
112 
113 
114 /*
115  * Return a pointer to the string value of the context.
116  */
context_str(context_t context)117 const char *context_str(context_t context)
118 {
119 	context_private_t *n = context->ptr;
120 	int i;
121 	size_t total = 0;
122 	conditional_free(&n->current_str);
123 	for (i = 0; i < 4; i++) {
124 		if (n->component[i]) {
125 			total += strlen(n->component[i]) + 1;
126 		}
127 	}
128 	n->current_str = malloc(total);
129 	if (n->current_str != 0) {
130 		char *cp = n->current_str;
131 
132 		cp = stpcpy(cp, n->component[0]);
133 		for (i = 1; i < 4; i++) {
134 			if (n->component[i]) {
135 				*cp++ = ':';
136 				cp = stpcpy(cp, n->component[i]);
137 			}
138 		}
139 	}
140 	return n->current_str;
141 }
142 
143 
144 /* Returns nonzero iff failed */
set_comp(context_private_t * n,int idx,const char * str)145 static int set_comp(context_private_t * n, int idx, const char *str)
146 {
147 	char *t = NULL;
148 	const char *p;
149 	if (str) {
150 		for (p = str; *p; p++) {
151 			if (*p == '\t' || *p == '\n' || *p == '\r' ||
152 			    ((*p == ':' || *p == ' ') && idx != COMP_RANGE)) {
153 				errno = EINVAL;
154 				return -1;
155 			}
156 		}
157 
158 		t = strdup(str);
159 		if (!t) {
160 			return -1;
161 		}
162 	}
163 	conditional_free(&n->component[idx]);
164 	n->component[idx] = t;
165 	return 0;
166 }
167 
168 #define def_get(name,tag) \
169 const char * context_ ## name ## _get(context_t context) \
170 { \
171         context_private_t *n = context->ptr; \
172         return n->component[tag]; \
173 }
174 
175 def_get(type, COMP_TYPE)
176     def_get(user, COMP_USER)
177     def_get(range, COMP_RANGE)
178     def_get(role, COMP_ROLE)
179 #define def_set(name,tag) \
180 int context_ ## name ## _set(context_t context, const char* str) \
181 { \
182         return set_comp(context->ptr,tag,str);\
183 }
184     def_set(type, COMP_TYPE)
185     def_set(role, COMP_ROLE)
186     def_set(user, COMP_USER)
187     def_set(range, COMP_RANGE)
188