Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5369190

Browse files
committed
Make the world at least marginally safe for usernames with embedded spaces.
Per recent gripe.
1 parent cb36e74 commit 5369190

File tree

2 files changed

+68
-54
lines changed

2 files changed

+68
-54
lines changed

src/backend/utils/adt/acl.c

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.86 2003/01/24 21:53:29 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.87 2003/06/02 19:00:29 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -31,6 +31,7 @@
3131
#define ACL_IDTYPE_UID_KEYWORD "user"
3232

3333
static const char *getid(const char *s, char *n);
34+
static void putid(char *p, const char *s);
3435
static Acl *makeacl(int n);
3536
static const char *aclparse(const char *s, AclItem *aip);
3637
static bool aclitemeq(const AclItem *a1, const AclItem *a2);
@@ -64,42 +65,68 @@ static AclMode convert_schema_priv_string(text *priv_type_text);
6465
static const char *
6566
getid(const char *s, char *n)
6667
{
67-
unsigned len;
68-
const char *id;
69-
int in_quotes = 0;
68+
int len = 0;
69+
bool in_quotes = false;
7070

7171
Assert(s && n);
7272

7373
while (isspace((unsigned char) *s))
74-
++s;
75-
76-
if (*s == '"')
77-
{
78-
in_quotes = 1;
7974
s++;
80-
}
81-
82-
for (id = s, len = 0;
83-
isalnum((unsigned char) *s) || *s == '_' || in_quotes;
84-
++len, ++s)
75+
/* This test had better match what putid() does, below */
76+
for (;
77+
*s != '\0' &&
78+
(isalnum((unsigned char) *s) ||
79+
*s == '_' ||
80+
*s == '"' ||
81+
in_quotes);
82+
s++)
8583
{
86-
if (in_quotes && *s == '"')
84+
if (*s == '"')
8785
{
88-
len--;
89-
in_quotes = 0;
86+
in_quotes = !in_quotes;
87+
}
88+
else
89+
{
90+
if (len >= NAMEDATALEN-1)
91+
elog(ERROR, "identifier must be less than %d characters",
92+
NAMEDATALEN);
93+
n[len++] = *s;
9094
}
9195
}
92-
if (len >= NAMEDATALEN)
93-
elog(ERROR, "getid: identifier must be <%d characters",
94-
NAMEDATALEN);
95-
if (len > 0)
96-
memmove(n, id, len);
9796
n[len] = '\0';
9897
while (isspace((unsigned char) *s))
99-
++s;
98+
s++;
10099
return s;
101100
}
102101

102+
/*
103+
* Write a user or group Name at *p, surrounding it with double quotes if
104+
* needed. There must be at least NAMEDATALEN+2 bytes available at *p.
105+
*/
106+
static void
107+
putid(char *p, const char *s)
108+
{
109+
const char *src;
110+
bool safe = true;
111+
112+
for (src = s; *src; src++)
113+
{
114+
/* This test had better match what getid() does, above */
115+
if (!isalnum((unsigned char) *src) && *src != '_')
116+
{
117+
safe = false;
118+
break;
119+
}
120+
}
121+
if (!safe)
122+
*p++ = '"';
123+
for (src = s; *src; src++)
124+
*p++ = *src;
125+
if (!safe)
126+
*p++ = '"';
127+
*p = '\0';
128+
}
129+
103130
/*
104131
* aclparse
105132
* Consumes and parses an ACL specification of the form:
@@ -304,7 +331,12 @@ aclitemout(PG_FUNCTION_ARGS)
304331
unsigned i;
305332
char *tmpname;
306333

307-
p = out = palloc(strlen("group = ") + 2 * N_ACL_RIGHTS + 2* NAMEDATALEN + 2);
334+
out = palloc(strlen("group =/") +
335+
2 * N_ACL_RIGHTS +
336+
2 * (NAMEDATALEN+2) +
337+
1);
338+
339+
p = out;
308340
*p = '\0';
309341

310342
switch (ACLITEM_GET_IDTYPE(*aip))
@@ -315,36 +347,25 @@ aclitemout(PG_FUNCTION_ARGS)
315347
0, 0, 0);
316348
if (HeapTupleIsValid(htup))
317349
{
318-
strncat(p,
319-
NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename),
320-
NAMEDATALEN);
350+
putid(p, NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename));
321351
ReleaseSysCache(htup);
322352
}
323353
else
324354
{
325355
/* Generate numeric UID if we don't find an entry */
326-
char *tmp;
327-
328-
tmp = DatumGetCString(DirectFunctionCall1(int4out,
329-
Int32GetDatum((int32) aip->ai_grantee)));
330-
strcat(p, tmp);
331-
pfree(tmp);
356+
sprintf(p, "%d", aip->ai_grantee);
332357
}
333358
break;
334359
case ACL_IDTYPE_GID:
335-
strcat(p, "group ");
360+
strcpy(p, "group ");
361+
p += strlen(p);
336362
tmpname = get_groname(aip->ai_grantee);
337363
if (tmpname != NULL)
338-
strncat(p, tmpname, NAMEDATALEN);
364+
putid(p, tmpname);
339365
else
340366
{
341367
/* Generate numeric GID if we don't find an entry */
342-
char *tmp;
343-
344-
tmp = DatumGetCString(DirectFunctionCall1(int4out,
345-
Int32GetDatum((int32) aip->ai_grantee)));
346-
strcat(p, tmp);
347-
pfree(tmp);
368+
sprintf(p, "%d", aip->ai_grantee);
348369
}
349370
break;
350371
case ACL_IDTYPE_WORLD:
@@ -375,20 +396,13 @@ aclitemout(PG_FUNCTION_ARGS)
375396
0, 0, 0);
376397
if (HeapTupleIsValid(htup))
377398
{
378-
strncat(p,
379-
NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename),
380-
NAMEDATALEN);
399+
putid(p, NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename));
381400
ReleaseSysCache(htup);
382401
}
383402
else
384403
{
385404
/* Generate numeric UID if we don't find an entry */
386-
char *tmp;
387-
388-
tmp = DatumGetCString(DirectFunctionCall1(int4out,
389-
Int32GetDatum((int32) aip->ai_grantor)));
390-
strcat(p, tmp);
391-
pfree(tmp);
405+
sprintf(p, "%d", aip->ai_grantor);
392406
}
393407

394408
while (*p)

src/bin/initdb/initdb.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
2828
# Portions Copyright (c) 1994, Regents of the University of California
2929
#
30-
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.191 2003/05/28 18:19:09 tgl Exp $
30+
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.192 2003/06/02 19:00:29 tgl Exp $
3131
#
3232
#-------------------------------------------------------------------------
3333

@@ -1021,11 +1021,11 @@ echo "ok"
10211021
$ECHO_N "setting privileges on built-in objects... "$ECHO_C
10221022
(
10231023
cat <<EOF
1024-
UPDATE pg_class SET relacl = '{"=r/$POSTGRES_SUPERUSERNAME"}' \
1024+
UPDATE pg_class SET relacl = '{"=r/\\\\"$POSTGRES_SUPERUSERNAME\\\\""}' \
10251025
WHERE relkind IN ('r', 'v', 'S') AND relacl IS NULL;
1026-
UPDATE pg_proc SET proacl = '{"=X/$POSTGRES_SUPERUSERNAME"}' \
1026+
UPDATE pg_proc SET proacl = '{"=X/\\\\"$POSTGRES_SUPERUSERNAME\\\\""}' \
10271027
WHERE proacl IS NULL;
1028-
UPDATE pg_language SET lanacl = '{"=U/$POSTGRES_SUPERUSERNAME"}' \
1028+
UPDATE pg_language SET lanacl = '{"=U/\\\\"$POSTGRES_SUPERUSERNAME\\\\""}' \
10291029
WHERE lanpltrusted;
10301030
GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;
10311031
GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;

0 commit comments

Comments
 (0)