| 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 ( |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 14 | "flag" |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 15 | "fmt" |
| 16 | "io" |
| 17 | "log" |
| 18 | "os" |
| 19 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 20 | "source.monogon.dev/osbase/verity" |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 21 | ) |
| 22 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 23 | // createImage creates a dm-verity target image by combining the input image |
| 24 | // with Verity metadata. Contents of the data image are copied to the output |
| 25 | // image. Then, the same contents are verity-encoded and appended to the |
| 26 | // output image. The verity superblock is written only if wsb is true. It |
| 27 | // returns either a dm-verity target table, or an error. |
| 28 | func createImage(dataImagePath, outputImagePath string, wsb bool) (*verity.MappingTable, error) { |
| 29 | // Hardcode both the data block size and the hash block size as 4096 bytes. |
| 30 | bs := uint32(4096) |
| 31 | |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 32 | // Open the data image for reading. |
| 33 | dataImage, err := os.Open(dataImagePath) |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("while opening the data image: %w", err) |
| 36 | } |
| 37 | defer dataImage.Close() |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 38 | |
| 39 | // Check that the data image is well-formed. |
| 40 | ds, err := dataImage.Stat() |
| 41 | if err != nil { |
| 42 | return nil, fmt.Errorf("while stat-ing the data image: %w", err) |
| 43 | } |
| 44 | if !ds.Mode().IsRegular() { |
| 45 | return nil, fmt.Errorf("the data image must be a regular file") |
| 46 | } |
| 47 | if ds.Size()%int64(bs) != 0 { |
| Tim Windelschmidt | 73e9882 | 2024-04-18 23:13:49 +0200 | [diff] [blame] | 48 | 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] | 49 | } |
| 50 | |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 51 | // Create an empty hash image file. |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 52 | 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] | 53 | if err != nil { |
| 54 | return nil, fmt.Errorf("while opening the hash image for writing: %w", err) |
| 55 | } |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 56 | defer outputImage.Close() |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 57 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 58 | // Copy the input data into the output file, then rewind dataImage to be read |
| 59 | // again by the Verity encoder. |
| 60 | _, err = io.Copy(outputImage, dataImage) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | _, err = dataImage.Seek(0, os.SEEK_SET) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | // Write outputImage contents. Start with initializing a verity encoder, |
| 70 | // seting outputImage as its output. |
| 71 | v, err := verity.NewEncoder(outputImage, bs, bs, wsb) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("while initializing a verity encoder: %w", err) |
| 74 | } |
| 75 | // Hash the contents of dataImage, block by block. |
| 76 | _, err = io.Copy(v, dataImage) |
| 77 | if err != nil { |
| 78 | return nil, fmt.Errorf("while reading the data image: %w", err) |
| 79 | } |
| 80 | // The resulting hash tree won't be written until Close is called. |
| 81 | err = v.Close() |
| 82 | if err != nil { |
| 83 | return nil, fmt.Errorf("while writing the hash image: %w", err) |
| 84 | } |
| 85 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 86 | // Return an encoder-generated verity mapping table, containing the salt and |
| 87 | // the root hash. First, calculate the starting hash block by dividing the |
| 88 | // data image size by the encoder data block size. |
| 89 | hashStart := ds.Size() / int64(bs) |
| 90 | mt, err := v.MappingTable(dataImagePath, outputImagePath, hashStart) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 91 | if err != nil { |
| 92 | return nil, fmt.Errorf("while querying for the mapping table: %w", err) |
| 93 | } |
| 94 | return mt, nil |
| 95 | } |
| 96 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 97 | var ( |
| 98 | input = flag.String("input", "", "input disk image (required)") |
| 99 | output = flag.String("output", "", "output disk image with Verity metadata appended (required)") |
| 100 | dataDeviceAlias = flag.String("data_alias", "", "data device alias used in the mapping table") |
| 101 | hashDeviceAlias = flag.String("hash_alias", "", "hash device alias used in the mapping table") |
| 102 | table = flag.String("table", "", "a file the mapping table will be saved to; disables stdout") |
| 103 | ) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 104 | |
| 105 | func main() { |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 106 | flag.Parse() |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 107 | |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 108 | // Ensure that required parameters were provided before continuing. |
| 109 | if *input == "" { |
| 110 | log.Fatalf("-input must be set.") |
| 111 | } |
| 112 | if *output == "" { |
| 113 | log.Fatalf("-output must be set.") |
| 114 | } |
| 115 | |
| 116 | // Build the image. |
| 117 | mt, err := createImage(*input, *output, false) |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 118 | if err != nil { |
| 119 | log.Fatal(err) |
| 120 | } |
| Mateusz Zalega | ba1da9d | 2022-01-25 19:12:02 +0100 | [diff] [blame] | 121 | |
| 122 | // Patch the device names, if alternatives were provided. |
| 123 | if *dataDeviceAlias != "" { |
| 124 | mt.DataDevicePath = *dataDeviceAlias |
| 125 | } |
| 126 | if *hashDeviceAlias != "" { |
| 127 | mt.HashDevicePath = *hashDeviceAlias |
| 128 | } |
| 129 | |
| 130 | // Print a DeviceMapper target table, or save it to a file, if the table |
| 131 | // parameter was specified. |
| 132 | if *table != "" { |
| 133 | if err := os.WriteFile(*table, []byte(mt.String()), 0644); err != nil { |
| 134 | log.Fatal(err) |
| 135 | } |
| 136 | } else { |
| 137 | fmt.Println(mt) |
| 138 | } |
| Mateusz Zalega | 356b896 | 2021-08-10 17:27:15 +0200 | [diff] [blame] | 139 | } |