There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the lengh of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch simply limit the string copy to sizeof(prog) less 1 (space for \0).
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
strncpy(prog, argv[0], sizeof prog);
if (prog[sizeof prog - 1])
scream_bloody_murder();
Silently using the wrong data is a worse habit than not checking for
overflows ;-)
Segher
strncpy(prog, argv[0], sizeof prog);
if (prog[sizeof prog - 1])
scream_bloody_murder();
Silently using the wrong data is a worse habit than not checking for
overflows ;-)
Completely agree! Thanks for bringing this up.
If you don't mind, I would solve this problem slightly different, as it seems
to be more readable.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
+ fprintf(stderr, "Very big executable name: %s\n", argv[0]);
+ return 1;
+ }
+
+ strncpy(prog, argv[0], sizeof(prog) - 1);
return test_harness(dscr_inherit_exec, "dscr_inherit_exec_test");
strncpy(prog, argv[0], sizeof prog);
if (prog[sizeof prog - 1])
scream_bloody_murder();
Silently using the wrong data is a worse habit than not checking for
overflows ;-)
Completely agree! Thanks for bringing this up.
If you don't mind, I would solve this problem slightly different, as it seems
to be more readable.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
wouldn't it be better to use sizeof(prog) instead of LEN_MAX ?
From: Paul Clarke <hidden> Date: 2018-06-22 15:15:39
On 06/22/2018 09:43 AM, Breno Leitao wrote:
If you don't mind, I would solve this problem slightly different, as it seems
to be more readable.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
+ fprintf(stderr, "Very big executable name: %s\n", argv[0]);
"Very big" is an observation. "Too big" indicates a problem better. Or, more explicitly "Executable name is too long".
PC
From: Al Dunsmuir <hidden> Date: 2018-06-22 21:07:54
On Friday, June 22, 2018, 11:15:29 AM, Paul Clarke wrote:
On 06/22/2018 09:43 AM, Breno Leitao wrote:
quoted
If you don't mind, I would solve this problem slightly different, as it seems
to be more readable.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
+ fprintf(stderr, "Very big executable name: %s\n", argv[0]);
"Very big" is an observation. "Too big" indicates a problem
better. Or, more explicitly "Executable name is too long".
Or even better, display the limit that is being exceeded, in case that
value changes over time. Something like.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
+ fprintf(stderr, "Executable name exceeds limit (%d): %s\n",
+ LEN_MAX,
+ argv[0]);
strncpy(prog, argv[0], sizeof prog);
if (prog[sizeof prog - 1])
scream_bloody_murder();
Silently using the wrong data is a worse habit than not checking for
overflows ;-)
Completely agree! Thanks for bringing this up.
If you don't mind, I would solve this problem slightly different, as it seems
to be more readable.
- strncpy(prog, argv[0], strlen(argv[0]));
+ if (strlen(argv[0]) >= LEN_MAX){
+ fprintf(stderr, "Very big executable name: %s\n", argv[0]);
+ return 1;
+ }
+
+ strncpy(prog, argv[0], sizeof(prog) - 1);
The strlen reads all of argv[0], which can be very big in theory. It won't
matter in this test file -- program arguments cannot be super long, for one
thing -- but it's not a good idea in general (that is one of the problems
of strlcpy, btw).
Best of course is to avoid string length restrictions completely, if you can.
Segher
The strlen reads all of argv[0], which can be very big in theory. It won't
matter in this test file -- program arguments cannot be super long, for one
thing -- but it's not a good idea in general (that is one of the problems
of strlcpy, btw).
Best of course is to avoid string length restrictions completely, if you can.
Right, I was thinking about this problem and there is no motivation to have a
statically allocated and limited region.
I will send a v2 where 'prog' and avoid this restriction completely.
Thanks
There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch allocates 'prog' according to the argv[0] length, avoiding LEN_MAX
restriction.
CC: Segher Boessenkool <redacted>
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2018-06-26 05:24:14
Breno Leitao [off-list ref] writes:
quoted hunk
There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch allocates 'prog' according to the argv[0] length, avoiding LEN_MAX
restriction.
CC: Segher Boessenkool <redacted>
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch allocates 'prog' according to the argv[0] length, avoiding LEN_MAX
restriction.
CC: Segher Boessenkool <redacted>
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch maps 'prog' to the argv[0] memory region, removing the static
allocation and the LEN_MAX size restriction.
CC: Michael Ellerman <mpe@ellerman.id.au>
CC: Segher Boessenkool <redacted>
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Fix two typos in the file header. Replacing the word 'priviledged'
by 'privileged' and 'exuecuted' by 'executed'.
Signed-off-by: Breno Leitao <leitao@debian.org>
Signed-off-by: Gustavo Romero <redacted>
---
tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Michael Ellerman <hidden> Date: 2018-07-11 13:24:15
On Tue, 2018-06-26 at 13:20:12 UTC, Breno Leitao wrote:
There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).
This patch maps 'prog' to the argv[0] memory region, removing the static
allocation and the LEN_MAX size restriction.
CC: Michael Ellerman <mpe@ellerman.id.au>
CC: Segher Boessenkool <redacted>
CC: Anshuman Khandual <redacted>
Signed-off-by: Breno Leitao <leitao@debian.org>