LoginSignup
6
3

More than 5 years have passed since last update.

信頼できない実行ファイルに対してlddを使ってはいけない理由

Posted at

lddのmanより

Security
Be aware that in some circumstances (e.g., where the program specifies an ELF interpreter other than ld-linux.so), some versions of ldd may attempt to obtain the dependency information by attempting to directly execute the program(which may lead to the execution of whatever code is defined in the program's ELF interpreter, and perhaps to execution of the program itself).
Thus, you should never employ ldd on an untrusted executable, since this may result in the execution of arbitrary code.

要するに「依存するライブラリの情報を得るためにlddがそのバイナリ(のdynamic linker?)を実行してしまうケースが有るから」

(まあそもそもlddを使う時点で普通はその対象のファイルを実行するつもりなわけで問題になることはあまりないとは思うけど)

実際にやってみる

  • ソースコード
test.cc
#include <iostream>

int main() {
    std::cout << "!!!!!!" << std::endl ;
}
  • ビルド

1.lddした時に実行されるdynamic linker用にstatic linkでビルド
$ g++ test.cc -o loader -static
2.dynamic linkerに1を使うようにしてビルド
$ g++ -Wl,--dynamic-linker=./loader test.cc

  • 実行
$ ldd ./a.out
!!!!!!

確かにlddしただけで実行されてしまった。

対策

これまたlddのmanより

Thus, you should never employ ldd on an untrusted executable, since this may result in the execution of arbitrary code. A safer alternative when dealing with untrusted executables is:
$ objdump -p /path/to/program | grep NEEDED

信頼できないバイナリに対してはlddではなくobjdumpやreadelfを使いましょう。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3