Changeset 4089466


Ignore:
Timestamp:
Feb 19, 2016 5:01:40 PM (5 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
bd49b1d
Parents:
7d6fe01
Message:

Clock: Add sanity checks to detect invalid system clock

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/Clock.java

    r7d6fe01 r4089466  
    11package net.i2p.util;
    22
     3import java.util.Date;
    34import java.util.Set;
    45import java.util.concurrent.CopyOnWriteArraySet;
    56
    67import net.i2p.I2PAppContext;
     8import net.i2p.time.BuildTime;
    79import net.i2p.time.Timestamper;
    810
     
    2022public class Clock implements Timestamper.UpdateListener {
    2123    protected final I2PAppContext _context;
     24    protected final boolean _isSystemClockBad;
    2225    protected long _startedOn;
    2326    protected boolean _statCreated;
     
    2932        _context = context;
    3033        _listeners = new CopyOnWriteArraySet<ClockUpdateListener>();
    31         _startedOn = System.currentTimeMillis();
     34        long now = System.currentTimeMillis();
     35        long min = BuildTime.getEarliestTime();
     36        long max = BuildTime.getLatestTime();
     37        // If the system clock is obviously bad, set our offset so our time is something "close"
     38        // We do not call setOffset() here as it sets _alreadyChanged.
     39        // Don't use Log here.
     40        if (now < min) {
     41            // positive offset
     42            _offset = min - now;
     43            System.out.println("ERROR: System clock is invalid: " + new Date(now));
     44            now = min;
     45            _isSystemClockBad = true;
     46        } else if (now > max) {
     47            // negative offset
     48            _offset = max - now;
     49            System.out.println("ERROR: System clock is invalid: " + new Date(now));
     50            now = max;
     51            _isSystemClockBad = true;
     52        } else {
     53            _isSystemClockBad = false;
     54        }
     55        _startedOn = now;
    3256    }
    3357
     
    6488     * Specify how far away from the "correct" time the computer is - a positive
    6589     * value means that the system time is slow, while a negative value means the system time is fast.
     90     *
    6691     * Warning - overridden in RouterClock
    6792     *
     
    7196        long delta = offsetMs - _offset;
    7297        if (!force) {
    73             if ((offsetMs > MAX_OFFSET) || (offsetMs < 0 - MAX_OFFSET)) {
     98            if (!_isSystemClockBad && (offsetMs > MAX_OFFSET || offsetMs < 0 - MAX_OFFSET)) {
    7499                Log log = getLog();
    75100                if (log.shouldLog(Log.WARN))
     
    129154   
    130155    public void setNow(long realTime) {
     156        if (realTime < BuildTime.getEarliestTime() || realTime > BuildTime.getLatestTime()) {
     157            Log log = getLog();
     158            String msg = "Invalid time received: " + new Date(realTime);
     159            if (log.shouldWarn())
     160                log.warn(msg, new Exception());
     161            else
     162                log.logAlways(Log.WARN, msg);
     163            return;
     164        }
    131165        long diff = realTime - System.currentTimeMillis();
    132166        setOffset(diff);
     
    134168
    135169    /**
     170     *  Warning - overridden in RouterClock
     171     *
    136172     *  @param stratum ignored
    137173     *  @since 0.7.12
    138174     */
    139175    public void setNow(long realTime, int stratum) {
    140         long diff = realTime - System.currentTimeMillis();
    141         setOffset(diff);
     176        setNow(realTime);
    142177    }
    143178
  • history.txt

    r7d6fe01 r4089466  
     12016-02-19 zzz
     2 * Clock: Add sanity checks to detect invalid system clock
     3
     42016-02-18 zzz
     5 * Console: Clean up display and form handling
     6   for specifying a fixed host name or IP on /confignet
     7 * Crypto: Add utilities for loading CRLs from disk;
     8   check for revocation when reading in certificates
     9 * Transport:
     10   - Implement mayDisconnect() for outbound connections also,
     11     use when publishing RI directly to floodfill
     12   - Run SSU idle disconnect check faster if floodfill or near connection limit
     13 * NetDB: Fix bug publishing router info too often
     14
    1152016-02-17 zzz
    216 * i2psnark: Increase max files per torrent to 2000
  • router/java/src/net/i2p/router/RouterClock.java

    r7d6fe01 r4089466  
    11package net.i2p.router;
    22
     3import java.util.Date;
    34import java.util.Set;
    45import java.util.concurrent.CopyOnWriteArraySet;
     
    67import net.i2p.data.DataHelper;
    78import net.i2p.router.time.RouterTimestamper;
     9import net.i2p.time.BuildTime;
    810import net.i2p.time.Timestamper;
    911import net.i2p.util.Clock;
     
    109111        long delta = offsetMs - _offset;
    110112        if (!force) {
    111             if ((offsetMs > MAX_OFFSET) || (offsetMs < 0 - MAX_OFFSET)) {
     113            if (!_isSystemClockBad && (offsetMs > MAX_OFFSET || offsetMs < 0 - MAX_OFFSET)) {
    112114                Log log = getLog();
    113115                if (log.shouldLog(Log.WARN))
     
    219221    @Override
    220222    public void setNow(long realTime, int stratum) {
     223        if (realTime < BuildTime.getEarliestTime() || realTime > BuildTime.getLatestTime()) {
     224            Log log = getLog();
     225            String msg = "Invalid time received: " + new Date(realTime);
     226            if (log.shouldWarn())
     227                log.warn(msg, new Exception());
     228            else
     229                log.logAlways(Log.WARN, msg);
     230            return;
     231        }
    221232        long diff = realTime - System.currentTimeMillis();
    222233        setOffset(diff, stratum);
  • router/java/src/net/i2p/router/RouterVersion.java

    r7d6fe01 r4089466  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 6;
     21    public final static long BUILD = 7;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.