|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/toob-boot/toob/internal/elfaudit" |
| 13 | + "github.com/toob-boot/toob/internal/manifest" |
| 14 | + "github.com/toob-boot/toob/internal/ui" |
| 15 | +) |
| 16 | + |
| 17 | +var elfauditProfile string |
| 18 | +var elfauditHardware string |
| 19 | +var elfauditBudget uint64 |
| 20 | + |
| 21 | +var elfauditCmd = &cobra.Command{ |
| 22 | + Use: "elfaudit [path/to/target.elf]", |
| 23 | + Short: "Post-link ELF audit: mock poison-pill, memory overlap, budget check", |
| 24 | + Long: `Inspects a linked ELF binary for: |
| 25 | + (a) Mock Poison-Pill — no *_mock symbols in production profile |
| 26 | + (b) Memory Overlap — sections vs. reserved_ram_regions from hardware.json |
| 27 | + (c) Budget Footprint — loadable content vs. stage1_size budget`, |
| 28 | + Args: cobra.ExactArgs(1), |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + elfPath, err := filepath.Abs(args[0]) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("invalid ELF path: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + if _, err := os.Stat(elfPath); os.IsNotExist(err) { |
| 36 | + return fmt.Errorf("ELF binary not found: %s", elfPath) |
| 37 | + } |
| 38 | + |
| 39 | + config := elfaudit.ELFAuditConfig{ |
| 40 | + Profile: elfauditProfile, |
| 41 | + Stage1MaxBytes: elfauditBudget, |
| 42 | + } |
| 43 | + |
| 44 | + // Load reserved regions from hardware.json if provided |
| 45 | + if elfauditHardware != "" { |
| 46 | + absHW, err := filepath.Abs(elfauditHardware) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("invalid hardware.json path: %w", err) |
| 49 | + } |
| 50 | + data, err := os.ReadFile(absHW) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to read hardware.json: %w", err) |
| 53 | + } |
| 54 | + var hj manifest.HardwareJson |
| 55 | + if err := json.Unmarshal(data, &hj); err != nil { |
| 56 | + return fmt.Errorf("failed to parse hardware.json: %w", err) |
| 57 | + } |
| 58 | + for _, r := range hj.ReservedRamRegions { |
| 59 | + base, err := strconv.ParseUint(strings.TrimPrefix(r.Base, "0x"), 16, 64) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("invalid base address '%s' for reserved region '%s': %w", r.Base, r.Name, err) |
| 62 | + } |
| 63 | + config.ReservedRegions = append(config.ReservedRegions, elfaudit.MemoryRegion{ |
| 64 | + Name: r.Name, |
| 65 | + Base: base, |
| 66 | + Size: uint64(r.Size), |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + ui.Header(fmt.Sprintf("ELF Audit: %s [profile=%s]", filepath.Base(elfPath), config.Profile)) |
| 72 | + |
| 73 | + report, err := elfaudit.AuditELF(elfPath, config) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("ELF audit failed: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + for _, chk := range report.Checks { |
| 79 | + if chk.Passed { |
| 80 | + ui.Success("%s: %s", chk.Name, chk.Details) |
| 81 | + } else { |
| 82 | + ui.Error("%s: %s", chk.Name, chk.Details) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Export JSON report next to the ELF |
| 87 | + reportPath := elfPath + ".audit.json" |
| 88 | + reportData, _ := json.MarshalIndent(report, "", " ") |
| 89 | + _ = os.WriteFile(reportPath, reportData, 0o644) |
| 90 | + |
| 91 | + if !report.Passed { |
| 92 | + return fmt.Errorf("FATAL [ELF_AUDIT_FAIL]: Binary failed post-link audit!") |
| 93 | + } |
| 94 | + |
| 95 | + ui.Success("ELF audit PASSED cleanly. Report: %s", reportPath) |
| 96 | + return nil |
| 97 | + }, |
| 98 | +} |
| 99 | + |
| 100 | +func init() { |
| 101 | + elfauditCmd.Flags().StringVar(&elfauditProfile, "profile", "production", "Build profile (production|sandbox)") |
| 102 | + elfauditCmd.Flags().StringVar(&elfauditHardware, "hardware", "", "Path to hardware.json for reserved region checks") |
| 103 | + elfauditCmd.Flags().Uint64Var(&elfauditBudget, "budget", 0, "Stage1 size budget in bytes (0 = skip budget check)") |
| 104 | + rootCmd.AddCommand(elfauditCmd) |
| 105 | +} |
| 106 | + |
0 commit comments