Skip to content

Commit fb77b44

Browse files
esnowbergvathpela
authored andcommitted
Generate Authenticode for the entire PE file
The Authenticode is a hash calculation that excludes parts of the PE file that are altered in the signing process itself. The Authenticode for a PE file should be the same for both the signed and unsigned versions. Both sbsign and pesign have chosen to modify a portion of the PE file that falls outside the actual digital signature. This is done by zero padding the end of the PE file to align the signature table that is later added. After doing this padding, the new zero padded data is included within the Authenticode hash calculation. Both pesign and sbsign can display the Authenticode for an unsigned binary with the padding included within the calculation. Adding this hash to the MOK does not allow the program to run. The pesign program also has an option to generate the Authenticode without the padding included. Adding this hash to the MOK also does NOT allow the program to run. When shim finds a PE file without a digital signature, it completely stops calculating the hash towards the end of the file. Part of the file is excluded. Testing has shown that the last 3K of the file can be omitted from the calculation. If the Authenticode is generated using Shim’s MokManager, it will calculate a hash without the last part and allow the program to run. Since the end of the file is not included within the calculation, other things could be added. Fix all this by hashing the entire file that is outside the digital signature to calculate the Authenticode. Also add zero padding when necessary to do the Authenticode calculation. If the program is signed, this code should never be referenced. However, if this code is entered by a signed PE file, there is potentially something nefarious going on. link: https://blog.hansenpartnership.com/problems-with-tianocore-after-multi-sign-r14141-fixed/ link: osresearch/sbsigntools@370abb7 link: https://git.kernel.org/pub/scm/linux/kernel/git/jejb/sbsigntools.git/commit/?id=592ec2188f7b9cf003fe7cb0835e93559f19156f Signed-off-by: Eric Snowberg <eric.snowberg@oracle.com>
1 parent 1abc7ca commit fb77b44

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

pe.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,16 @@ generate_hash(char *data, unsigned int datasize,
410410
goto done;
411411
}
412412

413-
#if 1
414-
}
415-
#else // we have to migrate to doing this later :/
416413
SumOfBytesHashed += hashsize;
417414
}
418415

419-
/* Hash all remaining data */
420-
if (datasize > SumOfBytesHashed) {
416+
/* Hash all remaining data. If SecDir->Size is > 0 this code should not
417+
* be entered. If it is, there are still things to hash. For a file
418+
* without a SecDir, we need to hash what remains. */
419+
if (datasize > SumOfBytesHashed + context->SecDir->Size) {
420+
char padbuf[8];
421+
ZeroMem(padbuf, 8);
422+
421423
hashbase = data + SumOfBytesHashed;
422424
hashsize = datasize - SumOfBytesHashed;
423425

@@ -431,8 +433,17 @@ generate_hash(char *data, unsigned int datasize,
431433
}
432434

433435
SumOfBytesHashed += hashsize;
436+
hashsize = ALIGN_VALUE(SumOfBytesHashed, 8) - SumOfBytesHashed;
437+
438+
if (hashsize) {
439+
if (!(Sha256Update(sha256ctx, padbuf, hashsize)) ||
440+
!(Sha1Update(sha1ctx, padbuf, hashsize))) {
441+
perror(L"Unable to generate hash\n");
442+
efi_status = EFI_OUT_OF_RESOURCES;
443+
goto done;
444+
}
445+
}
434446
}
435-
#endif
436447

437448
if (!(Sha256Final(sha256ctx, sha256hash)) ||
438449
!(Sha1Final(sha1ctx, sha1hash))) {

0 commit comments

Comments
 (0)