Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite php memory for security reason?

I am actually working on a security script and it seems that I meet a problem with PHP and the way PHP uses memory.

my.php:

<?php
// Display current PID
echo 'pid= ', posix_getpid(), PHP_EOL;

// The user type a very secret key
echo 'Fill secret: ';
$my_secret_key = trim(fgets(STDIN));

// 'Destroty' the secret key
unset($my_secret_key);

// Wait for something
echo 'waiting...';
sleep(60);

And now I run the script:

php my.php
pid= 1402
Fill secret: AZERTY             <= User input
waiting...

Before the script end (while sleeping), I generate a core file sending SIGSEV signal to the script

kill -11 1402

I inspect the corefile:

 strings core | less

Here is an extract of the result:

...
fjssdd
sleep    
STDIN
AZERTY            <==== this is the secret key
zergdf
...

I understand that the memory is just released with the unset and not 'destroyed'. The data are not really removed (a call to the free() function)

So if someone dumps the memory of the process, even after the script execution, he could read $my_secret_key (until the memory space will be overwritten by another process)

Is there a way to overwrite this memory segment of the full memory space after the PHP script execution?


Thanks to all for your comments.

I already now how memory is managed by the system.

Even if PHP doesn't use malloc and free (but some edited versions like emalloc or efree), it seems (and I understand why) it is simply impossible for PHP to 'trash' after freeing disallowed memory.

The question was more by curiosity, and every comments seems to confirm what I previously intend to do: write a little piece of code in a memory aware language (c?) to handle this special part by allocating a simple string with malloc, overwriting with XXXXXX after using THEN freeing.

Thanks to all

J

like image 729
nemenems Avatar asked Aug 30 '11 13:08

nemenems


2 Answers

You seem to be lacking a lot of understanding about how memory management works in general, and specifically within PHP.

A discussion of the various salient points is redundant when you consider what the security risk is here:

So if someone dumps the memory of the process, even after the script execution

If someone can access the memory of a program running under a different uid then they have root access and can compromise the target in so many other ways - and it doesn't matter if it's PHP script, ssh, an Oracle DBMS....

If someone can access the memory previously occupied by a process which has now terminated, then not only have they got root, they've already compromised the kernel.

like image 140
symcbean Avatar answered Nov 16 '22 03:11

symcbean


You seem to have missed an important lesson in what computers mean by "delete operations".

See, it's never feasible for computer to zero-out memory, but instead they just "forget" they were using that memory.

In other words, if you want to clear memory, you most definitely need to overwrite it, just as @hakre suggested.

That said, I hardly see the point of your script. PHP just isn't made for the sort of thing you are doing. You're probably better off with a small dedicated solution rather than using PHP. But this is just my opinion. I think.

like image 44
Christian Avatar answered Nov 16 '22 01:11

Christian