git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[patch] credential-osxkeychain: Clear username_buffer before getting the converted C string.

From
HJHong Jiang <ilford@gmail.com>
Date
Jul 31, 2024, 03:41 UTC
Message-ID
<CAEcKSiyo3dyNpGkE_FWE-Y710RV0H3EytM2psC=+by=4wP5qpg@mail.gmail.com>

I encountered this problem with homebrew after I upgraded to macOS 12.7.5, but I am not sure the OS upgrade is the only reason.

After `brew upgrade`, I received the following message:
Error: invalid byte sequence in UTF-8
/usr/local/Homebrew/Library/Homebrew/utils/github/api.rb:182:in `[]'
/usr/local/Homebrew/Library/Homebrew/utils/github/api.rb:182:in `block
in keychain_username_password'
The related lines in api.rb are:
        git_credential_out, _, result = system_command "git",
                                                       args:
["credential-osxkeychain", "get"],
                                                       input:
["protocol=https\n", "host=github.com\n"],
                                                       env:          {
"HOME" => uid_home }.compact,
                                                       print_stderr: false
        return unless result.success?
        github_username = git_credential_out[/username=(.+)/, 1]
        github_password = git_credential_out[/password=(.+)/, 1]
        return unless github_username

So it looks like that git_credential_out has invalid UTF-8 byte sequence. I print it after the system_command "git":

password=gho_SHADOWED username=jdp1024��` F� capability[]=state state[]=osxkeychain:seen=1

and
echo "protocol=https\nhost=github.com\n" | git credential-osxkeychain get
reproduced the problem.

So I made the patch, which zeros the username_buf before retrieving the converted C string.

From: Jiang Hong <ilford@gmail.com>
Date: Wed, 31 Jul 2024 11:05:44 +0800
Subject: [PATCH] Zeroing username_buffer before retrieving the
converted C string.
In macOS 12.7.5 and 12.7.6, the uninitialized username_buffer receives
a non-NULL-terminated C string.
---
 contrib/credential/osxkeychain/git-credential-osxkeychain.c | 1 +
 1 file changed, 1 insertion(+)
diff --git a/contrib/credential/osxkeychain/git-credential-osxkeychain.c
b/contrib/credential/osxkeychain/git-credential-osxkeychain.c
index 6ce22a28ed..89cd575bd5 100644
--- a/contrib/credential/osxkeychain/git-credential-osxkeychain.c
+++ b/contrib/credential/osxkeychain/git-credential-osxkeychain.c
@@ -137,6 +137,7 @@ static void find_username_in_item(CFDictionaryRef item)
  buffer_len = CFStringGetMaximumSizeForEncoding(
  CFStringGetLength(account_ref), ENCODING) + 1;
  username_buf = xmalloc(buffer_len);
+ memset(username_buf, 0, buffer_len);
  if (CFStringGetCString(account_ref,
  username_buf,
  buffer_len,
-- 
2.37.1 (Apple Git-137.1)
Next: Jeff King
Message 1 of 7 in “credential-osxkeychain: Clear username_buffer before getting the converted C string.”
  1. credential-osxkeychain: Clear username_buffer before getting the converted C string.Hong Jiang, Jul 31, 2024
  2. Jeff KingJul 31, 2024
  3. Bo AndersonJul 31, 2024
  4. credential/osxkeychain: respect NUL terminator in usernameJeff King, Aug 1, 2024
  5. Hong JiangAug 1, 2024
  6. Jeff KingAug 1, 2024
  7. Junio C HamanoAug 1, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.