Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4837932rwb; Tue, 8 Aug 2023 15:00:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJyl1ZUCUcSBDgaTA0YM1Iu9NALvW9S50uaES7kbgQ4e1QZ50RJDAjbDxMv89DY991vrJn X-Received: by 2002:a17:902:cec2:b0:1bc:8c6c:217c with SMTP id d2-20020a170902cec200b001bc8c6c217cmr1012794plg.15.1691532039971; Tue, 08 Aug 2023 15:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691532039; cv=none; d=google.com; s=arc-20160816; b=qGqk9QPwwKvdZAIdnEdY1DA8QORvXh/O0Gej1U93ZqV8i017sls3hhsirr/WhF/X4C N7rsrCRt1cINdkph2TMBg5oVeoRGMMl6Gjj9f6B3aKeg9Cd5e7v8vSySrudfI2NRidRk Vk5mwuFgqyyxGBOuposD2GM3uSb/iabIaJ7G9uY/xRilL1vdMW/Xhtn05Pnf2pEn1Kjp f5H1Nu1lITBYjjnNDviW9jmYEiXaBoLTBXSSZW5vKOp8AWVfjJI74xDkAPBBYXX6hOnP nIrSu5SLwGMVgxPqTxhbk9ae2gEXKL1BRGXCQnnU6nIzNveezvx1LoHhFsH1XpoXLojz qJ/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=MhIk+2a1OkjbpS1n8BuF5MVDq5Y21VihUXQjTqwAte4=; fh=RSlaAQ2vQ/vwV/mE5lEo0Y977v1XOuNegR71H0xy+F4=; b=LOYShOC8ROQhyqF20+JLGbxfZ6xleyMwIMoHGvS5AhN321TTXuRuSqZaWQppU/l03M bluoDV2+xWFkRI2sP5qDvbbfSRRW4vooIZopAkrl1EBc/xksV3Azac7JrSJ9NJHAkaWs HGr4HKlrjLZUA/4gp/OqRtlG3pWPC8yJpYTWeTY5OUHLxtmVzDAduAjfdGHBZM94q571 fsw6k1K+JaxSEF+zkJAPCItkmPVA9YjhvovzHL++L3qGiHCLs9c7usBttb3udOmkB9P1 IBCCgL0lYLW7U48t9/OjD8ujHWyDTslmu3r3j2NrhzwWmTs8NovCRwmta+RFpgKBtoDF B7Iw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kt4-20020a170903088400b001b55070e154si7900558plb.96.2023.08.08.15.00.27; Tue, 08 Aug 2023 15:00:39 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233158AbjHHUDa (ORCPT + 99 others); Tue, 8 Aug 2023 16:03:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232964AbjHHUDE (ORCPT ); Tue, 8 Aug 2023 16:03:04 -0400 Received: from www.kot-begemot.co.uk (ns1.kot-begemot.co.uk [217.160.28.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A1F27AB8 for ; Tue, 8 Aug 2023 11:20:29 -0700 (PDT) Received: from 212-39-89-7.ip.btc-net.bg ([212.39.89.7] helo=[192.168.14.227]) by www.kot-begemot.co.uk with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qTFpV-000Srs-NP; Tue, 08 Aug 2023 06:04:32 +0000 Message-ID: <0bdda692-25a3-227c-69ef-341f037e1dd7@cambridgegreys.com> Date: Tue, 8 Aug 2023 07:04:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [PATCH] um: vector: refactor deprecated strncpy To: Justin Stitt , Richard Weinberger , Johannes Berg Cc: linux-um@lists.infradead.org, linux-kernel@vger.kernel.org, Kees Cook , linux-hardening@vger.kernel.org References: <20230807-arch-um-drivers-v1-1-10d602c5577a@google.com> Content-Language: en-US From: Anton Ivanov In-Reply-To: <20230807-arch-um-drivers-v1-1-10d602c5577a@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.0 X-Clacks-Overhead: GNU Terry Pratchett X-Spam-Status: No, score=1.1 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_SBL_CSS,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * 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 07/08/2023 19:22, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings [1]. > > A suitable replacement is `strscpy` [2] due to the fact that it > guarantees NUL-termination on its destination buffer argument which is > _not_ the case for `strncpy`! > > In this case, we are able to drop the now superfluous `... - 1` > instances because `strscpy` will automatically truncate the last byte by > setting it to a NUL byte if the source size exceeds the destination size > or if the source string is not NUL-terminated. > > I've also opted to remove the seemingly useless char* casts. I'm not > sure why they're present at all since (after expanding the `ifr_name` > macro) `ifr.ifr_ifrn.ifrn_name` is a char* already. > > All in all, `strscpy` is a more robust and less ambiguous interface > while also letting us remove some `... -1`'s which cleans things up a > bit. > > [1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings > [2]: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html > > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > arch/um/drivers/vector_user.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/arch/um/drivers/vector_user.c b/arch/um/drivers/vector_user.c > index c650e428432b..c719e1ec4645 100644 > --- a/arch/um/drivers/vector_user.c > +++ b/arch/um/drivers/vector_user.c > @@ -141,7 +141,7 @@ static int create_tap_fd(char *iface) > } > memset(&ifr, 0, sizeof(ifr)); > ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; > - strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1); > + strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); > > err = ioctl(fd, TUNSETIFF, (void *) &ifr); > if (err != 0) { > @@ -171,7 +171,7 @@ static int create_raw_fd(char *iface, int flags, int proto) > goto raw_fd_cleanup; > } > memset(&ifr, 0, sizeof(ifr)); > - strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1); > + strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); > if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) { > err = -errno; > goto raw_fd_cleanup; > > --- > base-commit: c1a515d3c0270628df8ae5f5118ba859b85464a2 > change-id: 20230807-arch-um-drivers-ad44050885d0 > > Best regards, > -- > Justin Stitt > > > _______________________________________________ > linux-um mailing list > linux-um@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-um > Acked-by: Anton Ivanov -- Anton R. Ivanov Cambridgegreys Limited. Registered in England. Company Number 10273661 https://www.cambridgegreys.com/