一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Linux|Centos|Ubuntu|系統進程|Fedora|注冊表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服務器之家 - 服務器系統 - Linux - linux下system函數的簡單分析

linux下system函數的簡單分析

2022-01-19 17:231oner Linux

這篇文章主要簡單分析了linux下system函數,具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡單分析了linux下system函數的相關內容,具體內容如下

?
1
2
3
4
5
6
7
8
9
10
11
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函數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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信號,此處我們不過多關心,關鍵代碼段在這里

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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定義如下

?
1
2
#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中定義的,我們來看幾個關鍵語句。

?
1
# define LIBC_START_MAIN __libc_start_main

__libc_start_main是_start調用的函數,這涉及到程序開始時的一些初始化工作,對這些名詞不了解的話可以看一下這篇文章。接下來看LIBC_START_MAIN函數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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中下斷查看。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品日韩欧美在线 | 福利视频一区二区思瑞 | 欧美黑人一级片 | 日产欧产va高清 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 日本不卡免免费观看 | 久久免费资源福利资源站 | 精品福利视频一区二区三区 | 羞羞影院午夜男女爽爽影院网站 | 国产一区二区免费福利片 | 美女被狂揉下部羞羞动漫 | 亚洲v日韩v欧美在线观看 | 精品精品国产yyy5857香蕉 | 国产精品久久久久久久久久久久久久 | 欧美高清无砖专区欧美精品 | 精品夜夜澡人妻无码AV蜜桃 | 女主被当众调教虐np | 俺去俺来也www色官网免费的 | ass天天裸妇pics | 男人把大ji巴放进男人免费视频 | 拔插拔插8x8x海外华人免费视频 | 三级伦理在线播放 | 国产精品第2页 | 久草在在线免视频在线观看 | 91香蕉影院 | 日本不卡在线视频高清免费 | 亚洲色图欧美色 | 沟厕okn系列在线播放 | 毛片一区二区三区提莫影院 | 亚洲欧美国产精品完整版 | 国产精品久久久久久久久ktv | 国产一级毛片潘金莲的奶头 | 成年女人毛片免费观看中文w | 999精品视频在线 | 范冰冰特黄xx大片 | 九九精品免视看国产成人 | 极品丝袜老师h系列全文阅读 | 男神插曲女生动漫完整版动漫 | 特级非洲黑人一级毛片 | 9总探花新品牛仔背带裤 | 丰满在线观看 |