Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7827497

Browse files
committed
Fix unportable use of getnameinfo() in pg_hba_file_rules view.
fill_hba_line() thought it could get away with passing sizeof(struct sockaddr_storage) rather than the actual addrlen previously returned by getaddrinfo(). While that appears to work on many platforms, it does not work on FreeBSD 11: you get back a failure, which leads to the view showing NULL for the address and netmask columns in all rows. The POSIX spec for getnameinfo() is pretty clearly on FreeBSD's side here: you should pass the actual address length. So it seems plausible that there are other platforms where this coding also fails, and we just hadn't noticed. Also, IMO the fact that getnameinfo() failure leads to a NULL output is pretty bogus in itself. Our pg_getnameinfo_all() wrapper is careful to emit "???" on failure, and we should use that in such cases. NULL should only be emitted in rows that don't have IP addresses. Per bug #16695 from Peter Vandivier. Back-patch to v10 where this code was added. Discussion: https://postgr.es/m/16695-a665558e2f630be7@postgresql.org
1 parent 06801ae commit 7827497

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/backend/libpq/hba.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,11 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
11511151

11521152
ret = pg_getaddrinfo_all(str, NULL, &hints, &gai_result);
11531153
if (ret == 0 && gai_result)
1154+
{
11541155
memcpy(&parsedline->addr, gai_result->ai_addr,
11551156
gai_result->ai_addrlen);
1157+
parsedline->addrlen = gai_result->ai_addrlen;
1158+
}
11561159
else if (ret == EAI_NONAME)
11571160
parsedline->hostname = str;
11581161
else
@@ -1201,6 +1204,7 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
12011204
token->string);
12021205
return NULL;
12031206
}
1207+
parsedline->masklen = parsedline->addrlen;
12041208
pfree(str);
12051209
}
12061210
else if (!parsedline->hostname)
@@ -1251,6 +1255,7 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
12511255

12521256
memcpy(&parsedline->mask, gai_result->ai_addr,
12531257
gai_result->ai_addrlen);
1258+
parsedline->masklen = gai_result->ai_addrlen;
12541259
pg_freeaddrinfo_all(hints.ai_family, gai_result);
12551260

12561261
if (parsedline->addr.ss_family != parsedline->mask.ss_family)
@@ -2420,20 +2425,26 @@ fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
24202425
}
24212426
else
24222427
{
2423-
if (pg_getnameinfo_all(&hba->addr, sizeof(hba->addr),
2424-
buffer, sizeof(buffer),
2425-
NULL, 0,
2426-
NI_NUMERICHOST) == 0)
2428+
/*
2429+
* Note: if pg_getnameinfo_all fails, it'll set buffer to
2430+
* "???", which we want to return.
2431+
*/
2432+
if (hba->addrlen > 0)
24272433
{
2428-
clean_ipv6_addr(hba->addr.ss_family, buffer);
2434+
if (pg_getnameinfo_all(&hba->addr, hba->addrlen,
2435+
buffer, sizeof(buffer),
2436+
NULL, 0,
2437+
NI_NUMERICHOST) == 0)
2438+
clean_ipv6_addr(hba->addr.ss_family, buffer);
24292439
addrstr = pstrdup(buffer);
24302440
}
2431-
if (pg_getnameinfo_all(&hba->mask, sizeof(hba->mask),
2432-
buffer, sizeof(buffer),
2433-
NULL, 0,
2434-
NI_NUMERICHOST) == 0)
2441+
if (hba->masklen > 0)
24352442
{
2436-
clean_ipv6_addr(hba->mask.ss_family, buffer);
2443+
if (pg_getnameinfo_all(&hba->mask, hba->masklen,
2444+
buffer, sizeof(buffer),
2445+
NULL, 0,
2446+
NI_NUMERICHOST) == 0)
2447+
clean_ipv6_addr(hba->mask.ss_family, buffer);
24372448
maskstr = pstrdup(buffer);
24382449
}
24392450
}

src/include/libpq/hba.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ typedef enum UserAuth
4242
#define USER_AUTH_LAST uaPeer /* Must be last value of this enum */
4343
} UserAuth;
4444

45+
/*
46+
* Data structures representing pg_hba.conf entries
47+
*/
48+
4549
typedef enum IPCompareMethod
4650
{
4751
ipCmpMask,
@@ -97,6 +101,8 @@ typedef struct HbaLine
97101
char *radiusidentifiers_s;
98102
List *radiusports;
99103
char *radiusports_s;
104+
int addrlen; /* zero if we don't have a valid addr */
105+
int masklen; /* zero if we don't have a valid mask */
100106
} HbaLine;
101107

102108
typedef struct IdentLine

0 commit comments

Comments
 (0)