Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2006653rwn; Fri, 16 Sep 2022 04:27:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4HLOFm0d1fjYOxLPfj4f8QXP13S0PMlYn9zGGX26gIbeQCGE0Q3NWQqpYAjkuL3tvG9xCk X-Received: by 2002:a05:6402:34c2:b0:44f:322f:f0de with SMTP id w2-20020a05640234c200b0044f322ff0demr3615317edc.297.1663327630526; Fri, 16 Sep 2022 04:27:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663327630; cv=none; d=google.com; s=arc-20160816; b=ZtV3+CglxucH9T+oeaFjET8koiWLndI7nRobYZ3joNeeYbmYT4usSLopcuOO1fd5NJ vy7b5x9o376GIbfjpEoGSLqgfTvv2HDG3Q0V1GstIKOxb3m5Qazp/72R075bm9BHCQOL VBGFVfNrnUER40b3Hn037i2LmoDPIo97FjUsXGJfh0u6mWr/a/hfDGm+jcCxtiA4D/G/ xY3Wdho9iJDlx6f35F///MfuxBzDylmxrKfGP5OgABCcvTIHWS1ikWE2/tBa/qSQbQso TrxrB5jyDV6MO5fjt9xSNN1Ra22rNCPIlA9Smn0MZ1sg4a4q9Lt62iW9X3OB0Yzd5KH0 j4Kw== 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=+c+X3n3nDrK8nHpE4DKFcOu/omHWgHqfEgCTLvi6yLY=; b=A67BZ0+k4kwtarA0Q0sVwz+Tu3zHpL0q/Q8Ux+g91y4VH9hVS9a3KIvxCz5UbOEmT/ hYoCTrCID6AQXq8aIGc9UvMBlZzeVj5qNqQFbPVm+vrnd8SeQfvTpKs6Tgbtx6G/sVcW EqOK7KLDAouyLJ1HAOf26MAkukoHlFOsBaXZ0CHNvAK62E7iy6D4fsLYB3tOHzmPLVVv kwlg8mVRqrlltixULVnVpmLGKb6MfjLcr3xJZgZewDZRNvWZ5JFuS3D1aSWzZqnqrNyX /6oYsIhBbBWrgu428+IPTwNMSqUACjuR06JmYh1DtJsCGKKd2BSLcg7LJKnwrZOzl9h5 REMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Em1kEGk2; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p5-20020a170906784500b00741550f828bsi14187568ejm.919.2022.09.16.04.26.42; Fri, 16 Sep 2022 04:27:10 -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=@redhat.com header.s=mimecast20190719 header.b=Em1kEGk2; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbiIPLNb (ORCPT + 99 others); Fri, 16 Sep 2022 07:13:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbiIPLN3 (ORCPT ); Fri, 16 Sep 2022 07:13:29 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C07F316 for ; Fri, 16 Sep 2022 04:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663326801; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+c+X3n3nDrK8nHpE4DKFcOu/omHWgHqfEgCTLvi6yLY=; b=Em1kEGk2/mYlcNUxKH7BADJcJMcLq9HKOHDhz9/E1v1CI0iYVCeJu3s+srwipIifoO6LCJ aonl6lqYPUmHykCEKkCJJpwFr3W/SsnO6/+c7Bq/+crRABfaU26BnysbXLmecDVFbTqXuG FEdiIweAFMNbxE89rYKkYRlocnK5P48= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-556-H6694pYoMH66fxOj--oSrg-1; Fri, 16 Sep 2022 07:13:20 -0400 X-MC-Unique: H6694pYoMH66fxOj--oSrg-1 Received: by mail-wm1-f69.google.com with SMTP id v128-20020a1cac86000000b003b33fab37e8so11023986wme.0 for ; Fri, 16 Sep 2022 04:13:19 -0700 (PDT) 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; bh=+c+X3n3nDrK8nHpE4DKFcOu/omHWgHqfEgCTLvi6yLY=; b=H4QOInGBXF4k3MywaW8qCnIDJJGPr0fWEnEtzw5t6hdt7fpp/gy5IqAXpPQ7lIzBxi 9/2tDkwb7HST7m01VyI9HncEDdIBsKLqKsAmw/PUIRcNqP9AodSaUUtQNfSt2Au2Kp+L ZGWuAaayxdLNIRribOejb/WOljxP7KjtfBIiGhTX6iPjsATk0BnLbzaS36WLDHXuavG0 zyar//JDl8GnSHQkbFDxSrp5JyfGb97CJnuZsy04l3ErnIHmHUOsBbomn6sd2XEWtzZw ShleD+YOXn6ZcjckQU5S98uZNUKe9RQBE9beObTNTbFlZuw1fJ/jBbDw5VEVpBlB0YyO 6dfg== X-Gm-Message-State: ACrzQf1AfuL+Csg400nnvY1p6/c8fsvBt5JRKLu6/usesuuZM5xpmsii YAsNqhQrpj7D+qYZ5v45s97fhiRALqBcmIStjbLc1luX0Z2TRwo/2RtC0qQPIpIKxzW6zPo1pDP DFs+amOIlOUfPZDC7+uFXGUPsL5tpw4yCRgGm3V8c X-Received: by 2002:a5d:6390:0:b0:22a:4831:e35 with SMTP id p16-20020a5d6390000000b0022a48310e35mr2436279wru.88.1663326798829; Fri, 16 Sep 2022 04:13:18 -0700 (PDT) X-Received: by 2002:a5d:6390:0:b0:22a:4831:e35 with SMTP id p16-20020a5d6390000000b0022a48310e35mr2436256wru.88.1663326798543; Fri, 16 Sep 2022 04:13:18 -0700 (PDT) MIME-Version: 1.0 References: <20220908121927.3074843-1-tcs_kernel@tencent.com> In-Reply-To: <20220908121927.3074843-1-tcs_kernel@tencent.com> From: Alexander Aring Date: Fri, 16 Sep 2022 07:13:07 -0400 Message-ID: Subject: Re: [PATCH V3] net/ieee802154: fix uninit value bug in dgram_sendmsg To: Haimin Zhang Cc: alex.aring@gmail.com, stefan@datenfreihafen.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, linux-wpan@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Haimin Zhang Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE 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 Hi, just something that we should fix in the future... On Thu, Sep 8, 2022 at 8:19 AM Haimin Zhang wrote: > > There is uninit value bug in dgram_sendmsg function in > net/ieee802154/socket.c when the length of valid data pointed by the > msg->msg_name isn't verified. > > We introducing a helper function ieee802154_sockaddr_check_size to > check namelen. First we check there is addr_type in ieee802154_addr_sa. > Then, we check namelen according to addr_type. > > Also fixed in raw_bind, dgram_bind, dgram_connect. > > Signed-off-by: Haimin Zhang > --- > include/net/ieee802154_netdev.h | 37 +++++++++++++++++++++++++++++ > net/ieee802154/socket.c | 42 ++++++++++++++++++--------------- > 2 files changed, 60 insertions(+), 19 deletions(-) > > diff --git a/include/net/ieee802154_netdev.h b/include/net/ieee802154_netdev.h > index d0d188c32..a8994f307 100644 > --- a/include/net/ieee802154_netdev.h > +++ b/include/net/ieee802154_netdev.h > @@ -15,6 +15,22 @@ > #ifndef IEEE802154_NETDEVICE_H > #define IEEE802154_NETDEVICE_H > > +#define IEEE802154_REQUIRED_SIZE(struct_type, member) \ > + (offsetof(typeof(struct_type), member) + \ > + sizeof(((typeof(struct_type) *)(NULL))->member)) > + > +#define IEEE802154_ADDR_OFFSET \ > + offsetof(typeof(struct sockaddr_ieee802154), addr) > + > +#define IEEE802154_MIN_NAMELEN (IEEE802154_ADDR_OFFSET + \ > + IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, addr_type)) > + > +#define IEEE802154_NAMELEN_SHORT (IEEE802154_ADDR_OFFSET + \ > + IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, short_addr)) > + > +#define IEEE802154_NAMELEN_LONG (IEEE802154_ADDR_OFFSET + \ > + IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, hwaddr)) > + > #include > #include > #include > @@ -165,6 +181,27 @@ static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr) > memcpy(raw, &temp, IEEE802154_ADDR_LEN); > } > > +static inline int > +ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 *daddr, int len) > +{ > + struct ieee802154_addr_sa *sa; > + > + sa = &daddr->addr; > + if (len < IEEE802154_MIN_NAMELEN) > + return -EINVAL; > + switch (sa->addr_type) { > + case IEEE802154_ADDR_SHORT: > + if (len < IEEE802154_NAMELEN_SHORT) > + return -EINVAL; > + break; > + case IEEE802154_ADDR_LONG: > + if (len < IEEE802154_NAMELEN_LONG) > + return -EINVAL; > + break; > + } There is a missing IEEE802154_ADDR_NONE here. If it's NONE the size is correct, although all other possible values which are not SHORT, LONG or NONE, should here end in an -EINVAL as well. In those cases the size is "undefined" for now. > + return 0; > +} > + > static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a, > const struct ieee802154_addr_sa *sa) > { > diff --git a/net/ieee802154/socket.c b/net/ieee802154/socket.c > index 718fb77bb..7889e1ef7 100644 > --- a/net/ieee802154/socket.c > +++ b/net/ieee802154/socket.c > @@ -200,8 +200,9 @@ static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len) > int err = 0; > struct net_device *dev = NULL; > > - if (len < sizeof(*uaddr)) > - return -EINVAL; > + err = ieee802154_sockaddr_check_size(uaddr, len); > + if (err < 0) > + return err; > > uaddr = (struct sockaddr_ieee802154 *)_uaddr; > if (uaddr->family != AF_IEEE802154) > @@ -493,7 +494,8 @@ static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len) > > ro->bound = 0; > > - if (len < sizeof(*addr)) > + err = ieee802154_sockaddr_check_size(addr, len); > + if (err < 0) > goto out; > > if (addr->family != AF_IEEE802154) > @@ -564,8 +566,9 @@ static int dgram_connect(struct sock *sk, struct sockaddr *uaddr, > struct dgram_sock *ro = dgram_sk(sk); > int err = 0; > > - if (len < sizeof(*addr)) > - return -EINVAL; > + err = ieee802154_sockaddr_check_size(addr, len); > + if (err < 0) > + return err; > > if (addr->family != AF_IEEE802154) > return -EINVAL; > @@ -604,6 +607,7 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) > struct ieee802154_mac_cb *cb; > struct dgram_sock *ro = dgram_sk(sk); > struct ieee802154_addr dst_addr; > + DECLARE_SOCKADDR(struct sockaddr_ieee802154*, daddr, msg->msg_name); > int hlen, tlen; > int err; > > @@ -612,10 +616,20 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) > return -EOPNOTSUPP; > } > > - if (!ro->connected && !msg->msg_name) > - return -EDESTADDRREQ; > - else if (ro->connected && msg->msg_name) > - return -EISCONN; > + if (msg->msg_name) { > + if (ro->connected) > + return -EISCONN; > + if (msg->msg_namelen < IEEE802154_MIN_NAMELEN) > + return -EINVAL; nitpick, we do this in ieee802154_sockaddr_check_size() as well? > + err = ieee802154_sockaddr_check_size(daddr, msg->msg_namelen); > + if (err < 0) > + return err; > + ieee802154_addr_from_sa(&dst_addr, &daddr->addr); > + } else { > + if (!ro->connected) > + return -EDESTADDRREQ; I'm not sure about this change but it looks okay to me. That's it. I am sorry for the delay... I usually schedule my review for the weekend if I can't do it then the next weekend... - Alex