Why can the execve system call run “/bin/sh” without any argv arguments, but not “/bin/ls”?

前端 未结 1 1157
庸人自扰
庸人自扰 2020-12-11 02:24

I am confused with the syscall of __NR_execve. When I learn linux system call. The correct way that I know to use execve is like this:



        
相关标签:
1条回答
  • 2020-12-11 02:39

    This is not a kernel issues, kernel will run filename arg of execve regardless of argv and envp are NULL or not, it is just a unix convention that argv[0] points to the program name.

    And what's you saw is just normal, nothing is wrong. Because ls is part of GNU's coreutils, and all programs in the coreutils package call set_program_name to do some setup work, you can see in the source, it checks whether argv[0] if NULL, and it will call abort when it is. On the other hand, /bin/sh is apparently a program that does not belong to coreutils, and does not check against argv[0], that's why it run without the problem.

    Refer to the source code:

    http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c#n1285

    http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/progname.c#n51

    0 讨论(0)
提交回复
热议问题