473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Buffer overflow protection

If we want our programs to be protected against buffer overflows, must we
check the size of the various containers explicitly?

E.g.

#include <iostream>
#include <string>
int main()
{
using namespace std;

string s;

while(cin>>s)
;

// ...
}
should become:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
using namespace std;

string s;

while(cin && s.size()<s.max_size())
{
char c;

cin>>c;

if(isspace(c))
continue;

s.push_back(c);
}

// ...
}


Ioannis Vranos
Jul 22 '05 #1
13 2787
"Ioannis Vranos" <iv*@guesswh.at.emails.ru> wrote in message
news:c5***********@ulysses.noc.ntua.gr...
If we want our programs to be protected against buffer overflows, must we
check the size of the various containers explicitly?

E.g.

#include <iostream>
#include <string>
int main()
{
using namespace std;
string s, temp;
while(cin>>temp)
s+=temp;
// ...
}
should become:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
using namespace std;

string s;

while(cin && s.size()<s.max_size())
{
char c;

cin>>c;

if(isspace(c))
continue;

s.push_back(c);
}

// ...
}


Ioannis Vranos


Jul 22 '05 #2
I'm puzzled. Is it even *possible* for s.size() to have a value greater
than s.max_size()? That would seem to violate the concept of "max",
wouldn't it? So, shouldn't the streaming operator prevent s.size() from
ever exceeding s.max_size() in the first place, making any such check on
your part redundant?

I know that checking for buffer overruns is important when filling arrays,
but I would think one of the advantages of using a string class and
streaming operators is to protect against such things.

But for filling arrays, I'd agree on your design, where you add one
character at a time. It's silly to try to see if you've *already* overrun
memory.

-Howard
Jul 22 '05 #3
On Tue, 13 Apr 2004 14:59:35 +0300 in comp.lang.c++, "Ioannis Vranos"
<iv*@guesswh.at.emails.ru> wrote,
If we want our programs to be protected against buffer overflows, must we
check the size of the various containers explicitly?
You need to ensure that the sizes are checked.
How explicit it is, is another matter.
string s;

while(cin>>s)


Here std::string and its operator>> do the checking.
So, it does not need to be explicit.

Jul 22 '05 #4
Howard wrote:
I'm puzzled. Is it even *possible* for s.size() to have a value greater
than s.max_size()? That would seem to violate the concept of "max",
wouldn't it?
I believe so. I was unable to find exact details, but I'm fairly sure
that attempting to exceed a container's max_size will fail for one
reason or another. I suspect it will fail due to memory exhaustion
before you get to that point, and if that doesn't occur it will probably
throw an exception. The standard definitely says that an exception will
be thrown in a few cases (like calling reserve() for a vector where the
new capacity is too large, if I recall correctly).
So, shouldn't the streaming operator prevent s.size() from
ever exceeding s.max_size() in the first place, making any such check on
your part redundant?


I think the container (or string) itself will prevent it.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #5
Ioannis Vranos wrote:
"Ioannis Vranos" <iv*@guesswh.at.emails.ru> wrote in message
news:c5***********@ulysses.noc.ntua.gr...
If we want our programs to be protected against buffer overflows, must we
check the size of the various containers explicitly?

E.g.

#include <iostream>
#include <string>
int main()
{
using namespace std;


string s, temp;
while(cin>>temp)
s+=temp;

// ...
}


The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else. If max_size() is reached first, I'm not 100% sure what
should happen. My guess is that an exception will be thrown, either
length_error or bad_alloc. In effect, exceeding max_size() means
exhausting the memory of the allocator I think, so bad_alloc might be
appropriate, though I suppose I should check the standard and see how
allocators are supposed to handle running out of memory.

From a security standpoint, if we assume that an overflow of this sort
could exist, it seems likely that an attack exploiting such an overflow
would have much less chance of succeeding than a traditional
fixed-buffer-length overflow attack. max_size() is probably up around 2
or 4 billion. That's a hell of a lot of data to dump into the program.
If it were a remote attack, it would take quite a while to transfer all
that (over 3 minutes on a 10 Mbps connection?), and I imagine a IDS
(which I know nothing about) would have a very good chance of detecting
that.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.earthl ink.net...

The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.

This can't happen on all modern systems using virtual memory (swap) file. If
one has 10 GB swap file my guess is that max_size() can be reached. I am too
bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.

If max_size() is reached first, I'm not 100% sure what
should happen. My guess is that an exception will be thrown, either
length_error or bad_alloc. In effect, exceeding max_size() means
exhausting the memory of the allocator I think, so bad_alloc might be
appropriate, though I suppose I should check the standard and see how
allocators are supposed to handle running out of memory.

From a security standpoint, if we assume that an overflow of this sort
could exist, it seems likely that an attack exploiting such an overflow
would have much less chance of succeeding than a traditional
fixed-buffer-length overflow attack. max_size() is probably up around 2
or 4 billion. That's a hell of a lot of data to dump into the program.
If it were a remote attack, it would take quite a while to transfer all
that (over 3 minutes on a 10 Mbps connection?), and I imagine a IDS
(which I know nothing about) would have a very good chance of detecting
that.

Buffer overflow attacks happen all the time. However i am not checking about
protection against attacks here. This is a general reliability question. If
there is no such a check implicitly in standard library containers, the
whole scenario will defeat the abstraction mechanism of the standard library
and i do not think this can happen anyway. In 15 minutes or so i 'll check
the standard and drop a message here.


Ioannis Vranos

Jul 22 '05 #7
Ioannis Vranos wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.earthl ink.net...
The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.


This can't happen on all modern systems using virtual memory (swap) file. If
one has 10 GB swap file my guess is that max_size() can be reached. I am too
bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.


I found this in the unofficial list of C++2003 changes:

Insert subclause 21.3, paragraph 4a:

4a For any string operation, if as a result of the operation,
size() would exceed max_size() then the operation throws
length_error.
Strangely, I couldn't find anything similar for other containers.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #8
Kevin Goodsell wrote:
Ioannis Vranos wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.earthl ink.net...
The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.



This can't happen on all modern systems using virtual memory (swap)
file. If
one has 10 GB swap file my guess is that max_size() can be reached. I
am too
bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.

I found this in the unofficial list of C++2003 changes:

Insert subclause 21.3, paragraph 4a:

4a For any string operation, if as a result of the operation,
size() would exceed max_size() then the operation throws
length_error.
Strangely, I couldn't find anything similar for other containers.


This document, by the way, came from Stroustrup's web page if I recall
correctly. If it's not there, then maybe it was Koenig's page.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #9
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:u9*****************@newsread2.news.pas.earthl ink.net...
Ioannis Vranos wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.earthl ink.net...
The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.


This can't happen on all modern systems using virtual memory (swap) file. If one has 10 GB swap file my guess is that max_size() can be reached. I am too bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.


I found this in the unofficial list of C++2003 changes:

Insert subclause 21.3, paragraph 4a:

4a For any string operation, if as a result of the operation,
size() would exceed max_size() then the operation throws
length_error.

Can you provide the URL please? And what is C++2003? I assume C++0x as it
had so far?


Regards,

Ioannis Vranos

Jul 22 '05 #10
Ioannis Vranos wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:u9*****************@newsread2.news.pas.earthl ink.net...
Ioannis Vranos wrote:

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.ear thlink.net...
The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.

This can't happen on all modern systems using virtual memory (swap)
file. If
one has 10 GB swap file my guess is that max_size() can be reached. I am
too
bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.


I found this in the unofficial list of C++2003 changes:

Insert subclause 21.3, paragraph 4a:

4a For any string operation, if as a result of the operation,
size() would exceed max_size() then the operation throws
length_error.


Can you provide the URL please? And what is C++2003? I assume C++0x as it
had so far?


I think this is it:

http://www.acceleratedcpp.com/author.../revisions.pdf

C++2003 is an official update to the language that includes corrections
and clarifications, but no new features.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #11
"Ioannis Vranos" <iv*@guesswh.at.emails.ru> wrote in message
news:c5**********@ulysses.noc.ntua.gr...

In 15 minutes or so i 'll check the standard and drop a message here.

From the C++98 standard:

<stdexcept>

19.1.4 Class length_error

namespace std {
class length_error : public logic_error {
public:
explicit length_error(const string& what_arg);
};
}

1 The class length_error defines the type of objects thrown as exceptions to
report an attempt to produce
an object whose length exceeds its maximum allowable size.
length_error(const string& what_arg);

2 Effects: Constructs an object of class length_error.

3 Postcondition: strcmp(what(), what_arg.c_str()) == 0.
....
21.3 Template class basic_string

3 In all cases, size() <= capacity().

4 The functions described in this clause can report two kinds of errors,
each associated with a distinct exception:
- a length error is associated with exceptions of type length_error
(19.1.4);
- an outofrange
error is associated with exceptions of type out_of_range (19.1.5).
[Elsewhere it mentions length error check in the constructors: "Throws:
length_error if n == npos". And in many other places. ]

....

vector throws it too: "Throws: length_error if n > max_size()."

basic_string (including string of course), and vector are the only standard
library containers that throw std::length_error exception.
Naturally i should not expect any such checks from classes of the kind
valarray. For classes like std::bitset i shall check some other time...


Regards,

Ioannis Vranos

Jul 22 '05 #12
"Ioannis Vranos" <iv*@guesswh.at.emails.ru> wrote in message
news:c5***********@ulysses.noc.ntua.gr...

Provided the length_error exception the reliability of the code can be
ensured:
#include <iostream>
#include <string>
#include <stdexcept>
int main()
{
using namespace std;

string s, temp;

try
{
while(cin>>temp)
s+=temp;
}

catch(length_error)
{
// ...
}

// ...
}
or

#include <iostream>
#include <string>
#include <stdexcept>
int main() try
{
using namespace std;

string s, temp;

while(cin>>temp)
s+=temp;

// ...
}

catch(std::length_error)
{
// ...
}


Ioannis Vranos

Jul 22 '05 #13
On Tue, 13 Apr 2004 23:20:09 +0300, "Ioannis Vranos"
<iv*@guesswh.at.emails.ru> wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Oy*****************@newsread2.news.pas.earth link.net...

The library is responsible for managing the buffers and preventing their
overflowing. I generally assume memory exhaustion will occur before
anything else.

This can't happen on all modern systems using virtual memory (swap) file. If
one has 10 GB swap file my guess is that max_size() can be reached. I am too
bored to check the standard but if noone provides an answer i shall be
forced to do so, since i can't live for long with such a question.


On a 32 bit system, you generally only have about 2GB of virtual
memory to play with - hitting this limit with modern software is a
real possibility. The size of the swap file and physical memory have
no bearing on this - it's an addressing problem that is solved only by
moving to 64-bit architecture, or coming up with another arcane
segmented pointer architecture like DOS's near and far pointers.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Sworder | last post by:
Hi there, I come from a Visual C++ background. When writing a service that's exposed to the Internet, I had to check the incoming data stream (from the client) VERY carefully. If a hacker was...
2
by: Ioannis Vranos | last post by:
If we want our programs to be protected against buffer overflows, must we check the size of the various containers explicitly? E.g. #include <iostream> #include <string> int main()
22
by: Tommy | last post by:
Hi all. I am studying computer security, and I got this short and simple (?) c-code. Something is logical wrong in this code, and if used in the wrong hands of someone, it could be taken advantage...
2
by: Tim::.. | last post by:
Hi... I have a major problem with a web application I am about to launch and just can't find out what the problem is... I believe it might be a Buffer Overflow problem but can't pin point the...
2
by: jay | last post by:
I am attempting to impersonate an account in ASPNET. I am using aspnet_setreg to store the username and passwords. I have given the ASPNET account permisision to read the registry values. However,...
5
by: Tim | last post by:
Hi, I'm experiencing some problem with the following code: st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite) br = New BinaryReader(st) Do Until br.PeekChar = -1 Dim buffer()...
2
by: Chris | last post by:
I have experienced the "Blank Message Box" problem when using McAfee 8 with Visual Studio and VB. I can disable buffer overflow protection and it fixes the problem, but it is only temporary as my...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
4
by: raashid bhatt | last post by:
do buffer overflow happens with global variables
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.