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''

13
  • 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? Commented Sep 6, 2018 at 9:06
  • nice eyes sir! Now i have another error collect2: fatal error: cannot find 'ld' Commented Sep 6, 2018 at 9:48
  • Is gcc installed in a non-standard place? Is the webserver running scripts with a different PATH to your shell? Commented Sep 6, 2018 at 9: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? Commented Sep 6, 2018 at 10:01
  • apt-get install build-essential :-) packages.ubuntu.com/xenial/build-essential Commented Sep 6, 2018 at 10:02

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Chris for all the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.