Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEiTx-0008Gd-0C for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 16:52:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WEiTw-0004rm-G4 for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 16:52:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEiTv-0004rg-RO for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 16:52:47 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEiTs-0006DO-VK for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 16:52:47 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WEiTs-000679-0M; Sat, 15 Feb 2014 11:52:44 -0500 Date: Sat, 15 Feb 2014 11:52:44 -0500 From: Bruce Momjian To: jackie.qq.chang@gmail.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8540: Avoid sscanf buffer overflow Message-ID: <20140215165243.GA3651@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="6TrnltStXW4iwmi0" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --6TrnltStXW4iwmi0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Oct 19, 2013 at 09:49:05PM +0000, jackie.qq.chang@gmail.com wrote: > The following bug has been logged on the website: > > Bug reference: 8540 > Logged by: Jackie Chang > Email address: jackie.qq.chang@gmail.com > PostgreSQL version: 9.3.1 > Operating system: Any > Description: > > sscanf can set the maximum length of string read. If such a number is not > provided, it's vulnerable to buffer overflow. I have looked at your patch and I wasn't happy to be adding a hard-coded constant based on a macro definition. What I did do with the attached, applied patch is to remove the use of sscanf in pg_upgrade, and add a C comment in pg_dump explaining why the scanf can't overflow. I didn't see increasing the xlog.c length as useful. For pg_dump it would be nice from a sanity perspective if we could do: sscanf(str, "%" CppAsString2(MAXPGPATH-1) "s\n", ... but there is no way to string-ify a macro while also computing it during preprocessing. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --6TrnltStXW4iwmi0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="scanf.diff" diff --git a/contrib/pg_upgrade/option.c b/contrib/pg_upgrade/option.c new file mode 100644 index 79b59ee..4c08e94 *** a/contrib/pg_upgrade/option.c --- b/contrib/pg_upgrade/option.c *************** get_sock_dir(ClusterInfo *cluster, bool *** 453,461 **** sscanf(line, "%hu", &old_cluster.port); if (lineno == LOCK_FILE_LINE_SOCKET_DIR) { ! cluster->sockdir = pg_malloc(MAXPGPATH); /* strip off newline */ ! sscanf(line, "%s\n", cluster->sockdir); } } fclose(fp); --- 453,462 ---- sscanf(line, "%hu", &old_cluster.port); if (lineno == LOCK_FILE_LINE_SOCKET_DIR) { ! cluster->sockdir = pg_strdup(line); /* strip off newline */ ! if (strchr(cluster->sockdir, '\n') != NULL) ! *strchr(cluster->sockdir, '\n') = '\0'; } } fclose(fp); diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c new file mode 100644 index 71b96bd..1bed8a9 *** a/src/bin/pg_dump/pg_backup_directory.c --- b/src/bin/pg_dump/pg_backup_directory.c *************** _LoadBlobs(ArchiveHandle *AH, RestoreOpt *** 452,457 **** --- 452,458 ---- char fname[MAXPGPATH]; char path[MAXPGPATH]; + /* Can't overflow because line and fname are the same length. */ if (sscanf(line, "%u %s\n", &oid, fname) != 2) exit_horribly(modulename, "invalid line in large object TOC file \"%s\": \"%s\"\n", fname, line); --6TrnltStXW4iwmi0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --6TrnltStXW4iwmi0--