1From 5885ed8e6db7648e6842d9811aace7edc4e8aba7 Mon Sep 17 00:00:00 2001
2From: Fabrice Fontaine <fontaine.fabrice@gmail.com>
3Date: Wed, 20 Apr 2022 11:16:52 +0200
4Subject: [PATCH] buildtools/wafsamba: add --disable-stack-protector option
5
6Allow the user to disable stack-protector through
7--disable-stack-protector to avoid the following build failure with
8libtalloc on embedded toolchains which don't support stack-protector:
9
10/home/autobuild/autobuild/instance-5/output-1/host/lib/gcc/i686-buildroot-linux-musl/9.4.0/../../../../i686-buildroot-linux-musl/bin/ld: talloc.c.5.o: in function `_vasprintf_tc':
11talloc.c:(.text+0x427d): undefined reference to `__stack_chk_fail_local'
12
13This build failure is raised since
14https://gitlab.com/ffontaine/samba/-/commit/38e97f8b52e85bdfcf2d74a4fb3c848fa46ba371
15because stack-protector is enabled on libtalloc despite the fact that
16libssp is not available:
17
18Checking if compiler accepts -fstack-protector-strong                                           : yes
19
20Fixes:
21 - http://autobuild.buildroot.org/results/e221bde25c7622db99761d0adcd56663296beb15
22
23Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
24[Upstream status:
25https://gitlab.com/samba-team/samba/-/merge_requests/2493]
26---
27 buildtools/wafsamba/samba_autoconf.py | 49 ++++++++++++++-------------
28 buildtools/wafsamba/wscript           |  3 ++
29 2 files changed, 28 insertions(+), 24 deletions(-)
30
31diff --git a/buildtools/wafsamba/samba_autoconf.py b/buildtools/wafsamba/samba_autoconf.py
32index 78927d85193..23a995e1c34 100644
33--- a/buildtools/wafsamba/samba_autoconf.py
34+++ b/buildtools/wafsamba/samba_autoconf.py
35@@ -703,9 +703,28 @@ def SAMBA_CONFIG_H(conf, path=None):
36     if not IN_LAUNCH_DIR(conf):
37         return
38
39-    # we need to build real code that can't be optimized away to test
40-    stack_protect_list = ['-fstack-protector-strong', '-fstack-protector']
41-    for stack_protect_flag in stack_protect_list:
42+    if not Options.options.disable_stack_protector:
43+        # we need to build real code that can't be optimized away to test
44+        stack_protect_list = ['-fstack-protector-strong', '-fstack-protector']
45+        for stack_protect_flag in stack_protect_list:
46+            flag_supported = conf.check(fragment='''
47+                                        #include <stdio.h>
48+
49+                                        int main(void)
50+                                        {
51+                                            char t[100000];
52+                                            while (fgets(t, sizeof(t), stdin));
53+                                            return 0;
54+                                        }
55+                                        ''',
56+                                        execute=0,
57+                                        cflags=[ '-Werror', '-Wp,-D_FORTIFY_SOURCE=2', stack_protect_flag],
58+                                        mandatory=False,
59+                                        msg='Checking if compiler accepts %s' % (stack_protect_flag))
60+            if flag_supported:
61+                conf.ADD_CFLAGS('%s' % (stack_protect_flag))
62+                break
63+
64         flag_supported = conf.check(fragment='''
65                                     #include <stdio.h>
66
67@@ -717,29 +736,11 @@ def SAMBA_CONFIG_H(conf, path=None):
68                                     }
69                                     ''',
70                                     execute=0,
71-                                    cflags=[ '-Werror', '-Wp,-D_FORTIFY_SOURCE=2', stack_protect_flag],
72+                                    cflags=[ '-Werror', '-fstack-clash-protection'],
73                                     mandatory=False,
74-                                    msg='Checking if compiler accepts %s' % (stack_protect_flag))
75+                                    msg='Checking if compiler accepts -fstack-clash-protection')
76         if flag_supported:
77-            conf.ADD_CFLAGS('%s' % (stack_protect_flag))
78-            break
79-
80-    flag_supported = conf.check(fragment='''
81-                                #include <stdio.h>
82-
83-                                int main(void)
84-                                {
85-                                    char t[100000];
86-                                    while (fgets(t, sizeof(t), stdin));
87-                                    return 0;
88-                                }
89-                                ''',
90-                                execute=0,
91-                                cflags=[ '-Werror', '-fstack-clash-protection'],
92-                                mandatory=False,
93-                                msg='Checking if compiler accepts -fstack-clash-protection')
94-    if flag_supported:
95-        conf.ADD_CFLAGS('-fstack-clash-protection')
96+            conf.ADD_CFLAGS('-fstack-clash-protection')
97
98     if Options.options.debug:
99         conf.ADD_CFLAGS('-g', testflags=True)
100diff --git a/buildtools/wafsamba/wscript b/buildtools/wafsamba/wscript
101index 8729b0829da..d75bb3b1c0c 100644
102--- a/buildtools/wafsamba/wscript
103+++ b/buildtools/wafsamba/wscript
104@@ -165,6 +165,9 @@ Currently the only tested value is 'smbtorture,smbd/smbd' for Samba'''),
105     gr.add_option('--disable-warnings-as-errors',
106                    help=("Do not treat all warnings as errors (disable -Werror)"),
107                    action="store_true", dest='disable_warnings_as_errors', default=False)
108+    gr.add_option('--disable-stack-protector',
109+                   help=("Disable stack-protector"),
110+                   action="store_true", dest='disable_stack_protector', default=False)
111     opt.add_option('--enable-coverage',
112                    help=("enable options necessary for code coverage "
113                          "reporting on selftest (default=no)"),
114--
1152.35.1
116
117