From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 225C9EB64DD for ; Wed, 12 Jul 2023 19:59:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232695AbjGLT66 (ORCPT ); Wed, 12 Jul 2023 15:58:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230321AbjGLT65 (ORCPT ); Wed, 12 Jul 2023 15:58:57 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D55DA1FE3 for ; Wed, 12 Jul 2023 12:58:55 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6689430d803so4262755b3a.0 for ; Wed, 12 Jul 2023 12:58:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1689191935; x=1691783935; 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=IM1dSYo+80BYxXSJlIL/sk/xBjg6Ots+Pol0AVH4Yb0=; b=kN0riAjmUZltvjS1ZZu/cmL9YDBQwpNyRLMnVeyJUsFYI7J0wzmmClyaLlqoVavvmR HX0aXQxW3ZgAMNfjI5b5Zwx9uGTFHByzmco0yjmVtRRTEK4R9DkGvVDg6OGwrOpdcj56 K19maljE78z0Ngq7vlqpfwfvl2QxEWWBAC8oo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689191935; x=1691783935; 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=IM1dSYo+80BYxXSJlIL/sk/xBjg6Ots+Pol0AVH4Yb0=; b=Cidlm+u12MGGNZuzXMNUr+B/J4Er9q3MPu+owGEBeRe0gGp42g9EOtseeYG0CROwh9 q+WxBpIULokfKFq6Gpt6cTyCCFxbgV1yNZCxjnltua9cteC5cdr7s1l5SJIP4AB1QVtR m4wTTjOsdTgmZGSREdbpibV7/LEDefLCcwbZzClNFUfw8bgGw7w7LQlDipeY994mVyEW QLtGlsCQIylZDaRx0my65svBDbk1KdeSerCCELCy2wiX5cHCVLMCXfJBWgzbLZmxQm6w ks6yWWA3/Qn2Bto13d9q/crsOHBCW8k6AfVupqlMPE/9jTIRNeskh+VyizXtY+4QRkk2 P1Nw== X-Gm-Message-State: ABy/qLbyIbq/dghYqXrf5deE1zEHLN2eJBPuspg+eAtG4r76hLdLhewh fYK9wDlwilV7c4uLRmFpTPUNog== X-Google-Smtp-Source: APBJJlE4+AsaRrpGGfmncTL94MB7s8dvZvP4yBy4eC77VHo8u3xCXxW7nvKcyGe+QqgE6q1NDBivMQ== X-Received: by 2002:a05:6a20:e685:b0:132:c11d:4582 with SMTP id mz5-20020a056a20e68500b00132c11d4582mr2302449pzb.3.1689191935305; Wed, 12 Jul 2023 12:58:55 -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 u22-20020aa78396000000b0068285a7f107sm4080729pfm.177.2023.07.12.12.58.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 12:58:54 -0700 (PDT) Date: Wed, 12 Jul 2023 12:58:54 -0700 From: Kees Cook To: Kent Overstreet Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-bcachefs@vger.kernel.org, Kent Overstreet , linux-hardening@vger.kernel.org Subject: Re: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote Message-ID: <202307121248.36919B223@keescook> References: <20230509165657.1735798-1-kent.overstreet@linux.dev> <20230509165657.1735798-30-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230509165657.1735798-30-kent.overstreet@linux.dev> Precedence: bulk List-ID: X-Mailing-List: linux-bcachefs@vger.kernel.org On Tue, May 09, 2023 at 12:56:54PM -0400, Kent Overstreet wrote: > From: Kent Overstreet > > printbuf now needs to know the number of characters that would have been > written if the buffer was too small, like snprintf(); this changes > string_get_size() to return the the return value of snprintf(). Unfortunately, snprintf doesn't return characters written, it return what it TRIED to write, and can cause a lot of problems[1]. This patch would be fine with me if the snprintf was also replaced by scnprintf, which will return the actual string length copied (or 0) *not* including the trailing %NUL. > [...] > @@ -126,8 +126,8 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, > else > unit = units_str[units][i]; > > - snprintf(buf, len, "%u%s %s", (u32)size, > - tmp, unit); > + return snprintf(buf, len, "%u%s %s", (u32)size, > + tmp, unit); -Kees [1] https://github.com/KSPP/linux/issues/105 -- Kees Cook