1From d9d6e0bff831da03f4448f0cdb82fc3d143662c8 Mon Sep 17 00:00:00 2001
2From: Holger Liebig <holger.liebig@ts.fujitsu.com>
3Date: Tue, 4 Apr 2017 20:43:05 +0200
4Subject: [PATCH] ID:480 - Call EVP_CIPHER_CTX_free() instead of
5 EVP_CIPHER_CTX_cleanup()
6
7Call EVP_CIPHER_CTX_free() instead of EVP_CIPHER_CTX_cleanup() to fix memory
8leak.
9
10Upstream: https://github.com/ipmitool/ipmitool/commit/1664902525a1c3771b4d8b3ccab7ea1ba6b2bdd1
11
12Signed-off-by: Matthew Weber <matthew.weber@rockwellcollins.com>
13---
14 src/plugins/lanplus/lanplus_crypt_impl.c | 44 +++++++++++++++++---------------
15 1 file changed, 23 insertions(+), 21 deletions(-)
16
17diff --git a/src/plugins/lanplus/lanplus_crypt_impl.c b/src/plugins/lanplus/lanplus_crypt_impl.c
18index 0e330c1..9652a5e 100644
19--- a/src/plugins/lanplus/lanplus_crypt_impl.c
20+++ b/src/plugins/lanplus/lanplus_crypt_impl.c
21@@ -165,13 +165,6 @@ lanplus_encrypt_aes_cbc_128(const uint8_t * iv,
22 							uint32_t        * bytes_written)
23 {
24 	EVP_CIPHER_CTX *ctx = NULL;
25-	ctx = EVP_CIPHER_CTX_new();
26-	if (ctx == NULL) {
27-		*bytes_written = 0;
28-		return;
29-	}
30-	EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
31-	EVP_CIPHER_CTX_set_padding(ctx, 0);
32
33 	*bytes_written = 0;
34
35@@ -185,6 +178,14 @@ lanplus_encrypt_aes_cbc_128(const uint8_t * iv,
36 		printbuf(input, input_length, "encrypting this data");
37 	}
38
39+	ctx = EVP_CIPHER_CTX_new();
40+	if (ctx == NULL) {
41+		lprintf(LOG_DEBUG, "ERROR: EVP_CIPHER_CTX_new() failed");
42+		return;
43+	}
44+	EVP_CIPHER_CTX_init(ctx);
45+	EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
46+	EVP_CIPHER_CTX_set_padding(ctx, 0);
47
48 	/*
49 	 * The default implementation adds a whole block of padding if the input
50@@ -198,7 +199,6 @@ lanplus_encrypt_aes_cbc_128(const uint8_t * iv,
51 	{
52 		/* Error */
53 		*bytes_written = 0;
54-		return;
55 	}
56 	else
57 	{
58@@ -206,16 +206,17 @@ lanplus_encrypt_aes_cbc_128(const uint8_t * iv,
59
60 		if(!EVP_EncryptFinal_ex(ctx, output + *bytes_written, (int *)&tmplen))
61 		{
62+			/* Error */
63 			*bytes_written = 0;
64-			return; /* Error */
65 		}
66 		else
67 		{
68 			/* Success */
69 			*bytes_written += tmplen;
70-			EVP_CIPHER_CTX_cleanup(ctx);
71 		}
72 	}
73+	/* performs cleanup and free */
74+	EVP_CIPHER_CTX_free(ctx);
75 }
76
77
78@@ -243,13 +244,6 @@ lanplus_decrypt_aes_cbc_128(const uint8_t * iv,
79 							uint32_t        * bytes_written)
80 {
81 	EVP_CIPHER_CTX *ctx = NULL;
82-	ctx = EVP_CIPHER_CTX_new();
83-	if (ctx == NULL) {
84-		*bytes_written = 0;
85-		return;
86-	}
87-	EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
88-	EVP_CIPHER_CTX_set_padding(ctx, 0);
89
90 	if (verbose >= 5)
91 	{
92@@ -258,12 +252,20 @@ lanplus_decrypt_aes_cbc_128(const uint8_t * iv,
93 		printbuf(input, input_length, "decrypting this data");
94 	}
95
96-
97 	*bytes_written = 0;
98
99 	if (input_length == 0)
100 		return;
101
102+	ctx = EVP_CIPHER_CTX_new();
103+	if (ctx == NULL) {
104+		lprintf(LOG_DEBUG, "ERROR: EVP_CIPHER_CTX_new() failed");
105+		return;
106+	}
107+	EVP_CIPHER_CTX_init(ctx);
108+	EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
109+	EVP_CIPHER_CTX_set_padding(ctx, 0);
110+
111 	/*
112 	 * The default implementation adds a whole block of padding if the input
113 	 * data is perfectly aligned.  We would like to keep that from happening.
114@@ -277,7 +279,6 @@ lanplus_decrypt_aes_cbc_128(const uint8_t * iv,
115 		/* Error */
116 		lprintf(LOG_DEBUG, "ERROR: decrypt update failed");
117 		*bytes_written = 0;
118-		return;
119 	}
120 	else
121 	{
122@@ -285,20 +286,21 @@ lanplus_decrypt_aes_cbc_128(const uint8_t * iv,
123
124 		if (!EVP_DecryptFinal_ex(ctx, output + *bytes_written, (int *)&tmplen))
125 		{
126+			/* Error */
127 			char buffer[1000];
128 			ERR_error_string(ERR_get_error(), buffer);
129 			lprintf(LOG_DEBUG, "the ERR error %s", buffer);
130 			lprintf(LOG_DEBUG, "ERROR: decrypt final failed");
131 			*bytes_written = 0;
132-			return; /* Error */
133 		}
134 		else
135 		{
136 			/* Success */
137 			*bytes_written += tmplen;
138-			EVP_CIPHER_CTX_cleanup(ctx);
139 		}
140 	}
141+	/* performs cleanup and free */
142+	EVP_CIPHER_CTX_free(ctx);
143
144 	if (verbose >= 5)
145 	{
146--
1471.9.1
148
149