1 /*
2 * Copyright (c) 2020-2022 The strace developers.
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "tests.h"
9 #include "xmalloc.h"
10 #include <unistd.h>
11
12 char *secontext_full_fd(int) ATTRIBUTE_MALLOC;
13 char *secontext_full_file(const char *, bool) ATTRIBUTE_MALLOC;
14 char *secontext_full_pid(pid_t) ATTRIBUTE_MALLOC;
15
16 char *secontext_short_fd(int) ATTRIBUTE_MALLOC;
17 char *secontext_short_file(const char *, bool) ATTRIBUTE_MALLOC;
18 char *secontext_short_pid(pid_t) ATTRIBUTE_MALLOC;
19
20 enum secontext_field {
21 SECONTEXT_USER,
22 SECONTEXT_ROLE,
23 SECONTEXT_TYPE
24 };
25
26 #if defined TEST_SECONTEXT && defined HAVE_SELINUX_RUNTIME
27
28 /**
29 * Parse a SELinux context string and return a specified field, duplicated
30 * in a separate string. The caller is responsible for freeing the memory
31 * pointed by the returned value.
32 */
33 char *get_secontext_field(const char *full_context, enum secontext_field field);
34
35 char *get_secontext_field_fd(int fd, enum secontext_field field);
36 char *get_secontext_field_file(const char *file, enum secontext_field field);
37
38 void reset_secontext_file(const char *file);
39
40 void update_secontext_field(const char *file, enum secontext_field field,
41 const char *newvalue);
42
43 # ifdef PRINT_SECONTEXT_FULL
44
45 # ifdef PRINT_SECONTEXT_MISMATCH
46 # define SECONTEXT_FILE(filename) secontext_full_file(filename, true)
47 # else
48 # define SECONTEXT_FILE(filename) secontext_full_file(filename, false)
49 # endif
50 # define SECONTEXT_FD(fd) secontext_full_fd(fd)
51 # define SECONTEXT_PID(pid) secontext_full_pid(pid)
52
53 # else
54
55 # ifdef PRINT_SECONTEXT_MISMATCH
56 # define SECONTEXT_FILE(filename) secontext_short_file(filename, true)
57 # else
58 # define SECONTEXT_FILE(filename) secontext_short_file(filename, false)
59 # endif
60 # define SECONTEXT_FD(fd) secontext_short_fd(fd)
61 # define SECONTEXT_PID(pid) secontext_short_pid(pid)
62
63 # endif
64
65 #else
66
67 static inline char *
68 get_secontext_field(const char *ctx, enum secontext_field field)
69 {
70 return NULL;
71 }
72 static inline char *
73 get_secontext_field_fd(int fd, enum secontext_field field)
74 {
75 return NULL;
76 }
77
78 static inline char *
79 get_secontext_field_file(const char *file, enum secontext_field field)
80 {
81 return NULL;
82 }
83
84 static inline void
85 reset_secontext_file(const char *file)
86 {
87 }
88
89 static inline void
90 update_secontext_field(const char *file, enum secontext_field field,
91 const char *newvalue)
92 {
93 }
94
95 # define SECONTEXT_FD(fd) xstrdup("")
96 # define SECONTEXT_FILE(filename) xstrdup("")
97 # define SECONTEXT_PID(pid) xstrdup("")
98
99 #endif
100
101 #define SECONTEXT_PID_MY() SECONTEXT_PID(getpid())