1

I have a program that only works on Linux, and I have my development environment on Windows.

  • When I run a WSL command from cmd in Windows it works as expected.

    • When I run C:\wsl echo "foo" in cmd.exe I get foo back via stdout.
  • When I run the same command from within php via exec I don't get any stdout output and get an exit code -1073740791 which I assume is an error.


Cmd.exe:

C:\wsl echo "foo"
foo

PHP:

<?php
    $cmd = 'wsl echo "foo"';
    exec($cmd, $out, $code);
    dd($out, $code);
?>
// $out is []
// $code is -1073740791
Dai
  • 141,631
  • 28
  • 261
  • 374
Akkua
  • 101
  • 1
  • 7
  • Is your code running from the same context? For example, is your code not running **inside** WSL? – online Thomas Oct 28 '21 at 09:56
  • The PHP code is executed on Windows (I use WAMP) – Akkua Oct 28 '21 at 09:58
  • 1
    This is a horrible, horrible idea. Why do you want to do this? Assuming you can even get it working reliably (w.r.t. user-accounts, authX, the host web-server loading profile data correctly, etc) you'll be opening yourself up to all kinds of vulnerabilities (using `exec` in PHP is a code-smell in itself). – Dai Oct 28 '21 at 09:59
  • 1
    I have a program that only works on Linux, and I have my development environment on Windows – Akkua Oct 28 '21 at 10:01
  • 1
    @Acqua Okay, that's reasonable - I've updated your question title to be clearer. – Dai Oct 28 '21 at 10:07
  • So, you're saying that when you start a shell and run that `wsl ...` command, it works. If you put the above code into a proper PHP file and run `php filename.php` from the shell it doesn't? – Ulrich Eckhardt Oct 28 '21 at 10:10
  • @UlrichEckhardt The OP is using `dd` which is a Laravel function, which suggests they're running it from within a webserver instead of PHP's command-line mode. I think we should avoid mentioning PHP's command-line mode because that will behave differently to PHP running in webserver (especially w.r.t. permissions, etc). – Dai Oct 28 '21 at 10:12
  • Have you gone through this page? https://learn.microsoft.com/en-us/windows/wsl/troubleshooting – Dai Oct 28 '21 at 10:14
  • You can use Laravel stuff via commandline, too. In any case, if it works via commandline or not is important. If it does, PHP is not the issue but rather the execution environment. If it doesn't, the execution environment is irrelevant. Answering these questions before even asking here is why a [mcve] is required. – Ulrich Eckhardt Oct 28 '21 at 10:14
  • @UlrichEckhardt If I run `php foo.php` from cmd, it works. If I run `exec('php foo.php')` from my code, it does not work. – Akkua Oct 28 '21 at 10:20
  • @Acqua That's not what Ulrich is asking you to do. Also, "it does not work" is not an adequate problem-statement. What happens if you run any other "normal" Win32 command-line program from `exec`, such as `whoami` (don't use a built-in like `echo`). – Dai Oct 28 '21 at 10:25
  • @Acqua It's possible your PHP environment is configured to do strange things with `exec` - so what happens when you do `$tail = exec( "whoami", $out, $code ); echo $tail; echo $out; echo $code;` and run that from your web-browser (with Apache-on-Windows)? – Dai Oct 28 '21 at 10:26
  • From cmd it returns `my-pc-name\my-username`. From my code it returns `nt authority\system` (this is without wsl; if I run the same with wsl, it returns -1073740791). – Akkua Oct 28 '21 at 10:38
  • `whoami` cannot be run via `wsl` (as it's a Windows program, not a Linux program) so that's normal. Also, I suspect that because your PHP code is somehow running under `SYSTEM` (which is very inadvisable - why is your webserver running as `SYSTEM`?) that wsl won't run because (I suspect, I'm not a WSL expert) it needs user profile data to know how WSL should be configured. – Dai Oct 28 '21 at 10:52
  • `whoami` is also available on Ubuntu – Akkua Oct 28 '21 at 11:22
  • 2
    SOLVED!: All I had to do was change in services.msc the services wampapache64 and wampmysqld64 to run as my user rather than Local System account – Akkua Oct 28 '21 at 11:37
  • @Acqua I'm pleased to hear that worked for you - I'll admit that I chose `whoami` at random (as I just wanted _any_ Win32 command-line program to use as a test), so it's entirely coincidental that it revealed the problem was the wrong user-account. – Dai Oct 28 '21 at 11:41

2 Answers2

2

As discussed in the comments, the underlying issue was that your systemwide Apache web-server service (wampapache64) was configured to run as NT Authority\SYSTEM (aka Local System or LocalSystem).

...and running anything like a web-server as LocalSystem is a very, very bad idea for other reasons, especially when it's running PHP code - a simple mistake in uploaded-file handling or a failure to correctly sanitize user input could potentially hose your entire computer, especially when it's a publicly-exposed web-server that a malicious attacker can connect to.


Anyway:

Apparently when a process is running as SYSTEM it cannot use Win32's CreateProcess to invoke wsl to then indirectly invoke a Linux program.

So by changing your web-server to run as a real user account that meant that the CreateProcess... call invoked wsl within a supported environment.

Dai
  • 141,631
  • 28
  • 261
  • 374
0

Summarizing Dai's answer;

You are probably running Apache as NT Authority\SYSTEM (aka Local System or LocalSystem), and WSL doesn't like to run as anything but your user (And its also a bad security practice)

A simple fix, at least for WSL's issues, is to change the service's properties to use your windows user, on services.msc, wampstackApache (or similar), log on.

echo shell_exec("whoami"); will then report your user correctly and WSL will run just fine.

Disclaimer; I dont know how bad of a security issue this is, but it sounds kinda bad

Juanjo Martinez
  • 679
  • 7
  • 18