Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1944995ybz; Thu, 23 Apr 2020 08:49:03 -0700 (PDT) X-Google-Smtp-Source: APiQypK5fqbcbywejAl1s2O2ZK6aZX0Pl+kz4v3VS/Wunu2Ij8FgGoRgPBfZhEZPJhDoKfgUXA9f X-Received: by 2002:a05:6402:752:: with SMTP id p18mr3402295edy.261.1587656943181; Thu, 23 Apr 2020 08:49:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587656943; cv=none; d=google.com; s=arc-20160816; b=xGL5bxhswMxjA06HBVRaRQOqNMS3WHz5IOtdTCRz3srSaV7idNc0wITU7g+cA07yUD h9RYKZEqXGBof1XS9pt6QNyr45G34Wrf23lfKym2g/XRsHUX7Zk/PRWJ1IA0GTg9q4vB n3Vjww5phHopA3GHZV/TPo3KlbObNHFOw+hkt3x+j9wEgGk/VKmwn0yuPRqg6FPlFdRi F7fDAe56VJTmiAriWS4nSbDBJyR0FSSbzINTBdoipLPbGkWTF51RhV6zKIJUE/p55eMF bV3JMS/N3nEP5Gvtr760lMUb0YG9dcqD2wCsrAyg5hxX1I7zIqVf3BELVr4ZY1D/95C0 UrRA== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Rm9KJcgesHnxEUAmWee/mQjOLa+QTtuSP9X5sIq0tgA=; b=mdudvjOkmuImT0LrjUBODy3LVNeEU/RA8gh/DnSXCADShvnDkWpnudOLNMvUzBdgBS oDcZpR7o5YF1a8f4rfJ47glcBX/BvWe80cn5tuiqmdPRbe78QxeJ7wb7hohtoCzq3ix/ VKlpj8/kjtd/35+MqzSxTLD0Bd9pkNy0OG8ggIq1Rry0ssGBkFbL6yDFDie7eoXZr1v2 pkHHFEg3oTSxA1FHGGOqjZZthHl171BiPDOQekV6ltgdJwVah01NqXOqtHyYzv87qfv4 R1Pm7eRif/mBOjrw/HrWZ1TBrESIPysHGZjihen0WJNaE0muegnRGVToRTX0NrCiRRyj 6iCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=jPvZSIVR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o59si1296855eda.39.2020.04.23.08.48.16; Thu, 23 Apr 2020 08:49:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=jPvZSIVR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729428AbgDWPpR (ORCPT + 99 others); Thu, 23 Apr 2020 11:45:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729420AbgDWPpQ (ORCPT ); Thu, 23 Apr 2020 11:45:16 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9729C09B040 for ; Thu, 23 Apr 2020 08:45:14 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id g6so3070158pgs.9 for ; Thu, 23 Apr 2020 08:45:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Rm9KJcgesHnxEUAmWee/mQjOLa+QTtuSP9X5sIq0tgA=; b=jPvZSIVRZtIuE/ljLJ3/1SMhxZNEYVUDCJn9nhJ1b8Rl/QIVp+M4bzvcP3LLZPvbj+ Ep1FWxzMDYCy8ID1pym0moYcnagwj44Z0fGnTNV/px2InnO20wPoAGY2Lehuaau4EKhV MgAEZsw3rHya1t16HQL+3GnyKd8WKAqsljxmA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Rm9KJcgesHnxEUAmWee/mQjOLa+QTtuSP9X5sIq0tgA=; b=dr4000YFnbCx4p2wKgKMxW1hjJzwzk/jOPDoHQNGkMTNmIMj52MhsCngLq2XsXqNwc ADOjI/G+UadjKGkFufjJGY3ZeTkS3gRaCSERYLHvKIQMNKGWewrziO9TZ6KsbkZlaNfp q5ir4pzYoB++hf1wSyeujfc+xTG5MR13gdoLfhXP1bqIfffUblod5LTUvvKl5RnUPJlT mlN4W494PF55nWFeD/cd8rIgg93WID3zy92Nwa2B5C3JjZ9QCd5VD1/d5Smj6rstteh0 eGgGOPybvLgFbWwF5bWs0asARwFZgSYLBcAuJKLg5M5rFIXWUml4ZfiqIhccmUkCpCPt PoMg== X-Gm-Message-State: AGi0PuaLk70oEUukOtR5awcpASdRGtlQISkF3MhTSq6s2X085W+eYb5o APu8oSiZ76Y9+USTnsLu/921jSB8V14= X-Received: by 2002:a62:1d48:: with SMTP id d69mr4369984pfd.102.1587656714044; Thu, 23 Apr 2020 08:45:14 -0700 (PDT) Received: from localhost (2001-44b8-111e-5c00-7979-720a-9390-aec6.static.ipv6.internode.on.net. [2001:44b8:111e:5c00:7979:720a:9390:aec6]) by smtp.gmail.com with ESMTPSA id w125sm2435466pgw.22.2020.04.23.08.45.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2020 08:45:13 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, kasan-dev@googlegroups.com Cc: dvyukov@google.com, christophe.leroy@c-s.fr, Daniel Axtens , Daniel Micay , Andrey Ryabinin , Alexander Potapenko Subject: [PATCH v3 1/3] kasan: stop tests being eliminated as dead code with FORTIFY_SOURCE Date: Fri, 24 Apr 2020 01:45:01 +1000 Message-Id: <20200423154503.5103-2-dja@axtens.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200423154503.5103-1-dja@axtens.net> References: <20200423154503.5103-1-dja@axtens.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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") Reviewed-by: Dmitry Vyukov 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 e3087d90e00d..939f395a5392 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 kasan_int_result; +void *kasan_ptr_result; + /* * Note: test functions are marked noinline so that their names appear in * reports. @@ -622,7 +630,7 @@ static noinline void __init kasan_memchr(void) if (!ptr) return; - memchr(ptr, '1', size + 1); + kasan_ptr_result = memchr(ptr, '1', size + 1); kfree(ptr); } @@ -637,8 +645,7 @@ static noinline void __init kasan_memcmp(void) if (!ptr) return; - memset(arr, 0, sizeof(arr)); - memcmp(ptr, arr, size+1); + kasan_int_result = memcmp(ptr, arr, size + 1); kfree(ptr); } @@ -661,22 +668,22 @@ static noinline void __init kasan_strings(void) * will likely point to zeroed byte. */ ptr += 16; - strchr(ptr, '1'); + kasan_ptr_result = strchr(ptr, '1'); pr_info("use-after-free in strrchr\n"); - strrchr(ptr, '1'); + kasan_ptr_result = strrchr(ptr, '1'); pr_info("use-after-free in strcmp\n"); - strcmp(ptr, "2"); + kasan_int_result = strcmp(ptr, "2"); pr_info("use-after-free in strncmp\n"); - strncmp(ptr, "2", 1); + kasan_int_result = strncmp(ptr, "2", 1); pr_info("use-after-free in strlen\n"); - strlen(ptr); + kasan_int_result = strlen(ptr); pr_info("use-after-free in strnlen\n"); - strnlen(ptr, 1); + kasan_int_result = strnlen(ptr, 1); } static noinline void __init kasan_bitops(void) @@ -743,11 +750,12 @@ static noinline void __init kasan_bitops(void) __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits); pr_info("out-of-bounds in test_bit\n"); - (void)test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits); + kasan_int_result = test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits); #if defined(clear_bit_unlock_is_negative_byte) pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n"); - clear_bit_unlock_is_negative_byte(BITS_PER_LONG + BITS_PER_BYTE, bits); + kasan_int_result = clear_bit_unlock_is_negative_byte(BITS_PER_LONG + + BITS_PER_BYTE, bits); #endif kfree(bits); } -- 2.20.1