wgarvin wrote:hgm wrote:Except that the programs that really suffered buffer overflow because of this already had been fixed long before. After all, doing a strcpy(a, b) as while(*a++ = *b++); can only end in two ways: either it works perfectly, or a lies within the sting b, and it will lead to an infinite repetitive string, certainly causing a segfault.
No buffer overflow would ever be caught by this measure that would not have segfaulted by itself.
It only makes a difference for the harmless cases, that worked absolutely correctly.
This post by Dann Corbit in the openchess thread of 2 weeks ago seems to me to be evidence that overlapping calls to strcpy can malfunction without necessarily causing a segfault.
Code: Select all
GCC gave me this:
dcorbit@dcorbit /q/cc
$ cat bozo.c
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char b[32];
strcpy(b, "123456789012345");
strcpy(b + 1, b);
printf("[%s]\n", b);
return 0;
}
dcorbit@dcorbit /q/cc
$ gcc -Wall -ansi -pedantic bozo.c
dcorbit@dcorbit /q/cc
$ ./a
[1123456788012345]
Look at it carefully, is it what you expected?
That's because of their hand-coded/optimized code that does different things depending on how many bytes you copy. It is quirky. And you want to know what is REALLY funny? this damned mavericks library did NOT detect that overlap.
Now isn't that absolutely-frickin' amazing? Here is the code:
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char b[64];
int i;
printf("overlap pass 1\n");
for (i=1;i<20;i++) {
strcpy(b, "123456789012345");
strcpy(b + i, b);
printf("(%d) [%s] strlen=%d\n", i, b, strlen(b));
}
printf("overlap pass 2\n");
for (i=1;i<20;i++) {
strcpy(b, "123456789012345");
strcpy(b, b + i);
printf("(%d) [%s] strlen=%d\n", i, b, strlen(b));
}
return 0;
}
Here is the output on mavericks:
scrappy% ./tst2
overlap pass 1
(1) [1123456788012345] strlen=15
(2) [12123456787812345] strlen=15
(3) [123123456786782345] strlen=15
(4) [1234123456785678345] strlen=15
(5) [12345123456784567845] strlen=15
(6) [123456123456783456785] strlen=15
(7) [1234567123456782345678] strlen=15
(8) [123456781234567812345678] strlen=15
(9) [1234567891234567891234567] strlen=15
(10) [12345678901234567890123456] strlen=15
(11) [123456789011234567890112345] strlen=15
(12) [1234567890121234567890121234] strlen=15
(13) [12345678901231234567890123123] strlen=15
(14) [123456789012341234567890123412] strlen=15
(15) [1234567890123451234567890123451] strlen=15
(16) [123456789012345] strlen=15
(17) [123456789012345] strlen=15
(18) [123456789012345] strlen=15
(19) [123456789012345] strlen=15
overlap pass 2
Abort
So isn't that absolutely-frickin' wonderful? They abort on the overlap that is perfectly safe, they ignore the one that causes the problems. What a WONDERFUL group of library folks, wouldn't you agree? They couldn't even break the most dangerous case.
wow..