Commit Diff
Diff:
c60f40996e8efeea7fbb5298ed1f28543f0e0ea4
831ae0c9e26e249f280009dd827695154c64a71e
Commit:
831ae0c9e26e249f280009dd827695154c64a71e
Tree:
76c6a893cd905984ee646edacb132eb3a72979bb
Author:
pjp <pjp@delphinusdns.org>
Committer:
pjp <pjp@delphinusdns.org>
Date:
Wed Nov 20 18:20:49 2019 UTC
Message:
in debug mode it was possible to pass ansi and other escape characters to the terminal via DNS. Such a request was done like so: dddctl query -Q192.168.177.2 -P53 '^[[35mcentroid.eu' would make the colour purple. To prevent that I have an ugly dance around a small ctype isprint() sanitizer. This should make debugging safe.
blob - e8c65b258e68d042b900874df94e8a8242aa56a8
blob + fde591b94ddcecd005eafe82bb06ceea6193256d
--- log.c
+++ log.c
@@ -27,7 +27,7 @@
*/
/*
- * $Id: log.c,v 1.6 2019/06/06 14:56:08 pjp Exp $
+ * $Id: log.c,v 1.7 2019/11/20 18:20:49 pjp Exp $
*/
@@ -41,6 +41,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include <syslog.h>
@@ -72,9 +73,65 @@ extern int verbose;
void dolog(int pri, char *fmt, ...);
void receivelog(char *buf, int len);
int remotelog(int fd, char *fmt, ...);
+char *input_sanitize(char *);
/*
+ * INPUT_SANITIZE - syslogd does this sanitization, but in debug mode we want
+ * this sanitizer at least.
+ */
+
+char *
+input_sanitize(char *fmt)
+{
+ char *buf;
+ char *p, *q;
+ char backslash = '\\';
+
+ buf = malloc((4 * strlen(fmt)) + 1);
+ if (buf == NULL)
+ return NULL;
+
+ q = buf;
+
+ for (p = fmt; *p; p++) {
+ if (*p == backslash) {
+ *q++ = *p++;
+ if (*p == '\0')
+ break;
+ switch (*p) {
+ case 'n':
+ case 't':
+ case 'r':
+ case '\'':
+ case '\\':
+ case '"':
+ *q++ = *p;
+ break;
+ default:
+ *q++ = '\\';
+ *q++ = *p;
+ break;
+ }
+ } else {
+ if (isprint(*p) || *p == '\n') {
+ *q++ = *p;
+ } else {
+ *q++ = '\\';
+ *q++ = 'x';
+ snprintf(q, 3, "%02X", *p & 0xff);
+ q += 2;
+ }
+ }
+ }
+
+ *q = '\0';
+
+ return (buf);
+}
+
+
+/*
* dolog() - is a wrapper to syslog and printf depending on debug flag
*
*/
@@ -83,6 +140,7 @@ void
dolog(int pri, char *fmt, ...)
{
va_list ap;
+ char *buf, *sanitize;
va_start(ap, fmt);
@@ -92,14 +150,40 @@ dolog(int pri, char *fmt, ...)
*/
if (pri == LOG_DEBUG) {
- if (verbose && debug)
- vprintf(fmt, ap);
- else if (verbose)
+ if (verbose && debug) {
+ buf = malloc(1024);
+ if (buf == NULL) {
+ printf("-= failed to allocate memory for output buffer =-\n");
+ } else {
+ vsnprintf(buf, 1024, fmt, ap);
+ sanitize = input_sanitize(buf);
+ if (sanitize == NULL) {
+ printf("-= failed to allocate memory for output buffer =-\n");
+ } else {
+ printf("%s", sanitize);
+ free(sanitize);
+ }
+ free(buf);
+ }
+ } else if (verbose)
vsyslog(pri, fmt, ap);
} else {
- if (debug)
- vprintf(fmt, ap);
- else
+ if (debug) {
+ buf = malloc(1024);
+ if (buf == NULL) {
+ printf("-= failed to allocate memory for output buffer =-\n");
+ } else {
+ vsnprintf(buf, 1024, fmt, ap);
+ sanitize = input_sanitize(buf);
+ if (sanitize == NULL) {
+ printf("-= failed to allocate memory for output buffer =-\n");
+ } else {
+ printf("%s", sanitize);
+ free(sanitize);
+ }
+ free(buf);
+ }
+ } else
vsyslog(pri, fmt, ap);
}
repomaster@centroid.eu