From 8da2424909a34701b55cfde3e5d4faee37b543e2 Mon Sep 17 00:00:00 2001 From: ZephyrTFA Date: Sun, 18 Aug 2024 13:26:09 -0400 Subject: [PATCH] move IsRoot to posix identity move checking root to CheckSystemCompatibility use Syscall internal getuid --- .../Components/InstanceManager.cs | 5 ++++ src/Tgstation.Server.Host/Program.cs | 29 ------------------- .../Security/PosixSystemIdentity.cs | 28 ++++++++++++++++++ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/InstanceManager.cs b/src/Tgstation.Server.Host/Components/InstanceManager.cs index c86f5c8ede..b56f9c0dd3 100644 --- a/src/Tgstation.Server.Host/Components/InstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/InstanceManager.cs @@ -675,6 +675,11 @@ namespace Tgstation.Server.Host.Components { if (!systemIdentity.CanCreateSymlinks) throw new InvalidOperationException($"The user running {Constants.CanonicalPackageName} cannot create symlinks! Please try running as an administrative user!"); + + if (systemIdentity is PosixSystemIdentity posixIdentity && posixIdentity.IsRoot()) + { + logger.LogWarning("TGS is being run as the root account. This is not recommended and may prevent launch in a future version."); + } } // This runs before the real socket is opened, ensures we don't perform reattaches unless we're fairly certain the bind won't fail diff --git a/src/Tgstation.Server.Host/Program.cs b/src/Tgstation.Server.Host/Program.cs index c9d99aa1bc..47988e4d1c 100644 --- a/src/Tgstation.Server.Host/Program.cs +++ b/src/Tgstation.Server.Host/Program.cs @@ -11,7 +11,6 @@ using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Properties; using Tgstation.Server.Host.System; -using InteropServices = System.Runtime.InteropServices; using Process = System.Diagnostics.Process; namespace Tgstation.Server.Host @@ -70,34 +69,6 @@ namespace Tgstation.Server.Host args = listArgs.ToArray(); } - if (InteropServices.RuntimeInformation.IsOSPlatform(InteropServices.OSPlatform.Linux)) - { - using var proc = new Process - { - StartInfo = new ProcessStartInfo - { - FileName = "id", - Arguments = "-u", - UseShellExecute = false, - RedirectStandardOutput = true, - CreateNoWindow = true, - }, - }; - - proc.Start(); - await proc.WaitForExitAsync(); - if (proc.ExitCode is not 0 || !int.TryParse(await proc.StandardOutput.ReadToEndAsync(), out var uid)) - { - Console.Error.WriteLine("Failed to obtain user id."); - return 1; - } - - if (uid is 0) - { - Console.Error.WriteLine("TGS is being run as root. This is not recommended and will prevent launching in a future version!"); - } - } - var program = new Program(); return (int)await program.Main(args, updatePath); } diff --git a/src/Tgstation.Server.Host/Security/PosixSystemIdentity.cs b/src/Tgstation.Server.Host/Security/PosixSystemIdentity.cs index 67f0e3ac83..73a0d36678 100644 --- a/src/Tgstation.Server.Host/Security/PosixSystemIdentity.cs +++ b/src/Tgstation.Server.Host/Security/PosixSystemIdentity.cs @@ -2,6 +2,8 @@ using System.Threading; using System.Threading.Tasks; +using Mono.Unix.Native; + namespace Tgstation.Server.Host.Security { /// @@ -9,6 +11,32 @@ namespace Tgstation.Server.Host.Security /// sealed class PosixSystemIdentity : ISystemIdentity { + /// + /// True if TGS is running under root. + /// + bool isRoot = false; + + /// + /// True if is populated. + /// + bool isRootChecked = false; + + /// + /// Checks whether TGS is running under the root user. + /// + /// True if running under root. False otherwise. + public bool IsRoot() + { + if (isRootChecked) + { + return isRoot; + } + + isRoot = Syscall.getuid() == 0; + isRootChecked = true; + return isRoot; + } + /// public string Uid => throw new NotImplementedException();