PROBLEM: NULL pointer dereference error in the "ufs_put_super_internal" function

From: 黄思聪
Date: Mon Oct 16 2023 - 00:26:45 EST


NULL pointer dereference error may occur in "ufs_put_super_internal" function.

The "ufs_put_super_internal" function located in "fs/ufs/super.c" may occur a NULL pointer dereference error when it calls the function "ubh_memcpyubh" defined in "fs/ufs/util.c". The relevant code definitions are as follows:
ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
ubh_memcpyubh (ubh, space, size);

It will call "ubh_bread" function to allocate enough memory to store the variable "ubh". The "ubh_bread" function may return NULL in various situations, especially when calling "kmalloc" function. If there are significant memory fragmentation issues, insufficient free memory blocks, or internal errors in the allocation function, the "kmalloc" function will return NULL. Finally, the "ubh" variable will get NULL, and the "ubh" will be passed as the first parameter to the "ubh_memcpyubh" function. This function will execute the following code snippet:
if (size > (ubh->count << uspi->s_fshift))
size = ubh->count << uspi->s_fshift;

If the "ubh" variable is NULL, it will cause an error. So I think that a null pointer check should be added in the function "ufs_put_super_internal".

My experimental kernel version is "LINUX 6.1", and this problem exists in all the version from "LINUX v3.0-rc1" to "LINUX v6.6-rc5".

Best regards,
Sicong Huang