Skip to content

Commit 5747c80

Browse files
ttaylorrpeff
authored andcommitted
contrib/credential: avoid fixed-size buffer in osxkeychain
The macOS Keychain-based credential helper reads the newline-delimited protocol stream one line at a time by repeatedly calling fgets() into a fixed-size buffer, and is thus affected by the vulnerability described in the previous commit. To mitigate this attack, avoid using a fixed-size buffer, and instead rely on getline() to allocate a buffer as large as necessary to fit the entire content of the line, preventing any protocol injection. We solved a similar problem in a5bb10f (config: avoid fixed-sized buffer when renaming/deleting a section, 2023-04-06) by switching to strbuf_getline(). We can't do that here because the contrib helpers do not link with the rest of Git, and so can't use a strbuf. But we can use the system getline() directly, which works similarly. In most parts of Git we don't assume that every platform has getline(). But this helper is run only on OS X, and that platform added support in 10.7 ("Lion") which was released in 2011. Tested-by: Taylor Blau <[email protected]> Co-authored-by: Jeff King <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71201ab commit 5747c80

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

contrib/credential/osxkeychain/git-credential-osxkeychain.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,16 @@ static void add_internet_password(void)
113113

114114
static void read_credential(void)
115115
{
116-
char buf[1024];
116+
char *buf = NULL;
117+
size_t alloc;
118+
ssize_t line_len;
117119

118-
while (fgets(buf, sizeof(buf), stdin)) {
120+
while ((line_len = getline(&buf, &alloc, stdin)) > 0) {
119121
char *v;
120122

121123
if (!strcmp(buf, "\n"))
122124
break;
123-
buf[strlen(buf)-1] = '\0';
125+
buf[line_len-1] = '\0';
124126

125127
v = strchr(buf, '=');
126128
if (!v)
@@ -165,6 +167,8 @@ static void read_credential(void)
165167
* learn new lines, and the helpers are updated to match.
166168
*/
167169
}
170+
171+
free(buf);
168172
}
169173

170174
int main(int argc, const char **argv)

0 commit comments

Comments
 (0)