From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E883320D for ; Wed, 4 Oct 2023 00:00:22 +0000 (UTC) Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C65FEB0 for ; Tue, 3 Oct 2023 17:00:19 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-3af609b9264so965481b6e.2 for ; Tue, 03 Oct 2023 17:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696377619; x=1696982419; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=b5SO7W7SD5TygXSLIRDkjMdwOJ3KiN1WzQFniH5ipwQ=; b=ffTkfy1Z8cvmDLeYVRVOrvxbXR3Uo59s6VVZSTe5HYnKDXAttnScWbVbPajMH8HCVX 9UgF4v2X+RzlGf1xBzCK482KYYQymjJrOEF2Pg07HAQkI+QTObabHf7HCE1sXrnvRoIN JFVp9U/s1V9AKVts6NU33xLT8yX9CBZBv4N2U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696377619; x=1696982419; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=b5SO7W7SD5TygXSLIRDkjMdwOJ3KiN1WzQFniH5ipwQ=; b=k7OVLXq8fMs09UfE4f1nrsLqRcuBB2QmvTL68p50/K6t/d1eAWt/hUtEvw73xMX/0B /m7VmL+bio4tm34QO8TzaYx70ugI4XO/etjLJ30tCvLwAnMCzdfnKvm2akX173rRb4n1 lX9IGio3WQcYoMbDMiVR5TYnOtsyIshr3c8AutDVrlJzvmHjr4WcTRBHDvPleZlHoZKX KfyMZoDnncL5QgNTbSY/vkOuxabhHmQ8koGsMWVdUWqamokDh2o/R8jjIWUDr5/hWSrh /dHvTknSEr2CM0FZq4WkgprrwI9r2Ktg9xNUisk51pt09BlaCi0hgTitPbYentDaYGXW qEow== X-Gm-Message-State: AOJu0YyHoZEgosoCyGhH1I65QJAjPXXKmVt9gb13LGufQuGsn5u/gN7o ISPIzUv6KuVnSVQAyqc+yNWusw== X-Google-Smtp-Source: AGHT+IESunAi1JY0py7CKhvc9kyhRQyA051984ihrMB/CuP8SVwPNcQucfz1gCvwrlcCJzN3q5nMeA== X-Received: by 2002:a05:6870:b688:b0:1d6:790e:dacc with SMTP id cy8-20020a056870b68800b001d6790edaccmr1166457oab.6.1696377619068; Tue, 03 Oct 2023 17:00:19 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id d17-20020aa78691000000b0068883728c16sm2002942pfo.144.2023.10.03.17.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 17:00:18 -0700 (PDT) Date: Tue, 3 Oct 2023 17:00:17 -0700 From: Kees Cook To: Jens Axboe Cc: Justin Stitt , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Nick Desaulniers , Nathan Chancellor Subject: Re: [PATCH v3] null_blk: replace strncpy with strscpy Message-ID: <202310031658.9F0DB21C@keescook> References: <20230919-strncpy-drivers-block-null_blk-main-c-v3-1-10cf0a87a2c3@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230919-strncpy-drivers-block-null_blk-main-c-v3-1-10cf0a87a2c3@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net On Tue, Sep 19, 2023 at 05:30:35AM +0000, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings [1]. > > We should favor a more robust and less ambiguous interface. > > We expect that both `nullb->disk_name` and `disk->disk_name` be > NUL-terminated: > | snprintf(nullb->disk_name, sizeof(nullb->disk_name), > | "%s", config_item_name(&dev->group.cg_item)); > ... > | pr_info("disk %s created\n", nullb->disk_name); > > It seems like NUL-padding may be required due to __assign_disk_name() > utilizing a memcpy as opposed to a `str*cpy` api. > | static inline void __assign_disk_name(char *name, struct gendisk *disk) > | { > | if (disk) > | memcpy(name, disk->disk_name, DISK_NAME_LEN); > | else > | memset(name, 0, DISK_NAME_LEN); > | } > > Then we go and print it with `__print_disk_name` which wraps `nullb_trace_disk_name()`. > | #define __print_disk_name(name) nullb_trace_disk_name(p, name) > > This function obviously expects a NUL-terminated string. > | const char *nullb_trace_disk_name(struct trace_seq *p, char *name) > | { > | const char *ret = trace_seq_buffer_ptr(p); > | > | if (name && *name) > | trace_seq_printf(p, "disk=%s, ", name); > | trace_seq_putc(p, 0); > | > | return ret; > | } > > >From the above, we need both 1) a NUL-terminated string and 2) a > NUL-padded string. So, let's use strscpy_pad() as per Kees' suggestion > from v1. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Cc: Kees Cook > Cc: Nick Desaulniers > Cc: Nathan Chancellor > Signed-off-by: Justin Stitt Ping on this one too. Jens, can you pick this up? Thanks! -Kees -- Kees Cook