Skip to content

Commit ac8cdb2

Browse files
committed
Fix inconsistent quoting of role names in ACLs.
getid() and putid(), which parse and deparse role names within ACL input/output, applied isalnum() to see if a character within a role name requires quoting. They did this even for non-ASCII characters, which is problematic because the results would depend on encoding, locale, and perhaps even platform. So it's possible that putid() could elect not to quote some string that, later in some other environment, getid() will decide is not a valid identifier, causing dump/reload or similar failures. To fix this in a way that won't risk interoperability problems with unpatched versions, make getid() treat any non-ASCII as a legitimate identifier character (hence not requiring quotes), while making putid() treat any non-ASCII as requiring quoting. We could remove the resulting excess quoting once we feel that no unpatched servers remain in the wild, but that'll be years. A lesser problem is that getid() did the wrong thing with an input consisting of just two double quotes (""). That has to represent an empty string, but getid() read it as a single double quote instead. The case cannot arise in the normal course of events, since we don't allow empty-string role names. But let's fix it while we're here. Although we've not heard field reports of problems with non-ASCII role names, there's clearly a hazard there, so back-patch to all supported versions. Reported-by: Peter Eisentraut <peter@eisentraut.org> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/3792884.1751492172@sss.pgh.pa.us Backpatch-through: 13
1 parent 0514616 commit ac8cdb2

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/backend/utils/adt/acl.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode);
115115
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue);
116116

117117

118+
/*
119+
* Test whether an identifier char can be left unquoted in ACLs.
120+
*
121+
* Formerly, we used isalnum() even on non-ASCII characters, resulting in
122+
* unportable behavior. To ensure dump compatibility with old versions,
123+
* we now treat high-bit-set characters as always requiring quoting during
124+
* putid(), but getid() will always accept them without quotes.
125+
*/
126+
static inline bool
127+
is_safe_acl_char(unsigned char c, bool is_getid)
128+
{
129+
if (IS_HIGHBIT_SET(c))
130+
return is_getid;
131+
return isalnum(c) || c == '_';
132+
}
133+
118134
/*
119135
* getid
120136
* Consumes the first alphanumeric string (identifier) found in string
@@ -137,21 +153,22 @@ getid(const char *s, char *n)
137153

138154
while (isspace((unsigned char) *s))
139155
s++;
140-
/* This code had better match what putid() does, below */
141156
for (;
142157
*s != '\0' &&
143-
(isalnum((unsigned char) *s) ||
144-
*s == '_' ||
145-
*s == '"' ||
146-
in_quotes);
158+
(in_quotes || *s == '"' || is_safe_acl_char(*s, true));
147159
s++)
148160
{
149161
if (*s == '"')
150162
{
163+
if (!in_quotes)
164+
{
165+
in_quotes = true;
166+
continue;
167+
}
151168
/* safe to look at next char (could be '\0' though) */
152169
if (*(s + 1) != '"')
153170
{
154-
in_quotes = !in_quotes;
171+
in_quotes = false;
155172
continue;
156173
}
157174
/* it's an escaped double quote; skip the escaping char */
@@ -185,10 +202,10 @@ putid(char *p, const char *s)
185202
const char *src;
186203
bool safe = true;
187204

205+
/* Detect whether we need to use double quotes */
188206
for (src = s; *src; src++)
189207
{
190-
/* This test had better match what getid() does, above */
191-
if (!isalnum((unsigned char) *src) && *src != '_')
208+
if (!is_safe_acl_char(*src, false))
192209
{
193210
safe = false;
194211
break;

src/test/regress/expected/privileges.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,26 @@ SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -
20762076
ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS FROM public;
20772077
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
20782078
ERROR: cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS
2079+
-- Test quoting and dequoting of user names in ACLs
2080+
CREATE ROLE "regress_""quoted";
2081+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
2082+
'SELECT', TRUE);
2083+
makeaclitem
2084+
------------------------------------------
2085+
"regress_""quoted"=r*/"regress_""quoted"
2086+
(1 row)
2087+
2088+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
2089+
aclitem
2090+
------------------------------------------
2091+
"regress_""quoted"=r*/"regress_""quoted"
2092+
(1 row)
2093+
2094+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
2095+
ERROR: a name must follow the "/" sign
2096+
LINE 1: SELECT '""=r*/""'::aclitem;
2097+
^
2098+
DROP ROLE "regress_""quoted";
20792099
--
20802100
-- Testing blanket default grants is very hazardous since it might change
20812101
-- the privileges attached to objects created by concurrent regression tests.

src/test/regress/sql/privileges.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,14 @@ ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS
12601260

12611261
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
12621262

1263+
-- Test quoting and dequoting of user names in ACLs
1264+
CREATE ROLE "regress_""quoted";
1265+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
1266+
'SELECT', TRUE);
1267+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
1268+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
1269+
DROP ROLE "regress_""quoted";
1270+
12631271
--
12641272
-- Testing blanket default grants is very hazardous since it might change
12651273
-- the privileges attached to objects created by concurrent regression tests.

0 commit comments

Comments
 (0)