was reading Robert Love’s book, chapter 5 on syscalls, and found this simple example a bit questionable:
asmlinkage long sys_silly_copy(unsigned long *src, unsigned long *dst, unsigned long len)
{
unsigned long buf;
if (copy_from_user(&buf, src, len))
return -EFAULT;
...
}
As we see ‘buf’ is object of type ‘unsigned long’ and defined on the kernel stack, i.e. its initial value is likely garbage. Anyway is it valid to copy ‘len’ bytes in the stack where buf is, i.e. it could overwrite something useful? Perhaps this is fine only for this particular example?
It is very questionable. In fact, it’s downright dangerous. I’ll give the author the benefit of the doubt here since they’re just trying to show how
copy_from_userandcopy_to_userwork but they really should have provided an example that wasn’t so dangerous.Especially since the book waxes lyrical about how you must be extra careful:
and then provides a means for the user to totally annihilate the kernel 🙂
The text from the copy I have states:
Other than the catastrophic failure of not checking parameters, I’m pretty certain the last parameter of the
SYSCALL_DEFINE3is missing a comma (though that would just be a typo).A far better example, without having to allocate arbitrary memory, would be along the lines of:
Anyone trying to implement that system call would be well advised to steer away from that particular sample in the book, although I suppose, at a bare minimum, it will give you some good kernel debugging experience 🙂