无法访问特定的内存地址。在 C 程序中

标签 c linux gcc memory gdb

我正在编写“HACKING Art Of Exploitation”一书练习 Convert2.c 第 61 页。

这是我的代码。下面是我的问题。

#include <stdio.h>

void usage(char *program_name) {
    printf("Usage: %s <message> <# of times to repeat>\n", program_name);
    exit(1);
}

int main(int argc, char *argv[]) {
    int i, count;

   // if(argc < 3) //if fewer than 3 arguments is used 
     //   usage(argv[0]); // display usage message and exit

    count = atoi(argv[2]); //convert the second arg into an  interger
    printf("Repeating %d times\n", count);

    for(i=0; i < count; i++) 
        printf("%3d - %s\n", i, argv[1]); // print the first arg 

}

GDB 输出...

    ➜ git:(master) ✗ 👽 gdb -q a.out
Reading symbols from a.out...done.
(gdb) run test
Starting program: /home/fruitdealer/clones/C_zombie/hacking/a.out test

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.
(gdb) break main
Breakpoint 1 at 0x555555554707: file convert.c, line 14.
(gdb) where
#0  __GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
#1  0x00007ffff7a29122 in __strtol (nptr=<optimized out>, endptr=endptr@entry=0x0, base=base@entry=10)
    at ../stdlib/strtol.c:106
#2  0x00007ffff7a24690 in atoi (nptr=<optimized out>) at atoi.c:27
#3  0x000055555555471f in main (argc=2, argv=0x7fffffffdeb8) at convert.c:14
(gdb) run test
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/fruitdealer/clones/C_zombie/hacking/a.out test

Breakpoint 1, main (argc=2, argv=0x7fffffffdeb8) at convert.c:14
14          count = atoi(argv[2]); //convert the second arg into an  interger
(gdb) cont
Continuing.

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.
(gdb) x/3xw 0x7fffffffdeb8
0x7fffffffdeb8: 0xffffe220      0x00007fff      0xffffe250
(gdb) x/s 0xffffe220
0xffffe220:     <error: Cannot access memory at address 0xffffe220>
(gdb) x/s 0xffffe250
0xffffe250:     <error: Cannot access memory at address 0xffffe250>
(gdb) x/sw 0xffffe250
0xffffe250:     <error: Cannot access memory at address 0xffffe250>
(gdb)

我发布了所有 gdb 输出,因为我不确定您需要多少。当我在 gdb 上运行“x/s”并获得 <error: Cannot access memory at address 0xffffe250> 时,我的问题出在我的 GDB 输出的底部错误。

Jon Erickson 可以访问这本书 0xffffe2200x00007fff然后他在 0xffffe250 上出错这部分内存。

我错过了什么?

为什么我无法访问 0x7fffffffdeb8 中的三个地址中的任何一个? ?

最佳答案

地址的前半部分被截断。如果您注意到,存储地址需要 8 个字节,因为您使用的是 64 位计算机,而不是 32 位计算机。您正在尝试访问截断的地址。

0x7fffffffdeb8 不是三个地址,而是一个半。尝试访问以 0x00007fff... 开头的字节

关于无法访问特定的内存地址。在 C 程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53235027/

相关文章:

c - 如何在C程序中使用sin、tan、cos、sec?

c - 从文件的前 N ​​个文件中读取数字

regex - 在 ed(编辑器)的行中间插入换行符

windows - Unix/Linux 线程的默认内存块?

gcc - ARM 的 gcc 代码源上的 memcpy

c - 使用 ATmega32 在 LCD 上显示数组中的 ASCII 字符

c - fscanf 读入双数组奇怪值

c - 理解fputc

linux - 如何将两个文本文件与第一列进行比较,如果匹配,则打印相同的内容,如果不匹配,则置零?

c++ - 无法在动态链接库中找到过程入口点 _ZNSt7_cxx1112basic_stringlcSt11char_traitslcESalcEEC1Ev