Browse Source

[syslog] Strip invalid characters from hostname

Avoid generating syntactically invalid log messages by ensuring that
invalid characters are not present in the hostname.  In particular,
ensure that any whitespace is stripped, since whitespace functions as
a field separator for syslog messages.

Reported-by: Alex Davies <adavies@jumptrading.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
e5878ce65d
1 changed files with 31 additions and 6 deletions
  1. 31
    6
      src/net/udp/syslog.c

+ 31
- 6
src/net/udp/syslog.c View File

27
 
27
 
28
 #include <stdint.h>
28
 #include <stdint.h>
29
 #include <stdlib.h>
29
 #include <stdlib.h>
30
+#include <ctype.h>
30
 #include <byteswap.h>
31
 #include <byteswap.h>
31
 #include <ipxe/xfer.h>
32
 #include <ipxe/xfer.h>
32
 #include <ipxe/open.h>
33
 #include <ipxe/open.h>
91
  */
92
  */
92
 int syslog_send ( struct interface *xfer, unsigned int severity,
93
 int syslog_send ( struct interface *xfer, unsigned int severity,
93
 		  const char *message, const char *terminator ) {
94
 		  const char *message, const char *terminator ) {
95
+	const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
96
+	const char *domain = ( ( hostname[0] && syslog_domain ) ?
97
+			       syslog_domain : "" );
94
 
98
 
95
 	return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
99
 	return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
96
 			     SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
100
 			     SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
97
-					       severity ),
98
-			     ( syslog_hostname ? syslog_hostname : "" ),
99
-			     ( syslog_domain ? "." : "" ),
100
-			     ( syslog_domain ? syslog_domain : "" ),
101
-			     ( ( syslog_hostname || syslog_domain ) ? " " : ""),
102
-			     message, terminator );
101
+					       severity ), hostname,
102
+			     ( domain[0] ? "." : "" ), domain,
103
+			     ( hostname[0] ? " " : "" ), message, terminator );
103
 }
104
 }
104
 
105
 
105
 /******************************************************************************
106
 /******************************************************************************
211
 	.scope = &ipv6_scope,
212
 	.scope = &ipv6_scope,
212
 };
213
 };
213
 
214
 
215
+/**
216
+ * Strip invalid characters from host/domain name
217
+ *
218
+ * @v name		Name to strip
219
+ */
220
+static void syslog_fix_name ( char *name ) {
221
+	char *fixed = name;
222
+	int c;
223
+
224
+	/* Do nothing if name does not exist */
225
+	if ( ! name )
226
+		return;
227
+
228
+	/* Strip any non-printable or whitespace characters from the name */
229
+	do {
230
+		c = *(name++);
231
+		*fixed = c;
232
+		if ( isprint ( c ) && ! isspace ( c ) )
233
+			fixed++;
234
+	} while ( c );
235
+}
236
+
214
 /**
237
 /**
215
  * Apply syslog settings
238
  * Apply syslog settings
216
  *
239
  *
223
 	/* Fetch hostname and domain name */
246
 	/* Fetch hostname and domain name */
224
 	free ( syslog_hostname );
247
 	free ( syslog_hostname );
225
 	fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
248
 	fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
249
+	syslog_fix_name ( syslog_hostname );
226
 	free ( syslog_domain );
250
 	free ( syslog_domain );
227
 	fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
251
 	fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
252
+	syslog_fix_name ( syslog_domain );
228
 
253
 
229
 	/* Fetch log server */
254
 	/* Fetch log server */
230
 	syslog_console.disabled = CONSOLE_DISABLED;
255
 	syslog_console.disabled = CONSOLE_DISABLED;

Loading…
Cancel
Save