10

A few days back I had an interview, and I was asked to write a program in C which crashes the system/which shuts down the system. Needless to say I felt pretty dumb having no clue on how to even approach :(

Still I gave it a try, writing programs which use a lot of memory. But my interviewer was not satisfied with any of my techniques.

Venugopal Madathil
  • 2,031
  • 3
  • 34
  • 44

5 Answers5

9

It's easy to write a program that invokes undefined or implementation-defined behavior. Some of those programs could potentially crash the system.

But by definition, this is inconsistent. And modern OSes take pains (though not 100% successfully) to prevent a rogue app from crashing the system.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
4

There is no portable way to write a C program that crashes the system.

A fork bomb might or might not bog down a system. Of course fork is not portable -- and a system can defend itself against such attacks by limiting the number of processes a given account can create.

Of course there's always this:

#include <stdio.h>
int main(void) {
    puts("HEY YOU, PULL THE PLUG!!");
    return 0;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

Well, Have you tried following ?

void main(void) {
    system("shutdown -P 0");
}

To execute this program on Linux you must log in as root.

Stephan
  • 41,764
  • 65
  • 238
  • 329
openstk
  • 137
  • 9
2

I would try writing garbage to /dev/kmem. There is a good chance that would cause an irrecoverable system crash.

makes
  • 6,438
  • 3
  • 40
  • 58
  • There's also a good chance that a normal user will be unable to write to `/dev/kmem`; indeed, the system is very badly setup indeed if that is successful (as a normal user). As root, all bets are off: `dd if=/dev/random of=/dev/kmem` will probably do the trick quite nicely. – Jonathan Leffler Oct 03 '11 at 06:45
1

One way to do that is by exploiting "Privilege Escalation" vulnerabilities of the current system.

Based on current configuration, you can search for vulnerabilities that impact the system. E.g. based on Kernel version. And then escalate privileges to root.

Once the process is "root", it can shutdown the system in various ways. Sending SIGPWR to "init" process is one clean way of doing that.

Shamit Verma
  • 3,839
  • 23
  • 22