shay has asked for the wisdom of the Perl Monks concerning the following question:

There is a little-known feature of perl on Win32 whereby PERL* environment variables can be looked up in the registry (first under HKCU\Software\Perl then under HKLM\Software\Perl) if they are not found in the environment.

(See more details here: http://www.nntp.perl.org/group/perl.par/2009/05/msg4055.html)

I have an Apache2.2/mod_perl2 setup which runs fine as it is, but can easily be broken if an inappropriate PERL5LIB setting is put in the registry. (With a suitably bogus setting in place, pointing at a completely different version of perl installed elsewhere on my machine, I find that the Apache service no longer starts: It fails to load startup.pl, presumably because it is loading C extension modules and tries to load the wrong versions of DLLs from the location specified in the bogus PERL5LIB setting.)

How can I modify my httpd.conf and/or startup.pl files to protect against this? I've tried adding SetEnv and PerlSetEnv directives in httpd.conf, and use lib qw() and BEGIN { $ENV{PERL5LIB}='...' } lines in startup.pl, but Apache still can't load startup.pl and fails to start.

I need to make the Apache/mod_perl setup override any possible PERL5LIB setting in the registry in a self-contained way, so that it can be installed on other machines and not be affected by those other machines having PERL5LIB settings in the registry.

  • Comment on Overriding the registry's PERL5LIB in mod_perl on Win32

Replies are listed 'Best First'.
Re: Overriding the registry's PERL5LIB in mod_perl on Win32
by perrin (Chancellor) on May 21, 2009 at 15:28 UTC
    Uh, you're asking how to protect against sabotage? I suppose you could write a wrapper script in another language that checks the registry before starting apache.
      Yes, I was coming round to that sort of idea myself.

      What it needs is something to set PERL5LIB in the environment (thus cloaking any nasty registry setting) before the LoadFile directive that loads perl510.dll, but there are no Apache or mod_perl directives to do that.

      The nearest that we have is SetEnv and PerlSetEnv, but they just set the environment seen by CGI scripts and mod_perl handlers much later on.

      The other idea is simply to rebuild my perl without that horrible registry lookup misfeature in it. It only requires a couple of lines to be deleted from win32/win32.c's win32_getenv(). In fact, in the email thread cited in my original post, Jan Dubois has already suggested actually removing it from perl altogether, so I might just take him up on pursuing that on p5p. (I'll still need to patch my own perl in the meantime, however: I can't wait until 5.12 for a fix.)

        I have no idea how it works on Windows, but on Linux all you need to do to set PERL5LIB is put it on the command-line when you start apache: PERL5LIB=/my/path apachectl start
Re: Overriding the registry's PERL5LIB in mod_perl on Win32
by Anonymous Monk on May 21, 2009 at 11:30 UTC
    LoadFile "C:/Perl/bin/perl58.dll"
      Ah, I should have said that I already have that.

      Apache/mod_perl is loading the perl DLL okay. The logic for looking up missing environment variables in the registry is actually contained in that perl DLL, so it is that which picks up the bogus PERL5LIB setting from the registry and then goes off loading the wrong versions of C extension modules' DLLs.

      (The extension modules' DLLs then fail to load because they have a dependency (shown by running depends.exe) on the perl56.dll (sic) of the perl installation that resides in the location pointed to by the PERL5LIB registry setting, and that DLL can't be found because it isn't on any PATH and I don't have a LoadFile directive for it. I don't want to load perl56.dll, of course, so fixing that problem wouldn't be the fix. What I want is to stop perl58.dll from loading up the wrong versions of C extension modules' DLLs in the first place.)