Skip to main content

tor_hsservice/
replay.rs

1//! Facility for detecting and preventing replays on introduction requests.
2//!
3//! If we were to permit the introduction point to replay the same request
4//! multiple times, it would cause the service to contact the rendezvous point
5//! again with the same rendezvous cookie as before, which could help with
6//! traffic analysis.
7//!
8//! (This could also be a DoS vector if the introduction point decided to
9//! overload the service.)
10//!
11//! Because we use the same introduction point keys across restarts, we need to
12//! make sure that our replay logs are already persistent.  We do this by using
13//! a file on disk.
14
15mod ipt;
16#[cfg(feature = "hs-pow-full")]
17mod pow;
18
19use crate::internal_prelude::*;
20
21/// A probabilistic data structure to record fingerprints of observed Introduce2
22/// messages.
23///
24/// We need to record these fingerprints to prevent replay attacks; see the
25/// module documentation for an explanation of why that would be bad.
26///
27/// A ReplayLog should correspond to a `KP_hss_ntor` key, and should have the
28/// same lifespan: dropping it sooner will enable replays, but dropping it later
29/// will waste disk and memory.
30///
31/// False positives are allowed, to conserve on space.
32pub(crate) struct ReplayLog<T> {
33    /// The inner probabilistic data structure.
34    seen: data::Filter,
35    /// Persistent state file etc., if we're persistent
36    ///
37    /// If is is `None`, this RelayLog is ephemeral.
38    file: Option<PersistFile>,
39    /// [`PhantomData`] so rustc doesn't complain about the unused type param.
40    ///
41    /// This type represents the type of data that we're storing, as well as the type of the
42    /// key/name for that data.
43    replay_log_type: PhantomData<T>,
44}
45
46/// A [`ReplayLog`] for [`Introduce2`](tor_cell::relaycell::msg::Introduce2) messages.
47pub(crate) type IptReplayLog = ReplayLog<ipt::IptReplayLogType>;
48
49/// A [`ReplayLog`] for Proof-of-Work [`Nonce`](tor_hscrypto::pow::v1::Nonce)s.
50#[cfg(feature = "hs-pow-full")]
51pub(crate) type PowNonceReplayLog = ReplayLog<pow::PowNonceReplayLogType>;
52
53/// The length of the [`ReplayLogType::MAGIC`] constant.
54///
55// TODO: If Rust's constant expressions supported generics we wouldn't need this at all.
56const MAGIC_LEN: usize = 32;
57
58/// The length of the message that we store on disk, in bytes.
59///
60/// If the message is longer than this, then we will need to hash or truncate it before storing it
61/// to disk.
62///
63// TODO: Once const generics are good, this should be a associated constant for ReplayLogType.
64pub(crate) const OUTPUT_LEN: usize = 16;
65
66/// A trait to represent a set of types that ReplayLog can be used with.
67pub(crate) trait ReplayLogType {
68    // TODO: It would be nice to encode the directory name as a associated constant here, rather
69    // than having the external code pass it in to us.
70
71    /// The name of this item, used for the log filename.
72    type Name;
73
74    /// The type of the messages that we are ensuring the uniqueness of.
75    type Message;
76
77    /// A magic string that we put at the start of each log file, to make sure that
78    /// we don't confuse this file format with others.
79    const MAGIC: &'static [u8; MAGIC_LEN];
80
81    /// Convert [`Self::Name`] to a [`String`]
82    fn format_filename(name: &Self::Name) -> String;
83
84    /// Convert [`Self::Message`] to bytes that will be stored in the log.
85    fn transform_message(message: &Self::Message) -> [u8; OUTPUT_LEN];
86
87    /// Parse a filename into [`Self::Name`].
88    fn parse_log_leafname(leaf: &OsStr) -> Result<Self::Name, Cow<'static, str>>;
89}
90
91/// Persistent state file, and associated data
92///
93/// Stored as `ReplayLog.file`.
94#[derive(Debug)]
95pub(crate) struct PersistFile {
96    /// A file logging fingerprints of the messages we have seen.
97    file: BufWriter<File>,
98    /// The path to the file for this [`ReplayLog`]
99    path: PathBuf,
100    /// Whether we had a possible partial write
101    ///
102    /// See the comment inside [`ReplayLog::check_for_replay`].
103    /// `Ok` means all is well.
104    /// `Err` means we may have written partial data to the actual file,
105    /// and need to make sure we're back at a record boundary.
106    needs_resynch: Result<(), ()>,
107    /// Filesystem lock which must not be released until after we finish writing
108    ///
109    /// Must come last so that the drop order is correct
110    #[allow(dead_code)] // Held just so we unlock on drop
111    lock: Arc<LockFileGuard>,
112}
113
114/// Replay log files have a `.bin` suffix.
115///
116/// The name of the file is determined by [`ReplayLogType::format_filename`].
117const REPLAY_LOG_SUFFIX: &str = ".bin";
118
119impl<T: ReplayLogType> ReplayLog<T> {
120    /// Create a new ReplayLog not backed by any data storage.
121    #[allow(dead_code)] // TODO #1186 Remove once something uses ReplayLog.
122    pub(crate) fn new_ephemeral() -> Self {
123        Self {
124            seen: data::Filter::new(),
125            file: None,
126            replay_log_type: PhantomData,
127        }
128    }
129
130    /// Create a ReplayLog backed by the file at a given path.
131    ///
132    /// If the file already exists, load its contents and append any new
133    /// contents to it; otherwise, create the file.
134    ///
135    /// **`lock` must already have been locked** and this
136    /// *cannot be assured by the type system*.
137    ///
138    /// # Limitations
139    ///
140    /// It is the caller's responsibility to make sure that there are never two
141    /// `ReplayLogs` open at once for the same path, or for two paths that
142    /// resolve to the same file.
143    pub(crate) fn new_logged(
144        dir: &InstanceRawSubdir,
145        name: &T::Name,
146    ) -> Result<Self, OpenReplayLogError> {
147        let leaf = T::format_filename(name);
148        let path = dir.as_path().join(leaf);
149        let lock_guard = dir.raw_lock_guard();
150
151        Self::new_logged_inner(&path, lock_guard).map_err(|error| OpenReplayLogError {
152            file: path,
153            error: error.into(),
154        })
155    }
156
157    /// Inner function for `new_logged`, with reified arguments and raw error type
158    fn new_logged_inner(path: impl AsRef<Path>, lock: Arc<LockFileGuard>) -> io::Result<Self> {
159        let mut file = {
160            let mut options = OpenOptions::new();
161            options.read(true).write(true).create(true);
162
163            #[cfg(target_family = "unix")]
164            {
165                use std::os::unix::fs::OpenOptionsExt as _;
166                options.mode(0o600);
167            }
168
169            options.open(&path)?
170        };
171
172        // If the file is new, we need to write the magic string. Else we must
173        // read it.
174        let file_len = file.metadata()?.len();
175        if file_len == 0 {
176            file.write_all(T::MAGIC)?;
177        } else {
178            let mut m = [0_u8; MAGIC_LEN];
179            file.read_exact(&mut m)?;
180            if &m != T::MAGIC {
181                return Err(io::Error::new(
182                    io::ErrorKind::InvalidData,
183                    LogContentError::UnrecognizedFormat,
184                ));
185            }
186
187            Self::truncate_to_multiple(&mut file, file_len)?;
188        }
189
190        // Now read the rest of the file.
191        let mut seen = data::Filter::new();
192        let mut r = BufReader::new(file);
193        loop {
194            let mut msg = [0_u8; OUTPUT_LEN];
195            match r.read_exact(&mut msg) {
196                Ok(()) => {
197                    let _ = seen.test_and_add(&msg); // ignore error.
198                }
199                Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
200                Err(e) => return Err(e),
201            }
202        }
203        let mut file = r.into_inner();
204        file.seek(SeekFrom::End(0))?;
205
206        let file = PersistFile {
207            file: BufWriter::new(file),
208            path: path.as_ref().to_owned(),
209            needs_resynch: Ok(()),
210            lock,
211        };
212
213        Ok(Self {
214            seen,
215            file: Some(file),
216            replay_log_type: PhantomData,
217        })
218    }
219
220    /// Truncate `file` to contain a whole number of records
221    ///
222    /// `current_len` should have come from `file.metadata()`.
223    // If the file's length is not an even multiple of MESSAGE_LEN after the MAGIC, truncate it.
224    fn truncate_to_multiple(file: &mut File, current_len: u64) -> io::Result<()> {
225        let excess = (current_len - T::MAGIC.len() as u64) % (OUTPUT_LEN as u64);
226        if excess != 0 {
227            file.set_len(current_len - excess)?;
228        }
229        Ok(())
230    }
231
232    /// Test whether we have already seen `message`.
233    ///
234    /// If we have seen it, return `Err(ReplayError::AlreadySeen)`.  (Since this
235    /// is a probabilistic data structure, there is a chance of returning this
236    /// error even if we have we have _not_ seen this particular message)
237    ///
238    /// Otherwise, return `Ok(())`.
239    pub(crate) fn check_for_replay(&mut self, message: &T::Message) -> Result<(), ReplayError> {
240        let h = T::transform_message(message);
241        self.seen.test_and_add(&h)?;
242        if let Some(f) = self.file.as_mut() {
243            (|| {
244                // If write_all fails, it might have written part of the data;
245                // in that case, we must truncate the file to resynchronise.
246                // We set a note to truncate just before we call write_all
247                // and clear it again afterwards.
248                //
249                // But, first, we need to deal with any previous note we left ourselves.
250
251                // (With the current implementation of std::io::BufWriter, this is
252                // unnecessary, because if the argument to write_all is smaller than
253                // the buffer size, BufWriter::write_all always just copies to the buffer,
254                // flushing first if necessary; and when it flushes, it uses write,
255                // not write_all.  So the use of write_all never causes "lost" data.
256                // However, this is not a documented guarantee.)
257                match f.needs_resynch {
258                    Ok(()) => {}
259                    Err(()) => {
260                        // We're going to reach behind the BufWriter, so we need to make
261                        // sure it's in synch with the underlying File.
262                        f.file.flush()?;
263                        let inner = f.file.get_mut();
264                        let len = inner.metadata()?.len();
265                        Self::truncate_to_multiple(inner, len)?;
266                        // cursor is now past end, must reset (see std::fs::File::set_len)
267                        inner.seek(SeekFrom::End(0))?;
268                    }
269                }
270                f.needs_resynch = Err(());
271
272                f.file.write_all(&h[..])?;
273
274                f.needs_resynch = Ok(());
275
276                Ok(())
277            })()
278            .map_err(|e| ReplayError::Log(Arc::new(e)))?;
279        }
280        Ok(())
281    }
282
283    /// Flush any buffered data to disk.
284    #[allow(dead_code)] // TODO #1208
285    pub(crate) fn flush(&mut self) -> Result<(), io::Error> {
286        if let Some(f) = self.file.as_mut() {
287            f.file.flush()?;
288        }
289        Ok(())
290    }
291
292    /// Tries to parse a filename in the replay logs directory
293    ///
294    /// If the leafname refers to a file that would be created by
295    /// [`ReplayLog::new_logged`], returns the name as a Rust type.
296    ///
297    /// Otherwise returns an error explaining why it isn't,
298    /// as a plain string (for logging).
299    pub(crate) fn parse_log_leafname(leaf: &OsStr) -> Result<T::Name, Cow<'static, str>> {
300        T::parse_log_leafname(leaf)
301    }
302
303    /// Remove the file backing this [`ReplayLog`].
304    pub(crate) fn delete(self) -> Result<(), std::io::Error> {
305        if let Some(file) = self.file {
306            std::fs::remove_file(file.path)?;
307        }
308
309        Ok(())
310    }
311}
312
313/// Wrapper around a fast-ish data structure for detecting replays with some
314/// false positive rate.  Bloom filters, cuckoo filters, and xorf filters are all
315/// an option here.  You could even use a HashSet.
316///
317/// We isolate this code to make it easier to replace.
318mod data {
319    use super::{OUTPUT_LEN, ReplayError};
320    use growable_bloom_filter::GrowableBloom;
321
322    /// A probabilistic membership filter.
323    pub(super) struct Filter(pub(crate) GrowableBloom);
324
325    impl Filter {
326        /// Create a new empty filter
327        pub(super) fn new() -> Self {
328            // TODO: Perhaps we should make the capacity here tunable, based on
329            // the number of entries we expect.  These values are more or less
330            // pulled out of thin air.
331            let desired_error_prob = 1.0 / 100_000.0;
332            let est_insertions = 100_000;
333            Filter(GrowableBloom::new(desired_error_prob, est_insertions))
334        }
335
336        /// Try to add `msg` to this filter if it isn't already there.
337        ///
338        /// Return Ok(()) or Err(AlreadySeen).
339        pub(super) fn test_and_add(&mut self, msg: &[u8; OUTPUT_LEN]) -> Result<(), ReplayError> {
340            if self.0.insert(&msg[..]) {
341                Ok(())
342            } else {
343                Err(ReplayError::AlreadySeen)
344            }
345        }
346    }
347}
348
349/// A problem that prevents us from reading a ReplayLog from disk.
350///
351/// (This only exists so we can wrap it up in an [`io::Error`])
352#[derive(thiserror::Error, Clone, Debug)]
353enum LogContentError {
354    /// The magic number on the log file was incorrect.
355    #[error("unrecognized data format")]
356    UnrecognizedFormat,
357}
358
359/// An error occurred while checking whether we've seen an element before.
360#[derive(thiserror::Error, Clone, Debug)]
361pub(crate) enum ReplayError {
362    /// We have already seen this item.
363    #[error("Already seen")]
364    AlreadySeen,
365
366    /// We were unable to record this item in the log.
367    #[error("Unable to log data")]
368    Log(Arc<std::io::Error>),
369}
370
371/// Error occurred while opening replay log.
372#[derive(thiserror::Error, Clone, Debug)]
373#[error("unable to open replay log: {file:?}")]
374pub struct OpenReplayLogError {
375    /// What filesystem object we tried to do it to
376    pub(crate) file: PathBuf,
377    /// What happened
378    #[source]
379    pub(crate) error: Arc<io::Error>,
380}
381
382#[cfg(test)]
383mod test {
384    // @@ begin test lint list maintained by maint/add_warning @@
385    #![allow(clippy::bool_assert_comparison)]
386    #![allow(clippy::clone_on_copy)]
387    #![allow(clippy::dbg_macro)]
388    #![allow(clippy::mixed_attributes_style)]
389    #![allow(clippy::print_stderr)]
390    #![allow(clippy::print_stdout)]
391    #![allow(clippy::single_char_pattern)]
392    #![allow(clippy::unwrap_used)]
393    #![allow(clippy::unchecked_time_subtraction)]
394    #![allow(clippy::useless_vec)]
395    #![allow(clippy::needless_pass_by_value)]
396    #![allow(clippy::string_slice)] // See arti#2571
397    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
398
399    use super::*;
400    use crate::test::mk_state_instance;
401    use rand::{Rng, RngExt};
402    use test_temp_dir::{TestTempDir, TestTempDirGuard, test_temp_dir};
403
404    struct TestReplayLogType;
405
406    type TestReplayLog = ReplayLog<TestReplayLogType>;
407
408    impl ReplayLogType for TestReplayLogType {
409        type Name = IptLocalId;
410        type Message = [u8; OUTPUT_LEN];
411
412        const MAGIC: &'static [u8; MAGIC_LEN] = b"<tor test replay>\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
413
414        fn format_filename(name: &IptLocalId) -> String {
415            format!("{name}{REPLAY_LOG_SUFFIX}")
416        }
417
418        fn transform_message(message: &[u8; OUTPUT_LEN]) -> [u8; OUTPUT_LEN] {
419            message.clone()
420        }
421
422        fn parse_log_leafname(leaf: &OsStr) -> Result<IptLocalId, Cow<'static, str>> {
423            let leaf = leaf.to_str().ok_or("not proper unicode")?;
424            let lid = leaf.strip_suffix(REPLAY_LOG_SUFFIX).ok_or("not *.bin")?;
425            let lid: IptLocalId = lid
426                .parse()
427                .map_err(|e: crate::InvalidIptLocalId| e.to_string())?;
428            Ok(lid)
429        }
430    }
431
432    fn rand_msg<R: Rng>(rng: &mut R) -> [u8; OUTPUT_LEN] {
433        rng.random()
434    }
435
436    /// Basic tests on an ephemeral IptReplayLog.
437    #[test]
438    fn simple_usage() {
439        let mut rng = tor_basic_utils::test_rng::testing_rng();
440        let group_1: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
441        let group_2: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
442
443        let mut log = TestReplayLog::new_ephemeral();
444        // Add everything in group 1.
445        for msg in &group_1 {
446            assert!(log.check_for_replay(msg).is_ok(), "False positive");
447        }
448        // Make sure that everything in group 1 is still there.
449        for msg in &group_1 {
450            assert!(log.check_for_replay(msg).is_err());
451        }
452        // Make sure that group 2 is detected as not-there.
453        for msg in &group_2 {
454            assert!(log.check_for_replay(msg).is_ok(), "False positive");
455        }
456    }
457
458    const TEST_TEMP_SUBDIR: &str = "replaylog";
459
460    fn create_logged(dir: &TestTempDir) -> TestTempDirGuard<TestReplayLog> {
461        dir.subdir_used_by(TEST_TEMP_SUBDIR, |dir| {
462            let inst = mk_state_instance(&dir, "allium");
463            let raw = inst.raw_subdir("iptreplay").unwrap();
464            TestReplayLog::new_logged(&raw, &IptLocalId::dummy(1)).unwrap()
465        })
466    }
467
468    /// Basic tests on an persistent IptReplayLog.
469    #[test]
470    fn logging_basics() {
471        let mut rng = tor_basic_utils::test_rng::testing_rng();
472        let group_1: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
473        let group_2: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
474
475        let dir = test_temp_dir!();
476        let mut log = create_logged(&dir);
477        // Add everything in group 1, then close and reload.
478        for msg in &group_1 {
479            assert!(log.check_for_replay(msg).is_ok(), "False positive");
480        }
481        drop(log);
482        let mut log = create_logged(&dir);
483        // Make sure everything in group 1 is still there.
484        for msg in &group_1 {
485            assert!(log.check_for_replay(msg).is_err());
486        }
487        // Now add everything in group 2, then close and reload.
488        for msg in &group_2 {
489            assert!(log.check_for_replay(msg).is_ok(), "False positive");
490        }
491        drop(log);
492        let mut log = create_logged(&dir);
493        // Make sure that groups 1 and 2 are still there.
494        for msg in group_1.iter().chain(group_2.iter()) {
495            assert!(log.check_for_replay(msg).is_err());
496        }
497    }
498
499    /// Test for a log that gets truncated mid-write.
500    #[test]
501    fn test_truncated() {
502        let mut rng = tor_basic_utils::test_rng::testing_rng();
503        let group_1: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
504        let group_2: Vec<_> = (0..=100).map(|_| rand_msg(&mut rng)).collect();
505
506        let dir = test_temp_dir!();
507        let mut log = create_logged(&dir);
508        for msg in &group_1 {
509            assert!(log.check_for_replay(msg).is_ok(), "False positive");
510        }
511        drop(log);
512        // Truncate the file by 7 bytes.
513        dir.subdir_used_by(TEST_TEMP_SUBDIR, |dir| {
514            let path = dir.join(format!("hss/allium/iptreplay/{}.bin", IptLocalId::dummy(1)));
515            let file = OpenOptions::new().write(true).open(path).unwrap();
516            // Make sure that the file has the length we expect.
517            let expected_len = MAGIC_LEN + OUTPUT_LEN * group_1.len();
518            assert_eq!(expected_len as u64, file.metadata().unwrap().len());
519            file.set_len((expected_len - 7) as u64).unwrap();
520        });
521        // Now, reload the log. We should be able to recover every non-truncated
522        // item...
523        let mut log = create_logged(&dir);
524        for msg in &group_1[..group_1.len() - 1] {
525            assert!(log.check_for_replay(msg).is_err());
526        }
527        // But not the last one, which we truncated.  (Checking will add it, though.)
528        assert!(
529            log.check_for_replay(&group_1[group_1.len() - 1]).is_ok(),
530            "False positive"
531        );
532        // Now add everything in group 2, then close and reload.
533        for msg in &group_2 {
534            assert!(log.check_for_replay(msg).is_ok(), "False positive");
535        }
536        drop(log);
537        let mut log = create_logged(&dir);
538        // Make sure that groups 1 and 2 are still there.
539        for msg in group_1.iter().chain(group_2.iter()) {
540            assert!(log.check_for_replay(msg).is_err());
541        }
542    }
543
544    /// Test for a partial write
545    #[test]
546    #[cfg(target_os = "linux")] // different platforms have different definitions of sigaction
547    fn test_partial_write() {
548        use std::env;
549        use std::os::unix::process::ExitStatusExt;
550        use std::process::Command;
551
552        // TODO this contraption should perhaps be productised and put somewhere else
553
554        const ENV_NAME: &str = "TOR_HSSERVICE_TEST_PARTIAL_WRITE_SUBPROCESS";
555        // for a wait status different from any of libtest's
556        const GOOD_SIGNAL: i32 = libc::SIGUSR2;
557
558        let sigemptyset = || unsafe {
559            let mut set = MaybeUninit::uninit();
560            libc::sigemptyset(set.as_mut_ptr());
561            set.assume_init()
562        };
563
564        // Check that SIGUSR2 starts out as SIG_DFL and unblocked
565        //
566        // We *reject* such situations, rather than fixing them up, because this is an
567        // irregular and broken environment that can cause arbitrarily weird behaviours.
568        // Programs on Unix are entitled to assume that their signal dispositions are
569        // SIG_DFL on entry, with signals unblocked.  (With a few exceptions.)
570        //
571        // So we want to detect and report any such environment, not let it slide.
572        unsafe {
573            let mut sa = MaybeUninit::uninit();
574            let r = libc::sigaction(GOOD_SIGNAL, ptr::null(), sa.as_mut_ptr());
575            assert_eq!(r, 0);
576            let sa = sa.assume_init();
577            assert_eq!(
578                sa.sa_sigaction,
579                libc::SIG_DFL,
580                "tests running in broken environment (SIGUSR2 not SIG_DFL)"
581            );
582
583            let empty_set = sigemptyset();
584            let mut current_set = MaybeUninit::uninit();
585            let r = libc::sigprocmask(
586                libc::SIG_UNBLOCK,
587                (&empty_set) as _,
588                current_set.as_mut_ptr(),
589            );
590            assert_eq!(r, 0);
591            let current_set = current_set.assume_init();
592            let blocked = libc::sigismember((&current_set) as _, GOOD_SIGNAL);
593            assert_eq!(
594                blocked, 0,
595                "tests running in broken environment (SIGUSR2 blocked)"
596            );
597        }
598
599        match env::var(ENV_NAME) {
600            Err(env::VarError::NotPresent) => {
601                eprintln!("in test runner process, forking..,");
602                let output = Command::new(env::current_exe().unwrap())
603                    .args(["--nocapture", "replay::test::test_partial_write"])
604                    .env(ENV_NAME, "1")
605                    .output()
606                    .unwrap();
607                let print_output = |prefix, data| match std::str::from_utf8(data) {
608                    Ok(s) => {
609                        for l in s.split("\n") {
610                            eprintln!(" {prefix} {l}");
611                        }
612                    }
613                    Err(e) => eprintln!(" UTF-8 ERROR {prefix} {e}"),
614                };
615                print_output("!", &output.stdout);
616                print_output(">", &output.stderr);
617                let st = output.status;
618                eprintln!("reaped actual test process {st:?} (expecting signal {GOOD_SIGNAL})");
619                assert_eq!(st.signal(), Some(GOOD_SIGNAL));
620                return;
621            }
622            Ok(y) if y == "1" => {}
623            other => panic!("bad env var {ENV_NAME:?} {other:?}"),
624        };
625
626        // Now we are in our own process, and can mess about with ulimit etc.
627
628        use std::fs;
629        use std::mem::MaybeUninit;
630        use std::ptr;
631
632        fn set_ulimit(size: usize) {
633            unsafe {
634                use libc::RLIMIT_FSIZE;
635                let mut rlim = libc::rlimit {
636                    rlim_cur: 0,
637                    rlim_max: 0,
638                };
639                let r = libc::getrlimit(RLIMIT_FSIZE, (&mut rlim) as _);
640                assert_eq!(r, 0);
641                rlim.rlim_cur = size.try_into().unwrap();
642                let r = libc::setrlimit(RLIMIT_FSIZE, (&rlim) as _);
643                assert_eq!(r, 0);
644            }
645        }
646
647        // This test is quite complicated.
648        //
649        // We want to test partial writes.  We could perhaps have done this by
650        // parameterising IptReplayLog so it could have something other than File,
651        // but that would probably leak into the public API.
652        //
653        // Instead, we cause *actual* partial writes.  We use the Unix setrlimit
654        // call to limit the size of files our process is allowed to write.
655        // This causes the underlying write(2) calls to (i) generate SIGXFSZ
656        // (ii) if that doesn't kill the process, return partial writes.
657
658        test_temp_dir!().used_by(|dir| {
659            let path = dir.join("test.log");
660            let lock = LockFileGuard::lock(dir.join("dummy.lock")).unwrap();
661            let lock = Arc::new(lock);
662            let mut rl = TestReplayLog::new_logged_inner(&path, lock.clone()).unwrap();
663
664            const BUF: usize = 8192; // BufWriter default; if that changes, test will break
665
666            // We let ourselves write one whole buffer plus an odd amount of extra
667            const ALLOW: usize = BUF + 37;
668
669            // Ignore SIGXFSZ (default disposition is for exceeding the rlimit to kill us)
670            unsafe {
671                let sa = libc::sigaction {
672                    sa_sigaction: libc::SIG_IGN,
673                    sa_mask: sigemptyset(),
674                    sa_flags: 0,
675                    sa_restorer: None,
676                };
677                let r = libc::sigaction(libc::SIGXFSZ, (&sa) as _, ptr::null_mut());
678                assert_eq!(r, 0);
679            }
680
681            let demand_efbig = |e| match e {
682                ReplayError::Log(e) if e.kind() == io::ErrorKind::FileTooLarge => {}
683                other => panic!("expected EFBIG, got {other:?}"),
684            };
685
686            // Generate a distinct message given a phase and a counter
687            #[allow(clippy::identity_op)]
688            let mk_msg = |phase: u8, i: usize| {
689                let i = u32::try_from(i).unwrap();
690                let mut msg = [0_u8; OUTPUT_LEN];
691                msg[0] = phase;
692                msg[1] = phase;
693                msg[4] = (i >> 24) as _;
694                msg[5] = (i >> 16) as _;
695                msg[6] = (i >> 8) as _;
696                msg[7] = (i >> 0) as _;
697                msg
698            };
699
700            // Number of hashes we can write to the file before failure occurs
701            const CAN_DO: usize = (ALLOW + BUF - MAGIC_LEN) / OUTPUT_LEN;
702            dbg!(MAGIC_LEN, OUTPUT_LEN, BUF, ALLOW, CAN_DO);
703
704            // Record of the hashes that TestReplayLog tells us were OK and not replays;
705            // ie, which it therefore ought to have recorded.
706            let mut gave_ok = Vec::new();
707
708            set_ulimit(ALLOW);
709
710            for i in 0..CAN_DO {
711                let h = mk_msg(b'y', i);
712                rl.check_for_replay(&h).unwrap();
713                gave_ok.push(h);
714            }
715
716            let md = fs::metadata(&path).unwrap();
717            dbg!(md.len(), &rl.file);
718
719            // Now we have written what we can.  The next two calls will fail,
720            // since the BufWriter buffer is full and can't be flushed.
721
722            for i in 0..2 {
723                eprintln!("expecting EFBIG {i}");
724                demand_efbig(rl.check_for_replay(&mk_msg(b'n', i)).unwrap_err());
725                let md = fs::metadata(&path).unwrap();
726                assert_eq!(md.len(), u64::try_from(ALLOW).unwrap());
727            }
728
729            // Enough that we don't get any further file size exceedances
730            set_ulimit(ALLOW * 10);
731
732            // Now we should be able to recover.  We write two more hashes.
733            for i in 0..2 {
734                eprintln!("recovering {i}");
735                let h = mk_msg(b'r', i);
736                rl.check_for_replay(&h).unwrap();
737                gave_ok.push(h);
738            }
739
740            // flush explicitly just so we catch any error
741            // (drop would flush, but it can't report errors)
742            rl.flush().unwrap();
743            drop(rl);
744
745            // Reopen the log - reading in the written data.
746            // We can then check that everything the earlier IptReplayLog
747            // claimed to have written, is indeed recorded.
748
749            let mut rl = TestReplayLog::new_logged_inner(&path, lock.clone()).unwrap();
750            for msg in &gave_ok {
751                match rl.check_for_replay(msg) {
752                    Err(ReplayError::AlreadySeen) => {}
753                    other => panic!("expected AlreadySeen, got {other:?}"),
754                }
755            }
756
757            eprintln!("recovered file contents checked, all good");
758        });
759
760        unsafe {
761            libc::raise(libc::SIGUSR2);
762        }
763        panic!("we survived raise SIGUSR2");
764    }
765}