From nobody Thu Oct 2 05:06:29 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47F2B308F3D for ; Mon, 22 Sep 2025 13:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758546283; cv=none; b=aSxXDFyYGITWHVFTe0rb2N2e0xfC/LIv/12VdOAZL96f3oB6j9U2in8tFt5xEULzNH0j8dBX0JLc4o2H90skA+XeVE6Uk/UShkpVaW9Vy8AFplIWCgGoDPGE7Jz97p/WczeZ7b4ACMMxSOqjZkKoE5rBVcqcwVpIeHuYS5eKPrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758546283; c=relaxed/simple; bh=DwWRtJ3rd/CdkhxYjHs4NtsDoNLv/YDE8+Xpp6baCpc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=l9TLeQoJAOrKVJbZGRN2eht0JOjO+skVTzDTtD6sRE/4GQyzF78mgOCGI028y4I1uGgRb6fMP+8kz8vIMtwx0alJEOC+R5FHjvpHY8x/4ehVQgGk2ZkM7MssoMcruG4cHCwpO2QXj05Rh4JIYeSwKSI+DzLHHmtXsPu8nGiAUUQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abarnas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=A+Xu+yV8; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--abarnas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="A+Xu+yV8" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3ee10a24246so3019082f8f.3 for ; Mon, 22 Sep 2025 06:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758546279; x=1759151079; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=dUR5XlhmBsWXkvLElhn0cMkxqmlD65gTww2Sp2iyBR8=; b=A+Xu+yV8/7w8DgZUaRqFY1goVbrnavDygpUJ8S9mwv/W+YHHy4ARHHIO+PTRZlO5LW Kkz3/KmooOcbArz2U+sokkckiIVcVpCihAyyIlVcWRilfyXh8UjQW0exCFYQXNDGNPq7 Vst8X4OdvX6V2NUWmlcUnbBFBPqJtToX8vrKRRcmjXIDD8IMbNh1OHqd6nXTqRQQHh24 BtrbMAscI3bd2JTcxj0lugKa+iCWab82J4lY+vdCVf1Dg8/rP7zhLd/sfKr0shr/uvT5 agJQUumHTozmdzWMZK7byukCkPjN76vsWbnPLSkRcM4u9/60sB3L/L8GVXyh3hYdrqaq 9r7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758546279; x=1759151079; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=dUR5XlhmBsWXkvLElhn0cMkxqmlD65gTww2Sp2iyBR8=; b=mnarLwntKExw1lleLQgvHVrMtJV0kQ4NF2IDa2TWsob41KatENnuoQYGGNE9DIVN9O 0IbrCvL+7Or+y6DR4af1cTe0ZnQ6Y3QBLXe1Sovf/BeHnUYOYnX4e0Xlstax9A0ut4XR pgxxGAeEcAY1ASadDmFk9ZpsAbgQ2EToThB1KN2DpXsrczI6mqdM0mkn/OJIxq/iFXwS 26DiLf/sGXV105ZXw2NTR/TdpjFUIEPhowQmDhG8APCEINeqaoNWj7dNvgP0L3Mliy9/ VrBOTx6p07AmlLJjNVv5x1qoC3sULfQ2PoMP3JcEOiKtmxan6pqxHsMAETF+NTTSVzVT KmFA== X-Forwarded-Encrypted: i=1; AJvYcCVylwPkvBd2E/XX15ILou2mlGU8GAdQKulCNm20U6CkYRzKUvR+3GtISrUycc2G7et4YYXZLbt+VSfrgh8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywdjqt83xy43TIX8HTpBQTUlS9Waz4zAGDYmwtp1SBMNsVM55H4 Sg0wvQr1DpGXvqENxjETuhzKNq4Mx4RiV8ZFKwuTfCwU+AwlfV2TAylhws3R7h/yi0ycEITdjaU m+t+2oq5awA== X-Google-Smtp-Source: AGHT+IEtBKpSpcNG7WXoHemURXbK3vDWac5r7lnyDOiYg4Vglm5g7x0+IvtA9rmKqvE/dsv0G1maA9DGUunn X-Received: from wrhb2.prod.google.com ([2002:adf:c742:0:b0:3df:3aeb:c0a8]) (user=abarnas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2c05:b0:3ee:11d1:29dd with SMTP id ffacd0b85a97d-3ee83da00e7mr9797391f8f.35.1758546278683; Mon, 22 Sep 2025 06:04:38 -0700 (PDT) Date: Mon, 22 Sep 2025 13:04:26 +0000 In-Reply-To: <20250922130427.2904977-1-abarnas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250922130427.2904977-1-abarnas@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250922130427.2904977-2-abarnas@google.com> Subject: [PATCH v2 1/2] arch: arm64: Fail module loading if dynamic SCS patching fails From: "=?UTF-8?q?Adrian=20Barna=C5=9B?=" To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: "=?UTF-8?q?Adrian=20Barna=C5=9B?=" , Catalin Marinas , Will Deacon , Ard Biesheuvel , Dylan Hatch , Mark Rutland Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disallow a module to load if SCS dynamic patching fails for its code. For module loading, instead of running a dry-run to check for patching errors, try to run patching in the first run and propagate any errors so module loading will fail. Signed-off-by: Adrian Barna=C5=9B Reviewed-by: Ard Biesheuvel --- arch/arm64/include/asm/scs.h | 2 +- arch/arm64/kernel/module.c | 12 ++++++++++-- arch/arm64/kernel/pi/map_kernel.c | 2 +- arch/arm64/kernel/pi/patch-scs.c | 10 ++++++---- arch/arm64/kernel/pi/pi.h | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/scs.h b/arch/arm64/include/asm/scs.h index a76f9b387a26..c59f6324f2bb 100644 --- a/arch/arm64/include/asm/scs.h +++ b/arch/arm64/include/asm/scs.h @@ -53,7 +53,7 @@ enum { EDYNSCS_INVALID_CFA_OPCODE =3D 4, }; =20 -int __pi_scs_patch(const u8 eh_frame[], int size); +int __pi_scs_patch(const u8 eh_frame[], int size, bool skip_dry_run); =20 #endif /* __ASSEMBLY __ */ =20 diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index 40148d2725ce..d32ab7dd86a8 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -484,10 +484,18 @@ int module_finalize(const Elf_Ehdr *hdr, if (scs_is_dynamic()) { s =3D find_section(hdr, sechdrs, ".init.eh_frame"); if (s) { - ret =3D __pi_scs_patch((void *)s->sh_addr, s->sh_size); - if (ret) + /* + * Because we can reject modules that are malformed + * so SCS patching fails, skip dry run and try to patch + * it in place. If patching fails, the module would not + * be loaded anyway. + */ + ret =3D __pi_scs_patch((void *)s->sh_addr, s->sh_size, true); + if (ret) { pr_err("module %s: error occurred during dynamic SCS patching (%d)\n", me->name, ret); + return -ENOEXEC; + } } } =20 diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_k= ernel.c index 0f4bd7771859..7187eda9e8a5 100644 --- a/arch/arm64/kernel/pi/map_kernel.c +++ b/arch/arm64/kernel/pi/map_kernel.c @@ -98,7 +98,7 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_of= fset, int root_level) =20 if (enable_scs) { scs_patch(__eh_frame_start + va_offset, - __eh_frame_end - __eh_frame_start); + __eh_frame_end - __eh_frame_start, false); asm("ic ialluis"); =20 dynamic_scs_is_enabled =3D true; diff --git a/arch/arm64/kernel/pi/patch-scs.c b/arch/arm64/kernel/pi/patch-= scs.c index 55d0cd64ef71..bbe7d30ed12b 100644 --- a/arch/arm64/kernel/pi/patch-scs.c +++ b/arch/arm64/kernel/pi/patch-scs.c @@ -225,7 +225,7 @@ static int scs_handle_fde_frame(const struct eh_frame *= frame, return 0; } =20 -int scs_patch(const u8 eh_frame[], int size) +int scs_patch(const u8 eh_frame[], int size, bool skip_dry_run) { int code_alignment_factor =3D 1; bool fde_use_sdata8 =3D false; @@ -277,11 +277,13 @@ int scs_patch(const u8 eh_frame[], int size) } } else { ret =3D scs_handle_fde_frame(frame, code_alignment_factor, - fde_use_sdata8, true); + fde_use_sdata8, !skip_dry_run); if (ret) return ret; - scs_handle_fde_frame(frame, code_alignment_factor, - fde_use_sdata8, false); + + if (!skip_dry_run) + scs_handle_fde_frame(frame, code_alignment_factor, + fde_use_sdata8, false); } =20 p +=3D sizeof(frame->size) + frame->size; diff --git a/arch/arm64/kernel/pi/pi.h b/arch/arm64/kernel/pi/pi.h index 46cafee7829f..220da972cce1 100644 --- a/arch/arm64/kernel/pi/pi.h +++ b/arch/arm64/kernel/pi/pi.h @@ -27,7 +27,7 @@ extern pgd_t init_pg_dir[], init_pg_end[]; void init_feature_override(u64 boot_status, const void *fdt, int chosen); u64 kaslr_early_init(void *fdt, int chosen); void relocate_kernel(u64 offset); -int scs_patch(const u8 eh_frame[], int size); +int scs_patch(const u8 eh_frame[], int size, bool skip_dry_run); =20 void map_range(u64 *pgd, u64 start, u64 end, u64 pa, pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, u64 va_offset); --=20 2.51.0.534.gc79095c0ca-goog From nobody Thu Oct 2 05:06:29 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63B4C3093CD for ; Mon, 22 Sep 2025 13:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758546283; cv=none; b=PsWtqGFiXYbZRD6M+lpCzjTginOhsyh6QUo1f7rU5PFBmqWVjH4pjeKYnngSXcCmA8QTXPQ18gT7DR9w2QBxgeCHF6F0P1vlrIxThay3pnCXjgyf/pXQ3DBf/H/0aOZlqVhEY1Wj3Ea2PnmYgVE+azwz3yYTs3Nff4LT7mi6dwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758546283; c=relaxed/simple; bh=FMYoIbZiYa0DS1g8RqN8iMEHg5Ih56HvWTWA1nUUjns=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dtZ7FNZLtm4y/jIRmhtYGU+kdnRmR5gCCgQuZ3P6PwcMD5a7gIyahkqmLBYDc9ROpeAl8DaWGG6o/kbrKYfsuQhYwXzVK8ws9WTTm8cs97zKZsrhKfyNmDZa3KA8xWzbg5qQ98rhfaK4hyv2rGzKZq/myYNyeyF9tMPaqncZ+go= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abarnas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vMRH8qs2; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--abarnas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vMRH8qs2" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45de27bf706so24218245e9.0 for ; Mon, 22 Sep 2025 06:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758546280; x=1759151080; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=JwElNbtoU6ryjkvg9ptzkO1BXP1SQnPo2fMtx+HCV1o=; b=vMRH8qs2UGP1wU3xBEJ92i+Wuw4VAsCQcgypji8gA21KZCbOcYw4tKPj9cBGrAV5Nq FUgOkg2A2v8RLLyXMNm4vTixJiNYPlyRUVzzOg/CbVN5Oukn5nQUYCPMrwhczfMY2A0X wYKuZae8nRrRZPSWwLQ6D2eYI37N6IPF7NubbOcTkRQpobxM+7gasIeYBcag+mCe6kU1 KZb7favcWFSLd6Kzba43UJ1k9q6aLOm1iDdkuJAyuafIn66ZPG/XN0L4nkcapNwO5+Ig LMk/D8zU4GDZ+6+4x8GUJwNg2e3sawV5JIudyfyMGlNs/p1REFH4m61hIBkmYlzi0B9L opjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758546280; x=1759151080; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=JwElNbtoU6ryjkvg9ptzkO1BXP1SQnPo2fMtx+HCV1o=; b=smE/5IolTHGyUPw1rDa3bq9TG/6AEUu8kRG5e2dEaqUhmokN0ouR5ZPgWldKQmFU1M qJhiQ0SQdfD9ycjnFogUQ/U2f1lxm8Z/oYec6XPA08NT2UqgGd//clGqi1WdeFXkMsDd HFr2sCXpQ3+AioOUM3rOs+5uYkLTCf+2zTmsbem+akYtGH7eYFcqygIbESNiYI3zbFVk BZOlCsPKyGDtv/vqrUU/5XOJbTgSH28ngcNJRqkqy1x8dJCDojcSzwmq5eKWIiEYfk5g NRM2cA/ZvoXRhzpYpfKLOb7H8nB3RmQQ3YFUaEs51w2I6aVgYJUkkEPwHUNkBbxxtVzt Vc9g== X-Forwarded-Encrypted: i=1; AJvYcCW2G9AW9DEnl1VfGpltJRW+cihiAa0gwnJpYiOpC2ngdW6hy94hzwyMo5UNdUJrhbD0zi/vrRo66plbulk=@vger.kernel.org X-Gm-Message-State: AOJu0YxyhMgiUbsbPSwwRKUsWwCz6bzqwmHvhvVszrK+r+rJvep8wIep dkRSSsrRhCCZ6S5vhKA+twIUFsfOZfDQnTLB2v9EgGazwd7QmPtigxKD2t932pZhMQw2edL0Q51 o8+6gBHHRzw== X-Google-Smtp-Source: AGHT+IEDFD3rxKYHYkV6r/KD3nctDYqhuTTynS2EvYjFqxAQWJnCTSezVvcxkZmuveyS5xGnrwZ/qy15N5O/ X-Received: from wmbez5.prod.google.com ([2002:a05:600c:83c5:b0:45f:2d07:91f9]) (user=abarnas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:630e:b0:46c:9e81:ad0 with SMTP id 5b1f17b1804b1-46c9e904ffamr39494195e9.0.1758546279719; Mon, 22 Sep 2025 06:04:39 -0700 (PDT) Date: Mon, 22 Sep 2025 13:04:27 +0000 In-Reply-To: <20250922130427.2904977-1-abarnas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250922130427.2904977-1-abarnas@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250922130427.2904977-3-abarnas@google.com> Subject: [PATCH v2 2/2] arch: arm64: Reject modules with internal alternative callbacks From: "=?UTF-8?q?Adrian=20Barna=C5=9B?=" To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: "=?UTF-8?q?Adrian=20Barna=C5=9B?=" , Catalin Marinas , Will Deacon , Ard Biesheuvel , Dylan Hatch , Mark Rutland , Fanqin Cui Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During module loading, check if a callback function used by the alternatives specified in the '.altinstruction' ELF section (if present) is located in core kernel .text. If not fail module loading before callback is called. Reported-by: Fanqin Cui Closes: https://lore.kernel.org/all/20250807072700.348514-1-fanqincui@163.c= om/ Signed-off-by: Adrian Barna=C5=9B Reviewed-by: Ard Biesheuvel --- arch/arm64/include/asm/alternative.h | 7 +++++-- arch/arm64/kernel/alternative.c | 19 ++++++++++++------- arch/arm64/kernel/module.c | 9 +++++++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/= alternative.h index 00d97b8a757f..51746005239b 100644 --- a/arch/arm64/include/asm/alternative.h +++ b/arch/arm64/include/asm/alternative.h @@ -26,9 +26,12 @@ void __init apply_alternatives_all(void); bool alternative_is_applied(u16 cpucap); =20 #ifdef CONFIG_MODULES -void apply_alternatives_module(void *start, size_t length); +int apply_alternatives_module(void *start, size_t length); #else -static inline void apply_alternatives_module(void *start, size_t length) {= } +static inline int apply_alternatives_module(void *start, size_t length) +{ + return 0; +} #endif =20 void alt_cb_patch_nops(struct alt_instr *alt, __le32 *origptr, diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternativ= e.c index 8ff6610af496..11893a0360ad 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -139,9 +139,9 @@ static noinstr void clean_dcache_range_nopatch(u64 star= t, u64 end) } while (cur +=3D d_size, cur < end); } =20 -static void __apply_alternatives(const struct alt_region *region, - bool is_module, - unsigned long *cpucap_mask) +static int __apply_alternatives(const struct alt_region *region, + bool is_module, + unsigned long *cpucap_mask) { struct alt_instr *alt; __le32 *origptr, *updptr; @@ -166,10 +166,13 @@ static void __apply_alternatives(const struct alt_reg= ion *region, updptr =3D is_module ? origptr : lm_alias(origptr); nr_inst =3D alt->orig_len / AARCH64_INSN_SIZE; =20 - if (ALT_HAS_CB(alt)) + if (ALT_HAS_CB(alt)) { alt_cb =3D ALT_REPL_PTR(alt); - else + if (!core_kernel_text((unsigned long)alt_cb)) + return -ENOEXEC; + } else { alt_cb =3D patch_alternative; + } =20 alt_cb(alt, origptr, updptr, nr_inst); =20 @@ -193,6 +196,8 @@ static void __apply_alternatives(const struct alt_regio= n *region, bitmap_and(applied_alternatives, applied_alternatives, system_cpucaps, ARM64_NCAPS); } + + return 0; } =20 static void __init apply_alternatives_vdso(void) @@ -277,7 +282,7 @@ void __init apply_boot_alternatives(void) } =20 #ifdef CONFIG_MODULES -void apply_alternatives_module(void *start, size_t length) +int apply_alternatives_module(void *start, size_t length) { struct alt_region region =3D { .begin =3D start, @@ -287,7 +292,7 @@ void apply_alternatives_module(void *start, size_t leng= th) =20 bitmap_fill(all_capabilities, ARM64_NCAPS); =20 - __apply_alternatives(®ion, true, &all_capabilities[0]); + return __apply_alternatives(®ion, true, &all_capabilities[0]); } #endif =20 diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index d32ab7dd86a8..6e5b488a219e 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -478,8 +478,13 @@ int module_finalize(const Elf_Ehdr *hdr, int ret; =20 s =3D find_section(hdr, sechdrs, ".altinstructions"); - if (s) - apply_alternatives_module((void *)s->sh_addr, s->sh_size); + if (s) { + ret =3D apply_alternatives_module((void *)s->sh_addr, s->sh_size); + if (ret < 0) { + pr_err("module %s: error occurred when applying alternatives\n", me->na= me); + return ret; + } + } =20 if (scs_is_dynamic()) { s =3D find_section(hdr, sechdrs, ".init.eh_frame"); --=20 2.51.0.534.gc79095c0ca-goog