Skip to contentSkip to conversation

libsepol/cil: Write a message when a log message is truncated

PATCH In review James Carter 1 participant 2 messages Mar 9, 2022 – Mar 30, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/cil: Write a message when a log message is truncated

The MAX_LOG_SIZE is 512. It is possible that a log message could exceed the max size (such as for neverallowx rules). If so, then write out "<LOG MESSAGE TRUNCATED>", so that it is obvious that the log message has been truncated.

---
 libsepol/cil/src/cil_log.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libsepol/cil/src/cil_log.c +7 -2
index a296929b..e78c0aeb 100644
@@ -53,8 +53,13 @@ __attribute__ ((format (printf, 2, 0))) void cil_vlog(enum cil_log_level lvl, co
 {
 	if (cil_log_level >= lvl) {
 		char buff[MAX_LOG_SIZE];
-		vsnprintf(buff, MAX_LOG_SIZE, msg, args);
-		(*cil_log_handler)(cil_log_level, buff);
+		int n = vsnprintf(buff, MAX_LOG_SIZE, msg, args);
+		if (n > 0) {
+			(*cil_log_handler)(cil_log_level, buff);
+			if (n >= MAX_LOG_SIZE) {
+				(*cil_log_handler)(cil_log_level, " <LOG MESSAGE TRUNCATED>");
+			}
+		}
 	}
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · James Carter

The MAX_LOG_SIZE is 512. It is possible that a log message could exceed the max size (such as for neverallowx rules). If so, then write out "<LOG MESSAGE TRUNCATED>", so that it is obvious that the log message has been truncated.

Merged.
Jim

Quoted 27 lines
---
 libsepol/cil/src/cil_log.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libsepol/cil/src/cil_log.c +7 -3
index a296929b..e78c0aeb 100644
@@ -53,8 +53,13 @@ __attribute__ ((format (printf, 2, 0))) void cil_vlog(enum cil_log_level lvl, co
 {
        if (cil_log_level >= lvl) {
                char buff[MAX_LOG_SIZE];
-               vsnprintf(buff, MAX_LOG_SIZE, msg, args);
-               (*cil_log_handler)(cil_log_level, buff);
+               int n = vsnprintf(buff, MAX_LOG_SIZE, msg, args);
+               if (n > 0) {
+                       (*cil_log_handler)(cil_log_level, buff);
+                       if (n >= MAX_LOG_SIZE) {
+                               (*cil_log_handler)(cil_log_level, " <LOG MESSAGE TRUNCATED>");
+                       }
+               }
        }
 }
 
--

2.34.1