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:

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?

  • Its probably to accidental calls from, for example, memory corruption or making a typo in a syscall number. Whether that's actually something worth worrying about, don't know. – derobert Jul 19 '14 at 20:44
  • @derobert Makes sense but... wouldn't all system calls need a protection like this? Except 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... – lgeorget Jul 19 '14 at 20:58
  • Indeed. I'm not sure if at one point Linus was chasing down a bug that kept #$&#! rebooting his machine, or if someone just added it because they were paranoid, etc. What it's supposed to protect against is fairly clear; why protection from that is needed is a good question. – derobert Jul 19 '14 at 21:08
  • @derobert Possibly a holdover from someone used to integrated circuits, where many chips implement some kind of guard to enable shutdown functionality (you must write XYZ bits to a special register before the shutdown instruction works, for example). Not sure if the timelines for that make sense though, – mbrig May 28 '18 at 21:59

This question has been answered in this Super User question:

Basically, a bit flip in an address can cause a program to think it is calling one system call when, in fact, it's calling the reboot() system call. Because reboot() is a very destructive, non-syncing operation that erases the state of the system -- thus erasing the evidence of the bit-flip problem that would otherwise be exposed as a program error or panic -- Linux includes extra protections around its successful use.

Interestingly enough, the second set of magic numbers correspond to the birthdays of Linus and his three daughters:

  • 1
    Ah, I didn't think about verifying the other sites. I've never thought this question could belong other than here. Anyway, thank you for your answer. :-) – lgeorget Jul 20 '14 at 7:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.