linux下system函数的简单分析
作者:1oner 发布时间:2023-11-03 05:29:56
简单分析了linux下system函数的相关内容,具体内容如下
int
__libc_system (const char *line)
{
if (line == NULL)
/* Check that we have a command processor available. It might
not be available after a chroot(), for example. */
return do_system ("exit 0") == 0;
return do_system (line);
}
weak_alias (__libc_system, system)
代码位于glibc/sysdeps/posix/system.c,这里system是__libc_system的弱别名,而__libc_system是do_system的前端函数,进行了参数的检查,接下来看do_system函数。
static int
do_system (const char *line)
{
int status, save;
pid_t pid;
struct sigaction sa;
#ifndef _LIBC_REENTRANT
struct sigaction intr, quit;
#endif
sigset_t omask;
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
__sigemptyset (&sa.sa_mask);
DO_LOCK ();
if (ADD_REF () == 0)
{
if (__sigaction (SIGINT, &sa, &intr) < 0)
{
(void) SUB_REF ();
goto out;
}
if (__sigaction (SIGQUIT, &sa, &quit) < 0)
{
save = errno;
(void) SUB_REF ();
goto out_restore_sigint;
}
}
DO_UNLOCK ();
/* We reuse the bitmap in the 'sa' structure. */
__sigaddset (&sa.sa_mask, SIGCHLD);
save = errno;
if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)
{
#ifndef _LIBC
if (errno == ENOSYS)
__set_errno (save);
else
#endif
{
DO_LOCK ();
if (SUB_REF () == 0)
{
save = errno;
(void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
out_restore_sigint:
(void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
__set_errno (save);
}
out:
DO_UNLOCK ();
return -1;
}
}
#ifdef CLEANUP_HANDLER
CLEANUP_HANDLER;
#endif
#ifdef FORK
pid = FORK ();
#else
pid = __fork ();
#endif
if (pid == (pid_t) 0)
{
/* Child side. */
const char *new_argv[4];
new_argv[0] = SHELL_NAME;
new_argv[1] = "-c";
new_argv[2] = line;
new_argv[3] = NULL;
/* Restore the signals. */
(void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
(void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
(void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
INIT_LOCK ();
/* Exec the shell. */
(void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
_exit (127);
}
else if (pid < (pid_t) 0)
/* The fork failed. */
status = -1;
else
/* Parent side. */
{
/* Note the system() is a cancellation point. But since we call
waitpid() which itself is a cancellation point we do not
have to do anything here. */
if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)
status = -1;
}
#ifdef CLEANUP_HANDLER
CLEANUP_RESET;
#endif
save = errno;
DO_LOCK ();
if ((SUB_REF () == 0
&& (__sigaction (SIGINT, &intr, (struct sigaction *) NULL)
| __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
|| __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)
{
#ifndef _LIBC
/* glibc cannot be used on systems without waitpid. */
if (errno == ENOSYS)
__set_errno (save);
else
#endif
status = -1;
}
DO_UNLOCK ();
return status;
}
do_system
首先函数设置了一些信号处理程序,来处理SIGINT和SIGQUIT信号,此处我们不过多关心,关键代码段在这里
#ifdef FORK
pid = FORK ();
#else
pid = __fork ();
#endif
if (pid == (pid_t) 0)
{
/* Child side. */
const char *new_argv[4];
new_argv[0] = SHELL_NAME;
new_argv[1] = "-c";
new_argv[2] = line;
new_argv[3] = NULL;
/* Restore the signals. */
(void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
(void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
(void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
INIT_LOCK ();
/* Exec the shell. */
(void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
_exit (127);
}
else if (pid < (pid_t) 0)
/* The fork failed. */
status = -1;
else
/* Parent side. */
{
/* Note the system() is a cancellation point. But since we call
waitpid() which itself is a cancellation point we do not
have to do anything here. */
if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)
status = -1;
}
首先通过前端函数调用系统调用fork产生一个子进程,其中fork有两个返回值,对父进程返回子进程的pid,对子进程返回0。所以子进程执行6-24行代码,父进程执行30-35行代码。
子进程的逻辑非常清晰,调用execve执行SHELL_PATH指定的程序,参数通过new_argv传递,环境变量为全局变量__environ。
其中SHELL_PATH和SHELL_NAME定义如下
#define SHELL_PATH "/bin/sh" /* Path of the shell. */
#define SHELL_NAME "sh" /* Name to give it. */
其实就是生成一个子进程调用/bin/sh -c "命令"来执行向system传入的命令。
下面其实是我研究system函数的原因与重点:
在CTF的pwn题中,通过栈溢出调用system函数有时会失败,听师傅们说是环境变量被覆盖,但是一直都是懵懂,今天深入学习了一下,总算搞明白了。
在这里system函数需要的环境变量储存在全局变量__environ中,那么这个变量的内容是什么呢。
__environ是在glibc/csu/libc-start.c中定义的,我们来看几个关键语句。
# define LIBC_START_MAIN __libc_start_main
__libc_start_main是_start调用的函数,这涉及到程序开始时的一些初始化工作,对这些名词不了解的话可以看一下这篇文章。接下来看LIBC_START_MAIN函数。
STATIC int
LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
int argc, char **argv,
#ifdef LIBC_START_MAIN_AUXVEC_ARG
ElfW(auxv_t) *auxvec,
#endif
__typeof (main) init,
void (*fini) (void),
void (*rtld_fini) (void), void *stack_end)
{
/* Result of the 'main' function. */
int result;
__libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
#ifndef SHARED
char **ev = &argv[argc + 1];
__environ = ev;
/* Store the lowest stack address. This is done in ld.so if this is
the code for the DSO. */
__libc_stack_end = stack_end;
......
/* Nothing fancy, just call the function. */
result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
#endif
exit (result);
}
我们可以看到,在没有define SHARED的情况下,在第19行定义了__environ的值。启动程序调用LIBC_START_MAIN之前,会先将环境变量和argv中的字符串保存起来(其实是保存到栈上),然后依次将环境变量中各项字符串的地址,argv中各项字符串的地址和argc入栈,所以环境变量数组一定位于argv数组的正后方,以一个空地址间隔。所以第17行的&argv[argc + 1]语句就是取环境变量数组在栈上的首地址,保存到ev中,最终保存到__environ中。第203行调用main函数,会将__environ的值入栈,这个被栈溢出覆盖掉没什么问题,只要保证__environ中的地址处不被覆盖即可。
所以,当栈溢出的长度过大,溢出的内容覆盖了__environ中地址中的重要内容时,调用system函数就会失败。具体环境变量距离溢出地址有多远,可以通过在_start中下断查看。


猜你喜欢
- 操作系统:CentOS 7.x 64位实现目的:安装部署Memcached服务器一、防火墙设置CentOS 7.x默认使用的是firewal
- 为了实现Linux环境下的FTP服务器配置,绝大多数的Linux发行套装中都选用的是Washington University FTP(Wu
- 新华网北京12月27日电一个名为“IE7攻击代码(Hack.Exploit.Script.JS.Agent.ic)”的恶意代码本周特别值得注
- 10月21日消息,据国外媒体报道,本周二,雅虎公布了第三季度财报,该季度利润超过去年同期的三倍,第三季度净利润同比增长244%,增至1.86
- 一、Windows Server2003的安装1、安装系统最少两需要个分区,分区格式都采用NTFS格式2、在断开网络的情况安装好2003系统
- 1.虚拟环境virtualenv安装1.安装virtualenv pip3 install virtualenv2.创建目录,把项
- 在这个互联网飞速发展的时代,对于传统行业的冲击是非常大的,就拿购物来说,越来越多的人开始由传统的购物方式转入到了潮兴的网上购物大军了。做为一
- 大家好,我是淘侠客,今天给大家带来的是我本人原创的通过百度i贴吧轻松带来流量的教程,首先大家知道现在想从百度贴吧带来流量是很困难的一件事了,
- 一、错误现象我在虚拟机下使用 LVGL 仿真器,编译仿真器源代码时发生报错cc: error: unrecognized command l
- 1、选择主题。主题选好也就成功了一半,所以这个步骤必须慎重考虑。主要有以下几方面因素:热门指数、该类主题的网站数量及其发展情况、该主题可能的
- 照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread
- IXWebHosting是一家获得好评的虚拟主机供应商,提供Linux和Windows两种方案,因此,您更容易能够到合适的主机方案。所有的X
- 这篇文章是VMware虚拟机的数据恢复的一个案例,虽然整个VMware虚拟机的数据恢复过程只需要三步,但是前后的问题分析和经验总结值得您借鉴
- c:\administrators 全部system 全部iis_wpg 只有该文件夹列出文件夹/读数据读属性读扩展属性读取权限c:\ine
- Google Adsense的西联汇款支付虽然已经准备了很长的时间,但是在相关配套的支持上还是有很多欠缺,所以发布者会有诸多疑问,这里有大部
- 10月29日消息,华硕电脑与NVIDIA、交大合作开发出全球最快、体积最小的单机超级电脑,抢攻企业用户伺服器市场,华硕伺服器部门也成立专属团
- 拥有7亿多用户和300多个互联网产品的马化腾如何在多元化的急速扩张中掌控航向,超越“跟风者”的形象,并构建一个世界上前所未有庞大的Web2.
- Discuz!7.0是康盛创想(Comsenz)公司于2008年12月份发布的一款论坛BBS建站产品。在Discuz!7.0版本中完善了论坛
- IIS团队刚刚发表了IIS7在Window Server 2008 R2 beta中的新改进.Windows Server 2008 R2包
- Google Adsense广告样式就那么几种,大家看久了,也会产生麻木,厌倦,这样非常不利于广告的收入,看了下面的文章相信会给你带来新的收