Skip to content

Coverity: Set umask 0077 before creating tmp files#243

Open
derobins wants to merge 1 commit intodevelopfrom
derobins/tmpfile_umask
Open

Coverity: Set umask 0077 before creating tmp files#243
derobins wants to merge 1 commit intodevelopfrom
derobins/tmpfile_umask

Conversation

@derobins
Copy link
Copy Markdown
Collaborator

Coverity points out that it's a potential security problem if you create tmp files with group and world permissions.

Adds umask calls to state_mt and the common test header.

Part of AIHIPFILE-161

Coverity points out that it's a potential security problem
if you create tmp files with group and world permissions.

Adds umask calls to state_mt and the common test header.
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

Updates test utilities to set a restrictive umask (0077) around temporary-file creation, addressing Coverity’s warning about potentially permissive group/world permissions on tmp files.

Changes:

  • Set umask to disallow group/world permissions before mkstemp() in the shared Tmpfile helper used by tests.
  • Set umask similarly in the state_mt multithreading stress test when creating its temporary file.
  • Add <sys/stat.h> include to the common test header to support umask()/permission macros.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
test/common/test-common.h Adds <sys/stat.h> and wraps mkstemp() with umask set/restore in Tmpfile.
test/amd_detail/state_mt.cpp Wraps mkstemp() with umask set/restore in the multithreaded temp-file helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +68 to +76
mode_t old_umask = umask(S_IRWXG | S_IRWXO);

path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}

umask(old_umask);

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

umask() is set before mkstemp(), but if mkstemp() fails you throw before restoring the previous umask. That leaves the process umask changed for the rest of the test run. Please ensure the old umask is restored on all paths (e.g., restore before throwing, or use an RAII guard/scope-exit).

Suggested change
mode_t old_umask = umask(S_IRWXG | S_IRWXO);
path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}
umask(old_umask);
struct UmaskGuard {
mode_t old_umask;
explicit UmaskGuard(mode_t new_mask) : old_umask(umask(new_mask)) {}
~UmaskGuard() { umask(old_umask); }
} umask_guard(S_IRWXG | S_IRWXO);
path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}

Copilot uses AI. Check for mistakes.
Comment on lines +52 to +55
// Security analyzers will be sad if you don't set umask 0077
// before creating temporary files
mode_t old_umask = umask(S_IRWXG | S_IRWXO);

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

This file uses mode_t, umask(), and S_IRWXG/S_IRWXO but does not include <sys/stat.h>, which is where these are defined on POSIX systems. Please add #include <sys/stat.h> (or the appropriate header) to avoid build failures on stricter toolchains.

Copilot uses AI. Check for mistakes.
Comment on lines 52 to +64
@@ -57,6 +61,8 @@ make_temp_file()
exit(EXIT_FAILURE);
}

umask(old_umask);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

umask() is process-global; in this multi-threaded stress test, concurrent calls to make_temp_file() can interleave such that the final umask is not restored to the original value (e.g., thread A restores an earlier value while thread B later restores 077). To avoid this race, either avoid changing umask here (e.g., rely on mkstemp()/fchmod() for 0600), or serialize the set-create-restore sequence with a dedicated mutex.

Copilot uses AI. Check for mistakes.
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