Skip to content

Commit 4b708b7

Browse files
hungtegregkh
authored andcommitted
firmware: google: check if size is valid when decoding VPD data
The VPD implementation from Chromium Vital Product Data project used to parse data from untrusted input without checking if the meta data is invalid or corrupted. For example, the size from decoded content may be negative value, or larger than whole input buffer. Such invalid data may cause buffer overflow. To fix that, the size parameters passed to vpd_decode functions should be changed to unsigned integer (u32) type, and the parsing of entry header should be refactored so every size field is correctly verified before starting to decode. Fixes: ad2ac9d ("firmware: Google VPD: import lib_vpd source files") Signed-off-by: Hung-Te Lin <hungte@chromium.org> Cc: stable <stable@vger.kernel.org> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Reviewed-by: Stephen Boyd <swboyd@chromium.org> Link: https://lore.kernel.org/r/20190830022402.214442-1-hungte@chromium.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8619e5b commit 4b708b7

3 files changed

Lines changed: 37 additions & 28 deletions

File tree

‎drivers/firmware/google/vpd.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ static int vpd_section_check_key_name(const u8 *key, s32 key_len)
9292
return VPD_OK;
9393
}
9494

95-
static int vpd_section_attrib_add(const u8 *key, s32 key_len,
96-
const u8 *value, s32 value_len,
95+
static int vpd_section_attrib_add(const u8 *key, u32 key_len,
96+
const u8 *value, u32 value_len,
9797
void *arg)
9898
{
9999
int ret;

‎drivers/firmware/google/vpd_decode.c‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#include "vpd_decode.h"
1111

12-
static int vpd_decode_len(const s32 max_len, const u8 *in,
13-
s32 *length, s32 *decoded_len)
12+
static int vpd_decode_len(const u32 max_len, const u8 *in,
13+
u32 *length, u32 *decoded_len)
1414
{
1515
u8 more;
1616
int i = 0;
@@ -30,18 +30,39 @@ static int vpd_decode_len(const s32 max_len, const u8 *in,
3030
} while (more);
3131

3232
*decoded_len = i;
33+
return VPD_OK;
34+
}
35+
36+
static int vpd_decode_entry(const u32 max_len, const u8 *input_buf,
37+
u32 *_consumed, const u8 **entry, u32 *entry_len)
38+
{
39+
u32 decoded_len;
40+
u32 consumed = *_consumed;
41+
42+
if (vpd_decode_len(max_len - consumed, &input_buf[consumed],
43+
entry_len, &decoded_len) != VPD_OK)
44+
return VPD_FAIL;
45+
if (max_len - consumed < decoded_len)
46+
return VPD_FAIL;
47+
48+
consumed += decoded_len;
49+
*entry = input_buf + consumed;
50+
51+
/* entry_len is untrusted data and must be checked again. */
52+
if (max_len - consumed < *entry_len)
53+
return VPD_FAIL;
3354

55+
consumed += decoded_len;
56+
*_consumed = consumed;
3457
return VPD_OK;
3558
}
3659

37-
int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
60+
int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed,
3861
vpd_decode_callback callback, void *callback_arg)
3962
{
4063
int type;
41-
int res;
42-
s32 key_len;
43-
s32 value_len;
44-
s32 decoded_len;
64+
u32 key_len;
65+
u32 value_len;
4566
const u8 *key;
4667
const u8 *value;
4768

@@ -56,26 +77,14 @@ int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
5677
case VPD_TYPE_STRING:
5778
(*consumed)++;
5879

59-
/* key */
60-
res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
61-
&key_len, &decoded_len);
62-
if (res != VPD_OK || *consumed + decoded_len >= max_len)
80+
if (vpd_decode_entry(max_len, input_buf, consumed, &key,
81+
&key_len) != VPD_OK)
6382
return VPD_FAIL;
6483

65-
*consumed += decoded_len;
66-
key = &input_buf[*consumed];
67-
*consumed += key_len;
68-
69-
/* value */
70-
res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
71-
&value_len, &decoded_len);
72-
if (res != VPD_OK || *consumed + decoded_len > max_len)
84+
if (vpd_decode_entry(max_len, input_buf, consumed, &value,
85+
&value_len) != VPD_OK)
7386
return VPD_FAIL;
7487

75-
*consumed += decoded_len;
76-
value = &input_buf[*consumed];
77-
*consumed += value_len;
78-
7988
if (type == VPD_TYPE_STRING)
8089
return callback(key, key_len, value, value_len,
8190
callback_arg);

‎drivers/firmware/google/vpd_decode.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ enum {
2525
};
2626

2727
/* Callback for vpd_decode_string to invoke. */
28-
typedef int vpd_decode_callback(const u8 *key, s32 key_len,
29-
const u8 *value, s32 value_len,
28+
typedef int vpd_decode_callback(const u8 *key, u32 key_len,
29+
const u8 *value, u32 value_len,
3030
void *arg);
3131

3232
/*
@@ -44,7 +44,7 @@ typedef int vpd_decode_callback(const u8 *key, s32 key_len,
4444
* If one entry is successfully decoded, sends it to callback and returns the
4545
* result.
4646
*/
47-
int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
47+
int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed,
4848
vpd_decode_callback callback, void *callback_arg);
4949

5050
#endif /* __VPD_DECODE_H */

0 commit comments

Comments
 (0)