Only zero sensitive information on destruction (and memory actually allocated by us)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/src/transports/cred.c b/src/transports/cred.c
index 05090ba..913ec36 100644
--- a/src/transports/cred.c
+++ b/src/transports/cred.c
@@ -30,7 +30,6 @@ static void plaintext_free(struct git_cred *cred)
git__free(c->password);
}
- git__memzero(c, sizeof(*c));
git__free(c);
}
@@ -73,8 +72,13 @@ static void ssh_key_free(struct git_cred *cred)
(git_cred_ssh_key *)cred;
git__free(c->username);
- git__free(c->publickey);
- git__free(c->privatekey);
+
+ if (c->privatekey) {
+ /* Zero the memory which previously held the private key */
+ size_t key_len = strlen(c->privatekey);
+ git__memzero(c->privatekey, key_len);
+ git__free(c->privatekey);
+ }
if (c->passphrase) {
/* Zero the memory which previously held the passphrase */
@@ -83,7 +87,13 @@ static void ssh_key_free(struct git_cred *cred)
git__free(c->passphrase);
}
- git__memzero(c, sizeof(*c));
+ if (c->publickey) {
+ /* Zero the memory which previously held the public key */
+ size_t key_len = strlen(c->publickey);
+ git__memzero(c->publickey, key_len);
+ git__free(c->publickey);
+ }
+
git__free(c);
}
@@ -93,7 +103,6 @@ static void ssh_interactive_free(struct git_cred *cred)
git__free(c->username);
- git__memzero(c, sizeof(*c));
git__free(c);
}
@@ -102,9 +111,14 @@ static void ssh_custom_free(struct git_cred *cred)
git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
git__free(c->username);
- git__free(c->publickey);
- git__memzero(c, sizeof(*c));
+ if (c->publickey) {
+ /* Zero the memory which previously held the publickey */
+ size_t key_len = strlen(c->publickey);
+ git__memzero(c->publickey, key_len);
+ git__free(c->publickey);
+ }
+
git__free(c);
}