Skip to content

Commit 093d3d7

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 ae693c0 commit 093d3d7

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

123123

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

144160
while (isspace((unsigned char) *s))
145161
s++;
146-
/* This code had better match what putid() does, below */
147162
for (;
148163
*s != '\0' &&
149-
(isalnum((unsigned char) *s) ||
150-
*s == '_' ||
151-
*s == '"' ||
152-
in_quotes);
164+
(in_quotes || *s == '"' || is_safe_acl_char(*s, true));
153165
s++)
154166
{
155167
if (*s == '"')
156168
{
169+
if (!in_quotes)
170+
{
171+
in_quotes = true;
172+
continue;
173+
}
157174
/* safe to look at next char (could be '\0' though) */
158175
if (*(s + 1) != '"')
159176
{
160-
in_quotes = !in_quotes;
177+
in_quotes = false;
161178
continue;
162179
}
163180
/* it's an escaped double quote; skip the escaping char */
@@ -191,10 +208,10 @@ putid(char *p, const char *s)
191208
const char *src;
192209
bool safe = true;
193210

211+
/* Detect whether we need to use double quotes */
194212
for (src = s; *src; src++)
195213
{
196-
/* This test had better match what getid() does, above */
197-
if (!isalnum((unsigned char) *src) && *src != '_')
214+
if (!is_safe_acl_char(*src, false))
198215
{
199216
safe = false;
200217
break;

src/test/regress/expected/privileges.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,26 @@ SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -
19621962
ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS FROM public;
19631963
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
19641964
ERROR: cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS
1965+
-- Test quoting and dequoting of user names in ACLs
1966+
CREATE ROLE "regress_""quoted";
1967+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
1968+
'SELECT', TRUE);
1969+
makeaclitem
1970+
------------------------------------------
1971+
"regress_""quoted"=r*/"regress_""quoted"
1972+
(1 row)
1973+
1974+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
1975+
aclitem
1976+
------------------------------------------
1977+
"regress_""quoted"=r*/"regress_""quoted"
1978+
(1 row)
1979+
1980+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
1981+
ERROR: a name must follow the "/" sign
1982+
LINE 1: SELECT '""=r*/""'::aclitem;
1983+
^
1984+
DROP ROLE "regress_""quoted";
19651985
--
19661986
-- Testing blanket default grants is very hazardous since it might change
19671987
-- 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
@@ -1196,6 +1196,14 @@ ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS
11961196

11971197
ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
11981198

1199+
-- Test quoting and dequoting of user names in ACLs
1200+
CREATE ROLE "regress_""quoted";
1201+
SELECT makeaclitem('regress_"quoted'::regrole, 'regress_"quoted'::regrole,
1202+
'SELECT', TRUE);
1203+
SELECT '"regress_""quoted"=r*/"regress_""quoted"'::aclitem;
1204+
SELECT '""=r*/""'::aclitem; -- used to be misparsed as """"
1205+
DROP ROLE "regress_""quoted";
1206+
11991207
--
12001208
-- Testing blanket default grants is very hazardous since it might change
12011209
-- the privileges attached to objects created by concurrent regression tests.

0 commit comments

Comments
 (0)