From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id EC9CA1FF0B7 for ; Fri, 13 Nov 2026 16:16:11 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 3A9A121C44; Fri, 13 Nov 2026 16:10:15 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-mail-forward 55/55] limit size of mail read from stdin Date: Fri, 13 Nov 2026 16:08:28 +0100 Message-ID: <20261113150828.756836-56-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261113150828.756836-1-l.wagner@proxmox.com> References: <20261113150828.756836-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: 1794582530016 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.202 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) 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: ZWIOKLDVTWQKJQ5WSAZTZTWLLCFT4ZXC X-Message-ID-Hash: ZWIOKLDVTWQKJQ5WSAZTZTWLLCFT4ZXC 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 1affeda..e4a2f08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,16 +128,33 @@ const PRODUCTS: &[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<&'static Product, Error> { PRODUCTS .iter() @@ -216,3 +233,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