Skip to content

Commit 3344582

Browse files
committed
Fix double-free bug in GSS authentication.
The logic to free the buffer after the gss_init_sec_context() call was always a bit wonky. Because gss_init_sec_context() sets the GSS context variable, conn->gctx, we would in fact always attempt to free the buffer. That only works, because previously conn->ginbuf.value was initialized to NULL, and free(NULL) is a no-op. Commit 61bf96c refactored things so that the GSS input token buffer is allocated locally in pg_GSS_continue, and not held in the PGconn object. After that, the now-local ginbuf.value variable isn't initialized when it's not used, so we pass a bogus pointer to free(). To fix, only try to free the input buffer if we allocated it. That was the intention, certainly after the refactoring, and probably even before that. But because there's no live bug before the refactoring, I refrained from backpatching this. The bug was also independently reported by Graham Dutton, as bug #14690. Patch reviewed by Michael Paquier. Discussion: https://www.postgresql.org/message-id/6288d80e-a0bf-d4d3-4e12-7b79c77f1771%40iki.fi Discussion: https://www.postgresql.org/message-id/20170605130954.1438.90535%40wrigleys.postgresql.org
1 parent d4bfc06 commit 3344582

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/interfaces/libpq/fe-auth.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ pg_GSS_continue(PGconn *conn, int payloadlen)
133133
return STATUS_ERROR;
134134
}
135135
}
136+
else
137+
{
138+
ginbuf.length = 0;
139+
ginbuf.value = NULL;
140+
}
136141

137142
maj_stat = gss_init_sec_context(&min_stat,
138143
GSS_C_NO_CREDENTIAL,
@@ -142,13 +147,13 @@ pg_GSS_continue(PGconn *conn, int payloadlen)
142147
GSS_C_MUTUAL_FLAG,
143148
0,
144149
GSS_C_NO_CHANNEL_BINDINGS,
145-
(conn->gctx == GSS_C_NO_CONTEXT) ? GSS_C_NO_BUFFER : &ginbuf,
150+
(ginbuf.value == NULL) ? GSS_C_NO_BUFFER : &ginbuf,
146151
NULL,
147152
&goutbuf,
148153
NULL,
149154
NULL);
150155

151-
if (conn->gctx != GSS_C_NO_CONTEXT)
156+
if (ginbuf.value)
152157
free(ginbuf.value);
153158

154159
if (goutbuf.length != 0)

0 commit comments

Comments
 (0)