Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 17 additions & 5 deletions src/crux/toolapprove/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
100 changes: 100 additions & 0 deletions src/crux/toolapprove/security_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Loading