1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-27 22:50:26 +03:00

lib: Use wrapper for string to integer conversion

In order to detect an value overflow error during
the string to integer conversion with strtoul/strtoull,
the errno variable must be set to zero before the execution and
checked after the conversion is performed. This is achieved by
using the wrapper function strtoul_err and strtoull_err.

Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Böhme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Swen Schillig 2019-01-28 12:54:07 +01:00 committed by Jeremy Allison
parent cef18c2dfd
commit c9f4b92a61
7 changed files with 52 additions and 23 deletions

View File

@ -358,6 +358,7 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
while (key != NULL && *key != '\0') {
char *next_key;
char *val;
int error = 0;
next_key = strchr_m(key, ',');
if (next_key != NULL) {
@ -369,7 +370,10 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
*val++ = 0;
if (strequal_m(key, "speed")) {
*speed = (uint64_t)strtoull(val, NULL, 0);
*speed = (uint64_t)strtoull_err(val, NULL, 0, &error);
if (error != 0) {
DBG_DEBUG("Invalid speed value (%s)\n", val);
}
} else if (strequal_m(key, "capability")) {
if (strequal_m(val, "RSS")) {
*cap |= FSCTL_NET_IFACE_RSS_CAPABLE;
@ -380,7 +384,10 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
"'%s'\n", val);
}
} else if (strequal_m(key, "if_index")) {
*if_index = (uint32_t)strtoul(val, NULL, 0);
*if_index = (uint32_t)strtoul_err(val, NULL, 0, &error);
if (error != 0) {
DBG_DEBUG("Invalid key value (%s)\n", val);
}
} else {
DBG_DEBUG("Key unknown: '%s'\n", key);
}
@ -515,9 +522,12 @@ static void interpret_interface(char *token)
return;
}
} else {
int error = 0;
char *endp = NULL;
unsigned long val = strtoul(p, &endp, 0);
if (p == endp || (endp && *endp != '\0')) {
unsigned long val;
val = strtoul_err(p, &endp, 0, &error);
if (p == endp || (endp && *endp != '\0') || error != 0) {
DEBUG(2,("interpret_interface: "
"can't determine netmask value from %s\n",
p));

View File

@ -18,6 +18,7 @@
*/
#include "replace.h"
#include "util/util.h"
#include "system/network.h"
#include "system/filesys.h"
#include "system/dir.h"
@ -1458,6 +1459,7 @@ static int messaging_dgm_read_unique(int fd, uint64_t *punique)
{
char buf[25];
ssize_t rw_ret;
int error = 0;
unsigned long long unique;
char *endptr;
@ -1467,13 +1469,15 @@ static int messaging_dgm_read_unique(int fd, uint64_t *punique)
}
buf[rw_ret] = '\0';
unique = strtoull(buf, &endptr, 10);
unique = strtoull_err(buf, &endptr, 10, &error);
if ((unique == 0) && (errno == EINVAL)) {
return EINVAL;
}
if ((unique == ULLONG_MAX) && (errno == ERANGE)) {
return ERANGE;
if (error != 0) {
return error;
}
if (endptr[0] != '\n') {
return EINVAL;
}
@ -1615,6 +1619,7 @@ int messaging_dgm_forall(int (*fn)(pid_t pid, void *private_data),
struct messaging_dgm_context *ctx = global_dgm_context;
DIR *msgdir;
struct dirent *dp;
int error = 0;
if (ctx == NULL) {
return ENOTCONN;
@ -1637,8 +1642,8 @@ int messaging_dgm_forall(int (*fn)(pid_t pid, void *private_data),
unsigned long pid;
int ret;
pid = strtoul(dp->d_name, NULL, 10);
if (pid == 0) {
pid = strtoul_err(dp->d_name, NULL, 10, &error);
if ((pid == 0) || (error != 0)) {
/*
* . and .. and other malformed entries
*/

View File

@ -22,6 +22,7 @@
#include "source3/lib/gencache.h"
#include "lib/util/debug.h"
#include "lib/util/strv.h"
#include "lib/util/util.h"
#include "lib/util/talloc_stack.h"
#include "lib/util/charset/charset.h"
#include "libcli/security/dom_sid.h"
@ -105,6 +106,7 @@ static void namemap_cache_find_sid_parser(
const char *domain;
const char *name;
const char *typebuf;
int error = 0;
char *endptr;
unsigned long type;
@ -123,11 +125,8 @@ static void namemap_cache_find_sid_parser(
return;
}
type = strtoul(typebuf, &endptr, 10);
if (*endptr != '\0') {
return;
}
if ((type == ULONG_MAX) && (errno == ERANGE)) {
type = strtoul_err(typebuf, &endptr, 10, &error);
if ((*endptr != '\0') || (error != 0)) {
return;
}
@ -253,6 +252,7 @@ static void namemap_cache_find_name_parser(
const char *sid_endptr;
const char *typebuf;
char *endptr;
int error = 0;
struct dom_sid sid;
unsigned long type;
bool ok;
@ -276,11 +276,8 @@ static void namemap_cache_find_name_parser(
return;
}
type = strtoul(typebuf, &endptr, 10);
if (*endptr != '\0') {
return;
}
if ((type == ULONG_MAX) && (errno == ERANGE)) {
type = strtoul_err(typebuf, &endptr, 10, &error);
if ((*endptr != '\0') || (error != 0)) {
return;
}

View File

@ -253,6 +253,7 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t
char *p2;
char *syscmd = NULL;
int _id = -1;
int error = 0;
switch(qtype) {
case SMB_USER_QUOTA_TYPE:
@ -285,7 +286,11 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t
/* we need to deal with long long unsigned here, if supported */
dp->qflags = strtoul(line, &p2, 10);
dp->qflags = strtoul_err(line, &p2, 10, &error);
if (error != 0) {
goto invalid_param;
}
p = p2;
while (p && *p && isspace(*p)) {
p++;

View File

@ -389,13 +389,22 @@ bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
{
char *str;
uint64_t result;
int error = 0;
str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
if (str == NULL) {
DEBUG(10, ("Could not find attribute %s\n", attr));
return false;
}
result = strtoull(str, NULL, 10);
result = strtoull_err(str, NULL, 10, &error);
if (error != 0) {
DBG_DEBUG("Attribute conversion failed (%s)\n",
strerror(error));
TALLOC_FREE(str);
return false;
}
TALLOC_FREE(str);
*presult = result;
return true;

View File

@ -851,14 +851,15 @@ uint64_t conv_str_size(const char * str)
{
uint64_t lval;
char * end;
int error = 0;
if (str == NULL || *str == '\0') {
return 0;
}
lval = strtoull(str, &end, 10 /* base */);
lval = strtoull_err(str, &end, 10, &error);
if (end == NULL || end == str) {
if (end == NULL || end == str || error != 0) {
return 0;
}

View File

@ -366,6 +366,7 @@ bld.SAMBA3_LIBRARY('messages_dgm',
PTHREADPOOL
msghdr
genrand
samba-util
''',
private_library=True)
@ -1363,6 +1364,7 @@ bld.RECURSE('smbd/notifyd')
bld.RECURSE('rpcclient')
bld.RECURSE('utils')
bld.RECURSE('nmbd')
bld.RECURSE('lib/util')
bld.ENFORCE_GROUP_ORDERING()
bld.CHECK_PROJECT_RULES()