On Tue, 1 Dec 2026 at 12:09, Chao Li <[email protected]> wrote:
>
> Hi,
>
> While reviewing another patch, I just spotted a use-after-close issue in 
> pg_upgrade.
>
> cleanup_output_dirs() immediately closes log_opts.internal:
> ```
> void
> cleanup_output_dirs(void)
> {
>         fclose(log_opts.internal);
> ```
>
> Later in the same function, there is a branch that calls pg_log():
> ```
>                 default:
>                         /* different failure, just report it */
>                         pg_log(PG_WARNING, "could not access directory 
> \"%s\": %m",
>                                    log_opts.rootdir);
>                         break;
> ```
>
> pg_log() calls pg_log_v(), which has the following check:
> ```
>         /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
>         /* fopen() on log_opts.internal might have failed, so check it */
>         if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
>                 log_opts.internal != NULL)
>         {
>                 if (type == PG_STATUS)
>                         /* status messages get two leading spaces, see below 
> */
>                         fprintf(log_opts.internal, "  %s\n", message);
>                 else if (type == PG_REPORT_NONL)
>                         fprintf(log_opts.internal, "%s", message);
>                 else
>                         fprintf(log_opts.internal, "%s\n", message);
>                 fflush(log_opts.internal);
>         }
> ```
>
> For PG_WARNING, the first part of the condition is true regardless of verbose 
> mode. Since cleanup_output_dirs() doesn’t set log_opts.internal to NULL after 
> closing the stream, pg_log_v() attempts to write to the closed stream with 
> fprintf() and then flush it with fflush(). This is the use-after-close issue.
>
> The fix is straightforward. We just need to set log_opts.internal to NULL 
> after closing it. The attached patch does that.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>

Hi!
I was able to repro this

Breakpoint 1 at 0x1362: file repro.c, line 37.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, pg_log_v (fmt=0x555555556084 "%s") at repro.c:37
37 write_rc = fprintf(log_opts.internal, "%s\n", message);
#0  pg_log_v (fmt=0x555555556084 "%s") at repro.c:37
#1  0x0000555555555517 in pg_log_warning (fmt=0x555555556088 "could
not access directory \"%s\": Permission denied") at repro.c:59
#2  0x00005555555555ee in cleanup_output_dirs () at repro.c:104
#3  0x000055555555565c in main () at repro.c:123
$1 = (FILE *) 0x5555555592a0
internal log write: FILE *=0x5555555592a0, fprintf return=-1, errno=9
(Bad file descriptor)
cleanup_output_dirs(): closing internal log, then forcing pg_check_dir() error
could not access directory "<inaccessible rootdir>": Permission denied
cleanup_output_dirs() returned
[Inferior 1 (process 915670) exited normally]

I think fix is good


-- 
Best regards,
Kirill Reshke


Reply via email to