LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2006, 11:32 AM   #1
Seniltai
LQ Newbie
 
Registered: Nov 2005
Location: Friesland, The Netherlands
Distribution: Fedora Core 5 Test 2 / FreeBSD 6 / Windows XP SP2
Posts: 17

Rep: Reputation: 0
Smashing the stack protection


I'm currently working a wrapper function for a network class, when I stumbled on a question. Tried googling it, but I couldn't find the answer there, so I decided to post it here.

Code:
const char* CRakPacket::GetNullTerminatedString(int MaxLength)
{
	int length = strlen(data + location) + 1; // \0 included

	// check if the length isn't bigger than a specified value, or that it surpasses the end of the pointer (buffer overrun)
	if(length > MaxLength || location + length > size)
		return 0;

	// store the returned pointer because the location has to be moved later.
	char* retptr = data + location;
	// set the current location to the proper position.
	location += length;

	return retptr;
}
As you can see i'm using strlen() to check if the data buffer has no null-terminator and thus copying it would cause a buffer overrun. Now my question is this: When strlen passes outside the area of allocated memory, can it crash on certain compilers (it worked on G++), or is it better for me to make a strlen function that checks whether it's outside the allocated memory?
 
Old 04-21-2006, 12:12 PM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Quote:
When strlen passes outside the area of allocated memory,
What do mean "when" ? If ANY function steps outside allocated memory, then your program is broken. Also, is there some specific reason you're using a char *. With C++, you're better off using a string object. If you have to use char * for some reason, then you need to check the bounds yourself. And never call libc string functions with a non-terminated string.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference b/t Kernel stack and User stack hazzyb Linux - Software 2 09-29-2008 07:40 PM
Looking for opinions: Best stack protection for Linux? chort Linux - Security 3 11-10-2004 02:43 PM
write protection speed_viper Linux - Software 5 10-02-2004 12:10 AM
Virus protection wmeler Linux - Security 1 02-23-2004 11:16 AM
Data protection .... phoeniXflame General 4 03-23-2003 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration