From patchwork Tue Feb 14 08:56:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 56775 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2850187wrn; Tue, 14 Feb 2023 00:57:19 -0800 (PST) X-Google-Smtp-Source: AK7set8/ycUBUOplvuMbKyJig5Pjhhtkt/HR82nrfUmKsHYKbSg42RFFUEWMtZVJLGFODpHYFBZZ X-Received: by 2002:a17:907:2089:b0:7c4:fa17:7203 with SMTP id pv9-20020a170907208900b007c4fa177203mr1716500ejb.63.1676365039640; Tue, 14 Feb 2023 00:57:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676365039; cv=none; d=google.com; s=arc-20160816; b=0Dvs9BfK/Cxh/cz8ekS02YIibH+QmPwQbm/BIu1YL/Vx6vwCdDFQ6lC3Z+CKJxEmLi l2gF31XWmuYFIyYoKDhO/3xkiP6JP21n8q9p1NvIJkCpjs6WyWbkCsGNSr1Lm2HcXDQu VcgT7znDV5uRS4a931DL3SSON0J7M9rYarTGxl9umkKfnH4xnui0ryLdGh38EigqQJKJ dXVSSeAWLph8Vl2GslEgsscCWgtj3Xa3KhylV0HQxwPU9dxaJLcuLkgzyeCp6mNDGtCa rKwbtDdEsswLlkoyRVEaKWRkHftITvKBo30FI+HRxBUPF1MhFL7sP++UnpdYnt04/q+h L3UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=SrkvUx//uo6gO2fznU+9VBtPYkR6SIGj/kYSlz1ku04=; b=sgUF1Ov7ZsqScGBW6ddDk0elVjI93Tk/emZnuaMU0CRuC8Aq1AH91O36sIdys+V6Ht wCXSNdEyA8Q0mjH/CLhp2ibTSrD22YNHEH8DiaNs+L+RDcgP5L3K2zZyuDL96pV4RHq0 /xJVJi3g3rSoISf+AYsC0e3yDWzVxbFOrcvuPXxOoMF0cuUCUUhyQ0sA97oEqqMohzh9 WYcf0ANX7yJDiMJy3NYgEF28aOgpetC2eR/sa7BlExi9ofZjHJXxREjKDZ2cmaReLryd JUT3Unk+0gyk/CFZ9Oupo+M0R/GjihgLF/WIAiqN19g0oNVFXil7DCNY95HbyexZ+75r 0FIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BNd5zxyJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id fi22-20020a1709073ad600b0088e18383de8si13441285ejc.613.2023.02.14.00.57.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 00:57:19 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.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=@gcc.gnu.org header.s=default header.b=BNd5zxyJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55ED23858C62 for ; Tue, 14 Feb 2023 08:57:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 55ED23858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676365038; bh=SrkvUx//uo6gO2fznU+9VBtPYkR6SIGj/kYSlz1ku04=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=BNd5zxyJgT+iSKlxiWXKMlmZSZ9llOZ8JpzfKzpaBxjd0aL+6Md2W8P8miO8cy0eY b0WGwGrzSlqRsi86UVPd8ud625H+GIjbMz3RIqEl2wc70GefnQN2ecRqUJ11IxV0SS pYgvop+22jWYiiZ7WnXM5TAaRxcZeBQ9Saiip7Aw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D7F423858D33 for ; Tue, 14 Feb 2023 08:56:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D7F423858D33 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-tL7iKfZrMkayuQhJZhG2AA-1; Tue, 14 Feb 2023 03:56:12 -0500 X-MC-Unique: tL7iKfZrMkayuQhJZhG2AA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8450118A6461; Tue, 14 Feb 2023 08:56:12 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 402904010E86; Tue, 14 Feb 2023 08:56:12 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 31E8u9iH553737 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 14 Feb 2023 09:56:09 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 31E8u8cM553736; Tue, 14 Feb 2023 09:56:08 +0100 Date: Tue, 14 Feb 2023 09:56:08 +0100 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] asan: Add --param=asan-kernel-mem-intrinsic-prefix= [PR108777] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757796147918928899?= X-GMAIL-MSGID: =?utf-8?q?1757796147918928899?= Hi! While in the -fsanitize=address case libasan overloads memcpy, memset, memmove and many other builtins, such that they are always instrumented, Linux kernel for -fsanitize=kernel-address recently changed or is changing, such that memcpy, memset and memmove actually aren't instrumented because they are often used also from no_sanitize ("kernel-address") functions and wants __{,hw,}asaN_{memcpy,memset,memmove} to be used instead for the instrumented calls. See e.g. the https://lkml.org/lkml/2023/2/9/1182 thread. Without appropriate support on the compiler side, that will mean any time a kernel-address instrumented function (most of them) calls memcpy/memset/memmove, they will not be instrumented and thus won't catch kernel bugs. Apparently clang 15 has a param for this. The following patch implements the same (except it is a usual GCC --param, not -mllvm argument) on the GCC side. I know this isn't a regression bugfix, but given that -fsanitize=kernel-address has a single project that uses it which badly wants this I think it would be worthwhile to make an exception and get this into GCC 13 rather than waiting another year, it won't affect non-kernel code, nor even the kernel unless the new parameter is used. Bootstrapped/regtested on x86_64-linux and i686-linux and Marco has tested it on the kernel, ok for trunk? 2023-02-14 Jakub Jelinek PR sanitizer/108777 * params.opt (-param=asan-kernel-mem-intrinsic-prefix=): New param. * asan.h (asan_memfn_rtl): Declare. * asan.cc (asan_memfn_rtls): New variable. (asan_memfn_rtl): New function. * builtins.cc (expand_builtin): If param_asan_kernel_mem_intrinsic_prefix and function is kernel-{,hw}address sanitized, emit calls to __{,hw}asan_{memcpy,memmove,memset} rather than {memcpy,memmove,memset}. Use sanitize_flags_p (SANITIZE_ADDRESS) instead of flag_sanitize & SANITIZE_ADDRESS to check if asan_intercepted_p functions shouldn't be expanded inline. * gcc.dg/asan/pr108777-1.c: New test. * gcc.dg/asan/pr108777-2.c: New test. * gcc.dg/asan/pr108777-3.c: New test. * gcc.dg/asan/pr108777-4.c: New test. * gcc.dg/asan/pr108777-5.c: New test. * gcc.dg/asan/pr108777-6.c: New test. * gcc.dg/completion-3.c: Adjust expected multiline output. Jakub --- gcc/params.opt.jj 2023-02-10 19:04:58.289014706 +0100 +++ gcc/params.opt 2023-02-13 16:19:50.411101775 +0100 @@ -50,6 +50,10 @@ Enable asan store operations protection. Common Joined UInteger Var(param_asan_instrumentation_with_call_threshold) Init(7000) Param Optimization Use callbacks instead of inline code if number of accesses in function becomes greater or equal to this number. +-param=asan-kernel-mem-intrinsic-prefix= +Common Joined UInteger Var(param_asan_kernel_mem_intrinsic_prefix) Init(0) IntegerRange(0, 1) Param Optimization +Prefix calls to memcpy, memset and memmove with __asan_ or __hwasan_ for -fsanitize=kernel-address or -fsanitize=kernel-hwaddress. + -param=asan-memintrin= Common Joined UInteger Var(param_asan_memintrin) Init(1) IntegerRange(0, 1) Param Optimization Enable asan builtin functions protection. --- gcc/asan.h.jj 2023-01-02 09:32:26.721222635 +0100 +++ gcc/asan.h 2023-02-13 16:45:14.475088159 +0100 @@ -33,6 +33,7 @@ extern bool asan_expand_check_ifn (gimpl extern bool asan_expand_mark_ifn (gimple_stmt_iterator *); extern bool asan_expand_poison_ifn (gimple_stmt_iterator *, bool *, hash_map &); +extern rtx asan_memfn_rtl (tree); extern void hwasan_record_frame_init (); extern void hwasan_record_stack_var (rtx, rtx, poly_int64, poly_int64); --- gcc/asan.cc.jj 2023-02-02 10:54:44.326473507 +0100 +++ gcc/asan.cc 2023-02-13 16:52:16.711015256 +0100 @@ -391,6 +391,46 @@ asan_memintrin (void) } +/* Support for --param asan-kernel-mem-intrinsic-prefix=1. */ +static GTY(()) rtx asan_memfn_rtls[3]; + +rtx +asan_memfn_rtl (tree fndecl) +{ + int i; + const char *f, *p; + char buf[sizeof ("__hwasan_memmove")]; + + switch (DECL_FUNCTION_CODE (fndecl)) + { + case BUILT_IN_MEMCPY: i = 0; f = "memcpy"; break; + case BUILT_IN_MEMSET: i = 1; f = "memset"; break; + case BUILT_IN_MEMMOVE: i = 2; f = "memmove"; break; + default: gcc_unreachable (); + } + if (asan_memfn_rtls[i] == NULL_RTX) + { + tree save_name = DECL_NAME (fndecl); + tree save_assembler_name = DECL_ASSEMBLER_NAME (fndecl); + rtx save_rtl = DECL_RTL (fndecl); + if (flag_sanitize & SANITIZE_KERNEL_HWADDRESS) + p = "__hwasan_"; + else + p = "__asan_"; + strcpy (buf, p); + strcat (buf, f); + DECL_NAME (fndecl) = get_identifier (buf); + DECL_ASSEMBLER_NAME_RAW (fndecl) = NULL_TREE; + SET_DECL_RTL (fndecl, NULL_RTX); + asan_memfn_rtls[i] = DECL_RTL (fndecl); + DECL_NAME (fndecl) = save_name; + DECL_ASSEMBLER_NAME_RAW (fndecl) = save_assembler_name; + SET_DECL_RTL (fndecl, save_rtl); + } + return asan_memfn_rtls[i]; +} + + /* Checks whether section SEC should be sanitized. */ static bool --- gcc/builtins.cc.jj 2023-02-02 10:54:44.330473449 +0100 +++ gcc/builtins.cc 2023-02-13 16:46:42.127826612 +0100 @@ -7326,7 +7326,24 @@ expand_builtin (tree exp, rtx target, rt by ASan. */ enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); - if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode)) + if (param_asan_kernel_mem_intrinsic_prefix + && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS + | SANITIZE_KERNEL_HWADDRESS)) + switch (fcode) + { + rtx save_decl_rtl, ret; + case BUILT_IN_MEMCPY: + case BUILT_IN_MEMMOVE: + case BUILT_IN_MEMSET: + save_decl_rtl = DECL_RTL (fndecl); + DECL_RTL (fndecl) = asan_memfn_rtl (fndecl); + ret = expand_call (exp, target, ignore); + DECL_RTL (fndecl) = save_decl_rtl; + return ret; + default: + break; + } + if (sanitize_flags_p (SANITIZE_ADDRESS) && asan_intercepted_p (fcode)) return expand_call (exp, target, ignore); /* When not optimizing, generate calls to library functions for a certain --- gcc/testsuite/gcc.dg/asan/pr108777-1.c.jj 2023-02-13 17:49:23.139002484 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-1.c 2023-02-13 17:51:09.433479988 +0100 @@ -0,0 +1,28 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1" } */ +/* { dg-final { scan-assembler "__asan_memcpy" } } */ +/* { dg-final { scan-assembler "__asan_memset" } } */ +/* { dg-final { scan-assembler "__asan_memmove" } } */ + +extern void *memcpy (void *, const void *, __SIZE_TYPE__); +extern void *memmove (void *, const void *, __SIZE_TYPE__); +extern void *memset (void *, int, __SIZE_TYPE__); + +void +foo (void *p, void *q, int s) +{ + memcpy (p, q, s); +} + +void +bar (void *p, void *q, int s) +{ + memmove (p, q, s); +} + +void +baz (void *p, int c, int s) +{ + memset (p, c, s); +} --- gcc/testsuite/gcc.dg/asan/pr108777-2.c.jj 2023-02-13 17:49:27.215944098 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-2.c 2023-02-13 17:51:09.433479988 +0100 @@ -0,0 +1,24 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1" } */ +/* { dg-final { scan-assembler "__asan_memcpy" } } */ +/* { dg-final { scan-assembler "__asan_memset" } } */ +/* { dg-final { scan-assembler "__asan_memmove" } } */ + +void +foo (void *p, void *q, int s) +{ + __builtin_memcpy (p, q, s); +} + +void +bar (void *p, void *q, int s) +{ + __builtin_memmove (p, q, s); +} + +void +baz (void *p, int c, int s) +{ + __builtin_memset (p, c, s); +} --- gcc/testsuite/gcc.dg/asan/pr108777-3.c.jj 2023-02-13 17:49:30.683894408 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-3.c 2023-02-13 17:51:09.434479973 +0100 @@ -0,0 +1,28 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1" } */ +/* { dg-final { scan-assembler-not "__asan_memcpy" } } */ +/* { dg-final { scan-assembler-not "__asan_memset" } } */ +/* { dg-final { scan-assembler-not "__asan_memmove" } } */ + +extern void *memcpy (void *, const void *, __SIZE_TYPE__); +extern void *memmove (void *, const void *, __SIZE_TYPE__); +extern void *memset (void *, int, __SIZE_TYPE__); + +__attribute__((no_sanitize("kernel-address"))) void +foo (void *p, void *q, int s) +{ + memcpy (p, q, s); +} + +__attribute__((no_sanitize("kernel-address"))) void +bar (void *p, void *q, int s) +{ + memmove (p, q, s); +} + +__attribute__((no_sanitize("kernel-address"))) void +baz (void *p, int c, int s) +{ + memset (p, c, s); +} --- gcc/testsuite/gcc.dg/asan/pr108777-4.c.jj 2023-02-13 17:49:33.985847114 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-4.c 2023-02-13 17:51:09.434479973 +0100 @@ -0,0 +1,24 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1" } */ +/* { dg-final { scan-assembler-not "__asan_memcpy" } } */ +/* { dg-final { scan-assembler-not "__asan_memset" } } */ +/* { dg-final { scan-assembler-not "__asan_memmove" } } */ + +__attribute__((no_sanitize("kernel-address"))) void +foo (void *p, void *q, int s) +{ + __builtin_memcpy (p, q, s); +} + +__attribute__((no_sanitize("kernel-address"))) void +bar (void *p, void *q, int s) +{ + __builtin_memmove (p, q, s); +} + +__attribute__((no_sanitize("kernel-address"))) void +baz (void *p, int c, int s) +{ + __builtin_memset (p, c, s); +} --- gcc/testsuite/gcc.dg/asan/pr108777-5.c.jj 2023-02-13 17:49:37.195801146 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-5.c 2023-02-13 17:51:09.434479973 +0100 @@ -0,0 +1,28 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address" } */ +/* { dg-final { scan-assembler-not "__asan_memcpy" } } */ +/* { dg-final { scan-assembler-not "__asan_memset" } } */ +/* { dg-final { scan-assembler-not "__asan_memmove" } } */ + +extern void *memcpy (void *, const void *, __SIZE_TYPE__); +extern void *memmove (void *, const void *, __SIZE_TYPE__); +extern void *memset (void *, int, __SIZE_TYPE__); + +void +foo (void *p, void *q, int s) +{ + memcpy (p, q, s); +} + +void +bar (void *p, void *q, int s) +{ + memmove (p, q, s); +} + +void +baz (void *p, int c, int s) +{ + memset (p, c, s); +} --- gcc/testsuite/gcc.dg/asan/pr108777-6.c.jj 2023-02-13 17:49:40.282756931 +0100 +++ gcc/testsuite/gcc.dg/asan/pr108777-6.c 2023-02-13 17:51:09.434479973 +0100 @@ -0,0 +1,24 @@ +/* PR sanitizer/108777 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-sanitize=all -fsanitize=kernel-address" } */ +/* { dg-final { scan-assembler-not "__asan_memcpy" } } */ +/* { dg-final { scan-assembler-not "__asan_memset" } } */ +/* { dg-final { scan-assembler-not "__asan_memmove" } } */ + +void +foo (void *p, void *q, int s) +{ + __builtin_memcpy (p, q, s); +} + +void +bar (void *p, void *q, int s) +{ + __builtin_memmove (p, q, s); +} + +void +baz (void *p, int c, int s) +{ + __builtin_memset (p, c, s); +} --- gcc/testsuite/gcc.dg/completion-3.c.jj 2020-01-14 20:02:47.249602853 +0100 +++ gcc/testsuite/gcc.dg/completion-3.c 2023-02-14 09:39:44.613203143 +0100 @@ -7,6 +7,7 @@ --param=asan-instrument-reads= --param=asan-instrument-writes= --param=asan-instrumentation-with-call-threshold= +--param=asan-kernel-mem-intrinsic-prefix= --param=asan-memintrin= --param=asan-stack= --param=asan-use-after-return=