0

I am currently trying to call a system call by passing a char* buffer as a parameter. However, I am not able to write any data to the char* buffer nor read from the char* buffer. I tried allocating data to a char x = 'a' and then using copy_to_user and vice versa using copy_from_user, but nothing works out and no values are copied. I have been trying since way too long and not getting headway. Can someone please help me with this issue or point out where I am going wrong.

The system call is:

static void generate_Tasklist(char*,int);
static void generate_Tasklist(char *buffer,int size)
{
    printk("\nINSIDE THE FUNCTION\n");
    char x;
    x = 'a';
    int res = access_ok(VERIFY_WRITE,buffer,size);
    printk("The return value of access_ok is %d\n",res);

    int res1 = copy_to_user(buffer,&x,1);
    printk("\nTHE RESULT AFTER copy_to_user IS %d\n",res1);

    int res2 = copy_from_user(x,buffer,1);
    printk("---------%c\n",x);
    printk("\nTHE RESULT AFTER copy_from_user IS %d\n",res2);
}

asmlinkage long sys_syscall(char __user *buffer,int size) {
   printk("\nThis is the system call to list processes!!!!!!!!!!!!!!!!!!!!!!!!\n");
   generate_Tasklist(buffer,size);
   return 0;
}

The user space program is :

int main(int argc, char *argv[]) {
    char *bytes = (char *) malloc( sizeof( struct task_info ) * 3 );
    int size = sizeof( struct task_info ) * 3;

    for (int i=0; i < size; i++)
    {
        *(bytes + i) = 'x';
    }

    long int output = syscall(__NR_hello,1,bytes,size);
    printf("\nThe value is %c\n",bytes[0]);
    //print(bytes);
    free( (void *) bytes );
    printf ("The listprocess_syscall() returned %ld\n", output);
    return 0;
}

So what I'm trying to do is overwrite the default value of 'x' in the buffer by 'a' for the first byte which is not happening. The value returned by copy_from_user and copy_to_user is 1. Any suggestions on what I can do?

mfro
  • 3,286
  • 1
  • 19
  • 28
  • 1
    Not knowing what the function does, shouldn't `copy_from_user(x,buffer,1)` be `copy_from_user(&x,buffer,1)`? – MikeCAT Mar 12 '16 at 03:03
  • 2
    Note: They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Mar 12 '16 at 03:03
  • I believe @MikeCAT is correct. Also, can you post what messages actually print to give us a better idea of what's going on? – Joel C Mar 12 '16 at 06:03
  • You also don't need `(void *)` in `free( (void *) bytes );` unless you're using C++, in which case you shouldn't be using `malloc` or `free` at all, ever. Not ever, no. – autistic Mar 12 '16 at 06:52
  • please properly indent the code (when asking the question, highlite the code, then click the `{}` button.) – user3629249 Mar 12 '16 at 12:03
  • when calling any of the memory allocation functions: (malloc, calloc, realloc), always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Mar 12 '16 at 12:09
  • there are other considerations beside the user function and the kernel function, like has the kernel been re-compiled with the additional `__NR_hello` entry in the syscall table. has that entry been set to point to the `generate_Tasklist()` function? Has the `generate_Tasklist()` function been compiled/linked into the kernel? BTW: mighty strange function name, especially given what it does. – user3629249 Mar 12 '16 at 12:20
  • the function: `generate_Tasklist()` is `static` is it in the same file as the function that handles the syscall()`, if not, then it will be invisible. the `generate_Tasklist()` function is returned void, I.E. returned nothing, so how can the user function expect to be getting a `long int` returned value? – user3629249 Mar 12 '16 at 12:26
  • you might want to read/understand the information at: `http://man7.org/linux/man-pages/man2/syscall.2.html` – user3629249 Mar 12 '16 at 12:35
  • the functions: `copy_from_user()` and `copy_to_user()` return the number of bytes copied, so the returned value of 1 indicates those function worked correctly. the most likely problem is the parameters – user3629249 Mar 12 '16 at 12:42
  • another possible problem is the call to `syscall()` is passing the call# pluse 3 parameters, but the call to `sys_syscall()` is only getting 2 parameters and the function: `generate_Tasklist()` is only getting 2 parameters. – user3629249 Mar 12 '16 at 12:48

0 Answers0