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

Commit a58a631

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 7957e75 commit a58a631

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/backend/libpq/hba.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,11 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
11661166

11671167
ret = pg_getaddrinfo_all(str, NULL, &hints, &gai_result);
11681168
if (ret == 0 && gai_result)
1169+
{
11691170
memcpy(&parsedline->addr, gai_result->ai_addr,
11701171
gai_result->ai_addrlen);
1172+
parsedline->addrlen = gai_result->ai_addrlen;
1173+
}
11711174
else if (ret == EAI_NONAME)
11721175
parsedline->hostname = str;
11731176
else
@@ -1216,6 +1219,7 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
12161219
token->string);
12171220
return NULL;
12181221
}
1222+
parsedline->masklen = parsedline->addrlen;
12191223
pfree(str);
12201224
}
12211225
else if (!parsedline->hostname)
@@ -1266,6 +1270,7 @@ parse_hba_line(TokenizedLine *tok_line, int elevel)
12661270

12671271
memcpy(&parsedline->mask, gai_result->ai_addr,
12681272
gai_result->ai_addrlen);
1273+
parsedline->masklen = gai_result->ai_addrlen;
12691274
pg_freeaddrinfo_all(hints.ai_family, gai_result);
12701275

12711276
if (parsedline->addr.ss_family != parsedline->mask.ss_family)
@@ -2518,20 +2523,26 @@ fill_hba_line(Tuplestorestate *tuple_store, TupleDesc tupdesc,
25182523
}
25192524
else
25202525
{
2521-
if (pg_getnameinfo_all(&hba->addr, sizeof(hba->addr),
2522-
buffer, sizeof(buffer),
2523-
NULL, 0,
2524-
NI_NUMERICHOST) == 0)
2526+
/*
2527+
* Note: if pg_getnameinfo_all fails, it'll set buffer to
2528+
* "???", which we want to return.
2529+
*/
2530+
if (hba->addrlen > 0)
25252531
{
2526-
clean_ipv6_addr(hba->addr.ss_family, buffer);
2532+
if (pg_getnameinfo_all(&hba->addr, hba->addrlen,
2533+
buffer, sizeof(buffer),
2534+
NULL, 0,
2535+
NI_NUMERICHOST) == 0)
2536+
clean_ipv6_addr(hba->addr.ss_family, buffer);
25272537
addrstr = pstrdup(buffer);
25282538
}
2529-
if (pg_getnameinfo_all(&hba->mask, sizeof(hba->mask),
2530-
buffer, sizeof(buffer),
2531-
NULL, 0,
2532-
NI_NUMERICHOST) == 0)
2539+
if (hba->masklen > 0)
25332540
{
2534-
clean_ipv6_addr(hba->mask.ss_family, buffer);
2541+
if (pg_getnameinfo_all(&hba->mask, hba->masklen,
2542+
buffer, sizeof(buffer),
2543+
NULL, 0,
2544+
NI_NUMERICHOST) == 0)
2545+
clean_ipv6_addr(hba->mask.ss_family, buffer);
25352546
maskstr = pstrdup(buffer);
25362547
}
25372548
}

src/include/libpq/hba.h

+6
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,
@@ -108,6 +112,8 @@ typedef struct HbaLine
108112
char *radiusidentifiers_s;
109113
List *radiusports;
110114
char *radiusports_s;
115+
int addrlen; /* zero if we don't have a valid addr */
116+
int masklen; /* zero if we don't have a valid mask */
111117
} HbaLine;
112118

113119
typedef struct IdentLine

0 commit comments

Comments
 (0)