[PATCH v2] libio: Fix race in _IO_new_file_init_internal initialization order [BZ #33785]
Florian Weimer
fweimer@redhat.com
Wed May 6 18:50:05 GMT 2026
* Shamil Abdulaev:
> _IO_new_file_init_internal linked the new stream into _IO_list_all
> before setting fp->_fileno to -1. A concurrent thread that walks
> _IO_list_all (for example via fflush (NULL)) could observe the stream
> with an uninitialized _fileno before initialization completed.
>
> Set _fileno = -1 before _IO_link_in so the stream is fully
> initialized when it becomes visible in the global list.
>
> This is the residual concurrency defect noted at the end of commit
> b657f72fa3 ("libio: Fix deadlock between freopen, fflush (NULL) and
> fclose (bug 24963)").
>
> Add libio/tst-file-init-race exercising concurrent fopen/fclose and
> fflush (NULL) to detect regressions.
>
> Signed-off-by: Shamil Abdulaev <ashamil435@gmail.com>
> ---
> Changes since v1:
> - Apply the same _fileno/_IO_link_in reordering to libio/oldfileops.c
> (suggested by Florian Weimer).
> - tst-file-init-race: treat fopen/fclose failures as test failures via
> FAIL_EXIT1 instead of silently ignoring them (suggested by Florian
> Weimer).
This version looks okay to me.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
I'll do a final round of testing and will push it for you afterwards.
Thanks,
Florian
More information about the Libc-alpha
mailing list