blob: 6bab1434fa800f3b67b567466b455680a4d0b1cf [file] [log] [blame]
Robert Sloan572a4e22017-04-17 10:52:19 -07001// 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 Sloanf068def2018-10-10 18:45:40 -070015// inject_hash parses an archive containing a file object file. It finds a FIPS
Robert Sloan572a4e22017-04-17 10:52:19 -070016// module inside that object, calculates its hash and replaces the default hash
17// value in the object with the calculated value.
18package main
19
20import (
21 "bytes"
22 "crypto/hmac"
Robert Sloan8ff03552017-06-14 12:40:58 -070023 "crypto/sha512"
Robert Sloan572a4e22017-04-17 10:52:19 -070024 "debug/elf"
Pete Bentley0c61efe2019-08-13 09:32:23 +010025 "encoding/binary"
Robert Sloan572a4e22017-04-17 10:52:19 -070026 "errors"
27 "flag"
28 "fmt"
29 "io"
30 "io/ioutil"
31 "os"
Pete Bentley0c61efe2019-08-13 09:32:23 +010032 "strings"
Robert Sloanf573be72018-09-17 15:29:11 -070033
Robert Sloanf068def2018-10-10 18:45:40 -070034 "boringssl.googlesource.com/boringssl/util/ar"
Robert Sloanf573be72018-09-17 15:29:11 -070035 "boringssl.googlesource.com/boringssl/util/fipstools/fipscommon"
Robert Sloan572a4e22017-04-17 10:52:19 -070036)
37
Robert Sloan9254e682017-04-24 09:42:06 -070038func do(outPath, oInput string, arInput string) error {
Robert Sloan572a4e22017-04-17 10:52:19 -070039 var objectBytes []byte
Pete Bentley0c61efe2019-08-13 09:32:23 +010040 var isStatic bool
Robert Sloan9254e682017-04-24 09:42:06 -070041 if len(arInput) > 0 {
Pete Bentley0c61efe2019-08-13 09:32:23 +010042 isStatic = true
43
Robert Sloan9254e682017-04-24 09:42:06 -070044 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 Sloanf068def2018-10-10 18:45:40 -070054 ar, err := ar.ParseAR(arFile)
Robert Sloan9254e682017-04-24 09:42:06 -070055 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 Bentley0c61efe2019-08-13 09:32:23 +010071 isStatic = strings.HasSuffix(oInput, ".o")
Robert Sloan9254e682017-04-24 09:42:06 -070072 } else {
73 return fmt.Errorf("exactly one of -in-archive or -in-object is required")
Robert Sloan572a4e22017-04-17 10:52:19 -070074 }
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 Bentley0c61efe2019-08-13 09:32:23 +010081 // Find the .text and, optionally, .data sections.
Robert Sloan572a4e22017-04-17 10:52:19 -070082
Pete Bentley0c61efe2019-08-13 09:32:23 +010083 var textSection, rodataSection *elf.Section
84 var textSectionIndex, rodataSectionIndex elf.SectionIndex
Robert Sloan572a4e22017-04-17 10:52:19 -070085 for i, section := range object.Sections {
Pete Bentley0c61efe2019-08-13 09:32:23 +010086 switch section.Name {
87 case ".text":
Robert Sloan572a4e22017-04-17 10:52:19 -070088 textSectionIndex = elf.SectionIndex(i)
89 textSection = section
Pete Bentley0c61efe2019-08-13 09:32:23 +010090 case ".rodata":
91 rodataSectionIndex = elf.SectionIndex(i)
92 rodataSection = section
Robert Sloan572a4e22017-04-17 10:52:19 -070093 }
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 Bentley0c61efe2019-08-13 09:32:23 +0100102 var textStart, textEnd, rodataStart, rodataEnd *uint64
Robert Sloan572a4e22017-04-17 10:52:19 -0700103
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 Bentley0c61efe2019-08-13 09:32:23 +0100110 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 Sloan572a4e22017-04-17 10:52:19 -0700120 continue
121 }
122
Pete Bentley0c61efe2019-08-13 09:32:23 +0100123 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 Sloan572a4e22017-04-17 10:52:19 -0700132 switch symbol.Name {
133 case "BORINGSSL_bcm_text_start":
Pete Bentley0c61efe2019-08-13 09:32:23 +0100134 if textStart != nil {
Robert Sloan572a4e22017-04-17 10:52:19 -0700135 return errors.New("duplicate start symbol found")
136 }
Pete Bentley0c61efe2019-08-13 09:32:23 +0100137 textStart = &value
Robert Sloan572a4e22017-04-17 10:52:19 -0700138 case "BORINGSSL_bcm_text_end":
Pete Bentley0c61efe2019-08-13 09:32:23 +0100139 if textEnd != nil {
Robert Sloan572a4e22017-04-17 10:52:19 -0700140 return errors.New("duplicate end symbol found")
141 }
Pete Bentley0c61efe2019-08-13 09:32:23 +0100142 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 Sloan572a4e22017-04-17 10:52:19 -0700153 default:
154 continue
155 }
156 }
157
Pete Bentley0c61efe2019-08-13 09:32:23 +0100158 if textStart == nil || textEnd == nil {
159 return errors.New("could not find .text module boundaries in object")
Robert Sloan572a4e22017-04-17 10:52:19 -0700160 }
161
Pete Bentley0c61efe2019-08-13 09:32:23 +0100162 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 Sloan572a4e22017-04-17 10:52:19 -0700178 }
179
180 // Extract the module from the .text section and hash it.
181
182 text := textSection.Open()
Pete Bentley0c61efe2019-08-13 09:32:23 +0100183 if _, err := text.Seek(int64(*textStart), 0); err != nil {
Robert Sloan572a4e22017-04-17 10:52:19 -0700184 return errors.New("failed to seek to module start in .text: " + err.Error())
185 }
Pete Bentley0c61efe2019-08-13 09:32:23 +0100186 moduleText := make([]byte, *textEnd-*textStart)
Robert Sloan572a4e22017-04-17 10:52:19 -0700187 if _, err := io.ReadFull(text, moduleText); err != nil {
188 return errors.New("failed to read .text: " + err.Error())
189 }
190
Pete Bentley0c61efe2019-08-13 09:32:23 +0100191 // 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 Sloan8ff03552017-06-14 12:40:58 -0700204 var zeroKey [64]byte
205 mac := hmac.New(sha512.New, zeroKey[:])
Pete Bentley0c61efe2019-08-13 09:32:23 +0100206
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 Sloan572a4e22017-04-17 10:52:19 -0700219 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 Sloanf573be72018-09-17 15:29:11 -0700224 offset := bytes.Index(objectBytes, fipscommon.UninitHashValue[:])
Robert Sloan572a4e22017-04-17 10:52:19 -0700225 if offset < 0 {
226 return errors.New("did not find uninitialised hash value in object file")
227 }
228
Robert Sloanf573be72018-09-17 15:29:11 -0700229 if bytes.Index(objectBytes[offset+1:], fipscommon.UninitHashValue[:]) >= 0 {
Robert Sloan572a4e22017-04-17 10:52:19 -0700230 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
238func main() {
Robert Sloan9254e682017-04-24 09:42:06 -0700239 arInput := flag.String("in-archive", "", "Path to a .a file")
240 oInput := flag.String("in-object", "", "Path to a .o file")
Robert Sloan572a4e22017-04-17 10:52:19 -0700241 outPath := flag.String("o", "", "Path to output object")
242
243 flag.Parse()
244
Robert Sloan9254e682017-04-24 09:42:06 -0700245 if err := do(*outPath, *oInput, *arInput); err != nil {
Robert Sloan572a4e22017-04-17 10:52:19 -0700246 fmt.Fprintf(os.Stderr, "%s\n", err)
247 os.Exit(1)
248 }
249}