Skip to contentSkip to conversation

sign-file: Fix memory leaks in the sign-file tool

PATCH In review Danny Hu 2 participants 5 messages Jul 18, 2025 – Aug 13, 2025

Subsystems (2)
lore
Share & tools
lore

Danny Hu

author
#0 lore raw

PATCH 1/2 sign-file: Fix memory leaks in the sign-file tool

Fix memory leaks in the sign-file tool by:
1. Freeing the strings that have been allocated via asprintf. This includes sig_file_name and dest_name.

2. Initialize X509 and EVP_PKEY pointers with NULL to avoid compiler warnings about uninitalized variables.

3. Ensuring that any structs which have been allocated are freed with their respective free functions.

---
 scripts/sign-file.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
scripts/sign-file.c +8 -2
index 7070245edfc1..0fa9b75f1c77 100644
@@ -230,14 +230,14 @@ int main(int argc, char **argv)
 	unsigned long module_size, sig_size;
 	unsigned int use_signed_attrs;
 	const EVP_MD *digest_algo;
-	EVP_PKEY *private_key;
+	EVP_PKEY *private_key = NULL;
 #ifndef USE_PKCS7
 	CMS_ContentInfo *cms = NULL;
 	unsigned int use_keyid = 0;
 #else
 	PKCS7 *pkcs7 = NULL;
 #endif
-	X509 *x509;
+	X509 *x509 = NULL;
 	BIO *bd, *bm;
 	int opt, n;
 	OpenSSL_add_all_algorithms();
@@ -351,6 +351,7 @@ int main(int argc, char **argv)
 			ERR(i2d_PKCS7_bio(b, pkcs7) != 1,
 			    "%s", sig_file_name);
 #endif
+			free(sig_file_name);
 			BIO_free(b);
 		}
 
@@ -377,10 +378,14 @@ int main(int argc, char **argv)
 	module_size = BIO_number_written(bd);
 
 	if (!raw_sig) {
+		EVP_PKEY_free(private_key);
+		X509_free(x509);
 #ifndef USE_PKCS7
 		ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name);
+		CMS_ContentInfo_free(cms);
 #else
 		ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name);
+		PKCS7_free(pkcs7);
 #endif
 	} else {
 		BIO *b;
@@ -406,5 +411,6 @@ int main(int argc, char **argv)
 	if (replace_orig)
 		ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
 
+	free(dest_name);
 	return 0;
 }
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH 2/2 sign-file: Extend sign-file tool to allow for certificate to be embedded in the signature

Add an optional -i flag to the sign-file tool provides the ability to embed the signing certificate in the signature generated by the sign-file tool.

This change is crucial for enabling the use of certificate hierarchies (chain of trust) within the kernel's Integrity Measurement Architecture (IMA). Critically, IMA can appraise kernel binaries and initrds signed by the sign-file tool.

Without this change, kernel binaries and initrds are limited to being signed directly by keys present in the .ima keyring. The kernel already has the facilities to extract and process embedded certificates so we can leverage this by embedding the signing certificate in appended signature to provide the kernel the information to perform full path validation in the certificate hierarchy.

This enhancement improves the flexibility and scalability of IMA deployments. Organizations can now leverage their existing Public Key Infrastructure by signing kernel modules, executables, or other measured files with certificates issued by intermediate CAs within their trusted hierarchy, rather than requiring direct root CA signing for every single appraised file.

---
 scripts/sign-file.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
scripts/sign-file.c +10 -7
index 0fa9b75f1c77..83767dc2a720 100644
@@ -80,7 +80,7 @@ static __attribute__((noreturn))
 void format(void)
 {
 	fprintf(stderr,
-		"Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
+		"Usage: scripts/sign-file [-dpi] <hash algo> <key> <x509> <module> [<dest>]\n");
 	fprintf(stderr,
 		"       scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n");
 	exit(2);
@@ -228,7 +228,7 @@ int main(int argc, char **argv)
 	bool raw_sig = false;
 	unsigned char buf[4096];
 	unsigned long module_size, sig_size;
-	unsigned int use_signed_attrs;
+	unsigned int use_signed_attrs, include_cert;
 	const EVP_MD *digest_algo;
 	EVP_PKEY *private_key = NULL;
 #ifndef USE_PKCS7
@@ -248,16 +248,19 @@ int main(int argc, char **argv)
 
 #ifndef USE_PKCS7
 	use_signed_attrs = CMS_NOATTR;
+	include_cert = CMS_NOCERTS;
 #else
 	use_signed_attrs = PKCS7_NOATTR;
+	include_cert = PKCS7_NOCERTS;
 #endif
 
 	do {
-		opt = getopt(argc, argv, "sdpk");
+		opt = getopt(argc, argv, "sdpki");
 		switch (opt) {
 		case 's': raw_sig = true; break;
 		case 'p': save_sig = true; break;
 		case 'd': sign_only = true; save_sig = true; break;
+		case 'i': include_cert = 0; break;
 #ifndef USE_PKCS7
 		case 'k': use_keyid = CMS_USE_KEYID; break;
 #endif
@@ -317,21 +320,21 @@ int main(int argc, char **argv)
 #ifndef USE_PKCS7
 		/* Load the signature message from the digest buffer. */
 		cms = CMS_sign(NULL, NULL, NULL, NULL,
-			       CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY |
+			       include_cert | CMS_PARTIAL | CMS_BINARY |
 			       CMS_DETACHED | CMS_STREAM);
 		ERR(!cms, "CMS_sign");
 
 		ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
-				     CMS_NOCERTS | CMS_BINARY |
+				     include_cert | CMS_BINARY |
 				     CMS_NOSMIMECAP | use_keyid |
 				     use_signed_attrs),
 		    "CMS_add1_signer");
-		ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1,
+		ERR(CMS_final(cms, bm, NULL, include_cert | CMS_BINARY) != 1,
 		    "CMS_final");
 
 #else
 		pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
-				   PKCS7_NOCERTS | PKCS7_BINARY |
+				   include_cert | PKCS7_BINARY |
 				   PKCS7_DETACHED | use_signed_attrs);
 		ERR(!pkcs7, "PKCS7_sign");
 #endif
Signature · 1 line
2.47.0
maintainer
#2 lore raw
In reply to Danny Hu
Quoted 2 lines · Danny Hu

3. Ensuring that any structs which have been allocated are freed with their respective free functions.

Why? exit() will clean that up.

David

author
#3 lore raw
Quoted 1 line

Why? exit() will clean that up.

You are right that exit() will clean up all allocated memory. However, when this binary runs within tests instrumented with AddressSanitizer (ASan) and LeakSanitizer (LSan), these leaks cause the instrumented tests to fail. This happens even if the program isn't leaking in a way that would cause long-term resource exhaustion.

The main reason for this change is to satisfy LSan. By doing so, we avoid false positives in LSan-instrumented test suites, which allows us to rely on LSan to detect actual, problematic leaks that need fixing. The alternative would be to omit any tests that use the sign-file binary from LSan instrumentation, but that would mean sacrificing test coverage, which isn't ideal.

Thanks,
Danny

Quoted 10 lines · David Howells
Quoted 2 lines · Danny Hu

3. Ensuring that any structs which have been allocated are freed with their respective free functions.

Why? exit() will clean that up.

David

author
#4 lore raw
In reply to David Howells
Quoted 1 line · David Howells

Why? exit() will clean that up.

You are right that exit() will clean up all allocated memory. However, when this binary runs within tests instrumented with AddressSanitizer (ASan) and LeakSanitizer (LSan), these leaks cause the instrumented tests to fail. This happens even if the program isn't leaking in a way that would cause long-term resource exhaustion.

The main reason for this change is to satisfy LSan. By doing so, we avoid false positives in LSan-instrumented test suites, which allows us to rely on LSan to detect actual, problematic leaks that need fixing. The alternative would be to omit any tests that use the sign-file binary from LSan instrumentation, but that would mean sacrificing test coverage, which isn't ideal.

Thanks,
Danny