pg_read_file() with virtual files returns empty string

From: Joe Conway <mail(at)joeconway(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: pg_read_file() with virtual files returns empty string
Date: 2020-06-27 19:00:21
Message-ID: 969b8d82-5bb2-5fa8-4eb1-f0e685c5d736@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Since pg11 pg_read_file() and friends can be used with absolute paths as long as
the user is superuser or explicitly granted the role pg_read_server_files.

I noticed that when trying to read a virtual file, e.g.:

SELECT pg_read_file('/proc/self/status');

the returned result is a zero length string.

However this works fine:

SELECT pg_read_file('/proc/self/status', 127, 128);

The reason for that is pg_read_file_v2() sets bytes_to_read=-1 if no offset and
length are supplied as arguments when it is called. It passes bytes_to_read down
to read_binary_file().

When the latter function sees bytes_to_read < 0 it tries to read the entire file
by getting the file size via stat, which returns 0 for a virtual file size.

The attached patch fixes this for me. I think it ought to be backpatched through
pg11.

Comments?

Joe
--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

Attachment Content-Type Size
read-virtual-files.00.diff text/x-patch 1.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ranier Vilela 2020-06-27 19:22:51 Re: Fwd: PostgreSQL: WolfSSL support
Previous Message Tom Lane 2020-06-27 18:52:41 Re: Fwd: PostgreSQL: WolfSSL support