Memory hog experiment

Memory hog experiment

Post by Ayr3s on Tue Jun 02, 2015 10:54 pm
([msg=88302]see Memory hog experiment[/msg])

I wanted to crash/make a system unusable by using a lot of memory. I set up a virtual machine running linux mint rebecca and started testing some code, but I didn't get the results I expected. So I asked about it in #coffeesh0p and got nice info about that from MadMouse and Zylopfa and I decided to try it again.

I searched the web for more info and found this website:
http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html?page=1
wich has good info about the subject and some code!
I tried the examples from the website and modified the second one slightly to make my memory hog:

Code: Select all
#include <stdlib.h>
#include <stdio.h>

int main() {
void *block = NULL;
int count = 0;
while(1){
block = malloc(1024*1024);
if (!block) fork();
else memset(block, 1, 1024*1024);
fork();
printf("%d MB\n", ++count);
}
return 0;
}


Bad joke warning!
I call it brucewillis.c because it is hard to kill. :lol:
It does what I wanted and every time I tried running it in my virtual machine it got so slow that I had to kill the vm and start it again. I tried it with 512Mb and 2048Mb of RAM (no swap) and both freezed very quickly after starting the program.

The thing is, for now I didn't have the time to do much more than that, but I want to understand better how linux manages memory, so I'll be trying more stuff as soon as I have the time to do so and I'll update here with new info. :)
User avatar
Ayr3s
New User
New User
 
Posts: 29
Joined: Mon Feb 02, 2015 2:34 pm
Blog: View Blog (0)


Re: Memory hog experiment

Post by ghost107 on Wed Jun 03, 2015 1:32 am
([msg=88304]see Re: Memory hog experiment[/msg])

I think your VM got slower because of the extensive CPU usage:
Your code doesn't contain only allocating memory, it also contain an infinite loop, and a fork, which forks your process.
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Memory hog experiment

Post by Ayr3s on Wed Jun 03, 2015 2:47 pm
([msg=88312]see Re: Memory hog experiment[/msg])

Indeed, when I run the program virtualbox just slams to the execution cap. I couldn't see that in htop inside the vm because htop also freezes and stops updating, but I can see how much cpu it's using from the outside. Thanks for pointing that out! :D
User avatar
Ayr3s
New User
New User
 
Posts: 29
Joined: Mon Feb 02, 2015 2:34 pm
Blog: View Blog (0)


Re: Memory hog experiment

Post by Iblist on Thu Jun 04, 2015 6:56 am
([msg=88326]see Re: Memory hog experiment[/msg])

Looks like a fork bomb that also mallocs a bunch of memory while creating child processes.

Try running this in your virtual machine.

Code: Select all
int main()
{
    while (1) fork();
}


It's basically the same thing, but without mallocing any memory.

The way Linux handles memory, to my limited knowledge, is using process IDs. When a program, this fork bomb for example, is run memory is allocated and a process ID assigned to that specific process. A fork bomb is just a program that makes a copy of itself, eventually using up all the system's memory.

Fork bombs are my favourite payload, because they're very noticeable when they work, and they don't (read shouldn't) damage the victims pc. And since they're so simple to write, if I ever see an unlocked laptop with an Internet Explorer icon on it...well, that icon won't be running Internet Explorer for much longer.
Those who create and rely upon brilliant and complex creations are often destroyed by some idiot plugging an infected usb stick somewhere they shouldn't have.
User avatar
Iblist
Experienced User
Experienced User
 
Posts: 68
Joined: Fri Jul 11, 2014 12:05 pm
Blog: View Blog (0)


Re: Memory hog experiment

Post by Ayr3s on Fri Jun 12, 2015 10:59 pm
([msg=88514]see Re: Memory hog experiment[/msg])

Okay, so I think this one is easier on the CPU than the last one. I removed all the forking and left only malloc and memset. It still has the infinite loop, but now it waits a little when it fails to allocate 256 bytes of RAM. The kernel kills it very quickly after starting.
Thanks guys!

Code: Select all
#include <unistd.h>

int main() {
void *block = NULL;
int count = 0;
int blocksize = 1024*1024;
while(1){
  block = malloc(blocksize);
    if (!block){
        blocksize = blocksize/2;
    }
    else {
        memset(block, 1, blocksize);
    }
    if (blocksize < 256){
        blocksize = 1024*1024;
        sleep(30);
    }
} //while loop
return 0;
} //main

User avatar
Ayr3s
New User
New User
 
Posts: 29
Joined: Mon Feb 02, 2015 2:34 pm
Blog: View Blog (0)


Re: Memory hog experiment

Post by pretentious on Fri Jun 19, 2015 7:36 am
([msg=88576]see Re: Memory hog experiment[/msg])

I like stuff like this. My version was just mallocing a billion times in a loop until the system froze, I've never learned about forking.

It's subtle, but I think you've reached a mile stone in your jurney when it occurs to you, how to overwhelm a system with 3 lines of code, without any special privileges.
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests