Inspired by @comintern.
Replacing /dev/null. Engaging sneaky mode. Requires kernel headers, superuser mode and a working compiler.
# make
# rm /dev/null
# insmod devnull.ko
# chmod go+rw /dev/null
Have fun.
Makefile:
MODULE := devnull
KVERS ?= $(shell uname -r)
KDIR ?= /lib/modules/$(KVERS)/build
KMAKE := make -C $(KDIR) M=$(PWD)
obj-m += $(MODULE).o
all:
$(KMAKE) modules
install:
$(KMAKE) modules_install
clean:
$(KMAKE) clean
Source code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/ioport.h>
#include <linux/device.h>
#include <linux/version.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define DEVICE_NAME "null"
#define MAJOR_NUMBER 0
MODULE_LICENSE("GPL");
MODULE_AUTHOR("nola <florian@n0la.org>");
MODULE_DESCRIPTION("/dev/null - memory leak style");
MODULE_VERSION("0.1");
MODULE_SUPPORTED_DEVICE("null");
static struct class *class_null;
static int major = 0;
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
static loff_t device_llseek(struct file *, loff_t, int);
static struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = &device_llseek,
.read = &device_read,
.write = &device_write,
.open = &device_open,
.release = &device_release
};
static int __init mod_init(void)
{
struct device *dev_null;
if ((major = register_chrdev(MAJOR_NUMBER, DEVICE_NAME, &fops)) < 0) {
return major;
}
/* create /dev/null
* We use udev to make the file.
*/
class_null = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(class_null)) {
unregister_chrdev(major, DEVICE_NAME);
return -EIO;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
dev_null = device_create(class_null, NULL, MKDEV(major, 0),
NULL, "%s", DEVICE_NAME
);
#else
dev_null = device_create(class_null, NULL, MKDEV(major, 0),
"%s", DEVICE_NAME
);
#endif
if (IS_ERR(dev_null)) {
class_destroy(class_null);
unregister_chrdev(major, DEVICE_NAME);
return -EIO;
}
return 0;
}
static void __exit mod_exit(void)
{
device_destroy(class_null, MKDEV(major, 0));
class_unregister(class_null);
class_destroy(class_null);
unregister_chrdev(major, DEVICE_NAME);
}
static int device_open(struct inode *inode, struct file *file)
{
file->f_pos = 0x00;
try_module_get(THIS_MODULE);
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
/* decrement usage count: Not. Uncomment the line for less fun. */
/* module_put(THIS_MODULE); */
return 0;
}
static loff_t device_llseek(struct file *filep, loff_t offs, int mode)
{
loff_t newpos;
switch (mode) {
case 2:
case 0:
newpos = offs;
break;
case 1:
newpos = filep->f_pos + offs;
break;
default:
return -EINVAL;
}
if (newpos < 0) {
return -EINVAL;
}
filep->f_pos = newpos;
return newpos;
}
static ssize_t device_read(struct file *filep, char *dst, size_t len,
loff_t *off)
{
char *buf = NULL;
if (dst == NULL || len == 0) {
return -EINVAL;
}
buf = kmalloc(sizeof(char) * len, GFP_KERNEL);
if (buf == NULL) {
return -EINVAL;
}
/* Do how a /dev/null does.
*/
memset(dst, 0, len);
*off += len;
return len;
}
static ssize_t device_write(struct file *filep, const char *src, size_t len,
loff_t *off)
{
char *buf = NULL;
buf = kmalloc(sizeof(char) * len, GFP_KERNEL);
if (buf == NULL) {
return -EINVAL;
}
*off += len;
return len;
}
module_init(mod_init);
module_exit(mod_exit);
Warning this may force you to reboot!
To remove it:
# rmmod -f devnull # or a reboot
# rm -rf /dev/null
# mknod /dev/null c 1 3
# chmod go+rw /dev/null
I'm closing this question as off-topic because it's asking for, in some sense, malicious software, which we do not allow. – Alex A. – 2016-02-05T23:05:27.283
8"Closing it should still make it hog memory" - if a program is a shell executable (like most of windows versions of scripting language interpreters are), closing its window will kill the program. – mniip – 2014-03-18T22:25:36.427
54Isn't this just
while(1)malloc(999);
? – Doorknob – 2014-03-18T22:32:21.96010I'm not sure if "Closing it should still make it hog memory" is compatible with "The application must be single threaded only." If no thread has a chunk of memory, the OS can take it back, right? – aebabis – 2014-03-18T22:34:41.767
Does writing an infinite string to a file count? – None – 2014-03-18T23:40:51.310
51Just run firefox 26 with a few tabs open running flash for a half hour. It'll bring your computer to its knees. – Braden Best – 2014-03-19T04:01:37.990
1@mniip. That's the whole point of the challenge. To make a difficult challenge. And doorknob. I wanted something different! ;) – George – 2014-03-19T08:15:27.617
@GeorgeH my previous comment was a troll (I apologize for that) See my answer which hangs computer in 2 second! Once started, your computer will hang so don't try! – Mukul Kumar – 2014-03-19T08:46:03.457
1"Closing it should still make it hog memory." without spawning some secret processes this is not possible on unices unless you exploit some kernel memory leak or mess directly with the os. You'd need a windows platform (preferably win98 or older) to do that with a conventional code. – orion – 2014-03-19T10:02:34.070
Is overriding garbage collection so that nothing happens a valid answer? – David Wilkins – 2014-03-19T12:37:25.157
@David. Thats fine, Garbage collection can be overridden for this. – George – 2014-03-19T15:54:56.953
Why specifically without fork bombs? Banning one technique is like saying "no Python" or "can't use BeOS". It just seems silly. – user2357112 supports Monica – 2014-03-21T01:41:43.173
You can always install Windows... that'll give you several interesting memory leaks, some of which have been there a really long time. You can also run malloc's, and store into the memory location given to you by malloc, until it barfs up and gives you a 0.L as an error code ("Nil Pointer"), then store into location 0, and see how long the machine lives. Judging by how many "zero-stores" I have seen in many places, Microsoft and Apple run neck and neck with Nil pointer references. grin Dave Small – None – 2014-03-21T02:25:00.993
1By "must be single threaded", do you mean "must not explicitly start any threads?" As a comment on the current most popular answer notes, some language runtimes have multiple threads behind the scenes, which effectively excludes those languages from the challenge. Furthermore, @acbabis raises a valid point. I don't know of any languages where "closing the program" wouldn't at least stop the main thread from running. Without at least allowing threads opened in the background by standard libraries (such as GUI threads) or the runtime itself, this seems like an impossible challenge. – jpmc26 – 2014-03-22T08:26:20.323
@jpmc26. No. I mean you cannot use memory by starting threads so like you cant use
for x in range(100000)
to spawn loads of threads to take up memory. – George – 2014-03-22T19:59:10.243How do you define "merely closing the app"? Closing all windows belonging to the process, or must the process itself be terminated? What if the program runs entirely in the background? What's the difference between "closing the app" and "killing the program" then? – hpsMouse – 2014-03-26T16:53:21.680
@hpsMouse. Closing is pressing the close button (the X in the top right corner). Killing it is going into task manager and clicking end task, or going into processes and ending the process – George – 2014-03-26T19:54:47.870
I wonder if I should remove the 10 days part!! – George – 2014-04-21T19:53:45.593