diff mbox series

[4/5] build: initialize stack variables to zero by default

Message ID 173689081941.3476119.6143322419702468919.stgit@frogsfrogsfrogs (mailing list archive)
State New
Headers show
Series [1/5] xfs_db: improve error message when unknown btree type given to btheight | expand

Commit Message

Darrick J. Wong Jan. 14, 2025, 9:41 p.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

Newer versions of gcc and clang can include the ability to zero stack
variables by default.  Let's enable it so that we (a) reduce the risk of
writing stack contents to disk somewhere and (b) try to reduce
unpredictable program behavior based on random stack contents.  The
kernel added this 6 years ago, so I think it's mature enough for
xfsprogs.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
 configure.ac            |    1 +
 include/builddefs.in    |    2 +-
 m4/package_sanitizer.m4 |   14 ++++++++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)

Comments

Christoph Hellwig Jan. 15, 2025, 5:21 a.m. UTC | #1
On Tue, Jan 14, 2025 at 01:41:25PM -0800, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
> 
> Newer versions of gcc and clang can include the ability to zero stack
> variables by default.  Let's enable it so that we (a) reduce the risk of
> writing stack contents to disk somewhere and (b) try to reduce
> unpredictable program behavior based on random stack contents.  The
> kernel added this 6 years ago, so I think it's mature enough for
> xfsprogs.

Hmm, this tends to paper of bugs quite badly.  But I guess we'd better
paper over bugs in the same way as the kernel code.

Reluctantly-Reviewed-by: Christoph Hellwig <hch@lst.de>
Darrick J. Wong Jan. 15, 2025, 5:26 a.m. UTC | #2
On Wed, Jan 15, 2025 at 06:21:55AM +0100, Christoph Hellwig wrote:
> On Tue, Jan 14, 2025 at 01:41:25PM -0800, Darrick J. Wong wrote:
> > From: Darrick J. Wong <djwong@kernel.org>
> > 
> > Newer versions of gcc and clang can include the ability to zero stack
> > variables by default.  Let's enable it so that we (a) reduce the risk of
> > writing stack contents to disk somewhere and (b) try to reduce
> > unpredictable program behavior based on random stack contents.  The
> > kernel added this 6 years ago, so I think it's mature enough for
> > xfsprogs.
> 
> Hmm, this tends to paper of bugs quite badly.  But I guess we'd better
> paper over bugs in the same way as the kernel code.

Yeah, I've been thinking about this one for a couple of weeks -- yeah,
it does paper over uninit variable bugs, but since the kernel does it
we had better do that too if we want to keep porting random kernel code
to support libxfs. :/

> Reluctantly-Reviewed-by: Christoph Hellwig <hch@lst.de>

Thanks for reviewing!

--D
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index 224d1d3930bf2f..90ef7925a925d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -177,6 +177,7 @@  AC_CONFIG_SYSTEMD_SYSTEM_UNIT_DIR
 AC_CONFIG_CROND_DIR
 AC_CONFIG_UDEV_RULE_DIR
 AC_HAVE_BLKID_TOPO
+AC_HAVE_TRIVIAL_AUTO_VAR_INIT
 
 if test "$enable_ubsan" = "yes" || test "$enable_ubsan" = "probe"; then
         AC_PACKAGE_CHECK_UBSAN
diff --git a/include/builddefs.in b/include/builddefs.in
index ac43b6412c8cbb..82840ec7fd3adb 100644
--- a/include/builddefs.in
+++ b/include/builddefs.in
@@ -146,7 +146,7 @@  ifeq ($(HAVE_LIBURCU_ATOMIC64),yes)
 PCFLAGS += -DHAVE_LIBURCU_ATOMIC64
 endif
 
-SANITIZER_CFLAGS += @addrsan_cflags@ @threadsan_cflags@ @ubsan_cflags@
+SANITIZER_CFLAGS += @addrsan_cflags@ @threadsan_cflags@ @ubsan_cflags@ @autovar_init_cflags@
 SANITIZER_LDFLAGS += @addrsan_ldflags@ @threadsan_ldflags@ @ubsan_ldflags@
 
 # Use special ar/ranlib wrappers if we have lto
diff --git a/m4/package_sanitizer.m4 b/m4/package_sanitizer.m4
index 41b729906a27ba..6488f7ebce2f50 100644
--- a/m4/package_sanitizer.m4
+++ b/m4/package_sanitizer.m4
@@ -57,3 +57,17 @@  AC_DEFUN([AC_PACKAGE_CHECK_THREADSAN],
     AC_SUBST(threadsan_cflags)
     AC_SUBST(threadsan_ldflags)
   ])
+
+# Check if we have -ftrivial-auto-var-init=zero
+AC_DEFUN([AC_HAVE_TRIVIAL_AUTO_VAR_INIT],
+  [ AC_MSG_CHECKING([if C compiler supports zeroing automatic vars])
+    OLD_CFLAGS="$CFLAGS"
+    TEST_CFLAGS="-ftrivial-auto-var-init=zero"
+    CFLAGS="$CFLAGS $TEST_CFLAGS"
+    AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
+        [AC_MSG_RESULT([yes])]
+        [autovar_init_cflags=$TEST_CFLAGS],
+        [AC_MSG_RESULT([no])])
+    CFLAGS="${OLD_CFLAGS}"
+    AC_SUBST(autovar_init_cflags)
+  ])