News for hackers. Stuff that matters.
Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @03:49AM (#2973012)
p = (char *)(path + strlen(path) - 4);

This is a very bad idea as path is user-supplied and has to be treated as malicious. An attacker can omit the string-terminator...

Re:Kernel module for advanced rickrolling replaces open() call by exDM69 on Thursday September 08, 2011 @04:58AM (#2973159)
Out of curiosity, I checked out how the open syscall in linux works (see fs/open.c). It uses getname (from fs/namei.h), which effectively copies the filename from userland memory to a chunk of kernel memory. It uses strncpy_from_user to do this and uses PATH_MAX as the maximum length. So even the linux kernel does rely on the fact that there is a null terminator in the string.

So your assumption on omitting the null terminator is wrong, BUT there's another flaw in this. The length of the string can be less than four bytes. By allocating the string in a nice page boundary this can be used to cause a memory protection fault.

Re:Kernel module for advanced rickrolling replaces open() call by 0x12 on Thursday September 08, 2011 @06:26AM (#2973299)
> It uses strncpy_from_user to do this and uses PATH_MAX as the maximum length. So even the linux kernel does rely on the fact that there is a null terminator in the string.

The PATH_MAX guarantees that the string copy will terminate even if no null character is present.

Now if any routines that call strncpy_from_user assume that it leaves them with a null terminated string there is still a potential for trouble but that does not stem from the use of strncpy_from_user.

Check the implementation for do_getname:

http://lxr.free-electrons.com/source/fs/namei.c

And you'll see that it returns -ENAMETOOLONG when the name is >= the buffer allocated, only < len is allowed. (line 133 in the link above).

Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @05:05AM (#2973179)
> It uses strncpy_from_user to do this and uses PATH_MAX as the maximum length. So even the linux kernel does rely on the fact that there is a null terminator in the string.

But the kernel does not call strlen() on the user-supplied string...

Re:Kernel module for advanced rickrolling replaces open() call by muppetman on Thursday September 08, 2011 @03:51AM (#2973015)
You're inserting a dodgy bit of a code into a live running kernel. You are an attacker! I mean, valid technicality, but really? If you've got the ability to insert any module you want, you have the ability (ok, not always, but usually) to rm -rf /
Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @03:53AM (#2973023)
Bullshit.

Anyone who's using open() can kill the kernel after loading this module.

Re:Kernel module for advanced rickrolling replaces open() call by dexen on Thursday September 08, 2011 @04:22AM (#2973094)
I guess you meant, ``Anyone who's using open() can kill the kernel after somebody else loads the module.''. Which is far worse: any unprivileged user, say the victim of rickroll, can bite back hard.
Re:Kernel module for advanced rickrolling replaces open() call by gfodor on Thursday September 08, 2011 @05:53AM (#2973236)
This seems like a feature not a bug.
Re:Kernel module for advanced rickrolling replaces open() call by burgerbrain on Thursday September 08, 2011 @03:55AM (#2973027)
Probably best that any user can kill the kernel in this situation, if you stop to think about it.
Re:Kernel module for advanced rickrolling replaces open() call by muppetman on Thursday September 08, 2011 @04:14AM (#2973077)
Anyone who can load a kernel module can do _whatever they want_. That was my point, which you've obviously missed.
Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @04:16AM (#2973082)
And you are obviously missing that modern operating systems have more than just one user. :-)
Re:Kernel module for advanced rickrolling replaces open() call by muppetman on Thursday September 08, 2011 @04:26AM (#2973104)
I'm genuinely interested - what do you mean? It's my understanding (and please, correct me if I'm wrong) that if you're able to insmod a non-standard kernel module into a linux system, you are root already. I don't understand what this has to do with unix being multi-user, only root has the ability to insmod /tmp/haha.ko Please note I'm not talking about the ability of the kernel to automatically load modules as required. That's only from the /lib/modules path and you need to be root have to inserted a module in there anyway.

And if you have that ability, worrying about the security of a module such as this seems, to me, totally and utterly pointless, because you could be inserting a rootkit, or a module that randomly deletes a file every time you press the letter A etc.

So could you explain what you mean please? Why be concerned about security when security is already compromised? How does it being multi-user make a difference?

Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @04:31AM (#2973113)
Sure.

Suppose your admin is a funny guy and loads this kernel module at April 1st. I (Jonny non-root user) can no longer listen to my favorite mp3, because the kernel module changes the path all the time. The admin loughs and has fun.

But then later I call open() with some fancy arguments and take over the whole machine (or at least crash it). This is the part where I lough.

Re:Kernel module for advanced rickrolling replaces open() call by muppetman on Thursday September 08, 2011 @04:49AM (#2973142)
Ahhh right, yes, Ok, I see what you mean. If you loaded this on a serious system, you're insane. But right, I see your point.
Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @04:10AM (#2973065)
BTW: write_cr0(read_cr0() | 0x10000)

is a absolute no-go. It's a nice and funny kernel module but it dooms the kernel's security.

Re:Kernel module for advanced rickrolling replaces open() call by exDM69 on Thursday September 08, 2011 @04:44AM (#2973133)
For the uninitiated, cr0 is a control register in the x86 family of processors. Bit 16 of that register is (according to wikipedia) WP - Write protect: Determines whether the CPU can write to pages marked read-only.

What this means is that the kernel can no longer see if a user is writing to a read-only location, which is a major part of memory protection. It has major security implications. I don't know whether other parts of the kernel enable or disable this at context switches or at other times.

What this also means is that this kernel module will only work on x86 CPU's. So no rickrolling you ARM boxes or smartphones.

Re:Kernel module for advanced rickrolling replaces open() call by bstx on Thursday September 08, 2011 @10:44AM (#2974082)
> What this means is that the kernel can no longer see if a user is writing to a read-only location, which is a major part of memory protection.

No, disabling WP in CR0 is only relevant for privileged code. Writes to write protected pages in user mode will still trap. It might mess up some in-kernel COW stuff though.

Re:Kernel module for advanced rickrolling replaces open() call by adobriyan on Thursday September 08, 2011 @06:22AM (#2973291)
If we could take CR0 et al manipulations away from module authors, we'd do it long ago. Sigh...
Re:Kernel module for advanced rickrolling replaces open() call by jrockway on Thursday September 08, 2011 @07:11AM (#2973386)
This is why I avoid out-of-tree modules. At least someone had to glance over the in-tree module before I run it on my machine. When you download random kernel modules from the Internet, they're probably from people too lazy to have their code properly reviewed. And if they're too lazy to have their code reviewed, they probably don't feel too bad about disabling all page write protection, either.
Re:Kernel module for advanced rickrolling replaces open() call by KonradKlause on Thursday September 08, 2011 @07:29AM (#2973431)
Can you name such a module?
Re:Kernel module for advanced rickrolling replaces open() call by bonzini on Thursday September 08, 2011 @11:36AM (#2974306)
Not sure why it's needed, too. It could also restrict the rw/ro toggle to the init_rickroll and exit_rickroll functions.
[deleted]
Re:Kernel module for advanced rickrolling replaces open() call by merijnv on Thursday September 08, 2011 @04:52AM (#2973148)
Eh, the code does look for the string terminator. It calls strlen, which indexes the char* until it finds a NULL byte. If the string doesn't have a NULL byte then strlen will happily continue reading past the boundary until it either segfaults or finds a NULL byte.
Re:Kernel module for advanced rickrolling replaces open() call by forgotusername on Thursday September 08, 2011 @07:49AM (#2973485)
Just some random nitpickery, but "NULL" has a specific meaning in C, which is 0 cast to a void pointer. So preferably talk about a "NUL" (as it appears in most ASCII tables) or perhaps "null character" (which is what recent C standards prefer instead, cant remember why they changed though).
Re:Kernel module for advanced rickrolling replaces open() call by datenwolf on Thursday September 08, 2011 @12:24PM (#2974529)
And… you got your nitpickery wrong. A C null pointer is just 0. The exact definition in the statement is "a null pointer is a integral constant expression with value 0".

There are some stddef.h which cast it to a void*, but technically that's just a compatibility hack. See http://c-faq.com/null/macro.html http://c-faq.com/null/safermacs.html http://c-faq.com/null/nullor0.html http://c-faq.com/null/long0.html