libfaketime在golang中不起作用。

huangapple go评论104阅读模式
英文:

libfaketime doesn't work with golang

问题

我想让我的Go程序在运行于Ubuntu服务器(14.04)上时,通过supervisor进行守护,并使用一个虚假的服务器时间。

在我的supervisor配置中,我将以下命令用作执行命令:

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

程序可以运行,但显示的是当前时间。

根据这篇文章:
使用libfaketime改变进程的时间

由于静态链接或设置了setuid的程序无法使用libfaketime,因为这些程序无法使用LD_PRELOAD。

有没有办法让我编译后的Go程序使用faketime?

英文:

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?

答案1

得分: 8

问题在于faketime使用LD_PRELOAD环境变量来指示程序的动态加载器在启动时加载libfaketimelibfaketime会进行所谓的“interpositioning”操作,即用自己的副本替换正常的动态库例程,以便在进行未来的动态库调用时,libfaketime可以影响发生的情况。特别是,libfaketime会interpose与时间相关的调用,因此它能够向程序返回虚假值。

这个方法对大多数程序有效的原因是它们使用libc来进行系统调用。libc提供了与系统调用交互的高级函数,使系统编程更加容易。在大多数使用libc的语言中,二进制文件是动态链接的,这意味着二进制文件中实际上并不包含libc,而是期望在运行二进制文件时系统上存在一个已编译的libc版本(称为“目标文件”),并且可以在那时加载动态库。这种动态加载是通过LD_PRELOAD指令实现的,它改变了加载器的行为。

然而,Go语言有两个不同之处。首先,它是静态链接的,因此没有加载器会关注LD_PRELOAD。其次,它不使用libc,所以即使它是动态链接的,并且LD_PRELOAD的技巧起作用,libc也永远不会被调用,因此它仍然无法实现欺骗程序使用虚假时间函数的目标。

英文:

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.

huangapple
  • 本文由 发表于 2016年3月16日 08:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/36024872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定