linux下关于system函数的简单分析
发布时间 - 2017-06-01 00:00:00 点击率:次这篇文章主要简单分析了linux下system函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
简单分析了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) s
igprocmask (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中下断查看。
# linux
# 在这里
# 这篇文章
# 都是
# 全局变量
# 几个
# 就会
# 是在
# 相关内容
# 有一定
# 感兴趣
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Win11搜索不到蓝牙耳机怎么办 Win11蓝牙驱动更新修复【详解】
bing浏览器学术搜索入口_bing学术文献检索地址
如何在橙子建站上传落地页?操作指南详解
手机怎么制作网站教程步骤,手机怎么做自己的网页链接?
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
香港服务器如何优化才能显著提升网站加载速度?
如何快速生成橙子建站落地页链接?
黑客入侵网站服务器的常见手法有哪些?
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
如何将凡科建站内容保存为本地文件?
Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程
EditPlus 正则表达式 实战(3)
javascript中闭包概念与用法深入理解
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧
网页设计与网站制作内容,怎样注册网站?
*服务器网站为何频现安全漏洞?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
Laravel怎么清理缓存_Laravel optimize clear命令详解
Laravel中间件如何使用_Laravel自定义中间件实现权限控制
焦点电影公司作品,电影焦点结局是什么?
Python高阶函数应用_函数作为参数说明【指导】
PythonWeb开发入门教程_Flask快速构建Web应用
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
Laravel如何创建自定义Facades?(详细步骤)
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
HTML 中动态设置元素 name 属性的正确语法详解
油猴 教程,油猴搜脚本为什么会网页无法显示?
Laravel如何使用Blade组件和插槽?(Component代码示例)
如何快速查询域名建站关键信息?
Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧
Laravel怎么使用Markdown渲染文档_Laravel将Markdown内容转HTML页面展示【实战】
如何自定义建站之星网站的导航菜单样式?
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
微信小程序 五星评分(包括半颗星评分)实例代码
Windows Hello人脸识别突然无法使用
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
免费视频制作网站,更新又快又好的免费电影网站?
百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏
Android滚轮选择时间控件使用详解
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
详解Huffman编码算法之Java实现
如何在阿里云购买域名并搭建网站?
Win11怎么设置默认图片查看器_Windows11照片应用关联设置
如何为不同团队 ID 动态生成多个独立按钮
Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】
如何制作一个表白网站视频,关于勇敢表白的小标题?
Laravel如何构建RESTful API_Laravel标准化API接口开发指南
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道


igprocmask (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