syslog: closelog before trying to figure out the fd

The syslog subsystem tries to figure out the file descriptor for the
connection to the system logger so that it can ensure that it doesn't
get closed in each device process.

However, the check does not work properly if there was already an open
connection to the system logger.  In this case the openlog call does not
do anything and we end up guessing the wrong file descriptor number for
the syslog connection.

Work around this by adding a closelog() call before attempting all of
this cleverness.  In the long run this should be fixed properly by just
bind mounting /dev/log into each device process's jail.

BUG=none
TEST=Running crosvm under minijail0 does not cause an InvalidFd error.

Change-Id: Iffd535d62acdf8053817af74b9e97444c746a0cf
Signed-off-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/851271
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Chirantan Ekbote 2018-01-04 16:07:54 -08:00 committed by chrome-bot
parent d42e493143
commit 0060077114

View file

@ -38,7 +38,7 @@ use std::ptr::null;
use std::str::from_utf8;
use std::sync::{Mutex, MutexGuard, Once, ONCE_INIT};
use libc::{tm, time, time_t, localtime_r, gethostname, openlog, fcntl, c_char, LOG_NDELAY,
use libc::{tm, time, time_t, localtime_r, gethostname, openlog, closelog, fcntl, c_char, LOG_NDELAY,
LOG_PERROR, LOG_PID, LOG_USER, F_GETFD};
use getpid;
@ -148,6 +148,11 @@ fn get_proc_name() -> Option<String> {
// libraries in use that hard depend on libc's syslogger. Remove this and go back to making the
// connection directly once minjail is ready.
fn openlog_and_get_socket() -> Result<UnixDatagram, Error> {
// closelog first in case there was already a file descriptor open. Safe because it takes no
// arguments and just closes an open file descriptor. Does nothing if the file descriptor
// was not already open.
unsafe { closelog(); }
// Ordinarily libc's FD for the syslog connection can't be accessed, but we can guess that the
// FD that openlog will be getting is the lowest unused FD. To guarantee that an FD is opened in
// this function we use the LOG_NDELAY to tell openlog to connect to the syslog now. To get the