Questioning the wisdom of GetEnvironmentValue()

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
mi
Posts: 123
Joined: 2005-01-25T14:14:43-07:00
Contact:

Questioning the wisdom of GetEnvironmentValue()

Post by mi »

Why does the function exist? Why not simply call getenv() -- an API, which has existed since forever?

If the call needs to be wrapped, why bother returning a malloc-ed copy of the string (which will then need to be free-ed) -- the value can't change once the process is running (except by the process itself)...

So, what's the purpose? Thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Questioning the wisdom of GetEnvironmentValue()

Post by magick »

Secure programming principles require that we don't rely on the value returned by getenv(). ImageMagick is an API. The calling program could, for example, change an environment variable between the time we get the value and when it is accessed. To be safe we immediately save the value to a local string variable.
User avatar
mi
Posts: 123
Joined: 2005-01-25T14:14:43-07:00
Contact:

Re: Questioning the wisdom of GetEnvironmentValue()

Post by mi »

magick wrote:Secure programming principles require that we don't rely on the value returned by getenv().
Mmm, you must be referring to a principle, I'm unfamiliar with... Could you elaborate? Why is it insecure to rely on the value returned by getenv()?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Questioning the wisdom of GetEnvironmentValue()

Post by magick »

See https://www.securecoding.cert.org/confl ... ment+(ENV) . Select ENV00-A. Do not store the pointer to the string returned by getenv(). In summary it says: make a copy of the referenced string returned by getenv() so that this copy may be safely referenced at a later time.
Post Reply