From 622007f2ee9b65af6ddfd13f41815b474cb1f683 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 4 Dec 2023 22:57:27 +0200 Subject: [PATCH] Shell: Verify that command exists before executing it This prevents page fault somewhere when executing non-existing commands. --- userspace/Shell/main.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index e88df1bd..78b4b3cb 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -501,6 +501,21 @@ pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_ executable_file = args.front(); } + // Verify that the file exists is executable + { + struct stat st; + if (stat(executable_file.data(), &st) == -1) + { + fprintf(stderr, "command not found: %s\n", args.front().data()); + return -1; + } + if ((st.st_mode & 0111) == 0) + { + fprintf(stderr, "permission denied: %s\n", executable_file.data()); + return -1; + } + } + pid_t pid = fork(); if (pid == 0) {