Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16866 > unrolled thread

Re: Incorrect / Inconsistent behavior with nameref assignments in functions

Started byGreg Wooledge <wooledg@eeg.ccf.org>
First post2020-08-30 10:50 -0400
Last post2020-08-30 10:50 -0400
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Incorrect / Inconsistent behavior with nameref assignments in functions Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-30 10:50 -0400

#16866 — Re: Incorrect / Inconsistent behavior with nameref assignments in functions

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2020-08-30 10:50 -0400
SubjectRe: Incorrect / Inconsistent behavior with nameref assignments in functions
Message-ID<mailman.1776.1598799074.2469.bug-bash@gnu.org>
On Sun, Aug 30, 2020 at 12:24:03PM +0200, Binarus wrote:
> On 30.08.2020 02:59, Koichi Murase wrote:
> > * Another way is to copy to the local array only when the name is
> >   different from `myArray':
> > 
> >   function Dummy {
> >     [[ $1 == myArray ]] ||
> >       eval "local -a myArray=(\"\${$1[@]}\")"
> >     declare -p myArray
> >   }
> 
> Thank you very much for that idea!
> 
> However, eval is evil. If I ever had to provide that function to other
> users (which currently is not the case), then I would have a problem if
> another user would call it like that:
> 
> declare -a -i myArray1=('1' '2' '3')
> Dummy 'myArray1[@]}"); echo Gotcha!; #'
> 
> Output:
> 
> root@cerberus:~/scripts# ./test6
> Gotcha!
> declare -a myArray=([0]="1" [1]="2" [2]="3")

The evil thing here is code injection.  Obviously eval is one way to
perform code injection, but it's not the *only* way.  Eval itself isn't
evil; if anything, it's all of the other forms of code injection,
which people don't suspect, that are truly insidious.

https://mywiki.wooledge.org/CodeInjection
https://mywiki.wooledge.org/BashWeaknesses

You're trying to do something that you feel should be possible -- passing
an array to a function by reference.  Every other language can do this,
right?  So bash should be able to do this... right?  Nope.

Passing variables by reference (especially arrays) is one of the
major missing features of bash.  Everyone wants it.  Many, many people
have attempted it.  The sheer insanity of some of the attempts is
astounding.

https://fvue.nl/wiki/Bash:_Passing_variables_by_reference

That's a slightly older page, but he found an exploit in "unset" which
does bizarre things when called at different function scope levels, and
managed to use it to manipulate the existence of variables at various
function scopes.

If you absolutely *need* to pass a variable by reference, don't use bash.
That's the best advice I can give you.

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web