mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-19 04:24:18 +00:00
Harden security-sensitive input handling (#3045)
* harden security-sensitive input handling * enable ctest failure checks in ci * add valgrind leak smoke tests * split tests into separate workflow
This commit is contained in:
+42
-7
@@ -27,9 +27,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#ifndef __MINGW32__
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#else
|
||||
@@ -104,6 +105,40 @@ ss_isnumeric(const char *s)
|
||||
return *s == '\0';
|
||||
}
|
||||
|
||||
int
|
||||
ss_parse_int(const char *s, int min_value, int max_value, int *out)
|
||||
{
|
||||
char *endptr;
|
||||
long value;
|
||||
|
||||
if (s == NULL || *s == '\0' || out == NULL || min_value > max_value) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
value = strtol(s, &endptr, 10);
|
||||
if (errno == ERANGE || endptr == s || *endptr != '\0' ||
|
||||
value < min_value || value > max_value) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = (int)value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ss_parse_uint16_port(const char *s, uint16_t *out)
|
||||
{
|
||||
int value;
|
||||
|
||||
if (out == NULL || ss_parse_int(s, 1, UINT16_MAX, &value) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = (uint16_t)value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* setuid() and setgid() for a specified user.
|
||||
*/
|
||||
@@ -214,16 +249,16 @@ run_as(const char *user)
|
||||
char *
|
||||
ss_strndup(const char *s, size_t n)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
char *ret;
|
||||
|
||||
if (len <= n) {
|
||||
return strdup(s);
|
||||
size_t len = 0;
|
||||
while (len < n && s[len] != '\0') {
|
||||
len++;
|
||||
}
|
||||
|
||||
ret = ss_malloc(n + 1);
|
||||
strncpy(ret, s, n);
|
||||
ret[n] = '\0';
|
||||
ret = ss_malloc(len + 1);
|
||||
memcpy(ret, s, len);
|
||||
ret[len] = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user