From a48a7cef962c4f539d87267e139f68e0c3073062 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 26 Feb 2026 11:28:13 -0800 Subject: [PATCH] libct/specconv: fix panic in initSystemdProps There is a chance of panic here -- eliminate it. Add a test case (which panics before the fix). Reported-by: Luke Hinds Signed-off-by: Kir Kolyshkin --- libcontainer/specconv/spec_linux.go | 2 +- libcontainer/specconv/spec_linux_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 162195d8e..5713460e4 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -758,7 +758,7 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) { return nil, fmt.Errorf("annotation %s=%s value parse error: %w", k, v, err) } // Check for Sec suffix. - if trimName := strings.TrimSuffix(name, "Sec"); len(trimName) < len(name) { + if trimName, ok := strings.CutSuffix(name, "Sec"); ok && len(trimName) > 0 { // Check for a lowercase ascii a-z just before Sec. if ch := trimName[len(trimName)-1]; ch >= 'a' && ch <= 'z' { // Convert from Sec to USec. diff --git a/libcontainer/specconv/spec_linux_test.go b/libcontainer/specconv/spec_linux_test.go index e1a55319f..3e532d490 100644 --- a/libcontainer/specconv/spec_linux_test.go +++ b/libcontainer/specconv/spec_linux_test.go @@ -750,6 +750,11 @@ func TestInitSystemdProps(t *testing.T) { in: inT{"org.systemd.property.FOOSec", "123"}, exp: expT{false, "FOOSec", 123}, }, + { + desc: "convert USec to Sec (short name)", + in: inT{"org.systemd.property.Sec", "123"}, + exp: expT{false, "Sec", 123}, + }, { desc: "CollectMode", in: inT{"org.systemd.property.CollectMode", "'inactive-or-failed'"},