diff mbox series

[3/7] e2fsprogs: fix unexpected NULL variable

Message ID 20210806095820.83731-3-lczerner@redhat.com
State Accepted
Headers show
Series [1/7] e2fsck: value stored to err is never read | expand

Commit Message

Lukas Czerner Aug. 6, 2021, 9:58 a.m. UTC
The ext2fs_check_mount_point() function can be called with mtpt being
NULL as for example from ext2fs_check_if_mounted(). However in the
is_swap_device condition we use the mtpt in strncpy without checking
whether it is non-null first.

This should not be a problem on linux since the previous attempt to open
the device exclusively would have prevented us from ever reaching the
problematic strncpy. However it's still a bug and can cause problems on
other systems, fix it by conditioning strncpy on mtpt not being null.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
---
 lib/ext2fs/ismounted.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Theodore Ts'o Aug. 10, 2021, 3:01 p.m. UTC | #1
On Fri, Aug 06, 2021 at 11:58:16AM +0200, Lukas Czerner wrote:
> The ext2fs_check_mount_point() function can be called with mtpt being
> NULL as for example from ext2fs_check_if_mounted(). However in the
> is_swap_device condition we use the mtpt in strncpy without checking
> whether it is non-null first.
> 
> This should not be a problem on linux since the previous attempt to open
> the device exclusively would have prevented us from ever reaching the
> problematic strncpy. However it's still a bug and can cause problems on
> other systems, fix it by conditioning strncpy on mtpt not being null.
> 
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>

Applied, thanks.

					- Ted
diff mbox series

Patch

diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c
index c9e6a9d0..aee7d726 100644
--- a/lib/ext2fs/ismounted.c
+++ b/lib/ext2fs/ismounted.c
@@ -393,7 +393,8 @@  errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
 
 	if (is_swap_device(device)) {
 		*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
-		strncpy(mtpt, "<swap>", mtlen);
+		if (mtpt)
+			strncpy(mtpt, "<swap>", mtlen);
 	} else {
 #ifdef HAVE_SETMNTENT
 		retval = check_mntent(device, mount_flags, mtpt, mtlen);