Fix bug that allowed any logged-in user to SET ROLE to any other database user
authorTom Lane <[email protected]>
Sun, 12 Feb 2006 22:32:57 +0000 (22:32 +0000)
committerTom Lane <[email protected]>
Sun, 12 Feb 2006 22:32:57 +0000 (22:32 +0000)
id (CVE-2006-0553).  Also fix related bug in SET SESSION AUTHORIZATION that
allows unprivileged users to crash the server, if it has been compiled with
Asserts enabled.  The escalation-of-privilege risk exists only in 8.1.0-8.1.2.
However, the Assert-crash risk exists in all releases back to 7.3.
Thanks to Akio Ishida for reporting this problem.

src/backend/commands/variable.c
src/backend/utils/mb/encnames.c
src/backend/utils/misc/guc.c
src/include/utils/guc_tables.h

index a054f7008753353b1447e5ff43ce2dbc3a2c2443..36fcd0c7d36c5836813af10cef4a236c43b2a38c 100644 (file)
@@ -586,7 +586,9 @@ assign_client_encoding(const char *value, bool doit, GucSource source)
  * by the numeric oid, followed by a comma, followed by the role name.
  * This cannot be confused with a plain role name because of the NAMEDATALEN
  * limit on names, so we can tell whether we're being passed an initial
- * role name or a saved/restored value.
+ * role name or a saved/restored value.  (NOTE: we rely on guc.c to have
+ * properly truncated any incoming value, but not to truncate already-stored
+ * values.  See GUC_IS_NAME processing.)
  */
 extern char *session_authorization_string;             /* in guc.c */
 
index a4baddd4a6db8e6c8dd40c0178dcbd71e7172fa0..c3fdc44cf88111037fa4f9e887b0a8d94e62e4e6 100644 (file)
@@ -449,7 +449,7 @@ pg_char_to_encname_struct(const char *name)
        if (name == NULL || *name == '\0')
                return NULL;
 
-       if (strlen(name) > NAMEDATALEN)
+       if (strlen(name) >= NAMEDATALEN)
        {
 #ifdef FRONTEND
                fprintf(stderr, "encoding name too long\n");
index ca40d34469d513c2f177569f0542c75b03f26db9..439d1711675a946f6dad660d9695e9cf43ffeabf 100644 (file)
@@ -48,6 +48,7 @@
 #include "optimizer/prep.h"
 #include "parser/parse_expr.h"
 #include "parser/parse_relation.h"
+#include "parser/scansup.h"
 #include "postmaster/autovacuum.h"
 #include "postmaster/bgwriter.h"
 #include "postmaster/syslogger.h"
@@ -1662,7 +1663,7 @@ static struct config_string ConfigureNamesString[] =
                {"client_encoding", PGC_USERSET, CLIENT_CONN_LOCALE,
                        gettext_noop("Sets the client's character set encoding."),
                        NULL,
-                       GUC_REPORT
+                       GUC_IS_NAME | GUC_REPORT
                },
                &client_encoding_string,
                "SQL_ASCII", assign_client_encoding, NULL
@@ -1742,7 +1743,8 @@ static struct config_string ConfigureNamesString[] =
        {
                {"default_tablespace", PGC_USERSET, CLIENT_CONN_STATEMENT,
                        gettext_noop("Sets the default tablespace to create tables and indexes in."),
-                       gettext_noop("An empty string selects the database's default tablespace.")
+                       gettext_noop("An empty string selects the database's default tablespace."),
+                       GUC_IS_NAME
                },
                &default_tablespace,
                "", assign_default_tablespace, NULL
@@ -1900,7 +1902,7 @@ static struct config_string ConfigureNamesString[] =
                {"server_encoding", PGC_INTERNAL, CLIENT_CONN_LOCALE,
                        gettext_noop("Sets the server (database) character set encoding."),
                        NULL,
-                       GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+                       GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
                },
                &server_encoding_string,
                "SQL_ASCII", NULL, NULL
@@ -1922,7 +1924,7 @@ static struct config_string ConfigureNamesString[] =
                {"role", PGC_USERSET, UNGROUPED,
                        gettext_noop("Sets the current role."),
                        NULL,
-                       GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+                       GUC_IS_NAME | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
                },
                &role_string,
                "none", assign_role, show_role
@@ -1933,7 +1935,7 @@ static struct config_string ConfigureNamesString[] =
                {"session_authorization", PGC_USERSET, UNGROUPED,
                        gettext_noop("Sets the session user name."),
                        NULL,
-                       GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+                       GUC_IS_NAME | GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
                },
                &session_authorization_string,
                NULL, assign_session_authorization, show_session_authorization
@@ -3934,6 +3936,12 @@ set_config_option(const char *name, const char *value,
                                        newval = guc_strdup(elevel, value);
                                        if (newval == NULL)
                                                return false;
+                                       /*
+                                        * The only sort of "parsing" check we need to do is
+                                        * apply truncation if GUC_IS_NAME.
+                                        */
+                                       if (conf->gen.flags & GUC_IS_NAME)
+                                               truncate_identifier(newval, strlen(newval), true);
                                }
                                else if (conf->reset_val)
                                {
index c6e93eff9a761e7b12e4855e3c9607094d18368b..3807673f80d05526c13f65df4eb201be9e998a8f 100644 (file)
@@ -126,6 +126,7 @@ struct config_generic
 #define GUC_DISALLOW_IN_FILE   0x0040  /* can't set in postgresql.conf */
 #define GUC_CUSTOM_PLACEHOLDER 0x0080  /* placeholder for custom variable */
 #define GUC_SUPERUSER_ONLY             0x0100  /* show only to superusers */
+#define GUC_IS_NAME                            0x0200  /* limit string to NAMEDATALEN-1 */
 
 /* bit values in status field */
 #define GUC_HAVE_TENTATIVE     0x0001          /* tentative value is defined */