efi_loader: fix get_last_capsule()
PATCH 1/1 efi_loader: fix get_last_capsule()
fix get_last_capsule() leads to writes beyond the stack allocated buffer. This was indicated when enabling the stack protector.
utf16_utf8_strcpy() only stops copying when reaching '\0'. The current invocation always writes beyond the end of value[].
The output length of utf16_utf8_strcpy() may be longer than the number of UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15 UTF-8 tokens. Hence, using utf16_utf8_strcpy() without checking the input may lead to further writes beyond value[].
The current invocation of strict_strtoul() reads beyond the end of value[].
A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must result in an error. We cat catch this by checking the return value of strict_strtoul().
A value that is too short after "Capsule" (e.g. "Capsule0") must result in an error. We must check the string length of value[].
--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
lib/efi_loader/efi_capsule.c +13 -5
@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret =efi_get_variable_int (L"CapsuleLast" , &efi_guid_capsule_report,NULL , &size, value16,NULL ); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) +if (ret !=EFI_SUCCESS || size !=22 || +u16_strncmp (value16,L"Capsule" ,7 )) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ]; - p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err; + value[i] = c; + } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; }
--
2.30.0
PATCH 1/1 efi_loader: fix get_last_capsule()
Quoted 8 lines · Heinrich Schuchardt
fix
get_last_capsule()leads to writes beyond the stack allocated buffer. This was indicated when enabling the stack protector.
utf16_utf8_strcpy()only stops copying when reaching '\0'. The current invocation always writes beyond the end of value[].The output length of
utf16_utf8_strcpy()may be longer than the number of UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15 UTF-8
First of all, "CapsuleLast" is a read-only variable from user's viewpoint, and is maintained solely by efi code. Then its value is expected to always be sane.
The case you suggested above is quite unlikely.
Although I don't think we need this patch,
Quoted 36 lines
tokens. Hence, using
utf16_utf8_strcpy()without checking the input may lead to further writes beyond value[].The current invocation of
strict_strtoul()reads beyond the end of value[].A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must result in an error. We cat catch this by checking the return value of
strict_strtoul().A value that is too short after "Capsule" (e.g. "Capsule0") must result in an error. We must check the string length of value[].
Signed-off-by Heinrich Schuchardt <[email protected]>--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)lib/efi_loader/efi_capsule.c +3
-2@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret =efi_get_variable_int (L"CapsuleLast" , &efi_guid_capsule_report,NULL , &size, value16,NULL ); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) + if (ret != EFI_SUCCESS || size != 22 ||
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 61
Quoted 10 lines
lib/efi_loader/efi_capsule.c +5
-3+ u16_strncmp(value16, L"Capsule", 7)) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ]; - p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err;
Is this check necessary assuming size == 22?
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 62
Quoted 1 line
lib/efi_loader/efi_capsule.c +1
-0+ value[i] = c;
You are implicitly casting the value from u16 to u8 here. This may lead to making an illegal code legal.
-Takahiro Akashi
Quoted 10 lines
lib/efi_loader/efi_capsule.c +4
-1+ } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; } --2.30.0
PATCH 1/1 efi_loader: fix get_last_capsule()
Am 10. Februar 2021 01:38:38 MEZ schrieb AKASHI Takahiro <[email protected]>:
Quoted 12 lines · Heinrich Schuchardt
Quoted 1 line
fix
get_last_capsule()leads to writes beyond the stack allocatedbuffer.
Quoted 3 lines
This was indicated when enabling the stack protector.
utf16_utf8_strcpy()only stops copying when reaching '\0'. Thecurrent
Quoted 3 lines
invocation always writes beyond the end of value[].
The output length of
utf16_utf8_strcpy()may be longer than thenumber of
Quoted 1 line
UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15
Quoted 8 lines
UTF-8
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
The case you suggested above is quite unlikely.
What happens if the user tries to create a variable of the same name, e.g. in the variable store on disk?
Quoted 2 lines
Although I don't think we need this patch,
Why do you think the patch is not necessary given that the current code is writing ouside buffers?
Quoted 49 lines
Quoted 2 lines
tokens. Hence, using
utf16_utf8_strcpy()without checking the inputmay
Quoted 3 lines
lead to further writes beyond value[].
The current invocation of
strict_strtoul()reads beyond the end ofvalue[].
Quoted 2 lines
A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must
result in
Quoted 1 line
an error. We cat catch this by checking the return value of
strict_strtoul().Quoted 2 lines
A value that is too short after "Capsule" (e.g. "Capsule0") must
result in
Review
Quoted 8 lines
an error. We must check the string length of value[].
Signed-off-by Heinrich Schuchardt <[email protected]>--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/lib/efi_loader/efi_capsule.cQuoted 6 lines
index
d39d731080..0017f0c0db100644lib/efi_loader/efi_capsule.c +0
-0@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 52
Quoted 10 lines
lib/efi_loader/efi_capsule.c +2
-1u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret = efi_get_variable_int(L"CapsuleLast",
&efi_guid_capsule_report,Quoted 8 lines
lib/efi_loader/efi_capsule.c +4
-1NULL, &size, value16, NULL); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) +if (ret !=EFI_SUCCESS || size !=22 || +u16_strncmp (value16,L"Capsule" ,7 )) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ];
Quoted 7 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 61
Quoted 5 lines
lib/efi_loader/efi_capsule.c +2
-3- p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err;Is this check necessary assuming size == 22?
Ok. Instead we should check for > 0x7f here to avoid illegal codes.
Best regards
Heinrich
Quoted 19 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 62
Quoted 3 lines
lib/efi_loader/efi_capsule.c +1
-0+ value[i] = c;You are implicitly casting the value from u16 to u8 here. This may lead to making an illegal code legal.
-Takahiro Akashi
Quoted 10 lines
lib/efi_loader/efi_capsule.c +4
-1+ } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; } --2.30.0
PATCH 1/1 efi_loader: fix get_last_capsule()
Quoted 15 lines · Heinrich Schuchardt
Am 10. Februar 2021 01:38:38 MEZ schrieb AKASHI Takahiro <[email protected]>:
Quoted 14 lines · Heinrich Schuchardt
Quoted 1 line
fix
get_last_capsule()leads to writes beyond the stack allocatedbuffer.
Quoted 3 lines
This was indicated when enabling the stack protector.
utf16_utf8_strcpy()only stops copying when reaching '\0'. Thecurrent
Quoted 3 lines
invocation always writes beyond the end of value[].
The output length of
utf16_utf8_strcpy()may be longer than thenumber of
Quoted 1 line
UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15
UTF-8
Quoted 8 lines
Quoted 6 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
The case you suggested above is quite unlikely.What happens if the user tries to create a variable of the same name, e.g. in the variable store on disk?
If this is your concern, the best solution would be to prohibit users to create system-managed (read-only) variables, like CapsuleLast.
If a user can create such a variable with an arbitrary value, it will hurt the system's integrity.
Quoted 5 lines
Quoted 3 lines
Although I don't think we need this patch,
Why do you think the patch is not necessary given that the current code is writing ouside buffers?
My assumption here is, as I said,
Quoted 5 lines
Quoted 5 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
-Takahiro Akashi
Quoted 83 lines
Quoted 49 lines
Quoted 2 lines
tokens. Hence, using
utf16_utf8_strcpy()without checking the inputmay
Quoted 3 lines
lead to further writes beyond value[].
The current invocation of
strict_strtoul()reads beyond the end ofvalue[].
Quoted 2 lines
A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must
result in
Quoted 1 line
an error. We cat catch this by checking the return value of
strict_strtoul().Quoted 2 lines
A value that is too short after "Capsule" (e.g. "Capsule0") must
result in
Review
Quoted 8 lines
an error. We must check the string length of value[].
Signed-off-by Heinrich Schuchardt <[email protected]>--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/lib/efi_loader/efi_capsule.cQuoted 6 lines
index
d39d731080..0017f0c0db100644lib/efi_loader/efi_capsule.c +0
-0@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 52
Quoted 10 lines
lib/efi_loader/efi_capsule.c +2
-1u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret = efi_get_variable_int(L"CapsuleLast",
&efi_guid_capsule_report,Quoted 8 lines
lib/efi_loader/efi_capsule.c +4
-1NULL, &size, value16, NULL); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) +if (ret !=EFI_SUCCESS || size !=22 || +u16_strncmp (value16,L"Capsule" ,7 )) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ];Quoted 7 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 61
Quoted 5 lines
lib/efi_loader/efi_capsule.c +2
-3- p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err;Is this check necessary assuming size == 22?
Ok. Instead we should check for >
0x7fhere to avoid illegal codes.Best regards
Heinrich
Quoted 19 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 62
Quoted 3 lines
lib/efi_loader/efi_capsule.c +1
-0+ value[i] = c;You are implicitly casting the value from u16 to u8 here. This may lead to making an illegal code legal.
-Takahiro Akashi
Quoted 10 lines
lib/efi_loader/efi_capsule.c +4
-1+ } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; } --2.30.0
PATCH 1/1 efi_loader: fix get_last_capsule()
Am 10. Februar 2021 07:43:25 MEZ schrieb AKASHI Takahiro <[email protected]>:
Quoted 35 lines · Heinrich Schuchardt
Quoted 1 line
Am 10. Februar 2021 01:38:38 MEZ schrieb AKASHI Takahiro
Quoted 14 lines
Quoted 14 lines · Heinrich Schuchardt
Quoted 1 line
fix
get_last_capsule()leads to writes beyond the stack allocatedbuffer.
Quoted 3 lines
This was indicated when enabling the stack protector.
utf16_utf8_strcpy()only stops copying when reaching '\0'. Thecurrent
Quoted 3 lines
invocation always writes beyond the end of value[].
The output length of
utf16_utf8_strcpy()may be longer than thenumber of
Quoted 1 line
UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15
UTF-8
Quoted 8 lines
Quoted 6 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
The case you suggested above is quite unlikely.What happens if the user tries to create a variable of the same name,
e.g. in the variable store on disk?
If this is your concern, the best solution would be to prohibit users to create system-managed (read-only) variables, like CapsuleLast.
If a user can create such a variable with an arbitrary value, it will hurt the system's integrity.
Quoted 2 lines
Quoted 2 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
Although I don't think we need this patch,
Why do you think the patch is not necessary given that the current
code is writing ouside buffers?
My assumption here is, as I said,
Quoted 5 lines
Quoted 5 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
Even if the value is sane, the current code overwrites the stack of the caller? This becomes visible when the stack protector is enabled. Why don't you want to fix it?
Best regards
Heinrich
Quoted 88 lines
-Takahiro Akashi
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
tokens. Hence, using
utf16_utf8_strcpy()without checking theinput
Quoted 81 lines
Quoted 47 lines
may
Quoted 3 lines
lead to further writes beyond value[].
The current invocation of
strict_strtoul()reads beyond the end ofvalue[].
Quoted 2 lines
A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must
result in
Quoted 1 line
an error. We cat catch this by checking the return value of
strict_strtoul().Quoted 2 lines
A value that is too short after "Capsule" (e.g. "Capsule0") must
result in
Review
Quoted 8 lines
an error. We must check the string length of value[].
Signed-off-by Heinrich Schuchardt <[email protected]>--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/lib/efi_loader/efi_capsule.cQuoted 6 lines
index
d39d731080..0017f0c0db100644lib/efi_loader/efi_capsule.c +0
-0@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 52
Quoted 10 lines
lib/efi_loader/efi_capsule.c +2
-1u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret = efi_get_variable_int(L"CapsuleLast",
&efi_guid_capsule_report,Quoted 8 lines
lib/efi_loader/efi_capsule.c +4
-1NULL, &size, value16, NULL); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) +if (ret !=EFI_SUCCESS || size !=22 || +u16_strncmp (value16,L"Capsule" ,7 )) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ];Quoted 7 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 61
Quoted 5 lines
lib/efi_loader/efi_capsule.c +2
-3- p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err;Is this check necessary assuming size == 22?
Ok. Instead we should check for >
0x7fhere to avoid illegal codes.Best regards
Heinrich
Quoted 19 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 62
Quoted 3 lines
lib/efi_loader/efi_capsule.c +1
-0+ value[i] = c;You are implicitly casting the value from u16 to u8 here. This may lead to making an illegal code legal.
-Takahiro Akashi
Quoted 10 lines
lib/efi_loader/efi_capsule.c +4
-1+ } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; } --2.30.0
PATCH 1/1 efi_loader: fix get_last_capsule()
In reply to AKASHI TakahiroAm 10. Februar 2021 07:43:25 MEZ schrieb AKASHI Takahiro <[email protected]>:
Quoted 47 lines · Heinrich Schuchardt
Quoted 1 line
Am 10. Februar 2021 01:38:38 MEZ schrieb AKASHI Takahiro
Quoted 14 lines
Quoted 14 lines · Heinrich Schuchardt
Quoted 1 line
fix
get_last_capsule()leads to writes beyond the stack allocatedbuffer.
Quoted 3 lines
This was indicated when enabling the stack protector.
utf16_utf8_strcpy()only stops copying when reaching '\0'. Thecurrent
Quoted 3 lines
invocation always writes beyond the end of value[].
The output length of
utf16_utf8_strcpy()may be longer than thenumber of
Quoted 1 line
UTF-16 tokens. E.g has "Capsule????" has 11 UTF-16 tokens but 15
UTF-8
Quoted 8 lines
Quoted 6 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
The case you suggested above is quite unlikely.What happens if the user tries to create a variable of the same name,
e.g. in the variable store on disk?
If this is your concern, the best solution would be to prohibit users to create system-managed (read-only) variables, like CapsuleLast.
If a user can create such a variable with an arbitrary value, it will hurt the system's integrity.
Quoted 5 lines
Quoted 3 lines
Although I don't think we need this patch,
Why do you think the patch is not necessary given that the current
code is writing ouside buffers?
My assumption here is, as I said,
Quoted 5 lines
Quoted 5 lines
First of all, "CapsuleLast" is a read-only variable from user's viewpoint,
and is maintained solely by efi code. Then its value is expected to always
be sane.
Quoted 60 lines
-Takahiro Akashi
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
tokens. Hence, using
utf16_utf8_strcpy()without checking theinput
Quoted 54 lines
Quoted 54 lines
may
Quoted 3 lines
lead to further writes beyond value[].
The current invocation of
strict_strtoul()reads beyond the end ofvalue[].
Quoted 2 lines
A non-hexadecimal value after "Capsule" (e.g. "CapsuleZZZZ") must
result in
Quoted 1 line
an error. We cat catch this by checking the return value of
strict_strtoul().Quoted 2 lines
A value that is too short after "Capsule" (e.g. "Capsule0") must
result in
Review
Quoted 8 lines
an error. We must check the string length of value[].
Signed-off-by Heinrich Schuchardt <[email protected]>--- lib/efi_loader/efi_capsule.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/lib/efi_loader/efi_capsule.cQuoted 6 lines
index
d39d731080..0017f0c0db100644lib/efi_loader/efi_capsule.c +0
-0@@ -42,20 +42,28 @@ static struct efi_file_handle *bootdev_root;static __maybe_unused unsigned int get_last_capsule (void ) {lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 52
Quoted 10 lines
lib/efi_loader/efi_capsule.c +2
-1u16 value16[11 ];/* "CapsuleXXXX": non-null-terminated */ -char value[11 ], *p; +char value[5 ];efi_uintn_t size;unsigned long index =0xffff ;efi_status_t ret; +int i; size =sizeof (value16); ret = efi_get_variable_int(L"CapsuleLast",
&efi_guid_capsule_report,lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 61
Quoted 13 lines
lib/efi_loader/efi_capsule.c +6
-4NULL, &size, value16, NULL); -if (ret !=EFI_SUCCESS ||u16_strncmp (value16,L"Capsule" ,7 )) +if (ret !=EFI_SUCCESS || size !=22 || +u16_strncmp (value16,L"Capsule" ,7 )) goto err; +for (i =0 ; i <4 ; ++i) { +u16 c = value16[i +7 ]; - p = value; -utf16_utf8_strcpy (&p, value16); -strict_strtoul (&value[7 ],16 , &index); +if (!c) + goto err;Is this check necessary assuming size == 22?
The value of bytes 19-22 could be zero.
Quoted 27 lines
Quoted 27 lines
Ok. Instead we should check for >
0x7fhere to avoid illegal codes.Best regards
Heinrich
Quoted 19 lines
lib/efi_loader/efi_capsule.c static struct efi_file_handle *bootdev_root; · line 62
Quoted 3 lines
lib/efi_loader/efi_capsule.c +1
-0+ value[i] = c;You are implicitly casting the value from u16 to u8 here. This may lead to making an illegal code legal.
-Takahiro Akashi
Quoted 10 lines
lib/efi_loader/efi_capsule.c +4
-1+ } + value[4 ] =0 ; +if (strict_strtoul (value,16 , &index)) + index =0xffff ; err:return index; } --2.30.0