InternLM / InternLM/lmdeploy

[Bug] Vulnerability Report: Code Injection Risk Due to Unsafe Usage of 'exec' and 'compile' in Code

Open
#3,254 2 comments 1 reaction 1 assignee Claimed by @lvhan028 View on GitHub
Dominant language
Python
Stars
8.1k
Forks
748
Avg merge
6d 2h
Merged PRs (30d)
54

Description

### Checklist

- [x] 1. I have searched related issues but cannot get the expected help.
- [x] 2. The bug has not been fixed in the latest version.
- [x] 3. Please note that if the bug-related issue you submitted lacks corresponding environment info and a minimal reproducible demo, it will be challenging for us to reproduce and resolve the issue, reducing the likelihood of receiving feedback.

### Describe the bug

## Description:

I have come across a section of code in the project that appears to be vulnerable due to the use of exec and compile functions in an unsafe manner. The relevant code snippet is as follows:
```python
with open(version_file, 'r') as f:
exec(compile(f.read(), version_file, 'exec'))
__version__ = locals()['__version__']
```

code location:
https://github.com/InternLM/lmdeploy/blob/df06ae3e5531711438b9046aa2bf2710c17f1649/docs/en/conf.py#L20

This code reads the contents of a file (version.py) and then executes it using exec after compilation with compile. The problem with this approach is that if the version.py file has been tampered with by an attacker, it can lead to serious security breaches. For example, an attacker could inject malicious code into version.py that deletes important files, executes unauthorized system commands, or steals sensitive information.​

## Affected Versions:
All versions of this project

from Aug 21, 2023

https://github.com/InternLM/lmdeploy/blob/c238f1cde6d983963f5c2eee572e0cb852f81a44/docs/en/conf.py

to Jan 21, 2025

https://github.com/InternLM/lmdeploy/blob/df06ae3e5531711438b9046aa2bf2710c17f1649/docs/en/conf.py#L20

### Reproduction

Possible malicious actions that could be carried out through this vulnerability include, but are not limited to:​

(1)File Deletion: The attacker could modify version.py to delete files in the system.For instance, code like this could be added to version.py:
```python
import os
for root, dirs, files in os.walk('.'):
for file in files:
file_path = os.path.join(root, file)
os.remove(file_path)
```

(2)Unauthorized System Command Execution: Malicious code could be inserted to execute system commands. For example:
```python
import os
os.system('rm -rf /path/to/important/directory')
```

### Environment

```Shell
This vulnerability can be reproduced independently of the environment. It is mainly due to the inherent risky coding practice of using exec and compile functions in the code. As long as the code structure remains the same and an attacker has the ability to modify the version.py file, regardless of the specific operating system, programming language version, or other environmental factors, the vulnerability can be exploited to execute malicious code.
```

### Error traceback

```Shell

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.