From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id CADB4918C8 for ; Thu, 15 Feb 2024 16:20:15 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id AB22016702 for ; Thu, 15 Feb 2024 16:20:15 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Thu, 15 Feb 2024 16:20:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 863304841C for ; Thu, 15 Feb 2024 16:20:14 +0100 (CET) From: Stefan Sterz To: pbs-devel@lists.proxmox.com Date: Thu, 15 Feb 2024 16:19:55 +0100 Message-Id: <20240215152001.269490-7-s.sterz@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240215152001.269490-1-s.sterz@proxmox.com> References: <20240215152001.269490-1-s.sterz@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.083 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox 06/12] sys: crypt: use constant time comparison for password verification X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Feb 2024 15:20:15 -0000 by using `openssl::memcmp::eq()` we can avoid potential timing side channels as its runtime only depends on the length of the arrays, not the contents. this requires the two arrays to have the same length, but that should be a given since the hashes should always have the same length. Signed-off-by: Stefan Sterz --- proxmox-sys/Cargo.toml | 3 ++- proxmox-sys/src/crypt.rs | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/proxmox-sys/Cargo.toml b/proxmox-sys/Cargo.toml index 5ddbe21..1a44702 100644 --- a/proxmox-sys/Cargo.toml +++ b/proxmox-sys/Cargo.toml @@ -16,6 +16,7 @@ lazy_static.workspace = true libc.workspace = true log.workspace = true nix.workspace = true +openssl = { workspace = true, optional = true } regex.workspace = true serde_json.workspace = true serde = { workspace = true, features = [ "derive" ] } @@ -29,5 +30,5 @@ proxmox-time.workspace = true default = [] logrotate = ["dep:zstd"] acl = [] -crypt = [] +crypt = ["dep:openssl"] timer = [] diff --git a/proxmox-sys/src/crypt.rs b/proxmox-sys/src/crypt.rs index fa10911..3313f66 100644 --- a/proxmox-sys/src/crypt.rs +++ b/proxmox-sys/src/crypt.rs @@ -155,9 +155,15 @@ pub fn encrypt_pw(password: &str) -> Result { /// Verify if an encrypted password matches pub fn verify_crypt_pw(password: &str, enc_password: &str) -> Result<(), Error> { let verify = crypt(password.as_bytes(), enc_password.as_bytes())?; - if verify != enc_password { + + // `openssl::memcmp::eq()`'s runtime does not depend on the content of the arrays only the + // length, this makes it harder to exploit timing side-channels. + if verify.len() != enc_password.len() + || !openssl::memcmp::eq(verify.as_bytes(), enc_password.as_bytes()) + { bail!("invalid credentials"); } + Ok(()) } -- 2.39.2