From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF24E8F5D for ; Wed, 7 Feb 2024 00:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707265952; cv=none; b=QGOa5EimIvNrplzP6qYPl8E69vxqdG2mwuHzlFksZi9WHbEpy4129T2YftmTkjRcLSV/UmvTCD3vTHXPH1JmJk3J763380+BUJELpgBDVpWWsyh1Ns3uJvDFeJgteRKKt2SjzAtVmAerUitCFzMtk8OEg8Pj94iaaDrWPPBft+o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707265952; c=relaxed/simple; bh=ax4kbwR1wG2ecwpHsW2swGQp3y7VpR79Vvh+CgItsug=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u173U8ddfZt8GfEUegsH8cSFgo9duoKcpFCvmhI9hH5DTTfMvqqcxm8j7Ysw8ulYBa1djFwFL+97bC/7oMHYUdMB5pG5T2pdC4MPUf8TRBCmvqkzVuq5Q8w9G4VcJP529dCOb7wwIn0K+NXpfOU7e83YeKjAE4zx5ScwwzZb9z4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=KYbiyfde; arc=none smtp.client-ip=209.85.166.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KYbiyfde" Received: by mail-io1-f43.google.com with SMTP id ca18e2360f4ac-7bffface817so2911639f.3 for ; Tue, 06 Feb 2024 16:32:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707265950; x=1707870750; 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=QJXlby+AD30AsP8JwtKyQnYYLLwLHxDqcJGOtskX1jw=; b=KYbiyfdeSfwTPP2NVxxfCUMOF06JOc0Dhu1QtOsyxzSH/iyKgbpTvBtLv0CB13hXKs OZiTi7P3N7ZEM+TDwspeORv3wdckfkEUCfnKdw4lNOEoVp7w8fMaU0CRSvc+gcZ64/gi um8ous412hq2o3sI0LfxM/u33qnFOKLCO2vNgSKQttURnjKsIVEo1l+0FFnQ/EAa+2jZ il1QcHqlFf86SY3YJmJ11O2CsQDUTyYVQiRtzOmopC1GB4+gG5zzu7aINF8q5Muw6KB2 TC9wkstRtCBFX89cV1K02aRFgrrMSMH6XE0IJqjbDnxRowLcvmBoNMaau31fVAYgWqn7 ijBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707265950; x=1707870750; 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=QJXlby+AD30AsP8JwtKyQnYYLLwLHxDqcJGOtskX1jw=; b=OoFTlm1WeAecsmbpWAm9jfvYP6Q8t50Y37CV3BLAV5WVaE4cFwHcAluQ51ynUni3qy TbU+aXgDPROIsOCZ+YnmwstUNheeZNwoZ3GSbb+uy4z9vp25q6fXqw0p+txhCxylaF29 GtoO9takDiw0y7RPdznlz0vSDVk37kQ8L2TwPEFt18iqXQW9ajgSjLXJPKdZLAfjYBeB IvaU4o1TMg0SxSBNueJZMK3VauqJ77M3dzJ40ORC7H8EEBK7ti3s9XtpPQTyWWyyF5eP giqyq8hz5f4j/e6CN7OvqtHOwwkXs05sRbEjm8iWcOstxn+g4+d4fFC1X9q6N3tY6Tkf tQ3Q== X-Gm-Message-State: AOJu0YxOXe/MLJB1kDl1wnC2ASRYTxDh9LpQJYT87fR1xsHT/Gr2T3ne 6j4BntjlLx4RkaclXy/r3D/OezSZjK/NaMyVsTNumm1X5jNCgr16DFCLmzr5Fg== X-Google-Smtp-Source: AGHT+IFc68UeTfh18sBbRkqxKprSuedOLBwsjWYaAXb6UcbNSOYzvLEShWlSsdkoB4CHqFd0WVDn7A== X-Received: by 2002:a6b:7b4b:0:b0:7c3:f398:7de8 with SMTP id m11-20020a6b7b4b000000b007c3f3987de8mr3414654iop.11.1707265949818; Tue, 06 Feb 2024 16:32:29 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVqEC4vX8ewfaMiHobk4xS6GjGAJVg4zEGV4f2EcUVSYtvbaa7WKav90QosTXetCDOM6AK7MFFlYmnzWOVrdd9AWRh4DNgHtFbxbbQcRQGGg62wefpQmEAtz435+IlJizHzvyB1fSQFjZUAu8Ij6jhKuiMMU29qK4Xcw8xxSPG0YiQDLk5nnwaRZrQPB9FKLoIBXwo2mW9/mFypT35cHOkm1N2lrrjqxJGqqDiQU8L97onZjq58CVrmq8JdWPwAspczV9+p65+jtwNNGsJGz/HzogXLM63QHGSLsUxgt9zRpbNMofmu5rzgyzFM9tFoDLx0rleiv6bd8sk4zLRW+MPIkXu8cl3Y4YND1BBOn+ezc3Qi7K/WF1/YVkWjXWGTQWz/+D2Fpd7aLJZxJH0A9aMcX9R0OZivHReMhWiaALR0QE8iXdSvkjvN Received: from google.com (20.10.132.34.bc.googleusercontent.com. [34.132.10.20]) by smtp.gmail.com with ESMTPSA id b66-20020a0295c8000000b00470b6d256c1sm2975jai.91.2024.02.06.16.32.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 16:32:28 -0800 (PST) Date: Wed, 7 Feb 2024 00:32:24 +0000 From: Justin Stitt To: Kees Cook Cc: Andy Shevchenko , linux-hardening@vger.kernel.org, Richard Weinberger , Anton Ivanov , Johannes Berg , Willem de Bruijn , Jason Wang , kernel test robot , Nathan Chancellor , Azeem Shaikh , linux-kernel@vger.kernel.org, linux-um@lists.infradead.org Subject: Re: [PATCH v3 1/4] string: Redefine strscpy_pad() as a macro Message-ID: <20240207003224.gzatchm47gy3aihv@google.com> References: <20240206142027.make.107-kees@kernel.org> <20240206142221.2208763-1-keescook@chromium.org> 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: <20240206142221.2208763-1-keescook@chromium.org> Hi, On Tue, Feb 06, 2024 at 06:22:16AM -0800, Kees Cook wrote: > In preparation for making strscpy_pad()'s 3rd argument optional, redefine > it as a macro. This also has the benefit of allowing greater FORITFY > introspection, as it couldn't see into the strscpy() nor the memset() > within strscpy_pad(). > > Cc: Andy Shevchenko > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Kees Cook > --- > include/linux/string.h | 33 +++++++++++++++++++++++++++++++-- > lib/string_helpers.c | 34 ---------------------------------- > 2 files changed, 31 insertions(+), 36 deletions(-) > > diff --git a/include/linux/string.h b/include/linux/string.h > index ab148d8dbfc1..03f59cf7fe72 100644 > --- a/include/linux/string.h > +++ b/include/linux/string.h > @@ -70,8 +70,37 @@ extern char * strncpy(char *,const char *, __kernel_size_t); > ssize_t strscpy(char *, const char *, size_t); > #endif > > -/* Wraps calls to strscpy()/memset(), no arch specific code required */ > -ssize_t strscpy_pad(char *dest, const char *src, size_t count); > +/** > + * strscpy_pad() - Copy a C-string into a sized buffer > + * @dest: Where to copy the string to > + * @src: Where to copy the string from > + * @count: Size of destination buffer > + * > + * Copy the string, or as much of it as fits, into the dest buffer. The > + * behavior is undefined if the string buffers overlap. The destination > + * buffer is always %NUL terminated, unless it's zero-sized. > + * > + * If the source string is shorter than the destination buffer, zeros > + * the tail of the destination buffer. > + * > + * For full explanation of why you may want to consider using the > + * 'strscpy' functions please see the function docstring for strscpy(). > + * > + * Returns: > + * * The number of characters copied (not including the trailing %NULs) > + * * -E2BIG if count is 0 or @src was truncated. > + */ > +#define strscpy_pad(dest, src, count) ({ \ > + char *__dst = (dest); \ > + const char *__src = (src); \ > + const size_t __count = (count); \ > + ssize_t __wrote; \ > + \ > + __wrote = strscpy(__dst, __src, __count); \ > + if (__wrote >= 0 && __wrote < __count) \ > + memset(__dst + __wrote + 1, 0, __count - __wrote - 1); \ > + __wrote; \ > +}) > > #ifndef __HAVE_ARCH_STRCAT > extern char * strcat(char *, const char *); > diff --git a/lib/string_helpers.c b/lib/string_helpers.c > index 7713f73e66b0..606c3099013f 100644 > --- a/lib/string_helpers.c > +++ b/lib/string_helpers.c > @@ -825,40 +825,6 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n) > } > EXPORT_SYMBOL_GPL(devm_kasprintf_strarray); > > -/** > - * strscpy_pad() - Copy a C-string into a sized buffer > - * @dest: Where to copy the string to > - * @src: Where to copy the string from > - * @count: Size of destination buffer > - * > - * Copy the string, or as much of it as fits, into the dest buffer. The > - * behavior is undefined if the string buffers overlap. The destination > - * buffer is always %NUL terminated, unless it's zero-sized. > - * > - * If the source string is shorter than the destination buffer, zeros > - * the tail of the destination buffer. > - * > - * For full explanation of why you may want to consider using the > - * 'strscpy' functions please see the function docstring for strscpy(). > - * > - * Returns: > - * * The number of characters copied (not including the trailing %NUL) > - * * -E2BIG if count is 0 or @src was truncated. > - */ > -ssize_t strscpy_pad(char *dest, const char *src, size_t count) > -{ > - ssize_t written; > - > - written = strscpy(dest, src, count); > - if (written < 0 || written == count - 1) > - return written; > - > - memset(dest + written + 1, 0, count - written - 1); > - > - return written; > -} > -EXPORT_SYMBOL(strscpy_pad); Yep, looks good. This is reminiscent of strtomem and strtomem_pad. Reviewed-by: Justin Stitt > - > /** > * skip_spaces - Removes leading whitespace from @str. > * @str: The string to be stripped. > -- > 2.34.1 > Thanks Justin