]> granicus.if.org Git - postgresql/commit
Guard against empty buffer in gets_fromFile()'s check for a newline.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Jul 2016 22:57:24 +0000 (18:57 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Jul 2016 22:57:52 +0000 (18:57 -0400)
commit76c10ca318df0e7f2b2e2840ae496d7000d1ca37
tree4247390d530425830943f235b8d7de80c7521ee8
parent7b8526e5d6e0a010d4461aa565ff4cc13c2dc447
Guard against empty buffer in gets_fromFile()'s check for a newline.

Per the fgets() specification, it cannot return without reading some data
unless it reports EOF or error.  So the code here assumed that the data
buffer would necessarily be nonempty when we go to check for a newline
having been read.  However, Agostino Sarubbo noticed that this could fail
to be true if the first byte of the data is a NUL (\0).  The fgets() API
doesn't really work for embedded NULs, which is something I don't feel
any great need for us to worry about since we generally don't allow NULs
in SQL strings anyway.  But we should not access off the end of our own
buffer if the case occurs.  Normally this would just be a harmless read,
but if you were unlucky the byte before the buffer would contain '\n'
and we'd overwrite it with '\0', and if you were really unlucky that
might be valuable data and psql would crash.

Agostino reported this to pgsql-security, but after discussion we concluded
that it isn't worth treating as a security bug; if you can control the
input to psql you can do far more interesting things than just maybe-crash
it.  Nonetheless, it is a bug, so back-patch to all supported versions.
src/bin/psql/input.c