1

I have been following this answer and I can't figure out what the hell I'm doing wrong. When I call my api endpoint (via postman) I get following error, when my script tries to generate and compile script c script.

My php code

        exec('
cat > wrapper.c <<CONTENT
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>

  int
  main (int argc, char *argv[])
  {
     setuid (0);

     system ("/bin/sh /var/www/html/myapp/script.sh");

     return 0;
   }
CONTENT
gcc wrapper.c -o php_root 2>&1
',  $output, $returnVar);

        var_dump($output);
        var_dump($returnVar);
        die();

And the error that I get is gcc: error trying to exec 'cc1': execvp: No such file or directory' which happens when gcc wrapper.c -o php_root 2>&1 command is executed

Running all those commands in bash works fine, but when they are executed via PHP exec, I get the error.

gcc -v
(venv) certify-dev: /var/www/html/certify/public $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.10' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 

Can you guy please help me find out where the problem is. If you have any additional questions, please let me know and I will provide answer. Thank you!

UPDATE

@squeamish notified me, I should provide full gcc path and this was a case, but now I have another error

Updated line

/usr/bin/gcc wrapper.c -o php_root 2>&1

Error

  0 => string 'collect2: fatal error: cannot find 'ld'' (length=39)
  1 => string 'compilation terminated.' (length=23)

2. UPDATE

After checking my PATHs (suggested by @ChrisTurner) for php I found out that my shell is missing php executables...

my php code

$phpFinder = new PhpExecutableFinder;
    if (!$phpPath = $phpFinder->find()) {
        throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
    }
    var_dump($phpPath); /usr/bin/php
    var_dump(PHP_BINDIR); /usr/bin/php

and shell

dev: /var/www/html/myapp $ sudo -H -u www-data bash -c 'echo $PATH'

outputs

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin

That's why I added export PATH="$PATH:/usr/bin/php" in to my ~/.bashrc and this are my PATHs now in bash

myApp: /var/www/html/certify $ sudo -H -u www-data bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/bin/php

Code is still throwing errors 'collect2: fatal error: cannot find 'ld''

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Valor_
  • 3,461
  • 9
  • 60
  • 109
  • 1
    On the command line, type in `which gcc`. It should output something like `/usr/bin/gcc`. Copy this to where the last line of your bash code (i.e., change it to `/usr/bin/gcc wrapper.c -o php_root 2>&1`). Does that help? – r3mainer Sep 06 '18 at 09:06
  • nice eyes sir! Now i have another error collect2: fatal error: cannot find 'ld' – Valor_ Sep 06 '18 at 09:48
  • Is gcc installed in a non-standard place? Is the webserver running scripts with a different PATH to your shell? – Chris Turner Sep 06 '18 at 09:57
  • @ChrisTurner not that I know (I know literary nothing about C and gcc thought). I made classic Ubuntu 16.04 installation. I have installed all nginx, php-fpm, phyton, but I haven't installed nothing regarding gcc... maybe I'm missing some libraries? – Valor_ Sep 06 '18 at 10:01
  • `apt-get install build-essential` :-) https://packages.ubuntu.com/xenial/build-essential – João Neto Sep 06 '18 at 10:02
  • @JoãoNeto `build-essential is already the newest version (12.1ubuntu2). build-essential set to manually installed. 0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.` – Valor_ Sep 06 '18 at 10:03
  • If you add `echo $_ENV["PATH"];` to your PHP code, that'll show what your path is set to on the webserver and if you compare that with typing `echo $PATH` in your shell, I am expecting there is a difference. – Chris Turner Sep 06 '18 at 10:20
  • @ChrisTurner Could be. While php outputs `$phpFinder = new PhpExecutableFinder; var_dump($phpFinder->find()); // outputs /usr/bin/php` while my shell doesn't even have any php value in PATH `/home/vagrant/certbot/venv/bin:/home/vagrant/bin:/home/vagrant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin` Let me try to add it – Valor_ Sep 06 '18 at 10:48
  • @ChrisTurner I have added an update to my question, because its still not working and I have added a path to my $PATH var. Did I do it incorrectly? – Valor_ Sep 06 '18 at 11:10
  • PATH contains a list of directories that are searched for executables... /usr/bin is in there, so /usr/bin/php is covered. The problem you're facing is that "ld" isn't in the PATH – Chris Turner Sep 06 '18 at 11:44
  • hmm then ld should be covered also i think `sudo -H -u www-data bash -c 'whereis ld' ld: /usr/bin/ld /usr/bin/ld.bfd /usr/bin/ld.gold /usr/share/man/man1/ld.1.gz` – Valor_ Sep 06 '18 at 11:50
  • Where is the output from adding `echo $_ENV["PATH"];` to your PHP? – Chris Turner Sep 06 '18 at 12:09
  • Omg its working now! :) I'm using Symphony 4, so there is no $_ENV["PATH"] variable. After adding value to .env file it started to work ;) @ChrisTurner will you write an answer and I will accept it – Valor_ Sep 06 '18 at 12:14

1 Answers1

1

The problem you're encountered is that you're trying to run "gcc" but PHP cannot find it or any of the subprograms it runs to perform the compilation or linking. This is because the PATH environment variable isn't set. This variable defines a list of directories that are searched in order to find a program if the full path isn't provided to it.

To fix it in the Symfony environment, you need to add the variable to your .env file. Something along the lines of the following for example

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Chris Turner
  • 8,082
  • 1
  • 14
  • 18