C linux内核中的潜在内存泄漏?

C linux内核中的潜在内存泄漏?,c,linux,memory-leaks,linux-kernel,C,Linux,Memory Leaks,Linux Kernel,在对linux内核进行静态内存泄漏分析时,我遇到了一个有趣的场景,在这个场景中,我无法找到变量的解分配。 分配发生在以下函数中(使用kmalloc调用),如下所示: static int mounts_open_common(struct inode *inode, struct file *file, int (*show)(struct seq_file *, struct vfsmount *)){ struct proc_mounts *p; //s

在对linux内核进行静态内存泄漏分析时,我遇到了一个有趣的场景,在这个场景中,我无法找到变量的解分配。 分配发生在以下函数中(使用kmalloc调用),如下所示:

static int mounts_open_common(struct inode *inode, struct file *file,
              int (*show)(struct seq_file *, struct vfsmount *)){
  struct proc_mounts *p;

  //some code//
  *p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);**
  file->private_data = &p->m;//the allocated variable is escaped to file structure
  //some code

}
我希望分配的内存固定在:

static int mounts_release(struct inode *inode, struct file *file)
{
    struct proc_mounts *p = proc_mounts(file->private_data);
    path_put(&p->root);
    put_mnt_ns(p->ns);
    return seq_release(inode, file);
}
但是这个函数似乎正在访问分配的变量来释放它的一些内部成员,而不是变量“p”本身。
那么这个变量的内存在哪里被释放呢?如果它应该在mounts\u release函数中释放,那么它可能是内存泄漏。

如果您查看以下内容:

int seq_release(struct inode *inode, struct file *file)
{
        struct seq_file *m = file->private_data;
        kvfree(m->buf);
        kfree(m);
        return 0;
}
它有效地执行
kfree(文件->私有\u数据)

现在,
file->private\u data
设置为

这就是你问题中的
p
,它是
kmalloc
m
成员不是指针,因此不允许释放它。然而,它是1。
struct proc_mounts的成员

struct proc_mounts {
        struct seq_file m;
        struct mnt_namespace *ns;
        struct path root;
        int (*show)(struct seq_file *, struct vfsmount *);
        void *cached_mount;
        u64 cached_event;
        loff_t cached_index;
};
因此
seq_release()
m
成员的地址执行kfree(),该地址与使用
p=kmalloc(sizeof(struct proc_mounts),GFP_内核获得的地址相同


我想这对静态分析器不是很友好。但是没有内存泄漏

什么是seq_发布(inode,文件);调用mounts_release()函数应该释放与已安装设备相关的内存,据我所知!在seq_release()函数中,明确指出m是seq_file类型的指针,因此kfree(m)将释放由该指针分配的内存(我认为这将在原始mounts_open_common()函数调用的seq_open()函数中分配)而不是变量p所指的父结构proc_point。linux还使用宏的容器_从其子成员中提取父指针。您可以在generic_create_cred()/generic_free_cred函数中检查…………您不能使用类型为结构第一个成员的变量以及指向结构第一个成员地址的变量来释放整个父结构。PurofHy不,它没有按照您的建议在seq_open()中分配,请查看1。struct proc_挂载的成员,它不是指针。查看进程挂载的分配位置。看看
file->private\u data
的设置位置,这就是线索。查看
seq_open()
并查看当
file->private_data
不为空(它不分配任何内容)时它会做什么。请注意,文件->私有数据是如何在seq_release()中被释放的。@PurofHy所以最后
file->private
是传递给
kfree()
的数据,这与在前面提到的第257行中获得的地址相同。还请注意,C保证结构的第一个成员的地址与结构本身的地址相同。(即,其偏移量始终为0)。这里的代码中没有使用宏的容器,因为不需要提取任何包含宏的结构。这只是因为
struct seq_file m
是1。
struct proc\u mounts的成员
,因此
kfree(p)
与执行
kfree(&p->m)
struct proc_mounts {
        struct seq_file m;
        struct mnt_namespace *ns;
        struct path root;
        int (*show)(struct seq_file *, struct vfsmount *);
        void *cached_mount;
        u64 cached_event;
        loff_t cached_index;
};