summaryrefslogtreecommitdiff
path: root/tests/secontext.c
blob: 21c6370c90af76466f5ded4fadd07797ebaa6ebd (plain)
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
/*
 * Copyright (c) 2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#ifdef HAVE_SELINUX_RUNTIME

# include <assert.h>
# include <errno.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <selinux/selinux.h>

# include "xmalloc.h"

# define TEST_SECONTEXT
# include "secontext.h"

static char *
secontext_format(char *context, const char *fmt)
	ATTRIBUTE_FORMAT((printf, 2, 0)) ATTRIBUTE_MALLOC;

static char *
secontext_format(char *context, const char *fmt)
{
	int saved_errno = errno;
	char *res = context ? xasprintf(fmt, context) : xstrdup("");
	free(context);
	errno = saved_errno;
	return res;
}

# define FORMAT_SPACE_BEFORE(string)	secontext_format(string, " [%s]")
# define FORMAT_SPACE_AFTER(string)	secontext_format(string, "[%s] ")

static char *
strip_trailing_newlines(char *context)
{
	/*
	 * On the CI at least, the context may have a trailing \n,
	 * let's remove it just in case.
	 */
	size_t len = strlen(context);
	for (; len > 0; --len) {
		if (context[len - 1] != '\n')
			break;
	}
	context[len] = '\0';
	return context;
}

static char *
raw_secontext_full_file(const char *filename)
{
	int saved_errno = errno;
	char *full_secontext = NULL;
	char *secontext;

	if (getfilecon(filename, &secontext) >= 0) {
		full_secontext = strip_trailing_newlines(xstrdup(secontext));
		freecon(secontext);
	}
	errno = saved_errno;
	return full_secontext;
}

static char *
raw_secontext_short_file(const char *filename)
{
	int saved_errno = errno;

	char *ctx = raw_secontext_full_file(filename);
	if (ctx == NULL)
		return ctx;

	char *saveptr = NULL;
	const char *token;
	unsigned int i;

	char *ctx_copy = xstrdup(ctx);
	char *context = NULL;
	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
		if (i == 2) {
			context = xstrdup(token);
			break;
		}
	}
	if (context == NULL)
		context = xstrdup(ctx);
	free(ctx_copy);
	free(ctx);

	errno = saved_errno;
	return context;
}

static char *
raw_secontext_full_pid(pid_t pid)
{
	int saved_errno = errno;
	char *full_secontext = NULL;
	char *secontext;

	if (getpidcon(pid, &secontext) == 0) {
		full_secontext = strip_trailing_newlines(xstrdup(secontext));
		freecon(secontext);
	}
	errno = saved_errno;
	return full_secontext;
}

static char *
raw_secontext_short_pid(pid_t pid)
{
	int saved_errno = errno;

	char *ctx = raw_secontext_full_pid(pid);
	if (ctx == NULL)
		return ctx;

	char *saveptr = NULL;
	const char *token;
	int i;

	char *ctx_copy = xstrdup(ctx);
	char *context = NULL;
	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
		if (i == 2) {
			context = xstrdup(token);
			break;
		}
	}
	if (context == NULL)
		context = xstrdup(ctx);
	free(ctx_copy);
	free(ctx);

	errno = saved_errno;
	return context;
}

char *
secontext_full_file(const char *filename)
{
	return FORMAT_SPACE_BEFORE(raw_secontext_full_file(filename));
}

char *
secontext_full_pid(pid_t pid)
{
	return FORMAT_SPACE_AFTER(raw_secontext_full_pid(pid));
}

char *
secontext_short_file(const char *filename)
{
	return FORMAT_SPACE_BEFORE(raw_secontext_short_file(filename));
}

char *
secontext_short_pid(pid_t pid)
{
	return FORMAT_SPACE_AFTER(raw_secontext_short_pid(pid));
}

void
update_secontext_type(const char *file, const char *newtype)
{
	char *ctx = raw_secontext_full_file(file);
	if (ctx == NULL)
		return;

	char *saveptr = NULL;
	char *token;
	int field;
	char *split[4];

	for (token = strtok_r(ctx, ":", &saveptr), field = 0;
	     token; token = strtok_r(NULL, ":", &saveptr), field++) {
		assert(field < 4);
		split[field] = token;
	}
	assert(field == 4);

	char *newcontext = xasprintf("%s:%s:%s:%s", split[0], split[1],
				     newtype, split[3]);

	(void) setfilecon(file, newcontext);

	free(newcontext);
	free(ctx);
}

#endif /* HAVE_SELINUX_RUNTIME */