* Re: Strange errors from strtok()
@ 1997-12-01 21:09 Przemek Klosowski
0 siblings, 0 replies; 7+ messages in thread
From: Przemek Klosowski @ 1997-12-01 21:09 UTC (permalink / raw)
Your CGI program
#include <stdio.h>
#include <stdlib.h>
char postline[80];
...
main() {
..
while(gets(postline)!= NULL) {
..
}
}
When I try to compile this program I get the following warnings:
rsvp.c:20: warning: assignment makes pointer from integer without a cast
rsvp.c:25: warning: assignment makes pointer from integer without a cast
rsvp.c(.text+0x8c): the `gets' function is dangerous and should not be used.
I would forget the pointer/integer problem for now; the third warning basically
says that your CGI script is a wide-open security hole. The exploit: someone
sends you a line exceeding 80 characters; gets() obediently overflows your
measly 80 bytes and writes over other local variables, and then over the returning
stack frame. With some cunning, the attackers put a fake stack frame pointing
to a chunk of their code in the same overlong CGI line (it requires some skill, but
is trivial otherwise). This results in them being able to run any code with the
permissions/privileges of the web server process. You should be using fgets(),
which will guarantee that your buffer will not overflow.
As to your strtok() problems, you should be including <string.h>. I
think stdlib.h includes some but not all str... routines. Your code
may have worked on Intel, but just fortuitously. In general, gcc -Wall
is the right thing to do---it would have alerted you to lack of prototypes.
It is good practice to resolve all warnings given by -Wall.
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread[parent not found: <199712012014.NAA14201@ellpspace.math.ualberta.ca>]
* Re: Strange errors from strtok()
[not found] <199712012014.NAA14201@ellpspace.math.ualberta.ca>
@ 1997-12-01 20:37 ` Rob Zook
1997-12-01 22:15 ` Michal Jaegermann
0 siblings, 1 reply; 7+ messages in thread
From: Rob Zook @ 1997-12-01 20:37 UTC (permalink / raw)
At 01:14 PM 12/1/97 -0700, you wrote:
>Rob Zook wrote:
>> I'm trying to use the strtok function from stdlib in a program and
> ^^^^^^^^^^^^^^^^^^^^
> strtok() is not in stdlib
>> I get these strange errors:
>
>...therefore errors are not strange at all.
Well, they seemed so to me at the time :-D.
>> [rzook@babylon5 cgi-bin]$ cc -o rsvp rsvp.c
>> rsvp.c: In function `main':
>> rsvp.c:20: warning: assignment makes pointer from integer without a cast
>> rsvp.c:25: warning: assignment makes pointer from integer without a cast
>
>strtok() is not declared, so compiler assumes a default 'int' as a type
>of a return value. You try to assign this to a pointer. This is a bug
>in your code.
But why didn't I get some kind of link error? Why did it produce an
executible at all if strtok was not declared or defined in my code?
>> /tmp/cca197261.o: In function `main':
>> rsvp.c(.text+0x8c): the `gets' function is dangerous and should not be
used.
>
>Indeed! Anybody feeding your program a line longer than your buffer
>will crash it or worse (manipulating your stack one can make your
>program to execute any code at all). Big fun in a CGI context when
>you apparently are heading.
I know, gets() can produce dangerours results, and I plan to add a
dynamic allocation for postline based on the results of CONTENT_LENGTH,
but not while I'm testing.
Besides, just using a pointer in c can cause dangerous results, so
this message from the c compiler looks more like somebody at GNU's
personal problem to me.
Rob Z.
--------------------------------------------------------
Not reaching a goal on a given day does not preclude
reaching it tomorrow, or next year. One failure does not
mean all is lost. Let it be motivation to improve, to
deliver optimum performance in the next undertaking,
whatever it may be - not to give up and quit trying.
-- Capt. Spock
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: Strange errors from strtok()
1997-12-01 20:37 ` Rob Zook
@ 1997-12-01 22:15 ` Michal Jaegermann
0 siblings, 0 replies; 7+ messages in thread
From: Michal Jaegermann @ 1997-12-01 22:15 UTC (permalink / raw)
Rob Zook wrote:
> >strtok() is not declared,
....
> But why didn't I get some kind of link error? Why did it produce an
> executible at all if strtok was not declared or defined in my code?
strtok() is not declared, making compiler unhappy, but it still exists
in libraries which are passed to a linker. Linker does not know
anything about declarations and headers, it cares only about resolving
symbols, so your program will link.
Now compiler produced some code, assuming that all undeclared types
defaulted to 'int' and this may, or may not, be what was expected.
On 32-bit machine, where various integer types and pointers are all four
byte wide, there is, unfortunately, a pretty good chance that all this
fits and bugs are masked. But if your machine is 16-bit, or 64-bit -
like Alpha, and a silent assumptions of "the same width" fails, you may
be heading straight into a "crash and burn" land.
>
> I know, gets() can produce dangerours results, and I plan to add a
> dynamic allocation for postline based on the results of CONTENT_LENGTH,
> but not while I'm testing.
Using fgets() instead of gets() does not put much more burden on you
nor requires a dynamic allocation. Do not forget about trailing '\0'.
> Besides, just using a pointer in c can cause dangerous results, so
> this message from the c compiler looks more like somebody at GNU's
> personal problem to me.
A little knowledge of a C programming is a very dangerous thing.
The problem is not very personal as attested by numerous security
bugs caused by buffer overflows and stack smashing.
Michal
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <19971201115108.03468@dot.cygnus.com>]
* Re: Strange errors from strtok()
[not found] <19971201115108.03468@dot.cygnus.com>
@ 1997-12-01 20:16 ` Rob Zook
0 siblings, 0 replies; 7+ messages in thread
From: Rob Zook @ 1997-12-01 20:16 UTC (permalink / raw)
At 11:51 AM 12/1/97 -0800, you wrote:
>On Mon, Dec 01, 1997 at 01:03:09PM -0600, Rob Zook wrote:
>> I'm trying to use the strtok function from stdlib in a program and
>> I get these strange errors:
>
>Except that strtok isn't in <stdlib.h>, but in <string.h>.
<snip>
Yup, adding "include <string.h>" to my program removed the compile
warnings, but I still got the strange behavior in my loop.
When I tested this cgi, the postline string had a value of
"name=Rob+Zook&extension=1111&guest=yes&beverage=beer"
tokenPtr = strtok(postline,"&");
do
{
printf("<B>TokenPtr</B> = %s<BR>\n", tokenPtr);
}
while((tokenPtr = strtok(NULL,"&"))==NULL);
-----------------------------------------^^
And I goofed here. I should have said !=NULL. Haven't had to use a
do-while loop in a while.
Thanks to all who helped!
Rob Z.
--------------------------------------------------------
Not reaching a goal on a given day does not preclude
reaching it tomorrow, or next year. One failure does not
mean all is lost. Let it be motivation to improve, to
deliver optimum performance in the next undertaking,
whatever it may be - not to give up and quit trying.
-- Capt. Spock
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <01bcfe7f$60288060$323ccbd0@zeke.bainet.com>]
* Strange errors from strtok()
[not found] <01bcfe7f$60288060$323ccbd0@zeke.bainet.com>
@ 1997-12-01 19:03 ` Rob Zook
1997-12-01 19:51 ` Richard Henderson
1997-12-01 20:14 ` Michal Jaegermann
0 siblings, 2 replies; 7+ messages in thread
From: Rob Zook @ 1997-12-01 19:03 UTC (permalink / raw)
Hi all,
Hopefully, someone here has done some c programming on these Alphas.
I have a UDB and I'm running RedHat 4.1 w/ some of the RedHat 4.2
upgrades.
I'm trying to use the strtok function from stdlib in a program and
I get these strange errors:
#include <stdio.h>
#include <stdlib.h>
main()
{
char postline[80];
char tokenName[10][80];
char tokenValue[10][80];
char Name[30];
char Extension[9];
char Friend[5];
char *tokenPtr;
printf("Content-type: text/html\n\n");
printf("<HTML>\n<BODY>\n");
printf("<B>CONTENT LENGTH</B> = %d<BR>\n",
atoi(getenv("CONTENT_LENGTH")));
while(gets(postline)!= NULL)
{
printf("%s<BR>\n", postline);
tokenPtr = strtok(postline,"&");<<<<<<<<
do
{
printf("<B>TokenPtr</B> = %s<BR>\n", tokenPtr);
}
while((tokenPtr = strtok(NULL,'&'))==NULL); <<<<<<<<<
}
printf("</BODY>\n</HTML>\n");
}
When I try to compile this program I get the following warnings:
[rzook@babylon5 cgi-bin]$ cc -o rsvp rsvp.c
rsvp.c: In function `main':
rsvp.c:20: warning: assignment makes pointer from integer without a cast
rsvp.c:25: warning: assignment makes pointer from integer without a cast
/tmp/cca197261.o: In function `main':
rsvp.c(.text+0x8c): the `gets' function is dangerous and should not be used.
In the above program lines 20 and 25 are marked with the "<<<<<<<".
I'm not sure why I'd get these warnings. cc produces an executable, but
When I run this cgi program from my web form, it produces a server error.
If I add a fflush(stdout), just before the do loop, the server error goes
away and I get some output. However, the execution only goes thru the
do loop once, and then next call to strtok evidently returns a NULL.
The results of the program with the fflush(stdout) look like this:
CONTENT LENGTH = 52
name=Rob+Zook&extension=1111&guest=yes&beverage=beer
TokenPtr = name=Rob+Zook
As you can see the returned postline has 52 characters in it, and
with a call to strtok like: tokenPtr = strtok(NULL,'&') I should
get 4 tokens back. But I only get one.
To summarize:
1. Why do the warnings occur?
2. Why does my loop fail on the second call to strtok?
Thanks,
Rob Z.
--------------------------------------------------------
Not reaching a goal on a given day does not preclude
reaching it tomorrow, or next year. One failure does not
mean all is lost. Let it be motivation to improve, to
deliver optimum performance in the next undertaking,
whatever it may be - not to give up and quit trying.
-- Capt. Spock
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: Strange errors from strtok()
1997-12-01 19:03 ` Rob Zook
@ 1997-12-01 19:51 ` Richard Henderson
1997-12-01 20:14 ` Michal Jaegermann
1 sibling, 0 replies; 7+ messages in thread
From: Richard Henderson @ 1997-12-01 19:51 UTC (permalink / raw)
On Mon, Dec 01, 1997 at 01:03:09PM -0600, Rob Zook wrote:
> I'm trying to use the strtok function from stdlib in a program and
> I get these strange errors:
Except that strtok isn't in <stdlib.h>, but in <string.h>.
r~
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Strange errors from strtok()
1997-12-01 19:03 ` Rob Zook
1997-12-01 19:51 ` Richard Henderson
@ 1997-12-01 20:14 ` Michal Jaegermann
1 sibling, 0 replies; 7+ messages in thread
From: Michal Jaegermann @ 1997-12-01 20:14 UTC (permalink / raw)
Rob Zook wrote:
> I'm trying to use the strtok function from stdlib in a program and
^^^^^^^^^^^^^^^^^^^^
strtok() is not in stdlib
> I get these strange errors:
...therefore errors are not strange at all.
> [rzook@babylon5 cgi-bin]$ cc -o rsvp rsvp.c
> rsvp.c: In function `main':
> rsvp.c:20: warning: assignment makes pointer from integer without a cast
> rsvp.c:25: warning: assignment makes pointer from integer without a cast
strtok() is not declared, so compiler assumes a default 'int' as a type
of a return value. You try to assign this to a pointer. This is a bug
in your code.
> /tmp/cca197261.o: In function `main':
> rsvp.c(.text+0x8c): the `gets' function is dangerous and should not be used.
Indeed! Anybody feeding your program a line longer than your buffer
will crash it or worse (manipulating your stack one can make your
program to execute any code at all). Big fun in a CGI context when
you apparently are heading.
Michal
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~1997-12-01 22:15 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-12-01 21:09 Strange errors from strtok() Przemek Klosowski
[not found] <199712012014.NAA14201@ellpspace.math.ualberta.ca>
1997-12-01 20:37 ` Rob Zook
1997-12-01 22:15 ` Michal Jaegermann
[not found] <19971201115108.03468@dot.cygnus.com>
1997-12-01 20:16 ` Rob Zook
[not found] <01bcfe7f$60288060$323ccbd0@zeke.bainet.com>
1997-12-01 19:03 ` Rob Zook
1997-12-01 19:51 ` Richard Henderson
1997-12-01 20:14 ` Michal Jaegermann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox