diff --git a/pkg/daemon/ceph/client/rados.go b/pkg/daemon/ceph/client/rados.go index e8fe36749..f94184e3a 100644 --- a/pkg/daemon/ceph/client/rados.go +++ b/pkg/daemon/ceph/client/rados.go @@ -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) diff --git a/pkg/daemon/ceph/client/rados_test.go b/pkg/daemon/ceph/client/rados_test.go index 560d002c0..c361142ca 100644 --- a/pkg/daemon/ceph/client/rados_test.go +++ b/pkg/daemon/ceph/client/rados_test.go @@ -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{ diff --git a/pkg/util/exec/exec_pod.go b/pkg/util/exec/exec_pod.go index afccf5756..91a127347 100644 --- a/pkg/util/exec/exec_pod.go +++ b/pkg/util/exec/exec_pod.go @@ -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,