簡體   English   中英

linux內核潛在的內存泄漏?

[英]Potential memory leak in linux kernel?

在對linux內核進行內存泄漏的靜態分析時,我遇到了一個有趣的場景,我無法找到變量的de分配。 分配發生在以下函數中(使用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_release函數中釋放,那么它可能會發生內存泄漏。

如果你看seq_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(file->private_data)

現在, file->private_datamounts_open_common中設置為

file->private_data = &p->m;

這就是p這是kmalloc'd在你的問題。 m成員不是指針,因此不應該允許釋放。 但是,它是struct proc_mounts的1.成員

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_KERNEL);獲得的地址相同p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);

我想這對靜態分析儀來說不是很友好。 但是沒有內存泄漏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM