Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition#1118
Merged
Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition#1118
Conversation
…e filesystem race condition Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a Time-of-check time-of-use (TOCTOU) filesystem race condition security vulnerability identified in GitHub code scanning alert #4. The fix eliminates the race window between checking file accessibility with access() and opening the file with fopen() by directly attempting to open the file and checking the result.
Changes:
- Replaced
access()+fopen()pattern with directfopen()call for output.name file handling - Changed conditional check from
access(noip_file_name, R_OK) != -1tofpname != NULL
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
zhblue
added a commit
that referenced
this pull request
Jan 21, 2026
* Refactor contest ranking logic in contestrank.xls.php * Sanitize nicknames with additional prefix check * Remove '../' from file names in problem import Sanitize file names by removing '../' to prevent directory traversal vulnerabilities. * Add exit to restrict access to proxy.php Added exit statement to prevent unauthorized access. * Update problem_import_hoj.php * Update problem_import_hydro.php * Update problem_import_md.php * Update problem_import_qduoj.php * Update problem_import_syzoj.php * Update problem_import_tyvj.php * Update problem_import_unkownoj.php * Update problem_import_hoj.php * Update my_func.inc.php * Update problem_import.php * Update problem_import_qduoj.php * Update problem_import_hoj.php * Update problem_import_qduoj.php * Update difficulty control standards in common.php * Update mail.php * Fix HTML form attributes in mail.php * Update mail.php * Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition (#1118) Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function (#1120) Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
zhblue
added a commit
that referenced
this pull request
Jan 21, 2026
* Refactor contest ranking logic in contestrank.xls.php * Sanitize nicknames with additional prefix check * Remove '../' from file names in problem import Sanitize file names by removing '../' to prevent directory traversal vulnerabilities. * Add exit to restrict access to proxy.php Added exit statement to prevent unauthorized access. * Update problem_import_hoj.php * Update problem_import_hydro.php * Update problem_import_md.php * Update problem_import_qduoj.php * Update problem_import_syzoj.php * Update problem_import_tyvj.php * Update problem_import_unkownoj.php * Update problem_import_hoj.php * Update my_func.inc.php * Update problem_import.php * Update problem_import_qduoj.php * Update problem_import_hoj.php * Update problem_import_qduoj.php * Update difficulty control standards in common.php * Update mail.php * Fix HTML form attributes in mail.php * Update mail.php * Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition (#1118) Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function (#1120) Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for code scanning alert no. 5: Incorrect return-value check for a 'scanf'-like function (#1119) Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Update date formatting in submit.php Replaced strftime with date for better date formatting. * Update submit.php --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/zhblue/hustoj/security/code-scanning/4
General approach: avoid separating the permission check (
access) from the actual open operation. Instead, perform the open directly and base further logic on whether it succeeded; or, where a check is unavoidable, operate on a file descriptor or handle returned by the check, not on the pathname.Best fix here: eliminate the
accesscall and just attempt to open the file directly withfopen.fopenitself enforces permissions at the time of use, so this removes the race window betweenaccessandfopen. There is no need to introduce file-descriptor–based APIs likeopen/fdopenbecause we are only reading. Behavior remains the same in normal cases: if the file is readable,fopensucceeds and the code uses it; if not, we fall back to theelsebranch that buildsuserfileand removesuser.out.Concretely, in trunk/core/judge_client/judge_client.cc around lines 2130–2145, we will:
access(noip_file_name, R_OK)check with a directFILE *fpname = fopen(noip_file_name, "r");.if/elseso that theifcondition isif (fpname != NULL)and theelseis taken whenfopenfails.fscanf, usingnoip_file_name, etc.) and ensurefclose(fpname);is only called iffpnameis non-null.This keeps existing functionality while removing the TOCTOU pattern.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.