[PLUG] Faking system time (was: Sun)

Shannon C. Dealy dealy at deatech.com
Wed Aug 21 03:44:30 UTC 2002


On Tue, 20 Aug 2002, Wil Cooley wrote:

[snip]
> You don't need kernel hacks to do this; just a loadable library.  In fact, I
> can't actually think of how to do this in the kernel, but doing it through
> a preloaded library should be fairly straightforward.
[snip]

This isn't entirely true.  You can create your own library with just the
functions you wish to replace, and use LD_PRELOAD to force your program to
use your custom version of these functions, but only if the program you
are running is not statically linked to the functions you are trying to
replace.  Most free software developers will use dynamically linked,
shared libraries because it makes their executables smaller, allows the
library functions to be updated without rebuilding their code, and there
is no major reason not to.  On the other hand, it has been my experience
that most commercial software developers/vendors will go with statically
linked programs because they often don't care that much about executable
program size on PC's, and static linking ensures that every customer is
running with the same, known version of the libraries, which makes
technical support and finding/fixing bugs much easier.

Of course, some vendors do use shared libraries, so it might work for this
particular case.  For a quick and dirty example of this:

====== libfaketime.c =========
/*  Override date/time related library function(s) */
#include <stdlib.h>
#include <time.h>

time_t time(time_t *tloc)
{
   time_t RetVal;
   struct tm FixedTime;

   FixedTime.tm_sec = 0;      /* Current time 00:00:00 */
   FixedTime.tm_min = 0;
   FixedTime.tm_hour = 0;
   FixedTime.tm_mday = 1;     /* 1st day of month */
   FixedTime.tm_mon = 0;      /* January */
   FixedTime.tm_year = 102;   /* 2002 */

   RetVal = mktime(&FixedTime);

   // Return the time value
   if (tloc) {
      *tloc = RetVal;
   }
   return RetVal;
}
======== End of File ==============
To create the library, put the above code in the file "libfaketime.c"
and run this command:

   gcc -shared -Wl,-soname,libfaketime.so -o libfaketime.so libfaketime.c

Now to test it execute this line:

  LD_PRELOAD=./libfaketime.so date

this will run the standard Linux "date" command, forcing it to use the
dummy library, and will print out the fixed time value hard coded in the
library instead of the system date/time.  This should work with any
program that uses the "time" function and is dynamically linked, however,
there at least a couple potentially serious limitations:

   1 - If the program expects the system time to change during execution
       this may cause problems (though of course a more sophisticated
       version could be created which provides some kind of time
       increment).
   2 - There are other platform specific date/time functions which the
       program could use instead, though a good programmer won't unless
       there is a real need for it, since "time" is the POSIX standard
       routine and ensures portability.  Unfortunately, in my experience
       there are alot of bad programmers out there :-(  Of course if they
       used other routines and didn't statically lin them, they could be
       added to the library and overridden as well.

FWIW

Shannon C. Dealy      |               DeaTech Research Inc.
dealy at deatech.com     |          - Custom Software Development -
                      |    Embedded Systems, Real-time, Device Drivers
Phone: (800) 467-5820 | Networking, Scientific & Engineering Applications
   or: (541) 451-5177 |                  www.deatech.com






More information about the PLUG mailing list