Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp519108rwb; Wed, 7 Dec 2022 01:21:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf6QHC4KMthVW+S5hjZsZDGUybW+DeBy39N2zKHlhcrK1pNbn7BXFdF/6rq4OWAw9AwQ2i6r X-Received: by 2002:a63:e84f:0:b0:477:7dc8:57a7 with SMTP id a15-20020a63e84f000000b004777dc857a7mr64561878pgk.498.1670404870095; Wed, 07 Dec 2022 01:21:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670404870; cv=none; d=google.com; s=arc-20160816; b=Z6MRfNPG+DYXoD+nRjiBBTkD9T8To0HotAu11ZskRZWIRfC+0fFGZjotZXGcqB3q9u icxrMhZjJxvD8BOq9lj/orMIIq9Fp2ZI1x57LziqoYOV6W7O/36r61RgeYJtSO6Nw7tk GYz07VK5K5XctHQ10IVHq4wLpyidGRzw9UbJHCHTQQ8QbbQhiK0nuvEOP1fBYjgOVYFn 5OjetbuG20d2ix1rayd9RTiyAIStnXCLmd/elr0Qr29F3ytxW7c3LHT5+pt3C/AT25ai uvTWsnkJZEDERj8ZI9I4A4Sye4ZX/130iPhsI6L7WJe0HmjfAtzUPjonNv5dJe9NAutJ Mw9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=tf4QGXiY3Iu7vc4wT4W/0lDuFM7T66XwpfNVxH60/M4=; b=rshD3qfX+SdZhNi1P6c8UivC+HlbV1hZx8mStUBuh1qf+/b2zIWeymI+u0UfE07Q/F d2C3uKa5tXmdYD1JeYvlidWVBBVSBzVXuFLFXG6ZEYnZQ2f3z+vCHGoHXZGrT8EQMjMD YlmJkVXjNCVX8DsiWCJEFZzF2gC9sGQ8gAnEMSWk0zgUvV2L3M1tTHQK2RVMtbNmL8oE xbnFnIR8JjbvKMLYfFZwCJpkenlkN9/zcg/ERZxkbc7GU64xUIz1LvN0dJPp/jzVr4uf bwdud1hQp3H/OTTTFxHLIbuy9mmYURYBHjbOKgqJ3E2wDzgoPOrDuopP1agNMR109nun giLg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3-20020a638f03000000b004769f0faab8si19716582pgd.740.2022.12.07.01.20.59; Wed, 07 Dec 2022 01:21:10 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229865AbiLGJJk (ORCPT + 77 others); Wed, 7 Dec 2022 04:09:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229700AbiLGJJ1 (ORCPT ); Wed, 7 Dec 2022 04:09:27 -0500 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51AB11A818; Wed, 7 Dec 2022 01:09:26 -0800 (PST) Received: from sslproxy05.your-server.de ([78.46.172.2]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1p2qQO-000K1N-K3; Wed, 07 Dec 2022 10:09:08 +0100 Received: from [85.1.206.226] (helo=linux.home) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2qQO-0009Hc-0Y; Wed, 07 Dec 2022 10:09:08 +0100 Subject: Re: [PATCH bpf-next] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno To: Andrii Nakryiko Cc: Xin Liu , andrii@kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, yanan@huawei.com, wuchangye@huawei.com, xiesongyang@huawei.com, kongweibin2@huawei.com, zhangmingyi5@huawei.com References: <20221203093740.218935-1-liuxin350@huawei.com> <6ac9f767-e7f5-6603-6234-97126ea22005@iogearbox.net> From: Daniel Borkmann Message-ID: <660fd781-2d86-9d99-2851-127d6b4d4595@iogearbox.net> Date: Wed, 7 Dec 2022 10:09:07 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.7/26743/Wed Dec 7 09:17:04 2022) X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On 12/7/22 1:00 AM, Andrii Nakryiko wrote: > On Mon, Dec 5, 2022 at 1:11 PM Daniel Borkmann wrote: >> >> On 12/3/22 10:37 AM, Xin Liu wrote: >>> This is a small improvement in libbpf_strerror. When libbpf_strerror >>> is used to obtain the system error description, if the length of the >>> buf is insufficient, libbpf_sterror returns ERANGE and sets errno to >>> ERANGE. >>> >>> However, this processing is not performed when the error code >>> customized by libbpf is obtained. Make some minor improvements here, >>> return -ERANGE and set errno to ERANGE when buf is not enough for >>> custom description. >>> >>> Signed-off-by: Xin Liu >>> --- >>> tools/lib/bpf/libbpf_errno.c | 6 ++++++ >>> 1 file changed, 6 insertions(+) >>> >>> diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c >>> index 96f67a772a1b..48ce7d5b5bf9 100644 >>> --- a/tools/lib/bpf/libbpf_errno.c >>> +++ b/tools/lib/bpf/libbpf_errno.c >>> @@ -54,10 +54,16 @@ int libbpf_strerror(int err, char *buf, size_t size) >>> >>> if (err < __LIBBPF_ERRNO__END) { >>> const char *msg; >>> + size_t msg_size; >>> >>> msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; >>> snprintf(buf, size, "%s", msg); >>> buf[size - 1] = '\0'; >>> + >>> + msg_size = strlen(msg); >>> + if (msg_size >= size) >>> + return libbpf_err(-ERANGE); >> >> Given this is related to libbpf_strerror_table[] where the error strings are known >> lets do compile-time error instead. All callers should pass in a buffer of STRERR_BUFSIZE >> size in libbpf. > > That sounds a bit too pessimistic?.. If the actual error message fits > in the buffer, why return -ERANGE just because theoretically some > error descriptions might fit? > > But I don't think we need to calculate strlen(). snprintf above > returns the number of bytes required to print a full string, even if > it was truncated. So just comparing snprintf's result to size should > be enough. I meant sth like below. For example if we were to shrink STRERR_BUFSIZE down to 32 for testing, you'd then get: # make libbpf_errno.o gcc -g -O2 -std=gnu89 -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-aliasing=3 -Wshadow -Wno-switch-enum -Werror -Wall -I. -I/home/darkstar/trees/bpf-next/tools/include -I/home/darkstar/trees/bpf-next/tools/include/uapi -fvisibility=hidden -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o libbpf_errno.o libbpf_errno.c libbpf_errno.c:27:31: error: initializer-string for array of chars is too long [-Werror] 27 | [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ libbpf_errno.c:27:31: note: (near initialization for ‘libbpf_strerror_table[2]’) libbpf_errno.c:31:29: error: initializer-string for array of chars is too long [-Werror] 31 | [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ libbpf_errno.c:31:29: note: (near initialization for ‘libbpf_strerror_table[7]’) libbpf_errno.c:34:31: error: initializer-string for array of chars is too long [-Werror] 34 | [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ libbpf_errno.c:34:31: note: (near initialization for ‘libbpf_strerror_table[10]’) libbpf_errno.c:37:30: error: initializer-string for array of chars is too long [-Werror] 37 | [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ libbpf_errno.c:37:30: note: (near initialization for ‘libbpf_strerror_table[13]’) cc1: all warnings being treated as errors make: *** [: libbpf_errno.o] Error 1 diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2a82f49ce16f..2e5df1624f79 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -265,8 +265,6 @@ static void pr_perm_msg(int err) buf); } -#define STRERR_BUFSIZE 128 - /* Copied from tools/perf/util/util.h */ #ifndef zfree # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c index 96f67a772a1b..2f03f861b8b6 100644 --- a/tools/lib/bpf/libbpf_errno.c +++ b/tools/lib/bpf/libbpf_errno.c @@ -21,7 +21,7 @@ #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) -static const char *libbpf_strerror_table[NR_ERRNO] = { +static const char libbpf_strerror_table[NR_ERRNO][STRERR_BUFSIZE] = { [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 377642ff51fc..d4dc4fe945a6 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -57,6 +57,8 @@ #define ELF64_ST_VISIBILITY(o) ((o) & 0x03) #endif +#define STRERR_BUFSIZE 128 + #define BTF_INFO_ENC(kind, kind_flag, vlen) \ ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)