From 4c8fe01bff58694df0bdf50337cea693aaf40681 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 30 Aug 2021 17:06:29 +0200 Subject: [PATCH] SQLServer: Don't stat()-then-mkdir() when mkdir() alone is enough Closes a TOCTOU race that SonarCloud complained about. SonarCloud: https://sonarcloud.io/project/issues?id=SerenityOS_serenity&issues=AXuVO_uKk92xXUF3qSVc&open=AXuVO_uKk92xXUF3qSVc --- Userland/Services/SQLServer/main.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Userland/Services/SQLServer/main.cpp b/Userland/Services/SQLServer/main.cpp index ac7c4f6610..7c4683a659 100644 --- a/Userland/Services/SQLServer/main.cpp +++ b/Userland/Services/SQLServer/main.cpp @@ -18,16 +18,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) return 1; } - struct stat statbuf; - if (stat("/home/anon/sql", &statbuf) != 0) { - if (errno != ENOENT) { - perror("stat"); - return 1; - } - if (mkdir("/home/anon/sql", 0700) != 0) { - perror("mkdir"); - return 1; - } + if (mkdir("/home/anon/sql", 0700) < 0 && errno != EEXIST) { + perror("mkdir"); + return 1; } if (unveil("/home/anon/sql", "rwc") < 0) {