As I was reading Linux source code, and more specifically the system calls code, I came across sys_reboot
implementation: http://lxr.free-electrons.com/source/kernel/reboot.c#L199.
199 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
200 void __user *, arg)
201 {
202 ...
...
286 }
In the middle, there is this specific piece of code:
209
210 /* For safety, we require "magic" arguments. */
211 if (magic1 != LINUX_REBOOT_MAGIC1 ||
212 (magic2 != LINUX_REBOOT_MAGIC2 &&
213 magic2 != LINUX_REBOOT_MAGIC2A &&
214 magic2 != LINUX_REBOOT_MAGIC2B &&
215 magic2 != LINUX_REBOOT_MAGIC2C))
216 return -EINVAL;
I wonder what kind of "safety" it actually provides. I mean, is it to prevent misuse? In this case, as the parameters are public, any library or application could misuse the system call even though they are required to pass the parameters. What did I miss?
getpid
and the like of course, they can all have unpredictable consequences if misused. Or maybe, all other syscalls already have enough parameters to be checked to make them harmless if they're wrongly called. It feels like a strange idea of "safety" to me, though...