I am getting the Coverity warning Untrusted value as argument with the below code when I pass argv as an argument to test_run()
. It says tainted variable passed to tainted sink but I am not sure how to make argv not-tainted!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int
main (int argc, char **argv)
{
/* some code */
// char **nstr;
// nstr = malloc(sizeof(char*));
// if(!nstr) {
// return 1;
// }
// int i=0;
// for(i=0;i<argc;i++){
// nstr[i] = strdup(argv[i]);
// }
test_run(argc-1, argv, testf);
}
int test_run(int argc, char **argv, testf_handle testf)
{
if (!argv) {
return 1;
}
/* some code */
testf->prog_name = argv[0];
parse_context(&pc, argc, argv);
/*- - other code - -*/
}
I tried by using the commented code and pass nstr insted of argv but still getting same warning when at line:
nstr[i] = strdup(argv[i]);
I am not sure what sanity check I should do before passing it as an argument. I am using gcc compiler on a linux machine.
testf is an instance of structure test_framework which has several member variables.
Thanks.
You can make
argv
not tainted by checking it to ensure it conforms to some particular specification. For example, checking the length of the string under argv to ensure it's less than some upper limit, ensuring it doesn't contain bad character sequences, etc.Coverity will detect your sanitization attempts and the defect will be resolved once you actually are sanitizing your inputs.