By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

memset + free

P: n/a
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Jun 10 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
dykeinthebox wrote:
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Since a conforming program cannot tell the difference,
the "as if rule" holds and the compiler is permitted to
delete the memset() call. For that matter, it is allowed
to delete the strcpy(), the if, and the malloc(): none of
them have any influence on the program's observable behavior,
so all of them can be optimized away.

What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 10 '07 #2

P: n/a
In article <f4**********@news6.zwoll1.ov.home.nl>,
dykeinthebox <dy**********@hotrmail.comwrote:
>Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
I don't believe there's any way that a strictly conforming program can
tell the difference, so the as-if rule allows the compiler to optimize
it away.

What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

Surprise your compiler. Write better code than it asks you to.
--Keith Thompson in comp.lang.c
Jun 10 '07 #3

P: n/a
dykeinthebox wrote, On 10/06/07 21:39:
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Since there is no way for a conforming program to tell the difference by
the "as-if" rule it is allowed to optimise out the memset.

If you are looking at this from a security perspective (i.e. you want to
guarantee that someone can't examine memory to get passwords) they I
suggest asking in a security group and/or a group dedicated to your
platform since C does not help you very much.
--
Flash Gordon
Jun 10 '07 #4

P: n/a
On Jun 10, 5:10 pm, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)
Why? AFAIK the libc should simply put the memory block back on the
free chain(s).

Regards,
Frodo B

Jun 10 '07 #5

P: n/a
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.

Regards,
Frodo B

Jun 11 '07 #6

P: n/a
Frodo Baggins <fr*********@gmail.comwrites:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
>allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
Probably not, but it might help if the data the pointer points to is
volatile.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 11 '07 #7

P: n/a
In article <f4**********@news6.zwoll1.ov.home.nl>,
dykeinthebox <dy**********@hotrmail.comwrote:
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
If the "SEC" here indicates that you're worried about security, you
need to describe just what you're trying to achieve. On a
general-purpose operating system, no other program should be able to
see the data: it will be zeroed before it's allocated to another
program. A user with normal privileges should not be able to
look at it, unless they're running the program.

In theory using volatile might force the write to occur, but in
practice I doubt any compiler does the optimisation anyway - it's hard
to imagine a situation where it would be worthwhile.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 11 '07 #8

P: n/a
dykeinthebox wrote:
>
#include <stdlib.h>
#include <string.h>

int main( void ) {
void *p = malloc( 4 );
if ( p ) {
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming
the memory allocation succeeded) guaranteed to be set to 0, just
before the memory is being deallocated; or is a compiler allowed
to 'optimize away' the call to memset?
Since, after the free call, nothing can tell the difference, it can
optimize it out.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #9

P: n/a
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 11 '07 #10

P: n/a
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
>On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.

You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.
If necessary (and I'm not convinced that it is), you could write your
own memset equivalent that takes a pointer to volatile whatever
argument.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 11 '07 #11

P: n/a
On Sun, 10 Jun 2007 20:11:33 -0700, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.

If necessary (and I'm not convinced that it is), you could write your
own memset equivalent that takes a pointer to volatile whatever
argument.
The standard sayeth (6.7.3 P5):

"If an attempt is made to modify an object defined with a
const-qualified type through use of an lvalue with non-const-qualified
type, the behavior is undefined. If an attempt is made to refer to an
object defined with a volatile-qualified type through use of an lvalue
with non-volatile-qualified type, the behavior is undefined."

....but I think you are right and I am incorrect, in this particular
case.

Since the memory was obtained from malloc(), the region of storage
itself is not truly volatile, even though the address returned by
malloc might be assigned to a volatile qualified pointer.

So in this case, casting away the volatile qualification to pass the
pointer to memset(), or any other function that requires a pointer to
non-volatile, would not produce undefined behavior.

The following example would:

#include <stdlib.h>
#include <string.h>

int main(void)
{
volatile int y;
memset((void *)&y, 0, sizeof y);
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 11 '07 #12

P: n/a
On Sun, 10 Jun 2007 23:55:03 -0000, Frodo Baggins
<fr*********@gmail.comwrote:
>On Jun 10, 5:10 pm, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
>What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)

Why? AFAIK the libc should simply put the memory block back on the
free chain(s).

Regards,
Frodo B
OT
Because let's say on your OS another process was able to allocate that
freed-up memory. Then, if that memory was never zeroed, that other
process would be able to see what was in that memory, which could be a
security issue. In this case, something in the chain should clear
that memory, either when it's freed or when it's allocated. That
could be libc or the OS or your app. It's suspected that this is what
the OP is trying to achieve.

Jim
Jun 11 '07 #13

P: n/a
On Jun 11, 2:16 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
dykeinthebox wrote:
Consider the following program:
#include <stdlib.h>
#include <string.h>
int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}
Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

Since a conforming program cannot tell the difference,
the "as if rule" holds and the compiler is permitted to
delete the memset() call. For that matter, it is allowed
to delete the strcpy(), the if, and the malloc(): none of
them have any influence on the program's observable behavior,
so all of them can be optimized away.

What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?
just curious to know how passwd/secret stuff can not be in core ? how
to do that ?

thanks,
-raxit sheth
>
--
Eric Sosman
esos...@acm-dot-org.invalid- Hide quoted text -

- Show quoted text -

Jun 12 '07 #14

P: n/a
dykeinthebox wrote:
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something reasonable...
I can't see any way this can be optimized away, you can even declare
errorFlag to be volatile if that helps.

However, as others have suggested - on most modern operating systems
should this not normally be a problem.
Jun 12 '07 #15

P: n/a
Johan Bengtsson wrote, On 12/06/07 17:55:
dykeinthebox wrote:
> strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something reasonable...
I can't see any way this can be optimized away, you can even declare
errorFlag to be volatile if that helps.
Easy, it can unconditionally not set errorFlag because it knows what the
result of the if should be. Then it can drop the memset.
However, as others have suggested - on most modern operating systems
should this not normally be a problem.
If you are seriously concerned about security there are various
problems. Even after being freed the memory could still be in a core
dump because it might not have been returned to the OS. However, the
best ways of handling this will require OS specific techniques,
including how to avoid it being paged to disk!
--
Flash Gordon
Jun 12 '07 #16

P: n/a
"Johan Bengtsson" <qw*******@hotmail.comwrote in message
news:Ud***************@newsb.telia.net...
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something
reasonable... I can't see any way this can be optimized away, you
can even declare errorFlag to be volatile if that helps.
It's entirely possible to optimize it away. There's no guarantee the
compiler won't inline memset() or that it can't do interprocedural
optimization to determine the "if" statement's condition will always be
false, which would then cause "errorFlag=1" to be optimized away as dead
code (volatile or not).

Most current compilers probably don't go that far, but I can't see any
reason they couldn't. The "as if" clause is a wonderful thing.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Jun 12 '07 #17

P: n/a
ra************@gmail.com wrote:
What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?

just curious to know how passwd/secret stuff can not be in core ? how
to do that ?
In this way the C standard takes an approach very much _unlike_ Java or C#.
The standard intends (in as much as history has borne the intention) to
provide a language with the minimal amount of library interfaces necessary
to provide portablility guarantees for the userbase (in knowledge and
environment). An interesting observation, I believe, is that the _broader_
the userbase the more minimal the language, since the relative merit and
value of any additional interface diminishes. I think this holds for other
languages as well, and serendipitously gives creedance to my prejudice that
monolithic language "packages"--Java/JDK, C#/.Net--just as well limit the
developer more than empower him.

Of course, with C you have to figure historical baggage (strncpy(), etc),
and also some committee tracking of longer term trends in usage which,
overtime, try to appreciate some common and widespread practices (i.e. the
standardization of fixed-width types a la uint32_t).

Others might say the totality of C99 blows my theory out of the water. Even
so, answer yourself this: does your toaster keep secrets?

Jun 12 '07 #18

P: n/a

"dykeinthebox" <dy**********@hotrmail.comha scritto nel messaggio
news:f4**********@news6.zwoll1.ov.home.nl...
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the
memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Try changing the declaration to void * volatile p. That should work.
Jun 15 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.