Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp1746519rwb; Sun, 6 Aug 2023 01:40:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+hkdpHuUjNQlaYpanEvVJfEROnpoq3oJA4U4L9j2Y+KPTtodoO+9MPJJcyAYx0tUA+EoH X-Received: by 2002:aa7:d493:0:b0:523:3751:1691 with SMTP id b19-20020aa7d493000000b0052337511691mr50868edr.11.1691311246496; Sun, 06 Aug 2023 01:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691311246; cv=none; d=google.com; s=arc-20160816; b=J7s8ujGSDfF1fOURZf5/m/dhdCYcLEsU/AE6KLHMcfXT5xtcL4vdvYuy6cCup8HxnR NzIKm5IHaQbB0c1EesumEmX61yzf4CaUdkLNzKTJfw8j65sRsW7TS3Ew7z8qy+voDy88 PjgmwkB7fROOAEp8Ry0wF9xKYmmOAJEnEqMVFvIGTUDFu1qwH2cv8wLU4f5mYHbtzPeS 7c/unXBT9Mvsaow73lsPhYYO8fqepW2v2OECAyufgrYi/kWooVcOBezMuQCDs1oVeyXn qjLz5wn70m15y8ImTs5WN4Gr3uPapM+KqCCcDDTRCEO2G97uAixjOhbtjCO1L0mGcJ3g iitQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=f7yvcz9DRaFTWWwuXOYGuV40bD3blixvS7HKQArMzmI=; fh=gn9MSGgVMLLrYS41ELheX3gDILgoBnwaj5DSpUF/ufM=; b=ars3xY/V10q0PP+zi2FtJuNfvfdqR/4YEPoBPvGX6/a1oXpxfYv6AkhYwyk/1DCXe7 o46sl2C7K7Fcz+lJqNKYIW9Wp0X0N21yMoZCLGvqhcCWGc9OheyjtWZdYRAC0m+URevZ hFPU6WgIPDKME8SJTd3RnDQhJKatXQj8sEGNkHc4QTh0wYjpmh4TLsayUKlmGBheWKhj KEnW8N67sfabESttXYFgbEINey6l/kPrR388r6Y02+jAbXj5aArJlYJ93lM7SH0MkqVW NHh9LM8id6QFfGfBgOdI4iS1/JEiwcIOc7xRvFlOmdKmwWq8+Xa9MPtknbRQgGlEILq2 cs7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen0n.name header.s=mail header.b=aORIwgZc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020aa7dcc6000000b005231008477bsi4251020edu.592.2023.08.06.01.40.22; Sun, 06 Aug 2023 01:40:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@xen0n.name header.s=mail header.b=aORIwgZc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229722AbjHFIal (ORCPT + 99 others); Sun, 6 Aug 2023 04:30:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjHFIaj (ORCPT ); Sun, 6 Aug 2023 04:30:39 -0400 Received: from mailbox.box.xen0n.name (mail.xen0n.name [115.28.160.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8587131 for ; Sun, 6 Aug 2023 01:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xen0n.name; s=mail; t=1691310632; bh=1PmgJ9UnGd7n2iOLnmG8C4ChzRpzduUPBOOmTK9zmH4=; h=From:To:Cc:Subject:Date:From; b=aORIwgZcAgfREdgO3i/MAXyip30WQh7Ju+cHHDGE3LrsfKMzunSQgNuaiZli7qyJz MzQ2quDLiPQ8iyhlS/TvrVrN1ZFjWKV/xQ33g60df/0PqiASyweEhLQIVgSrivBs1d 5dJFPyZqPNbVnohNfinR6eyaiWFrAyQLfk6g3/T4= Received: from ld50.lan (unknown [101.88.28.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mailbox.box.xen0n.name (Postfix) with ESMTPSA id C02B2600F8; Sun, 6 Aug 2023 16:30:31 +0800 (CST) From: WANG Xuerui To: Huacai Chen Cc: Nathan Chancellor , Xi Ruoyao , loongarch@lists.linux.dev, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, WANG Xuerui , Nick Desaulniers Subject: [PATCH v2] LoongArch: Replace -ffreestanding with finer-grained -fno-builtin's Date: Sun, 6 Aug 2023 16:30:21 +0800 Message-Id: <20230806083021.2243574-1-kernel@xen0n.name> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: WANG Xuerui As explained by Nick in the original issue: the kernel usually does a good job of providing library helpers that have similar semantics as their ordinary userspace libc equivalents, but -ffreestanding disables such libcall optimization and other related features in the compiler, which can lead to unexpected things such as CONFIG_FORTIFY_SOURCE not working (!). As it turns out to be the case, only the memory operations really need to be prevented from expansion by the compiler, and this is doable with finer-grained -fno-builtin-* toggles. So only disable memcpy, memmove and memset, while leaving other builtins enabled, to fix source fortification among others. Closes: https://github.com/ClangBuiltLinux/linux/issues/1897 Reported-by: Nathan Chancellor Suggested-by: Nick Desaulniers Signed-off-by: WANG Xuerui --- Changes in v2: - Keep the memory operation builtins disabled, add comments, and tweak the commit message along the way. arch/loongarch/Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile index b1e5db51b61c..34fc48df87f2 100644 --- a/arch/loongarch/Makefile +++ b/arch/loongarch/Makefile @@ -83,7 +83,14 @@ KBUILD_CFLAGS_KERNEL += -fPIE LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext endif -cflags-y += -ffreestanding +# Make sure the memory libcalls are not expanded by the compiler, for better +# control over unaligned accesses with respect to CONFIG_ARCH_STRICT_ALIGN, +# and also for avoiding https://gcc.gnu.org/PR109465. +# +# The overly broad -ffreestanding is undesirable as it disables *all* libcall +# handling, that unfortunately includes proper FORTIFY_SOURCE instrumentation. +cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset + cflags-y += $(call cc-option, -mno-check-zero-division) load-y = 0x9000000000200000 -- 2.40.0