diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs index 0edd54a2..b5567b08 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs @@ -1164,13 +1164,16 @@ namespace OpenSim.Region.ScriptEngine.Yengine // the script is instantiated. public bool SetXMLState(UUID itemID, string xml) { - XmlDocument doc = new(); - + XmlDocument doc; try { - doc.LoadXml(xml); + // OARs, inventory restores and region crossings may contain + // foreign state; apply the same bounded DTD-free XML parser + // used for regular .state file loading. + doc = XMRInstance.ParsePersistedStateXml(xml); } - catch + catch (Exception error) when (error is XmlException or InvalidDataException + or ArgumentException or System.IO.IOException) { return false; } @@ -1205,19 +1208,48 @@ namespace OpenSim.Region.ScriptEngine.Yengine return false; } - XmlAttribute assetA = doc.CreateAttribute("", "Asset", ""); - assetA.Value = stateN.GetAttribute("Asset"); - scriptStateN.Attributes.Append(assetA); + string assetId = stateN.GetAttribute("Asset"); + if (string.IsNullOrEmpty(assetId)) + return false; - // Write out the .state file with the ... XML text - string statePath = XMRInstance.GetStateFileName(m_ScriptBasePath, itemID); - using (FileStream ss = File.Create(statePath)) + if (!isX) { - using (StreamWriter sw = new(ss)) - sw.Write(scriptStateN.OuterXml); + // Do not replace a valid existing script state with malformed + // base64 from a foreign region/OAR. XEngine uses Variables + // instead of YEngine Snapshot and remains supported. + XmlElement snapshotN = (XmlElement)scriptStateN.SelectSingleNode("Snapshot"); + if (snapshotN is null || snapshotN.InnerText.Length == 0 || + snapshotN.InnerText.Length > XMRInstance.MaxPersistedStateXmlBytes) + return false; + try + { + Convert.FromBase64String(snapshotN.InnerText); + } + catch (FormatException) + { + return false; + } } - return true; + XmlAttribute assetA = doc.CreateAttribute("", "Asset", ""); + assetA.Value = assetId; + scriptStateN.Attributes.Append(assetA); + + // Never File.Create() over the last known-good state, including + // during imports/crossings. Validate and replace atomically. + string statePath = XMRInstance.GetStateFileName(m_ScriptBasePath, itemID); + try + { + XMRInstance.WriteStateFileAtomically(statePath, scriptStateN.OuterXml); + return true; + } + catch (Exception error) when (error is InvalidDataException or ArgumentException + or System.IO.IOException or UnauthorizedAccessException) + { + m_log.WarnFormat("[YEngine]: Refusing invalid/failed state import for {0}: {1}", + itemID, error.Message); + return false; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMRInstCapture.cs b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCapture.cs index 613ac107..b464b3a5 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/XMRInstCapture.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCapture.cs @@ -82,8 +82,10 @@ namespace OpenSim.Region.ScriptEngine.Yengine // stable. Setting suspendOnCheckRun tells CheckRun() to suspend // and return out so RunOne() will release the lock asap. suspendOnCheckRunHold = true; - lock(m_RunLock) + try { + lock(m_RunLock) + { //m_RunOnePhase = "GetExecutionState enter"; //CheckRunLockInvariants(true); @@ -184,10 +186,14 @@ namespace OpenSim.Region.ScriptEngine.Yengine scriptStateN.AppendChild(plugins); //m_RunOnePhase = "GetExecutionState H"; - // Let script run again. - suspendOnCheckRunHold = false; - //m_RunOnePhase = "GetExecutionState leave"; + } + } + finally + { + // Unsafe/unsupported legacy state can throw during MigrateOut. + // Never leave a running script frozen after a failed snapshot. + suspendOnCheckRunHold = false; } // scriptStateN represents the contents of the .state file so diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs index 1363d8f1..4cfd75d6 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs @@ -503,13 +503,14 @@ namespace OpenSim.Region.ScriptEngine.Yengine XmlElement doGblInitN = (XmlElement)scriptStateN.SelectSingleNode("DoGblInit"); doGblInit = bool.Parse(doGblInitN.InnerText); + // A corrupt/legacy migration snapshot must fail before any timer + // is reprogrammed on the partially constructed script instance. + double? restoredScriptTime = null; if (m_XMRLSLApi != null) { XmlElement scpttimeN = (XmlElement)scriptStateN.SelectSingleNode("scrpTime"); if (scpttimeN != null && Double.TryParse(scpttimeN.InnerText, out double t)) - { - m_XMRLSLApi.SetLSLTimer(Util.GetTimeStampMS() - t); - } + restoredScriptTime = t; } double minEventDelay = 0.0; @@ -538,6 +539,9 @@ namespace OpenSim.Region.ScriptEngine.Yengine MigrateInEventHandler(ms); } + if (restoredScriptTime.HasValue) + m_XMRLSLApi.SetLSLTimer(Util.GetTimeStampMS() - restoredScriptTime.Value); + XmlElement permissionsN = (XmlElement)scriptStateN.SelectSingleNode("Permissions"); m_Item.PermsGranter = new UUID(permissionsN.GetAttribute("granter")); m_Item.PermsMask = Convert.ToInt32(permissionsN.GetAttribute("mask")); diff --git a/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md b/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md index 891daa52..ebbf055c 100644 --- a/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md +++ b/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md @@ -89,3 +89,20 @@ A repository-level `Directory.Build.targets` overrides stale checked-in `runprebuild.sh`. ROBUST uses a nested `OpenSim/Server/Directory.Build.targets`; that nested file now imports the root security policy. CI queries the *effective* MSBuild property of OpenSim, ROBUST and YEngine after the Prebuild step. + +## Additional safety: failed YEngine snapshots and OAR imports + +A snapshot failure while `suspendOnCheckRunHold` was set could leave +a running script stuck. `XMRInstance.GetExecutionState` now clears +this suspension flag via `finally`, including errors from unsupported +historic objects and oversized state. + +`Yengine.SetXMLState` (used for OAR/import/region-crossing state) previously +used unbounded `XmlDocument.LoadXml` and `File.Create`, which could +truncate a last known-good `.state` file before successfully writing the +replacement. It now uses the same DTD-free 8 MiB XML reader as ordinary +state restore, checks the YEngine base64 snapshot, and writes atomically. +Legacy XEngine imports retain their distinct Variables-based format. + +Script timers are not changed until the YEngine binary snapshot was +decoded successfully. No automatic historic graph execution is enabled. diff --git a/tools/ci/verify_yengine_binaryformatter_safety.py b/tools/ci/verify_yengine_binaryformatter_safety.py index 9e212adb..07dc31f6 100644 --- a/tools/ci/verify_yengine_binaryformatter_safety.py +++ b/tools/ci/verify_yengine_binaryformatter_safety.py @@ -27,6 +27,14 @@ assert "File.Delete(m_StateFileName)" not in ctor.split("private void LoadInitia "private static EventParams", 1)[0] assert "ReadPersistedStateXmlFile(m_StateFileName)" in ctor assert "WriteStateFileAtomically(m_StateFileName, scriptStateN.OuterXml)" in capture +assert "finally\n {\n // Unsafe/unsupported legacy state" in capture +assert "suspendOnCheckRunHold = false;" in capture +engine = (ROOT / "OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs").read_text() +assert "doc = XMRInstance.ParsePersistedStateXml(xml)" in engine +assert "XMRInstance.WriteStateFileAtomically(statePath, scriptStateN.OuterXml)" in engine +assert "using (FileStream ss = File.Create(statePath))" not in engine +assert "Convert.FromBase64String(snapshotN.InnerText)" in engine +assert "restoredScriptTime.HasValue" in ctor assert "DtdProcessing = DtdProcessing.Prohibit" in capture assert "File.Move(temporary, statePath, overwrite: true)" in capture generator = (ROOT / "Prebuild/src/Core/Targets/VSGenericTarget.cs").read_text()