Only way to write secure C code is to know C really well.
The first principle was security: . . . A consequence of this principle is that every occurrence of every subscript of every subscripted variable was on every occasion checked at run time against both the upper and the lower declared bounds of the array... I note with fear and horror that even in 1980, language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law.
Hoare’s Turing Award Lecture, 1980
array vs. pointer
semantics makes it harder in C.printf(str)
is bad metacharacters can confuse log files.printf("%s", str)
is good%n
is the worstprintf("Hello\n%n", &cnt)
can be used to overwrite memory location!Make error-case specific!
“File names may be up to 1024 bytes long”
vs
“File names may be up to 1024 bytes long; longer file names must be rejected”
A program whose behavior has not been specified cannot be buggy, only surprising.
popen()
and system()
Whitelist good characters
rather than blacklist bad characters
.