-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathprocattr.c
323 lines (280 loc) · 6.92 KB
/
procattr.c
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
#include <assert.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include "selinux_internal.h"
#include "policy.h"
#define UNSET (char *) -1
/* Cached values so that when a thread calls set*con() then gen*con(), the value
* which was set is directly returned.
*/
static __thread char *prev_current = UNSET;
static __thread char *prev_exec = UNSET;
static __thread char *prev_fscreate = UNSET;
static __thread char *prev_keycreate = UNSET;
static __thread char *prev_sockcreate = UNSET;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_key_t destructor_key;
static int destructor_key_initialized = 0;
static __thread char destructor_initialized;
/* Bionic and glibc >= 2.30 declare gettid() system call wrapper in unistd.h and
* has a definition for it */
#ifdef __BIONIC__
#define HAVE_GETTID 1
#elif !defined(__GLIBC_PREREQ)
#define HAVE_GETTID 0
#elif !__GLIBC_PREREQ(2,30)
#define HAVE_GETTID 0
#else
#define HAVE_GETTID 1
#endif
static pid_t selinux_gettid(void)
{
#if HAVE_GETTID
return gettid();
#else
return syscall(__NR_gettid);
#endif
}
static void procattr_thread_destructor(void __attribute__((unused)) *unused)
{
if (prev_current != UNSET)
free(prev_current);
if (prev_exec != UNSET)
free(prev_exec);
if (prev_fscreate != UNSET)
free(prev_fscreate);
if (prev_keycreate != UNSET)
free(prev_keycreate);
if (prev_sockcreate != UNSET)
free(prev_sockcreate);
}
void __attribute__((destructor)) procattr_destructor(void);
void __attribute__((destructor)) procattr_destructor(void)
{
if (destructor_key_initialized)
__selinux_key_delete(destructor_key);
}
static inline void init_thread_destructor(void)
{
if (destructor_initialized == 0) {
__selinux_setspecific(destructor_key, /* some valid address to please GCC */ &selinux_page_size);
destructor_initialized = 1;
}
}
static void init_procattr(void)
{
if (__selinux_key_create(&destructor_key, procattr_thread_destructor) == 0) {
destructor_key_initialized = 1;
}
}
static int openattr(pid_t pid, const char *attr, int flags)
{
int fd, rc;
char path[44]; /* must hold "/proc/self/task/%d/attr/sockcreate" */
pid_t tid;
static_assert(sizeof(pid_t) <= sizeof(uint32_t), "content written to path might get truncated");
if (pid > 0) {
rc = snprintf(path, sizeof(path), "/proc/%d/attr/%s", pid, attr);
} else if (pid == 0) {
rc = snprintf(path, sizeof(path), "/proc/thread-self/attr/%s", attr);
if (rc < 0 || (size_t)rc >= sizeof(path)) {
errno = EOVERFLOW;
return -1;
}
fd = open(path, flags | O_CLOEXEC);
if (fd >= 0 || errno != ENOENT)
return fd;
tid = selinux_gettid();
rc = snprintf(path, sizeof(path), "/proc/self/task/%d/attr/%s", tid, attr);
} else {
errno = EINVAL;
return -1;
}
if (rc < 0 || (size_t)rc >= sizeof(path)) {
errno = EOVERFLOW;
return -1;
}
return open(path, flags | O_CLOEXEC);
}
static int getprocattrcon_raw(char **context, pid_t pid, const char *attr,
const char *prev_context)
{
char *buf;
size_t size;
int fd;
ssize_t ret;
int errno_hold;
__selinux_once(once, init_procattr);
init_thread_destructor();
if (prev_context && prev_context != UNSET) {
*context = strdup(prev_context);
if (!(*context)) {
return -1;
}
return 0;
}
fd = openattr(pid, attr, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -1;
size = selinux_page_size;
buf = calloc(1, size);
if (!buf) {
ret = -1;
goto out;
}
do {
ret = read(fd, buf, size - 1);
} while (ret < 0 && errno == EINTR);
if (ret < 0)
goto out2;
if (ret == 0) {
*context = NULL;
goto out2;
}
*context = strdup(buf);
if (!(*context)) {
ret = -1;
goto out2;
}
ret = 0;
out2:
free(buf);
out:
errno_hold = errno;
close(fd);
errno = errno_hold;
return ret;
}
static int getprocattrcon(char **context, pid_t pid, const char *attr,
const char *prev_context)
{
int ret;
char * rcontext;
ret = getprocattrcon_raw(&rcontext, pid, attr, prev_context);
if (!ret) {
ret = selinux_raw_to_trans_context(rcontext, context);
freecon(rcontext);
}
return ret;
}
static int setprocattrcon_raw(const char *context, const char *attr,
char **prev_context)
{
int fd;
ssize_t ret;
int errno_hold;
char *context2 = NULL;
__selinux_once(once, init_procattr);
init_thread_destructor();
if (!context && !*prev_context)
return 0;
if (context && *prev_context && *prev_context != UNSET
&& !strcmp(context, *prev_context))
return 0;
fd = openattr(0, attr, O_RDWR | O_CLOEXEC);
if (fd < 0)
return -1;
if (context) {
ret = -1;
context2 = strdup(context);
if (!context2)
goto out;
do {
ret = write(fd, context2, strlen(context2) + 1);
} while (ret < 0 && errno == EINTR);
} else {
do {
ret = write(fd, NULL, 0); /* clear */
} while (ret < 0 && errno == EINTR);
}
out:
errno_hold = errno;
close(fd);
errno = errno_hold;
if (ret < 0) {
free(context2);
return -1;
} else {
if (*prev_context != UNSET)
free(*prev_context);
*prev_context = context2;
return 0;
}
}
static int setprocattrcon(const char *context, const char *attr,
char **prev_context)
{
int ret;
char * rcontext;
if (selinux_trans_to_raw_context(context, &rcontext))
return -1;
ret = setprocattrcon_raw(rcontext, attr, prev_context);
freecon(rcontext);
return ret;
}
#define getselfattr_def(fn, attr, prev_context) \
int get##fn##_raw(char **c) \
{ \
return getprocattrcon_raw(c, 0, attr, prev_context); \
} \
int get##fn(char **c) \
{ \
return getprocattrcon(c, 0, attr, prev_context); \
}
#define setselfattr_def(fn, attr, prev_context) \
int set##fn##_raw(const char * c) \
{ \
return setprocattrcon_raw(c, attr, &prev_context); \
} \
int set##fn(const char * c) \
{ \
return setprocattrcon(c, attr, &prev_context); \
}
#define all_selfattr_def(fn, attr, prev_context) \
getselfattr_def(fn, attr, prev_context) \
setselfattr_def(fn, attr, prev_context)
all_selfattr_def(con, "current", prev_current)
getselfattr_def(prevcon, "prev", NULL)
all_selfattr_def(execcon, "exec", prev_exec)
all_selfattr_def(fscreatecon, "fscreate", prev_fscreate)
all_selfattr_def(sockcreatecon, "sockcreate", prev_sockcreate)
all_selfattr_def(keycreatecon, "keycreate", prev_keycreate)
int getpidcon_raw(pid_t pid, char **c)
{
if (pid <= 0) {
errno = EINVAL;
return -1;
}
return getprocattrcon_raw(c, pid, "current", NULL);
}
int getpidcon(pid_t pid, char **c)
{
if (pid <= 0) {
errno = EINVAL;
return -1;
}
return getprocattrcon(c, pid, "current", NULL);
}
int getpidprevcon_raw(pid_t pid, char **c)
{
if (pid <= 0) {
errno = EINVAL;
return -1;
}
return getprocattrcon_raw(c, pid, "prev", NULL);
}
int getpidprevcon(pid_t pid, char **c)
{
if (pid <= 0) {
errno = EINVAL;
return -1;
}
return getprocattrcon(c, pid, "prev", NULL);
}