Skip to content

Commit ea29052

Browse files
WavyEbuilderstephensmalley
authored andcommitted
libselinux: load_policy: log using selinux_log instead of fprintf
This allows consumers to override our logging to stderr using the callback based mechanism selinux_log provides. Signed-off-by: Rahul Sandhu <[email protected]> Acked-by: Stephen Smalley <[email protected]>
1 parent 7b59b94 commit ea29052

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

libselinux/src/load_policy.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <sepol/policydb.h>
1717
#endif
1818
#include <dlfcn.h>
19+
#include "callbacks.h"
1920
#include "policy.h"
2021
#include <limits.h>
2122

@@ -136,25 +137,25 @@ int selinux_mkload_policy(int preservebools __attribute__((unused)))
136137
fd = open(path, O_RDONLY | O_CLOEXEC);
137138
}
138139
if (fd < 0) {
139-
fprintf(stderr,
140-
"SELinux: Could not open policy file <= %s.%d: %m\n",
141-
selinux_binary_policy_path(), maxvers);
140+
selinux_log(SELINUX_ERROR,
141+
"SELinux: Could not open policy file <= %s.%d: %m\n",
142+
selinux_binary_policy_path(), maxvers);
142143
goto dlclose;
143144
}
144145

145146
if (fstat(fd, &sb) < 0) {
146-
fprintf(stderr,
147-
"SELinux: Could not stat policy file %s: %m\n",
148-
path);
147+
selinux_log(SELINUX_ERROR,
148+
"SELinux: Could not stat policy file %s: %m\n",
149+
path);
149150
goto close;
150151
}
151152

152153
size = sb.st_size;
153154
data = map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
154155
if (map == MAP_FAILED) {
155-
fprintf(stderr,
156-
"SELinux: Could not map policy file %s: %m\n",
157-
path);
156+
selinux_log(SELINUX_ERROR,
157+
"SELinux: Could not map policy file %s: %m\n",
158+
path);
158159
goto close;
159160
}
160161

@@ -175,9 +176,9 @@ int selinux_mkload_policy(int preservebools __attribute__((unused)))
175176
if (policydb_set_vers(policydb, kernvers) ||
176177
policydb_to_image(NULL, policydb, &data, &size)) {
177178
/* Downgrade failed, keep searching. */
178-
fprintf(stderr,
179-
"SELinux: Could not downgrade policy file %s, searching for an older version.\n",
180-
path);
179+
selinux_log(SELINUX_ERROR,
180+
"SELinux: Could not downgrade policy file %s, searching for an older version.\n",
181+
path);
181182
policy_file_free(pf);
182183
policydb_free(policydb);
183184
munmap(map, sb.st_size);
@@ -192,9 +193,9 @@ int selinux_mkload_policy(int preservebools __attribute__((unused)))
192193
rc = security_load_policy(data, size);
193194

194195
if (rc)
195-
fprintf(stderr,
196-
"SELinux: Could not load policy file %s: %m\n",
197-
path);
196+
selinux_log(SELINUX_ERROR,
197+
"SELinux: Could not load policy file %s: %m\n",
198+
path);
198199

199200
unmap:
200201
if (data != map)
@@ -205,7 +206,7 @@ int selinux_mkload_policy(int preservebools __attribute__((unused)))
205206
dlclose:
206207
#ifdef SHARED
207208
if (errormsg)
208-
fprintf(stderr, "libselinux: %s\n", errormsg);
209+
selinux_log(SELINUX_ERROR, "libselinux: %s\n", errormsg);
209210
if (libsepolh)
210211
dlclose(libsepolh);
211212
#endif
@@ -317,7 +318,7 @@ int selinux_init_load_policy(int *enforce)
317318
*enforce = 0;
318319
} else {
319320
/* Only emit this error if selinux was not disabled */
320-
fprintf(stderr, "Mount failed for selinuxfs on %s: %m\n", SELINUXMNT);
321+
selinux_log(SELINUX_ERROR, "Mount failed for selinuxfs on %s: %m\n", SELINUXMNT);
321322
}
322323

323324
if (rc == 0)
@@ -365,7 +366,7 @@ int selinux_init_load_policy(int *enforce)
365366
if (orig_enforce != *enforce) {
366367
rc = security_setenforce(*enforce);
367368
if (rc < 0) {
368-
fprintf(stderr, "SELinux: Unable to switch to %s mode: %m\n", (*enforce ? "enforcing" : "permissive"));
369+
selinux_log(SELINUX_ERROR, "SELinux: Unable to switch to %s mode: %m\n", (*enforce ? "enforcing" : "permissive"));
369370
if (*enforce)
370371
goto noload;
371372
}

0 commit comments

Comments
 (0)