Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverity: iio_stresstest : fix Untrusted value as argument (TAINTED_SCALAR) #436

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
#define SAMPLES_PER_READ 256
#define NUM_TIMESTAMPS (16*1024)

#ifdef _MSC_BUILD
#define inline __inline
#define iio_snprintf sprintf_s
#else
#define iio_snprintf snprintf
#endif

static int getNumCores(void) {
#ifdef _WIN32
SYSTEM_INFO sysinfo;
Expand Down Expand Up @@ -82,11 +89,14 @@ static size_t cache_line_size(void)

#elif __linux__
FILE * p = 0;
int ret;

p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
if (p) {
fscanf(p, "%zu", &cacheline);
ret = fscanf(p, "%zu", &cacheline);
fclose(p);
if (ret != 1)
cacheline = 0;
}
#endif
return cacheline;
Expand Down Expand Up @@ -388,16 +398,28 @@ static void *client_thread(void *data)
return (void *)EXIT_FAILURE;
}

static unsigned long int clamp(const char *name, unsigned long int val,
static unsigned long int sanitize_clamp(const char *name, const char *argv,
unsigned long int min, unsigned long int max)
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: extra line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

unsigned long int val;
char buf[20];

if (!argv) {
val = 0;
} else {
/* sanitized buffer by taking first 20 (or less) char */
iio_snprintf(buf, sizeof(buf), "%s", argv);
val = strtoul(buf, NULL, 10);
}

if (val > max) {
val = max;
fprintf(stderr, "Clamped %s to max %lu", name, max);
fprintf(stderr, "Clamped %s to max %lu\n", name, max);
}
if (val < min) {
val = min;
fprintf(stderr, "Clamped %s to min %lu", name, min);
fprintf(stderr, "Clamped %s to min %lu\n", name, min);
}
return val;
}
Expand All @@ -412,7 +434,6 @@ int main(int argc, char **argv)
struct timeval start, end, s_loop;
void **ret;
size_t min_samples;
unsigned long int tmp;

#ifndef _WIN32
set_handler(SIGHUP, &quit_all);
Expand Down Expand Up @@ -447,21 +468,21 @@ int main(int argc, char **argv)
break;
case 'b':
info.arg_index += 2;
tmp = strtoul(info.argv[info.arg_index], NULL, 10);
/* Max 4M , min 64 bytes (cache line) */
info.buffer_size = clamp("buffersize", tmp, min_samples, 1024 * 1024 * 4);
info.buffer_size = sanitize_clamp("buffersize", info.argv[info.arg_index],
min_samples, 1024 * 1024 * 4);
break;
case 't':
info.arg_index +=2;
tmp = strtoul(info.argv[info.arg_index], NULL, 10);
/* ensure between least once a day and never (0) */
info.timeout = 1000 * clamp("timeout", tmp, 0, 60 * 60 * 24);
info.timeout = 1000 * sanitize_clamp("timeout", info.argv[info.arg_index],
0, 60 * 60 * 24);
break;
case 'T':
info.arg_index +=2;
tmp = strtoul(info.argv[info.arg_index], NULL, 10);
/* Max number threads 1024, min 1 */
info.num_threads = clamp("threads", tmp, 1, 1024);
info.num_threads = sanitize_clamp("threads", info.argv[info.arg_index],
1, 1024);
break;
case 'v':
if (!info.verbose)
Expand Down