Skip to content

结合大语言模型、静态分析工具及领域知识来检测封装(safe 函数)是否为被调 unsafe API 的所有合约(contracts)提供保障,从而判断其健全性。 Safe4U 首先利用静态分析工具获取目标封装的相关上下文,并将 unsafe API 文档中冗长的安全性说明(Safety section)分解为细粒度的合约,并为每个合约进行分类。 随后结合领域知识和大语言模型的推理能力对每个合约进行单独分析,并最终将细粒度的分析结果聚合为对目标封装的健全性判断。 该方法不但能够检测出不健全的不安全调用,还能帮助维护人员定位细粒度的不健全来源,即具体的被调用不安全函数和无保障合约。

使用说明

Safe4U 的流程分为以下几个步骤:

  1. 爬取 Crates 并提取包含 unsafe 代码块的候选函数(脚本
  2. 使用 context_retriever 程序检索上下文
  3. 根据上下文检查并筛选候选函数(脚本
  4. 将 Safety section 分解为细粒度的分类合约
  5. 通过检查所有合约是否得到保证来验证函数

Prerequisite

  • 系统: Linux, e.g., Ubuntu, Debian.
  • 环境:
    • python: python==3.10
    • python 依赖: pip install -r requirements.txt
    • Rust 环境: latest stable tool-chain
  • 大模型:

获取上下文

按照以下模板在config.toml填写配置:

toml
[target]
function_locate = "start_line"   <- optional
item_root_path = "path_dir_for_functions"
repo_root_path = "root_dir_for_repositories"
result_root_path = "root_dir_for_retrieve_result"

然后,执行 cargo run target。获取上下文之后,可以使用sample_check剔除无法检查或者不相关的目标。

拆分和分类

执行decompose_safety_and_classify.py来拆分和分类 unsafe API 的原始安全性说明,prompt 推荐使用decompose_with_self_check:

plain
--prompt PROMPT
    Prompt name
--model MODEL
    Model name(OpenAI) or path(Local Models)
--target TARGET [TARGET ...]
    Sample target (risky, filtered_unsafe, 11cve, scan)
--device DEVICE
    CUDA device number

检查细粒度合约

运行batch_eval_vllm.py来调用 LLM 检查拆分后的细粒度合约:

plain
--prompt [PROMPT ...]
    Prompt name (e.g., Safe4U, basic_check)
--model MODEL
    Model name or path (e.g., Meta-Llama-3-8B-Instruct)
--target TARGET [TARGET ...]
    Sample target (e.g., risky, filtered_unsafe, 11cve)
--device DEVICE
    CUDA device number (e.g., 0, 1)

检查结果在result目录下

最后更新于: