summaryrefslogtreecommitdiff
path: root/src/secontext.c
blob: e06198b2a7e0c31bce75a6dd5f898e13b2c260ea (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
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
/*
 * Copyright (c) 2020-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "defs.h"

#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <selinux/selinux.h>
#include <selinux/label.h>

#include "largefile_wrappers.h"
#include "number_set.h"
#include "secontext.h"
#include "xmalloc.h"
#include "xstring.h"

/**
 * @param secontext Pointer to security context string.
 * @param result    Stores pointer to the beginning of the part to print.
 * @return          Number of characters of the string to be printed.
 */
static size_t
parse_secontext(char *secontext, char **result)
{
	char *end_pos = NULL;

	if (!is_number_in_set(SECONTEXT_FULL, secontext_set)) {
		/* We're looking for the type wihch is the third field */
		enum { SECONTEXT_TYPE = 2 };
		char *start_pos = secontext;

		for (unsigned int i = 0; i <= SECONTEXT_TYPE; i++) {
			end_pos = strchr(start_pos, ':');

			if (i == SECONTEXT_TYPE) {
				secontext = start_pos;
				break;
			}

			if (!end_pos)
				break;

			start_pos = end_pos + 1;
		}
	}

	size_t len = end_pos ? (size_t) (end_pos - secontext)
			     : strlen(secontext);

	/* Strip terminating \n as these tend to be present sometimes */
	while (len && secontext[len - 1] == '\n')
		len--;

	return *result = secontext, len;
}

static int
get_expected_filecontext(const char *path, char **secontext)
{
	static struct selabel_handle *hdl;

	if (!hdl) {
		static bool disabled;
		if (disabled)
			return -1;

		hdl = selabel_open(SELABEL_CTX_FILE, NULL, 0);
		if (!hdl) {
			perror_msg("could not open SELinux database, disabling "
				   "context mismatch checking");
			disabled = true;
			return -1;
		}
	}

	strace_stat_t stb;
	if (stat_file(path, &stb) < 0) {
		return -1;
	}

	return selabel_lookup(hdl, secontext, path, stb.st_mode);
}

/*
 * Retrieves the SELinux context of the given PID (extracted from the tcb).
 * Memory must be freed.
 * Returns 0 on success, -1 on failure.
 */
static int
selinux_getpidcon(struct tcb *tcp, char **secontext)
{
	if (number_set_array_is_empty(secontext_set, 0))
		return -1;

	int proc_pid = get_proc_pid(tcp->pid);
	if (!proc_pid)
		return -1;

	return getpidcon(proc_pid, secontext);
}

/*
 * Retrieves the SELinux context of the given pid and descriptor.
 * Memory must be freed.
 * Returns 0 on success, -1 on failure.
 */
static int
selinux_getfdcon(pid_t pid, int fd, char **secontext, char **expected)
{
	if (number_set_array_is_empty(secontext_set, 0) || pid <= 0 || fd < 0)
		return -1;

	int proc_pid = get_proc_pid(pid);
	if (!proc_pid)
		return -1;

	char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
	xsprintf(linkpath, "/proc/%u/fd/%u", proc_pid, fd);

	int rc = getfilecon(linkpath, secontext);
	if (rc < 0 || !is_number_in_set(SECONTEXT_MISMATCH, secontext_set))
		return rc;

	/*
	 * We need to resolve the path, because selabel_lookup() doesn't
	 * resolve anything.  Using readlink() is sufficient here.
	 */

	char buf[PATH_MAX];
	ssize_t n = readlink(linkpath, buf, sizeof(buf));
	if ((size_t) n >= sizeof(buf))
		return 0;
	buf[n] = '\0';

	get_expected_filecontext(buf, expected);

	return 0;
}

/*
 * Retrieves the SELinux context of the given path.
 * Memory must be freed.
 * Returns 0 on success, -1 on failure.
 */
static int
selinux_getfilecon(struct tcb *tcp, const char *path, char **secontext,
		   char **expected)
{
	if (number_set_array_is_empty(secontext_set, 0))
		return -1;

	int proc_pid = get_proc_pid(tcp->pid);
	if (!proc_pid)
		return -1;

	int rc = -1;
	char fname[PATH_MAX];

	if (path[0] == '/')
		rc = snprintf(fname, sizeof(fname), "/proc/%u/root%s",
			       proc_pid, path);
	else if (tcp->last_dirfd == AT_FDCWD)
		rc = snprintf(fname, sizeof(fname), "/proc/%u/cwd/%s",
			       proc_pid, path);
	else if (tcp->last_dirfd >= 0 )
		rc = snprintf(fname, sizeof(fname), "/proc/%u/fd/%u/%s",
			       proc_pid, tcp->last_dirfd, path);

	if ((unsigned int) rc >= sizeof(fname))
		return -1;

	rc = getfilecon(fname, secontext);
	if (rc < 0 || !is_number_in_set(SECONTEXT_MISMATCH, secontext_set))
		return rc;

	/*
	 * We need to fully resolve the path, because selabel_lookup() doesn't
	 * resolve anything.  Using realpath() is the only solution here to make
	 * sure the path is canonicalized.
	 */

	char *resolved = realpath(fname, NULL);
	if (!resolved)
		return 0;

	get_expected_filecontext(resolved, expected);
	free(resolved);

	return 0;
}

static void
print_context(char *secontext, char *expected)
{
	if (!secontext)
		return;

	unsigned int style = QUOTE_OMIT_LEADING_TRAILING_QUOTES
			     | QUOTE_OVERWRITE_HEXSTR |
			     (xflag == HEXSTR_NONE
			      ? QUOTE_HEXSTR_NONE
			      : QUOTE_HEXSTR_NON_ASCII_CHARS);

	char *ctx_str;
	ssize_t ctx_len = parse_secontext(secontext, &ctx_str);

	print_quoted_string_ex(ctx_str, ctx_len, style, "[]!");

	if (!expected)
		goto freecon_secontext;

	char *exp_str;
	ssize_t exp_len = parse_secontext(expected, &exp_str);

	if (ctx_len != exp_len || strncmp(ctx_str, exp_str, ctx_len)) {
		tprints("!!");
		print_quoted_string_ex(exp_str, exp_len, style, "[]!");
	}

	freecon(expected);
freecon_secontext:
	freecon(secontext);
}

void
selinux_printfdcon(pid_t pid, int fd)
{
	char *ctx = NULL;
	char *exp = NULL;

	if (selinux_getfdcon(pid, fd, &ctx, &exp) < 0)
		return;

	tprints(" [");
	print_context(ctx, exp);
	tprints("]");
}

void
selinux_printfilecon(struct tcb *tcp, const char *path)
{
	char *ctx = NULL;
	char *exp = NULL;

	if (selinux_getfilecon(tcp, path, &ctx, &exp) < 0)
		return;

	tprints(" [");
	print_context(ctx, exp);
	tprints("]");
}

void
selinux_printpidcon(struct tcb *tcp)
{
	char *ctx = NULL;

	if (selinux_getpidcon(tcp, &ctx) < 0)
		return;

	tprints("[");
	print_context(ctx, NULL);
	tprints("] ");
}