Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp6519364rwl; Thu, 29 Dec 2022 14:36:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXtKhusD9hFmqFUk/IMBYL7o/7lwhAD7D13O9qdTe6Haimp59kZ3IUhB+WE/oKI/R8KDDdGq X-Received: by 2002:a50:9f09:0:b0:46f:7453:c9b6 with SMTP id b9-20020a509f09000000b0046f7453c9b6mr26900890edf.8.1672353413894; Thu, 29 Dec 2022 14:36:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672353413; cv=none; d=google.com; s=arc-20160816; b=QUMMLqyut7QvkqF3OtAkYRmVn8C1NhhlRq4x+nQOA/WuYIyMnBJ1cJOWFllH/lRqHT TYuZQraduVzcBa3B8nhqZgdnCmuxg6JuD1pz+EgnKvAL4e5NEuOkcpxK2r7fc+jz5/Nr VN5hcs3sCvBAcqus0JKy5fBYYlRjduiYuu+MeFNeX3jBEB1lccXusslVngMeho0LYsWJ /b3iyXToSYj0FNqXH8wSC/51bXq23cS0ZJC+SbYhnCeZppgNPlZFmObmytUWl8o55xS7 q78EMowxuztYQgGbjfQS18rFES9dbTGHBgfjykYUccLMCmDaoCNd9EFcX4CtaPenO35c EKbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=YUR9lBWtgopswNBH5Ii+s9RjlfE8jrVHFGOZoG+ipZU=; b=WXF8879Z9RA9hN6CsyPu2CiXTRyP/HFesw+gZtNTWOlOtTnaRHSHOSesZtEQE1BLWM ViksBZtE4J+AuJWkaCMt8Ru1zUNH9ipUJ3jhjym/tt0WeFxP5Al/w6O1LSvolN3OAQTw VuwauJBDmiXQH3NcubjVL3hW1MzPu8ESq3pxmiQE2fOVmP8OBVinkagQvEu3y8zj/OsI vo5vCJAII7ovUwmWTNy5dB4ocP6/uHorE5HhGDQX6Mw7dxJshzWiJznSKHYf5kOwPrK9 KS3waV5ayZ5Qr1ySXPph08CT+DyVcEJSS3AlgZu9NBE9r9OlzEGF2YmiXRe1Jf0GQ5iE g21g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=VNzDd7dg; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z10-20020a056402274a00b00489ad2c4601si2840950edd.5.2022.12.29.14.36.39; Thu, 29 Dec 2022 14:36:53 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=VNzDd7dg; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234142AbiL2VoM (ORCPT + 61 others); Thu, 29 Dec 2022 16:44:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233924AbiL2VoK (ORCPT ); Thu, 29 Dec 2022 16:44:10 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E27B110061; Thu, 29 Dec 2022 13:44:09 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id u9so47832815ejo.0; Thu, 29 Dec 2022 13:44:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YUR9lBWtgopswNBH5Ii+s9RjlfE8jrVHFGOZoG+ipZU=; b=VNzDd7dgQakYLbIEbxJVE0KXx/QOusoi5AmJ6vF5gAqYxyyeWO2ZwmpAHEJSe7digb 35pB5OEVWv1P8obSC1sF7DZ0qTwgQhoxxZVTMxmhBcCpd4RTklYURueWI8yIFY1HD3LB pFYJiwTmVaDQhQseAtMjUYyqQfsadCopMobXziT1NiwiAQTAGLRR7l4pS850MKwCPy80 rwfMlUaub+/Bu84Q3fOlngyHedK8YRKy68FNY1OZC3FknJr+5vWEpmgb76HDA0V4bLeK AfoF/QUkQL0bB1//ttxw6WGzMaTqAj/CIf4oZz2bG2NZZjtV7skv7Y2uCKNSYyFdY13l Unng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YUR9lBWtgopswNBH5Ii+s9RjlfE8jrVHFGOZoG+ipZU=; b=NqoZ3Y5XVVEGg0MYcMUrzpe/hgMU6FGqTVmyXe3ufO+IsH2a82yKVWjV8tfwzeSDor o1xKUNsCYxOeAiEoEpU6RuUkZ9kcMDjOWw/KI0XqUcDJl0ce/XvRWZ//Wt+hFthfCKVS GUjmSx1vemdX3hAm+76WbVmFeeUCxfWjxksWciOsmQ6Fhq+1Xu0I3ee248m+KPpaY8J+ OKxy9u8a9GuokwP2TwhaNowSazwVRwewpgU9aOv89hXBn76vchf9bjW5cOkkH2EOsREz F1ZWyb7Ul0dWJnaAklDMNZL7kT3Qlm5daP81dPF5/+3PYZio16atTD27gvHnn0lDzTy1 NIeg== X-Gm-Message-State: AFqh2kpJwcS2L3n0gcJpgOwdBr/2PGYGj+MwdEx7zQJf+JlCwysn3Qmn aTaKqn97yIH8tuXhfTp3XiBBQhB0DoP76PTUsIM= X-Received: by 2002:a17:906:388:b0:7c1:1f2b:945f with SMTP id b8-20020a170906038800b007c11f2b945fmr1676696eja.302.1672350248384; Thu, 29 Dec 2022 13:44:08 -0800 (PST) MIME-Version: 1.0 References: <20221223133618.10323-1-liuxin350@huawei.com> In-Reply-To: <20221223133618.10323-1-liuxin350@huawei.com> From: Andrii Nakryiko Date: Thu, 29 Dec 2022 13:43:56 -0800 Message-ID: Subject: Re: [PATCH bpf-next] libbpf: fix errno is overwritten after being closed. To: Xin Liu Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net, 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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 Fri, Dec 23, 2022 at 5:36 AM Xin Liu wrote: > > In the ensure_good_fd function, if the fcntl function succeeds but > the close function fails, ensure_good_fd returns a normal fd and > sets errno, which may cause users to misunderstand. The close > failure is not a serious problem, and the correct FD has been > handed over to the upper-layer application. Let's restore errno here. > > Signed-off-by: Xin Liu > --- > tools/lib/bpf/libbpf_internal.h | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h > index 377642ff51fc..98333a6c38e9 100644 > --- a/tools/lib/bpf/libbpf_internal.h > +++ b/tools/lib/bpf/libbpf_internal.h > @@ -543,10 +543,9 @@ static inline int ensure_good_fd(int fd) > fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); > saved_errno = errno; > close(old_fd); > - if (fd < 0) { > + errno = saved_errno; > + if (fd < 0) > pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno); > - errno = saved_errno; pr_warn calls into user-provided callback, which can clobber errno, so `errno = saved_errno` should happen after pr_warn. With your change there is even higher chance of errno clobbering. Please send a follow up fix to unconditionally restore errno *after* pr_warn, thanks. > - } > } > return fd; > } > -- > 2.33.0 >