Skip to content

Instantly share code, notes, and snippets.

@ecki
Last active July 6, 2017 02:16
Show Gist options
  • Save ecki/77cc3249437fdefad073261b6e93de7c to your computer and use it in GitHub Desktop.
Save ecki/77cc3249437fdefad073261b6e93de7c to your computer and use it in GitHub Desktop.
Overwrite libc rename() for Java with LD_PRELOAD
# bash on Windows
/mnt/c/ws/github/JavaSystemTest$ sudo apt install openjdk-8-jre-headless gcc ltrace strace
Paketlisten werden gelesen... Fertig
Abhängigkeitsbaum wird aufgebaut.
Statusinformationen werden eingelesen.... Fertig
»gcc« ist bereits die neuste Version (4:5.3.1-1ubuntu1).
»ltrace« ist bereits die neuste Version (0.7.3-5.1ubuntu4).
»strace« ist bereits die neuste Version (4.11-1ubuntu3).
»openjdk-8-jre-headless« ist bereits die neuste Version (8u131-b11-0ubuntu1.16.04.2).
0 aktualisiert, 0 neu installiert, 0 zu entfernen und 40 nicht aktualisiert.
/mnt/c/ws/github/JavaSystemTest$ gcc -fPIC -shared -o libpreload.so wrap.c -ldl
/mnt/c/ws/github/JavaSystemTest$ LD_PRELOAD=./libpreload.so /usr/lib/jvm/java-8-openjdk-amd64/bin/java -cp target/classes/ net.eckenfels.test.javasystemtest.RenameFiles a b
hello world
wrapped rename(a, b)
Exception in thread "main" java.nio.file.NoSuchFileException: a -> b
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixCopyFile.move(UnixCopyFile.java:396)
at sun.nio.fs.UnixFileSystemProvider.move(UnixFileSystemProvider.java:262)
at java.nio.file.Files.move(Files.java:1395)
at net.eckenfels.test.javasystemtest.RenameFiles.main(RenameFiles.java:25)
/*
* RenameFiles.java
*
* created at 2017-07-06 by Bernd Eckenfels <[email protected]>
*
* License: ASL 2.0
*/
package net.eckenfels.test.javasystemtest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class RenameFiles
{
public static void main(String[] args) throws IOException
{
System.out.println("hello world");
Path a = Paths.get(args[0]);
Path b = Paths.get(args[1]);
Files.move(a, b, StandardCopyOption.ATOMIC_MOVE);
System.exit(0);
}
}
/*
* wrap.c
*
* created at 2017-07-06 by Bernd Eckenfels <[email protected]>
*
* License: ASL 2.0
*
* compile:
* gcc -fPIC -shared -o libpreload.so wrap.c -ldl
* run:
* LD_PRELOAD=./libpreload.so /usr/lib/jvm/java-8-openjdk-amd64/bin/java -cp ...
* Based On:
* http://samanbarghi.com/blog/2014/09/05/how-to-wrap-a-system-call-libc-function-in-linux/
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
/* Function pointers to hold the value of the glibc functions */
static int (*real_rename)(const char* from, const char* to) = NULL;
int rename(const char* from, const char* to)
{
printf("wrapped rename(%s, %s)\n", from, to);
if (!real_rename)
real_rename = dlsym(RTLD_NEXT, "rename");
return real_rename(from, to);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment