stat.h文件访问文件描述符open()黑客攻击的艺术

时间:2015-07-25 03:54:00

标签: c stat incomplete-type fcntl timespec

我正在使用VM(虚拟机箱)运行其附带的LiveCD(Ubuntu 7.04),从第二版Jon Erickson的“黑客攻击:剥削艺术”开始。在第0x281节“文件访问”中,作者解释了通过文件描述符访问文件,以及open()close()read()和write()函数,使用第82-84页上的示例。

simplenote.c的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(char *prog_name,char *filename){
        printf("Usage: %s < data to add to %s>\n",prog_name,filename);
        exit(0);
}

void fatal(char *);
void *ec_malloc(unsigned int );

int main(int argc,char *argv[]){
        int fd; //file descriptor
        char *buffer,*datafile;

        buffer = (char *)ec_malloc(100);
        datafile = (char *)ec_malloc(20);
        strcpy(datafile,"/tmp/notes");

        if(argc < 2)
                usage(argv[0],datafile);

        strcpy(buffer,argv[1]);

        printf("[DEBUG] buffer   @ %p:\'%s'\n",buffer,buffer);
        printf("[DEBUG] datafile @ %p:\'%s'\n",datafile,datafile);

        strncat(buffer,"\n",1);//Add a newline on the end.

        fd = open(datafile,O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() while opening file");
        printf("[DEBUG] file descriptor is %d\n",fd);
        //Writing data
        if(write(fd,buffer,strlen(buffer)) == -1)
                fatal("in main() while writing buffer to file");
        //Closing file
        if(close(fd) == -1)
                fatal("in main() while closing file");

        printf("Note has been saved.\n");
        free(buffer);
        free(datafile);
}

//A function to display an error message and then exit
void fatal(char *message){
        char error_message[100];

        strcpy(error_message,"[!!]Fatal Error");
        strncat(error_message,message,83);
        perror(error_message);
        exit(-1);
}

//An error-checked malloc() wrapper function 
void *ec_malloc(unsigned int size){
        void *ptr;
        ptr = malloc(size);
        if(ptr == NULL)
                fatal("in ec_malloc() on memory allocation");
        return ptr;
}

但是,当我在本书中输入以下说明到我的终端窗口时,它会返回以下错误消息:

reader@hacking:~/booksrc $ gcc -o simplenote simplenote.c
In file included from /usr/include/sys/stat.h:105, from simplenote.c:6:
/usr/include/bits/stat.h:70: error: field 'st_atim' has incomplete type
/usr/include/bits/stat.h:71: error: field 'st_mtim' has incomplete type
/usr/include/bits/stat.h:72: error: field 'st_ctim' has incomplete type
simplenote.c: In function 'main':
simplenote.c:35: error: 'O-WRONLY' undeclared (first use in this function)
simplenote.c:35: error: (Each undeclared identifier is reported only once
simplenote.c:35: error: for each function it appears in.)
simplenote.c:35: error: 'O_CREAT' undeclared (first use in this function)
simplenote.c:35: error: 'O_APPEND' undeclared (first use in this function)

这是sys / stat.h第105行:

#include <bits/stat.h>

这是bits / stat.h第63-83行:

#ifdef __USE_MISC
    /* Nanosecond resolution timestamps are stored in a format 
       equivalent to 'struct timespec'. This is the type used 
       whenever possible but the Unix namespace rules do not allow the 
       identifier 'timespec' to appear in the <sys/stat.h> header. 
       Therefore we have to handle the use of this header in strictly 
       standard-compliant sources special. */
    struct timespec st_atim;    /* Time of last access. */
    struct timespec st_mtim;    /* Time of last modification. */
    struct timespec st_ctim;    /* Time of last status change. */

# define st_atime st_atim.tv_sec    /* Backward compatibility */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
    __time_t st_atime;                 /* Time of last access. */
    unsigned long int st_atimensec;    /* Nscecs of last access. */
    __time_t st_mtime;                 /* Time of last modification. */
    unsigned long int st_mtimensec;    /* Nsecs of last modification. */
    __time_t st_ctime;                 /* Time of last status change. */
    unsigned long int st_ctimensec;    /* Nsecs of last status change. */
#endif

我认为这可能对第一组问题有用:

C++ system file bits/stat.h suddenly breaks with "error: field ‘st_atim’ has incomplete type"

/usr/include/time.h

cat time.h

在我的终端窗口中没有做任何事情。

这是simplenote.c主要功能行1-6,34-35:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

// Opening the file
    fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);

我猜测开放函数问题源于fcntl.h?

由于作者提供的错误代码,我似乎一直在遇到问题。我不想经常依赖stackoverflow社区寻求帮助,那么对于未来检查和修复这些问题的新手有什么建议?

感谢。

1 个答案:

答案 0 :(得分:0)

将一系列评论转换为半连贯的答案。

您应该明确启用POSIX定义。将-D_XOPEN_SOURCE=700添加到命令行,或者在第一个#define _XOPEN_SOURCE 700之前添加#include,看看是否能解决任何问题。你不应该遇到这个问题;标题应该是自包含的。

哦,但是Ubuntu 7.04很古老......你可能需要使用600而不是700.它什么时候发布(这本书何时发布)?如果是2009年或之前,您可能需要旧版本(600)。你看到错误仍然令人惊讶。您指定的命令行不包含通常会导致问题的选项(例如-ansi -pedantic-std=c99 -pedantic)。您也可以尝试使用-std=gnu99;它可能会更好。

你最近遇到了类似的问题(gcc -o stdlib.h syntax error c Hacking the Art of Exploitation)。你解决了吗?听起来好像Live CD上的编辑系统不是自我连贯的,或者你能够使用它的方式意味着它不是自我连贯的表现。你确定编译系统有效吗?它似乎是半失效的。是不是以某种方式使用了错误的标题?

  

我可以在#include <stdint.h>

之前插入#include <stdlib.h>来解决上一个问题      

我会尝试-D_XOPEN_SOURCE=600并回复你。编译系统肯定有问题。

好吧,您可能需要在<time.h>之前添加<sys/time.h>(或可能<sys/stat.h>),但如果有效,<sys/stat.h>标题会被破坏。如果在包含<stdlib.h>之前必须包含<stdint.h>,则#include <sys/types.h>标题会被破坏。我想Ubuntu 7.04可能已经很老了,你应该在许多这些标题之前<stdlib.h>,但这仍然不是#include <sys/types.h>的借口;这应该是独立的。 POSIX 1997在<sys/stat.h>之前需要st_atim; POSIX 2004没有。我不认为Ubuntu 7.04已经很老了。

但请注意,st_atime成员是新成员;它被添加到POSIX 2008(因此在POSIX 2013中)。它之前只有st_atimest_atim.tv_sec现在是-D_XOPEN_SOURCE=600的宏。

  

包括处理了比特统计问题的<stdio.h>。 Ubuntu 7.04于2007年发布,我使用的第2版本书于2008年发布。此外,不确定这是否有用,但在另一个包含<string.h>和{{1}的前一个示例中(而不是仅<stdio.h>),代码可以正常运行而无需任何干预。

有趣......它会让生活变得有趣,生活不应该是有趣的。 (中国的诅咒,如“你可能生活在有趣的时代”)。在所有编辑中使用-DXOPEN_SOURCE=600选项并保持手指交叉;这可能很好地解决了你的大部分问题。也可以考虑使用-std=gnu99。幸运的是,其中任何一个或两个都可以解决大多数问题。