incusd/instance/qemu: Use os.Root for template output

Matches the LXC driver, the existing checks already prevent escaping.

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
Stéphane Graber 2026-07-28 12:31:15 -04:00
parent 8e2eab60d8
commit 154089f398
No known key found for this signature in database
GPG Key ID: C638974D64792D67

View File

@ -3777,6 +3777,14 @@ func (d *qemu) templateApplyNow(trigger instance.TemplateTrigger, path string) e
instanceMeta["ephemeral"] = "false"
}
// Open the output directory as an os.Root so all template writes stay confined to it.
outputRoot, err := os.OpenRoot(path)
if err != nil {
return fmt.Errorf("Failed to open template output path: %w", err)
}
defer logger.WarnOnError(outputRoot.Close, "Failed to close template output path")
// Go through the templates.
for tplPath, tpl := range metadata.Templates {
err = func(tplPath string, tpl *api.ImageMetadataTemplate) error {
@ -3813,7 +3821,7 @@ func (d *qemu) templateApplyNow(trigger instance.TemplateTrigger, path string) e
}
// Create the file itself.
w, err = os.Create(filepath.Join(path, fmt.Sprintf("%s.out", tpl.Template)))
w, err = outputRoot.Create(fmt.Sprintf("%s.out", tpl.Template))
if err != nil {
return err
}