From patchwork Tue May 30 08:39:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 100675 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2040199vqr; Tue, 30 May 2023 02:09:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ68jcpNCwDJUx8KQFheyHLQEDSQLbqFsFDmo9Ht8R4eF1KNufdjfcwGfzPgZnlW/b1joawA X-Received: by 2002:a17:903:41c5:b0:1b0:28a9:fa78 with SMTP id u5-20020a17090341c500b001b028a9fa78mr2092752ple.34.1685437788829; Tue, 30 May 2023 02:09:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685437788; cv=none; d=google.com; s=arc-20160816; b=jdWi6LtacS3B+yhdlIQfqDQ0w2RijSnoTyE+b21x+No4Pk3wXcS/yK1TYYFsPld9sJ NmoIrOB+aZRB2YnSoj17DHyj32qqFROh3D1lmwoLEtS0D5fPLB35vUkk9o0m0Tg2Y6/K 4SkTLCDVney6HXFqDN21w/nQn2ZfiZzEkTiwB6aX5ujuKWxv6hxmASdZuLKx8wy4PHfe 84djbA5p3NZkpD4O2jCA9cmV9jSYWhKiVlSlk3gTc0JsWdiOHzhLw9Jttfik4Te1wgdQ v1XXgGrVNkAJ3+57IutjBAa2jw9foG3dxvclSAcVfP78wNwoEfmuerR+8F6RdxA+IMbG voIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=d2gZVOqgM2bnB8NENsAMpjub1Q/xoX1pCqU5k5h5AWY=; b=hYJB1IcxYJGCurR73Ea17lPQve27DthyAIFqcpwL+9ru3F+JODIFDb/4/qdQnXLoRT ZYuh3EbyrEhP5O/Xnw94VAVie3oyE954c3ls0P/iiky/yTKeGLyU+Rd0btAzQGB6OI6x IOwD0vyiGBwykQglOcmFSiADPrKy+G/O+W/1cYW9mc7NVheBRedzUuE3QAQd1bxOhtF6 oCYieZnSep7R2z6pHgOAlPwObYXrBj40pMBFOeDt5ST1JD5Jt+mxhDpLNp66jAAXTlWc QpRdfOdJ8sL7mgXqiFjF3JUfpfvoGwS3DVia55moT336KL/m0Xh7BoFiRSMKHzZ8SeS2 JJfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Qr8jpDkX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z10-20020a170903018a00b001ac59bc1c20si139290plg.47.2023.05.30.02.09.37; Tue, 30 May 2023 02:09:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Qr8jpDkX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230254AbjE3IjU (ORCPT + 99 others); Tue, 30 May 2023 04:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbjE3IjT (ORCPT ); Tue, 30 May 2023 04:39:19 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84627C9 for ; Tue, 30 May 2023 01:39:17 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-5142da822cbso4093776a12.2 for ; Tue, 30 May 2023 01:39:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685435956; x=1688027956; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=d2gZVOqgM2bnB8NENsAMpjub1Q/xoX1pCqU5k5h5AWY=; b=Qr8jpDkXiWtlWEKgM/OCiUY6Qr6uIug26AODpYGcZCgHTfMWI3yZaegQAjWlbgKu4z 104GXMaD2SXHLrPfTEEfuM9jfDznwWhEwcuiJuoh0higXz2jNLE7lzNCaL5n3nBCjacW +4FyxBv3CzhKMvvafis0fhTTfvmgShIcoQnQt3e2nDYnAr9DEl0ornOw8+oGgruVFraT 3nDsArzpfnNfbH/+0HI1ziVPlMgtUuqBISYUh+i2BGQ1NGqbPulsFH1ae3NrHFHtP+uF dGhVDhLJdr4OJfuu4gJzx3j6RhktmWrxEKilQRAG2UvMGfRIORAdf9bNp4Dvtj3JYb3q icYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685435956; x=1688027956; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=d2gZVOqgM2bnB8NENsAMpjub1Q/xoX1pCqU5k5h5AWY=; b=Fk6RxvDm/IA+nMaf5Lu+lTrgs16uhThH68q8FX2K3X2nyT2pl91s3lRNgWi8iPjZne 1/2MVu5dEDjAKV01QLcOnY+pkRn5bmZlGWomOnPrgegXfmJuaqJrDVVbWZpgf5wFgyl2 H9YsDLv7L8VtpAer1+hZyeD3Wawvy4J9gtCk32wQK1wNVTyowxbEziDwVC+0nUJ0gj4G MP24fhfKPrpyVzDnTSCvlFVME+Iak4d72F/TyWfrPp/LKCEOgKvACt/qEQ+4YDwtuhLJ lV5DTuI2IXPAe1EMOdeHaNQlswoAA6ELKLf4qYthNmaZLj7o5orvFXT6TC/MQzIMU8I9 AtTQ== X-Gm-Message-State: AC+VfDxTy9Oe1hlekiToWiCXLgKtL6h7MrgSwj3aE3NQnZOtYgDNArQd K1zRpC9vdOvQ8gMGrLG36ie83h8zj0A= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a4:5738:5a7f:a82e]) (user=glider job=sendgmr) by 2002:a50:8ada:0:b0:50b:c4f7:fa5a with SMTP id k26-20020a508ada000000b0050bc4f7fa5amr529352edk.3.1685435956071; Tue, 30 May 2023 01:39:16 -0700 (PDT) Date: Tue, 30 May 2023 10:39:11 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230530083911.1104336-1-glider@google.com> Subject: [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat From: Alexander Potapenko To: glider@google.com, andy@kernel.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, elver@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, ndesaulniers@google.com, nathan@kernel.org, keescook@chromium.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767309614947395696?= X-GMAIL-MSGID: =?utf-8?q?1767309614947395696?= lib/string.c is built with -ffreestanding, which prevents the compiler from replacing certain functions with calls to their library versions. On the other hand, this also prevents Clang and GCC from instrumenting calls to memcpy() when building with KASAN, KCSAN or KMSAN: - KASAN normally replaces memcpy() with __asan_memcpy() with the additional cc-param,asan-kernel-mem-intrinsic-prefix=1; - KCSAN and KMSAN replace memcpy() with __tsan_memcpy() and __msan_memcpy() by default. To let the tools catch memory accesses from strlcpy/strlcat, replace the calls to memcpy() with __builtin_memcpy(), which KASAN, KCSAN and KMSAN are able to replace even in -ffreestanding mode. This preserves the behavior in normal builds (__builtin_memcpy() ends up being replaced with memcpy()), and does not introduce new instrumentation in unwanted places, as strlcpy/strlcat are already instrumented. Suggested-by: Marco Elver Signed-off-by: Alexander Potapenko Link: https://lore.kernel.org/all/20230224085942.1791837-1-elver@google.com/ Acked-by: Kees Cook --- lib/string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/string.c b/lib/string.c index 3d55ef8901068..be26623953d2e 100644 --- a/lib/string.c +++ b/lib/string.c @@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size) if (size) { size_t len = (ret >= size) ? size - 1 : ret; - memcpy(dest, src, len); + __builtin_memcpy(dest, src, len); dest[len] = '\0'; } return ret; @@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count) count -= dsize; if (len >= count) len = count-1; - memcpy(dest, src, len); + __builtin_memcpy(dest, src, len); dest[len] = 0; return res; }