Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warns when running TGS as root #1882

Merged
merged 20 commits into from
Aug 21, 2024
Merged
Changes from 7 commits
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
33 changes: 32 additions & 1 deletion src/Tgstation.Server.Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using Tgstation.Server.Host.Properties;
using Tgstation.Server.Host.System;

using InteropServices = System.Runtime.InteropServices;
using Process = System.Diagnostics.Process;
ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved

ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved
namespace Tgstation.Server.Host
{
/// <summary>
Expand Down Expand Up @@ -40,7 +43,7 @@ public Program()
/// Entrypoint for the <see cref="Program"/>.
/// </summary>
/// <param name="args">The command line arguments.</param>
/// <returns>A <see cref="Task"/> resulting in the <see cref="global::System.Diagnostics.Process.ExitCode"/>.</returns>
/// <returns>A <see cref="Task"/> resulting in the <see cref="Process.ExitCode"/>.</returns>
ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved
public static async Task<int> Main(string[] args)
{
// first arg is 100% always the update path, starting it otherwise is solely for debugging purposes
Expand All @@ -67,6 +70,34 @@ public static async Task<int> Main(string[] args)
args = listArgs.ToArray();
}

if (InteropServices.RuntimeInformation.IsOSPlatform(InteropServices.OSPlatform.Linux))
ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved
{
using var proc = new Process
ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved
{
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!");
ZephyrTFA marked this conversation as resolved.
Show resolved Hide resolved
}
}

var program = new Program();
return (int)await program.Main(args, updatePath);
}
Expand Down
Loading