Skip to content

Commit

Permalink
Probe for SELinux execheap permission during bootup
Browse files Browse the repository at this point in the history
  • Loading branch information
Popax21 committed Dec 14, 2023
1 parent 1772c18 commit 0cb187f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Celeste.Mod.mm/Mod/Everest/BOOT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -48,6 +49,28 @@ private static void Main(string[] args) {
} catch {
}

// SELinux can cause weird game corruption-like symptoms when we lack the execheap permission
// So probe for it before continuing to boot on Linux
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
[DllImport("libc", SetLastError = true)]
static extern int mprotect(IntPtr ptr, nuint len, int prot);
const int PROT_READ = 1, PROT_WRITE = 2, PROT_EXEC = 4;

//Allocate a bit of memory on the heap
IntPtr heapAlloc = Marshal.AllocHGlobal(123);
IntPtr heapPage = heapAlloc & ~0xfff;

//Try to make it executable
if (mprotect(heapPage, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
throw new Win32Exception(Marshal.GetLastPInvokeError(), "SELinux execheap probe failed! Please ensure Everest has this permission, then try again");

//Cleanup
if (mprotect(heapPage, 0x1000, PROT_READ | PROT_WRITE) < 0)
throw new Win32Exception(Marshal.GetLastPInvokeError(), "Failed to revert memory permissions after SELinux execheap probe");

Marshal.FreeHGlobal(heapAlloc);
}

// Load the compatibility mode setting
Everest.CompatibilityMode = Everest.CompatMode.None;
bool useExclusiveFullscreen = false;
Expand Down

0 comments on commit 0cb187f

Please sign in to comment.