From patchwork Tue Feb 6 14:22:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1571936dyb; Tue, 6 Feb 2024 06:23:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEF715+EAoUyf8rtTyIYza20O5/VEqxW6+5aWCzY0TOPmYqAPppWXbtgKYadByaXsTKGN3K X-Received: by 2002:aa7:d94a:0:b0:55f:3975:209c with SMTP id l10-20020aa7d94a000000b0055f3975209cmr1570425eds.18.1707229405194; Tue, 06 Feb 2024 06:23:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707229405; cv=pass; d=google.com; s=arc-20160816; b=SGLcFBWb8ZWKGgfIhfVwF49u8nkohNXvILHM7ZXd95UULyiMj7Ik0w7u9Ghz1pitdK QmJDnzv668V7Sb3OQIbmylcvyftMSKPG42PH5Wd+NAP7uwRXZoCfUitZ9CoFe5jyTAI9 ygYO0BzCsBXeavI3DfLLsWg7H9AfgNEkZWhQUVf5ZmOiGR8vdjw08AfXVJTom+QYZTjY 3xKgUZaC5ITFoDrCkSJQ7EIngqCuTxfiHVaEzLmfywmk0AokC9XLVVqv+EWxiUvOaa4s oRoU6CopuUeswz325uvsuMiZ42hb0wgGoIKPSCAY3xUtuliFkodcZ07KGamasy4UP2W0 8D+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=xUoezDG1AzB3IpHrnkZKY44PgV8//O1B2eigzb1QW3g=; fh=t0sriLvbOZvj3kDULTbtoMeEZDqlNQlj88lIEr439XA=; b=DmJOrQ+HalFCekBXRRXPLFH8r8LR2nQRSsuQrkMIQwabVBVHPVADGXoa777HTIt60/ 0irn8Dk83ZrFNCsiqLQlgQX5lGuOavfB/wabE1WgRjPAOqUULdjavo/B6OwL95J+wOHL I+MyY5Pp+45PEIq2P7S+BmZ1FggFAsUeDpUy7J82ZitaoZ+ncnC6239JKCsFsS2NS8Io 3yJH/9S9N4DQ7xO4F3TvzXRQnyduK4L2SUrzeEceDgWhjEVW9aJdAiN/fYvtyD2QMmMv byozPIi0dAtUYOnRORGbTS+cRvhXvNGctsSxqmt6Ex2iWz9dw/513fFdeRkLnAoHfeXo wXFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=B9hixiV4; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55101-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCWNruswISptl81a90feYAt1oBjRoSw1j1iPjqz92EvQsYOAc50zI/vgKR/SrEQAf5SdpXcyYLGPsRY0So2KlAWYECyhJw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z13-20020a05640235cd00b00560bb3cb3a3si287823edc.446.2024.02.06.06.23.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:23:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=B9hixiV4; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55101-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55101-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A25B61F27B7B for ; Tue, 6 Feb 2024 14:23:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F05801332BD; Tue, 6 Feb 2024 14:22:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="B9hixiV4" Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 4A0A9131E4A for ; Tue, 6 Feb 2024 14:22:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229344; cv=none; b=Y77YI97HvgWyoQtK+/tNjPpJz74tGqoZolmhXRcPxM3dPdk8Rp4c8hCt1ZO0GHQ6wBY67ZE1izaBmkDMb2rSBw9pV+B2hnZBCepi1cEVRgxWAZp5mCAz9r8k9nAK/3A2ziObobhAWnpzQ3XIZ+m/2vjKFd4BYtBb2yEQod1d+/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229344; c=relaxed/simple; bh=ftA6fxUrXEunZTcxHNIjZ6BhF5aIrSt6eYUpeNFdlXA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KiAfgrJsSCsvryCaEKI0wCd6+4a+frZyO+SG8O7eKyAIvHdG8jkomadXx9elfLohZzVFsR6nSiy6Rb6+TrlFe9FCz/NQCn+VLr7mCaf6AI42J3W5gk9s0+yQWlHswPGVZbUxHGNN3ZYEJXxodwJiPkf3Kre7HEmrd/r6UITdy70= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=B9hixiV4; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1d932f6ccfaso6923825ad.1 for ; Tue, 06 Feb 2024 06:22:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707229342; x=1707834142; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xUoezDG1AzB3IpHrnkZKY44PgV8//O1B2eigzb1QW3g=; b=B9hixiV4Z9whtH2ix+CvJpnuXh71Zn5vv5acai7Rnwb8fISejtgpTKiM5WlDgVl3SZ QQ+5NYOo9ZGGDZANEcqT5iXeVVqSM8OQOoi9a3kYTgTXpqF+69r85o78qkWpAf0rYKaz 2wzcGSmogLkIb32M1f+4AZsA6JTi4OkWndxr4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707229342; x=1707834142; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xUoezDG1AzB3IpHrnkZKY44PgV8//O1B2eigzb1QW3g=; b=lB1/k334M3U3WrYQV3ThJxtjGLetOBWmL7OCJsZlaQXLUKwtfFQSKo5LX+XxxkZ1Hl Fhr0ToliBjvUzJF+q3D6E8YkVF+prAckCV+tcfwlBQCtJbfQzXLfLEXvZ9hIqtJQxOGg KRFbWnMLxj9rYO88nhrTep47FKu65yfQ9ejGlp3JyJDWiq1WH7+NVYafYGOOwTBf6+3j Sw4dbz4ur1mP5Iclh4gHeW/tGa66AkEbSa9l3m8GErqSAYz0qUP535lv/4fc4PxmY6fx pEJb4gRiA1k1wUB1pzkC7i0lAFiigfWakqJKAzFFHJeUmU7mAWnS3KyNQb51ZNfwN9v9 0feA== X-Gm-Message-State: AOJu0Yz1gfxwiZEltxM/lDhxwW3Km3c11oDaFn6zldK6LA9rsVKIdxyp ybbk8UFYC+ndbJDTkLoEvrf9ugmVAF2CaXzckQ1ev9/uCOwgiNaCtdw09SBjPA== X-Received: by 2002:a17:902:6803:b0:1d9:bba8:16f1 with SMTP id h3-20020a170902680300b001d9bba816f1mr1493962plk.50.1707229342518; Tue, 06 Feb 2024 06:22:22 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVBiJl5sSNuX3epyJPI7cpDQ5h5nkdlby2inL3eARtoSG8hy/jWr62Sc6tuHMgQEUnKg5QOk5dogumFVublnktDm64c8CvUzJX91ZJN9I5Z7V2zBNg8A9hGhJn7g9RQlfV0HqxomZgvHPINaEI+wPsRwHkF+aVzAVjX8Wi0Q0nlIFjzvk5IFsq9u9l8qiGPp0Hu27H5w+rhlfegqgE3XCoj8uF/ByEtlxUT9aMqT51gYvu5zx17GcdE3DpUHnWP1/twrAvC/2ykP1NJfKxaKGPRnBn9UhSJ7aPgR49VwKEpZMOlzeN/M6clSEMPPGRJgGLRCirqj28WnFQslRrT+TKCKhShx1gNtLoOVmYZRaRBIhGqyyuN2VYnnfol62Hqt9mlg+pU2pWqVECXYCg0ooJ+uojdvg0TdjtlpxMu8ZxmBMAWS3PLHnxR7yv8nTGt9PH+p00+ndYJEY/y+gfbntWb Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id l18-20020a170902d35200b001d8fb2591a6sm1927205plk.171.2024.02.06.06.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:22:21 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-hardening@vger.kernel.org, Richard Weinberger , Justin Stitt , 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: [PATCH v3 1/4] string: Redefine strscpy_pad() as a macro Date: Tue, 6 Feb 2024 06:22:16 -0800 Message-Id: <20240206142221.2208763-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206142027.make.107-kees@kernel.org> References: <20240206142027.make.107-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3826; i=keescook@chromium.org; h=from:subject; bh=ftA6fxUrXEunZTcxHNIjZ6BhF5aIrSt6eYUpeNFdlXA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwkCba+eO8AWAs1iqb8nBpmwYItGRsUp3cnZLt JSR4c+PTxCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcJAmwAKCRCJcvTf3G3A Jga6D/4qoBSjrvyh0UrxSO0LWPNsVnXplNq13jIv34e0RmgqAE/fRVb2ugkoIOyvKz7fVKtClfr JCDXYz7V3OjO7Mi8eLKK7Fw/VNM5pOpTiToyEWZy7YHWupSmHlohhO/QWNvxSiXAqQm13wKDJAM 2f1RTPD+zIAWGH2uhE5/rIPlW56QMYDXcwrUQ6fGi5ySfVQLLu7IVI6tvhXomWxwS2kmvgvXV3n CWt+FDQJORntS6V4zRHeHftm0p1PLqVBdvTiKxUMPhwq3r564GLgpAyQguR1HLmM4L+S5nvUfhh OLyCx37bhcvMm5IRct/WRY5bYMsSA9MTKILeh5KkBZAl/1esXhUOnuOYT3b5kT0khgZ+sakcKjT SeoczboKqmd5sw30klUx/7vvg26XrScYK4RwCrPHFTIihaeWsYUvNw3m7akbNtPv/CHKAQ95DYr EQoB/FCyrQrFq3qahBIpj5eJ/Kw2RF3y+mMLCDqCy5RapAGGz9zb7gUNiMcNIi9bJa2w8UE3UmL X1DWrnhxob8HVAVgx7g2GPY6E7xSNcS+xpWoOvWKr/OUWPzGAt4PTBqCncakdQlClecsvO3zMjS Ne7r55u11fFj92/hWpiJFPzcJXzoB+kNL8A2gFO0KdMCwEWuSjj1AImLORFnqEo9ej/vDMr8SUk Wk9mwenStuipW6Q== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790159780322719054 X-GMAIL-MSGID: 1790159780322719054 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 Reviewed-by: Justin Stitt --- 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); - /** * skip_spaces - Removes leading whitespace from @str. * @str: The string to be stripped. From patchwork Tue Feb 6 14:22:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1572310dyb; Tue, 6 Feb 2024 06:23:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IG9R0xVJ0GyXi2iMp1be8/rpA0bd38cVE4RvkfxsRNcr83jtP1kCp4MXT+eC8c1rFs4nhMI X-Received: by 2002:a17:907:77d8:b0:a38:f2f:9009 with SMTP id kz24-20020a17090777d800b00a380f2f9009mr1670943ejc.73.1707229434243; Tue, 06 Feb 2024 06:23:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707229434; cv=pass; d=google.com; s=arc-20160816; b=k15L80hUCg6BzPyzR+ckL71kd2Cr7piyV2MgRxyAc+zDZLPuRDu3vCRojEyTA7aLHI ahYhNRvVzxLGwn04vjynrDsCBH3/WiPkH/aPga3YNnBPI79A7hlMPT4vDbAS7dXjq2Cx 1CU2s3io41gGewbpyZ1m/7Q/wgd5mon02dV/HUaMlHrdtmcsaYXt9lPwrx8QBl6sw8Ta LdsXrBpMFHcJiNLrgciZIy6JfHzd6e35iMiV5zy8QznPhOJX8Ime14WYGh2/4AsyRppw dWm7bna7+KI7PWDJ4IKzL3PWgQ/faeF8XLpRM1rBPdjmJ+Myqro6ZsTlA1n80n+fwK+2 n8zw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Omntykkn9bEUOrKw198xOVlbqlgc0dP4ZEJ9JLlKKos=; fh=CBlXddL0M1pMQikT99ud9he0bMtE5Uji/miPG/Ei4Uw=; b=LOfz0Mov4raZfAcUczPUsXZsiT3kw6G7IMn/Xped/5FktJqG+MVKwJE6Q63v8L9HZ2 stW+/omY4dXVVYKVUqsIuH8ReTjPx6a35pO+RswLPqVB3EoCm7Fxm5eDgetlRNfFV7ga UP+JAAuUQRdASNziO2OGKuEr0aCE3kF2ZOLGNHqbwkvnhlvj4ifMt4/iCRNLOzad8vHJ MCsTg9afv/6+TD+FT2MFaNhWKAGbOn4yIwKIL3f9RLfvO8nNKgKlOdepJkPmUl+TBUGg 1XqYfYe2kANxv9gNeYxD6audM07XHw22LTa+5DzcSev0q3Pm0DskPryuOFxN4RZQbHa3 D0nA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BJUtpPFq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55104-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55104-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCU3YmK9Zmfv048d2ekPotUo8aMcYFFHa3ME0nIeg/GNhDkEk2sEPHo487/uDAYbhPoGRr0X1fqDisF86BTZEtxp9X+AOg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id x25-20020a170906711900b00a383ef3d2e0si388278ejj.462.2024.02.06.06.23.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:23:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55104-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BJUtpPFq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55104-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55104-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id AC3DE1F27C3C for ; Tue, 6 Feb 2024 14:23:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97982133434; Tue, 6 Feb 2024 14:22:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BJUtpPFq" Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 6E6D6132C03 for ; Tue, 6 Feb 2024 14:22:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229348; cv=none; b=L1Ndox8ScJCInT5955VcDT6Wn9GAR5evu9wOdLl6kY5RpdlFkz2SpmMt+AboDdJEB9vHmwN1MCKQWGT2bJjPyMP8JRuiA0T1EGQJ+JErHzRNdGUK+suckhZkWDisMUjHTUim602i+vEG50RUv9d6xmNcj75OE0fmxtghtAQwZD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229348; c=relaxed/simple; bh=a/ZO1lSBIKe/G4XEwpv5qSlhJSYeuazlBFOqiXEjlLw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cx5oz3EByAy4UvTDDTQuoUyRc4JINGTFiaMxK57L2nwbNqVCLFIcj6Sn4rFWoKEGr+Ix8+iIgLs8opwoy5rvwg3VgKx4DaYOcRWMToEg6xYohzsL7f7GAb7+xovZft1Jrqiokv2wOeh2QugFyZF9slR3RZNjNL266sAQciBRO8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BJUtpPFq; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e05d41828aso331718b3a.0 for ; Tue, 06 Feb 2024 06:22:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707229345; x=1707834145; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Omntykkn9bEUOrKw198xOVlbqlgc0dP4ZEJ9JLlKKos=; b=BJUtpPFqc4UMmPvlO/qYlO98BucOMxePYukiA99KQSsw35jV9Hp8lF4ohEEaxw8VMB p55IkMtU2HSni14GJwI/ftAE4wUJKNdkPqKzFECfEwGJnq30P0PksquIlDQNp6r1D8uR /ELRAlLUEZB4IEFxbZ0ldaHBb8HjAY9PnU7uU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707229345; x=1707834145; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Omntykkn9bEUOrKw198xOVlbqlgc0dP4ZEJ9JLlKKos=; b=YFSvemoZJXJ4a9oR+PMHsug7U5FTM6f8lnkUDL2O92pkVWagpVWNLnWdKzUt5csCgy We3kyEoUAAFhpSuKlr8U64f0L5E10c3ny4/5fYdjBnL8/wE3aRNvg6GusnlMMlYR1+mu ZXsNN38668tX8pyWESLj9i+IUJHrZOC5ABvy7hMpQNKLrhsGeRtyR320U1PKFoHhklmp 1UHNDTccYXFcGEnmMjVMqtS0oUdkvjWHPKyhs+yVvSo/EtjNUCYVrR2Fxiy9PPz+PVfN qBprqWhMJ+0uMycNibp7I1bLw/+uY09z/xoHUJwfoOMWgUA6QmgQxglvKv78YdD6YVp3 BxrQ== X-Gm-Message-State: AOJu0Yx9j+poF9UThi0ggs9WDv8H3kfBCITze4t/mlhlOdOToZMOtI14 zFYRMLktpSvDsI6qgy0r6lKu917srGT2yWQjaKgun+eblX7ft/Ht9x/VnoDX7g== X-Received: by 2002:a05:6a20:d90a:b0:19c:ad6b:e1c2 with SMTP id jd10-20020a056a20d90a00b0019cad6be1c2mr1789596pzb.12.1707229344720; Tue, 06 Feb 2024 06:22:24 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVkFa2eznuvH9ox7ho8IHbgQ2AjWbNHGc5Y7bK8DfaXyBVX9TTKwPIKXvYEIVFHpyhlamW7hKwVZDN+eawMuhME8k9UBiO/UsD8VoAHrHkwaXq4sx9IxuyRxqP3Sdu3HL5fY6ozbf1bvYrrG185gyT+vvw6GaZQfedx0nqPYeEOGr3AXiKg4kFba+1/CH4rpU0Sec6pbiADQTCRI6lfT1r/jq8lNe/VIzmqNkdDeGNIhf99+sCL7Drog3ESJuN56uDL7KiNH14bZMNJ/oxAhHK+g0qVWBE46H1nmOLj15boFbwLywnFAfmqrm2kC58YxT+gAS0mRquTYdtvpzbBQfWwoe/7gwUGiOcDmXU9hgV+0BKwPeBp+iIiMfPNzE6wGRNWwgG6JD+Q4FMj4HcQvcRIvsuFUf9jrSLmoYJ3p9gw496DJjuxvX2g1EJoHdf3/+JuxnvHgcWSD9/zbaoQ7emG Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id o5-20020a63e345000000b005bd980cca56sm2185593pgj.29.2024.02.06.06.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:22:21 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Justin Stitt , 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: [PATCH v3 2/4] string: Allow 2-argument strscpy() Date: Tue, 6 Feb 2024 06:22:17 -0800 Message-Id: <20240206142221.2208763-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206142027.make.107-kees@kernel.org> References: <20240206142027.make.107-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6328; i=keescook@chromium.org; h=from:subject; bh=a/ZO1lSBIKe/G4XEwpv5qSlhJSYeuazlBFOqiXEjlLw=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwkCbtzgJLlfVxVgPKedChizZ0HI78tjkCrJ8U uoXfJpRTQaJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcJAmwAKCRCJcvTf3G3A JipmEAC0raBrD5LoCWM8LxAB6MySIUEYaaex89+G8Wpar9TyuWzpUyBANr0d8ua6jp9tKzFe5ad DSlFAGyaw3fcZ1rHXqtzDjVUWgTRtd0YEcQ9uU2/jn43DUFjgCovmggJPYnT1tdDPnlQqZG413C lduNGIf42Bv8SVrJkgYqN38EGxHB+e6AbmiE8PvkdZkxxt++TEL/YIha2M4U8YIDjHWHmIYBqom bh91Ymj+vNVGB4aqIBE20TTQEnayorwIBSZ+iFT8v0/+egzLlhIv0wFx2/cZLnh24uX2pen4HEb SVUZrWuDc49dhRJQdaudgn7V6LECljo48cYqxdEpDYNjwPeFzqla6mr2P/VpZClmrqor464R0Cc B4PEgYTaDfZtEuzOHESVj6XBKzXvDg56ABA7IAd3EzrIWfhGKCh0D/C5Md+KdH3Se4sKPSGaL9k 4jK7XBnba4U56RivyyHgm5O2MuqkL2dyb+Y76pvUey05cwRmm0RSXd/UwCyVxfE8u321RBXU/Go zMfSL48LiXmGEaImil8yO0dmNFhcISbWtaj9HZf/8YcnmTo3ivzUWOPSqQfWCU8HbAOqbOHbdC6 TASSlwBs3gVDXbWxcmBqSyI++0P0n4b9oVWJ+kSMIXy0Fn2trrpJWGVcrsmOvGzBRDpEFDgHXv6 4wK12/YjUfoXK+g== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790159763115335056 X-GMAIL-MSGID: 1790159811247720901 Using sizeof(dst) for the "size" argument in strscpy() is the overwhelmingly common case. Instead of requiring this everywhere, allow a 2-argument version to be used that will use the sizeof() internally. There are other functions in the kernel with optional arguments[1], so this isn't unprecedented, and improves readability. Update and relocate the kern-doc for strscpy() too. Adjust ARCH=um build to notice the changed export name, as it doesn't do full header includes for the string helpers. This could additionally let us save a few hundred lines of code: 1177 files changed, 2455 insertions(+), 3026 deletions(-) with a treewide cleanup using Coccinelle: @needless_arg@ expression DST, SRC; @@ strscpy(DST, SRC -, sizeof(DST) ) Link: https://elixir.bootlin.com/linux/v6.7/source/include/linux/pci.h#L1517 [1] Reviewed-by: Justin Stitt Cc: Andy Shevchenko Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- arch/um/include/shared/user.h | 3 ++- include/linux/fortify-string.h | 22 ++------------------- include/linux/string.h | 36 +++++++++++++++++++++++++++++++++- lib/string.c | 4 ++-- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/arch/um/include/shared/user.h b/arch/um/include/shared/user.h index 981e11d8e025..9568cc04cbb7 100644 --- a/arch/um/include/shared/user.h +++ b/arch/um/include/shared/user.h @@ -51,7 +51,8 @@ static inline int printk(const char *fmt, ...) extern int in_aton(char *str); extern size_t strlcat(char *, const char *, size_t); -extern size_t strscpy(char *, const char *, size_t); +extern size_t sized_strscpy(char *, const char *, size_t); +#define strscpy(dst, src, size) sized_strscpy(dst, src, size) /* Copied from linux/compiler-gcc.h since we can't include it directly */ #define barrier() __asm__ __volatile__("": : :"memory") diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 89a6888f2f9e..06b3aaa63724 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -215,26 +215,8 @@ __kernel_size_t __fortify_strlen(const char * const POS p) } /* Defined after fortified strnlen() to reuse it. */ -extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy); -/** - * strscpy - Copy a C-string into a sized buffer - * - * @p: Where to copy the string to - * @q: Where to copy the string from - * @size: Size of destination buffer - * - * Copy the source string @q, or as much of it as fits, into the destination - * @p buffer. The behavior is undefined if the string buffers overlap. The - * destination @p buffer is always NUL terminated, unless it's zero-sized. - * - * Preferred to strncpy() since it always returns a valid string, and - * doesn't unnecessarily force the tail of the destination buffer to be - * zero padded. If padding is desired please use strscpy_pad(). - * - * Returns the number of characters copied in @p (not including the - * trailing %NUL) or -E2BIG if @size is 0 or the copy of @q was truncated. - */ -__FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size) +extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(sized_strscpy); +__FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const POS q, size_t size) { /* Use string size rather than possible enclosing struct size. */ const size_t p_size = __member_size(p); diff --git a/include/linux/string.h b/include/linux/string.h index 03f59cf7fe72..79b875de615e 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -2,6 +2,7 @@ #ifndef _LINUX_STRING_H_ #define _LINUX_STRING_H_ +#include #include #include /* for inline */ #include /* for size_t */ @@ -67,9 +68,42 @@ extern char * strcpy(char *,const char *); extern char * strncpy(char *,const char *, __kernel_size_t); #endif #ifndef __HAVE_ARCH_STRSCPY -ssize_t strscpy(char *, const char *, size_t); +ssize_t sized_strscpy(char *, const char *, size_t); #endif +/* + * The 2 argument style can only be used when dst is an array with a + * known size. + */ +#define __strscpy0(dst, src, ...) \ + sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst)) +#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size) + +/** + * strscpy - Copy a C-string into a sized buffer + * @dst: Where to copy the string to + * @src: Where to copy the string from + * @...: Size of destination buffer (optional) + * + * Copy the source string @src, or as much of it as fits, into the + * destination @dst buffer. The behavior is undefined if the string + * buffers overlap. The destination @dst buffer is always NUL terminated, + * unless it's zero-sized. + * + * The size argument @... is only required when @dst is not an array, or + * when the copy needs to be smaller than sizeof(@dst). + * + * Preferred to strncpy() since it always returns a valid string, and + * doesn't unnecessarily force the tail of the destination buffer to be + * zero padded. If padding is desired please use strscpy_pad(). + * + * Returns the number of characters copied in @dst (not including the + * trailing %NUL) or -E2BIG if @size is 0 or the copy from @src was + * truncated. + */ +#define strscpy(dst, src, ...) \ + CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) + /** * strscpy_pad() - Copy a C-string into a sized buffer * @dest: Where to copy the string to diff --git a/lib/string.c b/lib/string.c index 6891d15ce991..2869895a1180 100644 --- a/lib/string.c +++ b/lib/string.c @@ -104,7 +104,7 @@ EXPORT_SYMBOL(strncpy); #endif #ifndef __HAVE_ARCH_STRSCPY -ssize_t strscpy(char *dest, const char *src, size_t count) +ssize_t sized_strscpy(char *dest, const char *src, size_t count) { const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; size_t max = count; @@ -170,7 +170,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count) return -E2BIG; } -EXPORT_SYMBOL(strscpy); +EXPORT_SYMBOL(sized_strscpy); #endif /** From patchwork Tue Feb 6 14:22:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1572052dyb; Tue, 6 Feb 2024 06:23:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IGx1236H3TOh52ci1pxtLHIce6JvnVFILmaQ4lTcJ7Fu4QN5EgYmfeMcAolcSuoIBzeAtHG X-Received: by 2002:a17:90b:1191:b0:296:37c7:3b6b with SMTP id gk17-20020a17090b119100b0029637c73b6bmr2454803pjb.2.1707229412876; Tue, 06 Feb 2024 06:23:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707229412; cv=pass; d=google.com; s=arc-20160816; b=CltH/zKkog1Riqo948q27kSsU0VLz5IHiCQZXlheQfXZiTu942VylXZHnA9BdQY4zF S9KF3UAtuZ5fCJjZlpu2/G5LFtJjKbrdj0i+8x3TFlTRHNiO1n6g8io6XKFqAQSEO38m gpLkFc749hCWrzAlH8c2NmF4ZSVnhq57KyNJttmIl4DyDJgCAhg7lQwnrwW5hE6GG7PX gWy0Wf7ZqJtb4R4M8g9keNjnkWqqSzt7VZydIHBZba1LVcm9Q3V0Tf4nEhz9OBxvJ7hz eFuy0O+3TxTbV38MKKlD/y5W7PRb4QV7mh6pOKf3EW4LmKH2JaePwNH3D4Ocl3bD7kBI vzVg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XqYtmG8hHVF+DID/XSnE8UT4bODHlUYWehuF8bFwVfo=; fh=LHh7nxySCuWJtDtj4bkhap2Jgwgh3GbXFvIy8FwfZHA=; b=kbv8AlltpIUtn4dvOU3DRdPvWBQ5jgMz3ypd4cPgwDBM3nc/GSsBl/RofMh2Bsabwq sEcjq8NXvEAJEEYD9+K35M4CuhasD3Js1sda/Ok4JB3JZJlrcbqzMb5kwi8HFqOIfhzM HbzfgIKShAMRfyhq3Pr248G8wDCZb9O+o0huFGKeQmWIqiNyZ8h/RhgHI+FdWDzrzRXO +EsubulreNB5djK97Jar6VH16Z4GQUaFkb3ABs3CP11PI6RTny12bJRTXbeO6tVfmVSk /vCxU7aM4yUrZsPtbmotCNAhs5Q2MtgwtgYoCEcAtNiy3aZjxtsng+T+BAneG+zsrCEd 6sTg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=P7pYpUAD; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55102-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55102-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCVKq5cQR6oXoVuCSQZ5iymIepXrJME2XgrC1nENV0CcDMLGrPDDyCMa4sYAV4067jI9robiwSsW1MXy/khOF4of6R9LOw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bb2-20020a17090b008200b00295aaad967asi1203300pjb.137.2024.02.06.06.23.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:23:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55102-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=P7pYpUAD; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55102-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55102-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 45DF0287F3E for ; Tue, 6 Feb 2024 14:23:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EEB213340E; Tue, 6 Feb 2024 14:22:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="P7pYpUAD" Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 78E92132477 for ; Tue, 6 Feb 2024 14:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229346; cv=none; b=tfjiDMFQN6ZJQ5RyZi8nqeh49w12bkJ9WwZVWkfzIjy+sNrEFla5GREtBTZx8lJO33y0ws7JGHqDmPAWh9liS3rk7VmmV2JxZoyIagzAPrVsOLXDs4VKdYHdjek6iZXnvfgI5uUoFHiGCgiZl7YQB9PSebSWGfbMvOzFccbTRCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229346; c=relaxed/simple; bh=cgm+i5e0DImLno4Cl42VWs3gXJ/FYegxHzATOxk7JqU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bw2aT7Bh4tFISHzQXSVopqMGNK/JUJGTRrTEMmij2zGM6Y5R5ljdXegXI1eWn+FlBkk1NpRAdf2CUGyOhHNwhPo17hdWchuigZ7TWvAJh+TCTHCNpdQcPEO1e6ZGntX5FG2mnFh899nlnT6xw1wxCcZpeF7173SRDE3pLpbLAkw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=P7pYpUAD; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d8ef977f1eso42776775ad.0 for ; Tue, 06 Feb 2024 06:22:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707229344; x=1707834144; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XqYtmG8hHVF+DID/XSnE8UT4bODHlUYWehuF8bFwVfo=; b=P7pYpUAD8YtqTPEYS2BeoDxXtkiCsdsv2Wu8t1gtfFzN0H0qB7jPwWySKy9lAY6f3r J4Wi4p48orEdaqGu5CVymCLgQ3JurIQkuzxpYKstUpr1b98Dq+5wGvK8fHWnbye/t5pW 1BzBn5c8UxajKXaDZebDzrxRrg037v2wfFcNA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707229344; x=1707834144; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XqYtmG8hHVF+DID/XSnE8UT4bODHlUYWehuF8bFwVfo=; b=TJNDkxxgdwg3DQO4W+OQYJfLLu+XNL+rZiUtQpwwmfKGnOq8/XNGvOFZMgBSNQgh5p 3tJy87ZL6wv+P+7mOObZwUppVmbxW+B8vbOF666sSVLZ0RHUFIDxiznMqIWMlPALC6GO cCwqkEFGPZYljwpAugsXZjqFLoqontbjT2oYRWXcJrY61mly9BSuVmUcSfBofQKm6r3Z imyhjfhuBuJbK0q+P+pM5OrvT+eNr+S3FR215g7xCFOlSbO2/7q1A1SQZHw6vP4KeypW CbwZPyhxBM07bsEHLo4RIiBD4qZsNbyETmx6OZsk3kDPVvt4urdw6pXGW2/g58ptcdh4 W35w== X-Gm-Message-State: AOJu0YzaW3i0014am79s1C0x3WVT9J8sIXqmZuHbwb4xh0O2B6ASjjqD zDVopoj0rSFAuqmP3DCObfU6azK3Q/DrVncuuw/6AO2nL/+NW/8sBmsOAJVxhvoo8hqlcSXhzR4 = X-Received: by 2002:a17:903:2447:b0:1d9:b21f:539e with SMTP id l7-20020a170903244700b001d9b21f539emr1916374pls.59.1707229343723; Tue, 06 Feb 2024 06:22:23 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWlZZIrmY09eOI8iB8rBxq4yDPfOGMeSugdKEHsAUwl/TYliRCPITQB6UflzwNa6JD6+m0/iALSjO2W9mEH61tDhl7gbbYxKKAomD9F7Si3m3QgmKtXXbZXLHRB1BOYxi6qyOavwlb+cfLHPIZw51mYjzUYQHm6H0kxvRIUN0LNCqrYvAQ/uaGiNH/i0YXTHQjVwTFBHZOCKywdw7IQWRQli9+RCdSyS9CXB0nRrz+vA9m7N1Un3b4R6eG//KJLsi8XWKBHn/a3+iIIDmQ4FebYBAko+Yf+svJrEF3jdW02hVDst8SZkxQA50mUciRYf1REuV5/D1ryjePFmAlHLTVtCP8TJryISBJpBIYqBt9jWh2+1YKE4uxabtST3KdTOROMnDeFl21LsvgkDMdD60vm9DD0OK0CNQ53GzeBtHqyEVDeZ+kwmQW8+MPAV5N6hZiz+2moLjM8Htxm0qr/xPPL Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id v3-20020a170902d68300b001d72f6ba383sm1900996ply.224.2024.02.06.06.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:22:21 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-hardening@vger.kernel.org, Richard Weinberger , Justin Stitt , 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: [PATCH v3 3/4] string: Allow 2-argument strscpy_pad() Date: Tue, 6 Feb 2024 06:22:18 -0800 Message-Id: <20240206142221.2208763-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206142027.make.107-kees@kernel.org> References: <20240206142027.make.107-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2506; i=keescook@chromium.org; h=from:subject; bh=cgm+i5e0DImLno4Cl42VWs3gXJ/FYegxHzATOxk7JqU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwkCbbZsr3bu0aaa7OMhvDrcFMrisXsQMmFbyZ gKEKT/ac8WJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcJAmwAKCRCJcvTf3G3A JqcqD/0U2D4y2McJjWOrTxf/kgRo8AYOPREygm/mXLqHiRTstAJBgEwSxsRtRU/aJALSJv/JqbR qzu+ONINnH1o5SoyLsRLvXzP0vuziQLKciy6hHtOhjWJ7GTi15TWaBDgtHXayK9OFff+gCFEyiW zPZcsQ7phH8aiLYRagH93X2309qmXfCvrA4WsnUnc3cQfDVmFj2/Sl1sJKy9LwNnrVoqwYTQhl6 rOV22gcSHQ6+Tlaz3QIcQhiN7uQSl+mb0mBjQG41W0NEBQHB7UKaQ1+fHbyjzf0idQGBpW613kY oyb/21y/eto/hljZm2iTgblRDBtKIDFbxazryj6TNJ7BdQkBYwVX5gLN5p//oDz9cryic14FLpQ /oaHbLL52MvoTRZa4w6jHoQZhU6ByXQOE9DAVn6XjBUelg1JnuqgqHHBP3vsC4XOep7luf9W17x sWFkgFQlnBjI4XUfEx0jxUlk5bEqixkFIr8zMR8H2EKIrLqmcHZhYQkNmMxwUvcbvL6A4L2zXwN FRb5pIsF1exRKTSPoCLOum0ZKtMtS/5gynHgSJMlbw7WOWVywd81ptTlb1TUux1ekCTgyZHs4eF Av+HQi968qemEjvAnon7yaIet+VBuKRN3ZteDrNsf7JomVPfSv+n6sm+rn37SNm2qRT+TWK02i3 f17sfSHEF5ioSew== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790159788776891811 X-GMAIL-MSGID: 1790159788776891811 Similar to strscpy(), update strscpy_pad()'s 3rd argument to be optional when the destination is a compile-time known size array. Cc: Andy Shevchenko Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook Reviewed-by: Justin Stitt --- include/linux/string.h | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 79b875de615e..9bd421ad92a4 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -79,6 +79,10 @@ ssize_t sized_strscpy(char *, const char *, size_t); sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst)) #define __strscpy1(dst, src, size) sized_strscpy(dst, src, size) +#define __strscpy_pad0(dst, src, ...) \ + sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst)) +#define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size) + /** * strscpy - Copy a C-string into a sized buffer * @dst: Where to copy the string to @@ -104,6 +108,18 @@ ssize_t sized_strscpy(char *, const char *, size_t); #define strscpy(dst, src, ...) \ CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) +#define sized_strscpy_pad(dest, src, count) ({ \ + char *__dst = (dest); \ + const char *__src = (src); \ + const size_t __count = (count); \ + ssize_t __wrote; \ + \ + __wrote = sized_strscpy(__dst, __src, __count); \ + if (__wrote >= 0 && __wrote < __count) \ + memset(__dst + __wrote + 1, 0, __count - __wrote - 1); \ + __wrote; \ +}) + /** * strscpy_pad() - Copy a C-string into a sized buffer * @dest: Where to copy the string to @@ -124,17 +140,8 @@ ssize_t sized_strscpy(char *, const char *, size_t); * * 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; \ -}) +#define strscpy_pad(dst, src, ...) \ + CONCATENATE(__strscpy_pad, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) #ifndef __HAVE_ARCH_STRCAT extern char * strcat(char *, const char *); From patchwork Tue Feb 6 14:22:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 197476 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1586578dyb; Tue, 6 Feb 2024 06:44:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGZkrNlnau0QGmnTwoOz8W+lkJsCDBGkePMfR8af0YOdBSYnwBF+RxhAkfk9MBXzV9jtXn0 X-Received: by 2002:a17:90a:bf0d:b0:296:4177:fc4f with SMTP id c13-20020a17090abf0d00b002964177fc4fmr2589073pjs.30.1707230699277; Tue, 06 Feb 2024 06:44:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707230699; cv=pass; d=google.com; s=arc-20160816; b=Byl3jGaS4D/WBRbxyF+NNbrK4kvCddIl6RH1cpPny4VPWfSbn5uIiZ4yiq5fKCJCG4 FojdvRTT/RLTP+cEe6OoAisd4K44lUlqHoMYMk2fsKenZBBQghyj09FivFSYD47bvXDf aNHDEsZ7jiCd2wacpfyVkbVqpSMP2l1IoQMRPG+IvOKrVnmzuPDeBHUQvQODGFf+/ZpZ CWn3aOKEnxe4qFz0WWsi9K3ckzHJAPFNozdl9q1u5GpseY7gdWDZGmOxtUQsa+2WH10z YueuF0BqlxTtb8atHthndWZXWZtDNnAZA3V5jnedFRYqSWjI4LRQ8fZLx0NqvfodTyj8 p56Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=a8sipwMRC4Kd6MHj5r1Ne28hNhCgrZv7vPqfbBQ2q0M=; fh=8HCbw0SW/q+8lO8ZbN45np8nhcu4ekTwie5WcZ+K64I=; b=CyctyXsGeKXOTOnO3XBWv56LOAQGqsQROlHlqWwIGAq+rrPN1EngiFD+zXBJWF9BJK KTVh+T+n8e7QBAE1b7Vwl5bPBndCPotjugVUBajtleyMT80vbqvVXn1Aj4VoEFCcCV0d zvr1R3FCMZ6iDBImePwK9EfsmUsqaf0OKpkiYCAobYFkiNVQIdXTWW12WFGyiYvnH6Yp r82Ha3Y8VayG62YnIczbZqG6pqfLurUxZMmI7Gj3e71CBq4B8L0GxKOpH1Uws+KY6Fcz bp334md5bYjzDqdu/vPbNqPLcIzk6wsHhgpR9UnpvYRppZLlgW/ghTx06nqSttVQhLMT PQyQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EC1MJvSD; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55103-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55103-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCV9ExHQuwtjj2iUUuPxHiskMSwYbWy8Zy2ATUDbfbGq84KP++IogXrVUDGR8DGHZo/UtAYgoGpcwXjKik6eBs5UzYEE5Q== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b7-20020a17090acc0700b00295bc158e90si1236499pju.147.2024.02.06.06.44.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:44:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55103-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EC1MJvSD; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-55103-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55103-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 46DA7B29B2C for ; Tue, 6 Feb 2024 14:23:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E547133423; Tue, 6 Feb 2024 14:22:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="EC1MJvSD" Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 E10A513248B for ; Tue, 6 Feb 2024 14:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229346; cv=none; b=ZDTnjkU4WTgYaNwXRDDafH/vYUTn6n0r9rb7vAHMQMHiOhnDTt5yViNU0gtLuAsL+3ZKbwvyYOiRhI1rd12yOWWhUYN7+Z6UEkJIrakS/kJhW1C50HWu4d8hJPVpTS6uhvrm50X/e1cFDj/YgRArS5DfK//cGjLRxBN0twB4A/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707229346; c=relaxed/simple; bh=5d6UiYMglkTbWmRhqsEoPg9BNX2q12MK63EVcZLBafI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bx1wozHHDKQeh6PjZ+W4LESGmD9WPt72Ty1pSd+kh5CBXfShulvyKHhBO1PhrAZXW+dfDhpewQPb+eQGZPlyg4r2tDuXcPC6TIR4eh94q/kPiYWdDtglT8jwqMNKDqUqQ2AG/rD4JlP2Q9i6vF/9FBPISBWme3cdmIYZGK0dIsE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=EC1MJvSD; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1d94323d547so45649365ad.3 for ; Tue, 06 Feb 2024 06:22:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707229344; x=1707834144; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=a8sipwMRC4Kd6MHj5r1Ne28hNhCgrZv7vPqfbBQ2q0M=; b=EC1MJvSD/KKmEnzdZBiQdaFG96XYZduDz1p+hnLFj1nMFuBQdP6Vok3gW497iCXw1l ENyaWtDAp7wR6UHqCDkLTVreetn8Bx/49eZAjXnZrahssrb/rHjZp7ZjfJ6e9XpfELs9 6yg4hWehIX+KZIGiXI0pHVbVX/Bkhd+ZzMP6U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707229344; x=1707834144; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a8sipwMRC4Kd6MHj5r1Ne28hNhCgrZv7vPqfbBQ2q0M=; b=DNDRbUWgLYTGpIL7At+VtbT5isq8C5dwPnDlcRcZ78LnsxrgOkecJgXkajCyen5rHG LbRMCEfsIo0bwxMLnTgOBdt5MsCsAEGNCaZ5irvjl84v7P5Em95IWHUfjQiBPzNDBReL zLIWVl1S3+RoXB81Dos7EeX5HNYuHrW1OLRg7SsM/L235EBg6vcYcb20Lpizor4VY2rc h3HRVg3yrcmjnG4kwHeBxI0YDmboMgJ3z6OVjWnh6jZZ/COaIsBCvrIH4aFkq526UGLG mf4S1lFtMw2V17qP+FqhUpbVHdu9O0jhd+hca8eAhPLQcGiiKTgRdStojHovvuSMrhrp rjbg== X-Gm-Message-State: AOJu0YzfVH8ywE7cCBZ5N9+A0hkwWgaBXlWR/5wZPijOPjnhreMvhXYV 2HFnyyoUDgY0YUhOXqF25oy49iO0BmLR+qWlEdGjomDAaXxXvotmr3/9OOi3VQ== X-Received: by 2002:a17:902:f542:b0:1d7:133e:5c09 with SMTP id h2-20020a170902f54200b001d7133e5c09mr1642447plf.22.1707229344131; Tue, 06 Feb 2024 06:22:24 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUbFC6MzphzX7s48wrl19nDEGaMUIhhhiQ13Coh/LyO2FjO7KheuxoclJRov+nF6HKUEA5bn3s0SpztfPJ3mPIP0R5TJno2nPN46T6lXi8bDx2kYTDH/53JPv5G0cn6QyzK0sykeUswyJUbdR+oCk4bZJO/SxxlsJ4CnPE5QwSRsaaBjBhGENbq/B/cT+beuDt4pge6/BOtLtz+6UwOS9YeZyIbCGETv1DKeAhem47gmmKoyeeGTL4/kIzMpBt8WXKrPJptwuM2SwqNTtG4m2Wma1aZ4RzRPG5AiLM2bsZHKAHJbjxyJxqovV1p30DDKw2ejrnhUCyLIArP19TkndjvaXqDcU2Ue6KqcpbPt3UMudvwzkgpBDxFi7z2moeo4W6ZJhFxDPYDgESWVFi2ka9Yq4y80vqpBqyE1n76OOTEpKczE0L3c9okrqEQvmC/LMI/ImXDuic9SsCZM3QEQ/y2 Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id l13-20020a170903244d00b001d9620e9ac9sm1922529pls.170.2024.02.06.06.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 06:22:21 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Richard Weinberger , linux-um@lists.infradead.org, Justin Stitt , Anton Ivanov , Johannes Berg , Willem de Bruijn , Jason Wang , kernel test robot , Nathan Chancellor , Azeem Shaikh , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v3 4/4] um: Convert strscpy() usage to 2-argument style Date: Tue, 6 Feb 2024 06:22:19 -0800 Message-Id: <20240206142221.2208763-4-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206142027.make.107-kees@kernel.org> References: <20240206142027.make.107-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5346; i=keescook@chromium.org; h=from:subject; bh=5d6UiYMglkTbWmRhqsEoPg9BNX2q12MK63EVcZLBafI=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwkCbb+R0+gw7GgnI3QI4KGLyNGSb52JGRxet5 1XMuj4aUkGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcJAmwAKCRCJcvTf3G3A JvgOEACrK+19fO5TvvKkPLA3lsjbTwmh6j03j+gO1FLjDgE6NpUFUN3owP+6g03lsKp09hc7gIl EMQicPbXlLKVXbz1YmLPx7zDelYGWby10hglehRvQnvhssFjPTB6EhA1H6U4o2geZsWZGvWef7e FLZ2aPhtS9ITWTsUPZLgrOgXJZPpcOXRRMkcbgH7zy9LrYOvuKyrOk3jpQd9B6mEFnBlJFbBYnc owMbotjUIKfSvk/o7IhZvdfYQsFuJOEeWGyu/ba5Cdm5VLymZ4f961qutEhEA4UuqR6qMVZdL98 qpDSxA2bI+FtvRQvV6AFPZ2nVN7cexonUiyhvLQylRQ7EufLhnM/WXyL2PiHfIuLC6c+XOfbptw 3v4Hh4A9B2zjmIUVrrToFCrU8d3MacbS6XaPf/1kc8h4tjMhHDTD5LYyOeUNA7CloaBx7ZoVeV1 AYnbWMGFLvgrcTHYLgifat9iRfPNqonWjYlxbaOJrjk2B3wNvD5cMrbZQyFneVA0K1k0Ye4EGW5 Y3paNJzKZTKVSwPhDN20cAZewJpRMLz0jAWJX2vYmRS1zaYu2lsJtZjKn+MaugVL58UyZeVGreo FXXiVdl11N1na+abV9bHYWm/gizgAuM2IQ1u8FSCDA1fqSELtHFvg4I2JdBPXxN5Ls4DIb9Fmxi MvFlNm9TRhcbsyQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790161137643775542 X-GMAIL-MSGID: 1790161137643775542 The ARCH=um build has its own idea about strscpy()'s definition. Adjust the callers to remove the redundant sizeof() arguments ahead of treewide changes, since it needs a manual adjustment for the newly named sized_strscpy() export. Cc: Richard Weinberger Cc: linux-um@lists.infradead.org Signed-off-by: Kees Cook --- arch/um/drivers/net_kern.c | 2 +- arch/um/drivers/vector_kern.c | 2 +- arch/um/drivers/vector_user.c | 4 ++-- arch/um/include/shared/user.h | 2 +- arch/um/os-Linux/drivers/ethertap_user.c | 2 +- arch/um/os-Linux/drivers/tuntap_user.c | 2 +- arch/um/os-Linux/umid.c | 6 +++--- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/um/drivers/net_kern.c b/arch/um/drivers/net_kern.c index cabcc501b448..77c4afb8ab90 100644 --- a/arch/um/drivers/net_kern.c +++ b/arch/um/drivers/net_kern.c @@ -265,7 +265,7 @@ static void uml_net_poll_controller(struct net_device *dev) static void uml_net_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) { - strscpy(info->driver, DRIVER_NAME, sizeof(info->driver)); + strscpy(info->driver, DRIVER_NAME); } static const struct ethtool_ops uml_net_ethtool_ops = { diff --git a/arch/um/drivers/vector_kern.c b/arch/um/drivers/vector_kern.c index 131b7cb29576..dc2feae789cb 100644 --- a/arch/um/drivers/vector_kern.c +++ b/arch/um/drivers/vector_kern.c @@ -1373,7 +1373,7 @@ static void vector_net_poll_controller(struct net_device *dev) static void vector_net_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) { - strscpy(info->driver, DRIVER_NAME, sizeof(info->driver)); + strscpy(info->driver, DRIVER_NAME); } static int vector_net_load_bpf_flash(struct net_device *dev, diff --git a/arch/um/drivers/vector_user.c b/arch/um/drivers/vector_user.c index c719e1ec4645..b16a5e5619d3 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; - strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); + strscpy(ifr.ifr_name, iface); 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)); - strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); + strscpy(ifr.ifr_name, iface); if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) { err = -errno; goto raw_fd_cleanup; diff --git a/arch/um/include/shared/user.h b/arch/um/include/shared/user.h index 9568cc04cbb7..326e52450e41 100644 --- a/arch/um/include/shared/user.h +++ b/arch/um/include/shared/user.h @@ -52,7 +52,7 @@ static inline int printk(const char *fmt, ...) extern int in_aton(char *str); extern size_t strlcat(char *, const char *, size_t); extern size_t sized_strscpy(char *, const char *, size_t); -#define strscpy(dst, src, size) sized_strscpy(dst, src, size) +#define strscpy(dst, src) sized_strscpy(dst, src, sizeof(dst)) /* Copied from linux/compiler-gcc.h since we can't include it directly */ #define barrier() __asm__ __volatile__("": : :"memory") diff --git a/arch/um/os-Linux/drivers/ethertap_user.c b/arch/um/os-Linux/drivers/ethertap_user.c index 3363851a4ae8..bdf215c0eca7 100644 --- a/arch/um/os-Linux/drivers/ethertap_user.c +++ b/arch/um/os-Linux/drivers/ethertap_user.c @@ -105,7 +105,7 @@ static int etap_tramp(char *dev, char *gate, int control_me, sprintf(data_fd_buf, "%d", data_remote); sprintf(version_buf, "%d", UML_NET_VERSION); if (gate != NULL) { - strscpy(gate_buf, gate, sizeof(gate_buf)); + strscpy(gate_buf, gate); args = setup_args; } else args = nosetup_args; diff --git a/arch/um/os-Linux/drivers/tuntap_user.c b/arch/um/os-Linux/drivers/tuntap_user.c index 2284e9c1cbbb..91f0e27ca3a6 100644 --- a/arch/um/os-Linux/drivers/tuntap_user.c +++ b/arch/um/os-Linux/drivers/tuntap_user.c @@ -146,7 +146,7 @@ static int tuntap_open(void *data) } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - strscpy(ifr.ifr_name, pri->dev_name, sizeof(ifr.ifr_name)); + strscpy(ifr.ifr_name, pri->dev_name); if (ioctl(pri->fd, TUNSETIFF, &ifr) < 0) { err = -errno; printk(UM_KERN_ERR "TUNSETIFF failed, errno = %d\n", diff --git a/arch/um/os-Linux/umid.c b/arch/um/os-Linux/umid.c index 288c422bfa96..e09d65b05d1c 100644 --- a/arch/um/os-Linux/umid.c +++ b/arch/um/os-Linux/umid.c @@ -40,7 +40,7 @@ static int __init make_uml_dir(void) __func__); goto err; } - strscpy(dir, home, sizeof(dir)); + strscpy(dir, home); uml_dir++; } strlcat(dir, uml_dir, sizeof(dir)); @@ -243,7 +243,7 @@ int __init set_umid(char *name) if (strlen(name) > UMID_LEN - 1) return -E2BIG; - strscpy(umid, name, sizeof(umid)); + strscpy(umid, name); return 0; } @@ -262,7 +262,7 @@ static int __init make_umid(void) make_uml_dir(); if (*umid == '\0') { - strscpy(tmp, uml_dir, sizeof(tmp)); + strscpy(tmp, uml_dir); strlcat(tmp, "XXXXXX", sizeof(tmp)); fd = mkstemp(tmp); if (fd < 0) {