Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3978897rwb; Sun, 9 Oct 2022 15:10:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6IqfqKVMk7BpS+sQ7VgBs7xfjKtGs+i0UmbvVz+O2gypr5ZO8/WXn9pjEqTsNyRkHwt8Rg X-Received: by 2002:a17:907:9611:b0:78d:bb06:90a3 with SMTP id gb17-20020a170907961100b0078dbb0690a3mr501423ejc.233.1665353459523; Sun, 09 Oct 2022 15:10:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665353459; cv=none; d=google.com; s=arc-20160816; b=qnrVuk5YR2fH7UYzKqrcvsYCyiZFp98fVn6eGkASkZyitiRzeHIY/L9tAwO1xScYGA pwmKLOiZbd9WB0xlzZScvz2RB6VMmm19lJs5ejkqf1alhN3RnDqNw9CyMX4E1F2yqWAq SsKOCMt661w0/vNTyrBe6uueENSOWeGHzKajvSA6xLX85EB6CvZx26JfREf8QCfkFi+W s1dbjQJFqN/FXmL1XDukfrNvVNvhU+6eocEsypNkH5TlnTbmE+9WW7bShJuJ9xIHPiTP p6my69ceLntfAbt58MofEW29H53hgshfeCqE3sgtOqxLVg+Ifu1/rWJ1yzbBAwQ1Lok4 bAaQ== 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:message-id:date:subject:cc:to:from :dkim-signature; bh=bS7GMCwTOsRT8Iyvlthz16Lg0wMkLiPypLN2pT9/fqk=; b=Rw6QCkXzOKppkiMvSOc16T2l7qxrVf7nT0SPKCwaS/1t8KdXF+76bdrIMRD+LCm1E+ AEV3UNscVbbdJbKmYQEZlHmNWmVZjwpZLolzs4ZtdUdjlsuHB4uY2gg8ZiV8UxgCKLvT 2l/vzRTMEqb+rZ8JLRHgreTXzCuVoFoOXPPvJhjzCir//0WBeSAnv0zIOU/tOPUmkp4n Z+h0243SKXeGrbCd9OZ6te8cvRDBNPADZw1A9hIAMEvziRWXjgZAM6zxDwALzZS5iVl9 eedamclVdxi4VvGm32SHJcnV5RGujWnSCoz8RtV2gLFDzijMnxhzEFpmD+ub6EhsyPPh BfOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PvDxvORL; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ky10-20020a170907778a00b00767e24156dbsi8584493ejc.256.2022.10.09.15.10.33; Sun, 09 Oct 2022 15:10:59 -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=@kernel.org header.s=k20201202 header.b=PvDxvORL; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230503AbiJIWIz (ORCPT + 99 others); Sun, 9 Oct 2022 18:08:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230301AbiJIWI0 (ORCPT ); Sun, 9 Oct 2022 18:08:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF396286F5; Sun, 9 Oct 2022 15:08:16 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3E675B80DCD; Sun, 9 Oct 2022 22:08:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08B51C4347C; Sun, 9 Oct 2022 22:08:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665353292; bh=x0O9W2XoIIMXYnl74aXnnkx0BzJcyvrdEjk6w1tkkfU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PvDxvORL3SEUcTpC+b9eQro8ENXw36/obFpCfRJrkUVdMmpi1hp5dQkyIPdgxZOBf 0DVYg3plWE2sJXJLnX4WZXoSVCvXQswBO3T2q9/jszJ2Bm6JL6JUxK8dQCBA1sbf7b kfYZb3Sx8u4MHblWIqsK3QxFGGRUi+Tp8/g9cdfThJjhxqcSGfMvrX3/NKoZ3ExqCu /y35aVY6fbjbU0TxgYgMJpWrG6EKeRVjujx0dSxOyWAImVAlziVxxIqap0LMQTYODC 0u0gd/yCKkZhiJupywF1Gqq+y4KnYkBwcLgXViBSQE05onus2aXdR9R6DWsSmhC7x4 8rB97dMxP49PA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Quentin Monnet , Daniel Borkmann , Sasha Levin , ast@kernel.org, andrii@kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 6.0 06/77] bpftool: Clear errno after libcap's checks Date: Sun, 9 Oct 2022 18:06:43 -0400 Message-Id: <20221009220754.1214186-6-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221009220754.1214186-1-sashal@kernel.org> References: <20221009220754.1214186-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Quentin Monnet [ Upstream commit cea558855c39b7f1f02ff50dcf701ca6596bc964 ] When bpftool is linked against libcap, the library runs a "constructor" function to compute the number of capabilities of the running kernel [0], at the beginning of the execution of the program. As part of this, it performs multiple calls to prctl(). Some of these may fail, and set errno to a non-zero value: # strace -e prctl ./bpftool version prctl(PR_CAPBSET_READ, CAP_MAC_OVERRIDE) = 1 prctl(PR_CAPBSET_READ, 0x30 /* CAP_??? */) = -1 EINVAL (Invalid argument) prctl(PR_CAPBSET_READ, CAP_CHECKPOINT_RESTORE) = 1 prctl(PR_CAPBSET_READ, 0x2c /* CAP_??? */) = -1 EINVAL (Invalid argument) prctl(PR_CAPBSET_READ, 0x2a /* CAP_??? */) = -1 EINVAL (Invalid argument) prctl(PR_CAPBSET_READ, 0x29 /* CAP_??? */) = -1 EINVAL (Invalid argument) ** fprintf added at the top of main(): we have errno == 1 ./bpftool v7.0.0 using libbpf v1.0 features: libbfd, libbpf_strict, skeletons +++ exited with 0 +++ This has been addressed in libcap 2.63 [1], but until this version is available everywhere, we can fix it on bpftool side. Let's clean errno at the beginning of the main() function, to make sure that these checks do not interfere with the batch mode, where we error out if errno is set after a bpftool command. [0] https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/libcap/cap_alloc.c?h=libcap-2.65#n20 [1] https://git.kernel.org/pub/scm/libs/libcap/libcap.git/commit/?id=f25a1b7e69f7b33e6afb58b3e38f3450b7d2d9a0 Signed-off-by: Quentin Monnet Signed-off-by: Daniel Borkmann Link: https://lore.kernel.org/bpf/20220815162205.45043-1-quentin@isovalent.com Signed-off-by: Sasha Levin --- tools/bpf/bpftool/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 451cefc2d0da..ccd7457f92bf 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -435,6 +435,16 @@ int main(int argc, char **argv) setlinebuf(stdout); +#ifdef USE_LIBCAP + /* Libcap < 2.63 hooks before main() to compute the number of + * capabilities of the running kernel, and doing so it calls prctl() + * which may fail and set errno to non-zero. + * Let's reset errno to make sure this does not interfere with the + * batch mode. + */ + errno = 0; +#endif + last_do_help = do_help; pretty_output = false; json_output = false; -- 2.35.1