Debian Codegraph @

src/selinux/gsm-selinux.c

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include "config.h"
6
7 #include <glib.h>
8
9 #include "gsm-selinux.h"
10
11
12 static int (*getpidcon) (pid_t, char **);
13 static void (*freecon) (char *);
14 static int (*is_selinux_enabled) (void);
15
16
17 static gboolean
18 load_selinux (void)
19 {
20 static GModule *module = NULL;
21
22 if (g_once_init_enter_pointer (&module)) {
23 GModule *selinux = g_module_open ("libselinux.so.1",
24 G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
25
26 if (!g_module_symbol (selinux, "getpidcon", (gpointer *) &getpidcon)) {
27 g_debug ("Could not load getpidcon from libselinux.so.0");
28 return FALSE;
29 } else {
30 g_debug ("Loaded getpidcon from libselinux.so.0");
31 }
32
33 if (!g_module_symbol (selinux, "freecon", (gpointer *) &freecon)) {
34 g_debug ("Could not load freecon from libselinux.so.0");
35 return FALSE;
36 } else {
37 g_debug ("Loaded freecon from libselinux.so.0");
38 }
39
40 if (!g_module_symbol (selinux,
41 "is_selinux_enabled",
42 (gpointer *) &is_selinux_enabled)) {
43 g_debug ("Could not load is_selinux_enabled from libselinux.so.0");
44 return FALSE;
45 } else {
46 g_debug ("Loaded is_selinux_enabled from libselinux.so.0");
47 }
48
49 g_module_make_resident (selinux);
50
51 g_once_init_leave_pointer (&module, g_steal_pointer (&selinux));
52 }
53
54 return module != NULL;
55 }
56
57
58 gboolean
59 gsm_selinux_is_enabled (void)
60 {
61 if (!load_selinux ()) {
62 return FALSE;
63 }
64
65 switch (is_selinux_enabled ()) {
66 case 1:
67 /* We're running on an SELinux kernel */
68 return TRUE;
69 case -1:
70 /* Error; hide the security context column */
71 case 0:
72 /* We're not running on an SELinux kernel:
73 hide the security context column */
74 default:
75 g_debug ("SELinux was found but is not enabled.\n");
76 return FALSE;
77 }
78 }
79
80
81 char *
82 gsm_selinux_get_context (pid_t pid)
83 {
84 g_autofree char *result = NULL;
85 char *con = NULL;
86
87 if (!load_selinux ()) {
88 goto out;
89 }
90
91 if (getpidcon (pid, &con) != 0) {
92 goto out;
93 }
94
95 /* Copy into glib memory */
96 g_set_str (&result, con);
97
98 out:
99 /* This comes from selinux's alloc */
100 g_clear_pointer (&con, freecon);
101
102 return g_steal_pointer (&result);
103 }