Insecure Coding Preferences in Long-Term Memory: Security Risks for LLM-based Code Generation
- URL: http://arxiv.org/abs/2607.17619v1
- Date: Mon, 20 Jul 2026 07:15:35 GMT
- Title: Insecure Coding Preferences in Long-Term Memory: Security Risks for LLM-based Code Generation
- Abstract summary: This study is the first systematic empirical study on the impact of insecure coding preferences stored in long-term memory on the security of LLM-based code generation.<n>We evaluate four LLMs across five programming languages (Python, C, C++, Go, and JavaScript)
- Score: 32.05457233792888
- License: http://arxiv.org/licenses/nonexclusive-distrib/1.0/
- Abstract: LLM-based systems increasingly incorporate long-term memory to improve cross-session continuity. However, once insecure coding preferences are stored, they may silently influence security-critical decisions in subsequent generations. In this study, we conduct the first systematic empirical study on the impact of insecure coding preferences stored in long-term memory on the security of LLM-based code generation. We evaluate four LLMs (ChatGPT, Gemini, Qwen, and Grok) across five programming languages (Python, C, C++, Go, and JavaScript). Our results show that insecure memories significantly increase the risk of generating vulnerable code by 2.7-50.3 percentage points (pp). Moreover, they create a 5.4-14.0 percentage-point risk-warning gap, where warning-rate increases lag behind vulnerability-rate increases. Further analysis reveals that insecure memories are difficult to overwrite through normal interactions and can broadly influence model outputs even when prompts are phrased differently. Finally, we evaluate three mitigation strategies: security-requirement appending and memory storage reduce vulnerability rates by 19.7-33.6 pp but may degrade functional correctness by up to 15.9 pp; memory-level safety filtering achieves a 100\% detection rate on our evaluated risky memory entries and restores generation behavior to the without-memory baseline. Based on these findings, we provide actionable suggestions to improve the security of long-term memory in LLM-based code generation.
Related papers
- MemEvoBench: Benchmarking Memory MisEvolution in LLM Agents [78.95081012334116]
Large Language Models (LLMs) with persistent memory enhances interaction continuity and personalization but introduces new safety risks.<n> contaminated or biased memory accumulation can trigger abnormal agent behaviors.<n>MemeEvoBench is the first benchmark evaluating memory safety in LLM agents against adversarial memory injection, noisy tool outputs, and biased feedback.
arXiv Detail & Related papers (2026-04-17T07:29:52Z) - Learned or Memorized ? Quantifying Memorization Advantage in Code LLMs [12.31163751026309]
We evaluate 8 open-source code LLMs on 19 benchmarks across four task families.<n> Sensitivity patterns vary widely across models and tasks.<n>For these datasets, models may rely more on learned generalization than direct memorization.
arXiv Detail & Related papers (2026-04-15T15:43:10Z) - SecPI: Secure Code Generation with Reasoning Models via Security Reasoning Internalization [50.71047638695205]
Reasoning language models (RLMs) are increasingly used in programming.<n>Yet, even state-of-the-art RLMs frequently introduce critical security vulnerabilities in generated code.<n>We present SecPI, a fine-tuning pipeline that teaches RLMs to internalize structured security reasoning.
arXiv Detail & Related papers (2026-04-04T04:29:11Z) - NextMem: Towards Latent Factual Memory for LLM-based Agents [58.35585202907478]
NextMem is a latent factual memory framework that utilizes an autoregressive autoencoder to efficiently construct latent memory.<n>Extensive experiments demonstrate that NextMem achieves superior performance.
arXiv Detail & Related papers (2026-02-26T14:35:27Z) - PersistBench: When Should Long-Term Memories Be Forgotten by LLMs? [4.657424818207681]
We introduce PersistBench to measure the extent of long-term memory-specific safety risks.<n>We identify two long-term memory-specific risks: cross-domain leakage and memory-induced sycophancy.<n>Our benchmark encourages the development of more robust and safer long-term memory usage in frontier conversational systems.
arXiv Detail & Related papers (2026-02-01T10:44:58Z) - RealSec-bench: A Benchmark for Evaluating Secure Code Generation in Real-World Repositories [58.32028251925354]
Large Language Models (LLMs) have demonstrated remarkable capabilities in code generation, but their proficiency in producing secure code remains a critical, under-explored area.<n>We introduce RealSec-bench, a new benchmark for secure code generation meticulously constructed from real-world, high-risk Java repositories.
arXiv Detail & Related papers (2026-01-30T08:29:01Z) - Agentic Memory: Learning Unified Long-Term and Short-Term Memory Management for Large Language Model Agents [57.38404718635204]
Large language model (LLM) agents face fundamental limitations in long-horizon reasoning due to finite context windows.<n>Existing methods typically handle long-term memory (LTM) and short-term memory (STM) as separate components.<n>We propose Agentic Memory (AgeMem), a unified framework that integrates LTM and STM management directly into the agent's policy.
arXiv Detail & Related papers (2026-01-05T08:24:16Z) - An Exploratory Study on Fine-Tuning Large Language Models for Secure Code Generation [16.000227726163967]
We study whether fine-tuning pre-trained Large Language Models on datasets of vulnerability-fixing commits can promote secure code generation.<n>We crawl a fine-tuning dataset (14,622 C/C++ files) for secure code generation by collecting code fixes of confirmed vulnerabilities from open-source repositories.<n>We observe maximum improvements in security of 6.4% in C language and 5.0% in C++ language.
arXiv Detail & Related papers (2024-08-17T02:51:27Z) - SORRY-Bench: Systematically Evaluating Large Language Model Safety Refusal [64.9938658716425]
SORRY-Bench is a proposed benchmark for evaluating large language models' (LLMs) ability to recognize and reject unsafe user requests.<n>First, existing methods often use coarse-grained taxonomy of unsafe topics, and are over-representing some fine-grained topics.<n>Second, linguistic characteristics and formatting of prompts are often overlooked, like different languages, dialects, and more -- which are only implicitly considered in many evaluations.
arXiv Detail & Related papers (2024-06-20T17:56:07Z)
This list is automatically generated from the titles and abstracts of the papers in this site.
This site does not guarantee the quality of this site (including all information) and is not responsible for any consequences.