Skip to content

Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition#1118

Merged
zhblue merged 1 commit intomasterfrom
alert-autofix-4
Jan 21, 2026
Merged

Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition#1118
zhblue merged 1 commit intomasterfrom
alert-autofix-4

Conversation

@zhblue
Copy link
Copy Markdown
Owner

@zhblue zhblue commented Jan 21, 2026

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 access call and just attempt to open the file directly with fopen. fopen itself enforces permissions at the time of use, so this removes the race window between access and fopen. There is no need to introduce file-descriptor–based APIs like open/fdopen because we are only reading. Behavior remains the same in normal cases: if the file is readable, fopen succeeds and the code uses it; if not, we fall back to the else branch that builds userfile and removes user.out.

Concretely, in trunk/core/judge_client/judge_client.cc around lines 2130–2145, we will:

  • Replace the access(noip_file_name, R_OK) check with a direct FILE *fpname = fopen(noip_file_name, "r");.
  • Adjust the if/else so that the if condition is if (fpname != NULL) and the else is taken when fopen fails.
  • Preserve the existing logic inside the block (reading via fscanf, using noip_file_name, etc.) and ensure fclose(fpname); is only called if fpname is non-null.
  • Make no other changes to imports or surrounding logic.

This keeps existing functionality while removing the TOCTOU pattern.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…e filesystem race condition

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@zhblue zhblue marked this pull request as ready for review January 21, 2026 01:04
Copilot AI review requested due to automatic review settings January 21, 2026 01:04
@zhblue zhblue merged commit 36d5448 into master Jan 21, 2026
7 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 direct fopen() call for output.name file handling
  • Changed conditional check from access(noip_file_name, R_OK) != -1 to fpname != 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants