Merge pull request #27713 from ddstreet/tpm2_replace_make_primary

Tpm2 replace make primary
This commit is contained in:
Lennart Poettering 2023-07-06 10:22:12 +02:00 committed by GitHub
commit a130b09513
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 711 additions and 312 deletions

View file

@ -103,6 +103,33 @@ void* greedy_realloc0(
return q;
}
void* greedy_realloc_append(
void **p,
size_t *n_p,
const void *from,
size_t n_from,
size_t size) {
uint8_t *q;
assert(p);
assert(n_p);
assert(from || n_from == 0);
if (n_from > SIZE_MAX - *n_p)
return NULL;
q = greedy_realloc(p, *n_p + n_from, size);
if (!q)
return NULL;
memcpy_safe(q + *n_p * size, from, n_from * size);
*n_p += n_from;
return q;
}
void *expand_to_usable(void *ptr, size_t newsize _unused_) {
return ptr;
}

View file

@ -147,6 +147,7 @@ static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t n
void* greedy_realloc(void **p, size_t need, size_t size);
void* greedy_realloc0(void **p, size_t need, size_t size);
void* greedy_realloc_append(void **p, size_t *n_p, const void *from, size_t n_from, size_t size);
#define GREEDY_REALLOC(array, need) \
greedy_realloc((void**) &(array), (need), sizeof((array)[0]))
@ -154,6 +155,9 @@ void* greedy_realloc0(void **p, size_t need, size_t size);
#define GREEDY_REALLOC0(array, need) \
greedy_realloc0((void**) &(array), (need), sizeof((array)[0]))
#define GREEDY_REALLOC_APPEND(array, n_array, from, n_from) \
greedy_realloc_append((void**) &(array), (size_t*) &(n_array), (from), (n_from), sizeof((array)[0]))
#define alloca0(n) \
({ \
char *_new_; \

File diff suppressed because it is too large Load diff

View file

@ -62,6 +62,10 @@ typedef struct {
ESYS_CONTEXT *esys_context;
/* Some selected cached capabilities of the TPM */
TPMS_ALG_PROPERTY *capability_algorithms;
size_t n_capability_algorithms;
TPMA_CC *capability_commands;
size_t n_capability_commands;
TPML_PCR_SELECTION capability_pcrs;
} Tpm2Context;
@ -84,7 +88,8 @@ int tpm2_handle_new(Tpm2Context *context, Tpm2Handle **ret_handle);
Tpm2Handle *tpm2_handle_free(Tpm2Handle *handle);
DEFINE_TRIVIAL_CLEANUP_FUNC(Tpm2Handle*, tpm2_handle_free);
int tpm2_supports_alg(Tpm2Context *c, TPM2_ALG_ID alg);
bool tpm2_supports_alg(Tpm2Context *c, TPM2_ALG_ID alg);
bool tpm2_supports_command(Tpm2Context *c, TPM2_CC command);
bool tpm2_test_parms(Tpm2Context *c, TPMI_ALG_PUBLIC alg, const TPMU_PUBLIC_PARMS *parms);

View file

@ -23,8 +23,8 @@ TEST(alloca) {
}
TEST(GREEDY_REALLOC) {
_cleanup_free_ int *a = NULL, *b = NULL;
size_t i, j;
_cleanup_free_ int *a = NULL, *b = NULL, *c = NULL;
size_t i, j, n_c = 0;
/* Give valgrind a chance to verify our realloc() operations */
@ -53,6 +53,45 @@ TEST(GREEDY_REALLOC) {
for (j = 30; j < i / 2; j += 7)
assert_se(b[j] == (int) j);
size_t n_from = 10;
int from[n_from];
for (i = 0; i < 2048; i++) {
for (j = 0; j < n_from; j++)
from[j] = n_from * i + j;
_cleanup_free_ int *before = NULL;
size_t n_before = 0;
assert_se(GREEDY_REALLOC_APPEND(before, n_before, c, n_c));
assert_se(before);
assert_se(n_before == n_c);
assert_se(memcmp_safe(c, before, n_c) == 0);
assert_se(GREEDY_REALLOC_APPEND(c, n_c, from, n_from));
assert_se(n_c == n_before + n_from);
assert_se(MALLOC_ELEMENTSOF(c) >= n_c);
assert_se(MALLOC_SIZEOF_SAFE(c) >= n_c * sizeof(int));
assert_se(memcmp_safe(c, before, n_before) == 0);
assert_se(memcmp_safe(&c[n_before], from, n_from) == 0);
before = mfree(before);
assert_se(!before);
n_before = 0;
assert_se(GREEDY_REALLOC_APPEND(before, n_before, c, n_c));
assert_se(before);
assert_se(n_before == n_c);
assert_se(memcmp_safe(c, before, n_c) == 0);
assert_se(GREEDY_REALLOC_APPEND(c, n_c, NULL, 0));
assert_se(c);
assert_se(n_c == n_before);
assert_se(MALLOC_ELEMENTSOF(c) >= n_c);
assert_se(MALLOC_SIZEOF_SAFE(c) >= n_c * sizeof(int));
assert_se(memcmp_safe(c, before, n_c) == 0);
}
for (j = 0; j < i * n_from; j++)
assert_se(c[j] == (int) j);
}
TEST(memdup_multiply_and_greedy_realloc) {

View file

@ -699,13 +699,22 @@ TEST(tpm_required_tests) {
assert_se(tpm2_test_parms(c, TPM2_ALG_SYMCIPHER, &parms));
/* Test invalid algs */
assert_se(tpm2_supports_alg(c, TPM2_ALG_ERROR) == 0);
assert_se(tpm2_supports_alg(c, TPM2_ALG_LAST + 1) == 0);
assert_se(!tpm2_supports_alg(c, TPM2_ALG_ERROR));
assert_se(!tpm2_supports_alg(c, TPM2_ALG_LAST + 1));
/* Test valid algs */
assert_se(tpm2_supports_alg(c, TPM2_ALG_RSA) == 1);
assert_se(tpm2_supports_alg(c, TPM2_ALG_AES) == 1);
assert_se(tpm2_supports_alg(c, TPM2_ALG_CFB) == 1);
assert_se(tpm2_supports_alg(c, TPM2_ALG_RSA));
assert_se(tpm2_supports_alg(c, TPM2_ALG_AES));
assert_se(tpm2_supports_alg(c, TPM2_ALG_CFB));
/* Test invalid commands */
assert_se(!tpm2_supports_command(c, TPM2_CC_FIRST - 1));
assert_se(!tpm2_supports_command(c, TPM2_CC_LAST + 1));
/* Test valid commands */
assert_se(tpm2_supports_command(c, TPM2_CC_Create));
assert_se(tpm2_supports_command(c, TPM2_CC_CreatePrimary));
assert_se(tpm2_supports_command(c, TPM2_CC_Unseal));
}
#endif /* HAVE_TPM2 */