jselvi / Delorean

NTP Main-in-the-Middle tool
222 stars 65 forks source link

Can this tool be used to set the leap second flag? #1

Closed hany closed 8 years ago

hany commented 8 years ago

I find this tool interesting to experiment with the issues caused by leap seconds. Can this be used to set a leap second flag that systems will respect?

jselvi commented 8 years ago

@hany I don't know if you can add a leap second using NTP since, at the low level, NTP sends timestamps in epoch-like format (adding an additional field for fractions of seconds), so what you really have is an amount of seconds (and fractions of seconds) since 01/01/1970 (several amounts of seconds, to be precise).

Perhaps you could use the "-s -1s" flag to send computers a second into the past and force a situation similar to the leap second, but currently most OSs only update the clock if there is a big clock change. If there is a small change, what most of them do is to slow down or speed up the internal clock, so each second would be slightly longer (or shorter) until the clock is synchronized, so I don't think you could force a leap second using NTP.

Nice article about this stuff: http://developerblog.redhat.com/2015/06/01/five-different-ways-handle-leap-seconds-ntp/

Anyway, I could be wrong, because I haven't go in depth on this, so if you find a way to do it, I'll be happy to include your technique in the tool.

hany commented 8 years ago

@jselvi thanks for the reply! I do believe that NTP is responsible for setting the leap second flag that systems can choose to respect. The Linux kernel is one such system that does respect this flag (and in fact, a bug fix in 2007 in the NTP code of the kernel was responsible for the fiasco in 2012). Here's more info on how NTP deals with leap seconds.

Setting the clock 1 second back, even with NTP, is not sufficient to trigger this same issue. It requires the "leap second flag" to be set. I noticed several references to the "leap" flag in the source of delorean.py, which prompted me to open this issue. It seems that the ntpserver.py source that this project was inspired from also supports the leap second.

jselvi commented 8 years ago

@hany OK, I can see the point. To be honest, I don't really know how the leap second feature works in NTP. As you said, that function is basically what I borrowed from ntpserver.py so I have used the same names. I think ntpserver.py hardcoded the value, but I can tell you 100% sure, since I did that part around two years ago.

Anyway, I'm going to keep this issue open and I'll go in depth on this as soon as I have some spare time to add this feature to Delorean. Thanks a lot!

hany commented 8 years ago

Thanks @jselvi. I may take a stab at this myself. I know it's been a while since this was created, but do you recall what the purpose of these lines are? Based on my initial reading of the code, 'leap' will always be set to 0.

jselvi commented 8 years ago

From RFC5905:

 LI Leap Indicator (leap): 2-bit integer warning of an impending leap
 second to be inserted or deleted in the last minute of the current
 month with values defined in Figure 9.

      +-------+----------------------------------------+
      | Value | Meaning                                |
      +-------+----------------------------------------+
      | 0     | no warning                             |
      | 1     | last minute of the day has 61 seconds  |
      | 2     | last minute of the day has 59 seconds  |
      | 3     | unknown (clock unsynchronized)         |
      +-------+----------------------------------------+

                    Figure 9: Leap Indicator

As far as I remember, I use '0' by default and I overwrite it with '3' when I send responses, but you could just change it and hardcode '1' or '2' for a quick test.