Debian Codegraph @

src/lib/util/selinux.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <selinux/selinux.h>
27 #include <selinux/label.h>
28 #include <sys/stat.h>
29
30 #include "common/common.h"
31 #include "lib/constants.h"
32 #include "lib/util/file.h"
33 #include "lib/util/template.h"
34 #include "lib/util/selinux.h"
35 #include "lib/util/string_array.h"
36
37 static errno_t
38 selinux_get_default_context(const char *path,
39 char **_context)
40 {
41 struct selabel_handle *handle;
42 char *context;
43 int ret;
44
45 handle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
46 if (handle == NULL) {
47 ret = errno;
48 ERROR("Unable to create selabel handle [%d]: %s", ret, strerror(ret));
49 return ret;
50 }
51
52 ret = selabel_lookup(handle, &context, path, 0);
53 if (ret < 0 && errno == ENOENT) {
54 context = NULL;
55 } else if (ret != 0) {
56 ret = errno;
57 ERROR("Unable to lookup selinux context [%d]: %s", ret, strerror(ret));
58 goto done;
59 }
60
61 INFO("Found default selinux context for [%s]: %s",
62 path, context == NULL ? "NULL" : context);
63
64 *_context = context;
65
66 ret = EOK;
67
68 done:
69 selabel_close(handle);
70
71 return ret;
72 }
73
74 errno_t
75 selinux_get_context(const char *path,
76 char **_context)
77 {
78 char *context;
79 int ret;
80
81 ret = getfilecon(path, &context);
82 if (ret < 0 && errno == ENOENT) {
83 return selinux_get_default_context(path, _context);
84 } else if (ret < 0) {
85 ret = errno;
86 ERROR("Unable to obtain selinux context for [%s] [%d]: %s",
87 path, ret, strerror(ret));
88
89 return ret;
90 }
91
92 INFO("Found selinux context for [%s]: %s",
93 path, context == NULL ? _("not set") : context);
94
95 *_context = context;
96
97 return EOK;
98 }
99
100 errno_t
101 selinux_mkstemp_for(const char *filepath,
102 mode_t mode,
103 char **_tmpfile)
104 {
105 char *original_context = NULL;
106 char *default_context = NULL;
107 char *tmpfile;
108 errno_t ret;
109 int seret;
110
111 if (is_selinux_enabled() != 1) {
112 return file_mktmp_for(filepath, mode, _tmpfile);
113 }
114
115 seret = getfscreatecon(&original_context);
116 if (seret != 0) {
117 ERROR("Unable to get current fscreate selinux context!");
118 return EIO;
119 }
120
121 ret = selinux_get_context(filepath, &default_context);
122 if (ret != EOK) {
123 ERROR("Unable to get default selinux context for [%s] [%d]: %s!",
124 filepath, ret, strerror(ret));
125 goto done;
126 }
127
128 /* Set desired fs create context. */
129 seret = setfscreatecon(default_context);
130 if (seret != 0) {
131 ERROR("Unable to set fscreate selinux context!");
132 ret = EIO;
133 goto done;
134 }
135
136 ret = file_mktmp_for(filepath, mode, &tmpfile);
137
138 /* Restore original fs create context. */
139 seret = setfscreatecon(original_context);
140 if (seret != 0) {
141 ERROR("Unable to restore fscreate selinux context!");
142 ret = EIO;
143 goto done;
144 }
145
146 /* Check result of file_mktmp_for() */
147 if (ret != EOK) {
148 free(tmpfile);
149 goto done;
150 }
151
152 *_tmpfile = tmpfile;
153
154 ret = EOK;
155
156 done:
157 if (original_context != NULL) {
158 freecon(original_context);
159 }
160
161 if (default_context != NULL) {
162 freecon(default_context);
163 }
164
165 return ret;
166 }
167
168 errno_t
169 selinux_file_copy(const char *source,
170 const char *destdir,
171 const char *destname,
172 mode_t dir_mode)
173 {
174 char *original_context = NULL;
175 char *default_context = NULL;
176 errno_t ret;
177 int seret;
178
179 if (is_selinux_enabled() != 1) {
180 return file_copy(source, destdir, destname, dir_mode);
181 }
182
183 seret = getfscreatecon(&original_context);
184 if (seret != 0) {
185 ERROR("Unable to get current fscreate selinux context!");
186 return EIO;
187 }
188
189 ret = selinux_get_context(source, &default_context);
190 if (ret != EOK) {
191 ERROR("Unable to get default selinux context for [%s] [%d]: %s!",
192 source, ret, strerror(ret));
193 goto done;
194 }
195
196 /* Set desired fs create context. */
197 seret = setfscreatecon(default_context);
198 if (seret != 0) {
199 ERROR("Unable to set fscreate selinux context!");
200 ret = EIO;
201 goto done;
202 }
203
204 ret = file_copy(source, destdir, destname, dir_mode);
205
206 /* Restore original fs create context. */
207 seret = setfscreatecon(original_context);
208 if (seret != 0) {
209 ERROR("Unable to restore fscreate selinux context!");
210 ret = EIO;
211 goto done;
212 }
213
214 /* Check result of textfile_copy() */
215 if (ret != EOK) {
216 goto done;
217 }
218
219 ret = EOK;
220
221 done:
222 if (original_context != NULL) {
223 freecon(original_context);
224 }
225
226 if (default_context != NULL) {
227 freecon(default_context);
228 }
229
230 return ret;
231 }
232
233 errno_t
234 selinux_mkstemp_copy(const char *source,
235 const char *destdir,
236 const char *destname,
237 mode_t dir_mode,
238 bool keep_secontext,
239 char **_tmpfile)
240 {
241 const char *context_file = keep_secontext ? source : destname;
242 char *original_context = NULL;
243 char *default_context = NULL;
244 errno_t ret;
245 int seret;
246
247 if (is_selinux_enabled() != 1) {
248 return file_mktmp_copy(source, destdir, destname, dir_mode, _tmpfile);
249 }
250
251 seret = getfscreatecon(&original_context);
252 if (seret != 0) {
253 ERROR("Unable to get current fscreate selinux context!");
254 return EIO;
255 }
256
257 ret = selinux_get_context(context_file, &default_context);
258 if (ret != EOK) {
259 ERROR("Unable to get default selinux context for [%s] [%d]: %s!",
260 source, ret, strerror(ret));
261 goto done;
262 }
263
264 /* Set desired fs create context. */
265 seret = setfscreatecon(default_context);
266 if (seret != 0) {
267 ERROR("Unable to set fscreate selinux context!");
268 ret = EIO;
269 goto done;
270 }
271
272 ret = file_mktmp_copy(source, destdir, destname, dir_mode, _tmpfile);
273
274 /* Restore original fs create context. */
275 seret = setfscreatecon(original_context);
276 if (seret != 0) {
277 ERROR("Unable to restore fscreate selinux context!");
278 ret = EIO;
279 goto done;
280 }
281
282 /* Check result of textfile_copy() */
283 if (ret != EOK) {
284 goto done;
285 }
286
287 ret = EOK;
288
289 done:
290 if (original_context != NULL) {
291 freecon(original_context);
292 }
293
294 if (default_context != NULL) {
295 freecon(default_context);
296 }
297
298 return ret;
299 }
300
301 static errno_t
302 parse_destination_path(const char *destination,
303 char **_dir,
304 char **_name)
305 {
306 const char *cname;
307 char *name;
308 char *dir;
309
310 dir = file_get_parent_directory(destination);
311 if (dir == NULL) {
312 return ENOMEM;
313 }
314
315 cname = file_get_basename(destination);
316 if (cname == NULL) {
317 free(dir);
318 return EINVAL;
319 }
320
321 name = strdup(cname);
322 if (name == NULL) {
323 free(dir);
324 return ENOMEM;
325 }
326
327 *_dir = dir;
328 *_name = name;
329
330 return EOK;
331 }
332
333 errno_t
334 selinux_copy_files_safely(struct selinux_safe_copy *table,
335 mode_t dir_mode,
336 bool keep_secontext)
337 {
338 char **tmpfiles = NULL;
339 char **names = NULL;
340 char **dirs = NULL;
341 errno_t ret;
342 int i;
343
344 for (i = 0; table[i].source != NULL; i++) {
345 /* Just counting. */
346 }
347
348 tmpfiles = string_array_create(i);
349 if (tmpfiles == NULL) {
350 ret = ENOMEM;
351 goto done;
352 }
353
354 dirs = string_array_create(i);
355 if (dirs == NULL) {
356 ret = ENOMEM;
357 goto done;
358 }
359
360 names = string_array_create(i);
361 if (names == NULL) {
362 ret = ENOMEM;
363 goto done;
364 }
365
366 /* Parse destination. */
367 for (i = 0; table[i].source != NULL; i++) {
368 if (table[i].destination == NULL) {
369 /* Skip this entry. */
370 continue;
371 }
372
373 ret = parse_destination_path(table[i].destination, &dirs[i], &names[i]);
374 if (ret != EOK) {
375 goto done;
376 }
377 }
378
379 /* First, write content into temporary files, so we can safely fail
380 * on error without overwriting destination files. */
381 for (i = 0; table[i].source != NULL; i++) {
382 if (table[i].destination == NULL) {
383 /* Skip this entry. */
384 continue;
385 }
386
387 if (file_exists(table[i].source) == ENOENT) {
388 if (table[i].write_empty_if_missing) {
389 INFO("File [%s] does not exist", table[i].source);
390 INFO("Writing empty temporary file for [%s]",
391 table[i].destination);
392 ret = template_write_temporary(table[i].destination, "",
393 AUTHSELECT_FILE_MODE,
394 &tmpfiles[i]);
395 if (ret != EOK) {
396 goto done;
397 }
398 } else if (!table[i].can_unlink) {
399 ERROR("File [%s] should exist but is missing. It is not safe to "
400 "delete [%s]. Aborting.", table[i].source,
401 table[i].destination);
402 ret = EPERM;
403 goto done;
404 } else {
405 /* destination will be removed later */
406 INFO("File [%s] does not exist", table[i].source);
407 tmpfiles[i] = NULL;
408 }
409 } else {
410 INFO("Writing temporary file for [%s]", table[i].destination);
411 ret = selinux_mkstemp_copy(table[i].source, dirs[i], names[i],
412 dir_mode, keep_secontext, &tmpfiles[i]);
413 if (ret != EOK) {
414 goto done;
415 }
416 }
417 }
418
419 /* Now rename the files.
420 *
421 * We now know that the system is writable, so rename call shall not
422 * fail and it will overwrite any existing file. The only reason it
423 * can fail is EIO which we can not do anything about and we can not
424 * even recover from it.
425 */
426 for (i = 0; table[i].source != NULL; i++) {
427 if (table[i].destination == NULL) {
428 /* Skip this entry. */
429 continue;
430 }
431
432 if (tmpfiles[i] == NULL) {
433 INFO("Removing [%s]", table[i].destination);
434 unlink(table[i].destination);
435 continue;
436 }
437
438 INFO("Renaming [%s] to [%s]", tmpfiles[i], table[i].destination);
439 ret = rename(tmpfiles[i], table[i].destination);
440 if (ret != 0) {
441 ret = errno;
442 ERROR("Unable to rename [%s] to [%s] [%d]: %s",
443 tmpfiles[i], table[i].destination, ret, strerror(ret));
444 goto done;
445 }
446 }
447
448 ret = EOK;
449
450 done:
451 if (ret != EOK && tmpfiles != NULL) {
452 for (i = 0; table[i].source != NULL; i++) {
453 if (tmpfiles[i] != NULL) {
454 unlink(tmpfiles[i]);
455 }
456 }
457 }
458
459 string_array_free(tmpfiles);
460 string_array_free(names);
461 string_array_free(dirs);
462
463 return ret;
464 }