alpine 3.6
race weakness #36

5

Weakness Breakdown


Definition:

A race condition exists when parallel code accesses shared data without proper coordination. An attack that uses a race-condition weakness takes advantage of the unsafe data access to manipulate how one of the parallel sections of code reacts. Even though each process runs as intended, the outcome is unexpected. For example, consider a bank service that depends on an encryption key that it reads from a known location. An independent cryptography service is responsible for generating the key and placing it where the bank is expected to read it in a timely manner. If the bank and cryptography services do not coordinate with each other, then the bank may read a blank encryption key before cryptography writes the key to the location. This can effectively turn off all encryption for the bank without either service, or the administrator, knowing that something has gone wrong.

Warning code(s):

This accepts filename arguments; if an attacker can move those files, a race condition results..

File Name:

kyua/src/kyua-0.13/utils/process/isolation.cpp

Context:

The highlighted line of code below is the trigger point of this particular Alpine 3.6 race weakness.

 ///
/// \param message The message to print.  The errno-based string will be
///     appended to this, just like in perror(3).
/// \param original_errno The error code to format.
static void
fail(const std::string& message, const int original_errno)
{
    std::cerr << message << ": " << std::strerror(original_errno) << '\n';
    std::exit(process::exit_isolation_failure);
}


/// Changes the owner of a path.
///
/// This function is intended to be called from a subprocess getting ready to
/// invoke an external binary.  Therefore, if there is any error during the
/// setup, the new process is terminated with an error code.
///
/// \param file The path to the file or directory to affect.
/// \param uid The UID to set on the path.
/// \param gid The GID to set on the path.
static void
do_chown(const fs::path& file, const uid_t uid, const gid_t gid)
{
    if (::chown(file.c_str(), uid, gid) == -1)
        fail(F("chown(%s, %s, %s) failed; UID is %s and GID is %s")
             % file % uid % gid % ::getuid() % ::getgid(), errno);
}


/// Resets the environment of the process to a known state.
///
/// \param work_directory Path to the work directory being used.
///
/// \throw std::runtime_error If there is a problem setting up the environment.
static void
prepare_environment(const fs::path& work_directory)
{
    const char* to_unset[] = { "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE",
                               "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC",
                               "LC_TIME", NULL };
    const char** iter;
    for (iter = to_unset; *iter != NULL; ++iter) {
        utils::unsetenv(*iter);
    }

    utils::setenv("HOME", work_directory.str());
    utils::setenv("TMPDIR", work_directory.str());
    utils::setenv("TZ", "UTC");
} 

The registered trademark Linux® is used pursuant to a sublicense from the Linux Foundation, the exclusive licensee of Linus Torvalds, owner of the mark on a world­wide basis.