Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@
path = Dependencies/Steamworks.NET
url = https://github.com/marchc1/Steamworks.NET
branch = 1-64
[submodule "Dependencies/CEF"]
path = Dependencies/CEF
url = https://github.com/SourceDotNet/CEF-SDK.git
17 changes: 2 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build Source.Launcher",
"program": "${workspaceFolder}/Build/Debug/Source.Launcher.exe",
"linux": { "program": "${workspaceFolder}/Build/Debug/Source.Launcher" },
"program": "${workspaceFolder}/Build/Debug/Source.Launcher.dll",
"cwd": "${workspaceFolder}/Build/Debug",
// "args": ["-noworkshop", "-noaddons"],
"stopAtEntry": false
Expand All @@ -17,23 +16,11 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build Source.Launcher",
"program": "${workspaceFolder}/Build/Debug/Source.Launcher.exe",
"linux": { "program": "${workspaceFolder}/Build/Debug/Source.Launcher" },
"program": "${workspaceFolder}/Build/Debug/Source.Launcher.dll",
"cwd": "${workspaceFolder}/Build/Debug",
"stopAtEntry": false,
"console": "externalTerminal"
},
{
"name": "Source.LauncherMain (no sandbox)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build Source.Launcher",
"program": "${workspaceFolder}/Build/Debug/Source.LauncherMain.dll",
"linux": { "program": "${workspaceFolder}/Build/Debug/Source.Launcher.dll" },
"cwd": "${workspaceFolder}/Build/Debug",
"args": ["-nochromiumsandbox"],
"stopAtEntry": false
},
{
"name": "Source.Dedicated",
"type": "coreclr",
Expand Down
1 change: 0 additions & 1 deletion Dependencies/CEF
Submodule CEF deleted from c11947
16 changes: 16 additions & 0 deletions HTML.CEF.Process/HTML.CEF.Process.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;Debug (Dedicated);Release (Dedicated)</Configurations>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CefGlue.Next.Core" Version="154.8037.98" />
</ItemGroup>

</Project>
176 changes: 176 additions & 0 deletions HTML.CEF.Process/ProcessLockdown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace HTML.CEF.Process;

[SupportedOSPlatform("windows")]
static unsafe partial class ProcessLockdown
{
const string LowIntegritySid = "S-1-16-4096";

const uint TokenQuery = 0x0008;
const uint TokenAdjustPrivileges = 0x0020;
const uint TokenAdjustDefault = 0x0080;
const int TokenIntegrityLevel = 25;
const uint SeGroupIntegrity = 0x00000020;

const int JobObjectBasicUIRestrictions = 4;
const int JobObjectExtendedLimitInformation = 9;
const uint JobObjectLimitActiveProcess = 0x00000008;
const uint JobObjectUILimitAll = 0x000000FF;

const int ProcessStrictHandleCheckPolicy = 3;
const int ProcessExtensionPointDisablePolicy = 6;
const int ProcessFontDisablePolicy = 9;
const int ProcessImageLoadPolicy = 10;

const uint StrictHandleCheckRaiseAndPermanent = 0x3;
const uint ExtensionPointsDisabled = 0x1;
const uint NonSystemFontsDisabled = 0x1;
const uint NoRemoteAndLowLabelImages = 0x3;

[StructLayout(LayoutKind.Sequential)]
struct JobObjectBasicLimitInformation
{
public long PerProcessUserTimeLimit;
public long PerJobUserTimeLimit;
public uint LimitFlags;
public nuint MinimumWorkingSetSize;
public nuint MaximumWorkingSetSize;
public uint ActiveProcessLimit;
public nuint Affinity;
public uint PriorityClass;
public uint SchedulingClass;
}

[StructLayout(LayoutKind.Sequential)]
struct IoCounters
{
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
}

[StructLayout(LayoutKind.Sequential)]
struct JobObjectExtendedLimitInformationData
{
public JobObjectBasicLimitInformation BasicLimitInformation;
public IoCounters IoInfo;
public nuint ProcessMemoryLimit;
public nuint JobMemoryLimit;
public nuint PeakProcessMemoryUsed;
public nuint PeakJobMemoryUsed;
}

[StructLayout(LayoutKind.Sequential)]
struct TokenMandatoryLabel
{
public nint Sid;
public uint Attributes;
}

[LibraryImport("kernel32.dll")]
private static partial nint GetCurrentProcess();

[LibraryImport("kernel32.dll")]
private static partial int CloseHandle(nint handle);

[LibraryImport("kernel32.dll")]
private static partial nint LocalFree(nint mem);

[LibraryImport("kernel32.dll", EntryPoint = "CreateJobObjectW")]
private static partial nint CreateJobObject(nint jobAttributes, nint name);

[LibraryImport("kernel32.dll")]
private static partial int SetInformationJobObject(nint job, int infoClass, void* info, uint infoLength);

[LibraryImport("kernel32.dll")]
private static partial int AssignProcessToJobObject(nint job, nint process);

[LibraryImport("kernel32.dll")]
private static partial int SetProcessMitigationPolicy(int policy, void* buffer, nuint length);

[LibraryImport("advapi32.dll")]
private static partial int OpenProcessToken(nint process, uint desiredAccess, out nint token);

[LibraryImport("advapi32.dll")]
private static partial int AdjustTokenPrivileges(nint token, int disableAllPrivileges, nint newState, uint bufferLength, nint previousState, nint returnLength);

[LibraryImport("advapi32.dll", EntryPoint = "ConvertStringSidToSidW", StringMarshalling = StringMarshalling.Utf16)]
private static partial int ConvertStringSidToSid(string stringSid, out nint sid);

[LibraryImport("advapi32.dll")]
private static partial uint GetLengthSid(nint sid);

[LibraryImport("advapi32.dll")]
private static partial int SetTokenInformation(nint token, int infoClass, void* info, uint infoLength);

public static bool Apply(ReadOnlySpan<char> processType) {
bool isRenderer = processType.SequenceEqual("renderer");

if (!SetMitigation(ProcessExtensionPointDisablePolicy, ExtensionPointsDisabled))
return false;
if (!SetMitigation(ProcessImageLoadPolicy, NoRemoteAndLowLabelImages))
return false;
if (!RestrictJob(isRenderer))
return false;

if (isRenderer) {
if (!SetMitigation(ProcessStrictHandleCheckPolicy, StrictHandleCheckRaiseAndPermanent))
return false;
if (!SetMitigation(ProcessFontDisablePolicy, NonSystemFontsDisabled))
return false;
}

return LowerToken();
}

static bool SetMitigation(int policy, uint flags) => SetProcessMitigationPolicy(policy, &flags, sizeof(uint)) != 0;

static bool RestrictJob(bool restrictUI) {
nint job = CreateJobObject(0, 0);
if (job == 0)
return false;

JobObjectExtendedLimitInformationData limits = default;
limits.BasicLimitInformation.LimitFlags = JobObjectLimitActiveProcess;
limits.BasicLimitInformation.ActiveProcessLimit = 1;
if (SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, (uint)sizeof(JobObjectExtendedLimitInformationData)) == 0)
return false;

if (restrictUI) {
uint uiRestrictions = JobObjectUILimitAll;
if (SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions, sizeof(uint)) == 0)
return false;
}

return AssignProcessToJobObject(job, GetCurrentProcess()) != 0;
}

static bool LowerToken() {
if (OpenProcessToken(GetCurrentProcess(), TokenQuery | TokenAdjustPrivileges | TokenAdjustDefault, out nint token) == 0)
return false;

try {
if (AdjustTokenPrivileges(token, 1, 0, 0, 0, 0) == 0)
return false;

if (ConvertStringSidToSid(LowIntegritySid, out nint sid) == 0)
return false;

try {
TokenMandatoryLabel label = new() { Sid = sid, Attributes = SeGroupIntegrity };
return SetTokenInformation(token, TokenIntegrityLevel, &label, (uint)sizeof(TokenMandatoryLabel) + GetLengthSid(sid)) != 0;
}
finally {
LocalFree(sid);
}
}
finally {
CloseHandle(token);
}
}
}
51 changes: 51 additions & 0 deletions HTML.CEF.Process/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Runtime.InteropServices;

using Xilium.CefGlue;

namespace HTML.CEF.Process;

static partial class Program
{
const int MainThreadStackSize = 8 * 1024 * 1024;
const int PrSetPdeathsig = 1;
const int SigKill = 9;
const string ProcessTypeSwitch = "--type=";

[LibraryImport("libc", EntryPoint = "prctl")]
private static partial int Prctl(int option, nuint arg2);

static ReadOnlySpan<char> GetProcessType(string[] args) {
foreach (string arg in args) {
if (arg.StartsWith(ProcessTypeSwitch, StringComparison.Ordinal))
return arg.AsSpan(ProcessTypeSwitch.Length);
}
return [];
}

static int Main() {
if (OperatingSystem.IsLinux())
Prctl(PrSetPdeathsig, SigKill);

if (OperatingSystem.IsWindows())
CefRuntime.Load(AppContext.BaseDirectory);
else
CefRuntime.Load();

string[] args = Environment.GetCommandLineArgs();

if (OperatingSystem.IsWindows() && !ProcessLockdown.Apply(GetProcessType(args)))
return 1;

CefMainArgs mainArgs = new(args);
SubprocessApp app = new();

if (!OperatingSystem.IsWindows())
return CefRuntime.ExecuteProcess(mainArgs, app, 0);

int exitCode = 0;
Thread thread = new(() => exitCode = CefRuntime.ExecuteProcess(mainArgs, app, 0), MainThreadStackSize);
thread.Start();
thread.Join();
return exitCode;
}
}
Loading
Loading