From patchwork Wed Nov 2 12:59:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 14220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3608708wru; Wed, 2 Nov 2022 06:09:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6eGu2G7M4QJ0jrgPmrXamqeeMlrzvFIP4dHTSxbQx3iSh7JFXGZArsO0wkpGCckoAAHnmd X-Received: by 2002:a17:902:ec01:b0:186:748f:e8b6 with SMTP id l1-20020a170902ec0100b00186748fe8b6mr24524088pld.131.1667394595682; Wed, 02 Nov 2022 06:09:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667394595; cv=none; d=google.com; s=arc-20160816; b=U2Urd8qeLigo2rFM/ikwerSboal0cYbZlgqJtKP7Ba4lZvlkDkq2ZOUv1xD9Caewia rDkp6C84UxP4/413e2fZ0+YlQf6YRE22TBUAo0NjTFeDBTy2NpFi5IZBa6dWo1ydYooW DpkRD/wYvBlcYXTdC0p+1WGzd0uFcc3hVeFFo/dUR2zuivG6JTd982LAjgQRGXSXAi63 QO/sjuyCuFcwuK+k+GORlCy/Lt/PgwwohAEpGRF6gk8P/NKYQAOg75HrlVFznIYYRJbo Z2aLPIttKKSqzfGxd/CNLEr0OM6XzOqNFt+6Eru5d/NzLhS40X1VCq1sz24A8953OA2E lL4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:date:subject:cc:to:from:message-id :dkim-signature; bh=stZjbsbh1QGjqNPL6prVTAW1AvJUHAcq+yDf+xactvU=; b=V2RdlyUNSFsP+EBaPzRuHa+Vbpj9m+HVHjW0e506bwI89/BYsz5IQNb+QoNuED28nt sLLLnKHlA/ZGMLXauACJK8w/Alc84ZnFRdsrtgkuO/mpAa75mrYFtzrM4sXpeQjvIplW fhtk9P55/7UlgHOSDdN6biawxKsmqR6FjWwSOLoAhp8+lZNu7/s3x9vp2mPzH4kHlP/I OXbeAXfeNwpfMWwVNp6qyTpGZH7KaFOBnQjgtNJMl+JKBhuzd09HbLuKfPa7pkCrb/Qj 0ciUkwKTJtK7pX91BgDnCh3jCvB+zZYux0bRcMhS9lOYsrPPd1IHVWIQbM5JwskC4cv1 3AQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=yL05dZrf; 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=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o33-20020a634e61000000b0043cad841eb1si16912110pgl.827.2022.11.02.06.09.39; Wed, 02 Nov 2022 06:09:55 -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=@foxmail.com header.s=s201512 header.b=yL05dZrf; 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=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231231AbiKBM7Z (ORCPT + 99 others); Wed, 2 Nov 2022 08:59:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231215AbiKBM7X (ORCPT ); Wed, 2 Nov 2022 08:59:23 -0400 X-Greylist: delayed 99233 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 02 Nov 2022 05:59:21 PDT Received: from out162-62-57-49.mail.qq.com (out162-62-57-49.mail.qq.com [162.62.57.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0B0A2871B; Wed, 2 Nov 2022 05:59:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1667393957; bh=stZjbsbh1QGjqNPL6prVTAW1AvJUHAcq+yDf+xactvU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=yL05dZrfLeMUOj4gruBroqn84DUNXdSpZrTVtTCmHEj3MF9sogTme39DKRqY4zPtR fEBHXAXIi+wupYplwnv7E/AUQ7F46RtOz81YnTtqkX/lUSnDRfbGSeVd5zqpVP4zTy AElWs3DeGaJFayhCyd1LHofHC/J2f8U4C+4Ekpbg= Received: from localhost.localdomain ([39.156.73.13]) by newxmesmtplogicsvrszc2-0.qq.com (NewEsmtp) with SMTP id ECC23E84; Wed, 02 Nov 2022 20:59:12 +0800 X-QQ-mid: xmsmtpt1667393952tgoyzzrog Message-ID: X-QQ-XMAILINFO: OZsapEVPoiO6T7UaXCU2GP2B98Bn/6Oj0GGHqbSaMeFY4dM6yCDCzIYZga8oGS Do/Q7K59QjCbX9HFc/nC+5m6Ydadnko/0vF1fOKB9+gAd+c73JctMJsTx+lFPmiC1tKEorfsa+YI AWg4rzXMWHIcxDtFXMH16ke60IY/DlzAXO3UCrSiWlcfEz5gD+yLoYua6NA7Sn64F75J+RJEf++v oBZHNv/KH8RmQ4zRmJlSYN9f6nuMo0BG4ASGMYUZia92+p+k24WJAnbRsg/79bjyeGgR4oUPkhdj nppcnjdrBO9Crx1t+A/zJeoKRQYsGK606zj+z4nQuy81EGdSk/mvzhjd9ciNGeSxlKOjzJrCOwd/ rrF0e54jkkMqGySf6mHe52JDUZ0b3FmxE+zY/vuFB/1Us0eTd2+2F7Ynm5t7yMoKoD4ZSTuAX3N4 D4VkqTGYv0hxVTyId2oCiH4xvsCsimUCZXybfwHQVyZQj/vY1HGNtr7Wbe7x/eFb76v4Xn7V9Ezw peXOo9iTu1ua7ncWeTKTCCx0gh94rgZF2+v5tSGMeupRuOVbdZ5uS6ve6SupgRxoZTLUNYprA4au 2x60gGkynfyrUaLmjuSpRs/K+GsifhfBSD/StaokSCXyIcyU30QEvyAGHMSBrA1b7Mf1BRYr621D tg+Mur6B7sR68dYRzw5ji/053oRTmtuglBL3F6iQ1cEaTppSzXfi1VHYD49HEW2K9Y39zk+E6SZO yaXOm58TasqRwN/1WzkYsk4CfugD6DsLblEzS+Y1tYJEQPkz0EndR6ULQU54NGkTUX3X+T5upzKj i7PMrOTBYvgWrp9v6bHJJhs928ZQQSyxhs3/UBQIBcPiTnACQtvqBzWQDjYPSW+J6dshWPIzHUdb 58WvZAb6Hecx1/6tvkxGivRBILXFWbURwINbXgFJBJyJWaNXH6zAiR9PuA+57H/8/+d94jC8UuGF uBCHepGNO9/hQyo/a7Cb+YQq0I5ZUYs6E81k9smxkutwGT2+2eott+ctUxAgPhTdJiN2baJiHRVN gTz8luaA== From: Rong Tao To: daniel@iogearbox.net Cc: andrii.nakryiko@gmail.com, andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org, david.laight@aculab.com, haoluo@google.com, john.fastabend@gmail.com, jolsa@kernel.org, kpsingh@kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, martin.lau@linux.dev, mykolal@fb.com, rongtao@cestc.cn, rtoax@foxmail.com, sdf@google.com, shuah@kernel.org, song@kernel.org, yhs@fb.com Subject: [PATCH bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning Date: Wed, 2 Nov 2022 20:59:10 +0800 X-OQ-MSGID: <20221102125910.465060-1-rtoax@foxmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RCVD_IN_DNSWL_NONE,RDNS_DYNAMIC,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1748389955333192725?= X-GMAIL-MSGID: =?utf-8?q?1748389955333192725?= From: Rong Tao Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning. We can't use libbpf_internal.h directly, because it introduces a lot of header dependency issues. So move libbpf_strlcpy() into libbpf_common.h, and if you need to use the libbpf_strlcpy() function, you need to include the header file libbpf.h How to reproduce this compilation warning: $ make -C samples/bpf cgroup_helpers.c: In function ‘__enable_controllers’: cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation] 80 | strncpy(enable, controllers, sizeof(enable)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Rong Tao --- tools/lib/bpf/libbpf_common.h | 19 +++++++++++++++++++ tools/lib/bpf/libbpf_internal.h | 19 ------------------- tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++- tools/testing/selftests/bpf/xsk.c | 19 ------------------- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/tools/lib/bpf/libbpf_common.h b/tools/lib/bpf/libbpf_common.h index 9a7937f339df..9d5132e0bec9 100644 --- a/tools/lib/bpf/libbpf_common.h +++ b/tools/lib/bpf/libbpf_common.h @@ -70,4 +70,23 @@ }; \ }) +/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst + * is zero-terminated string no matter what (unless sz == 0, in which case + * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs + * in what is returned. Given this is internal helper, it's trivial to extend + * this, when necessary. Use this instead of strncpy inside libbpf source code. + */ +static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) +{ + size_t i; + + if (sz == 0) + return; + + sz--; + for (i = 0; i < sz && src[i]; i++) + dst[i] = src[i]; + dst[i] = '\0'; +} + #endif /* __LIBBPF_LIBBPF_COMMON_H */ diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 377642ff51fc..902110ffb7a6 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -191,25 +191,6 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size) return realloc(ptr, total); } -/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst - * is zero-terminated string no matter what (unless sz == 0, in which case - * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs - * in what is returned. Given this is internal helper, it's trivial to extend - * this, when necessary. Use this instead of strncpy inside libbpf source code. - */ -static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) -{ - size_t i; - - if (sz == 0) - return; - - sz--; - for (i = 0; i < sz && src[i]; i++) - dst[i] = src[i]; - dst[i] = '\0'; -} - __u32 get_kernel_version(void); struct btf; diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index e914cc45b766..e3bfe2b13018 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "cgroup_helpers.h" @@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers enable[len] = 0; close(fd); } else { - strncpy(enable, controllers, sizeof(enable)); + libbpf_strlcpy(enable, controllers, sizeof(enable)); } snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path); diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c index 0b3ff49c740d..4b6890e2a0a9 100644 --- a/tools/testing/selftests/bpf/xsk.c +++ b/tools/testing/selftests/bpf/xsk.c @@ -521,25 +521,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk) return 0; } -/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst - * is zero-terminated string no matter what (unless sz == 0, in which case - * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs - * in what is returned. Given this is internal helper, it's trivial to extend - * this, when necessary. Use this instead of strncpy inside libbpf source code. - */ -static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) -{ - size_t i; - - if (sz == 0) - return; - - sz--; - for (i = 0; i < sz && src[i]; i++) - dst[i] = src[i]; - dst[i] = '\0'; -} - static int xsk_get_max_queues(struct xsk_socket *xsk) { struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };