联邦学习隐私攻击

尽管联邦学习通过数据最小化提供了隐私保护,但模型更新本身仍可能泄露敏感信息。本章系统分析联邦学习中各种隐私攻击的机制、威胁程度和防御方向。

1. 攻击威胁模型

1.1 攻击者能力假设

攻击者类型能力威胁等级
诚实但好奇(Honest-but-Curious)只能观察和记录协议执行
恶意服务器可操纵模型参数、发送伪造更新
恶意客户端可发送任意梯度、参与协作攻击中-高
外部攻击者窃听通信、获取部分信息

1.2 可被利用的信息

  1. 模型更新:梯度、权重更新
  2. 聚合结果:服务器公开的聚合模型
  3. 训练轨迹:模型在多轮训练中的变化
  4. 客户端元数据:参与模式、通信时间

2. 成员推断攻击(MIA)

2.1 攻击目标

判断特定数据样本是否参与了模型训练:

2.2 基于损失的推断

训练数据通常产生更低的损失值:

def mia_loss_based(shadow_model, target_model, data_loader):
    """
    基于损失的成员推断攻击
    """
    shadow_model.eval()
    target_model.eval()
    
    # 训练集和测试集的损失分布通常不同
    shadow_losses = []
    for x, y in shadow_model.train_data:
        with torch.no_grad():
            loss = F.cross_entropy(shadow_model(x), y)
        shadow_losses.append((loss.item(), True))  # True=成员
    
    for x, y in shadow_model.test_data:
        with torch.no_grad():
            loss = F.cross_entropy(shadow_model(x), y)
        shadow_losses.append((loss.item(), False))  # False=非成员
    
    # 训练攻击模型
    attack_model = train_attack_model(shadow_losses)
    
    # 对目标模型进行攻击
    target_losses = []
    for x, y in data_loader:
        with torch.no_grad():
            loss = F.cross_entropy(target_model(x), y)
        target_losses.append(loss.item())
    
    return attack_model.predict(target_losses)

2.3 FedMIA:利用”All for One”原则

FedMIA1提出利用所有客户端的信息进行更有效的成员推断:

def fedmia_attack(target_client_updates, all_client_updates, 
                  round_history, num_rounds=10):
    """
    FedMIA: 利用跨客户端和跨轮信息进行成员推断
    """
    membership_scores = {}
    
    # 构建似然比检验
    for target_round in range(num_rounds):
        target_update = target_client_updates[target_round]
        
        # 计算目标客户端更新相对于其他客户端的似然比
        likelihood_ratio = compute_likelihood_ratio(
            target_update,
            all_client_updates[target_round]
        )
        
        # 累积多轮信息
        if target_client_updates not in membership_scores:
            membership_scores[target_client] = []
        membership_scores[target_client].append(likelihood_ratio)
    
    # 综合多轮结果
    final_scores = {
        client: aggregate_scores(scores) 
        for client, scores in membership_scores.items()
    }
    
    return final_scores

2.4 攻击效果评估

数据集攻击准确率AUC
CIFAR-1072.3%0.81
Purchase-10078.5%0.86
CelebA68.9%0.75
基因组数据79.0%0.87

3. 梯度反转攻击(Gradient Inversion)

3.1 攻击原理

攻击者通过梯度信息重建原始训练数据:

其中 是观察到的梯度, 是正则化项。

3.2 深度梯度反转

def deep_gradient_inversion(model, gradient, original_image=None,
                           lr=0.1, iterations=100):
    """
    深度梯度反转攻击
    """
    # 初始化重建图像
    x_recon = torch.randn(gradient.shape).requires_grad_(True)
    optimizer = torch.optim.Adam([x_recon], lr=lr)
    
    for i in range(iterations):
        optimizer.zero_grad()
        
        # 计算模型在前向传播的梯度
        output = model(x_recon)
        loss = output.mean()  # 或其他适当的损失
        
        # 反向传播获取梯度
        model.zero_grad()
        loss.backward()
        
        # 重建图像的梯度应该匹配观察到的梯度
        grad_loss = F.mse_loss(x_recon.grad, gradient)
        
        # 总损失 = 梯度匹配损失 + 正则化
        total_loss = grad_loss
        
        if original_image is not None:
            # 如果有原始图像,加入距离正则化
            total_loss += 0.001 * F.mse_loss(x_recon, original_image)
        
        total_loss.backward()
        optimizer.step()
        
        # 应用像素裁剪
        x_recon.data = torch.clamp(x_recon.data, 0, 1)
    
    return x_recon.detach()

3.3 分析梯度反转攻击

FedMIA的实验表明1

  • 多轮信息泄露:随着训练轮数增加,攻击效果显著提升
  • 聚合器威胁更大:恶意的聚合器可以进行更有效的攻击
  • 数据特性影响:高维数据、图像数据更容易被攻击

4. 数据重构攻击

4.1 超平面攻击

Diana等人提出的超平面攻击2利用全连接层的几何特性:

核心思想:对于线性层 ,梯度正交于输入:

def hyperplane_attack(global_model, malicious_params, 
                      batch_size=128, num_iterations=500):
    """
    超平面数据重构攻击
    """
    # 构造恶意参数
    set_malicious_parameters(global_model, malicious_params)
    
    # 获取梯度
    gradient = get_gradient(global_model)
    
    # 重建批次数据
    reconstructed_data = []
    for i in range(batch_size):
        # 每个样本对应一个超平面
        hyperplane = extract_hyperplane(gradient, i)
        
        # 在超平面上采样
        sample = sample_on_hyperplane(hyperplane, num_iterations)
        reconstructed_data.append(sample)
    
    return torch.stack(reconstructed_data)

关键发现:可完美重构任意大小的批次(比之前方法大两个数量级)

4.2 全卷积层梯度反转

def conv_gradient_inversion(model, grad, known_labels=None,
                            image_size=(3, 224, 224)):
    """
    针对卷积层的梯度反转
    """
    x = torch.randn(1, *image_size, requires_grad=True)
    optimizer = torch.optim.Adam([x], lr=0.01)
    
    for iteration in range(1000):
        optimizer.zero_grad()
        
        # 前向传播
        output = model(x)
        
        # 如果已知标签,使用标签损失
        if known_labels is not None:
            loss = F.cross_entropy(output, known_labels)
        else:
            loss = output.mean()
        
        # 反向传播
        model.zero_grad()
        loss.backward()
        
        # 梯度匹配损失
        grad_loss = sum(
            F.mse_loss(x.grad, g) 
            for x, g in zip(model.parameters(), grad) 
            if x.grad is not None
        )
        
        grad_loss.backward()
        optimizer.step()
        
        # 像素裁剪
        x.data = torch.clamp(x.data, 0, 1)
    
    return x.detach()

5. 标签推断攻击

5.1 基于梯度方向的标签推断

def label_inference_attack(client_gradient, num_classes):
    """
    通过梯度推断训练数据的标签分布
    """
    # 梯度符号反映标签分布
    grad_sign = torch.sign(client_gradient)
    
    # 统计每个类的梯度模式
    label_patterns = []
    for class_id in range(num_classes):
        # 模拟one-hot标签的梯度
        one_hot = torch.zeros(num_classes)
        one_hot[class_id] = 1.0
        
        # 计算与各类标签的相似度
        similarity = compute_similarity(grad_sign, one_hot)
        label_patterns.append(similarity)
    
    # 推断最可能的标签
    inferred_labels = torch.argmax(torch.stack(label_patterns))
    confidence = torch.softmax(torch.stack(label_patterns), dim=0)
    
    return inferred_labels, confidence

5.2 标签推断攻击效果

数据集推断准确率
CIFAR-1085.3%
CIFAR-10062.1%
ImageNet71.8%

6. 来源推断攻击

6.1 攻击目标

不仅推断数据是否参与训练,还推断数据来自哪个客户端:

6.2 增强来源推断

def enhanced_source_inference(gradient_history, active_manipulation=False):
    """
    增强的来源推断攻击
    """
    client_signatures = {}
    
    for round_t, gradients in enumerate(gradient_history):
        for client_id, grad in enumerate(gradients):
            # 提取客户端特征
            features = extract_gradient_features(grad)
            
            if active_manipulation:
                # 主动操纵训练过程以增强差异
                features = enhance_differences(features, round_t)
            
            if client_id not in client_signatures:
                client_signatures[client_id] = []
            client_signatures[client_id].append(features)
    
    # 学习客户端特征模式
    client_patterns = {
        client_id: learn_pattern(signs) 
        for client_id, signs in client_signatures.items()
    }
    
    return client_patterns

7. LDP保护下的隐私风险

7.1 LDP理论保证的局限性

尽管本地差分隐私(LDP)被广泛使用,理论研究3表明:

理论下界:对于低多项式时间的攻击者,即使使用LDP,隐私风险仍然存在:

其中 是隐私预算, 是样本数量。

7.2 全连接层攻击

def ldp_violation_attack(model, ldp_gradient, epsilon):
    """
    针对LDP保护的全连接层攻击
    """
    # LDP添加的噪声规模
    noise_scale = 1.0 / epsilon
    
    # 估计真实梯度
    noisy_grad = ldp_gradient
    estimated_grad = noisy_grad  # LDP下噪声已足够大
    
    # 在全连接层,攻击仍然有效
    if has_fully_connected_layer(model):
        # 重建精度受噪声影响,但仍然泄露信息
        reconstruction_quality = estimate_reconstruction_quality(
            noisy_grad, noise_scale
        )
        
        return {
            'reconstruction_possible': reconstruction_quality > 0.1,
            'quality': reconstruction_quality,
            'privacy_leakage': compute_information_leakage(
                noisy_grad, noise_scale
            )
        }

7.3 注意力层攻击

def attention_layer_attack(model, ldp_gradient, num_heads=12):
    """
    针对自注意力层的LDP攻击
    """
    # 提取注意力相关的梯度
    attention_grads = extract_attention_gradients(ldp_gradient)
    
    # 即使有LDP保护,注意力模式仍可能泄露信息
    for head_id in range(num_heads):
        head_grad = attention_grads[head_id]
        noise_scale = compute_noise_scale(head_grad, epsilon)
        
        # 信息泄露分析
        if noise_scale < detect_threshold:
            # 噪声不足以掩盖信息
            leaked_info = extract_attention_pattern(head_grad)

8. 隐私攻击防御方向

8.1 攻击防御方法总结

攻击类型防御方法有效性
成员推断正则化、差分隐私中等
梯度反转梯度压缩、安全聚合
数据重构批量归一化、梯度扰动中-高
标签推断标签平滑、梯度裁剪
来源推断安全聚合、客户端聚类

8.2 实践建议

class PrivacyPreservingAggregator:
    """
    隐私保护聚合器
    """
    def __init__(self, epsilon=1.0, clip_norm=1.0):
        self.epsilon = epsilon
        self.clip_norm = clip_norm
    
    def secure_aggregate(self, gradients, client_weights):
        """
        安全聚合流程
        """
        # 步骤1:梯度裁剪
        clipped_grads = [self.clip_gradient(g) for g in gradients]
        
        # 步骤2:添加噪声(差分隐私)
        noisy_grads = [self.add_noise(g) for g in clipped_grads]
        
        # 步骤3:安全聚合
        aggregated = self.secure_sum(noisy_grads, client_weights)
        
        return aggregated
    
    def clip_gradient(self, gradient):
        norm = torch.norm(gradient)
        if norm > self.clip_norm:
            gradient = gradient * (self.clip_norm / norm)
        return gradient
    
    def add_noise(self, gradient):
        noise = torch.randn_like(gradient) * (1.0 / self.epsilon)
        return gradient + noise

9. 总结

联邦学习面临多种隐私威胁:

  1. 成员推断攻击:判断数据是否参与训练
  2. 梯度反转攻击:从梯度重建原始数据
  3. 数据重构攻击:利用层结构的几何特性
  4. 标签推断攻击:推断训练数据的标签信息
  5. 来源推断攻击:追踪数据来自哪个客户端
  6. LDP下的残余风险:即使使用差分隐私,信息泄露仍可能存在

防御需要综合考虑:

  • 隐私保护强度(隐私预算)
  • 模型效用(准确率)
  • 计算开销
  • 安全性假设

参考资料


相关主题[federated-learning-fundamentals][federated-learning-privacy-defense][information-bottleneck]

Footnotes

  1. FedMIA: “An Effective Membership Inference Attack Exploiting ‘All for One’ Principle in Federated Learning” (CVPR 2025) 2

  2. Diana et al. “Cutting Through Privacy: A Hyperplane-Based Data Reconstruction Attack in Federated Learning” (UAI 2025)

  3. Nguyen et al. “Theoretically Unmasking Inference Attacks Against LDP-Protected Clients in Federated Vision Models” (ICML 2025)