CVE-2024-6982
RCE via Python sandbox escape in lollms <= 9.8 using `eval()` and `_frozen_importlib`.
- CVSS 8.4
- CWE-94
- Design Defects
- Remote
A remote code execution vulnerability exists in the Calculate function of parisneo/lollms version 9.8. The vulnerability arises from the use of Python's `eval()` function to evaluate mathematical expressions within a Python sandbox that disables `__builtins__` and only allows functions from the `math` module. This sandbox can be bypassed by loading the `os` module using the `_frozen_importlib.BuiltinImporter` class, allowing an attacker to execute arbitrary commands on the server. The issue is fixed in version 9.10.
- CWE
- CWE-94
- CVSS base score
- 8.4
- Published
- 2025-03-20
- OWASP
- A03:2021-Injection
- Orthogonal defect classification
- Algorithm
- Code defect classification
- Incorrect Functionality
- Category
- Design Defects
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- parisneo/lol
- Fixed by upgrading
- Yes
Solution
Upgrade to version 9.10.
Vulnerable code sample
import math
def calculate(expression):
"""
Calculates a mathematical expression using eval() with limited scope.
"""
# Create a safe namespace for eval()
safe_dict = {
'math': math,
'__builtins__': None # Disable built-in functions
}
try:
# Evaluate the expression within the safe namespace
result = eval(expression, safe_dict)
return result
except Exception as e:
return f"Error: {e}"
if __name__ == '__main__':
while True:
expression = input("Enter a mathematical expression (or 'exit'): ")
if expression.lower() == 'exit':
break
result = calculate(expression)
print("Result:", result)Patched code sample
import os
import math
import ast
import operator as op
# Safe list of allowed operators
ALLOWED_OPERATORS = {
ast.Add: op.add,
ast.Sub: op.sub,
ast.Mult: op.mul,
ast.Div: op.truediv,
ast.Pow: op.pow,
ast.USub: op.neg
}
def safe_eval(expression):
"""
Safely evaluates a mathematical expression using ast and a limited set of operators.
"""
try:
node = ast.parse(expression, mode='eval')
def eval_(node):
"""Secure function that fixes the vulnerability."""
if isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
try:
return ALLOWED_OPERATORS[type(node.op)](eval_(node.left), eval_(node.right))
except KeyError:
raise ValueError("Operator not allowed")
elif isinstance(node, ast.UnaryOp):
try:
return ALLOWED_OPERATORS[type(node.op)](eval_(node.operand))
except KeyError:
raise ValueError("Operator not allowed")
elif isinstance(node, ast.Name):
if node.id in math.__dict__:
return math.__dict__[node.id]
else:
raise ValueError(f"Variable {node.id} not allowed")
elif isinstance(node, ast.Call):
#added support for basic functions to calculate things such as sin(x):
if isinstance(node.func, ast.Name) and node.func.id in math.__dict__:
func = math.__dict__[node.func.id]
args = [eval_(arg) for arg in node.args]:
return func(*args)
else:
raise ValueError(f"Function {node.func.id} not allowed")
else:
raise ValueError("Invalid expression")
return eval_(node.body)
except (SyntaxError, ValueError) as e:
return f"Error: {e}"
# Example usage:
if __name__ == '__main__':
print(safe_eval("2 + 3 * 4")) # Output: 14
print(safe_eval("math.sqrt(25)")) # Output 5.0
print(safe_eval("sin(0)")) # Output: Error: Variable 'sin' not allowed
print(safe_eval("math.sin(0)")) # Output: 0.0
print(safe_eval("2 ** 10")) # Output 1024
print(safe_eval("().__class__.__bases__[0].__subclasses__()[59].__init__.__globals__['__builtins__']['eval']('__import__(\"os\").system(\"ls -l\")')")) # Output Error. Expression blocked from being processed.
print(safe_eval("2 + os.system('ls -l')")) # Output: Error: Variable 'os' not allowedPayload
_frozen_importlib.BuiltinImporter.load_module('os').system('id')
Cite this entry
@misc{vaitp:cve20246982,
title = {{RCE via Python sandbox escape in lollms <= 9.8 using `eval()` and `_frozen_importlib`.
}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2025},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-6982},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-6982/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
