Message ID | 20240208184622.332678-1-adhemerval.zanella@linaro.org |
---|---|
Headers |
Return-Path: <libc-alpha-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE2CA3858439 for <patchwork@sourceware.org>; Thu, 8 Feb 2024 18:47:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id EE2F63858C60 for <libc-alpha@sourceware.org>; Thu, 8 Feb 2024 18:46:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE2F63858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EE2F63858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707417992; cv=none; b=FuybXkcxvxUOopFTBaev1VqGlCuLt9GddozcvpG5NecNYkB7VKU7/Gne0om5iXIFSV6P904tjICMweVxJfsrQZ8M2uum0CSVrMnJkD9rSgiqo1fcATPmQCiDAyjwXYYtW7RgA8+uCESDkC8/bqL/D5uZ2FgvJGSPuO37ALCHrto= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707417992; c=relaxed/simple; bh=Jo+1Hq6ZKTupXZpKUVPJjkoUq2U22JHJE7JXWIX6Zac=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ZIVMrZsIke4gRpKgKODOhT8IPuBB2qhwj6PYxR4S9Tl+YDTDtVOv5QoEmvYY4N0D3gKrLcW+/PRkTdse/GD9xmVASbWRKanQGNLjbqudMmpaZnQcwHq5+PV0UU9O4DZwqEt86Zc4QvcHUwIhNzf6OI31avx1C61Ik2VJ+7nbXoM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6e12d0af927so85249a34.0 for <libc-alpha@sourceware.org>; Thu, 08 Feb 2024 10:46:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1707417987; x=1708022787; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=u3xA9kjkIYBnU5L2SJcsy0IdeKLDV5P8qLbIYrsOpb4=; b=shM4JkujlSdYndo8RTi9FMNoj+V5wd6aD81Y/10GyIGWpDJ3OJKXIauqOIksBx1qLx 3yBspfmgbW9y1bKzgbuiUKVJnr4vhPVf5je47/hJZnqY4xRXf5NgiJ09geEKc67w2oUM J3RBehpGKV5KYVqq/g3e0blJvBSRxoACPjpOPp5njeUnJT72W1MIk8Yn37LFzvdCcBby lXFbcdJto8jkS3d7UoBYaKzNQDu5yKYyseq9zsd67PQL/6F/W33CXJfwBHjAiC6OMBka ecSCPntidg8SHxpjodOJDBiL68GmjR36rsmK75RFunf3Pk/WYiGrhwCvXKEZjHa2Pcnh lpgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707417987; x=1708022787; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=u3xA9kjkIYBnU5L2SJcsy0IdeKLDV5P8qLbIYrsOpb4=; b=h0Y/QlUGif4CqnK7npBxTOQDrmCE7Y94ORtiq/giYmBA9x3ZgcTEHZJSkRHAtrPJty mtrzfWNbQwLskicpTT3Mio8hOpvMsQoCerdpOEUOHttzYxwnsK7abhQgpQRKQRCFBHH0 +vFf+/9WNR2OLdjKO+Hmcp41sC3+go7vHUoKRSr5BKKz/TIWnMHP48EP2BHLhxwTrUl1 1vad+F5Lo+WEGy9OW017DWvtwB/UsCVpCnJIB2F57962vUElEWBqOnNDqwzgFn2tijxy Hn4dP0q3oafWNHj0ayLCiA2Rid9iQiKLzCnk/jodAIXEYlcS7T9N0wvdk6Q+hTfiDr7k fOgg== X-Gm-Message-State: AOJu0YzZzhl7O1b/rNdhK9j0YOipDxHXZFPKrejat9wiad4Fx10fSQzE pFHzKVuMjaBP7Dbuf9qDuEs5ZNc0wvLgy5DJ4aox2MAMWbFnGGhD2yVn9keYFJwGgtdjJqOUGmk V X-Google-Smtp-Source: AGHT+IG0bbRo7+h1nvILksDycbvrfhYvDnlc3u+a3dpxLssE/JGQJ7sutM9QY8NyZIMkCR4xLfW8sA== X-Received: by 2002:a05:6830:4d:b0:6e1:2e98:e5d9 with SMTP id d13-20020a056830004d00b006e12e98e5d9mr373328otp.18.1707417987340; Thu, 08 Feb 2024 10:46:27 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:378:6793:1dc3:1346:d6d6]) by smtp.gmail.com with ESMTPSA id n26-20020a638f1a000000b005d7994a08dcsm156408pgd.36.2024.02.08.10.46.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 10:46:26 -0800 (PST) From: Adhemerval Zanella <adhemerval.zanella@linaro.org> To: libc-alpha@sourceware.org Cc: Siddhesh Poyarekar <siddhesh@gotplt.org> Subject: [PATCH v3 00/10] Improve fortify support with clang Date: Thu, 8 Feb 2024 15:46:12 -0300 Message-Id: <20240208184622.332678-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list <libc-alpha.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/libc-alpha/> List-Post: <mailto:libc-alpha@sourceware.org> List-Help: <mailto:libc-alpha-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=subscribe> Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org |
Series |
Improve fortify support with clang
|
|
Message
Adhemerval Zanella Netto
Feb. 8, 2024, 6:46 p.m. UTC
When using clang, the fortify wrappers show less coverage on both compile and runtime. For instance, a snippet from tst-fortify.c: $ cat t.c #include <stdio.h> #include <string.h> const char *str1 = "JIHGFEDCBA"; int main (int argc, char *argv[]) { #define O 0 struct A { char buf1[9]; char buf2[1]; } a; strcpy (a.buf1 + (O + 4), str1 + 5); return 0; } $ gcc -O2 t.c -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=2 -o t $ ./t *** buffer overflow detected ***: terminated Aborted (core dumped) $ clang -O2 t.c -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=2 -o t $ ./t $ Although clang does support __builtin___strcpy_chk (which correctly lowers to __strcpy_chk), the __builtin_object_size passed as third the argument is not fully correct and thus limits the possible runtime checks. This limitation was already being raised some years ago [1], but the work has been staled. However, a similar patch has been used for ChromeOS for some time [2]. Bionic libc also uses a similar approach to enable fortified wrappers. Improve its support with clang, requires defining the fortified wrapper differently. For instance, the read wrapper is currently expanded as: extern __inline __attribute__((__always_inline__)) __attribute__((__artificial__)) __attribute__((__warn_unused_result__)) ssize_t read (int __fd, void *__buf, size_t __nbytes) { return __glibc_safe_or_unknown_len (__nbytes, sizeof (char), __glibc_objsize0 (__buf)) ? __read_alias (__fd, __buf, __nbytes) : __glibc_unsafe_len (__nbytes, sizeof (char), __glibc_objsize0 (__buf)) ? __read_chk_warn (__fd, __buf, __nbytes, __builtin_object_size (__buf, 0)) : __read_chk (__fd, __buf, __nbytes, __builtin_object_size (__buf, 0)); } The wrapper relies on __builtin_object_size call lowers to a constant at Compile time and many other operations in the wrapper depend on having a single, known value for parameters. Because this is impossible to have for function parameters, the wrapper depends heavily on inlining to work and While this is an entirely viable approach on GCC is not fully reliable on clang. This is because by the time llvm gets to inlining and optimizing, there is a minimal reliable source and type-level information available (more information on a more deep explanation on how to fortify wrapper works on clang [4]). To allow the wrapper to work reliably and with the same functionality as with GCC, clang requires a different approach: * __attribute__((diagnose_if(c, “str”, “warning”))) which is a * function level attribute; if the compiler can determine that 'c' is true at compile-time, it will emit a warning with the text 'str1'. If it would be better to emit an error, the wrapper can use "error" instead of "warning". * __attribute__((overloadable)) which is also a function-level attribute; and it allows C++-style overloading to occur on C functions. * __attribute__((pass_object_size(n))) which is a parameter-level attribute; and it makes the compiler evaluate __builtin_object_size(param, n) at each call site of the function that has the parameter and passes it in as a hidden parameter. This attribute has two side effects that are key to how FORTIFY works: 1. It can overload solely on pass_object_size (e.g. there are two overloads of foo in void foo(char * __attribute__((pass_object_size(0))) c); void foo(char *); (The one with pass_object_size attribute has preceded the default one). 2. A function with at least one pass_object_size parameter can never have its address taken (and overload resolution respects this). Thus the read wrapper can be implemented as follows, without hindering any fortify coverage compile and runtime: Thus the read wrapper can be implemented as follows, without hindering any fortify coverage compile and runtime: extern __inline __attribute__((__always_inline__)) __attribute__((__artificial__)) __attribute__((__overloadable__)) __attribute__((__warn_unused_result__)) ssize_t read (int __fd, void *const __attribute__((pass_object_size (0))) __buf, size_t __nbytes) __attribute__((__diagnose_if__ ((((__builtin_object_size (__buf, 0)) != -1ULL && (__nbytes) > (__builtin_object_size (__buf, 0)) / (1))), "read called with bigger length than the size of the destination buffer", "warning"))) { return (__builtin_object_size (__buf, 0) == (size_t) -1) ? __read_alias (__fd, __buf, __nbytes) : __read_chk (__fd, __buf, __nbytes, __builtin_object_size (__buf, 0)); } To avoid changing the current semantic for GCC, a set of macros is defined to enable the clang required attributes, along with some changes on internal macros to avoid the need to issue the symbol_chk symbols (which are done through the __diagnose_if__ attribute for clang). The read wrapper can be simplified as: __fortify_function __attribute_overloadable__ __wur ssize_t read (int __fd, __fortify_clang_overload_arg0 (void *, ,__buf), size_t __nbytes) __fortify_clang_warning_only_if_bos0_lt (__nbytes, __buf, "read called with bigger length than " "size of the destination buffer") { return __glibc_fortify (read, __nbytes, sizeof (char), __glibc_objsize0 (__buf), __fd, __buf, __nbytes); } There is no expected semantic or code change when using GCC. Also, clang does not support __va_arg_pack, so variadic functions are expanded to call va_arg implementations. The error function must not have bodies (address takes are expanded to nonfortified calls), and with the __fortify_function compiler might still create a body with the C++ mangling name (due to the overload attribute). In this case, the function is defined with __fortify_function_error_function macro instead. To fully test it, I used my clang branch [4] which allowed me to fully build all fortify tests with clang. With this patchset, there is no regressions anymore. [1] https://sourceware.org/legacy-ml/libc-alpha/2017-09/msg00434.html [2] https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay/+/refs/heads/main/sys-libs/glibc/files/local/glibc-2.35/0006-glibc-add-clang-style-FORTIFY.patch [3] https://docs.google.com/document/d/1DFfZDICTbL7RqS74wJVIJ-YnjQOj1SaoqfhbgddFYSM/edit [4] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/clang Changes from v2: - Fixed open/open64/openat/openat64 three argument variant overload. Changes from v1: - Use implementation-namespace identifiers pass_object_size and pass_dynamic_object_size. - Simplify the clang macros and enable it iff for clang 5.0 (that supports __diagnose_if__). Adhemerval Zanella (10): cdefs.h: Add clang fortify directives libio: Improve fortify with clang string: Improve fortify with clang stdlib: Improve fortify with clang unistd: Improve fortify with clang socket: Improve fortify with clang syslog: Improve fortify with clang wcsmbs: Improve fortify with clang debug: Improve fcntl.h fortify warnings with clang debug: Improve mqueue.h fortify warnings with clang io/bits/fcntl2.h | 92 ++++++++++++++++++ io/bits/poll2.h | 29 ++++-- io/fcntl.h | 3 +- libio/bits/stdio2.h | 173 +++++++++++++++++++++++++++++---- misc/bits/syslog.h | 14 ++- misc/sys/cdefs.h | 158 +++++++++++++++++++++++++++++- posix/bits/unistd.h | 110 +++++++++++++++------ rt/bits/mqueue2.h | 29 ++++++ rt/mqueue.h | 3 +- socket/bits/socket2.h | 20 +++- stdlib/bits/stdlib.h | 40 +++++--- string/bits/string_fortified.h | 56 ++++++----- wcsmbs/bits/wchar2.h | 167 ++++++++++++++++++++++--------- 13 files changed, 745 insertions(+), 149 deletions(-)
Comments
Ping. On 08/02/24 15:46, Adhemerval Zanella wrote: > When using clang, the fortify wrappers show less coverage on both > compile and runtime. For instance, a snippet from tst-fortify.c: > > $ cat t.c > #include <stdio.h> > #include <string.h> > > const char *str1 = "JIHGFEDCBA"; > > int main (int argc, char *argv[]) > { > #define O 0 > struct A { char buf1[9]; char buf2[1]; } a; > strcpy (a.buf1 + (O + 4), str1 + 5); > > return 0; > } > $ gcc -O2 t.c -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=2 -o t > $ ./t > *** buffer overflow detected ***: terminated > Aborted (core dumped) > $ clang -O2 t.c -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=2 -o t > $ ./t > $ > > Although clang does support __builtin___strcpy_chk (which correctly > lowers to __strcpy_chk), the __builtin_object_size passed as third > the argument is not fully correct and thus limits the possible runtime > checks. > > This limitation was already being raised some years ago [1], but the > work has been staled. However, a similar patch has been used for > ChromeOS for some time [2]. Bionic libc also uses a similar approach to > enable fortified wrappers. > > Improve its support with clang, requires defining the fortified wrapper > differently. For instance, the read wrapper is currently expanded as: > > extern __inline > __attribute__((__always_inline__)) > __attribute__((__artificial__)) > __attribute__((__warn_unused_result__)) > ssize_t read (int __fd, void *__buf, size_t __nbytes) > { > return __glibc_safe_or_unknown_len (__nbytes, > sizeof (char), > __glibc_objsize0 (__buf)) > ? __read_alias (__fd, __buf, __nbytes) > : __glibc_unsafe_len (__nbytes, > sizeof (char), > __glibc_objsize0 (__buf)) > ? __read_chk_warn (__fd, > __buf, > __nbytes, > __builtin_object_size (__buf, 0)) > : __read_chk (__fd, > __buf, > __nbytes, > __builtin_object_size (__buf, 0)); > } > > The wrapper relies on __builtin_object_size call lowers to a constant at > Compile time and many other operations in the wrapper depend on > having a single, known value for parameters. Because this is > impossible to have for function parameters, the wrapper depends heavily > on inlining to work and While this is an entirely viable approach on > GCC is not fully reliable on clang. This is because by the time llvm > gets to inlining and optimizing, there is a minimal reliable source and > type-level information available (more information on a more deep > explanation on how to fortify wrapper works on clang [4]). > > To allow the wrapper to work reliably and with the same functionality as > with GCC, clang requires a different approach: > > * __attribute__((diagnose_if(c, “str”, “warning”))) which is a > * function > level attribute; if the compiler can determine that 'c' is true at > compile-time, it will emit a warning with the text 'str1'. If it > would be better to emit an error, the wrapper can use "error" > instead of "warning". > > * __attribute__((overloadable)) which is also a function-level > attribute; and it allows C++-style overloading to occur on C > functions. > > * __attribute__((pass_object_size(n))) which is a parameter-level > attribute; and it makes the compiler evaluate > __builtin_object_size(param, n) at each call site of the function > that has the parameter and passes it in as a hidden parameter. > > This attribute has two side effects that are key to how FORTIFY > works: > > 1. It can overload solely on pass_object_size (e.g. there are two > overloads of foo in > > void foo(char * __attribute__((pass_object_size(0))) c); > void foo(char *); > > (The one with pass_object_size attribute has preceded the > default one). > > 2. A function with at least one pass_object_size parameter can never > have its address taken (and overload resolution respects this). > > Thus the read wrapper can be implemented as follows, without > hindering any fortify coverage compile and runtime: > > Thus the read wrapper can be implemented as follows, without > hindering any fortify coverage compile and runtime: > > extern __inline > __attribute__((__always_inline__)) > __attribute__((__artificial__)) > __attribute__((__overloadable__)) > __attribute__((__warn_unused_result__)) > ssize_t read (int __fd, > void *const __attribute__((pass_object_size (0))) __buf, > size_t __nbytes) > __attribute__((__diagnose_if__ ((((__builtin_object_size (__buf, > 0)) != -1ULL > && (__nbytes) > > (__builtin_object_size (__buf, 0)) / (1))), > "read called with bigger length > than the size of the destination buffer", > "warning"))) > { > return (__builtin_object_size (__buf, 0) == (size_t) -1) > ? __read_alias (__fd, > __buf, > __nbytes) > : __read_chk (__fd, > __buf, > __nbytes, > __builtin_object_size (__buf, 0)); > } > > To avoid changing the current semantic for GCC, a set of macros is > defined to enable the clang required attributes, along with some changes > on internal macros to avoid the need to issue the symbol_chk symbols > (which are done through the __diagnose_if__ attribute for clang). > The read wrapper can be simplified as: > > __fortify_function __attribute_overloadable__ __wur > ssize_t read (int __fd, > __fortify_clang_overload_arg0 (void *, ,__buf), > size_t __nbytes) > __fortify_clang_warning_only_if_bos0_lt (__nbytes, __buf, > "read called with bigger > length than " > "size of the destination > buffer") > > { > return __glibc_fortify (read, __nbytes, sizeof (char), > __glibc_objsize0 (__buf), > __fd, __buf, __nbytes); > } > > There is no expected semantic or code change when using GCC. > > Also, clang does not support __va_arg_pack, so variadic functions are > expanded to call va_arg implementations. The error function must not > have bodies (address takes are expanded to nonfortified calls), and > with the __fortify_function compiler might still create a body with the > C++ mangling name (due to the overload attribute). In this case, the > function is defined with __fortify_function_error_function macro > instead. > > To fully test it, I used my clang branch [4] which allowed me to fully > build all fortify tests with clang. With this patchset, there is no > regressions anymore. > > [1] https://sourceware.org/legacy-ml/libc-alpha/2017-09/msg00434.html > [2] https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay/+/refs/heads/main/sys-libs/glibc/files/local/glibc-2.35/0006-glibc-add-clang-style-FORTIFY.patch > [3] https://docs.google.com/document/d/1DFfZDICTbL7RqS74wJVIJ-YnjQOj1SaoqfhbgddFYSM/edit > [4] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/clang > > Changes from v2: > - Fixed open/open64/openat/openat64 three argument variant overload. > > Changes from v1: > - Use implementation-namespace identifiers pass_object_size and > pass_dynamic_object_size. > - Simplify the clang macros and enable it iff for clang 5.0 > (that supports __diagnose_if__). > > Adhemerval Zanella (10): > cdefs.h: Add clang fortify directives > libio: Improve fortify with clang > string: Improve fortify with clang > stdlib: Improve fortify with clang > unistd: Improve fortify with clang > socket: Improve fortify with clang > syslog: Improve fortify with clang > wcsmbs: Improve fortify with clang > debug: Improve fcntl.h fortify warnings with clang > debug: Improve mqueue.h fortify warnings with clang > > io/bits/fcntl2.h | 92 ++++++++++++++++++ > io/bits/poll2.h | 29 ++++-- > io/fcntl.h | 3 +- > libio/bits/stdio2.h | 173 +++++++++++++++++++++++++++++---- > misc/bits/syslog.h | 14 ++- > misc/sys/cdefs.h | 158 +++++++++++++++++++++++++++++- > posix/bits/unistd.h | 110 +++++++++++++++------ > rt/bits/mqueue2.h | 29 ++++++ > rt/mqueue.h | 3 +- > socket/bits/socket2.h | 20 +++- > stdlib/bits/stdlib.h | 40 +++++--- > string/bits/string_fortified.h | 56 ++++++----- > wcsmbs/bits/wchar2.h | 167 ++++++++++++++++++++++--------- > 13 files changed, 745 insertions(+), 149 deletions(-) >