summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-07-28 18:57:24 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2016-07-28 18:57:44 -0400
commit2833872b79bc3eb1c7a5e0e8fa232fc1e4c43e29 (patch)
tree3223c0005a0feba7d8c50be60fdad0d61240f4e8 /src
parent66f7e408125ddb2c737ba40d967c04201e79f165 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/bin/psql/input.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c
index aa5343205f5..83f84993f7f 100644
--- a/src/bin/psql/input.c
+++ b/src/bin/psql/input.c
@@ -218,7 +218,7 @@ gets_fromFile(FILE *source)
}
/* EOL? */
- if (buffer->data[buffer->len - 1] == '\n')
+ if (buffer->len > 0 && buffer->data[buffer->len - 1] == '\n')
{
buffer->data[buffer->len - 1] = '\0';
return pg_strdup(buffer->data);