From 4b1910bc680a7721f8791cbc9beb4e0a845134e7 Mon Sep 17 00:00:00 2001 From: Martin Samuelsson Date: Sat, 27 May 2023 13:33:45 +0200 Subject: Avoid insecurely passing untrusted string to syslog() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the second argument to syslog() being a format string, these function calls obviously need an actual format string prior to the log message. Thanks to Alexander Hansen Færøy for noticing and pointing out this embarrasing mistake. --- libsyslog/CHANGELOG.md | 4 ++++ libsyslog/src/syslog.rs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libsyslog/CHANGELOG.md b/libsyslog/CHANGELOG.md index eb0dad9..2d6cb4a 100644 --- a/libsyslog/CHANGELOG.md +++ b/libsyslog/CHANGELOG.md @@ -8,6 +8,10 @@ adheres to [Semantic Versioning][semver]. ## [Unreleased] +# Fixed + + - Stop insecurely passing untrusted data syslog(). + ## [0.1.0] - 2023-02-24 Initial release. diff --git a/libsyslog/src/syslog.rs b/libsyslog/src/syslog.rs index 6b36147..2a4d856 100644 --- a/libsyslog/src/syslog.rs +++ b/libsyslog/src/syslog.rs @@ -76,13 +76,17 @@ impl log::Log for Syslog { fn log(&self, record: &Record) { if self.enabled(record.metadata()) { - if let Ok(msg) = CString::new(format!("{}", record.args())) { + if let (Ok(fmt), Ok(msg)) = ( CString::new("%s"), + CString::new(format!("{}", record.args()))) + { + let fmt_ptr = fmt.as_ptr(); + let msg_ptr = msg.as_ptr(); match record.level() { - Level::Debug => unsafe { syslog(LOG_DEBUG, msg.as_ptr()); } - Level::Error => unsafe { syslog(LOG_ERR, msg.as_ptr()); } - Level::Info => unsafe { syslog(LOG_INFO, msg.as_ptr()); } - Level::Warn => unsafe { syslog(LOG_WARNING, msg.as_ptr()); } - Level::Trace => unsafe { syslog(LOG_DEBUG, msg.as_ptr()); } + Level::Debug => unsafe { syslog(LOG_DEBUG, fmt_ptr, msg_ptr); } + Level::Error => unsafe { syslog(LOG_ERR, fmt_ptr, msg_ptr); } + Level::Info => unsafe { syslog(LOG_INFO, fmt_ptr, msg_ptr); } + Level::Warn => unsafe { syslog(LOG_WARNING, fmt_ptr, msg_ptr); } + Level::Trace => unsafe { syslog(LOG_DEBUG, fmt_ptr, msg_ptr); } } } } -- cgit v1.2.3