Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6568685ybl; Wed, 15 Jan 2020 06:48:26 -0800 (PST) X-Google-Smtp-Source: APXvYqx9cWYdEoHQGOZTjOlVEGCdEtkYUS4hyHR5dvPYjUX20xKcUfEXgAMhjJOLH9s7vqq/8uWA X-Received: by 2002:a9d:7342:: with SMTP id l2mr2967173otk.98.1579099705922; Wed, 15 Jan 2020 06:48:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579099705; cv=none; d=google.com; s=arc-20160816; b=LELEKc25uADkHch4Zm7rVQVw1E3nrGVmQ5rSNUZDa03Y2uJRRZiG0SKXFtRNbIJ6E7 odvXBta6Bsqacaga6CZUPHjvbVknlY89tr9AeouNArOut7yPUPySuhVC50fH7u5Wbr4C fZuXem0JFta2jNdzZFaxCxgBXNcv7r7MPFziujZcrTdrpB4fqeZgRyEq0tjPR5RZd5Mi FjpDE1F2T0TeB4IJZt2RR0gPmmScpFsKuL4hlAQyQjs4octoPmxTpqZUL8iJmgr3Lxey 7Sd3Y3IEq6KlwsdV2i0hmw67LHQ0mt9439/c1PtuJLR+rAvyyl6hNVZFD0pC+d0WVqGG g4QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=GvjzXQnIL+hCqDUkI7JAeG4AEubHEzQxfXAYFm1+LnE=; b=Eiuh9b6g065vEl3DSU5bNSgAIRP5g7zehDnp4brHk6x7rkVJC5A1EzR7ZB2Z+PEeuK Hbaq1UgqBuyLEvVuIfT/IoWZUR5TdZkgWaxNVf6wU8z8cBcCSVRk7OYvHz0RoaCVj7ul 5M9fFuaeYSz3g1PQtQX6qEiTxYsrb/+BPYFAUevCllKgguc4Ffc2B9ByxmFbsNxqgHUJ FRKEPmMZAy+/arRC19LBCanPdM8WXqRl98Sgy2bNzZEkIhZJ7kSWbbcix7ZkuUiYH4N1 +9lKo65AJMcCPnEMVISn3gWYhygrP24If+M1tE8OEVX0onUb0kXoqR9Ag7XWvcX28NMI gFTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=v0ZdT0Jh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v26si11129853otj.0.2020.01.15.06.48.13; Wed, 15 Jan 2020 06:48:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=v0ZdT0Jh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729022AbgAOOrN (ORCPT + 99 others); Wed, 15 Jan 2020 09:47:13 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:59870 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726071AbgAOOrM (ORCPT ); Wed, 15 Jan 2020 09:47:12 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 47yVXY1sjBz9tylP; Wed, 15 Jan 2020 15:47:09 +0100 (CET) Authentication-Results: localhost; dkim=pass reason="1024-bit key; insecure key" header.d=c-s.fr header.i=@c-s.fr header.b=v0ZdT0Jh; dkim-adsp=pass; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id 1yEnk5s5C33f; Wed, 15 Jan 2020 15:47:09 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 47yVXY0mmhz9tylH; Wed, 15 Jan 2020 15:47:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1579099629; bh=GvjzXQnIL+hCqDUkI7JAeG4AEubHEzQxfXAYFm1+LnE=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=v0ZdT0JhTBUed9+RMH8/zgRWmVMtklpKF4OyD4fUhEXwH0bgTT7veCONXVm+nrzp0 OcRyq7vswits+6oi5xj+KxaIPN6CqvBJdySyxX2DpkqRfwvxMwmINAXHKUrtY9rDoF Ov5rL9rqArrPIuAwM0/8yTdbKGn9JuCm+oKttX5s= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 6E61F8B774; Wed, 15 Jan 2020 15:47:10 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id BnOhqHOrMIva; Wed, 15 Jan 2020 15:47:10 +0100 (CET) Received: from [172.25.230.100] (po15451.idsi0.si.c-s.fr [172.25.230.100]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 15D4B8B805; Wed, 15 Jan 2020 15:47:10 +0100 (CET) Subject: Re: [PATCH 1/2] kasan: stop tests being eliminated as dead code with FORTIFY_SOURCE To: Dmitry Vyukov , Daniel Axtens Cc: linux-s390 , linux-xtensa@linux-xtensa.org, the arch/x86 maintainers , LKML , kasan-dev , Linux-MM , Daniel Micay , Alexander Potapenko , Andrey Ryabinin , linuxppc-dev , Linux ARM References: <20200115063710.15796-1-dja@axtens.net> <20200115063710.15796-2-dja@axtens.net> From: Christophe Leroy Message-ID: <917cc571-a25c-3d3e-547c-c537149834d6@c-s.fr> Date: Wed, 15 Jan 2020 15:47:09 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: fr Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 15/01/2020 à 15:43, Dmitry Vyukov a écrit : > On Wed, Jan 15, 2020 at 7:37 AM Daniel Axtens wrote: >> >> 3 KASAN self-tests fail on a kernel with both KASAN and FORTIFY_SOURCE: >> memchr, memcmp and strlen. >> >> When FORTIFY_SOURCE is on, a number of functions are replaced with >> fortified versions, which attempt to check the sizes of the operands. >> However, these functions often directly invoke __builtin_foo() once they >> have performed the fortify check. The compiler can detect that the results >> of these functions are not used, and knows that they have no other side >> effects, and so can eliminate them as dead code. >> >> Why are only memchr, memcmp and strlen affected? >> ================================================ >> >> Of string and string-like functions, kasan_test tests: >> >> * strchr -> not affected, no fortified version >> * strrchr -> likewise >> * strcmp -> likewise >> * strncmp -> likewise >> >> * strnlen -> not affected, the fortify source implementation calls the >> underlying strnlen implementation which is instrumented, not >> a builtin >> >> * strlen -> affected, the fortify souce implementation calls a __builtin >> version which the compiler can determine is dead. >> >> * memchr -> likewise >> * memcmp -> likewise >> >> * memset -> not affected, the compiler knows that memset writes to its >> first argument and therefore is not dead. >> >> Why does this not affect the functions normally? >> ================================================ >> >> In string.h, these functions are not marked as __pure, so the compiler >> cannot know that they do not have side effects. If relevant functions are >> marked as __pure in string.h, we see the following warnings and the >> functions are elided: >> >> lib/test_kasan.c: In function ‘kasan_memchr’: >> lib/test_kasan.c:606:2: warning: statement with no effect [-Wunused-value] >> memchr(ptr, '1', size + 1); >> ^~~~~~~~~~~~~~~~~~~~~~~~~~ >> lib/test_kasan.c: In function ‘kasan_memcmp’: >> lib/test_kasan.c:622:2: warning: statement with no effect [-Wunused-value] >> memcmp(ptr, arr, size+1); >> ^~~~~~~~~~~~~~~~~~~~~~~~ >> lib/test_kasan.c: In function ‘kasan_strings’: >> lib/test_kasan.c:645:2: warning: statement with no effect [-Wunused-value] >> strchr(ptr, '1'); >> ^~~~~~~~~~~~~~~~ >> ... >> >> This annotation would make sense to add and could be added at any point, so >> the behaviour of test_kasan.c should change. >> >> The fix >> ======= >> >> Make all the functions that are pure write their results to a global, >> which makes them live. The strlen and memchr tests now pass. >> >> The memcmp test still fails to trigger, which is addressed in the next >> patch. >> >> Cc: Daniel Micay >> Cc: Andrey Ryabinin >> Cc: Alexander Potapenko >> Cc: Dmitry Vyukov >> Fixes: 0c96350a2d2f ("lib/test_kasan.c: add tests for several string/memory API functions") >> Signed-off-by: Daniel Axtens >> --- >> lib/test_kasan.c | 30 +++++++++++++++++++----------- >> 1 file changed, 19 insertions(+), 11 deletions(-) >> >> diff --git a/lib/test_kasan.c b/lib/test_kasan.c >> index 328d33beae36..58a8cef0d7a2 100644 >> --- a/lib/test_kasan.c >> +++ b/lib/test_kasan.c >> @@ -23,6 +23,14 @@ >> >> #include >> >> +/* >> + * We assign some test results to these globals to make sure the tests >> + * are not eliminated as dead code. >> + */ >> + >> +int int_result; >> +void *ptr_result; > > These are globals, but are not static and don't have kasan_ prefix. > But I guess this does not matter for modules? > Otherwise: > > Reviewed-by: Dmitry Vyukov > I think if you make them static, GCC will see they aren't used and will eliminate everything still ? Christophe