@@ -275,15 +275,18 @@ static void replace_event_msg(struct auditd_event *e, const char *buf)
275275
276276/*
277277* This function will take an audit structure and return a
278- * text buffer that's formatted for writing to disk. If there
279- * is an error the return value is NULL.
278+ * text buffer that's formatted for writing to disk. If there is
279+ * an error the return value is 0 and the format_buf is truncated.
280+ * format_buf will have any '\n' removed on return.
280281*/
281282static int format_raw (const struct audit_reply * rep )
282283{
283284 char * ptr ;
284285 int nlen ;
285286
286- if (rep == NULL ) {
287+ format_buf [0 ] = 0 ;
288+
289+ if (rep == NULL ) {
287290 if (config -> node_name_format != N_NONE )
288291 nlen = snprintf (format_buf , FORMAT_BUF_LEN - 32 ,
289292 "node=%s type=DAEMON_ERR op=format-raw msg=NULL res=failed" ,
@@ -292,10 +295,8 @@ static int format_raw(const struct audit_reply *rep)
292295 nlen = snprintf (format_buf , MAX_AUDIT_MESSAGE_LENGTH ,
293296 "type=DAEMON_ERR op=format-raw msg=NULL res=failed" );
294297
295- if (nlen < 1 ) {
296- format_buf [0 ] = 0 ;
298+ if (nlen < 1 )
297299 return 0 ;
298- }
299300 } else {
300301 int len ;
301302 const char * type , * message ;
@@ -325,10 +326,8 @@ static int format_raw(const struct audit_reply *rep)
325326 MAX_AUDIT_MESSAGE_LENGTH - 32 ,
326327 "type=%s msg=%.*s" , type , len , message );
327328
328- if (nlen < 1 ) {
329- format_buf [0 ] = 0 ;
329+ if (nlen < 1 )
330330 return 0 ;
331- }
332331
333332 /* Replace \n with space so it looks nicer. */
334333 ptr = format_buf ;
@@ -430,9 +429,11 @@ static int add_simple_field(auparse_state_t *au, size_t len_left, int encode)
430429}
431430
432431/*
433- * This function will take an audit structure and return a
434- * text buffer that's formatted and enriched. If there is an
435- * error the return value is NULL.
432+ * This function will take an audit structure and return a text
433+ * buffer that's formatted and enriched. If there is an error the
434+ * return value is the raw formatted buffer (which may be truncated if it
435+ * had an error)or an error message in the format_buffer. The return
436+ * value is never NULL.
436437*/
437438static const char * format_enrich (const struct audit_reply * rep )
438439{
@@ -459,25 +460,28 @@ static const char *format_enrich(const struct audit_reply *rep)
459460 // Add carriage return so auparse sees it correctly
460461 format_buf [mlen ] = 0x0A ;
461462 format_buf [mlen + 1 ] = 0 ;
463+ mlen ++ ; // Increase the length so auparse copies the '\n'
462464
463465 // init auparse
464466 if (au == NULL ) {
465467 au = auparse_init (AUSOURCE_BUFFER , format_buf );
466468 if (au == NULL ) {
467- format_buf [mlen ] = 0 ; //remove newline
469+ format_buf [mlen - 1 ] = 0 ; //remove newline
468470 return format_buf ;
469471 }
470472
471473 auparse_set_escape_mode (au , AUPARSE_ESC_RAW );
472474 auparse_set_eoe_timeout (config -> end_of_event_timeout );
473475 } else
474476 auparse_new_buffer (au , format_buf , mlen );
477+
475478 sep_done = 0 ;
476479
477480 // Loop over all fields while possible to add field
478481 rc = auparse_first_record (au );
479482 if (rc != 1 )
480- format_buf [mlen ] = 0 ; //remove newline on failure
483+ format_buf [mlen - 1 ] = 0 ; //remove newline on failure
484+
481485 rtype = auparse_get_type (au );
482486 switch (rtype )
483487 { // Flush before adding to pickup new associations
@@ -514,6 +518,9 @@ static const char *format_enrich(const struct audit_reply *rep)
514518 break ;
515519 }
516520 rc = auparse_next_field (au );
521+ //remove newline when nothing added
522+ if (rc < 1 && sep_done == 0 )
523+ format_buf [mlen - 1 ] = 0 ;
517524 }
518525
519526 switch (rtype )
@@ -528,6 +535,7 @@ static const char *format_enrich(const struct audit_reply *rep)
528535 break ;
529536 }
530537 }
538+
531539 return format_buf ;
532540}
533541
0 commit comments