Pull keys fixes from Jarkko Sakkinen: - An unprivileged keyring whose keys collide through the description-chunk path can drive assoc_array node splitting into an out-of-bounds slot write. Fix it. - Fix the DCP trusted keys backend * tag 'for-next-keys-7.2-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: assoc_array: trim the final shortcut word using the current chunk end keys: make keyring key-chunk byte order agree with keyring_diff_objects() keys: fix out-of-bounds read in keyring_get_key_chunk() KEYS: trusted: dcp: fix key_len validation and calc_blob_len() return type
Unified · Split
| @@ -255,7 +255,8 @@ follow_shortcut: · 254 unmodified lines | ||
| 255 | 255 | sc_segments = shortcut->index_key[sc_level >> ASSOC_ARRAY_KEY_CHUNK_SHIFT]; |
| 256 | 256 | dissimilarity = segments ^ sc_segments; |
| 257 | 257 | |
| 258 | − if (round_up(sc_level, ASSOC_ARRAY_KEY_CHUNK_SIZE) > shortcut->skip_to_level) { | |
| 258 | + if (shortcut->skip_to_level < round_down(sc_level, | |
| 259 | + ASSOC_ARRAY_KEY_CHUNK_SIZE) + ASSOC_ARRAY_KEY_CHUNK_SIZE) { | |
| 259 | 260 | /* Trim segments that are beyond the shortcut */ |
| 260 | 261 | int shift = shortcut->skip_to_level & ASSOC_ARRAY_KEY_CHUNK_MASK; |
| 261 | 262 | dissimilarity &= ~(ULONG_MAX << shift); |
| @@ -271,6 +271,7 @@ static unsigned long keyring_get_key_chunk(const void *data, int level) · 270 unmodified lines | ||
| 271 | 271 | unsigned long chunk = 0; |
| 272 | 272 | const u8 *d; |
| 273 | 273 | int desc_len = index_key->desc_len, n = sizeof(chunk); |
| 274 | + unsigned int offset; | |
| 274 | 275 | |
| 275 | 276 | level /= ASSOC_ARRAY_KEY_CHUNK_SIZE; |
| 276 | 277 | switch (level) { |
| @@ -284,17 +285,18 @@ static unsigned long keyring_get_key_chunk(const void *data, int level) · 7 unmodified lines | ||
| 284 | 285 | return (unsigned long)index_key->domain_tag; |
| 285 | 286 | default: |
| 286 | 287 | level -= 4; |
| 287 | − if (desc_len <= sizeof(index_key->desc)) | |
| 288 | + offset = sizeof(index_key->desc) + level * sizeof(long); | |
| 289 | + if (desc_len <= offset) | |
| 288 | 290 | return 0; |
| 289 | 291 | |
| 290 | − d = index_key->description + sizeof(index_key->desc); | |
| 291 | − d += level * sizeof(long); | |
| 292 | − desc_len -= sizeof(index_key->desc); | |
| 292 | + d = index_key->description + offset; | |
| 293 | + desc_len -= offset; | |
| 293 | 294 | if (desc_len > n) |
| 294 | 295 | desc_len = n; |
| 296 | + d += desc_len; | |
| 295 | 297 | do { |
| 296 | 298 | chunk <<= 8; |
| 297 | − chunk |= *d++; | |
| 299 | + chunk |= *--d; | |
| 298 | 300 | } while (--desc_len > 0); |
| 299 | 301 | return chunk; |
| 300 | 302 | } |
| @@ -375,7 +377,7 @@ same: · 74 unmodified lines | ||
| 375 | 377 | return -1; |
| 376 | 378 | |
| 377 | 379 | differ_plus_i: |
| 378 | − level += i; | |
| 380 | + level += i - (int)sizeof(a->desc); | |
| 379 | 381 | differ: |
| 380 | 382 | i = level * 8 + __ffs(seg_a ^ seg_b); |
| 381 | 383 | return i; |
| @@ -69,7 +69,7 @@ static bool skip_zk_test; · 68 unmodified lines | ||
| 69 | 69 | module_param_named(dcp_skip_zk_test, skip_zk_test, bool, 0); |
| 70 | 70 | MODULE_PARM_DESC(dcp_skip_zk_test, "Don't test whether device keys are zero'ed"); |
| 71 | 71 | |
| 72 | −static unsigned int calc_blob_len(unsigned int payload_len) | |
| 72 | +static size_t calc_blob_len(unsigned int payload_len) | |
| 73 | 73 | { |
| 74 | 74 | return sizeof(struct dcp_blob_fmt) + payload_len + DCP_BLOB_AUTHLEN; |
| 75 | 75 | } |
| @@ -200,7 +200,8 @@ static int encrypt_blob_key(u8 *plain_key, u8 *encrypted_key) · 124 unmodified lines | ||
| 200 | 200 | static int trusted_dcp_seal(struct trusted_key_payload *p, char *datablob) |
| 201 | 201 | { |
| 202 | 202 | struct dcp_blob_fmt *b = (struct dcp_blob_fmt *)p->blob; |
| 203 | − int blen, ret; | |
| 203 | + size_t blen; | |
| 204 | + int ret; | |
| 204 | 205 | u8 *plain_blob_key; |
| 205 | 206 | |
| 206 | 207 | blen = calc_blob_len(p->key_len); |
| @@ -242,7 +243,8 @@ out: · 35 unmodified lines | ||
| 242 | 243 | static int trusted_dcp_unseal(struct trusted_key_payload *p, char *datablob) |
| 243 | 244 | { |
| 244 | 245 | struct dcp_blob_fmt *b = (struct dcp_blob_fmt *)p->blob; |
| 245 | − int blen, ret; | |
| 246 | + size_t blen; | |
| 247 | + int ret; | |
| 246 | 248 | u8 *plain_blob_key = NULL; |
| 247 | 249 | |
| 248 | 250 | if (b->fmt_version != DCP_BLOB_VERSION) { |
| @@ -253,9 +255,14 @@ static int trusted_dcp_unseal(struct trusted_key_payload *p, char *datablob) · 4 unmodified lines | ||
| 253 | 255 | } |
| 254 | 256 | |
| 255 | 257 | p->key_len = le32_to_cpu(b->payload_len); |
| 258 | + if (p->key_len < MIN_KEY_SIZE || p->key_len > MAX_KEY_SIZE) { | |
| 259 | + ret = -EINVAL; | |
| 260 | + goto out; | |
| 261 | + } | |
| 262 | + | |
| 256 | 263 | blen = calc_blob_len(p->key_len); |
| 257 | 264 | if (blen != p->blob_len) { |
| 258 | − pr_err("DCP blob has bad length: %i != %i\n", blen, | |
| 265 | + pr_err("DCP blob has bad length: %zu != %u\n", blen, | |
| 259 | 266 | p->blob_len); |
| 260 | 267 | ret = -EINVAL; |
| 261 | 268 | goto out; |