fix(yengine): always release snapshot suspension and atomically validate OAR/script-state imports

This commit is contained in:
OpenGenesis (MPL 2.0) committed 2026-11-07 20:52:13 +01:00
1 parent 6594901761
commit 3eed7ca5d6
5 files changed
+87 -20

No files matched your search

@@ -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 <ScriptState ...>...</ScriptState> 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)]
@@ -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
@@ -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"));