From 937899687fd82a6fe5dcce76358f23b450f24180 Mon Sep 17 00:00:00 2001 From: Gianluca Mardente Date: Fri, 4 Dec 2026 11:10:39 +0100 Subject: [PATCH] fix: never trust a Helm release's own Hooks during internal recovery uninstalls handleUpgradeError/recoverRelease and handleInstall's consecutive-failure self-healing both call Helm's uninstall action, with hooks enabled by default, on a release whose recorded state (status, and the Hooks field that comes with it) Sveltos did not verify. Anyone with write access to Secrets in the release namespace can forge that state: relabel the current release as superseded, inject a next-revision Secret with status "unknown" and a pre-delete hook of their choosing, then trigger a redeploy. The upgrade fails with "has no deployed releases", recoverRelease decides to uninstall to clear the inconsistency, and the forged hook runs with addon-controller's own (often cluster-admin-level) credentials for the managed cluster - a confused-deputy privilege escalation from whatever RBAC the attacker had in their own namespace. Add forceDisableHooks to getHelmUninstallClient/uninstallRelease/ doUninstallRelease and set it for the two call sites that are an internal recovery heuristic rather than an admin decision: recoverRelease, and handleInstall's uninstall-after-N-consecutive- failures. Both read release state addon-controller never produced and has no reason to trust. The other three uninstallRelease callers (explicit HelmChartActionUninstall, a ClusterSummary's own teardown when its ClusterProfile/Profile is deleted or stops matching, and the stale/orphaned-release cleanup loop) are admin-driven lifecycle decisions and keep respecting the chart's own DisableHooks option, unchanged. Reproduced end-to-end on local kind clusters (management + managed, push mode): a ServiceAccount with only the built-in edit role in one namespace forged a release this way and had addon-controller create a ClusterRoleBinding granting it cluster-admin. Rebuilding with this fix and replaying the same forgery, the recovery still clears the inconsistent release but the forged hook no longer runs. handleInstall's path is harder to trigger than recoverRelease's: the same release-state read gates both whether handleInstall is entered (status nil/Uninstalled) and whether Helm's own Uninstall.Run skips hooks (it always does when status is already Uninstalled), so a static forged state can't satisfy both at once - reaching it for real would need winning a race between two near-simultaneous reads. Fixed anyway as free defense in depth: this is an internal heuristic, not an admin intent, so it should never have trusted hook content regardless of how hard it is to reach. --- controllers/export_test.go | 1 + controllers/handlers_helm.go | 53 +++++++++++++++++++++++++------ controllers/handlers_helm_test.go | 36 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/controllers/export_test.go b/controllers/export_test.go index 2296539c..56f66474 100644 --- a/controllers/export_test.go +++ b/controllers/export_test.go @@ -360,6 +360,7 @@ var ( FindRemovedCRDVersions = findRemovedCRDVersions WithRestConfig = withRestConfig GetHelmUpgradeClient = getHelmUpgradeClient + GetHelmUninstallClient = getHelmUninstallClient ) type ( diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index 7c00d9a4..4b116d63 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -1031,7 +1031,11 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c } if currentRelease != nil && currentRelease.Status != string(releasecommon.StatusUninstalled) { - err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions, logger) + // ClusterProfile/Profile deleted or stopped matching this cluster: an admin + // lifecycle decision, not an internal recovery heuristic. Respect the chart's + // own hook settings. + err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions, + false, logger) if err != nil { if !errors.Is(err, driver.ErrReleaseNotFound) { return nil, err @@ -1687,7 +1691,11 @@ func handleInstall(ctx context.Context, dCtx *deploymentContext, if !isPullMode { if fs := getFeatureSummaryForFeatureID(dCtx.clusterSummary, libsveltosv1beta1.FeatureHelm); fs != nil { if fs.ConsecutiveFailures%recoverAfter == 0 && fs.FailureMessage != nil { - err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions, logger) + // Internal recovery heuristic, not an admin decision: nothing validated that + // this release's recorded Hooks reflect what Sveltos itself last deployed, so + // they must not run. See getHelmUninstallClient. + err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions, + true, logger) if err != nil { // Ignore release not found error if errors.Is(err, driver.ErrReleaseNotFound) || strings.Contains(err.Error(), "release: not found") { @@ -1760,7 +1768,8 @@ func handleUninstall(ctx context.Context, clusterSummary *configv1beta1.ClusterS var report *configv1beta1.ReleaseReport logger.V(logs.LogDebug).Info("uninstall helm release") - err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, logger) + // Explicit HelmChartActionUninstall: an admin decision. Respect the chart's own hook settings. + err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, false, logger) if err != nil { return nil, err } @@ -2250,9 +2259,12 @@ func checkDependencies(chartRequested chartbase.Charter, installClient *action.I // uninstallRelease removes helm release from a managed Cluster. // No action in DryRun mode. +// uninstallRelease uninstalls a Helm release. forceDisableHooks is documented on +// getHelmUninstallClient: pass true only when recovering from a release whose stored +// state (and so whose Hooks) cannot be trusted. func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary, releaseName, releaseNamespace, kubeconfig string, registryOptions *registryClientOptions, - helmChart *configv1beta1.HelmChart, logger logr.Logger) error { + helmChart *configv1beta1.HelmChart, forceDisableHooks bool, logger logr.Logger) error { // No-op in DryRun mode if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun { @@ -2291,7 +2303,7 @@ func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Cluster return err } - uninstallClient := getHelmUninstallClient(helmChart, actionConfig) + uninstallClient := getHelmUninstallClient(helmChart, actionConfig, forceDisableHooks) _, err = uninstallClient.Run(releaseName) if err != nil { @@ -3228,8 +3240,10 @@ func recoverRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSu return nil } + // This release's state was just found inconsistent (status), so its stored Hooks + // cannot be trusted: forceDisableHooks=true. See getHelmUninstallClient. return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName, - requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, logger) + requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, true, logger) } // shouldInstall returns true if action is not uninstall and there is no release to manage @@ -3421,9 +3435,15 @@ func doInstallRelease(ctx context.Context, dCtx *deploymentContext, // doUninstallRelease uninstalls helm release from the Cluster. // No action in DryRun mode. +// doUninstallRelease uninstalls a Helm release that an admin decision (not an internal +// recovery heuristic) determined should go away: an explicit HelmChartActionUninstall, or a +// whole ClusterSummary being torn down because its ClusterProfile/Profile was deleted or +// stopped matching. forceDisableHooks is documented on getHelmUninstallClient; it must stay +// false here so the chart's own Options.DisableHooks (the admin's own choice for this chart) +// is respected, same as any other admin-intended uninstall. func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary, requestedChart *configv1beta1.HelmChart, kubeconfig string, registryOptions *registryClientOptions, - logger logr.Logger) error { + forceDisableHooks bool, logger logr.Logger) error { if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun { return nil @@ -3435,7 +3455,7 @@ func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Clust requestedChart.RepositoryName)) return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName, requestedChart.ReleaseNamespace, - kubeconfig, registryOptions, requestedChart, logger) + kubeconfig, registryOptions, requestedChart, forceDisableHooks, logger) } // doUpgradeRelease upgrades helm release in the Cluster. @@ -3544,7 +3564,7 @@ func undeployStaleReleases(ctx context.Context, c client.Client, clusterSummary } if err := uninstallRelease(ctx, clusterSummary, staleReleases[i].Name, - staleReleases[i].Namespace, kubeconfig, ®istryClientOptions{}, nil, logger); err != nil { + staleReleases[i].Namespace, kubeconfig, ®istryClientOptions{}, nil, false, logger); err != nil { return nil, err } @@ -4960,8 +4980,18 @@ func getHelmUpgradeClient(requestedChart *configv1beta1.HelmChart, actionConfig return upgradeClient } +// getHelmUninstallClient builds the Helm uninstall client. forceDisableHooks must be true +// when the uninstall is recovering from a release whose state we do not trust (see +// recoverRelease): the release being uninstalled was found pending/uninstalling/unknown, +// meaning its Hooks come from a Secret that is not known to reflect what Sveltos actually +// deployed. Anyone able to write Secrets in the release namespace can set those Hooks, so +// running them would execute attacker-controlled manifests with Sveltos' own, usually far +// more privileged, credentials for the managed cluster. requestedChart.Options' DisableHooks +// setting must not override this: it reflects what the admin wants for a normal deploy or +// uninstall of a release whose content Sveltos itself produced, not for clearing a release +// state that cannot be trusted. func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfig *action.Configuration, -) *action.Uninstall { + forceDisableHooks bool) *action.Uninstall { uninstallClient := action.NewUninstall(actionConfig) uninstallClient.DryRun = false @@ -4976,6 +5006,9 @@ func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfi uninstallClient.KeepHistory = getKeepHistoryValue(requestedChart.Options) uninstallClient.DeletionPropagation = getDeletionPropagation(requestedChart.Options) } + if forceDisableHooks { + uninstallClient.DisableHooks = true + } return uninstallClient } diff --git a/controllers/handlers_helm_test.go b/controllers/handlers_helm_test.go index 4a060bb0..7382f2df 100644 --- a/controllers/handlers_helm_test.go +++ b/controllers/handlers_helm_test.go @@ -3333,6 +3333,42 @@ var _ = Describe("getHelmUpgradeClient Force", func() { }) }) +var _ = Describe("getHelmUninstallClient forceDisableHooks", func() { + It("disables hooks when forceDisableHooks is true, even if the chart wants hooks enabled", func() { + requestedChart := &configv1beta1.HelmChart{ + ReleaseName: randomString(), + ReleaseNamespace: randomString(), + Options: &configv1beta1.HelmOptions{ + DisableHooks: false, + }, + } + + uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, true) + + Expect(uninstallClient.DisableHooks).To(BeTrue()) + }) + + It("respects the chart's own DisableHooks setting when forceDisableHooks is false", func() { + requestedChart := &configv1beta1.HelmChart{ + ReleaseName: randomString(), + ReleaseNamespace: randomString(), + Options: &configv1beta1.HelmOptions{ + DisableHooks: false, + }, + } + + uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, false) + + Expect(uninstallClient.DisableHooks).To(BeFalse()) + }) + + It("still disables hooks when forceDisableHooks is true and requestedChart is nil", func() { + uninstallClient := controllers.GetHelmUninstallClient(nil, &action.Configuration{}, true) + + Expect(uninstallClient.DisableHooks).To(BeTrue()) + }) +}) + var _ = Describe("getCRDObjects", func() { newChartWithFiles := func(name string, fileNames ...string) *chart.Chart { files := make([]*common.File, 0, len(fileNames))