Skip to content

Commit de73cb3

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 e3584e4 commit de73cb3

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
@@ -118,6 +118,22 @@ static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode);
118118
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue);
119119

120120

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

141157
while (isspace((unsigned char) *s))
142158
s++;
143-
/* This code had better match what putid() does, below */
144159
for (;
145160
*s != '\0' &&
146-
(isalnum((unsigned char) *s) ||
147-
*s == '_' ||
148-
*s == '"' ||
149-
in_quotes);
161+
(in_quotes || *s == '"' || is_safe_acl_char(*s, true));
150162
s++)
151163
{
152164
if (*s == '"')
153165
{
166+
if (!in_quotes)
167+
{
168+
in_quotes = true;
169+
continue;
170+
}
154171
/* safe to look at next char (could be '\0' though) */
155172
if (*(s + 1) != '"')
156173
{
157-
in_quotes = !in_quotes;
174+
in_quotes = false;
158175
continue;
159176
}
160177
/* it's an escaped double quote; skip the escaping char */
@@ -188,10 +205,10 @@ putid(char *p, const char *s)
188205
const char *src;
189206
bool safe = true;
190207

208+
/* Detect whether we need to use double quotes */
191209
for (src = s; *src; src++)
192210
{
193-
/* This test had better match what getid() does, above */
194-
if (!isalnum((unsigned char) *src) && *src != '_')
211+
if (!is_safe_acl_char(*src, false))
195212
{
196213
safe = false;
197214
break;

src/test/regress/expected/privileges.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,26 @@ SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -
21842184
ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS FROM public;
21852185
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
21862186
ERROR: cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS
2187+
-- Test quoting and dequoting of user names in ACLs
2188+
CREATE ROLE "regress_""quoted";
2189+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
2190+
'SELECT', TRUE);
2191+
makeaclitem
2192+
------------------------------------------
2193+
"regress_""quoted"=r*/"regress_""quoted"
2194+
(1 row)
2195+
2196+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
2197+
aclitem
2198+
------------------------------------------
2199+
"regress_""quoted"=r*/"regress_""quoted"
2200+
(1 row)
2201+
2202+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
2203+
ERROR: a name must follow the "/" sign
2204+
LINE 1: SELECT '""=r*/""'::aclitem;
2205+
^
2206+
DROP ROLE "regress_""quoted";
21872207
--
21882208
-- Testing blanket default grants is very hazardous since it might change
21892209
-- 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
@@ -1387,6 +1387,14 @@ ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS
13871387

13881388
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
13891389

1390+
-- Test quoting and dequoting of user names in ACLs
1391+
CREATE ROLE "regress_""quoted";
1392+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
1393+
'SELECT', TRUE);
1394+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
1395+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
1396+
DROP ROLE "regress_""quoted";
1397+
13901398
--
13911399
-- Testing blanket default grants is very hazardous since it might change
13921400
-- the privileges attached to objects created by concurrent regression tests.

0 commit comments

Comments
 (0)