Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libfaketime doesn't work with golang

I would like my go program, which runs on ubuntu server (14.04), daemonized with supervisor, to use a fake server time.

In my supervisor config, I use this as the executing command:

"faketime 'last Friday 5 pm' /home/user/main"

The program runs, but displays the current time.

According to this article: Changing what time a process thinks it is with libfaketime

libfaketime cannot be used with statically linked or setuid programs, because LD_PRELOAD is not available to such programs.

Is there anyway to have my compiled go program use the faketime?

like image 773
Mike Avatar asked Feb 07 '23 11:02

Mike


1 Answers

The issue is that faketime uses the LD_PRELOAD environment variable to instruct the program's dynamic loader to load libfaketime at startup. libfaketime will do what's called "interpositioning" - replacing normal dynamic library routines with its own copies of those routines - so that when future dynamic library calls are made, libfaketime can influence what happens. In particular, libfaketime interposes time-related calls, and thus it is able to return fake values to the program.

The reason that this works for most programs is that they use libc to make syscalls. libc provides high-level functions for interacting with syscalls, making it easier to do systems programming. In most languages that use libc, binaries are dynamically linked, meaning that libc isn't actually included in the binary, but rather it is expected that a compiled version of libc (called an "object file") will exist on the system when the binary is run, and the dynamic library can be loaded at that point. This dynamic loading is what makes faketime possible by way of the LD_PRELOAD directive, which changes the loader's behavior.

Go, however, differs in two ways. First, it is statically linked, and thus there is no loader that could ever pay attention to LD_PRELOAD. Second, it doesn't use libc, so even if it were dynamically linked, and the LD_PRELOAD trick worked, libc would never be called anyway, so it still wouldn't actually accomplish the intended goal of tricking the program into using fake time functions.

like image 146
joshlf Avatar answered Feb 10 '23 01:02

joshlf