From 40295112fe490c36572512a54aea4d32ab1769e3 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 17 Feb 2023 16:08:25 +0100 Subject: [PATCH] Cleanup state stack during sandboxing process --- Capy64/Runtime/Sandbox.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Capy64/Runtime/Sandbox.cs b/Capy64/Runtime/Sandbox.cs index d540a12..87f2db1 100644 --- a/Capy64/Runtime/Sandbox.cs +++ b/Capy64/Runtime/Sandbox.cs @@ -37,6 +37,8 @@ internal class Sandbox L.OpenOS(); L.OpenPackage(); + + L.SetTop(0); } internal static void Patch(Lua L) { @@ -70,10 +72,11 @@ internal class Sandbox L.PushString("config"); L.PushString(packageConfig); L.SetTable(-4); + L.Pop(1); // delete 3 and 4 searchers L.PushString("searchers"); - L.GetTable(-3); + L.GetTable(-2); L.PushNil(); L.SetInteger(-2, 3); @@ -84,7 +87,7 @@ internal class Sandbox L.PushCFunction(L_Searcher); L.SetInteger(-2, 2); - L.Pop(L.GetTop()); + L.Pop(2); // Replace loadfile with sandboxed one L.PushCFunction(L_Loadfile); @@ -94,8 +97,6 @@ internal class Sandbox L.PushCFunction(L_Dofile); L.SetGlobal("dofile"); - L.Pop(L.GetTop()); - // yeet dangerous os functions L.GetGlobal("os"); @@ -118,6 +119,8 @@ internal class Sandbox L.PushString("getenv"); L.PushNil(); L.SetTable(-3); + + L.Pop(1); } internal static int L_Searcher(IntPtr state)