Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

ComfyUI seeds: no use-after-free when a node has two seed inputs (found by ASan)

islandmededceda069a61
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57e6bfbc..aac9a96b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,10 @@ All notable changes to SculptForge are listed here. The format follows
adjacency has no limit). Unit tests divide fan cylinders of 3 to 300 sides and UV spheres up to 200 segments,
through OBJ and through the Blender Bridge, and compare every level's adjacency with a plain rebuild; a UI test
receives the issue's meshes from the bridge and divides them in the app.
+- Generating with a ComfyUI workflow and a seed read freed memory, which could crash at random or set the wrong
+ seed: while setting a node's seeds, the node was replaced after its first seed but its inputs were still read
+ for the second (a sampler with both `seed` and `noise_seed`). Found with AddressSanitizer; the keys are now
+ chosen before any is set. The unit test now has a node with both seeds.
### Added
- A log file, `sculptforge.log` in the user data folder (`%APPDATA%\SculptForge\SculptForge` on Windows,
diff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.md
index 11ad9dc1..216b09ac 100644
--- a/docs/guide/whats-new.md
+++ b/docs/guide/whats-new.md
@@ -11,6 +11,7 @@ chapter opens by itself the first time you start a new version; you can always f
- Divide (<kbd>Ctrl</kbd>+<kbd>D</kbd>) no longer crashes on models with a point where many polygons meet, such
as a Blender cylinder with Triangle Fan caps of 22 or more sides or a UV sphere's poles.
+- Generating with a [ComfyUI](generate-3d.md) workflow and a fixed seed could crash at random; fixed.
- SculptForge now keeps a log file, `sculptforge.log`, in its settings folder (**Help > Open log folder**). After
a crash it says what crashed and what you did last; attach it when you report a problem. See
[The log file](troubleshooting.md#the-log-file).
diff --git a/src/ai/ComfyUI.cpp b/src/ai/ComfyUI.cpp
index b68aa5d0..58de05bb 100644
--- a/src/ai/ComfyUI.cpp
+++ b/src/ai/ComfyUI.cpp
@@ -393,21 +393,22 @@ bool applyContract(Json& workflow, const ContractInputs& in, std::string& error,
if (in.seed) {
const double seed = double(*in.seed);
for (const auto& id : ids) {
- const Json& inputs = workflow[id]["inputs"];
- bool set = false;
- for (const char* key : {"seed", "noise_seed"})
- if (inputs[key].isNumber()) {
- setInput(workflow, id, key, seed);
- set = true;
- }
- if (!set && titleOf(workflow[id]) == "sf_seed")
- for (const char* key : {"value", "int", "number"})
- if (workflow[id]["inputs"][key].isNumber()) {
- setInput(workflow, id, key, seed);
- set = true;
- break;
- }
- if (set) rep.seedNodes.push_back(id);
+ // The keys are chosen first and set after: setInput replaces the node, so a reference into its
+ // inputs doesn't survive it.
+ std::vector<const char*> keys;
+ {
+ const Json& inputs = workflow[id]["inputs"];
+ for (const char* key : {"seed", "noise_seed"})
+ if (inputs[key].isNumber()) keys.push_back(key);
+ if (keys.empty() && titleOf(workflow[id]) == "sf_seed")
+ for (const char* key : {"value", "int", "number"})
+ if (inputs[key].isNumber()) {
+ keys.push_back(key);
+ break;
+ }
+ }
+ for (const char* key : keys) setInput(workflow, id, key, seed);
+ if (!keys.empty()) rep.seedNodes.push_back(id);
}
}
return true;
diff --git a/tests/test_comfyui.cpp b/tests/test_comfyui.cpp
index f306e0c9..67ef5149 100644
--- a/tests/test_comfyui.cpp
+++ b/tests/test_comfyui.cpp
@@ -121,15 +121,20 @@ TEST(comfy_seed_contract) {
"2": {"class_type": "SamplerCustom", "inputs": {"noise_seed": 5}},
"3": {"class_type": "PrimitiveInt", "inputs": {"value": 5}, "_meta": {"title": "sf_seed"}},
"4": {"class_type": "KSampler", "inputs": {"seed": ["3", 0]}},
- "5": {"class_type": "PrimitiveInt", "inputs": {"value": 5}}
+ "5": {"class_type": "PrimitiveInt", "inputs": {"value": 5}},
+ "6": {"class_type": "SamplerBoth", "inputs": {"seed": 5, "noise_seed": 6, "cfg": 7}}
})j");
comfy::ContractInputs in;
in.seed = 123456789012345ull; // above 1e15 would print as 1.2e+15 and fail ComfyUI's INT check
comfy::ContractReport rep;
std::string err;
CHECK(comfy::applyContract(wf, in, err, &rep));
- CHECK(rep.seedNodes.size() == 3);
+ CHECK(rep.seedNodes.size() == 4);
CHECK(wf["1"]["inputs"]["seed"].asNumber() == 123456789012345.0);
+ // Both seeds of one node (setting the first replaced the node the second was read from: a use-after-free).
+ CHECK(wf["6"]["inputs"]["seed"].asNumber() == 123456789012345.0);
+ CHECK(wf["6"]["inputs"]["noise_seed"].asNumber() == 123456789012345.0);
+ CHECK(wf["6"]["inputs"]["cfg"].asNumber() == 7.0);
CHECK(wf["2"]["inputs"]["noise_seed"].asNumber() == 123456789012345.0);
CHECK(wf["3"]["inputs"]["value"].asNumber() == 123456789012345.0);
CHECK(wf["4"]["inputs"]["seed"].isArray()); // linked seed stays linked