Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp3346397pxb; Mon, 6 Sep 2021 19:28:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmupJEX/gHoyRPtM17jsc3/ds2TSJGvj22frfkuebPxYGUumltivL0n7j3edpLarOcP5u/ X-Received: by 2002:a17:906:fa04:: with SMTP id lo4mr16475933ejb.560.1630981715478; Mon, 06 Sep 2021 19:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630981715; cv=none; d=google.com; s=arc-20160816; b=OY0OxcyChTVXTQOin7JEltqmFdKeocrMF4vlbMR6FCGsUQ03ZgX5NZKMi9XWRkosww r1DTB5F8C40EduqKhl9ietipJFY5U3x1ot9rkmr5dtbpVLetNY9tnnL8QqqXFy5X4cUr FVRNXdy6NuZjB/Jn2mmfNZgUy+qivcbHunv8NfhgifHDLQOVuhNwZzzXM3RDcWUUwD2y 5WkmVEvd3vzs1cBiVnBxDfA275+3nwKe+L0YlpSo6eY6HMN1g1wKpCS4DQWNrgOlc7uc SKYehzuMenSGeSwy4WEvJjWqop9i3qL++xWD5rp9fy9M3FKaxwyPOGT1DLOvf3d0kPtJ CPKA== 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; bh=Wag34i0mvHaNCgUONRntiQDWweufyQCEE0D3LbDF1CM=; b=KHFXGcAWypYKVSx9HEJAGFZO3Tgrg1i64OzLQUO+TNkQ47eKrrV2ni8/ByIr0vOK9J SbFdH+1jJpo6ZtMInaVtETqmmMpYq51XrYLLCsvYEafPvKGTPL9BslQ2s17O21Z+Vil9 ln3dGwXE7cIKmYpnE1OiHxqVyncDc5nlGEKg3hi7zvgN5XGPfjHdC1q/+eflGQFCkA+g RNN/qBAMiEkflNIWN4+H52C/VioMfx7ASoXtxnwfBEI9vnxgEYExesX+Mr6b3oyuaL+4 usr5Q7N2PAxUhpFtDUM5pM0POWYvIQD0jILHa9ONJ8noOgQlbTeQNZM2E+7mapj8yVOO WZsQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si10133746edo.6.2021.09.06.19.28.12; Mon, 06 Sep 2021 19:28:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240520AbhIGCHC (ORCPT + 99 others); Mon, 6 Sep 2021 22:07:02 -0400 Received: from mail-lj1-f182.google.com ([209.85.208.182]:33390 "EHLO mail-lj1-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233217AbhIGCHC (ORCPT ); Mon, 6 Sep 2021 22:07:02 -0400 Received: by mail-lj1-f182.google.com with SMTP id s12so14089102ljg.0; Mon, 06 Sep 2021 19:05:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Wag34i0mvHaNCgUONRntiQDWweufyQCEE0D3LbDF1CM=; b=f3xVvob51H2RgMzM9D6HraI7VM5O6sj5rUTO4Wh8Yjndw2vQNn0WTe5aWromSPNRar +CkU7D5XEBZPDzj5wSY3kpCawbABoLosfbMBkAOwVY/ggidIsHVt+V3JAlcrkHAlskx3 +H/EjGARwjufW4ko3UY7SbVxE2MQ2koABzg0WRz5U5BCZ1mWRZjM5T/FlNVtqyhAsq0X Z1Wvc1cuzieQt6mP4eXZL1gybR7rLxfxL4RxEnUAyjLpx2tTJLY69ho2Yq8PttrYwI4+ BDvcgp89GjT49ZWrSqXJZQ70Y8VA/Y0wvbUWxfAW0I8XT3wwo8Jk8X+D8OiysTopB5da 9aXg== X-Gm-Message-State: AOAM530Zx1RJuln/a3xkZCCkezC8yZGqk/wiDxujz2nchOXTSdPFrjgC qnZAiRjy1QA5h54H5RAkUgOHoEgedeVUIVICMJk= X-Received: by 2002:a2e:90d6:: with SMTP id o22mr13297103ljg.366.1630980355363; Mon, 06 Sep 2021 19:05:55 -0700 (PDT) MIME-Version: 1.0 References: <20210906160310.54831-1-mailhol.vincent@wanadoo.fr> <20210906160310.54831-2-mailhol.vincent@wanadoo.fr> In-Reply-To: <20210906160310.54831-2-mailhol.vincent@wanadoo.fr> From: Vincent MAILHOL Date: Tue, 7 Sep 2021 11:05:42 +0900 Message-ID: Subject: Re: [PATCH v3 1/2] can: netlink: prevent incoherent can configuration in case of early return To: Marc Kleine-Budde , linux-can Cc: netdev , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue. 7 Sep 2021 at 01:03, Vincent Mailhol wrote: > struct can_priv has a set of flags (can_priv::ctrlmode) which are > correlated with the other fields of the structure. In > can_changelink(), those flags are set first and copied to can_priv. If > the function has to return early, for example due to an out of range > value provided by the user, then the global configuration might become > incoherent. > > Example: the user provides an out of range dbitrate (e.g. 20 > Mbps). The command fails (-EINVAL), however the FD flag was already > set resulting in a configuration where FD is on but the databittiming > parameters are empty. > > * Illustration of above example * > > | $ ip link set can0 type can bitrate 500000 dbitrate 20000000 fd on > | RTNETLINK answers: Invalid argument > | $ ip --details link show can0 > | 1: can0: mtu 72 qdisc noop state DOWN mode DEFAULT group default qlen 10 > | link/can promiscuity 0 minmtu 0 maxmtu 0 > | can state STOPPED restart-ms 0 > ^^ FD flag is set without any of the databittiming parameters... > | bitrate 500000 sample-point 0.875 > | tq 12 prop-seg 69 phase-seg1 70 phase-seg2 20 sjw 1 > | ES582.1/ES584.1: tseg1 2..256 tseg2 2..128 sjw 1..128 brp 1..512 brp-inc 1 > | ES582.1/ES584.1: dtseg1 2..32 dtseg2 1..16 dsjw 1..8 dbrp 1..32 dbrp-inc 1 > | clock 80000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 > > To prevent this from happening, we do a local copy of can_priv, work > on it, an copy it at the very end of the function (i.e. only if all > previous checks succeeded). > > Once this done, there is no more need to have a temporary variable for > a specific parameter. As such, the bittiming and data bittiming (bt > and dbt) are directly written to the temporary priv variable. > > > N.B. The temporary can_priv is too big to be allocated on the stack > because, on x86_64 sizeof(struct can_priv) is 448 and: > > | $ objdump -d drivers/net/can/dev/netlink.o | ./scripts/checkstack.pl > | 0x00000000000002100 can_changelink []: 1200 > > > Fixes: 9859ccd2c8be ("can: introduce the data bitrate configuration for CAN FD") > Signed-off-by: Vincent Mailhol > --- > drivers/net/can/dev/netlink.c | 32 ++++++++++++++++++-------------- > 1 file changed, 18 insertions(+), 14 deletions(-) > > diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c > index 80425636049d..21b76ca8cb22 100644 > --- a/drivers/net/can/dev/netlink.c > +++ b/drivers/net/can/dev/netlink.c > @@ -58,14 +58,19 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[], > struct nlattr *data[], > struct netlink_ext_ack *extack) > { > - struct can_priv *priv = netdev_priv(dev); > + /* Work on a local copy of priv to prevent inconsistent value > + * in case of early return. > + */ > + static struct can_priv *priv; > int err; > > /* We need synchronization with dev->stop() */ > ASSERT_RTNL(); > > + priv = kmemdup(netdev_priv(dev), sizeof(*priv), GFP_KERNEL); Arg... I forgot to check the return value. + if (!priv) + return -ENOMEM; I will send a v4, sorry for the noise. Yours sincerely, Vincent