From patchwork Tue Nov 1 12:21:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 13646 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2932897wru; Tue, 1 Nov 2022 05:34:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5tkPc09vD9Q8HmNRISQXSvz1fYmxhxvLAiPZcFF4XHoGw8xW6C6ahXAdjMmMkDwUphEQGK X-Received: by 2002:a63:ff55:0:b0:438:fa5d:af36 with SMTP id s21-20020a63ff55000000b00438fa5daf36mr17040228pgk.533.1667306067983; Tue, 01 Nov 2022 05:34:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667306067; cv=none; d=google.com; s=arc-20160816; b=pTApyIYv6jrmLz5lGXtQWUnlvlPrhIiDDpc54dzSvMhL8/qNbMeFxmjZVOvx2V/ZrK PRnbttYtpaMDyLAziqcZsc1FOmq7ijSxQ+aEljCiGvHAvST3tV7KYLNPHxL03XefmSUe EnrpWsl+7qdwEawFGs5mftMGGjwsqvEvsz1gQ2eFiRZIgkv0SdDh1ZBX+iIjTiWS0O5u h/WqgbivguzteN05nUECUjDptoUuo/jTFXsXYHCuCy5ykkstr1Eerr/SxDSZWyDDY2eh K0g3i7mcgRk6Nml64Pxh82I7se5UMRSEZoIepQd5PXRADwv/uCfToq7mx9liVoW95Pn8 AinQ== 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=75pCasUsHqBk31UsD2MHbUsUh53DCMpvOo4pIhDwXDE=; b=i544BL3CCO50mbpJu4s/46akJUCWPxqTwyoLX8XtdvCCwpIN2G4Lme2iUpmRmCAse8 ji0NvXFv7tKbSaGoMxpB+7bQcH66kLQ3s32Ksgs7u/4JHRc4Y60z6tyNWHDOw7HJgP9v 7/r6ciXKPaqMvzX51FyXaOoU8fnOb27xHuTsHWdxBa6Ayngz0lqxjopbaLZYM4KgOGWB lHN6VMlSEr0MqSwSZJeE3a4Wtjliz+FuDFlmY8ZY8q5OdThezlj9SYk8xinLs2pUuKZn AVaLPCJuQoQJfLOFqYoAacnr4bNkgW/C1ufOcqjdWmMhRxi8jt/CcdTssSOuy9OoFABO IiYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=SxERwk2y; 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 c38-20020a631c66000000b0043a0121bb28si12377757pgm.811.2022.11.01.05.34.15; Tue, 01 Nov 2022 05:34:27 -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=SxERwk2y; 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 S230181AbiKAMVR (ORCPT + 99 others); Tue, 1 Nov 2022 08:21:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229742AbiKAMVP (ORCPT ); Tue, 1 Nov 2022 08:21:15 -0400 Received: from out162-62-57-137.mail.qq.com (out162-62-57-137.mail.qq.com [162.62.57.137]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E90D815A28; Tue, 1 Nov 2022 05:21:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1667305270; bh=75pCasUsHqBk31UsD2MHbUsUh53DCMpvOo4pIhDwXDE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=SxERwk2yicWLXruWR9UYPY62g4Sgsgofq1A/R1GPlOb98JGnKBLBLMt5vxUImCXUy DSb/35c4485XgN8jjipYBNVJQb17vlcR/6FnOxDUUvtAEz56eSrZornYvmDAajBxZ1 E3KkeCZXmDwBe1fVk+gcVjy6R5rhCU7KqanoXung= Received: from localhost.localdomain ([39.156.73.13]) by newxmesmtplogicsvrszb1-0.qq.com (NewEsmtp) with SMTP id 5458DCCE; Tue, 01 Nov 2022 20:21:05 +0800 X-QQ-mid: xmsmtpt1667305265tr27vlhfu Message-ID: X-QQ-XMAILINFO: NUmvkRAbRxZgsBl3jbK6+SAphFPsVPLkCwSPPdEguHkkovswAQWlXf+b79DpDm 6DnVWkC0Rrs/3iGq+CdtRWNhELQ4K/z4/1tbOy9DITMM2irKuYWSQ2Ehu8JOGeAsjAry5ojwr03A xKckEd02j5C2IisSzbBDR/xudTReQWARwQl7cbHf0C29A8H5TT3/LUkM7NDtWtK+iE2CMGoSizuf LVZNnUvNmIKmlimfexBxWoHEOaFrmgs0Wa56maLBc1Fi4foOdmhPF/mSLrsewRfDKoXxAVRL66+0 SgDPVTPdh7WzZefSLLBzkxpmIFRB4thWEgrPZIG/U9R0wNBq3l11Y630AH7JqGY0CXebf5BCtCV8 kzYf/Q+tv9dmIeHybF4Az0ySCvWTw6tiFSjkBy80YviD4mP5tMRUKveTijK+rNOiIugg6HHJwkDL 9hygDW7aXv1q/idJBFgm9EPKywl9kaEbig5bCV9hp/D25rbfo3Vb3cdfqViL3TVU3+3KAeMQqooa ECOTN5HVpG22Pnx8THkfu/ZCA6bV5FrkNUnmNtrSHV4M0nfxc5YiYqc+dd0tjyIqOUwTlIG93hqw JPWlCAS4H96fzVIU+KPTCAxAFcfvaAtozStmfcwA3myJZ0bPqz5w3vS3esvbKeHWNAT56CGtT4Gy X6Wm/D8XDL2mDZs5zqY/GWFojbh5i3OE/TP9iSbpBIqd6ozbtNR1Xumnh5xebhasa3HZUhwg+pkF 7fHaeBjN+w1mbGncsbk5ZotBQlj5Eaf13xZ65bQDJm/4kEg5an7gbxrWoMMjaxv9czEJJld5rmOh THSNU2xU//oDrbggjlIQJsYGUGNA4vIpBgT/hnkGCt7TKOi52x1G8ZNIxsmsAq42Oq8i+AmQ1gVu KcpN8kCNEi6fyGZXvHxLQ5mrKZ6TsGLeyjxXl/402aaM74NUOM+2lNV5NoexA1vQaqz6Phwf2+/z eXhFwLiRC5HAW73k5TEoJxnFYktB7u5swbWW/S7mzMtbBtupMx4QUHVzX/mak8J+5gGxjWVnzn5w /fhfHtqw== From: Rong Tao To: david.laight@aculab.com Cc: andrii.nakryiko@gmail.com, andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org, daniel@iogearbox.net, 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: Fix strncpy() fortify warning Date: Tue, 1 Nov 2022 20:21:04 +0800 X-OQ-MSGID: <20221101122104.54265-1-rtoax@foxmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <70ddf02157bc418cbc38230c87a7b228@AcuMS.aculab.com> References: <70ddf02157bc418cbc38230c87a7b228@AcuMS.aculab.com> 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,RCVD_IN_MSPIKE_H2,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?1747841326605338612?= X-GMAIL-MSGID: =?utf-8?q?1748297127528931649?= From: Rong Tao move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning. Compile samples/bpf, warning: $ cd samples/bpf $ make ... 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/testing/selftests/bpf/bpf_util.h | 19 +++++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++- tools/testing/selftests/bpf/xsk.c | 19 ------------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h index a3352a64c067..bf78212ff6e9 100644 --- a/tools/testing/selftests/bpf/bpf_util.h +++ b/tools/testing/selftests/bpf/bpf_util.h @@ -20,6 +20,25 @@ static inline unsigned int bpf_num_possible_cpus(void) return possible_cpus; } +/* 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'; +} + #define __bpf_percpu_val_align __attribute__((__aligned__(8))) #define BPF_DECLARE_PERCPU(type, name) \ diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index e914cc45b766..e33b70e509da 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -13,6 +13,7 @@ #include #include "cgroup_helpers.h" +#include "bpf_util.h" /* * To avoid relying on the system setup, when setup_cgroup_env is called @@ -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 };