Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only run id command if needed
  • Loading branch information
NoahvdAa authored and me4502 committed Oct 9, 2021
commit be1d76c276287ead5b5b40cb09fc013d1b45c49d
19 changes: 12 additions & 7 deletions patches/server/0814-Add-root-admin-user-detection.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Co-authored-by: Noah van der Aa <[email protected]>

diff --git a/src/main/java/io/papermc/paper/util/ServerEnvironment.java b/src/main/java/io/papermc/paper/util/ServerEnvironment.java
new file mode 100644
index 0000000000000000000000000000000000000000..afec67a3c69dcc0cbdc4fb14d9444b4434cb82e9
index 0000000000000000000000000000000000000000..9bc2cbf015998b98b6681a427596b151ed1806a0
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/ServerEnvironment.java
@@ -0,0 +1,31 @@
@@ -0,0 +1,36 @@
+package io.papermc.paper.util;
+
+import com.sun.security.auth.module.NTSystem;
+import com.sun.security.auth.module.UnixSystem;
+import org.apache.commons.lang.SystemUtils;
+
+import java.io.IOException;
Expand All @@ -33,11 +34,15 @@ index 0000000000000000000000000000000000000000..afec67a3c69dcc0cbdc4fb14d9444b44
+ if (SystemUtils.IS_OS_WINDOWS) {
+ RUNNING_AS_ROOT_OR_ADMIN = Set.of(new NTSystem().getGroupIDs()).contains(WINDOWS_HIGH_INTEGRITY_LEVEL);
+ } else {
+ boolean isRunningAsRoot;
+ try (final InputStream inputStream = Runtime.getRuntime().exec("id -u").getInputStream()) {
+ isRunningAsRoot = new String(inputStream.readAllBytes()).trim().equals("0");
+ } catch (IOException ignored) {
+ isRunningAsRoot = false;
+ boolean isRunningAsRoot = false;
+ if (new UnixSystem().getUid() == 0) {
+ // Due to an OpenJDK bug, UnixSystem#getUid incorrectly returns 0 when the user doesn't have a username.
+ // Because of this, we'll have to double-check if the user ID is actually 0 by running the id command.
+ try (final InputStream inputStream = Runtime.getRuntime().exec("id -u").getInputStream()) {
+ isRunningAsRoot = new String(inputStream.readAllBytes()).trim().equals("0");
+ } catch (IOException ignored) {
+ isRunningAsRoot = false;
+ }
+ }
+ RUNNING_AS_ROOT_OR_ADMIN = isRunningAsRoot;
+ }
Expand Down