Skip to contentSkip to conversation

mm: Fix potential memory leak in sys_swapon

PATCH Applied Miaohe Lin 2 participants 2 messages Sep 30, 2020

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm: Fix potential memory leak in sys_swapon

If we failed to drain inode, we would forget to free the swap address space allocated by init_swap_address_space() above.

Fixes dc617f29dbe5 ("vfs: don't allow writes to swap files")
---
 mm/swapfile.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/swapfile.c +3 -1
index 2b0c5fc1b0e6..4522b458a814 100644
@@ -3342,7 +3342,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	error = inode_drain_writes(inode);
 	if (error) {
 		inode->i_flags &= ~S_SWAPFILE;
-		goto bad_swap_unlock_inode;
+		goto free_swap_address_space;
 	}
 
 	mutex_lock(&swapon_mutex);
@@ -3367,6 +3367,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 
 	error = 0;
 	goto out;
+free_swap_address_space:
+	exit_swap_address_space(p->type);
 bad_swap_unlock_inode:
 	inode_unlock(inode);
 bad_swap:
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Miaohe Lin

If we failed to drain inode, we would forget to free the swap address space allocated by init_swap_address_space() above.

Fixes dc617f29dbe5 ("vfs: don't allow writes to swap files")

LGTM,

--D

Quoted 29 lines
---
 mm/swapfile.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/swapfile.c +3 -1
index 2b0c5fc1b0e6..4522b458a814 100644
@@ -3342,7 +3342,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	error = inode_drain_writes(inode);
 	if (error) {
 		inode->i_flags &= ~S_SWAPFILE;
-		goto bad_swap_unlock_inode;
+		goto free_swap_address_space;
 	}
 
 	mutex_lock(&swapon_mutex);
@@ -3367,6 +3367,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 
 	error = 0;
 	goto out;
+free_swap_address_space:
+	exit_swap_address_space(p->type);
 bad_swap_unlock_inode:
 	inode_unlock(inode);
 bad_swap:
Signature · 1 line
2.19.1