As other responses have noted, gets()
doesn't check the buffer space. In addition to accidental overflow problems, this weakness can be used by malicious users to create all sorts of havoc.
One of the first widespread worms, released in 1988, used gets()
to propogate itself throughout the internet. Here's an interesting excerpt from Expert C Programming by Peter Van Der Linden which discusses how it worked:
The Early Bug gets() the Internet Worm
The problems in C are not confined to just the language. Some routines in the standard library have unsafe semantics. This was dramatically demonstrated in November 1988 by the worm program that wriggled through thousands of machines on the Internet network. When the smoke had cleared and the investigations were complete, it was determined that one way the worm had propagated was through a weakness in the finger
daemon, which accepts queries over the network about who is currently logged in. The finger
daemon, in.fingerd
, used the standard I/O routine gets()
.
The nominal task of gets()
is to read in a string from a stream. The caller tells it where to put the incoming characters. But gets()
does not check the buffer space; in fact, it can't check the buffer space. If the caller provides a pointer to the stack, and more input than buffer space, gets()
will happily overwrite the stack. The finger
daemon contained the code:
main(argc, argv)
char *argv[];
{
char line[512];
...
gets(line);
Here, line
is a 512-byte array allocated automatically on the stack. When a user provides more input than that to the finger
daemon, the gets()
routine will keep putting it on the stack. Most architectures are vulnerable to overwriting an existing entry in the middle of the stack with something bigger, that also overwrites neighboring entries. The cost of checking each stack access for size and permission would be prohibitive in software. A knowledgeable malefactor can amend the return address in the procedure activation record on the stack by stashing the right binary patterns in the argument string. This will divert the flow of execution not back to where it came from, but to a special instruction sequence (also carefully deposited on the stack) that calls execv()
to replace the running image with a shell. Voilà, you are now talking to a shell on a remote machine instead of the finger
daemon, and you can issue commands to drag across a copy of the virus to another machine.
Ironically, the gets()
routine is an obsolete function that provided compatibility with the very first version of the portable I/O library, and was replaced by standard I/O more than a decade ago. The manpage even strongly recommends that fgets()
always be used instead. The fgets()
routine sets a limit on the number of characters read, so it won't exceed the size of the buffer. The finger
daemon was made secure with a two-line fix that replaced:
gets(line);
by the lines:
if (fgets(line, sizeof(line), stdin) == NULL)
exit(1);
This swallows a limited amount of input, and thus can't be manipulated into overwriting important locations by someone running the program. However, the ANSI C Standard did not remove gets()
from the language. Thus, while this particular program was made secure, the underlying defect in the C standard library was not removed.