From nobody Fri Oct 25 14:18:08 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (unknown [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XZlHh4qMvz5ZlKL; Fri, 25 Oct 2024 14:18:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XZlHh4L90z4M42; Fri, 25 Oct 2024 14:18:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729865888; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aJ8kTZnPbq2p5dQiz1Ydmnxl8K0k83klnUoSZyXmHDU=; b=tJaCyLFamgQEFZ8Uc0RttgGxyrm86O1JW7dmWpn5gbXxaZTOeMslMfI/0JZ5s9STjX/XYH 2lvjb1nna/bZqmNWqD1a+0X9xax6ma9nzwmZuM4lPM4ETMNbbJcd0jZeMjYuA7NbRzNW4W LhelpataCm1+l5lniBVpmhijaPACrl+FONumU+5CUdm69iUNUknXBw9wm5LBHHFuwHeNBB Y+rSOWSLB/+/2InyKJ3Z/UPUnLRC2hsJPFr2BOFm1dCirwrQEwie90I9ZunW9aYAhzjzzi PVGfrkhxVN/AzFgRcyOIpHrCL+F6ZJ9pTKVcCkV3MXjBZUWUldqbiB15Sb7e9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729865888; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aJ8kTZnPbq2p5dQiz1Ydmnxl8K0k83klnUoSZyXmHDU=; b=i3QryYyBLaANQynR84qgI7qd0HP7Sp87qp7c6bXOAW/NsqgC5QUxvE2RcTrzcQQogPji0w HLzFSANOCtLEIVH1Uo/tHHnwaMApuTg6l3IvvjaPeZeOcNWd/IKnz23ZAmEgn5FxpibL6j 8F2zQwcywyL1xb9Cc9MV/SvgmbFHY8Vp6rLMTa6H1hfdsHucYEcDICxn8SYCE7pUpdYWTu UJFhusTjYtDMkIzvC/rWhdLbtL+l05MmIVp8Lkl3V7otl1aI7/PPCf1hrSL8pB00q0kbaO 0VNU4Zlv37raSRYUc2JxI9yrhJasUSEbA5ODO1FOXcwZZXE7YDaKZkCx4r+0og== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729865888; a=rsa-sha256; cv=none; b=GwKI0DkYwB6jfQZw1/pwMELVghhPgKAoMINNQIB0SJWk4mxbBG9+Vri5MYpLYtgMAiwvt8 V2sTnjC9m/Cs9ZA5iQtq1pTkyDAxldetK29T5FUpmRsDjtpdZjV+TVuh//k3lhygUq1kVd FHFC4sixo5FDktmOGenXLwBWBin6vYd0F9MJ1HUKXajeAjaJuySW5xUpLZ2zdw1lAqhDrC w8DXwJNjnhT5i9TzfNWq3ZHpAc6uhu4BZGId6+tYGLTdYOgYtug269Od2z0fKq/7farjze Ek8Ov7b8zCZeyzKpycqg+UGt7ppc51JYoLDVQeCIIeZrjLIZpx1yQpzpqwST0g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XZlHh3xTHzSv0; Fri, 25 Oct 2024 14:18:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49PEI84P017088; Fri, 25 Oct 2024 14:18:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49PEI8xT017085; Fri, 25 Oct 2024 14:18:08 GMT (envelope-from git) Date: Fri, 25 Oct 2024 14:18:08 GMT Message-Id: <202410251418.49PEI8xT017085@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: a2c0d2026fb4 - main - lib/libcrypt: use explicit_bzero() to clear sensitive buffers List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a2c0d2026fb422ade2171da4bc6d5d2773b268a6 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=a2c0d2026fb422ade2171da4bc6d5d2773b268a6 commit a2c0d2026fb422ade2171da4bc6d5d2773b268a6 Author: Robert Clausecker AuthorDate: 2024-10-10 09:08:35 +0000 Commit: Robert Clausecker CommitDate: 2024-10-25 14:02:38 +0000 lib/libcrypt: use explicit_bzero() to clear sensitive buffers Prevent a potentially sufficiently smart compiler from optimising away our attempts to clear sensitive buffers. A related change was discussed and rejected in D16059, but I don't believe the reasoning there applies: the code clearly documents its intent that the `memset` calls clear sensitive buffers so they don't hang around. `explicit_bzero` is the appropriate function for this purpose. A potential performance disadvantage seems less important: the functions in crypt are specifically designed to be slow, so a few extra calls to guarantee that sensitive buffers are cleared does not significantly affect runtime. See also: D16059 Reviewed by: delphij, kevans MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D47037 --- lib/libcrypt/crypt-md5.c | 5 +++-- lib/libcrypt/crypt-sha256.c | 7 ++++--- lib/libcrypt/crypt-sha512.c | 7 ++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/libcrypt/crypt-md5.c b/lib/libcrypt/crypt-md5.c index 52d38c64cdeb..609fbbf67b77 100644 --- a/lib/libcrypt/crypt-md5.c +++ b/lib/libcrypt/crypt-md5.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "crypt.h" @@ -84,7 +85,7 @@ crypt_md5(const char *pw, const char *salt, char *buffer) (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl)); /* Don't leave anything around in vm they could use. */ - memset(final, 0, sizeof(final)); + explicit_bzero(final, sizeof(final)); /* Then something really weird... */ for (i = strlen(pw); i; i >>= 1) @@ -140,7 +141,7 @@ crypt_md5(const char *pw, const char *salt, char *buffer) *buffer = '\0'; /* Don't leave anything around in vm they could use. */ - memset(final, 0, sizeof(final)); + explicit_bzero(final, sizeof(final)); return (0); } diff --git a/lib/libcrypt/crypt-sha256.c b/lib/libcrypt/crypt-sha256.c index 35c36bf93f3d..6da1d518b12d 100644 --- a/lib/libcrypt/crypt-sha256.c +++ b/lib/libcrypt/crypt-sha256.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "crypt.h" @@ -234,9 +235,9 @@ crypt_sha256(const char *key, const char *salt, char *buffer) * the SHA256 implementation as well. */ SHA256_Init(&ctx); SHA256_Final(alt_result, &ctx); - memset(temp_result, '\0', sizeof(temp_result)); - memset(p_bytes, '\0', key_len); - memset(s_bytes, '\0', salt_len); + explicit_bzero(temp_result, sizeof(temp_result)); + explicit_bzero(p_bytes, key_len); + explicit_bzero(s_bytes, salt_len); return (0); } diff --git a/lib/libcrypt/crypt-sha512.c b/lib/libcrypt/crypt-sha512.c index 640398afadc4..b760623b5d8d 100644 --- a/lib/libcrypt/crypt-sha512.c +++ b/lib/libcrypt/crypt-sha512.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "crypt.h" @@ -246,9 +247,9 @@ crypt_sha512(const char *key, const char *salt, char *buffer) * the SHA512 implementation as well. */ SHA512_Init(&ctx); SHA512_Final(alt_result, &ctx); - memset(temp_result, '\0', sizeof(temp_result)); - memset(p_bytes, '\0', key_len); - memset(s_bytes, '\0', salt_len); + explicit_bzero(temp_result, sizeof(temp_result)); + explicit_bzero(p_bytes, key_len); + explicit_bzero(s_bytes, salt_len); return (0); }