From patchwork Mon Jan 8 20:21:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 760781 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp1061965wrz; Mon, 8 Jan 2024 12:22:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFw0zyh9H3oWtRJglMgVL4S/74P1Rh5EmhLLWFqifw3JAewa8IJLNLXCaXxor3mWarDp+40 X-Received: by 2002:a05:6808:4484:b0:3b9:ce35:dc47 with SMTP id eq4-20020a056808448400b003b9ce35dc47mr5892447oib.88.1704745330535; Mon, 08 Jan 2024 12:22:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704745330; cv=pass; d=google.com; s=arc-20160816; b=zo6lUDXYcGgNDTlwJVbaE2SqFJEsSXRy//BrGHQxsIBPhR2/e9p2A80EXl4yTRhc9E O0QURkgc0UG2MH5/9yxu9DkVLWryCRwPLixuekYP1e/up98GDsQMPrg1GN9wiPaYINcK 0LE9gWvHYanQ2ttbVDWCgMyb742g544yvAYiMhI8yW4EqCV2dPnRz/0ZzEM2LZfU6Yg0 F9tCjl7EsCcKEOfzIujgs54CdDMUreyUZs2q19okN3YpwE6vr2+o5oCBhNoc3MD62jwB HWvqR1kkzsEUqWOHqb78wm220K8EXum1I0lcsdlQyR7s/uxlidxz5hSoiUpfILfbb69r s9Tw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=+NWYmOBnopAKwyeA9J/SSYnszY+cIj9CHambDV9cEas=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=ec1a9ZDkF+kJPJ8LzG6zDpIy1/XouQYnCUVPXQaTxScv+BYpDFVfpEieEudFT0jI2g qA2FXYfDT0ISJ7W5awJBkHFKm4S2tbf2VzIAjuoP3g2fxeuum3AkbuqGoGvF420aiSfB S/7lewkkiWQQ2Yo4eEfeM3u4YuEmZ6X5jJ7He4KvTXFaowxf49cV6LZHwuZxeggHQ7M8 l2KFGX/0SHXLpd/2sNYIh2KAeOy3QEO03jDbbBe2uln54Bvkqp+YNurtVXHPiEdtLyq7 z3/Qippw7bhKhRrN/rIsQEaGUYAK9C8I6zYafTSzcanCEDp71tTN0tWb54GvcdFGaXov Nnaw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ojhYPQeu; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r17-20020a0ce291000000b0067f89b1a4e4si688849qvl.386.2024.01.08.12.22.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 12:22:10 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ojhYPQeu; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28CF938582A6 for ; Mon, 8 Jan 2024 20:22:10 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id D01803858CDB for ; Mon, 8 Jan 2024 20:21:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D01803858CDB 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 D01803858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704745318; cv=none; b=OrR8OCmvNW5fnVPJ0rP/3WeJ1++x3YM4WpRw9ZhyqUtDUqcy9lvqY7077I8vS2MMXks0vpzcHx6QyXKH4a6o6FesNAMzYZvIihf6hI6GP1iBeSSjv+VYqyJnw5+E6aqAoBaeuUxtuAk1+8JTlA2Cmo4yKP9tvJRzy5mtgs00MIg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704745318; c=relaxed/simple; bh=lolUAwhAZ/whvpKzy91vM4H7LfPIOx84pioxeEKrs6M=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=xOHD1dkva0+Y8ZqYsHZlP1hj/eIS+yf4yzMYc3oGhhuy6PwtWA4JWYyCG6Tyuw45BSxW5IyarYGliMzOBG0KhXX23ZG9BNEHkidafBl7VtpNCJ64WMHw+s2uuweaAk5Q9GOAlz3/BYlOTm1vCCCZ1XAV+Evfjxm12R7cpepe1oo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6d9b050e88cso812185b3a.0 for ; Mon, 08 Jan 2024 12:21:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704745314; x=1705350114; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=+NWYmOBnopAKwyeA9J/SSYnszY+cIj9CHambDV9cEas=; b=ojhYPQeuWZtkP7ox02WZ2Q4oGfTZVGigbGjy9jOnhbMcwC/l5Kf9pkRkZ/4SeHSsnr Ds7VU+1xiSD4pzvzCqTmBpu/T4gD7v8vKGrZ9dPTnrPDxL8tpIIcfJCCT3Z0munDoxdm yQ9of54zFaWas/SXYMxLp6lrDAceNcm6PwzkNyTm1mMSCrzlzHlsb+ubpR1XbbebrksU ev+gQK8rSUk8OE/UvKiGMGODyhHS4/10Z81Yc6m0jFjIeBj+PEooHuXz3yJM4+T1Y3LA Mnzk8nEAYA003CTZzKUt5XgvuMQmcX6NCQ7iKQc59xS3w8cdQa8mU3d3WYisNHF6whID Usng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704745314; x=1705350114; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+NWYmOBnopAKwyeA9J/SSYnszY+cIj9CHambDV9cEas=; b=pNtDTwBLJnsRPLGAmin2r8G5aZtMDKrKOELUmce+BAsciM3ZwTIjaT0xMs/Wt23mKg 3iGH+lLWic077el8gQvqso+NToErwITYX3yjRviB4z7WlrhkSy/2SXa2u84PAeyKobWT VpCJ0RoxrwfeIwxtTMgiY7OpZ5UWCOKESj1miH4fnH6MHedqa1n75s9uBdKzwQH9xhbS HEjkas5GByex3jaAVahAN4WGBYhxHfcKaaglRtpERDoLN3J4zf9ofBCDOraSULTuM27Q qOkmGKpqZB2Cwkyvj55OT3yabOoJVUjOw5nvzKCo08xjezDYo2WA2JWAjriMaK8pOd3T LbXQ== X-Gm-Message-State: AOJu0YxV5yzIu/y+k/sinJi0eXm+/ZueO5p1je1Ea3x8h4b8z+Q7K7FO 735cpYvo3dmt2qYXApnN/o9IxN9FLKj5HjySABDC89mYbSc= X-Received: by 2002:aa7:870f:0:b0:6d9:f593:fbcc with SMTP id b15-20020aa7870f000000b006d9f593fbccmr1355537pfo.44.1704745313965; Mon, 08 Jan 2024 12:21:53 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:9dd2:7f25:c108:2fff:5f8e]) by smtp.gmail.com with ESMTPSA id z13-20020a62d10d000000b006d9bdc0f765sm287936pfg.53.2024.01.08.12.21.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 12:21:53 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 00/10] Improve fortify support with clang Date: Mon, 8 Jan 2024 17:21:39 -0300 Message-Id: <20240108202149.335305-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.1 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org 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 #include 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 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 | 57 ++++++----- wcsmbs/bits/wchar2.h | 167 ++++++++++++++++++++++--------- 13 files changed, 746 insertions(+), 149 deletions(-)