From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id BA7B81FF0C0 for ; Wed, 18 Nov 2026 10:51:20 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 07FE421D6D; Wed, 18 Nov 2026 10:45:11 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-mail-forward v2 59/63] limit size of mail read from stdin Date: Wed, 18 Nov 2026 10:43:43 +0100 Message-ID: <20261118094348.273646-60-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118094348.273646-1-l.wagner@proxmox.com> References: <20261118094348.273646-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794995042543 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.137 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) PROLO_LEO2 0.1 Meta Catches all Leo drug variations so far RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: KGP7DZ4FBIQK6P24UTLFV3C4SR7XE5UV X-Message-ID-Hash: KGP7DZ4FBIQK6P24UTLFV3C4SR7XE5UV X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Previously, the whole mail passed via stdin was read into memory without any bound. Since the binary is invoked by postfix for every mail delivered to root, an unexpectedly large mail could cause excessive memory usage and result in an equally large entry in the notification queue. For mails sent from local system daemons, we don't expect any email attachments or the like, so a limit of 1MB should be more than enough. Signed-off-by: Lukas Wagner --- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a60df9..60c8336 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,16 +143,33 @@ impl Product { } } -/// Read data from stdin, until EOF is encountered. -fn read_stdin() -> Result, Error> { +/// Maximum number of bytes read from stdin, as a safeguard against excessively large mails. +const MAX_MAIL_SIZE: u64 = 1024 * 1024; + +/// Read data from `reader` until EOF is encountered. +/// +/// Returns an error if the input exceeds `limit` bytes. +fn read_limited(reader: impl Read, limit: u64) -> Result, Error> { let mut input = Vec::new(); - let stdin = std::io::stdin(); - let mut handle = stdin.lock(); + // Read one additional byte to detect whether the input exceeds the limit. + let mut handle = reader.take(limit + 1); handle.read_to_end(&mut input)?; + + if input.len() as u64 > limit { + bail!("mail exceeds maximum size of {limit} bytes"); + } + Ok(input) } +/// Read data from stdin until EOF is encountered. +/// +/// Returns an error if the input exceeds [`MAX_MAIL_SIZE`] bytes. +fn read_stdin() -> Result, Error> { + read_limited(std::io::stdin().lock(), MAX_MAIL_SIZE) +} + fn installed_product() -> Result { Product::ALL .iter() @@ -232,3 +249,34 @@ fn main() -> ExitCode { ExitCode::SUCCESS } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn read_limited_below_limit() { + let data = b"Subject: test\n\nbody\n"; + let input = read_limited(&data[..], 64).unwrap(); + assert_eq!(input, data); + } + + #[test] + fn read_limited_exactly_at_limit() { + let data = vec![b'a'; 64]; + let input = read_limited(&data[..], 64).unwrap(); + assert_eq!(input, data); + } + + #[test] + fn read_limited_rejects_above_limit() { + let data = vec![b'a'; 65]; + assert!(read_limited(&data[..], 64).is_err()); + } + + #[test] + fn read_limited_empty() { + let input = read_limited(&b""[..], 64).unwrap(); + assert!(input.is_empty()); + } +} -- 2.47.3