mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
Hardening of readonly variables (#239)
Ksh currently restricts readonly scalar variables from having their
values directly changed via a value assignment. However, since ksh
allows variable attributes to be altered, the variable's value can
be indirectly altered. For instance, if TMOUT=900 (for a 15 minute
idle timeout) was set to readonly, all that is needed to alter the
value of TMOUT from 900 to 0 is to issue 'typeset -R1 TMOUT',
perhaps followed by a 'typeset -i TMOUT' to turn off the shell's
timeout value.
In addition, there are problems with arrays. The following is
incorrectly allowed:
typeset -a arr=((a b c) 1)
readonly arr
arr[0][1]=d
arr=(alphas=(a b c);name=x)
readonly arr.alphas
arr.alphas[1]=([b]=5)
arr=(alphas=(a b c);name=x)
readonly arr.alphas
arr.alphas[1]=(b)
typeset -C arr=(typeset -r -a alphas=(a b c);name=x)
arr.alphas[1]=()
src/cmd/ksh93/bltins/typeset.c: setall():
- Relocate readonly attribute check higher up the code and widen
its application to issue an error message if the pre-existing
name-pair has the readonly bit flag set.
- To avoid compatibility problems, don't check for readonly if
NV_RDONLY is the only attribute set (ignoring NV_NOFREE). This
allows 'readonly foo; readonly foo' to keep working.
src/cmd/ksh93/sh/array.c: nv_endsubscript():
- Apply a readonly flag check when an array subscript or append
assignment occurs, but allow type variables (typeset -T) as they
utilize '-r' for 'required' sub-variables.
src/cmd/ksh93/tests/readonly.sh:
- New file. Create readonly tests that validate the warning message
and validate that the readonly variable did not change.
src/cmd/ksh93/sh/streval.c:
- Bump MAXLEVEL from 9 to 1024 as a workaround for arithmetic
expansion, avoiding a spurious error about too much recursion
when the readonly.sh tests are run. This change is backported
from ksh 93v-.
TODO: debug a spurious increase in arithmetic recursion level
variable when readonly.sh tests with 'typeset -i' are run.
That is a different bug for a different commit.
Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
parent
c4f980eb29
commit
264ba48bdd
6 changed files with 357 additions and 9 deletions
|
|
@ -732,6 +732,11 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp
|
|||
np = nv_open(name,troot,nvflags|((nvflags&NV_ASSIGN)?0:NV_ARRAY)|((iarray|(nvflags&(NV_REF|NV_NOADD)==NV_REF))?NV_FARRAY:0));
|
||||
if(!np)
|
||||
continue;
|
||||
if(np->nvflag&NV_RDONLY && !tp->pflag && (flag & ~NV_NOFREE) != NV_RDONLY)
|
||||
{
|
||||
errormsg(SH_DICT,ERROR_exit(1),e_readonly,nv_name(np));
|
||||
UNREACHABLE();
|
||||
}
|
||||
if(nv_isnull(np) && !nv_isarray(np) && nv_isattr(np,NV_NOFREE))
|
||||
nv_offattr(np,NV_NOFREE);
|
||||
else if(tp->tp && !nv_isattr(np,NV_MINIMAL|NV_EXPORT) && (mp=(Namval_t*)np->nvenv) && (ap=nv_arrayptr(mp)) && (ap->nelem&ARRAY_TREE))
|
||||
|
|
@ -889,14 +894,7 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp
|
|||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if((flag&NV_RDONLY) && (curflag&NV_RDONLY))
|
||||
{
|
||||
errormsg(SH_DICT,ERROR_exit(1),e_readonly,nv_name(np));
|
||||
UNREACHABLE();
|
||||
}
|
||||
newflag = curflag & ~flag;
|
||||
}
|
||||
if (tp->aflag && (tp->argnum || (curflag!=newflag)))
|
||||
{
|
||||
if(shp->subshell)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue