mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-06-14 10:17:54 +00:00
Fix TOCTOU race condition
Separately checking the state of a file before operating on it may allow an attacker to modify the file between the two operations. (CWE-367) Fix by using fstat() instead of stat().
This commit is contained in:
parent
cde793b0f5
commit
19b3ddac2f
3 changed files with 23 additions and 20 deletions
|
|
@ -48,9 +48,9 @@
|
|||
static void test_device(const SDL_bool iscapture, const char *fname, int flags, SDL_bool (*test)(int fd))
|
||||
{
|
||||
struct stat sb;
|
||||
if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
|
||||
const int audio_fd = open(fname, flags | O_CLOEXEC, 0);
|
||||
if (audio_fd >= 0) {
|
||||
const int audio_fd = open(fname, flags | O_CLOEXEC, 0);
|
||||
if (audio_fd >= 0) {
|
||||
if ((fstat(audio_fd, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
|
||||
const SDL_bool okay = test(audio_fd);
|
||||
close(audio_fd);
|
||||
if (okay) {
|
||||
|
|
@ -65,6 +65,8 @@ static void test_device(const SDL_bool iscapture, const char *fname, int flags,
|
|||
*/
|
||||
SDL_AddAudioDevice(iscapture, fname, NULL, (void *)(uintptr_t)dummyhandle);
|
||||
}
|
||||
} else {
|
||||
close(audio_fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue