Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp394535rwo; Tue, 1 Aug 2023 19:47:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlEED15RYZ78UMzB2+tr9ty5Znh9luX7gmnJ81DBzx7mjAPjs9LQr4dWiqp/T7nk0KvK69ai X-Received: by 2002:a17:90a:9dc8:b0:267:fe44:86c3 with SMTP id x8-20020a17090a9dc800b00267fe4486c3mr12560795pjv.31.1690944432456; Tue, 01 Aug 2023 19:47:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690944432; cv=none; d=google.com; s=arc-20160816; b=bBqURKSZY41vnXKTkAHQFAniJVU0GzSOlfeFB7Ey/PE1XVdF5BGp1PzGP2Qi1MoCrB BtoEskYxM4bWPu6UlUNkrzSyZytod3g8+qhPdbR2ux1aLxhM20lTo30Rk0y2h9WNRyTY CIPPHYE9QRtUF1RIhubi6SxrRVGSkMpFIR4AJ5qQF6Ns7204S//NgfNmWuB5D/IjzXtA HO/1Mof9RidTJfX2S1f2+iaVonUFQNcxAk7vfzmgm39EF5m0LwPbSx2mxG3bD8XDdAP+ QrVjCjX9xZ4O7q5yYI4fPTXIGmoO8fB7nVs3sBGc9QFbZoGUvttwuMNxAHaX/fj672ty WdiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=PZp8gGX8VEAsqKDOOD7UUWfIiEopULNUuDVfReWbP/Q=; fh=tM/NriDG/81zxaK/vgTBS8PIS1+5e/eDQBTvcYVXTiU=; b=EVy2s3ns2MfWUD+SkgBvntORUlP/Dc8z2IHU1cl7RdsUYnbMlaQHLb1ggWbuNQIGsn Tqhy5avI4tkJGk3/WNerfJsuxfAhubqqgwOiIidEa0sh8hkpSsqqe9hrvWbxtbjpnwg2 22L9JLzL/2eN9wwb5Kffm2JpxMYG69Rewl98NNeC5QRBR2wHEG3ATchEi+QB0Ba3OGzm yz3D/VIuO6Q+t0qiTrtYPXE4HiVy81KMjmyM7npeeTp5NfcYgnTzpQHYAXIsYz6t8bwP B3mqyraTqYYLL+/6wKplbbXs8emoGb7f0E2wTa6UoT+5NHd9Q56J4jAhgIex6/UqlgNm 8bkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=1q71HHUK; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m8-20020a17090a4d8800b0025979e8c246si391409pjh.70.2023.08.01.19.47.01; Tue, 01 Aug 2023 19:47:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-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=@google.com header.s=20221208 header.b=1q71HHUK; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231201AbjHBB0T (ORCPT + 58 others); Tue, 1 Aug 2023 21:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231396AbjHBB0P (ORCPT ); Tue, 1 Aug 2023 21:26:15 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6593D2D40 for ; Tue, 1 Aug 2023 18:26:10 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-52229f084beso9520529a12.2 for ; Tue, 01 Aug 2023 18:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690939569; x=1691544369; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=PZp8gGX8VEAsqKDOOD7UUWfIiEopULNUuDVfReWbP/Q=; b=1q71HHUKiqR1FZENTwLvFqYeCIaQLsqKPtWBWBQlMtZsIPkCBvguA2x0lduxepKNNP jjXZzVyRjxa0g9xcACvqNxKbgOd6R9/gMHmR96AqsxCfNC+znhC2mgjByxbic7Js2y2h /q2vFIvQRpSolDisYtzmNCX9BSXsELfv6HQzRjYilUoM6XSQSstVf4k6/U79AgiDqodg o6moxHpljTVDVKduNk65zv/s7VDXwUoGJ7LxUDgBoa3OZohpsZxxnkoY8fLEBCKwuY/H nCiTedISjM2482iRM993JM5Z/vKnEgecyKTm7gmiU+5IL4bMt9POAF3pUCaFOHSMmM8y nShA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690939569; x=1691544369; h=content-transfer-encoding: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=PZp8gGX8VEAsqKDOOD7UUWfIiEopULNUuDVfReWbP/Q=; b=RlioKFQWO9q+bC16AWrdSEc1q4g1RFSXIleq95fUMEPYPs2XGJ9YS+H5/9pNO0M7g+ o5THbB/GNaIMnwp8maMrbLxo9ZnL7b06VksBi89VHEUNqDMQQxRmAgey9u+B3Eq4/TB5 aV5gn+QrYHWDjokHQyVWbDTxXMhSPV9BXaV2/8fFqjX40r9MnbzXUGian9ZSyhvHBt0E xjV0GWZVY2XXSzebxsjMsCwkKf9WsDEGYRVU3SprpNg4uxxmSOJLIoy9Hh/BDPsGp6jU ejKkFa+GJkliH36xoLaQADh0qZK28pHy+L1qMZOcztWi2tbwpvd9HYQYJF4d8OPyWimH a0dA== X-Gm-Message-State: ABy/qLZ0tuZQD9Y16BH5LlUNur9UU0SkDUm/bErt8VU6IMYI4k0YFDkr N2rTEPBcwHPlggfkorLr8Ob2Lmb+3NEldGz5jZKgFg== X-Received: by 2002:a05:6402:8d5:b0:51d:d4c3:6858 with SMTP id d21-20020a05640208d500b0051dd4c36858mr3814153edz.12.1690939568796; Tue, 01 Aug 2023 18:26:08 -0700 (PDT) MIME-Version: 1.0 References: <20230802-wifi-ipw2x00-refactor-v1-1-6047659410d4@google.com> <86B2408D-BB52-4B29-BE4A-A6A12A129121@kernel.org> In-Reply-To: <86B2408D-BB52-4B29-BE4A-A6A12A129121@kernel.org> From: Justin Stitt Date: Tue, 1 Aug 2023 18:25:55 -0700 Message-ID: Subject: Re: [PATCH] wifi: ipw2x00: refactor to use kstrtoul To: Kees Cook Cc: Stanislav Yakovlev , Kalle Valo , Kees Cook , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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-wireless@vger.kernel.org On Tue, Aug 1, 2023 at 6:16=E2=80=AFPM Kees Cook wrote: > > On August 1, 2023 5:51:59 PM PDT, Justin Stitt w= rote: > >The current implementation seems to reinvent what `kstrtoul` already doe= s > >in terms of functionality and error handling. Remove uses of `simple_str= toul()` > >in favor of `kstrtoul()`. > > > >There is the following note at `lib/vsprintf.c:simple_strtoull()` which > >further backs this change: > >| * This function has caveats. Please use kstrtoull (or kstrtoul) instea= d. > > > >And here, simple_str* are explicitly deprecated [3]. > > > >This patch also removes an instance of the deprecated `strncpy` which he= lps [2]. > > > >Link: https://lore.kernel.org/all/202308011602.3CC1C0244C@keescook/ [1] > >Link: https://github.com/KSPP/linux/issues/90 [2] > >Link: https://docs.kernel.org/process/deprecated.html#simple-strtol-simp= le-strtoll-simple-strtoul-simple-strtoull [3] > >Cc: linux-hardening@vger.kernel.org > >Suggested-by: Kees Cook > >Signed-off-by: Justin Stitt > >--- > > > > > >Link: https://lore.kernel.org/all/20230801-drivers-net-wireless-intel-ip= w2x00-v1-1-ffd185c91292@google.com/ > >--- > > drivers/net/wireless/intel/ipw2x00/ipw2200.c | 43 +++++++++------------= ------- > > 1 file changed, 14 insertions(+), 29 deletions(-) > > > >diff --git a/drivers/net/wireless/intel/ipw2x00/ipw2200.c b/drivers/net/= wireless/intel/ipw2x00/ipw2200.c > >index dfe0f74369e6..ac10633f593e 100644 > >--- a/drivers/net/wireless/intel/ipw2x00/ipw2200.c > >+++ b/drivers/net/wireless/intel/ipw2x00/ipw2200.c > >@@ -1176,23 +1176,20 @@ static ssize_t debug_level_show(struct device_dr= iver *d, char *buf) > > static ssize_t debug_level_store(struct device_driver *d, const char *b= uf, > > size_t count) > > { > >- char *p =3D (char *)buf; > >- u32 val; > >+ unsigned long *val =3D NULL; > > > >- if (p[1] =3D=3D 'x' || p[1] =3D=3D 'X' || p[0] =3D=3D 'x' || p[0]= =3D=3D 'X') { > >- p++; > >- if (p[0] =3D=3D 'x' || p[0] =3D=3D 'X') > >- p++; > >- val =3D simple_strtoul(p, &p, 16); > >- } else > >- val =3D simple_strtoul(p, &p, 10); > >- if (p =3D=3D buf) > >+ int result =3D kstrtoul(buf, 0, val); > > kstrtoul needs somewhere to write the value, so val need to be actually u= nsigned long, and a pointer passed to that: > > unsigned long val; > ... > ... kstrtoul(but, 0, &val); > > But otherwise, yeah, this looks like the right direction to me. > > >+ > >+ if (result =3D=3D -EINVAL) > > printk(KERN_INFO DRV_NAME > > ": %s is not in hex or decimal form.\n", buf); > >+ else if (result =3D=3D -ERANGE) > >+ printk(KERN_INFO DRV_NAME > >+ ": %s has overflowed.\n", buf); > > else > >- ipw_debug_level =3D val; > >+ ipw_debug_level =3D *val; > > > >- return strnlen(buf, count); > >+ return count;. > > It might be worth mentioning this return value change, but I think it's c= orrect: we're communicating how much was consumed (we consumed it all). Whe= n the return value !=3D count, this function may be called again with the "= rest" of the input. As this is a sysfs interface, that kind of behavior is = very rare bordering on actively unwanted. :) So, I think these should eithe= r return a negative error or count. > > -Kees > > > } > > static DRIVER_ATTR_RW(debug_level); > > > >@@ -1461,33 +1458,21 @@ static ssize_t scan_age_store(struct device *d, = struct device_attribute *attr, > > { > > struct ipw_priv *priv =3D dev_get_drvdata(d); > > struct net_device *dev =3D priv->net_dev; > >- char buffer[] =3D "00000000"; > >- unsigned long len =3D > >- (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1; > >- unsigned long val; > >- char *p =3D buffer; > > > > IPW_DEBUG_INFO("enter\n"); > > > >- strncpy(buffer, buf, len); > >- buffer[len] =3D 0; > >+ unsigned long *val =3D NULL; > >+ int result =3D kstrtoul(buf, 0, val); > > > >- if (p[1] =3D=3D 'x' || p[1] =3D=3D 'X' || p[0] =3D=3D 'x' || p[0]= =3D=3D 'X') { > >- p++; > >- if (p[0] =3D=3D 'x' || p[0] =3D=3D 'X') > >- p++; > >- val =3D simple_strtoul(p, &p, 16); > >- } else > >- val =3D simple_strtoul(p, &p, 10); > >- if (p =3D=3D buffer) { > >+ if (result =3D=3D -EINVAL || result =3D=3D -ERANGE) { > > IPW_DEBUG_INFO("%s: user supplied invalid value.\n", dev-= >name); > > } else { > >- priv->ieee->scan_age =3D val; > >+ priv->ieee->scan_age =3D *val; > > IPW_DEBUG_INFO("set scan_age =3D %u\n", priv->ieee->scan_= age); > > } > > > > IPW_DEBUG_INFO("exit\n"); > >- return len; > >+ return count; > > } > > > > static DEVICE_ATTR_RW(scan_age); > > > >--- > >base-commit: 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4 > >change-id: 20230801-wifi-ipw2x00-refactor-fa6deb6c67ea > > > >Best regards, > >-- > >Justin Stitt > > > > > -- > Kees Cook Thanks for the feedback. v2 available here: https://lore.kernel.org/all/20230802-wifi-ipw2x00-refactor-v2-1-d33f765e9cd= 5@google.com/