【问题标题】:modlib3.c:21:9: error: invalid use of incomplete typedef 'DIR {aka struct _dirstream}modlib3.c:21:9:错误:不完整 typedef 'DIR {aka struct _dirstream} 的使用无效
【发布时间】:2020-11-19 08:09:04
【问题描述】:

问题是我需要使用 LD_PRELOAD 修改 opendir() 函数,这是 ls 命令的一部分,以在目标路径不在 /home 目录中时限制目录的打开。我得到的错误是返回行,上面写着: 返回((*original_opendir)(_name)); 因为 original_opendir 以粗体显示,并且显示错误:无效使用不完整的 typedef 'DIR{aka struct _distream)'

我在下面附上了我的代码。如果您有任何想法,请告诉我,我将不胜感激![在此处输入图片描述][1]

文件名叫做 modlib3.c,当我得到错误时,我用这个编译了它: //gcc -o modlib3.so -shared -fPIC -D_GNU_SOURCE modlib3.c -ldl

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dlfcn.h>
#include <dlfcn.h>


DIR *opendir(const char *_name) {
  DIR       (*original_opendir)(const char *);
  *(void **)(&original_opendir) = dlsym(RTLD_NEXT, "*opendir");  
  if (strcmp(_name, "/home") <0 || strcmp(_name, "/home")>0){
    syslog(LOG_EMERG, "Cannot open! ");
    exit(1);
  }
  return((*original_opendir)(_name));
 }

【问题讨论】:

  • 你想重塑chroot
  • 基本上我希望 ls 命令不打开主目录以外的任何内容
  • 首先,您应该使用realpath(3) 来解析符号链接和相对路径名。

标签: c linux typedef ls opendir


【解决方案1】:

修复了代码中的一些问题,请仔细检查差异

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dirent.h>
#include <dlfcn.h>

DIR *opendir(const char *_name) {
  DIR      *(*original_opendir)(const char *);
  *(void **)(&original_opendir) = dlsym(RTLD_NEXT, "opendir");
  if (strncmp(_name, "/home", 5)!=0) {
    syslog(LOG_EMERG, "Cannot open!");
    exit(1);
  }
  return((*original_opendir)(_name));
 }

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 2013-01-14
    • 2021-05-24
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2013-04-05
    • 2019-11-25
    相关资源
    最近更新 更多