CVE-2023-34236 (published 2023-07-14T22:15Z)

Weave GitOps Terraform Controller (aka Weave TF-controller) is a controller for Flux to reconcile Terraform resources in a GitOps way. A vulnerability has been identified in Weave GitOps Terraform Controller which could allow an authenticated remote attacker to view sensitive information. This vulnerability stems from Weave GitOps Terraform Runners (`tf-runner`), where sensitive data is inadvertently printed - potentially revealing sensitive user data in their pod logs. In particular, functions `tfexec.ShowPlan`, `tfexec.ShowPlanRaw`, and `tfexec.Output` are implicated when the `tfexec` object set its `Stdout` and `Stderr` to be `os.Stdout` and `os.Stderr`. An unauthorized remote attacker could exploit this vulnerability by accessing these prints of sensitive information, which may contain configurations or tokens that could be used to gain unauthorized control or access to resources managed by the Terraform controller. A successful exploit could allow the attacker to utilize this sensitive data, potentially leading to unauthorized access or control of the system. This vulnerability has been addressed in Weave GitOps Terraform Controller versions `v0.14.4` and `v0.15.0-rc.5`. Users are urged to upgrade to one of these versions to mitigate the vulnerability. As a temporary measure until the patch can be applied, users can add the environment variable `DISABLE_TF_LOGS` to the tf-runners via the runner pod template of the Terraform Custom Resource. This will prevent the logging of sensitive information and mitigate the risk of this vulnerability.

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-200
cwe_name: Exposure of Sensitive Information to an Unauthorized Actor
description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
extended_description: 
	   There are many different kinds of mistakes that introduce information exposures. The severity of the error can range widely, depending on the context in which the product operates, the type of sensitive information that is revealed, and the benefits it may provide to an attacker.  Some kinds of sensitive information include:
       
         private, personal information, such as personal messages, financial data, health records, geographic location, or contact details
         system status and environment, such as the operating system and installed packages
		 business secrets and intellectual property
		 network status and configuration
		 the product's own code or internal state
         metadata, e.g. logging of connections or message headers
		 indirect information, such as a discrepancy between two internal operations that can be observed by an outsider
       
	    Information might be sensitive to different parties, each of which may have their own expectations for whether the information should be protected.  These parties include:
               
                  the product's own users
                  people or organizations whose information is created or used by the product, even if they are not direct product users
		  the product's administrators, including the admins of the system(s) and/or networks on which the product operates
                  the developer
               
	    Information exposures can occur in different ways:
               
                  the code explicitly inserts sensitive information into resources or messages that are intentionally made accessible to unauthorized actors, but should not contain the information - i.e., the information should have been "scrubbed" or "sanitized"
                  a different weakness or mistake indirectly inserts the sensitive information into resources, such as a web script error revealing the full system path of the program.
                  the code manages resources that intentionally contain sensitive information, but the resources are unintentionally made accessible to unauthorized actors. In this case, the information exposure is resultant - i.e., a different weakness enabled the access to the information in the first place.
               
	    It is common practice to describe any loss of confidentiality as an "information exposure," but this can lead to overuse of CWE-200 in CWE mapping. From the CWE perspective, loss of confidentiality is a technical impact that can arise from dozens of different weaknesses, such as insecure file permissions or out-of-bounds read.  CWE-200 and its lower-level descendants are intended to cover the mistakes that occur in behaviors that explicitly manage, store, transfer, or cleanse sensitive information.
         
         

Fix information:
repo_url: https://github.com/weaveworks/tf-controller
commit_details: repo_url: https://github.com/weaveworks/tf-controller
author: Chanwit Kaewkasi
author_date: 2023-06-02 19:51:36+07:00
msg: fix logging in tf-runner

Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
repository: repo_url: https://github.com/weaveworks/tf-controller
repo_name: flux-iac/tofu-controller
description: A GitOps OpenTofu and Terraform controller for Flux
repo_language: Go
owner: weaveworks
file_changes: filename: server.go
change_type: ModificationType.MODIFY
diff: @@ -170,6 +170,18 @@ func (r *TerraformRunnerServer) ProcessCliConfig(ctx context.Context, req *Proce
 	return &ProcessCliConfigReply{FilePath: tfrcFilepath}, nil
 }
 
+// initLogger sets up the logger for the terraform runner
+func (r *TerraformRunnerServer) initLogger(log logr.Logger) {
+	disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1"
+	if !disableTestLogging {
+		r.tf.SetStdout(os.Stdout)
+		r.tf.SetStderr(os.Stderr)
+		if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" {
+			r.tf.SetLogger(&LocalPrintfer{logger: log})
+		}
+	}
+}
+
 func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraformRequest) (*NewTerraformReply, error) {
 	r.InstanceID = req.GetInstanceID()
 	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
@@ -191,14 +203,8 @@ func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraf
 	// cache the Terraform resource when initializing
 	r.terraform = &terraform
 
-	disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1"
-	if !disableTestLogging {
-		r.tf.SetStdout(os.Stdout)
-		r.tf.SetStderr(os.Stderr)
-		if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" {
-			r.tf.SetLogger(&LocalPrintfer{logger: log})
-		}
-	}
+	// init default logger
+	r.initLogger(log)
 
 	return &NewTerraformReply{Id: r.InstanceID}, nil
 }

programming_language: Go
filename: server_plan.go
change_type: ModificationType.MODIFY
diff: @@ -4,12 +4,40 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"io"
+
 	"github.com/hashicorp/terraform-exec/tfexec"
+	tfjson "github.com/hashicorp/terraform-json"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 	"sigs.k8s.io/controller-runtime"
+	ctrl "sigs.k8s.io/controller-runtime"
 )
 
+func (r *TerraformRunnerServer) tfShowPlanFile(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (*tfjson.Plan, error) {
+	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
+
+	// This is the only place where we disable the logger
+	r.tf.SetStdout(io.Discard)
+	r.tf.SetStderr(io.Discard)
+
+	defer r.initLogger(log)
+
+	return r.tf.ShowPlanFile(ctx, planPath, opts...)
+}
+
+func (r *TerraformRunnerServer) tfShowPlanFileRaw(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) {
+	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
+
+	// This is the only place where we disable the logger
+	r.tf.SetStdout(io.Discard)
+	r.tf.SetStderr(io.Discard)
+
+	defer r.initLogger(log)
+
+	return r.tf.ShowPlanFileRaw(ctx, planPath, opts...)
+}
+
 func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*PlanReply, error) {
 	log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
 	log.Info("creating a plan")
@@ -68,7 +96,8 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
 	planCreated := false
 	if req.Out != "" {
 		planCreated = true
-		plan, err := r.tf.ShowPlanFile(ctx, req.Out)
+
+		plan, err := r.tfShowPlanFile(ctx, req.Out)
 		if err != nil {
 			return nil, err
 		}
@@ -81,6 +110,7 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
 			plan.OutputChanges == nil {
 			planCreated = false
 		}
+
 	}
 
 	return &PlanReply{Message: "ok", Drifted: drifted, PlanCreated: planCreated}, nil

programming_language: Go
filename: server_save_tfplan.go
change_type: ModificationType.MODIFY
diff: @@ -15,11 +15,11 @@ import (
 	"k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/types"
-	"sigs.k8s.io/controller-runtime"
+	ctrl "sigs.k8s.io/controller-runtime"
 )
 
 func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanRequest) (*SaveTFPlanReply, error) {
-	log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
+	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
 	log.Info("save the plan")
 	if req.TfInstance != r.InstanceID {
 		err := fmt.Errorf("no TF instance found")
@@ -47,7 +47,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR
 	}
 
 	if r.terraform.Spec.StoreReadablePlan == "json" {
-		planObj, err := r.tf.ShowPlanFile(ctx, TFPlanName)
+		planObj, err := r.tfShowPlanFile(ctx, TFPlanName)
 		if err != nil {
 			log.Error(err, "unable to get the plan output for json")
 			return nil, err
@@ -63,7 +63,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR
 		}
 
 	} else if r.terraform.Spec.StoreReadablePlan == "human" {
-		rawOutput, err := r.tf.ShowPlanFileRaw(ctx, TFPlanName)
+		rawOutput, err := r.tfShowPlanFileRaw(ctx, TFPlanName)
 		if err != nil {
 			log.Error(err, "unable to get the plan output for human")
 			return nil, err

programming_language: Go
filename: server_show_plan.go
change_type: ModificationType.MODIFY
diff: @@ -16,7 +16,7 @@ func (r *TerraformRunnerServer) ShowPlanFileRaw(ctx context.Context, req *ShowPl
 		return nil, err
 	}
 
-	rawOutput, err := r.tf.ShowPlanFileRaw(ctx, req.Filename)
+	rawOutput, err := r.tfShowPlanFileRaw(ctx, req.Filename)
 	if err != nil {
 		log.Error(err, "unable to get the raw plan output")
 		return nil, err
@@ -34,7 +34,7 @@ func (r *TerraformRunnerServer) ShowPlanFile(ctx context.Context, req *ShowPlanF
 		return nil, err
 	}
 
-	plan, err := r.tf.ShowPlanFile(ctx, req.Filename)
+	plan, err := r.tfShowPlanFile(ctx, req.Filename)
 	if err != nil {
 		log.Error(err, "unable to get the json plan output")
 		return nil, err

programming_language: Go
repo_url: https://github.com/weaveworks/tf-controller
commit_details: repo_url: https://github.com/weaveworks/tf-controller
author: Chanwit Kaewkasi
author_date: 2023-06-06 20:07:06+07:00
msg: fix loggin in tf.output

Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
repository: repo_url: https://github.com/weaveworks/tf-controller
repo_name: flux-iac/tofu-controller
description: A GitOps OpenTofu and Terraform controller for Flux
repo_language: Go
owner: weaveworks
file_changes: filename: server_outputs.go
change_type: ModificationType.MODIFY
diff: @@ -3,8 +3,10 @@ package runner
 import (
 	"context"
 	"fmt"
+	"io"
 	"reflect"
 
+	"github.com/hashicorp/terraform-exec/tfexec"
 	infrav1 "github.com/weaveworks/tf-controller/api/v1alpha2"
 	corev1 "k8s.io/api/core/v1"
 	apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -13,6 +15,18 @@ import (
 	ctrl "sigs.k8s.io/controller-runtime"
 )
 
+func (r *TerraformRunnerServer) tfOutput(ctx context.Context, opts ...tfexec.OutputOption) (map[string]tfexec.OutputMeta, error) {
+	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
+
+	// This is the only place where we disable the logger
+	r.tf.SetStdout(io.Discard)
+	r.tf.SetStderr(io.Discard)
+
+	defer r.initLogger(log)
+
+	return r.tf.Output(ctx, opts...)
+}
+
 func (r *TerraformRunnerServer) Output(ctx context.Context, req *OutputRequest) (*OutputReply, error) {
 	log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
 	log.Info("creating outputs")
@@ -22,7 +36,7 @@ func (r *TerraformRunnerServer) Output(ctx context.Context, req *OutputRequest)
 		return nil, err
 	}
 
-	outputs, err := r.tf.Output(ctx)
+	outputs, err := r.tfOutput(ctx)
 	if err != nil {
 		log.Error(err, "unable to get outputs")
 		return nil, err

programming_language: Go