Skip to content

Commit e9ef727

Browse files
authored
Merge pull request #503 from aidangarske/fenrir-fixes-11
Hardening fixes for examples, fwTPM command handlers, and crypto helpers
2 parents 565b239 + 852c2d2 commit e9ef727

25 files changed

Lines changed: 466 additions & 48 deletions

examples/attestation/make_credential.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
133133
if (rc != TPM_RC_SUCCESS) {
134134
printf("TPM2_LoadExternal: failed %d: %s\n", rc,
135135
wolfTPM2_GetRCString(rc));
136-
return rc;
136+
goto exit;
137137
}
138138
printf("Public key for encryption loaded\n");
139139
handle.hndl = loadExtOut.objectHandle;

examples/boot/secret_unseal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ int TPM2_Boot_SecretUnseal_Example(void* userCtx, int argc, char *argv[])
167167
usage();
168168
return 0;
169169
}
170+
if (pcrArraySz >= sizeof(pcrArray) / sizeof(pcrArray[0])) {
171+
printf("Too many -pcr= arguments (max %zu)\n",
172+
sizeof(pcrArray) / sizeof(pcrArray[0]));
173+
usage();
174+
return 0;
175+
}
170176
pcrArray[pcrArraySz] = pcrIndex;
171177
pcrArraySz++;
172178
}

examples/firmware/ifx_fw_extract.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,20 @@ static int extractFW(
129129

130130
READ_BE16(size16, fw, fw_size, offset);
131131
offset += size16 + 1;
132+
if (offset > fw_size) { LOG("FW file too short"); return -1; }
132133

133134
READ_BE16(size16, fw, fw_size, offset);
134135
offset += size16;
136+
if (offset > fw_size) { LOG("FW file too short"); return -1; }
135137

136138
READ_BE16(size16, fw, fw_size, offset);
137139
offset2 = offset;
138140
offset += size16;
141+
if (offset > fw_size) { LOG("FW file too short"); return -1; }
139142

140143
READ_BE16(size16, fw, offset, offset2);
141144
offset2 += size16;
145+
if (offset2 > offset) { LOG("Bad manifest header size"); return -1; }
142146

143147
READ_BE16(num, fw, offset, offset2);
144148

@@ -149,6 +153,10 @@ static int extractFW(
149153

150154
READ_BE16(size16, fw, offset, offset2);
151155

156+
if ((size_t)offset2 + size16 > offset) {
157+
LOG("Bad manifest entry size");
158+
return -1;
159+
}
152160
if (group == keygroup_id) {
153161
printf("Chosen group found: %08x\n", group);
154162
*manifest = &fw[offset2];

examples/keygen/external_import.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,19 @@ int TPM2_ExternalImport_Example(void* userCtx, int argc, char *argv[])
127127
argc--;
128128
}
129129

130+
XMEMSET(&dev, 0, sizeof(dev));
131+
XMEMSET(&storage, 0, sizeof(storage));
132+
primary = &storage;
133+
130134
#ifndef WOLFTPM2_NO_HEAP
131135
key2 = wolfTPM2_NewKeyBlob();
132136
rsaKey3 = wolfTPM2_NewKeyBlob();
137+
if (key2 == NULL || rsaKey3 == NULL) {
138+
printf("wolfTPM2_NewKeyBlob allocation failed\n");
139+
rc = MEMORY_E;
140+
goto exit;
141+
}
133142
#endif
134-
XMEMSET(&storage, 0, sizeof(storage));
135-
primary = &storage;
136143

137144
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
138145
if (rc != TPM_RC_SUCCESS) {
@@ -237,8 +244,15 @@ int TPM2_ExternalImport_Example(void* userCtx, int argc, char *argv[])
237244
}
238245

239246
exit:
247+
#ifndef WOLFTPM2_NO_HEAP
248+
if (rsaKey3 != NULL)
249+
wolfTPM2_UnloadHandle(&dev, &rsaKey3->handle);
250+
if (key2 != NULL)
251+
wolfTPM2_UnloadHandle(&dev, &key2->handle);
252+
#else
240253
wolfTPM2_UnloadHandle(&dev, &rsaKey3->handle);
241254
wolfTPM2_UnloadHandle(&dev, &key2->handle);
255+
#endif
242256
wolfTPM2_UnloadHandle(&dev, &primary->handle);
243257

244258
#ifndef WOLFTPM2_NO_HEAP

examples/keygen/keygen.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
286286
}
287287
else if (XSTRNCMP(argv[argc-1], "-auth=", XSTRLEN("-auth=")) == 0) {
288288
authStr = argv[argc-1] + XSTRLEN("-auth=");
289+
if (XSTRLEN(authStr) > sizeof(auth.buffer)) {
290+
printf("-auth value too long (max %zu)\n",
291+
sizeof(auth.buffer));
292+
usage();
293+
return 0;
294+
}
289295
}
290296
else if (argv[argc-1][0] != '-') {
291297
outputFile = argv[argc-1];

examples/keygen/keyimport.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,15 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
191191

192192
/* setup an auth value */
193193
if (password != NULL) {
194-
impKey.handle.auth.size = (int)XSTRLEN(password);
194+
size_t pwLen;
195+
pwLen = XSTRLEN(password);
196+
if (pwLen > sizeof(impKey.handle.auth.buffer)) {
197+
printf("-password too long (max %zu)\n",
198+
sizeof(impKey.handle.auth.buffer));
199+
rc = BUFFER_E;
200+
goto exit;
201+
}
202+
impKey.handle.auth.size = (UINT16)pwLen;
195203
XMEMCPY(impKey.handle.auth.buffer, password, impKey.handle.auth.size);
196204
}
197205

examples/native/native_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
12321232
if (rc != TPM_RC_SUCCESS) {
12331233
printf("TPM2_ObjectChangeAuth failed 0x%x: %s\n", rc,
12341234
TPM2_GetRCString(rc));
1235-
//goto exit;
1235+
goto exit;
12361236
}
12371237
hmacKey.priv = cmdOut.objChgAuth.outPrivate;
12381238
printf("TPM2_ObjectChangeAuth: private %d\n", hmacKey.priv.size);

examples/nvram/seal_nv.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,15 @@ int TPM2_NVRAM_SealNV_Example(void* userCtx, int argc, char *argv[])
196196
/* Set owner auth */
197197
parent.hndl = TPM_RH_OWNER;
198198
if (XSTRLEN(ownerAuth) > 0) {
199-
parent.auth.size = (int)XSTRLEN(ownerAuth);
199+
size_t authLen;
200+
authLen = XSTRLEN(ownerAuth);
201+
if (authLen > sizeof(parent.auth.buffer)) {
202+
fprintf(stderr, "-ownerauth value too long (max %zu)\n",
203+
sizeof(parent.auth.buffer));
204+
rc = BUFFER_E;
205+
goto exit;
206+
}
207+
parent.auth.size = (UINT16)authLen;
200208
XMEMCPY(parent.auth.buffer, ownerAuth, parent.auth.size);
201209
}
202210

examples/pcr/policy_sign.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ int TPM2_PCR_PolicySign_Example(void* userCtx, int argc, char *argv[])
274274
usage();
275275
return 0;
276276
}
277+
if (pcrArraySz >= sizeof(pcrArray) / sizeof(pcrArray[0])) {
278+
printf("Too many -pcr= arguments (max %zu)\n",
279+
sizeof(pcrArray) / sizeof(pcrArray[0]));
280+
usage();
281+
return 0;
282+
}
277283
pcrArray[pcrArraySz] = pcrIndex;
278284
pcrArraySz++;
279285
}

examples/tls/tls_client_notpm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ int TLS_ClientArgs(int argc, char *argv[])
131131
ca_cert_der_2048, sizeof_ca_cert_der_2048,
132132
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
133133
printf("Error loading ca_cert_der_2048 DER cert\n");
134+
rc = WOLFSSL_FATAL_ERROR;
134135
goto exit;
135136
}
136137
#elif defined(HAVE_ECC)
137138
if (wolfSSL_CTX_load_verify_buffer(ctx,
138139
ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
139140
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
140141
printf("Error loading ca_ecc_cert_der_256 DER cert\n");
142+
rc = WOLFSSL_FATAL_ERROR;
141143
goto exit;
142144
}
143145
#endif
@@ -149,22 +151,26 @@ int TLS_ClientArgs(int argc, char *argv[])
149151
if (wolfSSL_CTX_use_certificate_buffer(ctx,
150152
client_cert_der_2048, sizeof_client_cert_der_2048,
151153
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
154+
rc = WOLFSSL_FATAL_ERROR;
152155
goto exit;
153156
}
154157
if (wolfSSL_CTX_use_PrivateKey_buffer(ctx,
155158
client_key_der_2048, sizeof_client_key_der_2048,
156159
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
160+
rc = WOLFSSL_FATAL_ERROR;
157161
goto exit;
158162
}
159163
#elif defined(HAVE_ECC)
160164
if (wolfSSL_CTX_use_certificate_buffer(ctx,
161165
cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
162166
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
167+
rc = WOLFSSL_FATAL_ERROR;
163168
goto exit;
164169
}
165170
if (wolfSSL_CTX_use_PrivateKey_buffer(ctx,
166171
ecc_clikey_der_256, sizeof_ecc_clikey_der_256,
167172
WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
173+
rc = WOLFSSL_FATAL_ERROR;
168174
goto exit;
169175
}
170176
#endif

0 commit comments

Comments
 (0)