in reply to Re: Security concern with sudo and system()
in thread Security concern with sudo and system()

No, Perl does not use an %ENV variable to figure out which shell to use for system. For Unix it is always /bin/sh (unless you changed that for some obscure reason when you built Perl).

If you always use the multi-argument form of system, then a shell will never be invoked. Accomplishing this can be made easier by using IPC::Open2 and/or IPC::Open3 to do I/O redirection w/o using a shell.

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: Security concern with sudo and system()

Replies are listed 'Best First'.
Re: (tye)Re: Security concern with sudo and system()
by John M. Dlugosz (Monsignor) on Sep 16, 2001 at 08:37 UTC
    Well, other OS's don't have a /bin/sh, or a /bin/ for that matter. I thought the ENV variable was standard now, but looking at the source it seems that each platform has its own logic to find/use the shell.

    Under Win32, it's:

    static void get_shell(void) { dTHXo; if (!w32_perlshell_tokens) { /* we don't use COMSPEC here for two reasons: * 1. the same reason perl on UNIX doesn't use SHELL--rampant and * uncontrolled unportability of the ensuing scripts. * 2. PERL5SHELL could be set to a shell that may not be fit for * interactive use (which is what most programs look in COMSPE +C * for). */ const char* defaultshell = (IsWinNT() ? "cmd.exe /x/c" : "command.com /c"); const char *usershell = PerlEnv_getenv("PERL5SHELL"); w32_perlshell_items = tokenize(usershell ? usershell : defaultshel +l, &w32_perlshell_tokens, &w32_perlshell_vec); } }
    This could certainly work for all platforms. But a quick search indicates:
    • On vos it's not supported.
    • On vms, it calls lib$spawn, whatever that means (is $ a letter on that platform's C compiler, or a scope resolution of some kind?
    • on VM/ESA it calls /bin/sh -c.
    • On OS/2, this comment: "Consensus on perl5-porters is that it is _very_ important to have a shell which will not change between computers with the same architecture, to avoid "action on a distance". And to have simple build, this shell should be sh"

      However, it can be compiled to try looking at environment variables EMXSHELL, SHELL, COMSPEC in that order (or CMD.EXE if none of those are found), and applying the /C switch to whatever it finds; or using the contents of a variable called PL_sh_path and the -c switch. That variable is listed in a few non-OS-specific files, so it looks like this is what it is supposed to look at in all builds. That is, the configuration in embedvar or perlapi can configure the shell, without editing the deep guts.

      Hmm, in other places it looks like the variable PL_sh_path contains only the directory of the shell, not the name itself, in other uses. But this OS/2 support module treats it in the same way whether it uses CMD.EXE or the contents of PL_sh_path, so it expects to find a complete executable file name there. Is this a bug?

    • Epoc seems to use a C++ API to the OS, with a member function RProcess::Create doing the work.
    • In another place, PL_sh_path is traced back to a member of Interpreter which does indeed have a complete command name. So the use is inconsistant in different builds. In VOS, it is "/system/ported/command_library/bash.pm"
    So, it's a real mess. Besides different specific logic for each OS or variation, there are lots of wrapping macros and levels of indirection just to make things more confusing, and inconsistant use of common code.

    —John

$ENV{PERL5SHELL}
by John M. Dlugosz (Monsignor) on Sep 16, 2001 at 08:45 UTC
    On Windows, it will look for the contents of $ENV{PERL5SHELL} for a complete string (command name and switches) that it prepends to the string you are running.

    If that environment variable does not exist, it uses either "CMD.EXE /X/C" or "COMMAND.COM /C" depending one whether it's running NT or Win9x.

    Having different shells on Win9x vs NT can expose differences, since CMD with the /X switch does more than COMMAND.COM.

    I know Perl used to use $ENV{COMSPEC} because I relied on the capabilites of my shell (the one that invoked the perl script in the first place) and found it broke at some point. That's when I learned about PERL5SHELL. I seem to recall that there was also some "common" min-shell shipped with activestate Perl at some point, to prevent differences between 9x and NT.

    —John

      Yes, I have vague memories of long ago Perl using $ENV{SHELL} for system under Unix and that being fixed.

      One of the reasons that building modules from CPAN doesn't work well under Win9x is that 2>&1 doesn't work in COMMAND. I realize now that the fix for this is IPC::Open2.

      And now I've drifted way off the original topic. (:

              - tye (but my friends call me "Tye")
        Re off topic: so change the topic.

        In the shell I use on Windows, command >& output redirects both stdout and stderr. I think something like what you show works too, but I'd have to look up the syntax details.

        Who needs a shell? Do it all in Perl, and run external programs when you need them but don't rely on the shell to do anything for you. Instead do the same stuff in Perl! Like you point out, redirect the output. Also, do globbing, directory listing, etc. all in Perl and then just call the program. That's a path I've taken, because COMMAND.COM isn't worthy of the name shell and using a real shell is non-portable.

        —John