Skip to contentSkip to conversation

mm: fix dereferencing possible ERR_PTR

PATCH Applied Binyi Han 2 participants 2 messages Sep 4, 2022

Subsystems (3)
lore
Share & tools
lore

Binyi Han

author
#0 lore raw

PATCH 1/1 mm: fix dereferencing possible ERR_PTR

Smatch checker complains that 'secretmem_mnt' dereferencing possible ERR_PTR().
Let the function return if 'secretmem_mnt' is ERR_PTR, to avoid deferencing it.

---
 mm/secretmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/secretmem.c +1 -1
index e3e9590c6fb3..3f7154099795 100644
@@ -285,7 +285,7 @@ static int secretmem_init(void)
 
 	secretmem_mnt = kern_mount(&secretmem_fs);
 	if (IS_ERR(secretmem_mnt))
-		ret = PTR_ERR(secretmem_mnt);
+		return PTR_ERR(secretmem_mnt);
 
 	/* prevent secretmem mappings from ever getting PROT_EXEC */
 	secretmem_mnt->mnt_flags |= MNT_NOEXEC;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Binyi Han

Smatch checker complains that 'secretmem_mnt' dereferencing possible ERR_PTR().
Let the function return if 'secretmem_mnt' is ERR_PTR, to avoid deferencing it.

---

Fixes 1507f51255c9ff07d75909a84e7c0d7f3c4b2f49 ("mm: introduce memfd_secret system call to create "secret" memory areas")
Quoted 8 lines
  mm/secretmem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/secretmem.c +0 -0
index e3e9590c6fb3..3f7154099795 100644

mm/secretmem.c static int secretmem_init(void) · line 291

Quoted 8 lines
mm/secretmem.c +1 -1
@@ -285,7 +285,7 @@ static int secretmem_init(void)
 
  	secretmem_mnt = kern_mount(&secretmem_fs);
  	if (IS_ERR(secretmem_mnt))
-		ret = PTR_ERR(secretmem_mnt);
+		return PTR_ERR(secretmem_mnt);
 
  	/* prevent secretmem mappings from ever getting PROT_EXEC */
  	secretmem_mnt->mnt_flags |= MNT_NOEXEC;

I agree that doing:

   secretmem_mnt->mnt_flags |= MNT_NOEXEC;

when IS_ERR(secretmem_mnt) evaluates to true is wrong. But I have a question: what happen if you invoke memfd_secret() syscall when @secretmem_mnt is an ERR_PTR?

Shouldn't we also guard the memfd_secret() path?

mm/secretmem.c +3 -0
index e3e9590c6fb3..2d52508d47a9 100644
@@ -230,18 +230,21 @@ static struct file *secretmem_file_create(unsigned long flags)
 
 SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)
 {
 	struct file *file;
 	int fd, err;
 
 	/* make sure local flags do not confict with global fcntl.h */
 	BUILD_BUG_ON(SECRETMEM_FLAGS_MASK & O_CLOEXEC);
 
+	if (IS_ERR(secretmem_mnt))
+		return PTR_ERR(secretmem_mnt);
+
 	if (!secretmem_enable)
 		return -ENOSYS;
 
 	if (flags & ~(SECRETMEM_FLAGS_MASK | O_CLOEXEC))
 		return -EINVAL;
 	if (atomic_read(&secretmem_users) < 0)
 		return -ENFILE;
 
 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
Signature · 1 line
Ammar Faizi