LazyShpee
LazyShpee

Reputation: 82

Execve inline assembly

I recently got into gcc's inline assembly and having basic assembly knowledge, I understood how to make syscalls pretty well until I tried to do a simple sys_execve with one or more arguments. The system call execve works fine if I am not passing it any additional parameters, and just runs the executable without parameters when trying to pass any.

#include <stdio.h>

char *argv[]={"/bin/echo","parameter test", NULL};

int main(){
  __asm__ volatile ("int $0x80"
          :
          :"a"(11), // syscall number (execve)
           "b"(argv[0]), // filename
           "c"(argv), // arguments
           "d"(0)); // env
  return 0;
}

I have no idea what could go wrong, as I have tested this with

execve(argv[0], argv, NULL);

and it worked as expected.

Upvotes: 0

Views: 1269

Answers (1)

Jester
Jester

Reputation: 58762

This is 32 bit code, using 32 bit conventions. Compile using gcc -m32 and it will work. Alternatively, switch to the proper 64 bit version such as:

#include <stdio.h>

char *argv[]={"/bin/echo","parameter test", NULL};

int main(){
    int ret;
  __asm__ volatile ("syscall"
          :"=a" (ret)
          :"a"(59), // syscall number (execve)
           "D"(argv[0]), // filename
           "S"(argv), // arguments
           "d"(0) // env
          :"rcx","r11","cc");
  return 0;
}

The actual problem is that you have 64 bit pointers in the array, but the 32 bit compatibility interrupt that you use of course expects 32 bit poiters.

Upvotes: 4

Related Questions