Skip to content

Commit c72b730

Browse files
JohnoKingMcDutchie
authored andcommitted
Scope variables to subshell before setting their discipline (#811)
Currently, running the tilde.sh tests under ASan will fail with a use after free. The crash occurs because the discipline function is assigned before .sh.tilde is scoped to the currently active virtual subshell. After this, sh_subshell() frees the discipline function by calling nv_delete() upon subshell completion, but because of improper scoping, .sh.tilde in the parent subshell now has an np->nvfun which points to freed memory. (As a side note, I'll note that this bug can be reproduced for any variable assigned a discipline function, not just .sh.tilde.) src/cmd/ksh93/sh/xec.c: sh_exec(): - Use sh_assignok to scope variables to subshells before assigning a new discipline function to them.
1 parent a4c82ef commit c72b730

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

NEWS

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ This documents significant changes in the 1.0 branch of ksh 93u+m.
22
For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
33
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.
44

5+
2025-01-05:
6+
7+
- Fixed a crash that could occur if a discipline function was first assigned
8+
to a variable in a virtual subshell before the variable was scoped to that
9+
subshell, then upon subshell completion another discipline function of the
10+
same type was assigned to that selfsame variable in the parent shell.
11+
512
2025-01-03:
613

714
- The performance of virtual subshells has been significantly improved by

src/cmd/ksh93/include/version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <ast_release.h>
1919
#include "git.h"
2020

21-
#define SH_RELEASE_DATE "2025-01-03" /* must be in this format for $((.sh.version)) */
21+
#define SH_RELEASE_DATE "2025-01-05" /* must be in this format for $((.sh.version)) */
2222
/*
2323
* This comment keeps SH_RELEASE_DATE a few lines away from SH_RELEASE_SVER to avoid
2424
* merge conflicts when cherry-picking dev branch commits onto a release branch.

src/cmd/ksh93/sh/xec.c

+7
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,14 @@ int sh_exec(const Shnode_t *t, int flags)
23252325
if(npv)
23262326
{
23272327
if(!sh.mktype)
2328+
{ /*
2329+
* Set the discipline function. If this is done in a subshell, the variable
2330+
* must be scoped to the subshell before nvfun is set to the discipline.
2331+
*/
2332+
if(sh.subshell && !sh.subshare)
2333+
sh_assignok(npv, 1);
23282334
cp = nv_setdisc(npv,cp,np,(Namfun_t*)npv);
2335+
}
23292336
if(!cp)
23302337
{
23312338
errormsg(SH_DICT,ERROR_exit(1),e_baddisc,fname);

0 commit comments

Comments
 (0)