How can I get argv from “struct linux_binprm”?

2019-07-30 15:10发布

问题:

I want to extract all argv from an existing struct linux_binprm. On kernel 3.4, I tried this piece of code: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg00278.html in do_excve_common, but it doesn't work. It returns (null). What is the problem and how can I get ALL the arguments in a char * string?

回答1:

. If you want to get the full command line before the binary loader executing in do_execve_common(), you can try following: there is one argument *argv in the function do_execve_common() parameter table, why bother to get the argv from "struct linux_binprm"? You can use the *argv directly with following codes. In the do_execve_common(), insert some codes as following:

argc = count(argv, MAX_ARG_STRINGS);
i = 0;
while (i < argc)
{
    const char __user *str;
    int len;

    ret = -EFAULT;
    str = get_user_arg_ptr(argv, i);
    if (IS_ERR(str))
        goto out;

    len = strnlen_user(str, MAX_ARG_STRLEN);
    if (!len)
        goto out;

    //copy the str to kernel temporary storage
    //NOTE: tmp[] is a string array, 
    //      the memory should have been allocated already for strings storage, 
    //      each string is ended with \0
    memcpy(tmp[i], str, len)
}

After executing these codes, I think the argv strings will be all saved in tmp[] array.

. While if you want to get the full command line after binary loader executing, I think at this time the argument page has been setup correctly, then you can try following approach to get the full command line: There is a function proc_pid_cmdline() in ./fs/proc/base.c file, you can re-use most codes in proc_pid_cmdline() function to get the full command line from the argument page.



标签: linux kernel