在内核模块中获取NFS客户端IP地址

问题描述 投票:0回答:1

我正在研究内核模块,以跟踪服务器上NFS客户端执行的操作。我可以使用黑客方式(劫持vfs层)来拦截文件操作,但无法获取客户端的IP地址。

current任务中是否可以存储任何信息,我可以用来获取执行操作的NFS客户端的IP地址?

[从源代码的挖掘中我知道nfsd将struct nfsd_net存储在struct super_blocks_fs_info字段中,但是我只能将其作为struct net指针来检索。在nfsd的实现中,正在使用net_generic方法获取struct nfsd_net指针(使用nfsd_net_id,即pernet_operationsid)。

我可以通过某种方式获取此ID吗?如果可以,我可以在内核模块中使用struct nfsd_net吗?是否在fs/nfsd/netns.h以外的地方定义?

编辑

我正在使用this approach劫持打开功能。我正在为内核版本4.15.0编写此代码。这是内核模块的代码:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/proc_fs.h>
#include <linux/cred.h>
#include <linux/sched.h>
#include <linux/preempt.h>
#include <linux/uaccess.h>
#include <linux/xattr.h>

MODULE_LICENSE("GPL");

#if defined(__i386__)
    #define POFF 1
    #define CSIZE 6
    // push address, addr, ret
    char *jmp_code="\x68\x00\x00\x00\x00\xc3";
    typedef unsigned int PSIZE;
#else
    #define POFF 2
    #define CSIZE 12
    // mov address to register rax, jmp rax. for normal x64 convention
    char *jmp_code="\x48\xb8\x00\x00\x00\x00\x00\x00\x00\x00\xff\xe0";
    typedef unsigned long PSIZE;
#endif

DEFINE_SPINLOCK(root_open_lock);

int (*orig_root_open) (struct inode *, struct file *);
void *orig_root_open_code;

void hook(void *src_func,void *dst_addr){
  barrier();
  write_cr0(read_cr0() & (~0x10000));
  memcpy(src_func,jmp_code,CSIZE);
  *(PSIZE *)&(((unsigned char*)src_func)[POFF])=(PSIZE)dst_addr;
  write_cr0(read_cr0() | 0x10000);
  barrier();
}

void save_and_hook(void **p_reserve,void *src_func,void *dst_addr){
  barrier();
  write_cr0(read_cr0() & (~0x10000));
  *p_reserve=kmalloc(CSIZE,GFP_KERNEL);
  // save origin code
  memcpy(*p_reserve,src_func,CSIZE);
  hook(src_func,dst_addr);
  write_cr0(read_cr0() | 0x10000);
  barrier();
}

void fix(void **p_reserve,void *src_func){
  barrier();
  write_cr0(read_cr0() & (~0x10000));
  memcpy(src_func,*p_reserve,CSIZE);
  write_cr0(read_cr0() | 0x10000);
  barrier();
}

int fake_root_open(struct inode *x, struct file *fp)
{
  int ret;

  printk("vfshijack: hijacked open\n"); // I need to find the client ip here.

  barrier();
  spin_lock(&root_open_lock);
  fix(&orig_root_open_code, orig_root_open);
  ret = orig_root_open(x, fp);
  hook(orig_root_open, fake_root_open);
  spin_unlock(&root_open_lock);
  barrier();
  return ret;
}

int vfs_init(void)
{
  struct file *fp = filp_open("/", O_DIRECTORY|O_RDONLY, 0);
  if (IS_ERR(fp))
    return -1;

  orig_root_open = fp->f_op->open;
  if(orig_root_open)
  {
    save_and_hook(&orig_root_open_code, orig_root_open, fake_root_open);
  }

  filp_close(fp, NULL);

  printk("vfshijack: vfshijack loaded\n");
  return 0;
}

void vfs_exit(void)
{
  if(orig_root_open)
  {
    fix(&orig_root_open_code, orig_root_open);
  }
  printk("vfshijack: vfshijack unloaded\n");
}

module_init(vfs_init);
module_exit(vfs_exit);
c linux-kernel kernel-module nfs
1个回答
0
投票

您可以尝试从linux内核跟踪工具中获取所需的信息,而不必将内核二进制文件与某些自定义程序挂钩。对于大多数内核版本,有perfftracetrace-cmd,对于更多自定义版本有staplttng。一些文档开始:https://www.kernel.org/doc/html/v4.18/trace/index.html“ Linux跟踪技术”

nfsd中定义了多个跟踪点:

# modprobe nfsd
# modprobe nfs
# perf list tracepoint|grep nfs
# find /sys/kernel/debug/tracing/events -type d|grep nfsd
# trace-cmd list -e nfsd:read_start -F

nfsd / read_start和nfsd / write_start跟踪点是很好的起点。两者都应该可以使用地址rqstp和fh指针访问请求结构rq_addr(但是某些eBPF或stap脚本可能有用)rq_addr

https://elixir.bootlin.com/linux/v4.15/source/fs/nfsd/vfs.c#L1020

我没有用于nfs守护程序跟踪的__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,...) trace_read_start(rqstp, fhp, offset, vlen); trace_read_opened(rqstp, fhp, offset, vlen); trace_read_io_done(rqstp, fhp, offset, vlen); trace_read_done(rqstp, fhp, offset, vlen); 或stap用法的完整示例。

Systemtap(trace-cmd)包含nfsd统计信息的一些示例:stap

https://github.com/jav/systemtap/blob/master/testsuite/systemtap.examples/index.txt

# stap nfsd_unlink.stp -c "sleep 0.2" The nfsdtop.stp script gathers and displays NFS lookups

© www.soinside.com 2019 - 2024. All rights reserved.