diff --git a/changelog.d/security-19089-command-safety-regex-widening.md b/changelog.d/security-19089-command-safety-regex-widening.md new file mode 100644 index 0000000000..e3ca3383c1 --- /dev/null +++ b/changelog.d/security-19089-command-safety-regex-widening.md @@ -0,0 +1 @@ +- Security: the command-safety scanner now flags `dd` writing to a raw block device from any source (`dd if=image.iso of=/dev/sda`), and credential reads that use quoting, `$HOME`/`${HOME}`/`/root`, or readers such as `less`, `head` and `base64` (#19089) diff --git a/src/crux/toolapprove/security.go b/src/crux/toolapprove/security.go index f52b45094e..e47364cef4 100644 --- a/src/crux/toolapprove/security.go +++ b/src/crux/toolapprove/security.go @@ -108,11 +108,23 @@ func NewDefaultSecurityScanner() *DefaultSecurityScanner { var ( // destructiveCmdRe matches the non-rm destructive commands: mkfs, dd - // from /dev/zero and the fork bomb, which needs no word boundary. A - // recursive rm of a protected directory is decided by analyzeRmCommand - // (shellrm.go), which reads the command the way the shell does. - destructiveCmdRe = regexp.MustCompile(`(?i)(\bmkfs\b|\bdd\s+if=/dev/zero\b|:\(\)\{\s*:\|:&\s*\};:)`) - credentialExfilRe = regexp.MustCompile(`(?i)\b(cat\s+~/\.ssh/|cat\s+/var/run/secrets/|printenv\s+.*TOKEN|echo\s+\$GH_TOKEN|echo\s+\$GITHUB_TOKEN)\b`) + // from /dev/zero or onto a raw block device, and the fork bomb, which + // needs no word boundary. A recursive rm of a protected directory is + // decided by analyzeRmCommand (shellrm.go), which reads the command the + // way the shell does. dd is destructive by its output target as much + // as its input source: "dd if=/dev/urandom of=/dev/sda" wipes a disk + // without ever reading /dev/zero, so both sides are checked. + destructiveCmdRe = regexp.MustCompile(`(?i)(\bmkfs\b|\bdd\s+if=/dev/zero\b|\bdd\s+[^;|&\n]*\bof=/dev/(?:[sh]d|vd|xvd|nvme)[a-z0-9]*\b|:\(\)\{\s*:\|:&\s*\};:)`) + // credentialExfilRe flags common commands that read or print credential + // material. It is a substring/pattern heuristic, not a shell parser + // (unlike analyzeRmCommand): it tolerates the quoting and $HOME/${HOME} + // variants a normal command line uses, but it does not follow variable + // indirection, and it deliberately omits bare env/printenv dumps, which + // are routine and would make the check too noisy to keep enabled. + credentialExfilRe = regexp.MustCompile(`(?i)\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)(?:\s+-\S+(?:\s+\d+)?)*\s+["']?(?:~|\$\{?HOME\}?|/root)["']?/\.ssh/` + + `|\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)(?:\s+-\S+(?:\s+\d+)?)*\s+["']?/var/run/secrets/` + + `|\bprintenv\s+.*TOKEN` + + `|\becho\s+["']?\$\{?(?:GH_TOKEN|GITHUB_TOKEN)\b`) ) // Scan analyzes the tool request for security violations. diff --git a/src/crux/toolapprove/security_command_test.go b/src/crux/toolapprove/security_command_test.go index c14f8f0f8a..41cf93391a 100644 --- a/src/crux/toolapprove/security_command_test.go +++ b/src/crux/toolapprove/security_command_test.go @@ -158,3 +158,103 @@ func TestDefaultSecurityScannerDestructiveCommandsWithoutTextFindings(t *testing }) } } + +func TestDefaultSecurityScannerDdBlockDeviceWithoutTextFindings(t *testing.T) { + scanner := NewDefaultSecurityScanner() + scanner.Text = textScannerFunc(func(string) TextVerdict { return TextVerdict{} }) + + for _, command := range []string{ + "dd if=/dev/zero of=/dev/sda", + "dd if=/dev/urandom of=/dev/sda", + "dd if=/dev/urandom of=/dev/sda bs=1M", + "dd of=/dev/sda if=/dev/urandom", + "dd if=image.iso of=/dev/sdb", + "dd if=image.iso of=/dev/nvme0n1", + "dd if=image.iso of=/dev/vda", + "dd if=image.iso of=/dev/xvda", + "dd if=image.iso of=/dev/hda", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if result.Passed { + t.Fatalf("Scan(%q) = %+v, want a destructive-command violation", command, result) + } + }) + } + + for _, command := range []string{ + "dd if=image.iso of=output.img", + "dd if=image.iso of=/dev/null bs=1M", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if !result.Passed || len(result.Violations) != 0 { + t.Fatalf("Scan(%q) = %+v, want clean scan", command, result) + } + }) + } +} + +func TestDefaultSecurityScannerCredentialExfilBypasses(t *testing.T) { + scanner := NewDefaultSecurityScanner() + scanner.Text = textScannerFunc(func(string) TextVerdict { return TextVerdict{} }) + + for _, command := range []string{ + "cat ~/.ssh/id_rsa", + "cat /var/run/secrets/kubernetes.io/serviceaccount/token", + "printenv GH_TOKEN", + "printenv GITHUB_TOKEN", + "echo $GH_TOKEN", + "echo $GITHUB_TOKEN", + // Previously missed: quoting around the variable. + `echo "$GH_TOKEN"`, + `echo '$GITHUB_TOKEN'`, + "echo ${GH_TOKEN}", + // Previously missed: $HOME/${HOME} instead of ~, and /root. + "cat $HOME/.ssh/id_rsa", + `cat "$HOME/.ssh/id_rsa"`, + "cat ${HOME}/.ssh/id_rsa", + "cat /root/.ssh/id_rsa", + // Previously missed: readers other than cat. + "less ~/.ssh/id_rsa", + "head -c 100 ~/.ssh/id_rsa", + "tail -n 5 $HOME/.ssh/id_rsa", + "base64 ~/.ssh/id_rsa", + "tail /var/run/secrets/kubernetes.io/serviceaccount/token", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if result.Passed { + t.Fatalf("Scan(%q) = %+v, want a credential-exfiltration violation", command, result) + } + }) + } + + for _, command := range []string{ + "env", + "printenv", + "echo hello", + "echo $PATH", + "cat README.md", + "cat ~/.ssh_backup/id_rsa", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if !result.Passed || len(result.Violations) != 0 { + t.Fatalf("Scan(%q) = %+v, want clean scan", command, result) + } + }) + } +}