Skip to content

Commit

Permalink
move IsRoot to posix identity
Browse files Browse the repository at this point in the history
move checking root to CheckSystemCompatibility
use Syscall internal getuid
  • Loading branch information
ZephyrTFA committed Aug 18, 2024
1 parent 3bcda56 commit 8da2424
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/Tgstation.Server.Host/Components/InstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ void CheckSystemCompatibility()
{
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
Expand Down
29 changes: 0 additions & 29 deletions src/Tgstation.Server.Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Tgstation.Server.Host.Properties;
using Tgstation.Server.Host.System;

using InteropServices = System.Runtime.InteropServices;
using Process = System.Diagnostics.Process;

namespace Tgstation.Server.Host
Expand Down Expand Up @@ -70,34 +69,6 @@ public static async Task<int> Main(string[] args)
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);
}
Expand Down
28 changes: 28 additions & 0 deletions src/Tgstation.Server.Host/Security/PosixSystemIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@
using System.Threading;
using System.Threading.Tasks;

using Mono.Unix.Native;

namespace Tgstation.Server.Host.Security
{
/// <summary>
/// <see cref="ISystemIdentity"/> for POSIX systems.
/// </summary>
sealed class PosixSystemIdentity : ISystemIdentity
{
/// <summary>
/// True if TGS is running under root.
/// </summary>
bool isRoot = false;

/// <summary>
/// True if <see cref="isRoot" /> is populated.
/// </summary>
bool isRootChecked = false;

/// <summary>
/// Checks whether TGS is running under the root user.
/// </summary>
/// <returns>True if running under root. False otherwise.</returns>
public bool IsRoot()
{
if (isRootChecked)
{
return isRoot;
}

isRoot = Syscall.getuid() == 0;
isRootChecked = true;
return isRoot;
}

/// <inheritdoc />
public string Uid => throw new NotImplementedException();

Expand Down

0 comments on commit 8da2424

Please sign in to comment.