From nobody Thu Oct 2 07:46:34 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 9F0213093A5 for ; Fri, 19 Sep 2025 12:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758284614; cv=none; b=eyjVuMMYdi8xhosAw3R0HlhWX0gT8uXCiBU0tfy1opzSUqMCrgG+kvMIYH5XAcsdeII6x6USHZFXwvVdblQ4o2RkvXVHpIorarwykoLeHO2GmAMWXmpgkj8F19Le9wSmyqpLfYTeR44kLCXrOSea06JK5viqtnYd32Ry4YxhmNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758284614; c=relaxed/simple; bh=PsO2XWHbF1yz3I3qxvPS4s5niHFvbCHeVXiIUEgUUwQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mJ8fF8Gu8wxO3q9HBx2oe7L2OIrriAtOVZeVaqclaSuQqLCUqcC5ISCMeasKgP5Ci6B2LvawNGQTifOoMBDsk0cJREWZ6/4//ZqodihiWiRycPdhiUB/g6B54K11isrSqdE/H7W1DucWDokj55AI/mtRlVu8tow6IPjDZsyIet8= 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=M6/lpI0t; arc=none smtp.client-ip=209.85.128.73 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="M6/lpI0t" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45f29c99f99so12996625e9.1 for ; Fri, 19 Sep 2025 05:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758284611; x=1758889411; 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=i2uPr5tRXP/hZXMndHpWASprs+At28VaxqR7vlo3oA8=; b=M6/lpI0tzIwaIdro2Frdmz7JJB6hDvGrvjgy7OnGG7n4XFZuByb+qOjdxN7rr9guez TQCXWZDK/swTa6/8XtNv1un6umO1APjhxDERM/SueRnp7WPU8qd893oWRukYdZhjgnLh n/HWi9O94ySo1x1duMv6phMv8O+xYCBpPXtLEPNwWc6Q3retM3Ni/CVwJ66l4uCaR8OO wXXK4hvZfNmyTfJAStOWK8xpSK9ullZKsPgkcZjp3u/vQoJ9hPtnDjQ8LYt6dGRwmfBM rIkdPQNhOQn4mg/7oem4O3I0AJLNMVgLhSnY3dz+4q+20912SAW30eCG3e/eiVBJv2R4 JNrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758284611; x=1758889411; 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=i2uPr5tRXP/hZXMndHpWASprs+At28VaxqR7vlo3oA8=; b=iam4FcBV7eBQmSmXAF6isDXivAkEqWbYPlwFSxJU2Gq8mI1rkJfNzoFRbpKCLUDU2y ndysJnDr0VBQ5ONnPeg5X03DcDdzQSAGrS7qpvUKl8AP5UOdS0nTGZ+RKogarfxhcfso PeiBbU6XMt+wTwKnPIvNCvliwK1WJIdg2KVw/NxGE4vw63JMMasEJqXfqh3DrVAkcU9Z YtSE66hKnsG+y5rGwYqTm7qiMwR3EQwKB1lgJrJDiLwFPFOGK1iaKrr4wNB6FAuMddTs yLv50IS0+jdeoto0BEuNIFXE7oDeWV/6EW8zNqgKPjSBIhc8TSkHyYdkhw2ZSd1Racth oUnw== X-Forwarded-Encrypted: i=1; AJvYcCV5wXodbeWD3bc11eUk5mFEwdJfmmggBZv4bBD44oqw2A3hlsP45T33h+qEEnb2J5vDEsPuEzyWVe4Pq2M=@vger.kernel.org X-Gm-Message-State: AOJu0YwWr5aV04yul7Iq2ChqBEHaWq6HxuxxdqPUsN+/sOlmYT1FpNwz 10BugOJllzVtUe3BLiKGAB4+WhUP/cQRHAnDrUdWj6cnrQ1ftwP+99FrWSr9NSfiKmvjCWz3Gcm dXHwR7amP3w== X-Google-Smtp-Source: AGHT+IG9GaXAZcjoq+ISPuOX7adIMdWJfDgiabm1SFO2WPOJbmJQLNLDFi21OpBUSrPr+CFdPSZdvz2oMtje X-Received: from wmbbi11.prod.google.com ([2002:a05:600c:3d8b:b0:45b:7580:5aa]) (user=abarnas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4714:b0:45b:615c:cd2 with SMTP id 5b1f17b1804b1-467e6b64fb2mr29795115e9.8.1758284611101; Fri, 19 Sep 2025 05:23:31 -0700 (PDT) Date: Fri, 19 Sep 2025 12:23:20 +0000 In-Reply-To: <20250919122321.946462-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: <20250919122321.946462-1-abarnas@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250919122321.946462-2-abarnas@google.com> Subject: [PATCH 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 --- arch/arm64/include/asm/scs.h | 2 +- arch/arm64/kernel/module.c | 6 ++++-- arch/arm64/kernel/pi/map_kernel.c | 2 +- arch/arm64/kernel/pi/patch-scs.c | 15 +++++++++++---- arch/arm64/kernel/pi/pi.h | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/scs.h b/arch/arm64/include/asm/scs.h index a76f9b387a26..ffcfcda87f10 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 is_module); =20 #endif /* __ASSEMBLY __ */ =20 diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index 40148d2725ce..5d6d228c6156 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -484,10 +484,12 @@ 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) + 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..78266fb1fa61 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 is_module) { int code_alignment_factor =3D 1; bool fde_use_sdata8 =3D false; @@ -276,12 +276,19 @@ int scs_patch(const u8 eh_frame[], int size) return EDYNSCS_INVALID_CIE_SDATA_SIZE; } } else { + /* + * For loadable module instead of running a dry run try + * to patch scs instruction in place and trigger error + * if failed, to prevent module loading. + */ ret =3D scs_handle_fde_frame(frame, code_alignment_factor, - fde_use_sdata8, true); + fde_use_sdata8, !is_module); if (ret) return ret; - scs_handle_fde_frame(frame, code_alignment_factor, - fde_use_sdata8, false); + + if (!is_module) + 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..4ccbba24fadc 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 is_module); =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 07:46:34 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 92BDC273810 for ; Fri, 19 Sep 2025 12:23:33 +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=1758284615; cv=none; b=UQNHI95x8y88/oNZToAEF1srK72JuZY8M2jD8aGwMNkTqp1jxr6UZVQApgVEdI4K2uQ+1VpdC+9YNl9wHPCtbjWxyJJ0DycgilFsnPkE+g/YEl8vsTUjCBUG/9NEqylxJaQkVU8exlUVV0HIDbbjLCJmkhT/J5qb7KCyH1q7Gbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758284615; c=relaxed/simple; bh=tAoJj8QUNo3whIuZ6bjZkGG24LElC39BstV8LKgyEC0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EcB1YqT9qFejh8sU6RVoGYyZr4TO0RcEtrA8TI+Nvh9PVBi4a5sNE4mBzXJi3do9w80QVXLIXLiLixrAtoaqN3vmPrc7WlM8hpcVS2eWycDhb0F+Ta94pLMAKRYDtHF5NLd2Tpmstv3ifjfVRccaf3I5wIdxOVuB99+0+V1bkVo= 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=xsI8B52q; 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="xsI8B52q" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3ee12ab7f33so763534f8f.2 for ; Fri, 19 Sep 2025 05:23:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758284612; x=1758889412; 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=qo/jsgLDkKISW5zIp48cnqAjqbEM5L00IhYTKUc6Zus=; b=xsI8B52qfF9P0WpAixog1ksyX7XkDEo9CV4UXq2sRSHaUhe66uH8NevoTXiLw+SaBS 5Ud918uQ+YpPXgWqlkXYvNbB9sKCVoKZ+Ow+93vPO3r8OxE+jqpRIo6A0aZJGVJtspNY xvVyjeiuHa/BSRnNgw0ybWP75WMeQ4Yg1kQPdrt/puwsrA8UYEbh8nxy3XjCc1dFJc6X BdA3wPr6JCyUtMUNauNgLGdg431SuLzcqBuT8LUAM9XZbr8vEnFMjw4zg9ea+cl7w6Va HlGkAbqOm1ZPm7G5y9HDaKXBkiQa5fiIC90B3KC5vAp2Cism66Pr/4gLM6+OgTOFVRFO NpOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758284612; x=1758889412; 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=qo/jsgLDkKISW5zIp48cnqAjqbEM5L00IhYTKUc6Zus=; b=LokrtN4ytzFCiHpJXcqCQWr68kytBZKM1fPE+EaMcFtyobGdaAuEErXs7ilt8T7qrU 6xySe888dqicaArvbkQtrUE5kbfg2uV9y5VAzNNzFGEQKsKL8X/DFmmHIm3Fj0bYOPyU A5L2bXXG5ZMePp7HZDlioOzSenw/56JRHjvYHgMPMPCWX8aGvT7H6YvPnnvy0ESHiGac YCjj9X+pLqW59Ie390JJx27iAZTbXdOtBB0dzuDZ5nGINyVuE4DFt51uyQengasY0kfP 8aBTnfZ9sMRSAYZs8dfdoGZLPM5At+mdrEppahd001/IxkMfo36bpYld2B7USdhNFYF8 r2Ow== X-Forwarded-Encrypted: i=1; AJvYcCXGrPCQHYaTKqBz6o6mer+6OMJCzUj3lLyGDUHKv8yrXBH+QXHLJv8D4WLHf/WgoozA9m14Y7upP++tx1w=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3IW9cd5TmGOAIH3FrKzxhggBM9vsetQjIwA76v62bINZ831l4 y0UnJypnKD+4/LgNylPCFGL2AZDDg9FWI59DcOZHUCcVl+hCN5t8GD7o1btv86rczXV/KJ4Nqx/ hynqsE2ixOw== X-Google-Smtp-Source: AGHT+IGdt2aMsHWTbWqIY3RHkP6ROCYcuLniubso42syhOjvPfHbKaGtZwHfzgKMLU5xJCioFirjKtfTBlCu X-Received: from wmbip15.prod.google.com ([2002:a05:600c:a68f:b0:45d:d39b:53ca]) (user=abarnas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2404:b0:3e7:4697:297a with SMTP id ffacd0b85a97d-3ee7e106157mr2318421f8f.24.1758284611916; Fri, 19 Sep 2025 05:23:31 -0700 (PDT) Date: Fri, 19 Sep 2025 12:23:21 +0000 In-Reply-To: <20250919122321.946462-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: <20250919122321.946462-1-abarnas@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250919122321.946462-3-abarnas@google.com> Subject: [PATCH 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 there is a callback function used by the alternatives specified in the '.altinstruction' ELF section and block loading the module if such a function is present. Reported-by: Fanqin Cui Closes: https://lore.kernel.org/all/20250807072700.348514-1-fanqincui@163.c= om/ Signed-off-by: Adrian Barna=C5=9B --- arch/arm64/include/asm/alternative.h | 4 ++-- arch/arm64/kernel/alternative.c | 15 ++++++++++----- arch/arm64/kernel/module.c | 8 ++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/= alternative.h index 00d97b8a757f..6eab2517c809 100644 --- a/arch/arm64/include/asm/alternative.h +++ b/arch/arm64/include/asm/alternative.h @@ -26,9 +26,9 @@ 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) { } #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..69fae94c843a 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -139,15 +139,18 @@ static noinstr void clean_dcache_range_nopatch(u64 st= art, 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; alternative_cb_t alt_cb; =20 for (alt =3D region->begin; alt < region->end; alt++) { + if (ALT_HAS_CB(alt) && is_module) + return -EPERM; + int nr_inst; int cap =3D ALT_CAP(alt); =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 5d6d228c6156..aeefc50229e3 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -478,8 +478,12 @@ 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