bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Recursively calling a bash script goes undetected and eats all syste


From: Diggory Hardy
Subject: Re: Recursively calling a bash script goes undetected and eats all system memory
Date: Fri, 10 Dec 2010 15:23:46 +0100
User-agent: KMail/1.13.5 (Linux/2.6.32-5-amd64; KDE/4.5.3; x86_64; ; )


On Friday 10 December 2010 Pierre Gaston wrote:
> On Fri, Dec 10, 2010 at 11:25 AM, Diggory Hardy <diggory.hardy@unibas.ch> 
> wrote:
> > On Thursday 09 December 2010 Pierre Gaston wrote:
> >> On Wed, Dec 8, 2010 at 11:15 AM, Diggory Hardy <diggory.hardy@unibas.ch> 
> >> wrote:
> >> > Hello,
> >> >
> >> > With a simple script such as that below, bash can enter an infinite loop 
> >> > of eating memory until the system is rendered unusable:
> >> >
> >> > #!/bin/bash
> >> > PATH=~
> >> > infinitely-recurse
> >> >
> >> > Save this as infinitely-recurse in your home directory and run - and 
> >> > make sure you kill it pretty quick. OK, so an obvious bug when put like 
> >> > this, though it bit me recently (mistakenly using PATH as an ordinary 
> >> > variable and having a script with the same name as a system program). 
> >> > Would it not be simple to add some kind of protection against this — say 
> >> > don't let a script call itself more than 100 times?
> >> >
> >> > Thanks,
> >> > Diggory
> >> >
> >> Well, I'm not a big fan of the technique, but out there I see a lot of
> >> wrapper scripts calling themselves to automatically restart an
> >> application.
> >>
> > Uh. Then over time it is legitimate to have a script recursively call 
> > itself a few thousand times with each instance still in memory?
> 
> Well they use exec to avoid that.
> 
> > The potential to grind the system to a complete halt is pretty serious 
> > though. Perhaps the ideal solution would be to have the kernel intervene 
> > before it starts thrashing memory, but that doesn't seem to happen.
> 
> Sure, but you can do that with pretty much any tools available.
> 
True. Sounds like a fix of sorts would be possible though, even if it isn't 
really bash's problem to fix:

What if bash were only to allow scripts to call themselves with exec? If bash 
didn't allow a recursive depth (not via exec) greater than 1000, I presume the 
vast majority of scripts would be unaffected by this, and those that are should 
either use exec instead or are doing something beyond what bash is supposed to 
do.

Or maybe I should just stop poking this problem at you; on the one hand 
software should be forgiving while on the other preventing every possible 
severe error is not easy.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]