From f03dde8549c5b49a84873698a67d103309777bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= Date: Mon, 3 Apr 2023 16:53:12 +1000 Subject: [PATCH] Skip newlines for SELinux logs libselinux log messages usually end with a new line character. Android log system does not require the new line character and will include the character as-is in the log buffer. This trimming was already implemented when the message is typed as SELINUX_AVC (see SelinuxAvcLog). Move the truncation one level up so it is occurring for all types of logs. Test: boot & adb logcat & inspect SELinux logs Change-Id: I360359c1b178ef24d555dd41f8d4a18b293a175c --- init/selinux.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/init/selinux.cpp b/init/selinux.cpp index 062ed3915..907eb80e3 100644 --- a/init/selinux.cpp +++ b/init/selinux.cpp @@ -761,15 +761,7 @@ void SelinuxSetEnforcement() { constexpr size_t kKlogMessageSize = 1024; -void SelinuxAvcLog(char* buf, size_t buf_len) { - CHECK_GT(buf_len, 0u); - - size_t str_len = strnlen(buf, buf_len); - // trim newline at end of string - if (buf[str_len - 1] == '\n') { - buf[str_len - 1] = '\0'; - } - +void SelinuxAvcLog(char* buf) { struct NetlinkMessage { nlmsghdr hdr; char buf[kKlogMessageSize]; @@ -835,8 +827,17 @@ int SelinuxKlogCallback(int type, const char* fmt, ...) { if (length_written <= 0) { return 0; } + + // libselinux log messages usually contain a new line character, while + // Android LOG() does not expect it. Remove it to avoid empty lines in + // the log buffers. + size_t str_len = strlen(buf); + if (buf[str_len - 1] == '\n') { + buf[str_len - 1] = '\0'; + } + if (type == SELINUX_AVC) { - SelinuxAvcLog(buf, sizeof(buf)); + SelinuxAvcLog(buf); } else { android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); }