git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / Documentation / technical / hash-function-transition.adoc
| 1 | Git hash function transition |
| 2 | ============================ |
| 3 | |
| 4 | Objective |
| 5 | --------- |
| 6 | Migrate Git from SHA-1 to a stronger hash function. |
| 7 | |
| 8 | Background |
| 9 | ---------- |
| 10 | At its core, the Git version control system is a content addressable |
| 11 | filesystem. It uses the SHA-1 hash function to name content. For |
| 12 | example, files, directories, and revisions are referred to by hash |
| 13 | values unlike in other traditional version control systems where files |
| 14 | or versions are referred to via sequential numbers. The use of a hash |
| 15 | function to address its content delivers a few advantages: |
| 16 | |
| 17 | * Integrity checking is easy. Bit flips, for example, are easily |
| 18 | detected, as the hash of corrupted content does not match its name. |
| 19 | * Lookup of objects is fast. |
| 20 | |
| 21 | Using a cryptographically secure hash function brings additional |
| 22 | advantages: |
| 23 | |
| 24 | * Object names can be signed and third parties can trust the hash to |
| 25 | address the signed object and all objects it references. |
| 26 | * Communication using Git protocol and out of band communication |
| 27 | methods have a short reliable string that can be used to reliably |
| 28 | address stored content. |
| 29 | |
| 30 | Over time some flaws in SHA-1 have been discovered by security |
| 31 | researchers. On 23 February 2017 the SHAttered attack |
| 32 | (https://web.archive.org/web/20260207211148/https://shattered.io/) |
| 33 | demonstrated a practical SHA-1 hash collision. |
| 34 | |
| 35 | Git v2.13.0 and later subsequently moved to a hardened SHA-1 |
| 36 | implementation by default, which isn't vulnerable to the SHAttered |
| 37 | attack, but SHA-1 is still weak. |
| 38 | |
| 39 | Thus it's considered prudent to move past any variant of SHA-1 |
| 40 | to a new hash. There's no guarantee that future attacks on SHA-1 won't |
| 41 | be published in the future, and those attacks may not have viable |
| 42 | mitigations. |
| 43 | |
| 44 | If SHA-1 and its variants were to be truly broken, Git's hash function |
| 45 | could not be considered cryptographically secure any more. This would |
| 46 | impact the communication of hash values because we could not trust |
| 47 | that a given hash value represented the known good version of content |
| 48 | that the speaker intended. |
| 49 | |
| 50 | SHA-1 still possesses the other properties such as fast object lookup |
| 51 | and safe error checking, but other hash functions are equally suitable |
| 52 | that are believed to be cryptographically secure. |
| 53 | |
| 54 | Choice of Hash |
| 55 | -------------- |
| 56 | The hash to replace the hardened SHA-1 should be stronger than SHA-1 |
| 57 | was: we would like it to be trustworthy and useful in practice for at |
| 58 | least 10 years. |
| 59 | |
| 60 | Some other relevant properties: |
| 61 | |
| 62 | 1. A 256-bit hash (long enough to match common security practice; not |
| 63 | excessively long to hurt performance and disk usage). |
| 64 | |
| 65 | 2. High quality implementations should be widely available (e.g., in |
| 66 | OpenSSL and Apple CommonCrypto). |
| 67 | |
| 68 | 3. The hash function's properties should match Git's needs (e.g. Git |
| 69 | requires collision and 2nd preimage resistance and does not require |
| 70 | length extension resistance). |
| 71 | |
| 72 | 4. As a tiebreaker, the hash should be fast to compute (fortunately |
| 73 | many contenders are faster than SHA-1). |
| 74 | |
| 75 | There were several contenders for a successor hash to SHA-1, including |
| 76 | SHA-256, SHA-512/256, SHA-256x16, K12, and BLAKE2bp-256. |
| 77 | |
| 78 | In late 2018 the project picked SHA-256 as its successor hash. |
| 79 | |
| 80 | See 0ed8d8da374 (doc hash-function-transition: pick SHA-256 as |
| 81 | NewHash, 2018-08-04) and numerous mailing list threads at the time, |
| 82 | particularly the one starting at |
| 83 | https://lore.kernel.org/git/20180609224913.GC38834@genre.crustytoothpaste.net/ |
| 84 | for more information. |
| 85 | |
| 86 | Goals |
| 87 | ----- |
| 88 | 1. The transition to SHA-256 can be done one local repository at a time. |
| 89 | a. Requiring no action by any other party. |
| 90 | b. A SHA-256 repository can communicate with SHA-1 Git servers |
| 91 | (push/fetch). |
| 92 | c. Users can use SHA-1 and SHA-256 identifiers for objects |
| 93 | interchangeably (see "Object names on the command line", below). |
| 94 | d. New signed objects make use of a stronger hash function than |
| 95 | SHA-1 for their security guarantees. |
| 96 | 2. Allow a complete transition away from SHA-1. |
| 97 | a. Local metadata for SHA-1 compatibility can be removed from a |
| 98 | repository if compatibility with SHA-1 is no longer needed. |
| 99 | 3. Maintainability throughout the process. |
| 100 | a. The object format is kept simple and consistent. |
| 101 | b. Creation of a generalized repository conversion tool. |
| 102 | |
| 103 | Non-Goals |
| 104 | --------- |
| 105 | 1. Add SHA-256 support to Git protocol. This is valuable and the |
| 106 | logical next step but it is out of scope for this initial design. |
| 107 | 2. Transparently improving the security of existing SHA-1 signed |
| 108 | objects. |
| 109 | 3. Intermixing objects using multiple hash functions in a single |
| 110 | repository. |
| 111 | 4. Taking the opportunity to fix other bugs in Git's formats and |
| 112 | protocols. |
| 113 | 5. Shallow clones and fetches into a SHA-256 repository. (This will |
| 114 | change when we add SHA-256 support to Git protocol.) |
| 115 | 6. Skip fetching some submodules of a project into a SHA-256 |
| 116 | repository. (This also depends on SHA-256 support in Git |
| 117 | protocol.) |
| 118 | |
| 119 | Overview |
| 120 | -------- |
| 121 | We introduce a new repository format extension. Repositories with this |
| 122 | extension enabled use SHA-256 instead of SHA-1 to name their objects. |
| 123 | This affects both object names and object content -- both the names |
| 124 | of objects and all references to other objects within an object are |
| 125 | switched to the new hash function. |
| 126 | |
| 127 | SHA-256 repositories cannot be read by older versions of Git. |
| 128 | |
| 129 | Alongside the packfile, a SHA-256 repository stores a bidirectional |
| 130 | mapping between SHA-256 and SHA-1 object names. The mapping is generated |
| 131 | locally and can be verified using "git fsck". Object lookups use this |
| 132 | mapping to allow naming objects using either their SHA-1 and SHA-256 names |
| 133 | interchangeably. |
| 134 | |
| 135 | "git cat-file" and "git hash-object" gain options to display an object |
| 136 | in its SHA-1 form and write an object given its SHA-1 form. This |
| 137 | requires all objects referenced by that object to be present in the |
| 138 | object database so that they can be named using the appropriate name |
| 139 | (using the bidirectional hash mapping). |
| 140 | |
| 141 | Fetches from a SHA-1 based server convert the fetched objects into |
| 142 | SHA-256 form and record the mapping in the bidirectional mapping table |
| 143 | (see below for details). Pushes to a SHA-1 based server convert the |
| 144 | objects being pushed into SHA-1 form so the server does not have to be |
| 145 | aware of the hash function the client is using. |
| 146 | |
| 147 | Detailed Design |
| 148 | --------------- |
| 149 | Repository format extension |
| 150 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 151 | A SHA-256 repository uses repository format version `1` (see |
| 152 | linkgit:gitrepository-layout[5]) with `extensions.objectFormat` and |
| 153 | `extensions.compatObjectFormat` (see linkgit:git-config[1]) set to: |
| 154 | |
| 155 | [core] |
| 156 | repositoryFormatVersion = 1 |
| 157 | [extensions] |
| 158 | objectFormat = sha256 |
| 159 | compatObjectFormat = sha1 |
| 160 | |
| 161 | The combination of setting `core.repositoryFormatVersion=1` and |
| 162 | populating `extensions.*` ensures that all versions of Git later than |
| 163 | `v0.99.9l` will die instead of trying to operate on the SHA-256 |
| 164 | repository, instead producing an error message. |
| 165 | |
| 166 | # Between v0.99.9l and v2.7.0 |
| 167 | $ git status |
| 168 | fatal: Expected git repo version <= 0, found 1 |
| 169 | # After v2.7.0 |
| 170 | $ git status |
| 171 | fatal: unknown repository extensions found: |
| 172 | objectformat |
| 173 | compatobjectformat |
| 174 | |
| 175 | See the "Transition plan" section below for more details on these |
| 176 | repository extensions. |
| 177 | |
| 178 | Object names |
| 179 | ~~~~~~~~~~~~ |
| 180 | Objects can be named by their 40 hexadecimal digit SHA-1 name or 64 |
| 181 | hexadecimal digit SHA-256 name, plus names derived from those (see |
| 182 | gitrevisions(7)). |
| 183 | |
| 184 | The SHA-1 name of an object is the SHA-1 of the concatenation of its |
| 185 | type, length, a nul byte, and the object's SHA-1 content. This is the |
| 186 | traditional <sha1> used in Git to name objects. |
| 187 | |
| 188 | The SHA-256 name of an object is the SHA-256 of the concatenation of its |
| 189 | type, length, a nul byte, and the object's SHA-256 content. |
| 190 | |
| 191 | Object format |
| 192 | ~~~~~~~~~~~~~ |
| 193 | The content as a byte sequence of a tag, commit, or tree object named |
| 194 | by SHA-1 and SHA-256 differ because an object named by SHA-256 name refers to |
| 195 | other objects by their SHA-256 names and an object named by SHA-1 name |
| 196 | refers to other objects by their SHA-1 names. |
| 197 | |
| 198 | The SHA-256 content of an object is the same as its SHA-1 content, except |
| 199 | that objects referenced by the object are named using their SHA-256 names |
| 200 | instead of SHA-1 names. Because a blob object does not refer to any |
| 201 | other object, its SHA-1 content and SHA-256 content are the same. |
| 202 | |
| 203 | The format allows round-trip conversion between SHA-256 content and |
| 204 | SHA-1 content. |
| 205 | |
| 206 | Object storage |
| 207 | ~~~~~~~~~~~~~~ |
| 208 | Loose objects use zlib compression and packed objects use the packed |
| 209 | format described in linkgit:gitformat-pack[5], just like |
| 210 | today. The content that is compressed and stored uses SHA-256 content |
| 211 | instead of SHA-1 content. |
| 212 | |
| 213 | Pack index |
| 214 | ~~~~~~~~~~ |
| 215 | Pack index (.idx) files use a new v3 format that supports multiple |
| 216 | hash functions. They have the following format (all integers are in |
| 217 | network byte order): |
| 218 | |
| 219 | - A header appears at the beginning and consists of the following: |
| 220 | * The 4-byte pack index signature: '\377t0c' |
| 221 | * 4-byte version number: 3 |
| 222 | * 4-byte length of the header section, including the signature and |
| 223 | version number |
| 224 | * 4-byte number of objects contained in the pack |
| 225 | * 4-byte number of object formats in this pack index: 2 |
| 226 | * For each object format: |
| 227 | ** 4-byte format identifier (e.g., 'sha1' for SHA-1) |
| 228 | ** 4-byte length in bytes of shortened object names. This is the |
| 229 | shortest possible length needed to make names in the shortened |
| 230 | object name table unambiguous. |
| 231 | ** 8-byte integer, recording where tables relating to this format |
| 232 | are stored in this index file, as an offset from the beginning. |
| 233 | * 8-byte offset to the trailer from the beginning of this file. |
| 234 | * Zero or more additional key/value pairs (4-byte key, 4-byte |
| 235 | value). Only one key is supported: 'PSRC'. See the "Loose objects |
| 236 | and unreachable objects" section for supported values and how this |
| 237 | is used. All other keys are reserved. Readers must ignore |
| 238 | unrecognized keys. |
| 239 | - Zero or more NUL bytes. This can optionally be used to improve the |
| 240 | alignment of the full object name table below. |
| 241 | - Tables for the first object format: |
| 242 | * A sorted table of shortened object names. These are prefixes of |
| 243 | the names of all objects in this pack file, packed together |
| 244 | without offset values to reduce the cache footprint of the binary |
| 245 | search for a specific object name. |
| 246 | |
| 247 | * A table of full object names in pack order. This allows resolving |
| 248 | a reference to "the nth object in the pack file" (from a |
| 249 | reachability bitmap or from the next table of another object |
| 250 | format) to its object name. |
| 251 | |
| 252 | * A table of 4-byte values mapping object name order to pack order. |
| 253 | For an object in the table of sorted shortened object names, the |
| 254 | value at the corresponding index in this table is the index in the |
| 255 | previous table for that same object. |
| 256 | This can be used to look up the object in reachability bitmaps or |
| 257 | to look up its name in another object format. |
| 258 | |
| 259 | * A table of 4-byte CRC32 values of the packed object data, in the |
| 260 | order that the objects appear in the pack file. This is to allow |
| 261 | compressed data to be copied directly from pack to pack during |
| 262 | repacking without undetected data corruption. |
| 263 | |
| 264 | * A table of 4-byte offset values. The index of this table in pack order |
| 265 | indicates where that object can be found in the pack file. These are |
| 266 | usually 31-bit pack file offsets, but large offsets are encoded as |
| 267 | an index into the next table with the most significant bit set. |
| 268 | |
| 269 | * A table of 8-byte offset entries (empty for pack files less than |
| 270 | 2 GiB). Pack files are organized with heavily used objects toward |
| 271 | the front, so most object references should not need to refer to |
| 272 | this table. |
| 273 | - Zero or more NUL bytes. |
| 274 | - Tables for the second object format, with the same layout as above, |
| 275 | up to and not including the table of CRC32 values. |
| 276 | - Zero or more NUL bytes. |
| 277 | - The trailer consists of the following: |
| 278 | * A copy of the full main hash checksum at the end of the |
| 279 | corresponding packfile. |
| 280 | |
| 281 | * Full main hash checksum of all of the above. |
| 282 | |
| 283 | The "full main hash" is a full-length hash of the main (not compatibility) |
| 284 | algorithm in the repository. Thus, if the main algorithm is SHA-256, this is |
| 285 | a 32-byte SHA-256 hash and for SHA-1, it's a 20-byte SHA-1 hash. |
| 286 | |
| 287 | Loose object index |
| 288 | ~~~~~~~~~~~~~~~~~~ |
| 289 | A new file $GIT_OBJECT_DIR/loose-object-idx contains information about |
| 290 | all loose objects. Its format is |
| 291 | |
| 292 | # loose-object-idx |
| 293 | (sha256-name SP sha1-name LF)* |
| 294 | |
| 295 | where the object names are in hexadecimal format. The file is not |
| 296 | sorted. |
| 297 | |
| 298 | The loose object index is protected against concurrent writes by a |
| 299 | lock file $GIT_OBJECT_DIR/loose-object-idx.lock. To add a new loose |
| 300 | object: |
| 301 | |
| 302 | 1. Write the loose object to a temporary file, like today. |
| 303 | 2. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the lock. |
| 304 | 3. Rename the loose object into place. |
| 305 | 4. Open loose-object-idx with O_APPEND and write the new object |
| 306 | 5. Unlink loose-object-idx.lock to release the lock. |
| 307 | |
| 308 | To remove entries (e.g. in "git pack-refs" or "git-prune"): |
| 309 | |
| 310 | 1. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the |
| 311 | lock. |
| 312 | 2. Write the new content to loose-object-idx.lock. |
| 313 | 3. Unlink any loose objects being removed. |
| 314 | 4. Rename to replace loose-object-idx, releasing the lock. |
| 315 | |
| 316 | Translation table |
| 317 | ~~~~~~~~~~~~~~~~~ |
| 318 | The index files support a bidirectional mapping between SHA-1 names |
| 319 | and SHA-256 names. The lookup proceeds similarly to ordinary object |
| 320 | lookups. For example, to convert a SHA-1 name to a SHA-256 name: |
| 321 | |
| 322 | 1. Look for the object in idx files. If a match is present in the |
| 323 | idx's sorted list of truncated SHA-1 names, then: |
| 324 | a. Read the corresponding entry in the SHA-1 name order to pack |
| 325 | name order mapping. |
| 326 | b. Read the corresponding entry in the full SHA-1 name table to |
| 327 | verify we found the right object. If it is, then |
| 328 | c. Read the corresponding entry in the full SHA-256 name table. |
| 329 | That is the object's SHA-256 name. |
| 330 | 2. Check for a loose object. Read lines from loose-object-idx until |
| 331 | we find a match. |
| 332 | |
| 333 | Step (1) takes the same amount of time as an ordinary object lookup: |
| 334 | O(number of packs * log(objects per pack)). Step (2) takes O(number of |
| 335 | loose objects) time. To maintain good performance it will be necessary |
| 336 | to keep the number of loose objects low. See the "Loose objects and |
| 337 | unreachable objects" section below for more details. |
| 338 | |
| 339 | Since all operations that make new objects (e.g., "git commit") add |
| 340 | the new objects to the corresponding index, this mapping is possible |
| 341 | for all objects in the object store. |
| 342 | |
| 343 | Reading an object's SHA-1 content |
| 344 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 345 | The SHA-1 content of an object can be read by converting all SHA-256 names |
| 346 | of its SHA-256 content references to SHA-1 names using the translation table. |
| 347 | |
| 348 | Fetch |
| 349 | ~~~~~ |
| 350 | Fetching from a SHA-1 based server requires translating between SHA-1 |
| 351 | and SHA-256 based representations on the fly. |
| 352 | |
| 353 | SHA-1s named in the ref advertisement that are present on the client |
| 354 | can be translated to SHA-256 and looked up as local objects using the |
| 355 | translation table. |
| 356 | |
| 357 | Negotiation proceeds as today. Any "have"s generated locally are |
| 358 | converted to SHA-1 before being sent to the server, and SHA-1s |
| 359 | mentioned by the server are converted to SHA-256 when looking them up |
| 360 | locally. |
| 361 | |
| 362 | After negotiation, the server sends a packfile containing the |
| 363 | requested objects. We convert the packfile to SHA-256 format using |
| 364 | the following steps: |
| 365 | |
| 366 | 1. index-pack: inflate each object in the packfile and compute its |
| 367 | SHA-1. Objects can contain deltas in OBJ_REF_DELTA format against |
| 368 | objects the client has locally. These objects can be looked up |
| 369 | using the translation table and their SHA-1 content read as |
| 370 | described above to resolve the deltas. |
| 371 | 2. topological sort: starting at the "want"s from the negotiation |
| 372 | phase, walk through objects in the pack and emit a list of them, |
| 373 | excluding blobs, in reverse topologically sorted order, with each |
| 374 | object coming later in the list than all objects it references. |
| 375 | (This list only contains objects reachable from the "wants". If the |
| 376 | pack from the server contained additional extraneous objects, then |
| 377 | they will be discarded.) |
| 378 | 3. convert to SHA-256: open a new SHA-256 packfile. Read the topologically |
| 379 | sorted list just generated. For each object, inflate its |
| 380 | SHA-1 content, convert to SHA-256 content, and write it to the SHA-256 |
| 381 | pack. Record the new SHA-1<-->SHA-256 mapping entry for use in the idx. |
| 382 | 4. sort: reorder entries in the new pack to match the order of objects |
| 383 | in the pack the server generated and include blobs. Write a SHA-256 idx |
| 384 | file |
| 385 | 5. clean up: remove the SHA-1 based pack file, index, and |
| 386 | topologically sorted list obtained from the server in steps 1 |
| 387 | and 2. |
| 388 | |
| 389 | Step 3 requires every object referenced by the new object to be in the |
| 390 | translation table. This is why the topological sort step is necessary. |
| 391 | |
| 392 | As an optimization, step 1 could write a file describing what non-blob |
| 393 | objects each object it has inflated from the packfile references. This |
| 394 | makes the topological sort in step 2 possible without inflating the |
| 395 | objects in the packfile for a second time. The objects need to be |
| 396 | inflated again in step 3, for a total of two inflations. |
| 397 | |
| 398 | Step 4 is probably necessary for good read-time performance. "git |
| 399 | pack-objects" on the server optimizes the pack file for good data |
| 400 | locality (see Documentation/technical/pack-heuristics.adoc). |
| 401 | |
| 402 | Details of this process are likely to change. It will take some |
| 403 | experimenting to get this to perform well. |
| 404 | |
| 405 | Push |
| 406 | ~~~~ |
| 407 | Push is simpler than fetch because the objects referenced by the |
| 408 | pushed objects are already in the translation table. The SHA-1 content |
| 409 | of each object being pushed can be read as described in the "Reading |
| 410 | an object's SHA-1 content" section to generate the pack written by git |
| 411 | send-pack. |
| 412 | |
| 413 | Signed Commits |
| 414 | ~~~~~~~~~~~~~~ |
| 415 | We add a new field "gpgsig-sha256" to the commit object format to allow |
| 416 | signing commits without relying on SHA-1. It is similar to the |
| 417 | existing "gpgsig" field. Its signed payload is the SHA-256 content of the |
| 418 | commit object with any "gpgsig" and "gpgsig-sha256" fields removed. |
| 419 | |
| 420 | This means commits can be signed |
| 421 | |
| 422 | 1. using SHA-1 only, as in existing signed commit objects |
| 423 | 2. using both SHA-1 and SHA-256, by using both gpgsig-sha256 and gpgsig |
| 424 | fields. |
| 425 | 3. using only SHA-256, by only using the gpgsig-sha256 field. |
| 426 | |
| 427 | Old versions of "git verify-commit" can verify the gpgsig signature in |
| 428 | cases (1) and (2) without modifications and view case (3) as an |
| 429 | ordinary unsigned commit. |
| 430 | |
| 431 | Signed Tags |
| 432 | ~~~~~~~~~~~ |
| 433 | We add new fields "gpgsig" and "gpgsig-sha256" to the tag object format to |
| 434 | allow signing tags in both formats. The in-body signature is used for the |
| 435 | signature in the current hash algorithm and the header is used for the |
| 436 | signature in the other algorithm. Thus, a dual-signature tag will contain both |
| 437 | an in-body signature and a gpgsig-sha256 header for the SHA-1 format of an |
| 438 | object or both an in-body signature and a gpgsig header for the SHA-256 format |
| 439 | of and object. |
| 440 | |
| 441 | The signed payload of the tag is the content of the tag in the current |
| 442 | algorithm with both its gpgsig and gpgsig-sha256 fields and |
| 443 | "-----BEGIN PGP SIGNATURE-----" delimited in-body signature removed. |
| 444 | |
| 445 | This means tags can be signed using one or both algorithms. |
| 446 | |
| 447 | Mergetag embedding |
| 448 | ~~~~~~~~~~~~~~~~~~ |
| 449 | The mergetag field in the SHA-1 content of a commit contains the |
| 450 | SHA-1 content of a tag that was merged by that commit. |
| 451 | |
| 452 | The mergetag field in the SHA-256 content of the same commit contains the |
| 453 | SHA-256 content of the same tag. |
| 454 | |
| 455 | Submodules |
| 456 | ~~~~~~~~~~ |
| 457 | To convert recorded submodule pointers, you need to have the converted |
| 458 | submodule repository in place. The translation table of the submodule |
| 459 | can be used to look up the new hash. |
| 460 | |
| 461 | Loose objects and unreachable objects |
| 462 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 463 | Fast lookups in the loose-object-idx require that the number of loose |
| 464 | objects not grow too high. |
| 465 | |
| 466 | "git gc --auto" currently waits for there to be 6700 loose objects |
| 467 | present before consolidating them into a packfile. We will need to |
| 468 | measure to find a more appropriate threshold for it to use. |
| 469 | |
| 470 | "git gc --auto" currently waits for there to be 50 packs present |
| 471 | before combining packfiles. Packing loose objects more aggressively |
| 472 | may cause the number of pack files to grow too quickly. This can be |
| 473 | mitigated by using a strategy similar to Martin Fick's exponential |
| 474 | rolling garbage collection script: |
| 475 | https://gerrit-review.googlesource.com/c/gerrit/+/35215 |
| 476 | |
| 477 | "git gc" currently expels any unreachable objects it encounters in |
| 478 | pack files to loose objects in an attempt to prevent a race when |
| 479 | pruning them (in case another process is simultaneously writing a new |
| 480 | object that refers to the about-to-be-deleted object). This leads to |
| 481 | an explosion in the number of loose objects present and disk space |
| 482 | usage due to the objects in delta form being replaced with independent |
| 483 | loose objects. Worse, the race is still present for loose objects. |
| 484 | |
| 485 | Instead, "git gc" will need to move unreachable objects to a new |
| 486 | packfile marked as UNREACHABLE_GARBAGE (using the PSRC field; see |
| 487 | below). To avoid the race when writing new objects referring to an |
| 488 | about-to-be-deleted object, code paths that write new objects will |
| 489 | need to copy any objects from UNREACHABLE_GARBAGE packs that they |
| 490 | refer to new, non-UNREACHABLE_GARBAGE packs (or loose objects). |
| 491 | UNREACHABLE_GARBAGE are then safe to delete if their creation time (as |
| 492 | indicated by the file's mtime) is long enough ago. |
| 493 | |
| 494 | To avoid a proliferation of UNREACHABLE_GARBAGE packs, they can be |
| 495 | combined under certain circumstances. If "gc.garbageTtl" is set to |
| 496 | greater than one day, then packs created within a single calendar day, |
| 497 | UTC, can be coalesced together. The resulting packfile would have an |
| 498 | mtime before midnight on that day, so this makes the effective maximum |
| 499 | ttl the garbageTtl + 1 day. If "gc.garbageTtl" is less than one day, |
| 500 | then we divide the calendar day into intervals one-third of that ttl |
| 501 | in duration. Packs created within the same interval can be coalesced |
| 502 | together. The resulting packfile would have an mtime before the end of |
| 503 | the interval, so this makes the effective maximum ttl equal to the |
| 504 | garbageTtl * 4/3. |
| 505 | |
| 506 | This rule comes from Thirumala Reddy Mutchukota's JGit change |
| 507 | https://git.eclipse.org/r/90465. |
| 508 | |
| 509 | The UNREACHABLE_GARBAGE setting goes in the PSRC field of the pack |
| 510 | index. More generally, that field indicates where a pack came from: |
| 511 | |
| 512 | - 1 (PACK_SOURCE_RECEIVE) for a pack received over the network |
| 513 | - 2 (PACK_SOURCE_AUTO) for a pack created by a lightweight |
| 514 | "gc --auto" operation |
| 515 | - 3 (PACK_SOURCE_GC) for a pack created by a full gc |
| 516 | - 4 (PACK_SOURCE_UNREACHABLE_GARBAGE) for potential garbage |
| 517 | discovered by gc |
| 518 | - 5 (PACK_SOURCE_INSERT) for locally created objects that were |
| 519 | written directly to a pack file, e.g. from "git add ." |
| 520 | |
| 521 | This information can be useful for debugging and for "gc --auto" to |
| 522 | make appropriate choices about which packs to coalesce. |
| 523 | |
| 524 | Caveats |
| 525 | ------- |
| 526 | Invalid objects |
| 527 | ~~~~~~~~~~~~~~~ |
| 528 | The conversion from SHA-1 content to SHA-256 content retains any |
| 529 | brokenness in the original object (e.g., tree entry modes encoded with |
| 530 | leading 0, tree objects whose paths are not sorted correctly, and |
| 531 | commit objects without an author or committer). This is a deliberate |
| 532 | feature of the design to allow the conversion to round-trip. |
| 533 | |
| 534 | More profoundly broken objects (e.g., a commit with a truncated "tree" |
| 535 | header line) cannot be converted but were not usable by current Git |
| 536 | anyway. |
| 537 | |
| 538 | Shallow clone and submodules |
| 539 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 540 | Because it requires all referenced objects to be available in the |
| 541 | locally generated translation table, this design does not support |
| 542 | shallow clone or unfetched submodules. Protocol improvements might |
| 543 | allow lifting this restriction. |
| 544 | |
| 545 | Alternates |
| 546 | ~~~~~~~~~~ |
| 547 | For the same reason, a SHA-256 repository cannot borrow objects from a |
| 548 | SHA-1 repository using objects/info/alternates or |
| 549 | $GIT_ALTERNATE_OBJECT_DIRECTORIES. |
| 550 | |
| 551 | git notes |
| 552 | ~~~~~~~~~ |
| 553 | The "git notes" tool annotates objects using their SHA-1 name as key. |
| 554 | This design does not describe a way to migrate notes trees to use |
| 555 | SHA-256 names. That migration is expected to happen separately (for |
| 556 | example using a file at the root of the notes tree to describe which |
| 557 | hash it uses). |
| 558 | |
| 559 | Server-side cost |
| 560 | ~~~~~~~~~~~~~~~~ |
| 561 | Until Git protocol gains SHA-256 support, using SHA-256 based storage |
| 562 | on public-facing Git servers is strongly discouraged. Once Git |
| 563 | protocol gains SHA-256 support, SHA-256 based servers are likely not |
| 564 | to support SHA-1 compatibility, to avoid what may be a very expensive |
| 565 | hash re-encode during clone and to encourage peers to modernize. |
| 566 | |
| 567 | The design described here allows fetches by SHA-1 clients of a |
| 568 | personal SHA-256 repository because it's not much more difficult than |
| 569 | allowing pushes from that repository. This support needs to be guarded |
| 570 | by a configuration option -- servers like git.kernel.org that serve a |
| 571 | large number of clients would not be expected to bear that cost. |
| 572 | |
| 573 | Meaning of signatures |
| 574 | ~~~~~~~~~~~~~~~~~~~~~ |
| 575 | The signed payload for signed commits and tags does not explicitly |
| 576 | name the hash used to identify objects. If some day Git adopts a new |
| 577 | hash function with the same length as the current SHA-1 (40 |
| 578 | hexadecimal digit) or SHA-256 (64 hexadecimal digit) objects then the |
| 579 | intent behind the PGP signed payload in an object signature is |
| 580 | unclear: |
| 581 | |
| 582 | object e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7 |
| 583 | type commit |
| 584 | tag v2.12.0 |
| 585 | tagger Junio C Hamano <gitster@pobox.com> 1487962205 -0800 |
| 586 | |
| 587 | Git 2.12 |
| 588 | |
| 589 | Does this mean Git v2.12.0 is the commit with SHA-1 name |
| 590 | e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7 or the commit with |
| 591 | new-40-digit-hash-name e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7? |
| 592 | |
| 593 | Fortunately SHA-256 and SHA-1 have different lengths. If Git starts |
| 594 | using another hash with the same length to name objects, then it will |
| 595 | need to change the format of signed payloads using that hash to |
| 596 | address this issue. |
| 597 | |
| 598 | Object names on the command line |
| 599 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 600 | To support the transition (see Transition plan below), this design |
| 601 | supports four different modes of operation: |
| 602 | |
| 603 | 1. ("dark launch") Treat object names input by the user as SHA-1 and |
| 604 | convert any object names written to output to SHA-1, but store |
| 605 | objects using SHA-256. This allows users to test the code with no |
| 606 | visible behavior change except for performance. This allows |
| 607 | running even tests that assume the SHA-1 hash function, to |
| 608 | sanity-check the behavior of the new mode. |
| 609 | |
| 610 | 2. ("early transition") Allow both SHA-1 and SHA-256 object names in |
| 611 | input. Any object names written to output use SHA-1. This allows |
| 612 | users to continue to make use of SHA-1 to communicate with peers |
| 613 | (e.g. by email) that have not migrated yet and prepares for mode 3. |
| 614 | |
| 615 | 3. ("late transition") Allow both SHA-1 and SHA-256 object names in |
| 616 | input. Any object names written to output use SHA-256. In this |
| 617 | mode, users are using a more secure object naming method by |
| 618 | default. The disruption is minimal as long as most of their peers |
| 619 | are in mode 2 or mode 3. |
| 620 | |
| 621 | 4. ("post-transition") Treat object names input by the user as |
| 622 | SHA-256 and write output using SHA-256. This is safer than mode 3 |
| 623 | because there is less risk that input is incorrectly interpreted |
| 624 | using the wrong hash function. |
| 625 | |
| 626 | The mode is specified in configuration. |
| 627 | |
| 628 | The user can also explicitly specify which format to use for a |
| 629 | particular revision specifier and for output, overriding the mode. For |
| 630 | example: |
| 631 | |
| 632 | git --output-format=sha1 log abac87a^{sha1}..f787cac^{sha256} |
| 633 | |
| 634 | Transition plan |
| 635 | --------------- |
| 636 | Some initial steps can be implemented independently of one another: |
| 637 | |
| 638 | - adding a hash function API (vtable) |
| 639 | - teaching fsck to tolerate the gpgsig-sha256 field |
| 640 | - excluding gpgsig-* from the fields copied by "git commit --amend" |
| 641 | - annotating tests that depend on SHA-1 values with a SHA1 test |
| 642 | prerequisite |
| 643 | - using "struct object_id", GIT_MAX_RAWSZ, and GIT_MAX_HEXSZ |
| 644 | consistently instead of "unsigned char *" and the hardcoded |
| 645 | constants 20 and 40. |
| 646 | - introducing index v3 |
| 647 | - adding support for the PSRC field and safer object pruning |
| 648 | |
| 649 | The first user-visible change is the introduction of the objectFormat |
| 650 | extension (without compatObjectFormat). This requires: |
| 651 | |
| 652 | - teaching fsck about this mode of operation |
| 653 | - using the hash function API (vtable) when computing object names |
| 654 | - signing objects and verifying signatures |
| 655 | - rejecting attempts to fetch from or push to an incompatible |
| 656 | repository |
| 657 | |
| 658 | Next comes introduction of compatObjectFormat: |
| 659 | |
| 660 | - implementing the loose-object-idx |
| 661 | - translating object names between object formats |
| 662 | - translating object content between object formats |
| 663 | - generating and verifying signatures in the compat format |
| 664 | - adding appropriate index entries when adding a new object to the |
| 665 | object store |
| 666 | - --output-format option |
| 667 | - ^{sha1} and ^{sha256} revision notation |
| 668 | - configuration to specify default input and output format (see |
| 669 | "Object names on the command line" above) |
| 670 | |
| 671 | The next step is supporting fetches and pushes to SHA-1 repositories: |
| 672 | |
| 673 | - allow pushes to a repository using the compat format |
| 674 | - generate a topologically sorted list of the SHA-1 names of fetched |
| 675 | objects |
| 676 | - convert the fetched packfile to SHA-256 format and generate an idx |
| 677 | file |
| 678 | - re-sort to match the order of objects in the fetched packfile |
| 679 | |
| 680 | The infrastructure supporting fetch also allows converting an existing |
| 681 | repository. In converted repositories and new clones, end users can |
| 682 | gain support for the new hash function without any visible change in |
| 683 | behavior (see "dark launch" in the "Object names on the command line" |
| 684 | section). In particular this allows users to verify SHA-256 signatures |
| 685 | on objects in the repository, and it should ensure the transition code |
| 686 | is stable in production in preparation for using it more widely. |
| 687 | |
| 688 | Over time projects would encourage their users to adopt the "early |
| 689 | transition" and then "late transition" modes to take advantage of the |
| 690 | new, more futureproof SHA-256 object names. |
| 691 | |
| 692 | When objectFormat and compatObjectFormat are both set, commands |
| 693 | generating signatures would generate both SHA-1 and SHA-256 signatures |
| 694 | by default to support both new and old users. |
| 695 | |
| 696 | In projects using SHA-256 heavily, users could be encouraged to adopt |
| 697 | the "post-transition" mode to avoid accidentally making implicit use |
| 698 | of SHA-1 object names. |
| 699 | |
| 700 | Once a critical mass of users have upgraded to a version of Git that |
| 701 | can verify SHA-256 signatures and have converted their existing |
| 702 | repositories to support verifying them, we can add support for a |
| 703 | setting to generate only SHA-256 signatures. This is expected to be at |
| 704 | least a year later. |
| 705 | |
| 706 | That is also a good moment to advertise the ability to convert |
| 707 | repositories to use SHA-256 only, stripping out all SHA-1 related |
| 708 | metadata. This improves performance by eliminating translation |
| 709 | overhead and security by avoiding the possibility of accidentally |
| 710 | relying on the safety of SHA-1. |
| 711 | |
| 712 | Updating Git's protocols to allow a server to specify which hash |
| 713 | functions it supports is also an important part of this transition. It |
| 714 | is not discussed in detail in this document but this transition plan |
| 715 | assumes it happens. :) |
| 716 | |
| 717 | Alternatives considered |
| 718 | ----------------------- |
| 719 | Upgrading everyone working on a particular project on a flag day |
| 720 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 721 | Projects like the Linux kernel are large and complex enough that |
| 722 | flipping the switch for all projects based on the repository at once |
| 723 | is infeasible. |
| 724 | |
| 725 | Not only would all developers and server operators supporting |
| 726 | developers have to switch on the same flag day, but supporting tooling |
| 727 | (continuous integration, code review, bug trackers, etc) would have to |
| 728 | be adapted as well. This also makes it difficult to get early feedback |
| 729 | from some project participants testing before it is time for mass |
| 730 | adoption. |
| 731 | |
| 732 | Using hash functions in parallel |
| 733 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 734 | (e.g. https://lore.kernel.org/git/22708.8913.864049.452252@chiark.greenend.org.uk/ ) |
| 735 | Objects newly created would be addressed by the new hash, but inside |
| 736 | such an object (e.g. commit) it is still possible to address objects |
| 737 | using the old hash function. |
| 738 | |
| 739 | * You cannot trust its history (needed for bisectability) in the |
| 740 | future without further work |
| 741 | * Maintenance burden as the number of supported hash functions grows |
| 742 | (they will never go away, so they accumulate). In this proposal, by |
| 743 | comparison, converted objects lose all references to SHA-1. |
| 744 | |
| 745 | Signed objects with multiple hashes |
| 746 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 747 | Instead of introducing the gpgsig-sha256 field in commit and tag objects |
| 748 | for SHA-256 content based signatures, an earlier version of this design |
| 749 | added "hash sha256 <SHA-256 name>" fields to strengthen the existing |
| 750 | SHA-1 content based signatures. |
| 751 | |
| 752 | In other words, a single signature was used to attest to the object |
| 753 | content using both hash functions. This had some advantages: |
| 754 | |
| 755 | * Using one signature instead of two speeds up the signing process. |
| 756 | * Having one signed payload with both hashes allows the signer to |
| 757 | attest to the SHA-1 name and SHA-256 name referring to the same object. |
| 758 | * All users consume the same signature. Broken signatures are likely |
| 759 | to be detected quickly using current versions of git. |
| 760 | |
| 761 | However, it also came with disadvantages: |
| 762 | |
| 763 | * Verifying a signed object requires access to the SHA-1 names of all |
| 764 | objects it references, even after the transition is complete and |
| 765 | translation table is no longer needed for anything else. To support |
| 766 | this, the design added fields such as "hash sha1 tree <SHA-1 name>" |
| 767 | and "hash sha1 parent <SHA-1 name>" to the SHA-256 content of a signed |
| 768 | commit, complicating the conversion process. |
| 769 | * Allowing signed objects without a SHA-1 (for after the transition is |
| 770 | complete) complicated the design further, requiring a "nohash sha1" |
| 771 | field to suppress including "hash sha1" fields in the SHA-256 content |
| 772 | and signed payload. |
| 773 | |
| 774 | Lazily populated translation table |
| 775 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 776 | Some of the work of building the translation table could be deferred to |
| 777 | push time, but that would significantly complicate and slow down pushes. |
| 778 | Calculating the SHA-1 name at object creation time at the same time it is |
| 779 | being streamed to disk and having its SHA-256 name calculated should be |
| 780 | an acceptable cost. |
| 781 | |
| 782 | Document History |
| 783 | ---------------- |
| 784 | |
| 785 | 2017-03-03 |
| 786 | bmwill@google.com, jonathantanmy@google.com, jrnieder@gmail.com, |
| 787 | sbeller@google.com |
| 788 | |
| 789 | * Initial version sent to https://lore.kernel.org/git/20170304011251.GA26789@aiede.mtv.corp.google.com |
| 790 | |
| 791 | 2017-03-03 jrnieder@gmail.com |
| 792 | Incorporated suggestions from jonathantanmy and sbeller: |
| 793 | |
| 794 | * Describe purpose of signed objects with each hash type |
| 795 | * Redefine signed object verification using object content under the |
| 796 | first hash function |
| 797 | |
| 798 | 2017-03-06 jrnieder@gmail.com |
| 799 | |
| 800 | * Use SHA3-256 instead of SHA2 (thanks, Linus and brian m. carlson).[1][2] |
| 801 | * Make SHA3-based signatures a separate field, avoiding the need for |
| 802 | "hash" and "nohash" fields (thanks to peff[3]). |
| 803 | * Add a sorting phase to fetch (thanks to Junio for noticing the need |
| 804 | for this). |
| 805 | * Omit blobs from the topological sort during fetch (thanks to peff). |
| 806 | * Discuss alternates, git notes, and git servers in the caveats |
| 807 | section (thanks to Junio Hamano, brian m. carlson[4], and Shawn |
| 808 | Pearce). |
| 809 | * Clarify language throughout (thanks to various commenters, |
| 810 | especially Junio). |
| 811 | |
| 812 | 2017-09-27 jrnieder@gmail.com, sbeller@google.com |
| 813 | |
| 814 | * Use placeholder NewHash instead of SHA3-256 |
| 815 | * Describe criteria for picking a hash function. |
| 816 | * Include a transition plan (thanks especially to Brandon Williams |
| 817 | for fleshing these ideas out) |
| 818 | * Define the translation table (thanks, Shawn Pearce[5], Jonathan |
| 819 | Tan, and Masaya Suzuki) |
| 820 | * Avoid loose object overhead by packing more aggressively in |
| 821 | "git gc --auto" |
| 822 | |
| 823 | Later history: |
| 824 | |
| 825 | * See the history of this file in git.git for the history of subsequent |
| 826 | edits. This document history is no longer being maintained as it |
| 827 | would now be superfluous to the commit log |
| 828 | |
| 829 | References: |
| 830 | |
| 831 | [1] https://lore.kernel.org/git/CA+55aFzJtejiCjV0e43+9oR3QuJK2PiFiLQemytoLpyJWe6P9w@mail.gmail.com/ |
| 832 | [2] https://lore.kernel.org/git/CA+55aFz+gkAsDZ24zmePQuEs1XPS9BP_s8O7Q4wQ7LV7X5-oDA@mail.gmail.com/ |
| 833 | [3] https://lore.kernel.org/git/20170306084353.nrns455dvkdsfgo5@sigill.intra.peff.net/ |
| 834 | [4] https://lore.kernel.org/git/20170304224936.rqqtkdvfjgyezsht@genre.crustytoothpaste.net |
| 835 | [5] https://lore.kernel.org/git/CAJo=hJtoX9=AyLHHpUJS7fueV9ciZ_MNpnEPHUz8Whui6g9F0A@mail.gmail.com/ |