| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 3 | |
| 4 | // This package implements a command line tool that creates dm-verity hash |
| 5 | // images at a selected path, given an existing data image. The tool |
| 6 | // outputs a Verity mapping table on success. |
| 7 | // |
| 8 | // For more information, see: |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 9 | // - source.monogon.dev/osbase/verity |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 10 | // - https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity |
| 11 | package main |
| 12 | |
| 13 | import ( |
| Jan Schär | 3871fa1 | 2025-07-09 17:30:00 +0000 | [diff] [blame] | 14 | "crypto/sha256" |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 15 | "flag" |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 16 | "fmt" |
| 17 | "io" |
| 18 | "log" |
| 19 | "os" |
| 20 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 21 | "source.monogon.dev/osbase/verity" |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 22 | ) |
| 23 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 24 | // createImage creates a dm-verity target image by combining the input image |
| 25 | // with Verity metadata. Contents of the data image are copied to the output |
| 26 | // image. Then, the same contents are verity-encoded and appended to the |
| 27 | // output image. The verity superblock is written only if wsb is true. It |
| 28 | // returns either a dm-verity target table, or an error. |
| 29 | func createImage(dataImagePath, outputImagePath string, wsb bool) (*verity.MappingTable, error) { |
| 30 | // Hardcode both the data block size and the hash block size as 4096 bytes. |
| 31 | bs := uint32(4096) |
| 32 | |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 33 | // Open the data image for reading. |
| 34 | dataImage, err := os.Open(dataImagePath) |
| 35 | if err != nil { |
| 36 | return nil, fmt.Errorf("while opening the data image: %w", err) |
| 37 | } |
| 38 | defer dataImage.Close() |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 39 | |
| 40 | // Check that the data image is well-formed. |
| 41 | ds, err := dataImage.Stat() |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("while stat-ing the data image: %w", err) |
| 44 | } |
| 45 | if !ds.Mode().IsRegular() { |
| 46 | return nil, fmt.Errorf("the data image must be a regular file") |
| 47 | } |
| 48 | if ds.Size()%int64(bs) != 0 { |
| Tim Windelschmidt | 73e9882 | 2024-04-18 23:13:49 +0200 | [diff] [blame] | 49 | return nil, fmt.Errorf("the data image must end on a %d-byte block boundary", bs) |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 52 | // Create an empty hash image file. |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 53 | outputImage, err := os.OpenFile(outputImagePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 54 | if err != nil { |
| 55 | return nil, fmt.Errorf("while opening the hash image for writing: %w", err) |
| 56 | } |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 57 | defer outputImage.Close() |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 58 | |
| Jan Schär | 3871fa1 | 2025-07-09 17:30:00 +0000 | [diff] [blame] | 59 | // Copy the input data into the output file, then rewind dataImage. |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 60 | _, err = io.Copy(outputImage, dataImage) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| Jan Schär | 3871fa1 | 2025-07-09 17:30:00 +0000 | [diff] [blame] | 64 | _, err = dataImage.Seek(0, io.SeekStart) |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| Jan Schär | 3871fa1 | 2025-07-09 17:30:00 +0000 | [diff] [blame] | 69 | // Hash the input data for use as the salt, then rewind dataImage. The purpose |
| 70 | // of the salt is to prevent reuse of collisions across different images. 16 |
| 71 | // bytes is enough for this. We use a hash of the input instead of generating |
| 72 | // random bytes to make the build reproducible. |
| 73 | dataHash := sha256.New() |
| 74 | _, err = io.Copy(dataHash, dataImage) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | _, err = dataImage.Seek(0, io.SeekStart) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | salt := dataHash.Sum(nil)[:16] |
| 83 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 84 | // Write outputImage contents. Start with initializing a verity encoder, |
| Jan Schär | 3871fa1 | 2025-07-09 17:30:00 +0000 | [diff] [blame] | 85 | // setting outputImage as its output. |
| 86 | v, err := verity.NewEncoder(outputImage, bs, bs, salt, wsb) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 87 | if err != nil { |
| 88 | return nil, fmt.Errorf("while initializing a verity encoder: %w", err) |
| 89 | } |
| 90 | // Hash the contents of dataImage, block by block. |
| 91 | _, err = io.Copy(v, dataImage) |
| 92 | if err != nil { |
| 93 | return nil, fmt.Errorf("while reading the data image: %w", err) |
| 94 | } |
| 95 | // The resulting hash tree won't be written until Close is called. |
| 96 | err = v.Close() |
| 97 | if err != nil { |
| 98 | return nil, fmt.Errorf("while writing the hash image: %w", err) |
| 99 | } |
| 100 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 101 | // Return an encoder-generated verity mapping table, containing the salt and |
| 102 | // the root hash. First, calculate the starting hash block by dividing the |
| 103 | // data image size by the encoder data block size. |
| 104 | hashStart := ds.Size() / int64(bs) |
| 105 | mt, err := v.MappingTable(dataImagePath, outputImagePath, hashStart) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 106 | if err != nil { |
| 107 | return nil, fmt.Errorf("while querying for the mapping table: %w", err) |
| 108 | } |
| 109 | return mt, nil |
| 110 | } |
| 111 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 112 | var ( |
| 113 | input = flag.String("input", "", "input disk image (required)") |
| 114 | output = flag.String("output", "", "output disk image with Verity metadata appended (required)") |
| 115 | dataDeviceAlias = flag.String("data_alias", "", "data device alias used in the mapping table") |
| 116 | hashDeviceAlias = flag.String("hash_alias", "", "hash device alias used in the mapping table") |
| 117 | table = flag.String("table", "", "a file the mapping table will be saved to; disables stdout") |
| 118 | ) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 119 | |
| 120 | func main() { |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 121 | flag.Parse() |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 122 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 123 | // Ensure that required parameters were provided before continuing. |
| 124 | if *input == "" { |
| 125 | log.Fatalf("-input must be set.") |
| 126 | } |
| 127 | if *output == "" { |
| 128 | log.Fatalf("-output must be set.") |
| 129 | } |
| 130 | |
| 131 | // Build the image. |
| 132 | mt, err := createImage(*input, *output, false) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 133 | if err != nil { |
| 134 | log.Fatal(err) |
| 135 | } |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 136 | |
| 137 | // Patch the device names, if alternatives were provided. |
| 138 | if *dataDeviceAlias != "" { |
| 139 | mt.DataDevicePath = *dataDeviceAlias |
| 140 | } |
| 141 | if *hashDeviceAlias != "" { |
| 142 | mt.HashDevicePath = *hashDeviceAlias |
| 143 | } |
| 144 | |
| 145 | // Print a DeviceMapper target table, or save it to a file, if the table |
| 146 | // parameter was specified. |
| 147 | if *table != "" { |
| 148 | if err := os.WriteFile(*table, []byte(mt.String()), 0644); err != nil { |
| 149 | log.Fatal(err) |
| 150 | } |
| 151 | } else { |
| 152 | fmt.Println(mt) |
| 153 | } |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 154 | } |