Manuscript Offprint

arXiv:2610.10345

SLDR: Defending Against Malicious Fine-tuning via Selective Layers Recovery and Dynamic Routing

Hui Zhang, Yachao Yuan, Jiayun Wang, Yuanzhuo Li, Hongtao Wang, Yali Yuan

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Fine-tuning-as-a-service enables users to adapt aligned large language models (LLMs) to specialized tasks, but malicious fine-tuning can erode refusal behavior while preserving task performance on legitimate inputs. We revisit recent layer-wise safety diagnostics and find that safety sensitivity is signed: scaling different layers can strengthen refusal, weaken it, or have little effect. Motivated by this observation, we propose SLDR, a post-fine-tuning defense based on Selective Layers Recovery and Dynamic Routing. SLDR trains a LoRA recovery adapter only on the layers with the maximum and minimum sensitivity scores in the signed spectrum, and uses representation-based dynamic routing inference to activate the adapter only for malicious queries. Across four model architectures, five downstream tasks, and four harmful benchmarks, SLDR substantially reduces harmful outputs while preserving downstream utility. On Llama3.1/SST2, SLDR reduces the average harmful score from 11.54 to 0.08 while maintaining downstream accuracy, and the harmful score remains near zero under poisoning ratios up to 0.9. The code is available at https://github.com/Stardust457/SLDR.

This paper contains offensive and harmful examples.

1 Introduction

Fine-tuning-as-a-service enables users to adapt aligned LLMs with their own data, facilitating domain-specific customization. However, the same interface can be exploited by attackers to compromise the model's safety alignment. By mixing a small number of harmful instruction-response pairs into otherwise benign task data, malicious fine-tuning can induce the model to respond to harmful queries while still retaining the intended task behavior on legitimate inputs [13, 20, 24, 49]. This creates a recovery challenge for the service provider: how to restore refusal behavior without sacrificing the utility gained from user-provided data.

Existing defenses include alignment-phase defense, fine-tuning constraints, and post-compromise repair [10, 11, 12, 14, 15, 33, 41, 52]. Their limitations fall into two categories. The first constrains fine-tuning before harmful samples are learned, but may fail to generalize to unseen attacks. The second relies on coarse-grained model-level recovery after fine-tuning, such as perturbing weights or pruning harmful parameters, entangling safety repair with task-specific behavior, making it difficult to restore refusal capabilities without compromising benign task performance.

∗^{*}These authors contributed equally.

†^{\dagger}Corresponding author.

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

This motivates us to shift from coarse model-level recovery to layer-wise diagnosis. Prior work has shown that safety-relevant layers can be localized by perturbing layer parameters and observing changes in refusal behavior [21, 23]. Yet these diagnostics mainly reveal where safety behavior is concentrated, not the direction of each layer's effect. In other words, they do not distinguish whether a layer reinforces refusal or weakens it. We therefore turn layer-wise scaling into a signed diagnostic and ask: which layers make refusal more likely when strengthened, and which suppress it?

We instantiate this layer-wise diagnosis with a lightweight scaling probe. For each layer, we perturb its attention and feed-forward weights and measure the change in refusal on over-refusal prompts. As shown in Figure 1, the resulting sensitivity scores form a signed spectrum: some layers make refusal stronger when scaled, some make it weaker, and many have little effect. This suggests a recovery strategy that treats layers differently according to their effect on refusal. Motivated by this observation, we propose SLDR, a post-fine-tuning defense based on Selective Layers Recovery and Dynamic Routing. After task adaptation, SLDR trains a LoRA recovery adapter only on the layers with the maximum and minimum sensitivity scores in the signed spectrum. Layers with near-zero scores are largely insensitive to refusal.

Figure

Figure 1: Signed layer-sensitivity scores for Llama3.1-8B-Instruct. Positive scores (green) indicate layers whose scaling increases refusal, while negative scores (red) indicate layers whose scaling decreases refusal.

under the probe, indicating that adjusting them would have minimal impact on refusal behavior. At inference time, malicious scores are computed by extracting representations. Harmful queries whose malicious scores exceed the threshold activate the adapter, while benign task queries falling below continue to use the unmodified fine-tuned model.

We evaluate SLDR under LoRA-based malicious fine-tuning across Llama3.1, Llama3 [7], Qwen2.5 [46], and Mistral-v0.2 [17], covering five downstream tasks and four harmful benchmarks. In the default Llama3.1/SST2 setting, SLDR reduces the average harmful score from 11.54 to 0.08 while matching downstream accuracy, and it remains near zero under poisoning ratios up to 0.9. Our contributions are threefold.

• We move beyond locating safety-sensitive layers and identify a signed layer-wise pattern in refusal behavior: scaling different layers can increase, decrease, or have little effect on refusal.

• We propose SLDR, a post-fine-tuning defense that turns this signed diagnosis into selective recovery by training a LoRA recovery adapter only on the layers with the maximum/minimum sensitivity scores in the spectrum, and activating it conditionally at inference.

• We conduct extensive experiments across model architectures, downstream tasks, poisoning ratios, and harmful benchmarks, showing that SLDR substantially reduces harmful outputs while preserving the task behavior acquired during fine-tuning.

References (54)