Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017, Google Inc. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 15 | // inject_hash parses an archive containing a file object file. It finds a FIPS |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 16 | // module inside that object, calculates its hash and replaces the default hash |
| 17 | // value in the object with the calculated value. |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "bytes" |
| 22 | "crypto/hmac" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 23 | "crypto/sha512" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 24 | "debug/elf" |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 25 | "encoding/binary" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 26 | "errors" |
| 27 | "flag" |
| 28 | "fmt" |
| 29 | "io" |
| 30 | "io/ioutil" |
| 31 | "os" |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 32 | "strings" |
Robert Sloan | f573be7 | 2018-09-17 15:29:11 -0700 | [diff] [blame] | 33 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 34 | "boringssl.googlesource.com/boringssl/util/ar" |
Robert Sloan | f573be7 | 2018-09-17 15:29:11 -0700 | [diff] [blame] | 35 | "boringssl.googlesource.com/boringssl/util/fipstools/fipscommon" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 38 | func do(outPath, oInput string, arInput string) error { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 39 | var objectBytes []byte |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 40 | var isStatic bool |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 41 | if len(arInput) > 0 { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 42 | isStatic = true |
| 43 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 44 | if len(oInput) > 0 { |
| 45 | return fmt.Errorf("-in-archive and -in-object are mutually exclusive") |
| 46 | } |
| 47 | |
| 48 | arFile, err := os.Open(arInput) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | defer arFile.Close() |
| 53 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 54 | ar, err := ar.ParseAR(arFile) |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | if len(ar) != 1 { |
| 60 | return fmt.Errorf("expected one file in archive, but found %d", len(ar)) |
| 61 | } |
| 62 | |
| 63 | for _, contents := range ar { |
| 64 | objectBytes = contents |
| 65 | } |
| 66 | } else if len(oInput) > 0 { |
| 67 | var err error |
| 68 | if objectBytes, err = ioutil.ReadFile(oInput); err != nil { |
| 69 | return err |
| 70 | } |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 71 | isStatic = strings.HasSuffix(oInput, ".o") |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 72 | } else { |
| 73 | return fmt.Errorf("exactly one of -in-archive or -in-object is required") |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | object, err := elf.NewFile(bytes.NewReader(objectBytes)) |
| 77 | if err != nil { |
| 78 | return errors.New("failed to parse object: " + err.Error()) |
| 79 | } |
| 80 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 81 | // Find the .text and, optionally, .data sections. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 82 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 83 | var textSection, rodataSection *elf.Section |
| 84 | var textSectionIndex, rodataSectionIndex elf.SectionIndex |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 85 | for i, section := range object.Sections { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 86 | switch section.Name { |
| 87 | case ".text": |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 88 | textSectionIndex = elf.SectionIndex(i) |
| 89 | textSection = section |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 90 | case ".rodata": |
| 91 | rodataSectionIndex = elf.SectionIndex(i) |
| 92 | rodataSection = section |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | if textSection == nil { |
| 97 | return errors.New("failed to find .text section in object") |
| 98 | } |
| 99 | |
| 100 | // Find the starting and ending symbols for the module. |
| 101 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 102 | var textStart, textEnd, rodataStart, rodataEnd *uint64 |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 103 | |
| 104 | symbols, err := object.Symbols() |
| 105 | if err != nil { |
| 106 | return errors.New("failed to parse symbols: " + err.Error()) |
| 107 | } |
| 108 | |
| 109 | for _, symbol := range symbols { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 110 | var base uint64 |
| 111 | switch symbol.Section { |
| 112 | case textSectionIndex: |
| 113 | base = textSection.Addr |
| 114 | case rodataSectionIndex: |
| 115 | if rodataSection == nil { |
| 116 | continue |
| 117 | } |
| 118 | base = rodataSection.Addr |
| 119 | default: |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 120 | continue |
| 121 | } |
| 122 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 123 | if isStatic { |
| 124 | // Static objects appear to have different semantics about whether symbol |
| 125 | // values are relative to their section or not. |
| 126 | base = 0 |
| 127 | } else if symbol.Value < base { |
| 128 | return fmt.Errorf("symbol %q at %x, which is below base of %x", symbol.Name, symbol.Value, base) |
| 129 | } |
| 130 | |
| 131 | value := symbol.Value - base |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 132 | switch symbol.Name { |
| 133 | case "BORINGSSL_bcm_text_start": |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 134 | if textStart != nil { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 135 | return errors.New("duplicate start symbol found") |
| 136 | } |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 137 | textStart = &value |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 138 | case "BORINGSSL_bcm_text_end": |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 139 | if textEnd != nil { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 140 | return errors.New("duplicate end symbol found") |
| 141 | } |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 142 | textEnd = &value |
| 143 | case "BORINGSSL_bcm_rodata_start": |
| 144 | if rodataStart != nil { |
| 145 | return errors.New("duplicate rodata start symbol found") |
| 146 | } |
| 147 | rodataStart = &value |
| 148 | case "BORINGSSL_bcm_rodata_end": |
| 149 | if rodataEnd != nil { |
| 150 | return errors.New("duplicate rodata end symbol found") |
| 151 | } |
| 152 | rodataEnd = &value |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 153 | default: |
| 154 | continue |
| 155 | } |
| 156 | } |
| 157 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 158 | if textStart == nil || textEnd == nil { |
| 159 | return errors.New("could not find .text module boundaries in object") |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 162 | if (rodataStart == nil) != (rodataSection == nil) { |
| 163 | return errors.New("rodata start marker inconsistent with rodata section presence") |
| 164 | } |
| 165 | |
| 166 | if (rodataStart != nil) != (rodataEnd != nil) { |
| 167 | return errors.New("rodata marker presence inconsistent") |
| 168 | } |
| 169 | |
| 170 | if max := textSection.Size; *textStart > max || *textStart > *textEnd || *textEnd > max { |
| 171 | return fmt.Errorf("invalid module .text boundaries: start: %x, end: %x, max: %x", *textStart, *textEnd, max) |
| 172 | } |
| 173 | |
| 174 | if rodataSection != nil { |
| 175 | if max := rodataSection.Size; *rodataStart > max || *rodataStart > *rodataEnd || *rodataEnd > max { |
| 176 | return fmt.Errorf("invalid module .rodata boundaries: start: %x, end: %x, max: %x", *rodataStart, *rodataEnd, max) |
| 177 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Extract the module from the .text section and hash it. |
| 181 | |
| 182 | text := textSection.Open() |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 183 | if _, err := text.Seek(int64(*textStart), 0); err != nil { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 184 | return errors.New("failed to seek to module start in .text: " + err.Error()) |
| 185 | } |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 186 | moduleText := make([]byte, *textEnd-*textStart) |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 187 | if _, err := io.ReadFull(text, moduleText); err != nil { |
| 188 | return errors.New("failed to read .text: " + err.Error()) |
| 189 | } |
| 190 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 191 | // Maybe extract the module's read-only data too |
| 192 | var moduleROData []byte |
| 193 | if rodataSection != nil { |
| 194 | rodata := rodataSection.Open() |
| 195 | if _, err := rodata.Seek(int64(*rodataStart), 0); err != nil { |
| 196 | return errors.New("failed to seek to module start in .rodata: " + err.Error()) |
| 197 | } |
| 198 | moduleROData = make([]byte, *rodataEnd-*rodataStart) |
| 199 | if _, err := io.ReadFull(rodata, moduleROData); err != nil { |
| 200 | return errors.New("failed to read .rodata: " + err.Error()) |
| 201 | } |
| 202 | } |
| 203 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 204 | var zeroKey [64]byte |
| 205 | mac := hmac.New(sha512.New, zeroKey[:]) |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 206 | |
| 207 | if moduleROData != nil { |
| 208 | var lengthBytes [8]byte |
| 209 | binary.LittleEndian.PutUint64(lengthBytes[:], uint64(len(moduleText))) |
| 210 | mac.Write(lengthBytes[:]) |
| 211 | mac.Write(moduleText) |
| 212 | |
| 213 | binary.LittleEndian.PutUint64(lengthBytes[:], uint64(len(moduleROData))) |
| 214 | mac.Write(lengthBytes[:]) |
| 215 | mac.Write(moduleROData) |
| 216 | } else { |
| 217 | mac.Write(moduleText) |
| 218 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 219 | calculated := mac.Sum(nil) |
| 220 | |
| 221 | // Replace the default hash value in the object with the calculated |
| 222 | // value and write it out. |
| 223 | |
Robert Sloan | f573be7 | 2018-09-17 15:29:11 -0700 | [diff] [blame] | 224 | offset := bytes.Index(objectBytes, fipscommon.UninitHashValue[:]) |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 225 | if offset < 0 { |
| 226 | return errors.New("did not find uninitialised hash value in object file") |
| 227 | } |
| 228 | |
Robert Sloan | f573be7 | 2018-09-17 15:29:11 -0700 | [diff] [blame] | 229 | if bytes.Index(objectBytes[offset+1:], fipscommon.UninitHashValue[:]) >= 0 { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 230 | return errors.New("found two occurrences of uninitialised hash value in object file") |
| 231 | } |
| 232 | |
| 233 | copy(objectBytes[offset:], calculated) |
| 234 | |
| 235 | return ioutil.WriteFile(outPath, objectBytes, 0644) |
| 236 | } |
| 237 | |
| 238 | func main() { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 239 | arInput := flag.String("in-archive", "", "Path to a .a file") |
| 240 | oInput := flag.String("in-object", "", "Path to a .o file") |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 241 | outPath := flag.String("o", "", "Path to output object") |
| 242 | |
| 243 | flag.Parse() |
| 244 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 245 | if err := do(*outPath, *oInput, *arInput); err != nil { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 246 | fmt.Fprintf(os.Stderr, "%s\n", err) |
| 247 | os.Exit(1) |
| 248 | } |
| 249 | } |