Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp137024iob; Tue, 17 May 2022 21:30:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLU0otW2EnwHIRysLi1+TZTgRhVz1I8u9VwKRPR3Mc6C458UBFvGBXSqxAQnlerRNjtq/I X-Received: by 2002:a05:6a00:84e:b0:510:5fbc:7738 with SMTP id q14-20020a056a00084e00b005105fbc7738mr26119979pfk.86.1652848226478; Tue, 17 May 2022 21:30:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652848226; cv=none; d=google.com; s=arc-20160816; b=E3bf83ROweB+tvqrKT8mfXFUqelx1n33vEK2ziM+5aU7sgMKVzcVkw1S87v2TvVnGn rctfjCJD68YOtxIpmKd7fp+TAF68ab13oXojAvVLTb3yW/1lmGjMeRQzM8ukqBl8AsuI 1XUMyj2Rn98WBzVyEBGAOY8twVjEaaV4y2LJOA+X5MDVefs1+x/QSr/ZHRm6BulT9Dzz PHInfxUYLihu0EIXb2aaceBTxE53g6rcO48FL5vfu6ss3VzMmsWwPwcWlOlZCo8dWYIj W0CtBfiuqt/Rym4EqfubqqVghpBHqrK8rwOe3HwU+6QmHM3kVAZV391tnrKOmEFN0+kf WgtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=QtQtPhTJLYG+aEUxqlxne5kLy9fKIvX9XV2SEvh/QBc=; b=0K/yujIexOGlPtD08ecwUmtRkEJDMM2QvmcR14McNj/BUfpFmaKq3Dy5mbPndTsTgE nmbtYoUGiuM0k+2noXq4OuDxGyVUIREy9VgBYShE7FRjMUXPcuNT7eNBeUYl1iCMM3rJ 3xJVOc9qdBBwS0ewbJPKKsQnryxBPhlHJERmqk3A3VqBd6WAuktii7SC4Yg8u517BVmV T4TROCbSYmNkJ3YdpMrrFxx/jiz+pNI1DSFGRn2232KuT18Sb+eKoAmQ7JmYyVnwpRhw F/0dkWEoB1MIUyF0Pn/RuYMf1+WCUUK/JRGnvFEvRrd9Z39pxoG574qoNIDsvWLlDB6U wRVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UkI3ZVQr; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id t1-20020a056a00138100b0050df434f381si1951595pfg.39.2022.05.17.21.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 21:30:26 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UkI3ZVQr; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6D43E26119; Tue, 17 May 2022 20:51:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346744AbiEQM5d (ORCPT + 99 others); Tue, 17 May 2022 08:57:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233877AbiEQM5T (ORCPT ); Tue, 17 May 2022 08:57:19 -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 F29273C70B; Tue, 17 May 2022 05:57:17 -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 B1070B8187F; Tue, 17 May 2022 12:57:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F19DC34113; Tue, 17 May 2022 12:57:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1652792235; bh=IlTqF9xbixgS80kjZ5i3ygHg1P4Ovvouhn3bcvuv1sY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=UkI3ZVQrwGL42WAhfAYCrgQUj3szOBRzKGGqLOLUTdZWWjVHtbXjONSSW5a6myelv jufpLCBzKA28Eahta47EjXLGKyR30vmyvnE+7T/o+DAjiDpohSU0WprT8rDBgyvmJ2 j7Vjm8H8UVdeBz9AqMxNbHd4x6DktefIhvbfDTcWRoYBm2JDSxfrcXrZKaEbs1NnBq 1qFTp2/hJU9WaUGhHujoPJDA06ByAv2291SbJgMH8x7zc6SFMND4x+gzwMYHJK5J8O FCrzeX9N2Hys0811KNFWnyaBJp22X6RtVeCBpqIot+3Y5h/sBYyw2v9n06LcTQskCR j+knIXEfGac9g== Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id A904638E985; Tue, 17 May 2022 14:57:11 +0200 (CEST) From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= To: Zhengchao Shao , bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net, kuba@kernel.org, hawk@kernel.org, john.fastabend@gmail.com, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, kpsingh@kernel.org Cc: weiyongjun1@huawei.com, shaozhengchao@huawei.com, yuehaibing@huawei.com Subject: Re: [PATCH v2,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd In-Reply-To: <20220517112748.358295-1-shaozhengchao@huawei.com> References: <20220517112748.358295-1-shaozhengchao@huawei.com> X-Clacks-Overhead: GNU Terry Pratchett Date: Tue, 17 May 2022 14:57:11 +0200 Message-ID: <87tu9ob9lk.fsf@toke.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Zhengchao Shao writes: > Before detach the prog, we should check detach prog exist or not. > > Signed-off-by: Zhengchao Shao > --- > samples/bpf/xdp_fwd_user.c | 52 +++++++++++++++++++++++++++++++------- > 1 file changed, 43 insertions(+), 9 deletions(-) > > diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c > index 1828487bae9a..2294486ef10a 100644 > --- a/samples/bpf/xdp_fwd_user.c > +++ b/samples/bpf/xdp_fwd_user.c > @@ -47,17 +47,51 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name) > return err; > } > > -static int do_detach(int idx, const char *name) > +static int do_detach(int idx, const char *name, const char *prog_name) two 'name' arguments is a bit confusing; could we rename the parameters to 'ifindex', 'ifname' and 'app_name', then use 'prog_name' for the stack variable below instead of 'namepad'? > { > - int err; > + int err = 1; > + __u32 info_len, curr_prog_id; > + struct bpf_prog_info prog_info = {}; > + int prog_fd; > + char namepad[BPF_OBJ_NAME_LEN]; Reverse x-mas tree, please. > + > + if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) { > + printf("ERROR: bpf_xdp_query_id failed\n"); strerror(errno) might be nice to add to the error message, so users have an inkling as to why the call is failing (same below). > + return err; > + } > + > + if (!curr_prog_id) { > + printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n", > + xdp_flags, name); > + return err; > + } > > - err = bpf_xdp_detach(idx, xdp_flags, NULL); > - if (err < 0) > - printf("ERROR: failed to detach program from %s\n", name); > + info_len = sizeof(prog_info); > + prog_fd = bpf_prog_get_fd_by_id(curr_prog_id); > + if (prog_fd < 0 && errno == ENOENT) { Why the ENOENT check? This should error out regardless of the errno, no? > + printf("ERROR: bpf_prog_get_fd_by_id failed\n"); strerror(errno) > + return err; > + } > + > + err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len); > + if (err) { > + printf("ERROR: bpf_obj_get_info_by_fd failed\n"); strerror(errno) > + return err; > + } > + snprintf(namepad, sizeof(namepad), "%s_prog", prog_name); If the binary somehow gets renamed, this may overflow and you'll end up without a NULL byte terminating the string. So either check the input length first, or make sure to set the last byte to '\0' after this call... > + > + if (strcmp(prog_info.name, namepad)) { > + printf("ERROR: %s isn't attached to %s\n", prog_name, name); > + } else { > + err = bpf_xdp_detach(idx, xdp_flags, NULL); This call should be including an opts struct with the fd obtained above as the old_prog_fd (so make sure it wasn't swapped out since the check). > + if (err < 0) > + printf("ERROR: failed to detach program from %s\n", > + name); strerror(errno) -Toke