core: shell-quote command strings passed to sh -c

Two call sites build a shell command by interpolating values that were
never shell-quoted, so a value holding a metacharacter is executed
rather than passed along: RadosNamespaceHasObjects joins the finalized
rados argument vector with spaces, and CopyLocalFileToContainer
interpolates the destination path into a redirect.

Neither has misbehaved so far because rook composes both values itself,
not because the inputs are constrained to shell-safe characters.
CopyLocalFileToContainer receives a temp-file path. The rados probe
receives a "<metadataPoolName>:<zone><suffix>" string assembled by
adjustZoneDefaultPools, whose pool half comes from the default pool
placement's metadataPoolName or from spec.sharedPools.metadataPoolName,
neither of which carries pattern validation, or from the zone JSON of a
zone rook adopted rather than created.

Quote both with FormatCommand.

Signed-off-by: Joshua Hoblitt <josh@hoblitt.com>
This commit is contained in:
Joshua Hoblitt committed 2026-08-05 10:50:51 -07:00
1 parent 239fb71e6a
commit 55a4543bc4
3 files changed
+40 -3

No files matched your search

+1 -2
View File
@@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"math"
"strings"
"time"
"github.com/pkg/errors"
@@ -183,7 +182,7 @@ func RadosNamespaceHasObjects(context *clusterd.Context, clusterInfo *ClusterInf
}
command, args := FinalizeCephCommandArgs(RadosTool, clusterInfo, radosArgs, context.ConfigDir)
// Pipe through "head -c 1" so only 1 byte is buffered regardless of object count.
shellCmd := command + " " + strings.Join(args, " ") + " 2>/dev/null | head -c 1"
shellCmd := exec.FormatCommand(command, args...) + " 2>/dev/null | head -c 1"
output, err := context.Executor.ExecuteCommandWithTimeout(exec.CephCommandsTimeout, "sh", "-c", shellCmd)
if err != nil {
return false, errors.Wrapf(err, "failed to check for objects in rados://%s/%s", pool, namespace)
+38
View File
@@ -27,8 +27,46 @@ import (
"github.com/rook/rook/pkg/util/exec"
"github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRadosNamespaceHasObjects(t *testing.T) {
RunAllCephCommandsInToolboxPod = ""
capturedShellCmd := func(t *testing.T, pool, namespace string) string {
var shellCmd string
me := &test.MockExecutor{
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
require.Equal(t, "sh", command)
require.Len(t, arg, 2)
require.Equal(t, "-c", arg[0])
shellCmd = arg[1]
return "", nil
},
}
ctx := &clusterd.Context{Executor: me, ConfigDir: "/var/lib/rook"}
_, err := RadosNamespaceHasObjects(ctx, AdminTestClusterInfo("rook-ceph"), pool, namespace)
require.NoError(t, err)
return shellCmd
}
t.Run("ordinary names stay unquoted", func(t *testing.T) {
assert.Equal(t,
"rados --pool mypool --namespace myns ls "+
"--cluster=rook-ceph "+
"--conf=/var/lib/rook/rook-ceph/rook-ceph.config "+
"--name=client.admin "+
"--keyring=/var/lib/rook/rook-ceph/client.admin.keyring "+
"2>/dev/null | head -c 1",
capturedShellCmd(t, "mypool", "myns"))
})
t.Run("shell metacharacters cannot break out of an argument", func(t *testing.T) {
assert.Contains(t, capturedShellCmd(t, "mypool", "myns; rm -rf /"),
"--namespace 'myns; rm -rf /' ls")
})
}
func TestRadosRemoveObject(t *testing.T) {
newTest := func(mockExec *test.MockExecutor) (*clusterd.Context, *ClusterInfo) {
ctx := &clusterd.Context{
+1 -1
View File
@@ -151,7 +151,7 @@ func (e *RemotePodCommandExecutor) CopyLocalFileToContainer(ctx context.Context,
}
defer file.Close()
stdOut, stdErr, err := e.ExecWithOptions(ctx, ExecOptions{
Command: []string{"sh", "-c", fmt.Sprintf("cat - > %s", dstPath)},
Command: []string{"sh", "-c", fmt.Sprintf("cat - > %s", quoteArg(dstPath))},
Namespace: namespace,
PodName: pods.Items[0].Name,
ContainerName: containerName,