raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

J9 Allow attachment as root #1631

Closed FelixMarxIBM closed 5 months ago

FelixMarxIBM commented 5 months ago

This PR aims to solve #1628

The J9 attachment allows root to start the attachment and be excluded from some of the checks. By checking all folders as root and also doing a chown on the replyFile, we are able to attach also to user owned JVMs as root.

This is also true upstream in openj9 while reading the attachinfo and writing the replyfile

References: https://github.com/eclipse-openj9/openj9/blob/master/jcl/src/java.base/share/classes/openj9/internal/tools/attach/target/Reply.java#L88-L90

if ((ROOT_UID == myUid) && (ROOT_UID != targetUid)) {
  IPC.chownFileToTargetUid(replyFileAbsolutePath, targetUid);
}

https://github.com/eclipse-openj9/openj9/blob/master/jcl/src/java.base/share/classes/openj9/internal/tools/attach/target/CommonDirectory.java#L418-L426

/**
* Check if the file is owned by the UID.  Note that UID 0 "owns" all files.
* @param f File or directory
* @param myUid user UID. 
* @return true if the uid owns the file or uid == 0.
*/
public static boolean isFileOwnedByUid(File f, long myUid) {
  return (0 == myUid) || (myUid == CommonDirectory.getFileOwner(f.getAbsolutePath()));
}