Sanitize job[:id] tuple read from UNIX socket using a regex

This is a really tight check for a single "job[:id]" tuple, not much
escapes it, Coverity should be a lot more happy about our addressing
CWE-20 with this one.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2020-01-07 02:51:15 +01:00
1 parent b369e1cfa2
commit 55c49a0bf0
1 file changed
+18 -17
+18 -17
View File
@@ -24,6 +24,7 @@
#include "config.h" #include "config.h"
#include <ctype.h> /* isprint() */ #include <ctype.h> /* isprint() */
#include <errno.h> #include <errno.h>
#include <regex.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
@@ -159,29 +160,29 @@ char *uptime(long secs, char *buf, size_t len)
return buf; return buf;
} }
/* Allowed characters in job/id/name */ /*
static int isallowed(int ch) * Verify string argument is NUL terminated
{ * Verify string is a JOB[:ID], JOB and ID
return isprint(ch); * can both be string or number, or combo.
} */
/* Sanitize user input, make sure to NUL terminate. */
char *sanitize(char *arg, size_t len) char *sanitize(char *arg, size_t len)
{ {
size_t i = 0; const char *regex = "[a-z0-9_]+[:]?[a-z0-9_]*";
regex_t preg;
int rc;
while (i < len && isallowed(arg[i])) if (strlen(arg) > len)
i++; return NULL;
if (i + 1 < len) { if (regcomp(&preg, regex, REG_ICASE | REG_EXTENDED))
arg[i + 1] = 0; return NULL;
return arg;
}
if (i > 0 && arg[i] == 0) rc = regexec(&preg, arg, 0, NULL, 0);
return arg; regfree(&preg);
if (rc)
return NULL;
return NULL; return arg;
} }
/* /*