Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5271217rdb; Wed, 13 Dec 2023 04:16:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IEiZjiVqihR+1lvmQAOf/AbuntVHglE77t/y93oNVnR8Ibt2P4b+FKd2JocI8X7fAzmGpXY X-Received: by 2002:a05:6870:d0c:b0:1fb:32a0:41ce with SMTP id mk12-20020a0568700d0c00b001fb32a041cemr10676420oab.0.1702469782906; Wed, 13 Dec 2023 04:16:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702469782; cv=none; d=google.com; s=arc-20160816; b=j6fHrz0wzkpRODmLdTzdmOwxsr9x7R7PFjMUruTTEP9MgDnVs4FtOdXtr1Ki7kKHvj 7NGXTBOnV+QH1f38o+iZ0dG/om8L6SR98FjC9/r/5u9jJe74gei0+5Gw1XQW6XB1egfa FWy9njMXux3Mjwvzkwc3E85INaEzJhsCV6GhMhQneAn7JfMtWWPc3euRelx/w4a5MiMH TIsH1blBTRCbGZCLZ7LE32cWeZlhfAi/owlUAa3Z9gxdGSTHYw4aLQGZAYgi9i3c7gnG jFkY9rOZPJXfVzax3j0q5HoobY64Y4Tu0gWf8PP8JDunXcTGV3+EjJthxefkraWMJH6K CEJg== 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:mime-version:date :dkim-signature:message-id; bh=DU7Ue/NZBO61kVWg5eFser6LJMK31DbbsBvBIKnZORg=; fh=0VuucfPzWbEV0d/8guy+wTmxXgRdC0gfd/baNDbk26c=; b=ZLjZpYsAY3tLjKOq8tvhkSVZaTFjFSk4oOZC+t7CXLm2WE6ByuuqENAniAYPUIDTtf nFGT56sAuyIALaOC6aZSnCqsrtI/GmvxicWlp1V2wv1df+SDMInUGl+GRB0gbEL4QFRm kAhQdonGI1bpUtsZB1CQIHjBZkuvKLS0sCgc+mxoOX5uoNk9NfFuYhGfL0qPluDhEuJK ttTi22oX3QlUSUwLimdcuFriDMhXB9O9MBmp3becCaaqRJ+JTywVTp4lW0DHq+7fv+xL ar0Dy0Hp4e0X7DzErUAaxmVWDHtUcCRRWeY+xNb0cYlt0UzvcRdO5ywVRaQayTvCWQNQ ugDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=M9Rgjoxa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id 1-20020a630201000000b005c1b303c414si9353151pgc.625.2023.12.13.04.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 04:16:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=M9Rgjoxa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 6A3718079638; Wed, 13 Dec 2023 04:15:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233364AbjLMMP3 (ORCPT + 99 others); Wed, 13 Dec 2023 07:15:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232873AbjLMMP1 (ORCPT ); Wed, 13 Dec 2023 07:15:27 -0500 X-Greylist: delayed 451 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 13 Dec 2023 04:15:32 PST Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBEDBCD for ; Wed, 13 Dec 2023 04:15:32 -0800 (PST) Message-ID: <78c25a62-44d5-d40d-a3e8-1f40a0c95c11@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1702469278; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DU7Ue/NZBO61kVWg5eFser6LJMK31DbbsBvBIKnZORg=; b=M9Rgjoxa+lySX3pH1qW05LzgbxUlUivYz+67rZWH7OnWNUGzpfJfodxrwxDf3aTZGSjyCh Gym/H14JCLFf7PtGmTW1bLpnkva18xTx4uSPQInOeIZiqMtOLtahvjjkmIV9kBQ1GVdIcy clznz3LNmOcfVDly6+Iuj6mCQcqxQ+0= Date: Wed, 13 Dec 2023 20:07:51 +0800 MIME-Version: 1.0 Subject: Re: [PATCH] block/rnbd-srv: Check for unlikely string overflow To: Kees Cook , "Md. Haris Iqbal" Cc: kernel test robot , Jack Wang , Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20231212214738.work.169-kees@kernel.org> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Guoqing Jiang In-Reply-To: <20231212214738.work.169-kees@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,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 groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 13 Dec 2023 04:15:46 -0800 (PST) On 12/13/23 05:47, Kees Cook wrote: > Since "dev_search_path" can technically be as large as PATH_MAX, > there was a risk of truncation when copying it and a second string > into "full_path" since it was also PATH_MAX sized. The W=1 builds were > reporting this warning: > > drivers/block/rnbd/rnbd-srv.c: In function 'process_msg_open.isra': > drivers/block/rnbd/rnbd-srv.c:616:51: warning: '%s' directive output may be truncated writing up to 254 bytes into a region of size between 0 and 4095 [-Wformat-truncation=] > 616 | snprintf(full_path, PATH_MAX, "%s/%s", > | ^~ > In function 'rnbd_srv_get_full_path', > inlined from 'process_msg_open.isra' at drivers/block/rnbd/rnbd-srv.c:721:14: drivers/block/rnbd/rnbd-srv.c:616:17: note: 'snprintf' output between 2 and 4351 bytes into a destination of size 4096 > 616 | snprintf(full_path, PATH_MAX, "%s/%s", > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > 617 | dev_search_path, dev_name); > | ~~~~~~~~~~~~~~~~~~~~~~~~~~ > > To fix this, unconditionally check for truncation (as was already done > for the case where "%SESSNAME%" was present). > > Reported-by: kernel test robot > Closes: https://lore.kernel.org/oe-kbuild-all/202312100355.lHoJPgKy-lkp@intel.com/ > Cc: "Md. Haris Iqbal" > Cc: Jack Wang > Cc: Jens Axboe > Cc: linux-block@vger.kernel.org > Signed-off-by: Kees Cook > --- > drivers/block/rnbd/rnbd-srv.c | 19 ++++++++++--------- > 1 file changed, 10 insertions(+), 9 deletions(-) > > diff --git a/drivers/block/rnbd/rnbd-srv.c b/drivers/block/rnbd/rnbd-srv.c > index 65de51f3dfd9..ab78eab97d98 100644 > --- a/drivers/block/rnbd/rnbd-srv.c > +++ b/drivers/block/rnbd/rnbd-srv.c > @@ -585,6 +585,7 @@ static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, > { > char *full_path; > char *a, *b; > + int len; > > full_path = kmalloc(PATH_MAX, GFP_KERNEL); > if (!full_path) > @@ -596,19 +597,19 @@ static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, > */ > a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path)); > if (a) { > - int len = a - dev_search_path; > + len = a - dev_search_path; > > len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len, > dev_search_path, srv_sess->sessname, dev_name); > - if (len >= PATH_MAX) { > - pr_err("Too long path: %s, %s, %s\n", > - dev_search_path, srv_sess->sessname, dev_name); > - kfree(full_path); > - return ERR_PTR(-EINVAL); > - } > } else { > - snprintf(full_path, PATH_MAX, "%s/%s", > - dev_search_path, dev_name); > + len = snprintf(full_path, PATH_MAX, "%s/%s", > + dev_search_path, dev_name); > + } > + if (len >= PATH_MAX) { > + pr_err("Too long path: %s, %s, %s\n", > + dev_search_path, srv_sess->sessname, dev_name); > + kfree(full_path); > + return ERR_PTR(-EINVAL); > } > > /* eliminitate duplicated slashes */ Looks good. Acked-by: Guoqing Jiang