kvm: properly check debugfs dentry before using it [Linux 5.0]

This Linux kernel change "kvm: properly check debugfs dentry before using it" is included in the Linux 5.0 release. This change is authored by Greg Kroah-Hartman <gregkh [at] linuxfoundation.org> on Thu Feb 28 16:34:37 2019 +0100. The commit for this change in Linux stable tree is 8ed0579 (patch).

kvm: properly check debugfs dentry before using it

debugfs can now report an error code if something went wrong instead of
just NULL.  So if the return value is to be used as a "real" dentry, it
needs to be checked if it is an error before dereferencing it.

This is now happening because of ff9fb72bc077 ("debugfs: return error
values, not NULL").  syzbot has found a way to trigger multiple debugfs
files attempting to be created, which fails, and then the error code
gets passed to dentry_path_raw() which obviously does not like it.

Reported-by: Eric Biggers <[email protected]>
Reported-and-tested-by: [email protected]
Cc: "Radim Krčmář" <[email protected]>
Cc: [email protected]
Acked-by: Paolo Bonzini <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 virt/kvm/kvm_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 5858452..076bc38 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4044,7 +4044,7 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
    }
    add_uevent_var(env, "PID=%d", kvm->userspace_pid);

-   if (kvm->debugfs_dentry) {
+   if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) {
        char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL);

        if (p) {

Leave a Reply

Your email address will not be published. Required fields are marked *