Skip to content

Commit

Permalink
Fix buffer overflow due to incorrect nv_endsubscript() calls
Browse files Browse the repository at this point in the history
The nv_endsubscript() function finds the end of an array subscript
and returns a pointer to the character following it. But this only
works correctly if the 'cp' pointer passed points to the initial
'[' of the subscript, not to any subsequente character. Otherwise,
backslash quotes may mess it up and a pointer past the character
following the subscript may be returned, causing potential buffer
overflows. (This manifested in some regression test failures in
tests/nameref.sh when run with a ksh compiled with ASan.)

These fixes were found by adding an assert(*cp=='[') at the
beginning of nv_subscript() and running the regression tests.

src/cmd/ksh93/sh/arith.c, src/cmd/ksh93/sh/name.c:
- Correct two erroneous nv_subscript() calls found by that assert
  by adjusting the cp parameter to point to the initial '['.
  • Loading branch information
McDutchie committed Nov 30, 2024
1 parent 70a0032 commit c27edcc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2024-11-29" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2024-11-30" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK

/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/ksh93/sh/arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ static Namval_t *scope(Namval_t *np,struct lval *lvalue,int assign)
while(c=mbchar(cp),isaname(c));
}
if(c=='[')
{
cp--;
continue;
}
}
flag = *cp;
*cp = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -3067,7 +3067,7 @@ static char *lastdot(char *cp, int eq)
if(*cp==']')
cp++;
else
cp = nv_endsubscript(NULL,ep=cp,0);
cp = nv_endsubscript(NULL,(ep=cp)-1,0);
}
else if(c=='.')
{
Expand Down

0 comments on commit c27edcc

Please sign in to comment.