微调小型模型可精准检测Python代码漏洞,兼顾隐私与效率
Case Study: Fine-tuning Small Language Models for Accurate and Private CWE Detection in Python Code
- 用3.5亿参数模型+指令微调,专注识别Python中的25个常见漏洞
- 在500样本上达到99%准确率,召回率100%,完全避免漏检
- 适合需本地部署、保护代码隐私的开发团队使用
大型语言模型虽能识别代码安全漏洞(如MITRE Top 25 CWE),但依赖云端算力,存在隐私和成本问题。本文探索小型语言模型(SLM)作为替代方案,将3.5亿参数的codegen-mono模型针对Python代码进行指令微调,以检测常见漏洞。研究构建了500条带标注数据,结合大模型生成与人工审核。初始模型无法识别任何漏洞,经微调后在测试集上实现约99%准确率、98.08%精确率、100%召回率和99.04% F1分数。结果表明,微调后的小型模型可在本地高效、高精度完成漏洞检测,适合嵌入开发流程并保障代码隐私。
原文摘要 · Abstract (English)
Large Language Models (LLMs) have demonstrated significant capabilities in understanding and analyzing code for security vulnerabilities, such as Common Weakness Enumerations (CWEs). However, their reliance on cloud infrastructure and substantial computational requirements pose challenges for analyzing sensitive or proprietary codebases due to privacy concerns and inference costs. This work explores the potential of Small Language Models (SLMs) as a viable alternative for accurate, on-premise vulnerability detection. We investigated whether a 350-million parameter pre-trained code model (codegen-mono) could be effectively fine-tuned to detect the MITRE Top 25 CWEs specifically within Python code. To facilitate this, we developed a targeted dataset of 500 examples using a semi-supervised approach involving LLM-driven synthetic data generation coupled with meticulous human review. Initial tests confirmed that the base codegen-mono model completely failed to identify CWEs in our samples. However, after applying instruction-following fine-tuning, the specialized SLM achieved remarkable performance on our test set, yielding approximately 99% accuracy, 98.08% precision, 100% recall, and a 99.04% F1-score. These results strongly suggest that fine-tuned SLMs can serve as highly accurate and efficient tools for CWE detection, offering a practical and privacy-preserving solution for integrating advanced security analysis directly into development workflows.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。