From nobody Sun Feb 8 11:43:50 2026 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 632541C6B2 for ; Mon, 5 Feb 2024 12:35:29 +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=1707136530; cv=none; b=Gc2u4YbSprJCYfhQQmt3n2MQPlZj9PN3xF9sUiUbsB2DYOEeTXyl1Q+g+T33PPUA1cAi8tXNsed/HhI4IIvpynP+TOhQ3ygPckD1uL+mwDody8dsCSg9iH+soUhYYEEOMij/Ks18gVYFgKlAuJKay3aWL2L95VExTDdUsBP2IH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136530; c=relaxed/simple; bh=TEqXeG3EE5r0HJJI2aXfyZ+mDUoPdveS85d+Fo7JBf4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LPiV94GvPd1yXB2OK0PealigD8a7pgiYJFV/IEWmsCu9R14DMZ9/5Hc4fxQl04p8yIx+2dEc8GMFI/f2zxaC2hsNEkMl6JoURLYhlXpUU5IUFUC3pqGwIoAwvjZ9rYo1jZuBkyxVVwzD+kZ4c3zD0jHfZdIHQXrMHrWoWyrqOoU= 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=L4VxypKJ; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="L4VxypKJ" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e038bdf6b1so738630b3a.3 for ; Mon, 05 Feb 2024 04:35:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707136528; x=1707741328; 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=FH86lG6MyR7uXWPlPZaZQtj5zclv3S/IDeQNDhD0wsU=; b=L4VxypKJ7KRXVpUCrAk89VOHjkk462r/l9kQsHKwYwuuEVGsndX6FCzgGt7B5sA7zA xHYna+NdYFR9Nunfiz0FwpyIItLQk9yEhuBhj1ZammQoC1LoykfJzlo9f/rx9mJQZJ+0 l2yKO0XY/9l96hib65YzYdmMa0NZKlfOnl7P8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707136528; x=1707741328; 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=FH86lG6MyR7uXWPlPZaZQtj5zclv3S/IDeQNDhD0wsU=; b=lDYwtaQwkt9zKo6F4dspDaX8PoUeCr4IKeMlbLoLwHDCwqlNqaavB2T1IC69XoB2eu oNCtYuSKfkh7j8WIYe/DeHnERYRXpuiIhonTYEyCh6DuhMhOrcrDImKJ2KzP5JbJR0vm oosR1HCwvlTcQs5fWvpZGstPsDp5gdpiGXOsqQ+5IlMItUN16ZIRAFbWurJuE6lHo7Be l/6ldqX4wpmscSCzJvcYNR3pak9N+Sl76C731TBx31WJGpVeExNi5qEvxRC2+nGpCuQh TPGLDF64x75e/L7rIaw7cVR4DzoGcuGp9QpY2DnjLncdE2pcEEft4rFw7BKWzKVtU/fY NcAw== X-Gm-Message-State: AOJu0YwsvpOSBX4l2c51RPaoSUfFVYiDjoH1T0gKgQ53fL3IrmRk0WBZ +YjGOjf6piL86/rsYEA2/sXVcL7QjfWrRqp88Yome+ebwyvENB4SklYoj1SVbQ== X-Google-Smtp-Source: AGHT+IFlfffgYQK5YYez78+IfsArDP07cdit0XksGG6AD/xQaqmTxf1lIWlhL3nllltxLu/AYqTxfA== X-Received: by 2002:aa7:87d3:0:b0:6e0:4e58:f21b with SMTP id i19-20020aa787d3000000b006e04e58f21bmr761630pfo.15.1707136528648; Mon, 05 Feb 2024 04:35:28 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWwHha8YxvP2aoIRIZZ8PXriiDHebzEwEPdQS1GCiUmNtMp7csrX2Vw+AKCDxmAxF83WOZEXOVnTruJgzWNTBTfEcH8if2KGpE7zm4SbpGoDOFecCGttc6XrjLdg+cwsWTIBGQh5Rag9UuMLglMTn4D74UUHRXvOc9Lgxs2hHtrGRIksJx1ww110fVmUquqOsQMM6L2QFLzpfH8nI0wGJAuw5Xbe4J2yH9Zc/aKfbVbhNvsAMXOxtfmY+qPqxqsh1/x8z6/VrRBthvrmE5xAbFTX0XoD6rJILJyX5/84ez76SejdmlFWiwS/CfnQ+rizfNNf09XD4CkbEEAICIoGTB+wpJtmAlY8zC5RTRIH9/ypAT7vPEwWXPaQZnPL3l/gXTt2ZjvKM/gh6wI2WUCCrkx5DUphblH+z9OfbXszwBmF4xlPjUvePU6T0W/RaCdgw+0iIb4zBpJ7rkGof6jWgeKjfRGG3D9MPokDa17rA8H6FNyR6Z7cIqpEbhw Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id j32-20020a632320000000b005cd821a01d4sm6877508pgj.28.2024.02.05.04.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:35:26 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Andrew Morton , 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 v2 1/4] string: Redefine strscpy_pad() as a macro Date: Mon, 5 Feb 2024 04:35:21 -0800 Message-Id: <20240205123525.1379299-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205122916.it.909-kees@kernel.org> References: <20240205122916.it.909-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=3873; i=keescook@chromium.org; h=from:subject; bh=TEqXeG3EE5r0HJJI2aXfyZ+mDUoPdveS85d+Fo7JBf4=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwNYLoPV5NTQ5GwRGCijGU/aLTVRbyEZipneuU MRho0SleAeJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcDWCwAKCRCJcvTf3G3A JjLlD/977C+XMdrLxO0tkJtnq9nO/lyXGwJ3l1LW0AnI2sQrcE8MvllgCnv8jv/II3aTMd2vN9I xgwhNpMJ4NRgwd/aQT3EcXg7gegX4oXTTo5Cr44s9gCAMWcNouxVQLVO1OFC83tBUNnkVXxNVnN PaATkrRl3yMdrcA2BEP7vtPJ239fB/ZzU0a4wGgIJ/A1+AvNy4L4M2GIYQf0vowoT2g769325Qw u0uXMqpFubwmpTBbFKkQPUb/lNkZKSq87HnXXOmTSFyqvZEa5mrXoTl10VCF86rmQyIN1fwj7GL QbQVjt9Bou0wk8qUR0pkjoRA4q+z95bzhsx4vftom9CzgugBJzNdo0tzZ/4rM/iRIdwanBfUcvg loh2poV/udgDFcMlOiP8J/gM8ty7rRlFIEsc/ZIwTz9qFXFDgQzneRVBfq/pQ5wVAHBHmM3Msts hnz3lXsTCSCyJusPtxPVqqwh1wO/3fIxp+SPqjbTzBucFtG2by68KAuQShhC8LtCFqG690T5h2b mQIU2FZgmjwEzErgF37A6wSigJ1+eQRblZsMQrMVIZej/ALqfadFztxgiF5Qc4SfC8p8YiWUbPN 7s/9oP/Imsnzy24+O1UMvEaRTX4bAVV1nMq05nP7DuB9/Jk4AMp/bY24MWR+3XEdB4leJ4KJbVp fhcKDnAmLXTx9Aw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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: Andrew Morton 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 =20 -/* 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 =3D (dest); \ + const char *__src =3D (src); \ + const size_t __count =3D (count); \ + ssize_t __wrote; \ + \ + __wrote =3D strscpy(__dst, __src, __count); \ + if (__wrote >=3D 0 && __wrote < __count) \ + memset(__dst + __wrote + 1, 0, __count - __wrote - 1); \ + __wrote; \ +}) =20 #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, con= st char *prefix, size_t n) } EXPORT_SYMBOL_GPL(devm_kasprintf_strarray); =20 -/** - * 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 =3D strscpy(dest, src, count); - if (written < 0 || written =3D=3D 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. --=20 2.34.1 From nobody Sun Feb 8 11:43:50 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 DAB141CA8D for ; Mon, 5 Feb 2024 12:35:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136531; cv=none; b=tB2/QeFsjKF6Ao1A3SsI+8BRPEpaF7EBjrF6cZ0hZOTlFGTKcwxLjT8LdOebg+Q1ezODagwTzfpoabvxxkDn4nwjtV1KCGXWmppaqzf0hBr0XwqvI1R4bFOsrd8Z6I6mHbXcSQ3WKeabnCZtD6v9jz4NEsBKqtyNtEinRJgzwD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136531; c=relaxed/simple; bh=mJaVHVptIigPFzZ/zR9JERbMLBqYYNcKpf70/6AE0qw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D+hTb/0n1XoiGggAH8IBKB5JTxlehMTv1+bify4beOpXfK6JVjakI62IrU8jl1SSMocLN8Dcnsp8rDdZT+6TSLq5GPuhW/rxR1e1AmqasKGOQgPDJHY4TGI6vct7/5GqLI1gP4nXJRBj0pHaSW/qQFdx87EXWZJ+SBb3ugp8VZY= 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=htpe0+ww; arc=none smtp.client-ip=209.85.214.169 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="htpe0+ww" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1d74045c463so33461385ad.3 for ; Mon, 05 Feb 2024 04:35:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707136529; x=1707741329; 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=xBM9WxTj4Xh4iFKbJAS9nYsTd9tTRHElbt7v/kERb54=; b=htpe0+wwzmD3Td7ZdFLPmXsoewp9HjDWEK0jFxt7Xy15b1u9y1nRwuRPXCMMwC2TEs IapY5DBQ4lE6Gsy8lsej9UbOJTCWI8K5MLxVvyd6fMa50ph8yEd13AqA/umdQ4kus4E2 FAs7JYyM1A/HukuB+s5oe1NOt/6Wit1Sjaksk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707136529; x=1707741329; 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=xBM9WxTj4Xh4iFKbJAS9nYsTd9tTRHElbt7v/kERb54=; b=Nqzsm2uanzSjCn6Xsmj/0ggILqs88lhcmSNy6BNB4+OlAygyil6e/5dSDTrCl5u+M4 0enjdGVPi4QIVjMZf+5tbvoPzx4rndPffaRUl6YThDF+9VXpLoAHl04tmqmQdyUmE0Gv yRF2c+fE4+WPwcKWPXJSwXNEYNcB9kFAVIrhk16MJffrC+BZCZLUqUTPuiBfkhZdQ9Sf vZU87cH/6VNCT5XmpztvEdd1kQzMH7hutn/n7Kvdzadj89l6ymoFENZ6/BAdkb/W81xD 6yURuWvDfWgKu/yfT7u5e9/xREjoX7cgG5ugZBDVdmwRjppkVuDohvNcNugm5A+byPd/ dtHw== X-Gm-Message-State: AOJu0YzMHcdaE0dOCAh1m7IluL/+AxQv1a+NOOeAApobOWDh6nZBqXRu FSTXxjPS8NIUeCH2LiOpDR4ZILGHWqCRdWNU8mpmfHnvLyyFAmZ0Km+Rxz0o7Q== X-Google-Smtp-Source: AGHT+IFTTqqrWRJgFFkuhIFSdxjlp5gIlB3CMmmYW9dZei/xGuN95raXdDCg689tZTBtii8Fqz2Myw== X-Received: by 2002:a17:903:22cd:b0:1d9:4d3f:cbf6 with SMTP id y13-20020a17090322cd00b001d94d3fcbf6mr10142104plg.48.1707136529264; Mon, 05 Feb 2024 04:35:29 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUEpbF+H7yLOzWQv3G1vKnFCs8F/5pEM6MLM7TrUj9SdzHZsyLibhM5xXVRRyDcejgeQiWnpb8oJnfnXihVhQW2C798wt69vVsErM1ODCVO9CidizGakcPoqklr/hbJT5aEqQLPiBy8xxlAZNBcDUDfzc0s3Pfr1Voeq2FEdi4diDaOMNaUkXS+L3GIfnkmJCCIIzzieh1FnIT+dKhDrR3tqjz0QS3fq4n5tVEJtag7FX/Sfl3Q4e8gJlXFzkSQfpCrDmfw1HHCwJe3wT/nQAHqHvq8euSoxQ2CFSIO8u/QcDZsTJvA/HZmQvl+B2LkQ6Ofh6Zd8MwUXbERl5/oxxJLIHe5H7DxrOHUYspbu6FIjz8kpQsjH1Hmazh1HGtwXwN3B1fXigaPI6/nIYz41yL3pZ1tfclMwv2lhAQj723qoZKcTsmAGgqXlvKYDS7ehwWPssK5Ha35kfr7EANSZyN8 Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id iy5-20020a170903130500b001d944e8f0fdsm6204478plb.32.2024.02.05.04.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:35:26 -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 v2 2/4] string: Allow 2-argument strscpy() Date: Mon, 5 Feb 2024 04:35:22 -0800 Message-Id: <20240205123525.1379299-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205122916.it.909-kees@kernel.org> References: <20240205122916.it.909-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=6105; i=keescook@chromium.org; h=from:subject; bh=mJaVHVptIigPFzZ/zR9JERbMLBqYYNcKpf70/6AE0qw=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwNYLWD0Xozo5J+xTTvfw+MfKqBkxQCUARxBeI UMtcoHx5rmJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcDWCwAKCRCJcvTf3G3A JtCJD/4hwxxn2am+sHHbObqVTYQbb4y+Jpm7wgzrqcFhv4UmfzqMvuqRRqoxDHzkJsp1hacbVTJ K6hWg4adcpeNJMYH4H1Xt+YxwDSMUPO+6BDZo3zQZBXI088Sa/2uw52X5hUPd5dC0S2erW8Z9nh +6XajX+dBTtbFwYc1D7NhWxp5gplKA2PfhmX1mE6JecX79snifY2o+UN5MNw9u/ar2U6AzIdiaH FvSCjNAVhyNZmbkSdMV5qtiscZ6iXAbFmAtkaYYOthNgTmG9JPJGYqkoW43tKeVp+66jx7eylHy 2GChRBCMIlIUK/8zXwevu4wRzpXfVVVeLQlRj/B2G60XFIFt7kpWjZ1dPCDtXqbK4L3b9ZmF22F zgvbGcoG35MgHtijYSTg2K+6noY77qzG8OXVGYY6vE8V1sukn+PG6DV/rBCSxRuysrWi/RjwroS T02QCvY4jfm5ucmnz8tFBRfo1uaBzqSaBkKYdbS2mZo4u34kJrsBf4r2YI2Ab2yWkLFGPoWbJzz zujgvMOu4Y5t5ZoBK5QYH3X1SARTmWRJVERp8KUnUq4OHfPRMc2l0IqJXX/e1myzDyZzKeDbMh4 JSujIp/sfCzqdgCpxSvSn/z4eiFlJg0f6h92lMxovebpoLXQgUHfMfkjr/XYJP9t92yCCR/KoLL cfiYj2mSD9fbCcA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3Dum 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#L151= 7 [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 | 35 +++++++++++++++++++++++++++++++++- lib/string.c | 4 ++-- 4 files changed, 40 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, ...) =20 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) =20 /* 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 PO= S p) } =20 /* Defined after fortified strnlen() to reuse it. */ -extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strsc= py); -/** - * 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 destinati= on - * @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 PO= S 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 * co= nst POS q, size_t size) { /* Use string size rather than possible enclosing struct size. */ const size_t p_size =3D __member_size(p); diff --git a/include/linux/string.h b/include/linux/string.h index 03f59cf7fe72..a21371aa2fd6 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -67,9 +67,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 =20 +/* + * 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 =20 #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 =3D WORD_AT_A_TIME_CONSTANTS; size_t max =3D count; @@ -170,7 +170,7 @@ ssize_t strscpy(char *dest, const char *src, size_t cou= nt) =20 return -E2BIG; } -EXPORT_SYMBOL(strscpy); +EXPORT_SYMBOL(sized_strscpy); #endif =20 /** --=20 2.34.1 From nobody Sun Feb 8 11:43:50 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 C5D221C2B2 for ; Mon, 5 Feb 2024 12:35:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136530; cv=none; b=pN3ADbOHGebP0vrBwdWZ9LJL0kPFTg1eyWk3dY/nd6zDBtdAYdFQwgECdd7p5Ni0RX8XU1mJfYDmrk1iLEOn0aUjMWQUkUi2uEqmkvK1S97t+SaYaNq8of2R4P15AfXLYEjUYuA5IF31Vd0v6TJwYsVT4dfLNdI4kLOagZih1ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136530; c=relaxed/simple; bh=SCI5AVp/xP0K3m/Awqe4PFOk3LYWSNoQ5BQ2wUkaFRk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y1oCFKUbLjRAJZvH6ZO3W2bCgDnJdSgp2FCJFOBDzmBarWT9HZvnHZieTt/itKZJMR8zoZ3c2UaaSXM820mjTOt5Nl3mfJikAe6+x8Tp4wamgDr6WWYFNQCesUz2snYTPbN1H03E83VCODoRcO9K6YtCQ/d09RDE8+7CzMpc8H0= 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=oAP5NDby; arc=none smtp.client-ip=209.85.210.175 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="oAP5NDby" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6e0353e47daso833086b3a.3 for ; Mon, 05 Feb 2024 04:35:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707136528; x=1707741328; 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=c5JNANRkgim7oX9erlFG0aAyh/IbhwSXSNW8ijW6EiU=; b=oAP5NDbytlUEJbHmz7Jlie+xzHOo5gxHYx9jxDHMIRp80EjA1mB+uHxaTsYJn9Uhyz uJ13m1cBTGRTQSlY623KGbJLPENs6jovpNb8cyqEdPcizwtboe5vSLZmhw4U5e5OdJnK X9oDpGi0uLLbJ6qTT546qKNZA+T2m/JYjNfXQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707136528; x=1707741328; 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=c5JNANRkgim7oX9erlFG0aAyh/IbhwSXSNW8ijW6EiU=; b=tbteeoANXgo9ko0JKrSt63bGB1oUcVqEkN5o4S2qQXVI1vm8X9jHCbHQ1lqs3B6HgC puxJUzsrFqL/6UYG1mbgBQlyoNUiC8Xdhw1NzxiAFt1F5R2eM9iTw9QcpeL1UqWkmion t5f7QutaO96JkFmFJ2aGwuXqZhpwwtoF4KuwfkesviHINHGfMGj6vQtR0tqcvvTx2VUR 1AdX17nVb5RcxeLQnj3hykER9/IjuB45cpfRLLe4Yw+D2v8gqxtarAeGA1ozkWmC1uYB FwU7HIWj4BqWZfjFFveT/mfBw98LMhDXCheN/zADY59KrygesusdcA4pLj56lp7RycxB rZbQ== X-Gm-Message-State: AOJu0Yz6uFf5tOEwYasA6+6+5b1+Vx++/TkB8ki09Jgqlwh5X2mlpZlF fxt/n4AkpeN700dM+qmMYSxB/a5/OnSEMSu+S0fQovCMv/QsH/aZxkwaO70NZbTz3ZWcDB3riSm dPA== X-Google-Smtp-Source: AGHT+IGcyv6oUMvzmDLTrZfuvHow7dYZGNngdUIPt9/kNHjY4m35Nv3RLQZaeWenbYTl6YwPkxUvvA== X-Received: by 2002:a05:6a20:6f05:b0:19c:922b:13e5 with SMTP id gt5-20020a056a206f0500b0019c922b13e5mr8107466pzb.59.1707136528154; Mon, 05 Feb 2024 04:35:28 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWagow0Xuqfyuxmq8ypSMoRdf8e/fatW5Km0Cy9EIBfS8Bu8otndraIk1Rh+l1J50Uw+3uqo4+kLRteVP/c7c1JUENfvDvsQ+hMrhl1w1jU2SFpUwdWIdPI9Ng3sTrGFPIIzzPBu/YJOCLERL1TMLWN/oWbQcvGo5iWImJ4xtyYgPGGi7AZZFEGJtyHcI/UMBpAglK7vaUg2S0rG/28zLmr9RYcafYYGZ0ixHConRJM03tyoZ/qvME8mBVetDJggGibbRGWBrh4KKdR53Qkzfg24dqBvyYrvOSh8OUIo17dFzVqnEBZ4Ln2hbuMs9mJWxI9+XpsBsO0nUf3KYAOkzScVnndD5QEaq64qo4sGONV7ZPEPAwutGac5THprucFy/HSyFp8zJ+TZo+/ODwoIoGgyvOgOOTJ9KOn04NF42iqgiiO1A5InQdi7W1c3z9PE7agbvVqFvOTCa4wRa2yO+c+ Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id 20-20020a631554000000b005d8be4c125csm6881440pgv.80.2024.02.05.04.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:35:26 -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 v2 3/4] string: Allow 2-argument strscpy_pad() Date: Mon, 5 Feb 2024 04:35:23 -0800 Message-Id: <20240205123525.1379299-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205122916.it.909-kees@kernel.org> References: <20240205122916.it.909-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=SCI5AVp/xP0K3m/Awqe4PFOk3LYWSNoQ5BQ2wUkaFRk=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwNYLp7ZL5ezJvmcodRQbG2/1dyQRVQycV79xB /6BzOPeySKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcDWCwAKCRCJcvTf3G3A Jvo2EACPXKqow/a1M7hjAGcliIdGfXhiepw3TyxOozHwHyFCXonE4ajdHlUJPs10NbzydfvJN7g nLdam0lewEg/i3/kbxy4xNDPDUziZd8urp1MnoqrSxnPXxfieBSy0SCTjCijMcy1XhJ53bTP8VJ Xp0kj8hktnbsgJorBULPaACOrK3SbM5jvagSfNhnkAdfqro/ds2EMVlBNUVdyUF/gbWoor0NaiM DpMOv9I1wf6/UFA0xRJ4Kb9+XJOEvAbl4Qan9i1Zn+lIO3w4pG4A9HtCm5hysX8KV6hFGiKxbjZ FL5rRAr5YoBMLj5YFVXFBmogCV1il7zo39IGffAMXJ8p35Rg0DSCoC1jEIThnS92DfNLZq5q5ht hRB9o5XCsNTrkfbgjD4HHEAAKUqDCjem3uMdkjE42CuSPRN7z8YJ0yTvlGg+9RjNm463Jm0JDQU sXFFR35pDYH9r2OWO4i4xhIKRTZvFoXuV427qHabe3Qvr6EhracQfE6W5VlvrO3GwdQ2YS9oSe7 XMMeWFatpxe8VL/1+itq2dnnt8iWYkwr/I2kjjyhfDcT1MBir9g+ACaU8QRv21G9Fi7Jkga4yfU a5QAwX2USngq3ZMAitufdkf+VgoUnUqzee4E0lP9gg7m86XYSeJrRiOQGNd8X9xehhP5bKi6TD0 TkZFNDYTgcD33rA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 a21371aa2fd6..4f0f27013418 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -78,6 +78,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) =20 +#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 @@ -103,6 +107,18 @@ ssize_t sized_strscpy(char *, const char *, size_t); #define strscpy(dst, src, ...) \ CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) =20 +#define sized_strscpy_pad(dest, src, count) ({ \ + char *__dst =3D (dest); \ + const char *__src =3D (src); \ + const size_t __count =3D (count); \ + ssize_t __wrote; \ + \ + __wrote =3D sized_strscpy(__dst, __src, __count); \ + if (__wrote >=3D 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 @@ -123,17 +139,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 =3D (dest); \ - const char *__src =3D (src); \ - const size_t __count =3D (count); \ - ssize_t __wrote; \ - \ - __wrote =3D strscpy(__dst, __src, __count); \ - if (__wrote >=3D 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__) =20 #ifndef __HAVE_ARCH_STRCAT extern char * strcat(char *, const char *); --=20 2.34.1 From nobody Sun Feb 8 11:43:50 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 5BA231C6AF for ; Mon, 5 Feb 2024 12:35:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136529; cv=none; b=GfQb//zv/xZhl1q5GPxkDSWn50emwdlXC1sU2B5+xqQt9URWwxhU60NaHVzEautiomIv8/ayC5AHct7GETro+bIPJMYHsiSzGh03F/ByMcFSU1hio+BVfk0SHfD+SkNcsRT6RrwNjBy9UULxuBxBjre0sdpwz0HP+HyJuOtVhNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136529; c=relaxed/simple; bh=5d6UiYMglkTbWmRhqsEoPg9BNX2q12MK63EVcZLBafI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jOJ3bQ1V0xnilqU5UpOdbCu1ttZi4H6jlYcvPvwxdeXmM9PBipLM7fPLLmzxPGWWHPcH0kz7sc6woXvI6dSeJFCHLZ53QxCh2n9XOeNi1rfJ9A6Vsqo+LXm/XIETa5iJej3uwc3ZtSMW0Oo7PMDne+yeF+khnJWV3Q0z+gpYEHM= 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=ZtrYtgLE; arc=none smtp.client-ip=209.85.214.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ZtrYtgLE" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1d918008b99so31962165ad.3 for ; Mon, 05 Feb 2024 04:35:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707136527; x=1707741327; 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=ZtrYtgLE4lrp4hep7JNiDbGQt0SinOyw1fqb5W9sqzCg3bEWODfWAHbF/grbhueypU GxZ2fTweRhyPmz8nBXFX1Xx9bdsTemSnC9II1PhocTspmrUyiQMH58+cOtptElaKytKB OUD9jpl5LwJxxqNitFOTHSdwJD32oeDIaf4Hk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707136527; x=1707741327; 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=GysnQnWReolSI+eFghALNxYSj8tVNQpY8m01QZXZwxBFRrWoW2TeAn62bd1+NLkMRw yDwT98E5Jbsm8ATEKXvRgQc3rGE0GuaRl67xfn1yn4+E8IWMK/MH9fvlxPDzd9AN7GdR 2Iy0OTpCBYBummMl58v05q42LXRNMi9L4xxN8Nk0FO+KLgkN5k+9vD7qRsmYjNLoL34m U/b3uWpO2wEDbRzBXR/u7bfMVir81Ed73Sl1EEC+fKG9CnNvEocW+SP7rgUn0yoQwvgN ZCsgSyCtcuy/PoG1aXWMxUlHyoGhVqzvleNcuxOH/SZtqdwdYYjPNgiYTPAhaJl59Ivk VZ+g== X-Gm-Message-State: AOJu0YzTO/+9Trrt+yKzKDhjNWCxSazLmlSlg30N/IK31vaRlccKbjoP rJk24XemSYlgltfpe90F+5w8jsa3B/n6qCsag4sdslBBDLzr2pUOYW4JxBD4ig== X-Google-Smtp-Source: AGHT+IHkHiW4eCpoZrH5VS9fL3wKdw7R87tdm2Mzop8u3my/RGXZjyKz1iWzGWMARGlP4o8yTfJaLQ== X-Received: by 2002:a17:903:11d0:b0:1d6:f1cb:7a95 with SMTP id q16-20020a17090311d000b001d6f1cb7a95mr17046893plh.57.1707136526725; Mon, 05 Feb 2024 04:35:26 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWByyKi34EiBtuytAcMo6KMj6URrFVZ74/I4YaRUx7jw6ft7ydbHcPrJMa34hmYyUIhs9cH2qnU/xx8FhYaq9HJ0U3MkBMjasin/nW1UcbkS2a7mj1SzmE8BTcYjHDA7AOHT22fZ6eRugXVe6Wi/7kvmoRMWLibqZswU8V70tczIDUzmG0UC+65vvl+/8O1BFB6VGOFye62MbQrrtinDcuyGokYnuGz8v+wbZScio1q/AWhlvG6Wos7OcFTU0oo5aq1W5sjgZP5CnfVqKgNyi9AF2mm5agu8+3qn8u+xENHnq7e0qzvAO+zwRTYWRqvAyId9Ee58YJtv2tSp6QysUWodSAEnx8YCAaXJj6jvEZShKsJWbRXUSMoFbCiJu2BXFJGGykEt9ZXjazuc5b8kPZmaCsdA1dnyEVd8mAxehAYat+/ZuaQMIv88Lgc1EXfOcl6F4SWfULyk2KSIRZSmKsH Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id mn16-20020a1709030a5000b001d8aa88f59esm6202642plb.110.2024.02.05.04.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:35:25 -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 v2 4/4] um: Convert strscpy() usage to 2-argument style Date: Mon, 5 Feb 2024 04:35:24 -0800 Message-Id: <20240205123525.1379299-4-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205122916.it.909-kees@kernel.org> References: <20240205122916.it.909-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/cbcAmAcsmYgBlwNYLb+R0+gw7GgnI3QI4KGLyNGSb52JGRxet5 1XMuj4aUkGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcDWCwAKCRCJcvTf3G3A JrXHD/4k2l9SR9HgecqdmGaTxym3IrejezIaI8b5MzIPvoImcFWT0Mf8aXTgXEyxq8uWxUSGeHF WbBtFjN5vndp+eT6d7Aau0bKlAgZIyzEb5oh+f4vwVnyErCKtq8eTR2yjy+ki6dPXhZ6yV/6yL/ GX58ENWMI4OasMu+pLJDYzsdkCia0HyOtB6IYN+UcnWWEdLT4p6q1WN8uLfGUgxAWDif1KgsnHt wL1eZTaHu04b3TXgAbUYcHaAT7WdkDwcotPpcAshvhqyhILsV9GaRrkXZIq4mVVw3kn7KAuO8VU wmm4YHXaQgZO6P0F3dbT9CVdvBFQ95hne9VsR+LTrnh+eLKUyXYTtBZcE7qo/gC8P6gV7tmFTSn tyo0uiKhU1h4ykOlP4qxbmRQXTceEMzGZaWf7PuO8sOeHjfssdyktyPLKxuB+qfDGp5B7DLfel8 6kgbcZJ/PgHx45rckxp+K6ea68m6d5S5K3hCpfNcrmAbg3o4N8DCSLcC3cimv9KJtzQxDhYoRsT HO7X2O25NJL+WtwX62RvK8qmvioZ4Lxadnhl9DWe6o4Mi/bLK8es5poQg3jOy4wqI0x7F9HnhOX vfIrHiKUhhofqwMXaGiv2ZxH4yN/5Q7mUbiv/NPgHVG7c9p7EckxeVDWYw0VSyqUiFM2rTbu0TA qVzP3WSRN66gMAw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ARCH=3Dum 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); } =20 static const struct ethtool_ops uml_net_ethtool_ops =3D { 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_dev= ice *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); } =20 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 =3D IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; - strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); + strscpy(ifr.ifr_name, iface); =20 err =3D ioctl(fd, TUNSETIFF, (void *) &ifr); if (err !=3D 0) { @@ -171,7 +171,7 @@ static int create_raw_fd(char *iface, int flags, int pr= oto) 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 =3D -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)) =20 /* 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/dr= ivers/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 contro= l_me, sprintf(data_fd_buf, "%d", data_remote); sprintf(version_buf, "%d", UML_NET_VERSION); if (gate !=3D NULL) { - strscpy(gate_buf, gate, sizeof(gate_buf)); + strscpy(gate_buf, gate); args =3D setup_args; } else args =3D nosetup_args; diff --git a/arch/um/os-Linux/drivers/tuntap_user.c b/arch/um/os-Linux/driv= ers/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 =3D 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 =3D -errno; printk(UM_KERN_ERR "TUNSETIFF failed, errno =3D %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; =20 - strscpy(umid, name, sizeof(umid)); + strscpy(umid, name); =20 return 0; } @@ -262,7 +262,7 @@ static int __init make_umid(void) make_uml_dir(); =20 if (*umid =3D=3D '\0') { - strscpy(tmp, uml_dir, sizeof(tmp)); + strscpy(tmp, uml_dir); strlcat(tmp, "XXXXXX", sizeof(tmp)); fd =3D mkstemp(tmp); if (fd < 0) { --=20 2.34.1