Skip to contentSkip to conversation

xfs_io: fix memory leak in add_enckey

PATCH In review Eric Sandeen 5 participants 8 messages Nov 7, 2019 – Nov 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_io: fix memory leak in add_enckey

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

---

io/encrypt.c +1 -0
index 17d61cfb..c6a4e190 100644
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Eric Sandeen

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

Quoted 15 lines

---

io/encrypt.c +1 -0
index 17d61cfb..c6a4e190 100644
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}
#2 lore raw
In reply to Eric Sandeen
Quoted 14 lines · Eric Sandeen

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

---

io/encrypt.c +0 -0
index 17d61cfb..c6a4e190 100644

io/encrypt.c add_enckey_f(int argc, char **argv) · line 702

Quoted 7 lines
io/encrypt.c +1 -0
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}

The same leak happens later in the function too. How about this instead:

io/encrypt.c +8 -4
index 17d61cfb..de48c50c 100644
@@ -678,6 +678,7 @@ add_enckey_f(int argc, char **argv)
 	int c;
 	struct fscrypt_add_key_arg *arg;
 	ssize_t raw_size;
+	int retval = 0;
 
 	arg = calloc(1, sizeof(*arg) + FSCRYPT_MAX_KEY_SIZE + 1);
 	if (!arg) {
@@ -696,14 +697,17 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
-			return command_usage(&add_enckey_cmd);
+			retval = command_usage(&add_enckey_cmd);
+			goto out;
 		}
 	}
 	argc -= optind;
 	argv += optind;
 
-	if (argc != 0)
-		return command_usage(&add_enckey_cmd);
+	if (argc != 0) {
+		retval = command_usage(&add_enckey_cmd);
+		goto out;
+	}
 
 	raw_size = read_until_limit_or_eof(STDIN_FILENO, arg->raw,
 					   FSCRYPT_MAX_KEY_SIZE + 1);
@@ -732,7 +736,7 @@ add_enckey_f(int argc, char **argv)
 out:
 	memset(arg->raw, 0, FSCRYPT_MAX_KEY_SIZE + 1);
 	free(arg);
-	return 0;
+	return retval;
 }
 
 static int
#3 lore raw
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Eric Sandeen

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

---

Quoted 15 lines

io/encrypt.c add_enckey_f(int argc, char **argv) · line 702

Quoted 13 lines
io/encrypt.c +1 -0
index 17d61cfb..c6a4e190 100644
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}

The same leak happens later in the function too. How about this instead:

whoops yes it does. I kind of hate "retval = command_usage" but seeing the memset of the key on the way out it's probably prudent to have one common exit point after the function gets started.

Care to send this as a formal patch?

Thanks,
-Eric

Quoted 43 lines
io/encrypt.c +8 -4
index 17d61cfb..de48c50c 100644
@@ -678,6 +678,7 @@ add_enckey_f(int argc, char **argv)
 	int c;
 	struct fscrypt_add_key_arg *arg;
 	ssize_t raw_size;
+	int retval = 0;
 
 	arg = calloc(1, sizeof(*arg) + FSCRYPT_MAX_KEY_SIZE + 1);
 	if (!arg) {
@@ -696,14 +697,17 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
-			return command_usage(&add_enckey_cmd);
+			retval = command_usage(&add_enckey_cmd);
+			goto out;
 		}
 	}
 	argc -= optind;
 	argv += optind;
 
-	if (argc != 0)
-		return command_usage(&add_enckey_cmd);
+	if (argc != 0) {
+		retval = command_usage(&add_enckey_cmd);
+		goto out;
+	}
 
 	raw_size = read_until_limit_or_eof(STDIN_FILENO, arg->raw,
 					   FSCRYPT_MAX_KEY_SIZE + 1);
@@ -732,7 +736,7 @@ add_enckey_f(int argc, char **argv)
 out:
 	memset(arg->raw, 0, FSCRYPT_MAX_KEY_SIZE + 1);
 	free(arg);
-	return 0;
+	return retval;
 }
 
 static int
#4 lore raw
Quoted 26 lines · Eric Sandeen
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Eric Sandeen

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

---

Quoted 15 lines

io/encrypt.c add_enckey_f(int argc, char **argv) · line 702

Quoted 13 lines
io/encrypt.c +1 -0
index 17d61cfb..c6a4e190 100644
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}

The same leak happens later in the function too. How about this instead:

Quoted 5 lines

whoops yes it does. I kind of hate "retval = command_usage" but seeing the memset of the key on the way out it's probably prudent to have one common exit point after the function gets started.

Care to send this as a formal patch?

<interprets silence as a "no"> ;)

I'll just incorporate your fixes as an addendum to my patch, then.

-Eric

#5 lore raw
Quoted 31 lines · Eric Sandeen
Quoted 30 lines · Eric Sandeen
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Eric Sandeen

Invalid arguments to add_enckey will leak the "arg" allocation, so fix that.

Fixes ba71de04 ("xfs_io/encrypt: add 'add_enckey' command")

Fixes-coverity-id: 1454644

---

Quoted 15 lines

io/encrypt.c add_enckey_f(int argc, char **argv) · line 702

Quoted 13 lines
io/encrypt.c +1 -0
index 17d61cfb..c6a4e190 100644
@@ -696,6 +696,7 @@ add_enckey_f(int argc, char **argv)
 				goto out;
 			break;
 		default:
+			free(arg);
 			return command_usage(&add_enckey_cmd);
 		}
 	}

The same leak happens later in the function too. How about this instead:

whoops yes it does. I kind of hate "retval = command_usage" but seeing the memset of the key on the way out it's probably prudent to have one common exit point after the function gets started.

Quoted 7 lines
Quoted 1 line

Care to send this as a formal patch?

<interprets silence as a "no"> ;)

I'll just incorporate your fixes as an addendum to my patch, then.

-Eric

Sorry, I didn't receive this because I was dropped from Cc, and I'm not currently subscribed to linux-xfs. The patch you committed looks fine, thanks.

- Eric

#6 lore raw

On 11/11/19 11:27 AM, Eric Biggers wrote:

...

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 1 line

The same leak happens later in the function too. How about this instead:

whoops yes it does. I kind of hate "retval = command_usage" but seeing the memset of the key on the way out it's probably prudent to have one common exit point after the function gets started.

Care to send this as a formal patch?

Quoted 8 lines
Quoted 5 lines

<interprets silence as a "no"> ;)

I'll just incorporate your fixes as an addendum to my patch, then.

-Eric

Sorry, I didn't receive this because I was dropped from Cc, and I'm not currently subscribed to linux-xfs. The patch you committed looks fine, thanks.

Oh, I'm sorry about that, my mistake.

Thanks,
-Eric

#7 lore raw
Quoted 6 lines · Eric Sandeen
Quoted 3 lines

Sorry, I didn't receive this because I was dropped from Cc, and I'm not currently subscribed to linux-xfs. The patch you committed looks fine, thanks.

Oh, I'm sorry about that, my mistake.

And it happened again :-)

For the record, this seems to have been my fault. My .muttrc was missing

set followup_to = no

and at some point I had added mailing list declarations including:

subscribe .*@vger.kernel.org

so Mutt was generating a Mail-Followup-To header excluding me. I hadn't noticed this problem earlier because I'm normally subscribed to one of the lists anyway.

- Eric