pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #8540: Avoid sscanf buffer overflow
2+ messages / 2 participants
[nested] [flat]

* BUG #8540: Avoid sscanf buffer overflow
@ 2013-10-19 21:49 jackie.qq.chang@gmail.com
  2014-02-15 16:52 ` Re: BUG #8540: Avoid sscanf buffer overflow Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 2+ messages in thread

From: jackie.qq.chang@gmail.com @ 2013-10-19 21:49 UTC (permalink / raw)
  To: pgsql-bugs

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.


Diff is:


inline patch 1: contrib/pg_upgrade/option.c, src/backend/access/transam/xlog.c (+1/-1 lines) download inline-1.patchdiff --git a/contrib/pg_upgrade/option.c b/contrib/pg_upgrade/option.c index 250aeb8..293ef1c 100644 --- a/contrib/pg_upgrade/option.c +++ b/contrib/pg_upgrade/option.c @@ -443,7 +443,7 @@ get_sock_dir(ClusterInfo *cluster, bool live_check) { cluster->sockdir = pg_malloc(MAXPGPATH); /* strip off newline */ - sscanf(line, "%s\n", cluster->sockdir); + sscanf(line, "%1023s\n", cluster->sockdir); } } fclose(fp); diff --git a/src/backend/access/transam/xlog.c
b/src/backend/access/transam/xlog.c index 06f5eb0..560230f 100644
inline patch 2: src/backend/access/transam/xlog.c (+0/-0 lines) download inline-2.patch--- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -10029,7 +10029,7 @@ do_pg_stop_backup(char *labelfile, bool
waitforarchive, TimeLineID *stoptli_p) * Read and parse the START WAL LOCATION line (this code is pretty crude, * but we are not expecting any variability in the file format). */ - if (sscanf(labelfile, "START WAL LOCATION: %X/%X (file %24s)%c", + if (sscanf(labelfile, "START WAL LOCATION: %X/%X (file %63s)%c", &hi, &lo, startxlogfilename, &ch) != 4 || ch != '\n') ereport(ERROR, diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c index f803186..48e0db9 100644
inline patch 3: src/bin/pg_dump/pg_backup_directory.c (+1/-1 lines) download inline-3.patch--- a/src/bin/pg_dump/pg_backup_directory.c +++ b/src/bin/pg_dump/pg_backup_directory.c @@ -452,7 +452,7 @@ _LoadBlobs(ArchiveHandle *AH, RestoreOptions *ropt) char fname[MAXPGPATH]; char path[MAXPGPATH]; - if (sscanf(line, "%u %s\n", &oid, fname) != 2) + if (sscanf(line, "%u %1023s\n", &oid, fname) != 2) exit_horribly(modulename, "invalid line in large object TOC file \"%s\": \"%s\"\n", fname, line);
-- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs Patches: download all (3 files, .tar.gz) ^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: BUG #8540: Avoid sscanf buffer overflow
  2013-10-19 21:49 BUG #8540: Avoid sscanf buffer overflow jackie.qq.chang@gmail.com
@ 2014-02-15 16:52 ` Bruce Momjian <bruce@momjian.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Bruce Momjian @ 2014-02-15 16:52 UTC (permalink / raw)
  To: jackie.qq.chang@gmail.com; +Cc: pgsql-bugs

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  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

scanf.diff (text/x-diff, 1.4K) download 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);
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2014-02-15 16:52 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-10-19 21:49 BUG #8540: Avoid sscanf buffer overflow jackie.qq.chang@gmail.com
2014-02-15 16:52 ` Bruce Momjian <bruce@momjian.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox