0

I try to run multiple commands (or using simple output redirection) via execve().

When I put this (of course before I pass this string to function I split into spaces and put each each separate to char* []):

"bash -c ' /usr/bin/cat /root/script.sh > /root/script1.sh ' "

to execve() function, I've got an error:

/usr/bin/cat: -c: line 0: unexpected EOF while looking for matching `''

/usr/bin/cat: -c: line 1: syntax error: unexpected end of file

This is my proposal to run multiple linux commands (applications located into PATH) using exactly execve() function (because of security reasons)

But this solution don't work as I expect.

Any idea to fix my solution? Maybe I can use execve() otherwise, but I don't know how..

EDIT: Added simplified (sorry, I can't paste in the original form, because of company restriction) source code:

int foo(const char *cmdline)
{
    char d[] = "bash -c ' /usr/bin/cat /root/script.sh > /root/script1.sh ' ";

    args = strtok(d, " ");
    counter = 0;
    while (args != NULL)
    {
        cmdline_args[counter++] = args;
        args = strtok(NULL, " ");
    }
    cmdline_args[counter] = '\0';

    switch (pid = fork()) {
    case -1:
        ret = -1;
    case 0: // for execve
        status = execve(cmdline_args[0], cmdline_args, env);
        exit(status);
    default: // for parent pid
        if (waitpid(pid, &status, 0) < 0) {
            // in case when waitpid failed
        }
    }
    return ret;
}
3
  • The error message tells you have an unmatched quote somewhere in the strings that you pass to execve(). Inspects the strings and also post the relevant code.
    – P.P
    Nov 23, 2015 at 10:47
  • show your C code, it is more easier to help when to show it
    – developer
    Nov 23, 2015 at 10:49
  • I added code. I suppose that this is good enough to understand function meaning. @BlueMoon I also think so, but I don't know where is a bug.
    – maslokarol
    Nov 23, 2015 at 11:19

2 Answers 2

1

As your code is written just now, it will I think execute the executable bash with arguments:

[ "bash", "-c", "'", "/usr/bin/cat", "/root/script.sh", ">", "/root/script1.sh", "'", 0]

I'm guessing that you instead want to be aiming for something like:

[ "/bin/bash", "-c", "/usr/bin/cat /root/script.sh >/root/script1.sh", NULL]

The bash binary is unlikely to respond well to an argument '. When bash processes a command that you type, it does quite a lot of intricate work to process the contents of quoted strings, and pull out the actual intended arguments from them. It looks like you may have to duplicate some of that work, if you really have to process near-arbitrary commands in cmdline (in which case, I would step back and think ‘is this really the right way to be doing X?’).

Also, execve requires a full path to the binary as its first argument; it doesn't search the PATH.

Also^2: your title mentions a backquote `, but your example code mentions single right-quotes ' – you're aware those are very different, yes?

0

Works for me if done like this:

cmdline_args[0] = "bash";
cmdline_args[1] = "-c";
cmdline_args[2] = "/usr/bin/cat /root/script.sh > /root/script1.sh";
cmdline_args[3] = NULL;

So the problem is that you're crunching the whole command line string in strtok. Because of that bash gets multiple parameters (not just one as it's supposed to - the whole command string). bash is probably only interpreting the first command parameter so you'll end up executing the ' command ...

There are probably better ways to do that though ...

1
  • You are right. Problem was caused by using strtok. Thanks for help.
    – maslokarol
    Dec 1, 2015 at 10:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.