diff --git a/CHANGELOG.md b/CHANGELOG.md index 48776701..47d9d2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,7 +112,9 @@ - **Your own fonts: a stricter background check.** What the check found of a font is now kept on the Kindle itself (`/var/local/grimoire`, a few hundred bytes a font) and tied to the font file by its - SHA-256. The check also times each glyph as Grimoire will draw it: a glyph that would take too long is + SHA-256. Grimoire keeps that folder to itself: anything else found at its name is moved aside untouched + (`grimoire.not-own-...` beside it), the folder is made afresh, and the fonts are checked once more. The + check also times each glyph as Grimoire will draw it: a glyph that would take too long is drawn in Terminess, and a font whose letters would is "Took too long to read.". Each font is checked once more after the update, and Grimoire reads in Terminess until the font in use has passed again. Deleting `grimoire/fonts/.cache` still has every font checked again. diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index 44139150..1b32dce6 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -825,7 +825,8 @@ SHA-256, and the glyph limit; docs/TESTING.md has the device checks, none of whi entry above: the font would then load before its family's Bold file has passed in every family, not only in those whose Bold file sorts later. - **Hashing a font while it is read** (one pass, not two), if the load time on the Scribe asks for it. -- **Removing `/var/local/grimoire` when the package is removed** (today: by hand, KINDLE.md). +- **Removing `/var/local/grimoire` when the package is removed**, with any `grimoire.not-own-*` entry moved aside + beside it (today: by hand, KINDLE.md). - **A font with a slow glyph ahead of its broken ones** ends as "Took too long to read." (after its second check) where the broken glyphs alone would have made it "Unreadable font.": the check ends at the first count that is too many, and says what it knows then. @@ -834,9 +835,6 @@ SHA-256, and the glyph limit; docs/TESTING.md has the device checks, none of whi stand before it, and a second folder for one data folder that was named by two paths. An empty folder with no `root` file cannot be told from one another data root's job is making at that moment, so it is left. What holds in the end is the free-room test before every check. -- **The parent of Grimoire's own folder is not tested** (`/var/local` on the Kindle): the code holds the - folder itself and the two below it to "root's, and no one else can write it", and the parent is the - device check's (TESTING.md). From the whole-release review of 2.1.0-beta.2 (the fix round, lane F3; the review's finding IDs). Fixed there: H8, M17, M19, M20, the watchdog timeout that was a verdict for good (a second timeout is the verdict now), the diff --git a/docs/DESIGN.md b/docs/DESIGN.md index d8085223..bf57446f 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -2478,9 +2478,23 @@ its bytes.** Four parts keep it (`fontcheck.rs`, `userfonts.rs`, `font.rs`): `userfonts.rs`; so from the verdicts numbered 2 on): on the Kindle a folder of Grimoire's own outside the data root, `/var/local/grimoire`, with a folder for each data root; in a build that names no such folder (`GRIMOIRE_STATE` unset), the cache, as before. The own folder is walked name by name, by descriptor, from its parent: each name is - made by the job with mode 0700 when missing, opened without following a link, and must be a folder of the - app's user that no one else can write; verdicts are read and written from that descriptor. When it cannot - be used, its file system has too little room, or it takes no write (the job writes and removes a small + made by the job with mode 0700 when missing (the own folder itself by the first check of a run alone), + opened without following a link, and must be a folder of the + app's user that no one else can write; verdicts are read and written from that descriptor. Grimoire relies + on the parent (`/var/local` on the Kindle) only to hold the own folder's name: it tests the folder it opens + at every open, and never trusts the parent's owner or mode. When the entry at the own folder's name is not + Grimoire's own, the first check of a run moves it aside within the parent, to + `grimoire.not-own--`, and makes the folder afresh, at most once a run: the UI, which forks a + process for each check, gives the first check of a run alone leave to make the own folder or make it + afresh, from its own memory, never from a file. The entry is renamed by its name, from the parent's + descriptor, as it stands, by a rename that never replaces an entry, and is never followed, opened, read, + listed or deleted; its kind, owner and mode, as looked at just before the rename, are logged (an entry + another writer of the parent puts at the name in between is the one moved, and is left as it is too). + Every font is then checked once more, as after + `rm -r /var/local/grimoire`. The folders below the own folder are its own, which no one else can write, and + are never moved. When the own folder cannot be used (the move failed, say, the fresh folder is not + Grimoire's own either, or, after the first check of a run, it is gone or not Grimoire's own), its file + system has too little room, or it takes no write (the job writes and removes a small file there before a font's check), the job ends with status 6 before any worker starts: nothing is marked, and the session checks no more fonts (the ones that have their verdicts go on loading). The previews and the marks stay in the cache, and a verdict counts only with its companion there (a pass with its preview, a refusal with `.no`), so deleting `fonts/.cache` has every font diff --git a/docs/KINDLE.md b/docs/KINDLE.md index 0291f828..3d49410e 100644 --- a/docs/KINDLE.md +++ b/docs/KINDLE.md @@ -645,7 +645,12 @@ What the check found of a font, its verdict, is kept on the Kindle itself and no verdicts numbered 2 on, FORMAT.md; 2.1.0 kept its own in `grimoire/fonts/.cache/`): `/var/local/grimoire/<16 hex digits>/fonts/.font`, a few hundred bytes a font (at most 200 files of 8 KiB for a data folder), in folders Grimoire makes for root alone (mode 0700; a folder that is already there is used -only while it is root's and no one else can write it). The 16 digits name the data folder (a second one, a +only while it is root's and no one else can write it). When the entry at `/var/local/grimoire` is not Grimoire's +own (a link, a file, or a folder of another user or that others can write), the first check after Grimoire starts +moves it aside as it is, never opened, to `/var/local/grimoire.not-own--`, and makes the folder +afresh: every font is then checked once more, as after `rm -r /var/local/grimoire`. That is done once a run: a +later check of the same run that finds such an entry there, or nothing, keeps nothing, and no font is checked +until Grimoire starts again. The 16 digits name the data folder (a second one, a development copy, has its own, and the file `root` beside `fonts` says which folder it is of); what was kept for a data folder that is gone is removed at the next check. The previews and marks stay in `grimoire/fonts/.cache/`, and a verdict counts only with its preview (or, for a font the check refused, its `.no` file) there. So after the @@ -654,7 +659,7 @@ after a factory reset, or with the storage moved to another Kindle, once more th worker died (its `.bad` file in `grimoire/fonts/.cache/`) is not checked again by this: it stays out until its file changes or the cache is deleted. `/var/local/grimoire` is left when Grimoire is removed; `rm -r /var/local/grimoire` over SSH, with Grimoire closed, removes it, at the -price of one more check of every font. A `.font` file in `grimoire/fonts/.cache/` is 2.1.0's: it is not read, +price of one more check of every font. An entry moved aside is left too: Grimoire never opens or removes it. A `.font` file in `grimoire/fonts/.cache/` is 2.1.0's: it is not read, and 2.1.0 finds it again if you go back. What grimoire.log says (never a file's name or a family's: counts, sizes, timings, reasons): @@ -685,6 +690,12 @@ What grimoire.log says (never a file's name or a family's: counts, sizes, timing system that went read-only). No font is read, none is marked; the rows of unchecked fonts say "Unreadable font." for this session, and fonts that have their verdicts go on working. `fonts: N data folder(s) are gone: what was kept for them is removed from Grimoire's own folder`. +- `fonts: the entry at the name of Grimoire's own folder was not its own (, owner , mode ): it + is moved aside as it was, and the folder is made afresh; every font is checked once more`: at most once a + run, by its first check, which then goes on in the fresh folder. When the move cannot be made, `... is not its own (...) and + cannot be moved aside ()`, or when another entry stands at the name again at once, `fonts: the folder made + afresh is not Grimoire's own either (); nothing more is moved aside`; either is followed by `what the + check finds cannot be kept (it is not Grimoire's own)` above. - `fonts: the check ended badly; N file(s) marked unreadable`: the background job itself died on a file. `fonts: what the check found could not be written. Is the storage, or Grimoire's own folder, full?`. - `font: custom font loaded: S KB in T ms, bold by its weight axis|a Bold file|overprint, K glyph(s) from diff --git a/docs/TESTING.md b/docs/TESTING.md index 73e86cc6..2a9082f8 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -2913,36 +2913,46 @@ What the background check found of a font (its verdict) is kept on the Kindle it will draw it. Automated: the tests of `rust/src/sha256.rs`, `rust/tests/test_sec_font_verdict.rs`, the functions of `rust/tests/test_fontcheck.rs` about the glyph limit and the second try's mark, the unit tests of `fontcheck.rs` and `userfonts.rs`, and (Linux, root, CI's `sandbox` job) the font test of -`rust/tests/test_sandbox_linux.rs`. +`rust/tests/test_sandbox_linux.rs` and its test of a folder of another user at the own folder's name. -**Not run on a Scribe, and it must be before a release.** This change can leave a Kindle with no custom font -(if Grimoire's own folder cannot be used there), can refuse a font that works today or hand some of its -glyphs to Terminess (the time limit), and changes what the sandboxed worker does for every glyph. Its -values that are placeholders, and wait for the answers below: the glyph limit (`s1::FONT_GLYPH_CPU_MS` in -`sandbox.rs`, 20 ms) and the size a draw is timed at (`FONT_TIME_PX` in `fontcheck.rs`, 160 px), which -steps 6 to 8 answer; the own folder's place (`STATE_BASE` in `userfonts.rs`, `/var/local/grimoire`), which -steps 1, 5 and 11 answer; and the two floors of free room beside it (`STATE_FREE_FLOOR`, 4 MB, and -`STATE_FILES_FLOOR`, 512 files), which step 1 answers. +**Step 1 is answered on a Scribe (below); the rest is not run on a Scribe yet, and it must be before a +release.** This change can leave a Kindle with no custom font (if Grimoire's own folder cannot be used there), +can refuse a font that works today or hand some of its glyphs to Terminess (the time limit), and changes what +the sandboxed worker does for every glyph. Its values that are placeholders, and wait for the answers below: +the glyph limit (`s1::FONT_GLYPH_CPU_MS` in `sandbox.rs`, 20 ms) and the size a draw is timed at +(`FONT_TIME_PX` in `fontcheck.rs`, 160 px), which steps 6 to 8 answer; and the own folder's place +(`STATE_BASE` in `userfonts.rs`, `/var/local/grimoire`), which steps 5 and 11 answer (step 1's part is +answered). The two floors of free room beside it are set from step 1's answers. 1. **Before installing, over SSH, with 2.1.0 still on.** `mount | grep /var/local`, `df -k /var/local`, - `df -i /var/local`, `ls -ld /var/local`. Write the answers here. /var/local must be a file system of its - own that keeps its files across a restart (not the tmpfs at /var) and that keeps owners and modes (ext3 - or ext4, say; on one without them the test "root's, and no one else can write it" means nothing). It - must be root's, with no write bit for group or others (`drwxr-xr-x root`: Grimoire tests the folders it - makes there, never /var/local itself), with well over 4 MB and at least 512 inodes free (a file system - that counts no inodes, `df -i` showing 0, is fine). If it is not, stop: the place (`STATE_BASE`) and the - floors beside it are set from these numbers before a release. Also keep 2.1.0's grimoire.log lines + `df -i /var/local`, `ls -ld /var/local`. The answers on a Scribe (firmware 5.20.1.0.2, Grimoire 2.1.0): + /var/local is a file system of its own, `/dev/mmcblk0p9`, ext4, mounted read and write (`df` names it + `/var/base-local`); 996,780 KB in all with 868,764 KB free, and 65,536 inodes with 63,718 free. /var/local + itself is `drwxrwsr-x`, the system's `framework` user's and `javausers` group's (mode 2775: the group may + write it, the setgid bit is set). What Grimoire needs of it: a file system of its own that keeps its files + across a restart (not the tmpfs at /var) and that keeps owners and modes (ext3 or ext4, say; on one + without them the test "root's, and no one else can write it" means nothing), with well over 4 MB and at + least 512 inodes free (a file system that counts no inodes, `df -i` showing 0, is fine). Grimoire never + relies on /var/local itself: it tests its own folder at every open and moves aside an entry at its name + that is not its own (step 12), so the owner and mode of /var/local ask nothing of a release. The floors + stand on these numbers: `STATE_FREE_FLOOR`, 4 MB, and `STATE_FILES_FLOOR`, 512 files, against a check + that keeps a few hundred bytes and one file a font (at most 200 for a data folder). If a Kindle answers + otherwise (no file system of its own there, or one without owners and modes), stop: the place + (`STATE_BASE`) is set from its answers before a release. Also keep 2.1.0's grimoire.log lines `fonts: file I of N passed: ... checked in T ms` and `font: custom font loaded: S KB in T ms`. 2. **The update.** Install, start Grimoire with a font of your own in use. The Library is in Terminess at first; the font comes back by itself with one redraw once its check has passed (how long?). Settings > Font shows "Checking..." rows meanwhile. grimoire.log has one `fonts: file I of N passed: ...` line a - font, and no `work: a font's worker made call N`, no `marked unreadable`, no `cannot be kept`. If the font + font, and no `work: a font's worker made call N`, no `marked unreadable`, no `cannot be kept`, no `moved + aside` (nothing stood at the folder's name before). If the font in use has a Bold file whose name sorts after its own (`Name.ttf` with `NameBold.ttf`), look at text Grimoire draws bold once the font is back: it is the regular overprinted until Grimoire is started again, then the Bold file's (docs/BACKLOG.md, "Bold stays overprinted"). 3. **Where it is kept.** `ls -ld /var/local/grimoire /var/local/grimoire/* /var/local/grimoire/*/fonts - /var/local/grimoire/*/root` (three folders, `drwx------`, and one file, `-rw-------`, all root's; `cat` - of the file gives the data folder's path); `ls -l /var/local/grimoire/*/fonts` (one small `.font` a + /var/local/grimoire/*/root` (three folders, `drwx------`, and one file, `-rw-------`, all root's; under a + /var/local with the setgid bit, as in step 1, the folders show `drwx--S---` and its group, which they take + from it and which has no rights in them; `cat` of the file gives the data folder's path); + `ls -l /var/local/grimoire/*/fonts` (one small `.font` a font); `head -3` of one says `grimoire-font 2` and a 64-digit `hash=`, which is `sha256sum` of the font file. `ls /mnt/us/grimoire/fonts/.cache` has a `.pgm` a font (a `.no` for one the check refused), and 2.1.0's `.font` files untouched. @@ -2974,6 +2984,20 @@ steps 1, 5 and 11 answer; and the two floors of free room beside it (`STATE_FREE the worker's narrower system calls and this change's walk are in the same process. 11. When a firmware update next arrives: is /var/local/grimoire still there? (If not, fonts are checked once more: note it in KINDLE.md.) +12. **What is not Grimoire's own is moved aside.** With Grimoire closed: `mv /var/local/grimoire + /var/local/grimoire.test && mkdir -m 775 /var/local/grimoire`. Start Grimoire with a font of your own in + use: grimoire.log has once `fonts: the entry at the name of Grimoire's own folder was not its own (a + folder, owner 0, mode 0775): it is moved aside as it was, and the folder is made afresh; every font is + checked once more` (`mode 2775` if mkdir gave the folder /var/local's setgid bit), then a pass line a font: + every font is checked once more, as after `rm -r /var/local/grimoire` (step 9), and the font comes back. `ls -ld + /var/local/grimoire*`: the folder made afresh as in step 3, the one mkdir made under + `grimoire.not-own--` with its mode as it was and nothing in it, and `grimoire.test`. A second + start moves nothing. Once a run: with Grimoire running, `mv /var/local/grimoire /var/local/grimoire.test2 && + mkdir -m 775 /var/local/grimoire`, then copy a font that is not there yet into `grimoire/fonts`. Nothing more + is moved aside: grimoire.log has `cannot be kept (it is not Grimoire's own)` and `no font is checked until + Grimoire starts again`, once each, and that font's row says "Unreadable font.". The next start moves the folder + aside and checks every font once more. Then, with Grimoire closed, `rm -r /var/local/grimoire.not-own-* + /var/local/grimoire.test /var/local/grimoire.test2`. ## 2.2, From a file... and Use as template (issue #146): device checks diff --git a/rust/src/userfonts.rs b/rust/src/userfonts.rs index d8fd471b..6a0028f6 100644 --- a/rust/src/userfonts.rs +++ b/rust/src/userfonts.rs @@ -150,7 +150,7 @@ fn key_of(rel: &str, size: u64, mtime: i64) -> u64 { /// a verdict for this size and time and passed: one small file of the check's own, read where the verdicts are /// kept and counted only with its companion (`verdict_for`), no font read here. None else. pub fn userfont_family(rel: &str, size: u64, mtime: i64) -> Option { - let vd = VerdictDir::open(false).ok()?; + let vd = VerdictDir::open(false, false).ok()?; match verdict_for(&vd, key_of(rel, size, mtime), size)? { Verdict { what: VerdictKind::Passed(facts), .. } if !facts.family.is_empty() => Some(facts.family), _ => None, @@ -240,8 +240,9 @@ fn read_listed(st: &FsStorage, l: &Listed) -> Option> { /// Grimoire's own folder on the Kindle, outside the storage: a constant, which no environment variable moves. #[cfg(feature = "kindle")] const STATE_BASE: &str = "/var/local/grimoire"; -/// The room that folder's file system must have free before a font is checked: bytes, and files (inodes). -/// Placeholders until the Scribe's figures are in (docs/TESTING.md). +/// The room that folder's file system must have free before a font is checked: bytes, and files (inodes). Set +/// from the Scribe's figures (docs/TESTING.md): a check keeps a few hundred bytes and one file a font, at most +/// 200 for a data folder, where hundreds of MB and tens of thousands of inodes are free. const STATE_FREE_FLOOR: u64 = 4 << 20; const STATE_FILES_FLOOR: u64 = 512; /// The folders of other data roots looked at in a job, and how long a `root` file is at most. @@ -292,13 +293,22 @@ fn enough_room(free_bytes: u64, files: u64, files_free: u64) -> bool { /// Whether the folder behind a descriptor is this user's and no one else may write it. fn own_dir(fd: &OwnedFd) -> bool { - // SAFETY: st is written by fstat before it is read; geteuid takes no arguments. + // SAFETY: st is written by fstat before it is read. unsafe { let mut st: libc::stat = std::mem::zeroed(); - libc::fstat(fd.as_raw_fd(), &mut st) == 0 && (st.st_mode & libc::S_IFMT) == libc::S_IFDIR && st.st_uid == libc::geteuid() && st.st_mode & 0o022 == 0 + libc::fstat(fd.as_raw_fd(), &mut st) == 0 && own_stat(&st) } } +/// Whether a stat is of a folder of this user's that no one else may write. Only the write bits of group and others +/// are asked. The setgid bit is taken: a folder made under a parent that has it, as /var/local has on the Kindle, +/// comes out with it and with the parent's group (mode 02700), and that group has no rights in it. So is the sticky +/// bit, which only narrows who may remove names inside. Neither lets anyone else write the folder. +fn own_stat(st: &libc::stat) -> bool { + // SAFETY: geteuid takes no arguments. + (st.st_mode & libc::S_IFMT) == libc::S_IFDIR && st.st_uid == unsafe { libc::geteuid() } && st.st_mode & 0o022 == 0 +} + /// The folder `name` of the folder `at`, by descriptor: made first when `make` (mode 0700; one that is there is /// fine), opened without following a link, and held to `own_dir`. Or why not. fn own_dir_at(at: &OwnedFd, name: &str, make: bool) -> Result { @@ -322,6 +332,99 @@ fn own_dir_at(at: &OwnedFd, name: &str, make: bool) -> Result String { + format!("{name}.not-own-{secs}-{pid}") +} + +/// What an entry is, by its mode, for the log. +fn kind_of(mode: libc::mode_t) -> &'static str { + match mode & libc::S_IFMT { + libc::S_IFDIR => "a folder", + libc::S_IFLNK => "a link", + libc::S_IFREG => "a file", + libc::S_IFIFO => "a pipe", + libc::S_IFSOCK => "a socket", + _ => "a device", + } +} + +/// What a test runs between the move aside and the fresh folder's make (userfonts_aside_seam). +#[cfg(any(test, feature = "sim"))] +type AsideSeam = Box; +#[cfg(any(test, feature = "sim"))] +static ASIDE_SEAM: std::sync::Mutex> = std::sync::Mutex::new(None); + +/// Tests (never the Kindle's build): `f` runs once, in the next move aside, between the rename and the fresh +/// folder's make: what another writer of the parent does at that moment. +#[cfg(any(test, feature = "sim"))] +pub fn userfonts_aside_seam(f: Option) { + *ASIDE_SEAM.lock().unwrap_or_else(|e| e.into_inner()) = f; +} + +/// Tests (never the Kindle's build): this process may move an entry aside once more, as a fresh process may, and +/// the next check the UI queues is the first of a run again (USERFONTS_AFRESH). +#[cfg(any(test, feature = "sim"))] +pub fn userfonts_aside_reset() { + MOVED_ASIDE.store(false, std::sync::atomic::Ordering::Relaxed); + with(|st| st.afresh_given = false); +} + +/// The entry at `name` in `parent`, the name of Grimoire's own folder, did not pass `own_dir_at`. When it is not a +/// folder of this user's that no one else may write (a link, a file, a pipe, a folder of another user or one others +/// may write), the job moves it aside within the parent (`aside_name`) and makes the folder afresh: only the first +/// check of a run gets here (USERFONTS_AFRESH), and a process tries at most once. The entry is renamed by its name, +/// from the parent's descriptor, as it stands: never followed, opened, read, listed or deleted; the rename never +/// replaces an entry. Its kind, owner and mode, as looked at just before the rename, are logged (an entry another +/// writer of the parent puts at the name in between is the one moved), never its name or what is in it. The fresh +/// folder is made, opened and held to `own_dir` as every folder of Grimoire's own is, and holds no verdict: every +/// font is checked once more. When the move fails, or the fresh folder is not Grimoire's own either (another entry +/// came to the name meanwhile), nothing is kept, as for any folder that is not Grimoire's own, and the log says why. +fn base_afresh(parent: &OwnedFd, name: &str) -> Result { + let c = CString::new(name).map_err(|_| NOT_OWN)?; + // SAFETY: a stat is plain data; zeroed, then written by fstatat (which follows no link) before it is read. + let mut st: libc::stat = unsafe { std::mem::zeroed() }; + if unsafe { libc::fstatat(parent.as_raw_fd(), c.as_ptr(), &mut st, libc::AT_SYMLINK_NOFOLLOW) } != 0 { + return Err(NOT_OWN); + } + // (a folder of this user's that no one else may write and did not open is not moved: the open failed for + // another reason, out of descriptors, say) + if own_stat(&st) || MOVED_ASIDE.swap(true, std::sync::atomic::Ordering::Relaxed) { + return Err(NOT_OWN); + } + let (kind, owner, mode) = (kind_of(st.st_mode), st.st_uid, (st.st_mode & 0o7777) as u32); + let aside = aside_name(name, wall_time(), std::process::id()); + let moved = match CString::new(aside.as_str()) { + Ok(to) if fsview_name_check(b"", aside.as_bytes(), true).is_ok() => rename_noreplace(parent.as_raw_fd(), &c, parent.as_raw_fd(), &to), + _ => Err(libc::ENAMETOOLONG), + }; + if let Err(e) = moved { + qlog!("fonts: the entry at the name of Grimoire's own folder is not its own (%s, owner %u, mode %04o) and cannot be moved aside (%s)", kind, owner, mode, &strerror(e)); + return Err(NOT_OWN); + } + qlog!("fonts: the entry at the name of Grimoire's own folder was not its own (%s, owner %u, mode %04o): it is moved aside as it was, and the folder is made afresh; every font is checked once more", kind, owner, mode); + #[cfg(any(test, feature = "sim"))] + if let Some(f) = ASIDE_SEAM.lock().unwrap_or_else(|e| e.into_inner()).take() { + f(); + } + own_dir_at(parent, name, true).inspect_err(|why| qlog!("fonts: the folder made afresh is not Grimoire's own either (%s); nothing more is moved aside", *why)) +} + /// A folder by its path (links in the path are followed unless `nofollow`, which holds for its last name). fn open_dir(path: &str, nofollow: bool) -> Option { let c = CString::new(path).ok()?; @@ -434,15 +537,18 @@ impl VerdictDir { /// Opens the folder, or says why not. In Grimoire's own folder the walk goes name by name from the base's /// parent, which is the system's: the base, this data root's folder (`root_tag`) and its `fonts`, each made /// when `make` (the job alone asks; the UI makes nothing there and writes no file's text: it only renames a - /// verdict to the key of a file renamed in Files), opened without following a link, and held to + /// verdict to the key of a file renamed in Files), the base itself only when `afresh` too (the first check + /// of a run, USERFONTS_AFRESH), opened without following a link, and held to /// be this user's with no write bit for anyone else. The test asks who can write a folder: one that is /// there and that others can read is taken, and only the folders made here are 0700. The parent itself is /// opened by its path and held to nothing: what stands at the base's name must pass the test whoever put - /// it there, so a parent that others can write can cost the verdicts (no font is then checked) and cannot - /// supply one. What the parent must be on a device is that device's check (docs/TESTING.md). In the cache: - /// the folder made when `make`, as the job always made it, then opened; no owner and no mode is asked of - /// it. - fn open(make: bool) -> Result { + /// it there, so a parent that others can write cannot supply a verdict. When what stands there does not + /// pass, the first check of a run moves it aside and makes the base afresh (`base_afresh`), so such a + /// parent costs at most one more check of every font a run; a later check of the run finds the base gone + /// or not Grimoire's own and keeps nothing. The folders below the base are the base's own, which no one + /// else can write: one of them that does not pass is not moved. In the cache: the folder made when `make`, + /// as the job always made it, then opened; no owner and no mode is asked of it. + fn open(make: bool, afresh: bool) -> Result { let Some(base) = state_base() else { let cache = cache_dir(); if make && (!no_link_below(&store_root(), &cache) || mkdir_p(&cache) != 0) { @@ -458,7 +564,12 @@ impl VerdictDir { let Some((parent, name)) = base.rsplit_once('/').filter(|(_, n)| !n.is_empty() && *n != "." && *n != "..") else { return Err(NOT_MADE) }; let parent = open_dir(if parent.is_empty() { "/" } else { parent }, false).ok_or(NOT_THERE)?; // (never util::mkdir_p here: it takes any name that is there as made, and makes folders others can read) - let base = own_dir_at(&parent, name, make)?; + let afresh = make && afresh; + let base = match own_dir_at(&parent, name, afresh) { + // (the first check of a run alone, which may make the folder, moves aside what is at its name) + Err(NOT_OWN) if afresh => base_afresh(&parent, name)?, + r => r?, + }; let tag = own_dir_at(&base, &root_tag(&store_root()), make)?; let fd = own_dir_at(&tag, "fonts", make)?; Ok(VerdictDir { fd, own: Some((base, tag)) }) @@ -571,7 +682,7 @@ pub fn userfonts_renamed(old: &str, new: &str) { } else { keys.push((key_of(old, s.size, s.mtime), key_of(new, s.size, s.mtime))); } - let vd = VerdictDir::open(false).ok(); + let vd = VerdictDir::open(false, false).ok(); let cache = no_link_below(&store_root(), &cache_dir()); let mut carried = 0; for (from, to) in keys.into_iter().filter(|(from, to)| from != to) { @@ -588,7 +699,7 @@ pub fn userfonts_renamed(old: &str, new: &str) { /// The names of the verdicts kept (none when their folder cannot be opened: every font is then "checking", /// and the job says why). fn verdict_names() -> HashSet { - VerdictDir::open(false).map(|vd| vd.names()).unwrap_or_default() + VerdictDir::open(false, false).map(|vd| vd.names()).unwrap_or_default() } /// The job's upkeep of Grimoire's own folder: this data root's folder names its root (the file `root`: the @@ -796,6 +907,9 @@ struct State { /// a font was copied into the folder (`userfonts_added`): when the folder is looked at for it. Until then no /// look at the folder is queued: the file would only be found too young (bg_font_check) and left for later added_ms: Option, + /// the first check of this run was given leave to make Grimoire's own folder, or make it afresh + /// (USERFONTS_AFRESH): no later check of the run is + afresh_given: bool, } /// Checks in a row that end with no verdict before the session stops asking (H8: no check forked 29 times a @@ -1144,7 +1258,7 @@ pub fn userfont_resolve(f: &mut UserFont) { if f.state != UserFontState::Checked { return; } - f.state = match VerdictDir::open(false).ok().and_then(|vd| verdict_for(&vd, f.key, f.size)) { + f.state = match VerdictDir::open(false, false).ok().and_then(|vd| verdict_for(&vd, f.key, f.size)) { Some(v) => match v.what { VerdictKind::Passed(facts) => UserFontState::Ready(Rc::new(facts), v.hash), VerdictKind::Refused(Refuse::Unreadable) => UserFontState::No(FontWhy::Unreadable), @@ -1178,7 +1292,10 @@ fn queue_check(arg: &str, dpi: f32) { } // (the first check of a look is what the user waits on; the rest of its fonts are one job each, behind // exports, previews and the slices of a PDF import: userfonts_job_done) - bg_queue(BgKind::FontCheck, Some(""), Some(arg), dpi, arg != "next"); + let notify = arg != "next"; + // (the first check of a run alone may make Grimoire's own folder, or make it afresh: USERFONTS_AFRESH) + let arg = if with(|st| !std::mem::replace(&mut st.afresh_given, true)) { format!("{USERFONTS_AFRESH}{arg}") } else { arg.to_string() }; + bg_queue(BgKind::FontCheck, Some(""), Some(&arg), dpi, notify); } /// A check ended with no verdict for what was wanted: the next one waits (backoff), and a few in a row stop @@ -1363,7 +1480,7 @@ fn load_file(st: &FsStorage, rel: &str, dpi: f32, room: usize) -> Result FONT_FILE_MAX as u64 || guard_out(l.key) { return Err(LoadErr::Refused); } - let Some(v) = VerdictDir::open(false).ok().and_then(|vd| verdict_for(&vd, l.key, l.size)) else { + let Some(v) = VerdictDir::open(false, false).ok().and_then(|vd| verdict_for(&vd, l.key, l.size)) else { let bad = std::path::Path::new(&cache_file(l.key, "bad")).exists(); if bad { return Err(LoadErr::Refused); @@ -1393,7 +1510,7 @@ fn pair_of(rel: &str, facts: &FaceFacts) -> Option { } let folder = |r: &str| r.rfind('/').map_or(String::new(), |i| r[..i].to_string()); let (files, _) = list_fonts(); - let vd = VerdictDir::open(false).ok()?; + let vd = VerdictDir::open(false, false).ok()?; let mut best: Option<(i32, String)> = None; for l in files.iter().filter(|l| l.rel != rel && folder(&l.rel) == folder(rel) && l.size <= FONT_FILE_MAX as u64) { let Some(Verdict { what: VerdictKind::Passed(f), .. }) = verdict_for(&vd, l.key, l.size) else { continue }; @@ -1755,19 +1872,21 @@ fn write_cache(path: &str, data: &[u8]) -> bool { /// BgKind::FontCheck, in the background child: every font file of grimoire/fonts that has no verdict yet is /// read whole and checked by the sandboxed worker (fontcheck.rs), one after another, its preview (or its /// `.no`) written to the cache and its verdict to where verdicts are kept (`VerdictDir`). `arg` is "" or -/// "recheck:" (that file's verdict is stale: it is checked again). `dpi` sizes the preview. Returns 0 +/// "recheck:" (that file's verdict is stale: it is checked again), with USERFONTS_AFRESH at its head for the +/// first check of a run (it may make Grimoire's own folder, or make it afresh). `dpi` sizes the preview. Returns 0 /// done, 3 some files wait (still being copied, no memory now, or left for their second try), 4 no worker can /// be had (nothing is marked), 5 one font was checked and more are left for the next job, 6 what a check finds /// cannot be kept (Grimoire's own folder cannot be used, has too little room or takes no write: nothing is /// checked and nothing is marked), -1 it could not start, or a mark, a companion or a verdict could not be /// written. pub fn bg_font_check(arg: &str, dpi: f32) -> i32 { + let (afresh, arg) = arg.strip_prefix(USERFONTS_AFRESH).map_or((false, arg), |a| (true, a)); let cache = cache_dir(); // (its files are written and stale entries deleted there as root: never through a planted link) if !no_link_below(&store_root(), &cache) || mkdir_p(&cache) != 0 { return -1; } - let vd = match VerdictDir::open(true) { + let vd = match VerdictDir::open(true, afresh) { Ok(vd) => vd, Err(why) if state_base().is_some() => { // (before any worker starts and before any mark is written; the folder's path is never logged) @@ -2191,6 +2310,51 @@ mod tests { assert!(enough_room(bytes, 0, 0)); } + // The name an entry is moved aside to says what it is, keeps the rules of every other name, and differs from + // the folder's own name and from the name another process or another second would give. + #[test] + fn an_entry_moved_aside_has_a_name_of_grimoires_own() { + let n = aside_name("grimoire", 1_763_500_000, 1234); + assert_eq!(n, "grimoire.not-own-1763500000-1234"); + assert_eq!(fsview_name_check(b"", n.as_bytes(), true), Ok(())); + // the longest the Kindle's can be: the largest time and process id still fit the name rules + let longest = aside_name("grimoire", i64::MAX, u32::MAX); + assert!(longest.len() < FS_NAME_MAX && fsview_name_check(b"", longest.as_bytes(), true).is_ok()); + assert_ne!(n, aside_name("grimoire", 1_763_500_000, 1235)); + assert_ne!(n, aside_name("grimoire", 1_763_500_001, 1234)); + assert!(n != "grimoire" && n.starts_with("grimoire.") && !n.starts_with('.')); + // a name the rules refuse is never moved to (the move is not tried) + assert!(fsview_name_check(b"", aside_name(&"x".repeat(250), 1, 1).as_bytes(), true).is_err()); + assert_eq!(kind_of(libc::S_IFDIR | 0o775), "a folder"); + assert_eq!(kind_of(libc::S_IFLNK | 0o777), "a link"); + assert_eq!(kind_of(libc::S_IFREG | 0o644), "a file"); + assert_eq!(kind_of(libc::S_IFIFO | 0o644), "a pipe"); + } + + // own_stat's rule, by mode: a folder of this user's with no write bit for group or others. A folder made under + // the Kindle's /var/local (setgid) is 02700 and is Grimoire's own; the setgid and sticky bits never make a folder + // Grimoire's own when a write bit does not let it be. + #[test] + fn own_stat_asks_only_who_may_write() { + // SAFETY: geteuid takes no arguments. + let me = unsafe { libc::geteuid() }; + let st = |kind: libc::mode_t, mode: u32, uid: libc::uid_t| { + // SAFETY: a stat is plain data: all zero is a valid one. + let mut s: libc::stat = unsafe { std::mem::zeroed() }; + s.st_mode = kind | mode as libc::mode_t; + s.st_uid = uid; + s + }; + for mode in [0o700, 0o750, 0o755, 0o2700, 0o2750, 0o2755, 0o1700, 0o1755, 0o3700] { + assert!(own_stat(&st(libc::S_IFDIR, mode, me)), "{mode:04o} is Grimoire's own"); + } + for mode in [0o720, 0o702, 0o770, 0o777, 0o2720, 0o2770, 0o2702, 0o2777, 0o1777, 0o1770, 0o3770] { + assert!(!own_stat(&st(libc::S_IFDIR, mode, me)), "{mode:04o} is not Grimoire's own"); + } + assert!(!own_stat(&st(libc::S_IFDIR, 0o2700, me.wrapping_add(1)))); + assert!(!own_stat(&st(libc::S_IFLNK, 0o700, me)) && !own_stat(&st(libc::S_IFREG, 0o700, me))); + } + #[test] fn a_data_roots_tag_ignores_a_trailing_slash() { let t = root_tag("/mnt/us/grimoire"); diff --git a/rust/tests/test_sandbox_linux.rs b/rust/tests/test_sandbox_linux.rs index 851895e0..460fe1ed 100644 --- a/rust/tests/test_sandbox_linux.rs +++ b/rust/tests/test_sandbox_linux.rs @@ -691,3 +691,86 @@ fn a_scribe_sized_colour_page_mirrors_under_the_mirror_childs_cap() { assert_eq!(String::from_utf8_lossy(&pdf).matches("/DeviceRGB").count(), 4, "every page in colour"); let _ = std::fs::remove_dir_all(&dir); } + +// Grimoire's own folder (issue #201), as root as on the Kindle, in a parent like the Kindle's /var/local: another +// user's and another group's, mode 02775 (the group may write it, the setgid bit is set). A folder of another user at +// the own folder's name, which no one else may write (so its owner alone makes it not Grimoire's own), is moved aside +// as it is, by a rename that never replaces an entry: the same inode and mode, still that user's, what it holds +// unchanged. The folder is made afresh, root's, and takes the parent's group and setgid bit: 02700. It is Grimoire's +// own: a second check takes it and moves nothing. A member of that group, not root, cannot make a file in it. No +// worker is needed: the fonts folder is empty. +#[test] +fn a_folder_of_another_user_at_the_own_folders_name_is_moved_aside() { + use grimoire::prelude::*; + use grimoire::userfonts::*; + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + if !root() { + return; + } + let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let base = std::env::temp_dir().join(format!("grimoire-sandbox-statedir-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&base); + let (data, parent) = (base.join("grimoire"), base.join("local")); + std::fs::create_dir_all(data.join("fonts")).unwrap(); + std::fs::create_dir_all(&parent).unwrap(); + // (the parent's owner, its group, and a member of that group who is neither: none of them root) + let (owner, group, member) = (65534, 65533, 65532); + std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o755)).unwrap(); + std::os::unix::fs::chown(&parent, Some(owner), Some(group)).unwrap(); + std::fs::set_permissions(&parent, std::fs::Permissions::from_mode(0o2775)).unwrap(); + assert_eq!(std::fs::metadata(&parent).unwrap().mode() & 0o7777, 0o2775); + let state = parent.join("grimoire"); + std::fs::create_dir(&state).unwrap(); + std::fs::write(state.join("keep.me"), b"not Grimoire's").unwrap(); + std::os::unix::fs::chown(&state, Some(owner), Some(owner)).unwrap(); + std::fs::set_permissions(&state, std::fs::Permissions::from_mode(0o700)).unwrap(); + let was = std::fs::symlink_metadata(&state).unwrap(); + std::env::set_var("GRIMOIRE_STATE", &state); + assert_eq!(store_init(data.to_str().unwrap()), 0); + userfonts_aside_reset(); + // (as the first check of a run, which alone may make the folder afresh) + let rc = bg_font_check(USERFONTS_AFRESH, 300.0); + std::env::remove_var("GRIMOIRE_STATE"); + assert_eq!(rc, 0); + let mut names: Vec = std::fs::read_dir(&parent).unwrap().flatten().map(|e| e.file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names.len(), 2, "{names:?}"); + assert_eq!(names[0], "grimoire"); + assert!(names[1].starts_with("grimoire.not-own-") && names[1].ends_with(&format!("-{}", std::process::id())), "{names:?}"); + let aside = parent.join(&names[1]); + let moved = std::fs::symlink_metadata(&aside).unwrap(); + assert!(moved.is_dir() && moved.ino() == was.ino() && moved.mode() == was.mode() && moved.uid() == owner); + assert_eq!(std::fs::read(aside.join("keep.me")).unwrap(), b"not Grimoire's"); + let fresh = std::fs::symlink_metadata(&state).unwrap(); + assert!(fresh.is_dir() && fresh.uid() == 0 && fresh.gid() == group && fresh.mode() & 0o7777 == 0o2700, "{:o} gid {}", fresh.mode(), fresh.gid()); + // Grimoire's own: a second check, itself given leave to move, takes it as it is and moves nothing + std::env::set_var("GRIMOIRE_STATE", &state); + userfonts_aside_reset(); + let rc = bg_font_check(USERFONTS_AFRESH, 300.0); + std::env::remove_var("GRIMOIRE_STATE"); + assert_eq!(rc, 0); + assert_eq!(std::fs::read_dir(&parent).unwrap().count(), 2); + let again = std::fs::symlink_metadata(&state).unwrap(); + assert!(again.ino() == fresh.ino() && again.mode() == fresh.mode() && again.gid() == group); + // A member of its group, not root, cannot make a file in it. + let probe = std::ffi::CString::new(state.join("probe").into_os_string().into_encoded_bytes()).unwrap(); + // SAFETY: the child makes only raw system calls on data made before the fork (setgroups, setgid, setuid, open, + // _exit) and ends with _exit; the parent waits for it. + let code = unsafe { + let pid = libc::fork(); + if pid == 0 { + let refused = libc::setgroups(0, std::ptr::null()) == 0 + && libc::setgid(group) == 0 + && libc::setuid(member) == 0 + && libc::open(probe.as_ptr(), libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL, 0o600) < 0 + && *libc::__errno_location() == libc::EACCES; + libc::_exit(if refused { 0 } else { 1 }); + } + let mut status = 0; + libc::waitpid(pid, &mut status, 0); + libc::WIFEXITED(status).then(|| libc::WEXITSTATUS(status)) + }; + assert_eq!(code, Some(0), "a member of the folder's group could make a file in it, or could not become one"); + assert!(std::fs::symlink_metadata(state.join("probe")).is_err()); + let _ = std::fs::remove_dir_all(&base); +} diff --git a/rust/tests/test_sec_font_verdict.rs b/rust/tests/test_sec_font_verdict.rs index 8338e242..06eb5e3f 100644 --- a/rust/tests/test_sec_font_verdict.rs +++ b/rust/tests/test_sec_font_verdict.rs @@ -49,6 +49,9 @@ fn clean_env() { } std::env::set_var("GRIMOIRE_WORK_EXE", BIN); std::env::set_var("GRIMOIRE_IMPORT_STABLE", "fast"); + // (each case is a fresh process to the move aside, which a process tries once: the app forks each job) + userfonts_aside_reset(); + userfonts_aside_seam(None); } /// A fresh data root with an empty fonts folder; the store looks at it from now on. @@ -70,6 +73,41 @@ fn own_folder(name: &str) -> String { d } +/// A folder for the parent of Grimoire's own folder, as /var/local is on the Kindle, with nothing in it; +/// GRIMOIRE_STATE names `/grimoire` from now on. The parent and that path. +fn own_parent(name: &str) -> (String, String) { + let p = format!("{}/fontverdict-{}-{name}.parent", tl_tmp(), std::process::id()); + remove_any(&p); + std::fs::create_dir(&p).unwrap(); + chmod(&p, 0o755); + let state = format!("{p}/grimoire"); + std::env::set_var("GRIMOIRE_STATE", &state); + (p, state) +} + +/// An entry as it stands, looked at without following it, and what it holds where that is read without opening a +/// pipe: its inode, its mode (with its kind), a link's target, a file's bytes, a folder's names with each one's +/// bytes. None when nothing is there. +type Entry = (u64, u32, Option, Vec<(String, Vec)>); +fn entry(p: &str) -> Option { + use std::os::unix::fs::MetadataExt; + let m = std::fs::symlink_metadata(p).ok()?; + let t = m.file_type(); + let held = if t.is_dir() { + names(p).into_iter().map(|n| (n.clone(), std::fs::read(format!("{p}/{n}")).unwrap_or_default())).collect() + } else if t.is_file() { + vec![(String::new(), std::fs::read(p).unwrap_or_default())] + } else { + Vec::new() + }; + Some((m.ino(), m.mode(), t.is_symlink().then(|| std::fs::read_link(p).unwrap()), held)) +} + +/// The names in a parent of entries moved aside there. +fn asides(parent: &str) -> Vec { + names(parent).into_iter().filter(|n| n.starts_with("grimoire.not-own-")).collect() +} + /// Removes whatever is at a path: a folder with what it holds, a file, or a link (never what it names). fn remove_any(p: &str) { match std::fs::symlink_metadata(p) { @@ -89,13 +127,17 @@ fn put(root: &str, rel: &str, bytes: &[u8]) { std::fs::write(format!("{root}/fonts/{rel}"), bytes).unwrap(); } -/// The job in this process, as the background child runs it: one font a job, until none is left (5: more). +/// The job in this process, as the background child runs it: one font a job, until none is left (5: more). The +/// first is given leave to make Grimoire's own folder, or make it afresh, as the first check of a run is +/// (USERFONTS_AFRESH); the ones after it are not, as the app's next jobs are not. fn job() -> i32 { + let mut arg = USERFONTS_AFRESH.to_string(); loop { - let rc = bg_font_check("", 300.0); + let rc = bg_font_check(&arg, 300.0); if rc != 5 { return rc; } + arg = "next".to_string(); } } @@ -340,10 +382,11 @@ fn checked_once_on_this_device_and_again_elsewhere() { } } -// A folder that is not Grimoire's own keeps nothing and gives nothing: one that others can write, one that is a -// link, one that is no folder. The genuine verdict is behind each of them, and is not read; the job ends before -// any worker starts (6) and leaves no mark and no file anywhere. A verdict file that is a link is not read -// either, and the next check puts a file of its own in the link's place. +// A folder below Grimoire's own that is not Grimoire's own keeps nothing and gives nothing: one that others can +// write, one that is a link. The genuine verdict is behind each of them, and is not read; the job ends before any +// worker starts (6), moves nothing aside, and leaves no mark and no file anywhere. A verdict file that is a link is +// not read either, and the next check puts a file of its own in the link's place. (What stands at the name of +// Grimoire's own folder itself and is not its own is moved aside: `what_is_not_grimoires_own_is_moved_aside`.) fn a_folder_that_is_not_grimoires_own_keeps_nothing() { clean_env(); let r = root("notown"); @@ -387,6 +430,10 @@ fn a_folder_that_is_not_grimoires_own_keeps_nothing() { check!(names(&cache) == before); check!(everything(&state, &real) == whole); check!(std::fs::read(&verdict).or_else(|_| std::fs::read(format!("{real}/{:016x}/fonts/{key:016x}.font", fnv(r.as_bytes())))).is_ok_and(|b| b == genuine)); + // (nothing is moved aside below Grimoire's own folder, nor beside it) + let aside = format!("fontverdict-{}-notown.state.not-own-", std::process::id()); + check!(!names(&tl_tmp()).iter().any(|n| n.starts_with(&aside))); + check!(!everything(&state, &real).iter().any(|n| n.contains(".not-own-"))); }; chmod(&fonts, 0o777); refused("its fonts folder can be written by anyone"); @@ -394,11 +441,6 @@ fn a_folder_that_is_not_grimoires_own_keeps_nothing() { chmod(&tag, 0o770); refused("the data root's folder can be written by its group"); chmod(&tag, 0o700); - std::fs::rename(&state, &real).unwrap(); - std::os::unix::fs::symlink(&real, &state).unwrap(); - refused("the folder is a link to the genuine one"); - std::fs::remove_file(&state).unwrap(); - std::fs::rename(&real, &state).unwrap(); std::fs::rename(&fonts, format!("{tag}/fonts.real")).unwrap(); std::os::unix::fs::symlink(format!("{tag}/fonts.real"), &fonts).unwrap(); check!(userfonts_try_load("Mine.ttf", "") == Err(LoadErr::Checking)); @@ -407,11 +449,6 @@ fn a_folder_that_is_not_grimoires_own_keeps_nothing() { check!(names(&cache) == before && names(&format!("{tag}/fonts.real")) == vec![format!("{key:016x}.font")]); std::fs::remove_file(&fonts).unwrap(); std::fs::rename(format!("{tag}/fonts.real"), &fonts).unwrap(); - std::fs::rename(&state, &real).unwrap(); - std::fs::write(&state, b"a file where the folder was").unwrap(); - refused("a file is where the folder was"); - std::fs::remove_file(&state).unwrap(); - std::fs::rename(&real, &state).unwrap(); // whole again: the genuine verdict is read as before check!(loads("Mine.ttf") == Ok("overprint")); check!(bg_font_check("", 300.0) == 0); @@ -682,13 +719,17 @@ fn a_name_that_is_no_absolute_path_is_not_the_cache() { std::env::set_var("GRIMOIRE_WORK_EXE", BIN); } -// The app, when its own folder cannot be used: it says so once, checks no font in that session and marks none; -// Terminess draws, and the Font sheet's row says the font cannot be read. +// The app, when its own folder cannot be used (here the data root's folder in it is a file, which is not moved +// aside: only what stands at the name of Grimoire's own folder itself is): it says so once, checks no font in +// that session and marks none; Terminess draws, and the Font sheet's row says the font cannot be read. fn the_app_says_once_that_nothing_can_be_kept() { clean_env(); let r = root("app"); let state = own_folder("app"); - std::fs::write(&state, b"a file, not a folder").unwrap(); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o700); + let tag = tag_dir(&state, &r); + std::fs::write(&tag, b"a file, not a folder").unwrap(); put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); std::fs::write(format!("{r}/settings.ini"), OWN).unwrap(); let script = "wait 100\nwaitbg\nwait 300\nexpect font is terminess\nexpect settings.font is file:Mine.ttf\ntap 1535 95\nwait 50\ntap library settings.cat.1\nwait 50\ntap library font\nwait 300\n\ @@ -702,8 +743,10 @@ expect library.font.state.Mine.ttf is unreadable\nexpect library.font.checking i check!(out.matches("fonts: what the check finds cannot be kept (see the line above): no font is checked until Grimoire starts again").count() == 1); check!(out.lines().filter(|l| l.contains("bg: font check of")).count() == 1); check!(!out.contains(&state)); + check!(!out.contains("moved aside")); check!(names(&format!("{r}/fonts/.cache")).is_empty()); - check!(std::fs::read(&state).is_ok_and(|b| b == b"a file, not a folder")); + check!(std::fs::read(&tag).is_ok_and(|b| b == b"a file, not a folder")); + check!(names(&state).len() == 1); remove_any(&state); } @@ -783,12 +826,359 @@ fn a_renamed_font_keeps_its_verdict() { } } +/// The log line of an entry moved aside from the name of Grimoire's own folder, as far as it names what it was. +fn moved_line(what: &str, mode: u32) -> String { + // SAFETY: geteuid takes no arguments. + let uid = unsafe { libc::geteuid() }; + format!("fonts: the entry at the name of Grimoire's own folder was not its own ({what}, owner {uid}, mode {:04o}): it is moved aside as it was", mode & 0o7777) +} + +const MOVED: &str = "it is moved aside as it was, and the folder is made afresh; every font is checked once more"; + +// What stands at the name of Grimoire's own folder and is not Grimoire's own (issue #201) is moved aside in its +// parent, and the folder is made afresh: a folder its group or others can write, a link to a folder of this +// user's, a link to nothing, a file, a pipe. Each is moved as it was, under a name of Grimoire's own +// (`grimoire.not-own--`): the same inode and mode, what it holds unchanged, a link still a link to +// the same place and never followed (the folder it names, outside the parent, as it was), a pipe never opened. +// The log says once what it was (its kind, owner and mode), never a path. The fresh folder is 0700, the font's +// verdict is kept in it and the font loads; a second job moves nothing. Nothing else in the parent changes. +fn what_is_not_grimoires_own_is_moved_aside() { + use std::os::unix::fs::MetadataExt; + let font = ttf(&test_font("Regular", 400)); + // SAFETY: geteuid takes no arguments. + let uid = unsafe { libc::geteuid() }; + for (kind, what) in [("group", "a folder"), ("others", "a folder"), ("link", "a link"), ("dangling", "a link"), ("file", "a file"), ("pipe", "a pipe")] { + eprintln!(" {kind}"); + clean_env(); + let r = root(&format!("aside-{kind}")); + let (parent, state) = own_parent(&format!("aside-{kind}")); + std::fs::write(format!("{parent}/beside"), b"left alone").unwrap(); + let outside = format!("{}/fontverdict-{}-aside-{kind}.outside", tl_tmp(), std::process::id()); + remove_any(&outside); + std::fs::create_dir(&outside).unwrap(); + chmod(&outside, 0o700); + std::fs::write(format!("{outside}/keep.me"), b"outside").unwrap(); + match kind { + "group" | "others" => { + std::fs::create_dir(&state).unwrap(); + std::fs::write(format!("{state}/keep.me"), b"not Grimoire's").unwrap(); + chmod(&state, if kind == "group" { 0o775 } else { 0o757 }); + } + "link" => std::os::unix::fs::symlink(&outside, &state).unwrap(), + "dangling" => std::os::unix::fs::symlink(format!("{outside}/nothing"), &state).unwrap(), + "file" => std::fs::write(&state, b"a file where the folder was").unwrap(), + _ => { + let c = std::ffi::CString::new(state.as_str()).unwrap(); + // SAFETY: mkfifo reads the NUL-terminated path. + check!(unsafe { libc::mkfifo(c.as_ptr(), 0o644) } == 0); + } + } + let Some(was) = entry(&state) else { continue }; + let times = |p: &str| std::fs::symlink_metadata(p).map(|m| (m.mtime(), m.mtime_nsec())).ok(); + let outside_was = (entry(&outside), times(&outside), times(&format!("{outside}/keep.me"))); + put(&r, "Mine.ttf", &font); + let (rc, text) = with_log(&format!("{r}/aside.log"), job); + check!(rc == 0); + check!(text.matches(&moved_line(what, was.1)).count() == 1 && text.matches(MOVED).count() == 1); + check!(!text.contains(&parent)); + // the parent: the fresh folder, the entry under its new name, and what was beside it, as it was + let moved = asides(&parent); + check!(moved.len() == 1 && names(&parent).len() == 3); + let Some(aside) = moved.first() else { continue }; + let tail: Vec<&str> = aside["grimoire.not-own-".len()..].split('-').collect(); + check!(tail.len() == 2 && tail[0].parse::().is_ok_and(|s| s > 1_700_000_000) && tail[1] == std::process::id().to_string()); + check!(entry(&format!("{parent}/{aside}")).as_ref() == Some(&was)); + check!(std::fs::read(format!("{parent}/beside")).is_ok_and(|b| b == b"left alone")); + check!((entry(&outside), times(&outside), times(&format!("{outside}/keep.me"))) == outside_was); + // the fresh folder: Grimoire's own, with the font's verdict in it, and the font loads + check!(std::fs::symlink_metadata(&state).is_ok_and(|m| m.is_dir() && m.mode() & 0o7777 == 0o700 && m.uid() == uid)); + check!(with_ext(&format!("{}/fonts", tag_dir(&state, &r)), ".font").len() == 1); + check!(loads("Mine.ttf") == Ok("overprint")); + // a second job, in this process and in one forked as the app forks it: nothing is moved again + check!(job() == 0); + bg_queue(BgKind::FontCheck, Some(""), Some("again"), 300.0, false); + check!(wait_job() == 0); + check!(asides(&parent) == moved && names(&parent).len() == 3); + check!(entry(&format!("{parent}/{aside}")).as_ref() == Some(&was)); + remove_any(&parent); + remove_any(&outside); + } +} + +// At most once in a process too, even with leave (USERFONTS_AFRESH): after a move aside, what stands at the name +// and is not Grimoire's own again (the fresh folder renamed away and a folder others can write put in its place) is +// not moved by that process. Nothing is kept (6, as for any folder that is not Grimoire's own), and the entry stays +// as it is. A fresh process given leave moves it, and checks the font once more; but never onto a name that is +// taken: with an empty folder at every name it could take in the next seconds (one a plain rename of a folder would +// replace), it gives up and they are as they were. (The app gives that leave to the first check of a run alone: +// `a_run_makes_its_own_folder_afresh_once`.) +fn moved_aside_at_most_once_a_process() { + clean_env(); + let r = root("aside-once"); + let (parent, state) = own_parent("aside-once"); + put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o777); + check!(job() == 0); + check!(asides(&parent).len() == 1); + std::fs::rename(&state, format!("{parent}/renamed")).unwrap(); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o777); + let was = entry(&state); + let cache = format!("{r}/fonts/.cache"); + let cache_before = names(&cache); + let (rc, text) = with_log(&format!("{r}/once.log"), || bg_font_check(USERFONTS_AFRESH, 300.0)); + check!(rc == 6); + check!(text.contains("fonts: what the check finds cannot be kept (it is not Grimoire's own); nothing is marked")); + check!(!text.contains("moved aside")); + check!(asides(&parent).len() == 1 && names(&parent).len() == 3 && entry(&state) == was); + check!(names(&cache) == cache_before); + // the next process, with every name it could move to taken + let first: i64 = asides(&parent).first().and_then(|n| n["grimoire.not-own-".len()..].split('-').next()?.parse().ok()).unwrap_or(0); + let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64; + let decoys: Vec = (now - 1..now + 30).map(|s| format!("{parent}/grimoire.not-own-{s}-{}", std::process::id())).filter(|p| !std::path::Path::new(p).exists()).collect(); + for d in &decoys { + std::fs::create_dir(d).unwrap(); + } + let all_before: Vec> = names(&parent).iter().map(|n| entry(&format!("{parent}/{n}"))).collect(); + userfonts_aside_reset(); + let (rc, text) = with_log(&format!("{r}/once.log"), || bg_font_check(USERFONTS_AFRESH, 300.0)); + check!(rc == 6 && text.contains(") and cannot be moved aside (File exists)") && !text.contains("moved aside as it was")); + check!(names(&parent).iter().map(|n| entry(&format!("{parent}/{n}"))).collect::>() == all_before); + for d in &decoys { + std::fs::remove_dir(d).unwrap(); + } + // the next process: moved (in a later second than the first move, whose name it would take) + while std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64 <= first { + std::thread::sleep(std::time::Duration::from_millis(50)); + } + userfonts_aside_reset(); + let (rc, text) = with_log(&format!("{r}/once.log"), job); + check!(rc == 0 && text.matches(MOVED).count() == 1); + check!(asides(&parent).len() == 2 && names(&parent).len() == 4); + check!(text.contains("fonts: file 1 of 1 passed")); + check!(loads("Mine.ttf") == Ok("overprint")); + remove_any(&parent); +} + +// An entry that comes to the name between the move aside and the fresh folder's make (another writer of the parent, +// at that moment) is not moved: the job gives up (6, as for any folder that is not Grimoire's own), says why and +// marks nothing, and the entry stays at the name as it was put there, beside the one entry moved aside. +fn an_entry_back_at_once_is_not_moved_again() { + clean_env(); + let r = root("aside-race"); + let (parent, state) = own_parent("aside-race"); + put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + let again = state.clone(); + userfonts_aside_seam(Some(Box::new(move || { + std::fs::create_dir(&again).unwrap(); + std::fs::set_permissions(&again, std::fs::Permissions::from_mode(0o777)).unwrap(); + }))); + let (rc, text) = with_log(&format!("{r}/race.log"), || bg_font_check(USERFONTS_AFRESH, 300.0)); + userfonts_aside_seam(None); + check!(rc == 6); + check!(text.matches(MOVED).count() == 1); + check!(text.contains("fonts: the folder made afresh is not Grimoire's own either (it is not Grimoire's own); nothing more is moved aside")); + check!(text.contains("fonts: what the check finds cannot be kept (it is not Grimoire's own); nothing is marked")); + check!(asides(&parent).len() == 1 && names(&parent).len() == 2); + check!(mode_of(&state) == 0o777 && names(&state).is_empty()); + check!(names(&format!("{r}/fonts/.cache")).is_empty()); + remove_any(&parent); +} + +// A parent that takes no write: the entry cannot be moved aside. The job gives up (6, as for any folder that is not +// Grimoire's own), says why, and the parent is as it was. With nothing at the name the folder cannot be made, as +// before this change. (Root writes into a folder whatever its mode says: not run as root.) +fn a_parent_that_takes_no_write_moves_nothing() { + clean_env(); + // SAFETY: geteuid takes no arguments. + let uid = unsafe { libc::geteuid() }; + if uid == 0 { + eprintln!(" not run as root: no folder refuses root a write by its mode"); + return; + } + let r = root("aside-nowrite"); + let (parent, state) = own_parent("aside-nowrite"); + put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + let (was, before) = (entry(&state), names(&parent)); + chmod(&parent, 0o555); + let (rc, text) = with_log(&format!("{r}/nowrite.log"), || bg_font_check(USERFONTS_AFRESH, 300.0)); + chmod(&parent, 0o755); + check!(rc == 6); + check!(text.contains(&format!("fonts: the entry at the name of Grimoire's own folder is not its own (a folder, owner {uid}, mode 0775) and cannot be moved aside ("))); + check!(text.contains("fonts: what the check finds cannot be kept (it is not Grimoire's own); nothing is marked")); + check!(!text.contains(MOVED)); + check!(names(&parent) == before && entry(&state) == was); + remove_any(&state); + userfonts_aside_reset(); + chmod(&parent, 0o555); + let (rc, text) = with_log(&format!("{r}/nowrite.log"), || bg_font_check(USERFONTS_AFRESH, 300.0)); + chmod(&parent, 0o755); + check!(rc == 6); + check!(text.contains("fonts: what the check finds cannot be kept (it cannot be made); nothing is marked")); + check!(names(&parent).is_empty()); + remove_any(&parent); +} + +// The app, started with a folder others can write at the name of its own folder: the job it forks moves that aside, +// checks the font in the fresh folder, and the font draws. The log says so once and never names a path. +fn the_app_moves_aside_what_is_not_its_own() { + clean_env(); + let r = root("app-aside"); + let (parent, state) = own_parent("app-aside"); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); + std::fs::write(format!("{r}/settings.ini"), OWN).unwrap(); + let script = "wait 100\nwaitbg\nwait 300\nexpect font is file:Mine.ttf\nexpect settings.font is file:Mine.ttf\nquit\n"; + let (code, out) = start(&r, script, &[("GRIMOIRE_STATE", &state), ("GRIMOIRE_WORK_EXE", BIN)]); + check!(code == Some(0)); + if code != Some(0) { + eprintln!("{out}"); + } + check!(out.matches(&moved_line("a folder", 0o775)).count() == 1); + check!(!out.contains(&parent)); + check!(asides(&parent).len() == 1 && names(&parent).len() == 2); + check!(mode_of(&state) == 0o700); + check!(with_ext(&format!("{}/fonts", tag_dir(&state, &r)), ".font").len() == 1); + remove_any(&parent); +} + +// At most once a run: the UI gives the first check of a run alone leave to make Grimoire's own folder, or make it +// afresh (USERFONTS_AFRESH). The run's first look queues a check (forked, as the app forks it) that moves aside the +// folder others can write at the name and checks the font in the fresh folder. With such a folder at the name again, +// and then with nothing there, the run's later checks keep nothing (6): they move nothing, make nothing, mark nothing, +// and say why. The next run (the UI's memory starts afresh) moves it aside once more and checks the font again. +fn a_run_makes_its_own_folder_afresh_once() { + clean_env(); + let r = root("aside-run"); + let (parent, state) = own_parent("aside-run"); + put(&r, "Mine.ttf", &ttf(&test_font("Regular", 400))); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + let log = format!("{r}/run.log"); + let (rc, text) = with_log(&log, || { + let _ = userfonts_scan(300.0); + wait_job() + }); + check!(rc == 0 && text.matches(MOVED).count() == 1 && text.contains("fonts: file 1 of 1 passed")); + check!(asides(&parent).len() == 1 && mode_of(&state) == 0o700); + // (a later check of the run, queued by a load that finds no verdict) + let later = || { + check!(userfonts_try_load("Mine.ttf", "") == Err(LoadErr::Checking)); + wait_job() + }; + std::fs::rename(&state, format!("{parent}/away")).unwrap(); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + let (was, cache_before) = (entry(&state), names(&format!("{r}/fonts/.cache"))); + let (rc, text) = with_log(&log, &later); + check!(rc == 6 && !text.contains("moved aside")); + check!(text.contains("fonts: what the check finds cannot be kept (it is not Grimoire's own); nothing is marked")); + check!(asides(&parent).len() == 1 && entry(&state) == was); + remove_any(&state); + let (rc, text) = with_log(&log, &later); + check!(rc == 6 && text.contains("fonts: what the check finds cannot be kept (it is not there); nothing is marked")); + check!(std::fs::symlink_metadata(&state).is_err() && asides(&parent).len() == 1); + check!(names(&format!("{r}/fonts/.cache")) == cache_before); + // the next run + remove_any(&state); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + userfonts_aside_reset(); + let (rc, text) = with_log(&log, &later); + check!(rc == 0 && text.matches(MOVED).count() == 1 && text.contains("fonts: file 1 of 1 passed")); + check!(asides(&parent).len() == 2 && mode_of(&state) == 0o700); + check!(loads("Mine.ttf") == Ok("overprint")); + remove_any(&parent); +} + +/// A writer of the parent that is never Grimoire, racing the app (the review's helper of issue #201): whenever a +/// folder of Grimoire's own (0700) stands at the name it renames it away and, when `plant`, puts a folder others can +/// write in its place. It runs until `stop`; how many folders it took. +fn racer(parent: String, plant: bool, stop: std::sync::Arc) -> std::thread::JoinHandle { + std::thread::spawn(move || { + let name = format!("{parent}/grimoire"); + let mut taken = 0; + while !stop.load(std::sync::atomic::Ordering::Relaxed) { + let own = std::fs::symlink_metadata(&name).is_ok_and(|m| m.is_dir() && m.permissions().mode() & 0o777 == 0o700); + if own && std::fs::rename(&name, format!("{parent}/taken-{taken}")).is_ok() { + taken += 1; + let p = format!("{parent}/planting"); + if plant && std::fs::create_dir(&p).is_ok() { + let _ = std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o775)); + if std::fs::rename(&p, &name).is_err() { + let _ = std::fs::remove_dir(&p); + } + } + } + std::thread::sleep(std::time::Duration::from_micros(500)); + } + taken + }) +} + +// The app against a writer of the parent that takes Grimoire's folder away after every check, putting a folder others +// can write in its place, or leaving nothing. Three fonts, so that a run goes on from check to check (one font a +// check). One move aside a run at most, and three checks at most: once the run's first check is done, a check that +// finds the folder gone or not Grimoire's own keeps nothing, and the session checks no more fonts (said once). The +// next start, with no such writer, moves aside the folder put there once, and the font draws. +fn a_writer_of_the_parent_costs_one_move_a_run() { + for plant in [true, false] { + clean_env(); + let what = if plant { "plant" } else { "away" }; + let r = root(&format!("app-race-{what}")); + let (parent, state) = own_parent(&format!("app-race-{what}")); + let font = ttf(&test_font("Regular", 400)); + for n in ["Mine.ttf", "Two.ttf", "Three.ttf"] { + put(&r, n, &font); + } + std::fs::write(format!("{r}/settings.ini"), OWN).unwrap(); + if plant { + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + } + let env = [("GRIMOIRE_STATE", state.as_str()), ("GRIMOIRE_WORK_EXE", BIN)]; + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let racing = racer(parent.clone(), plant, stop.clone()); + let (code, out) = start(&r, "wait 2500\nquit\n", &env); + stop.store(true, std::sync::atomic::Ordering::Relaxed); + let taken = racing.join().unwrap_or(0); + let checks = out.lines().filter(|l| l.contains("bg: font check of")).count(); + let (moved, stopped) = (out.matches(MOVED).count(), out.matches("no font is checked until Grimoire starts again").count()); + eprintln!(" {what}: {checks} check(s), {moved} moved aside, {taken} folder(s) taken by the writer, the checks stopped {stopped} time(s)"); + check!(code == Some(0)); + check!(taken >= 1 && (1..=3).contains(&checks) && stopped <= 1); + check!(moved == usize::from(plant) && asides(&parent).len() == usize::from(plant)); + check!(!out.contains(&parent)); + if plant { + remove_any(&state); + std::fs::create_dir(&state).unwrap(); + chmod(&state, 0o775); + let (code, out) = start(&r, "wait 100\nwaitbg\nwait 300\nexpect font is file:Mine.ttf\nquit\n", &env); + check!(code == Some(0) && out.matches(MOVED).count() == 1 && asides(&parent).len() == 2); + if code != Some(0) { + eprintln!("{out}"); + } + } + remove_any(&parent); + } +} + #[test] fn test_sec_font_verdict() { run!(only_grimoires_own_folder_is_read); run!(the_verdict_is_kept_in_grimoires_own_folder); run!(checked_once_on_this_device_and_again_elsewhere); run!(a_folder_that_is_not_grimoires_own_keeps_nothing); + run!(what_is_not_grimoires_own_is_moved_aside); + run!(moved_aside_at_most_once_a_process); + run!(an_entry_back_at_once_is_not_moved_again); + run!(a_parent_that_takes_no_write_moves_nothing); run!(two_data_roots_keep_their_verdicts_apart); run!(an_earlier_versions_verdict_is_left_alone); run!(the_fonts_cache_deleted_checks_again); @@ -798,5 +1188,8 @@ fn test_sec_font_verdict() { run!(a_name_that_is_no_absolute_path_is_not_the_cache); run!(a_renamed_font_keeps_its_verdict); run!(the_app_says_once_that_nothing_can_be_kept); + run!(the_app_moves_aside_what_is_not_its_own); + run!(a_run_makes_its_own_folder_afresh_once); + run!(a_writer_of_the_parent_costs_one_move_a_run); done(); } diff --git a/rust/tests/test_sec_invariants.rs b/rust/tests/test_sec_invariants.rs index 68a0586a..5af8904f 100644 --- a/rust/tests/test_sec_invariants.rs +++ b/rust/tests/test_sec_invariants.rs @@ -218,9 +218,9 @@ fn the_docs_name_the_record_cap_with_its_size_and_not_as_a_jobs() { // ties it to its file, the `.slow` mark, and the limits. The sentences that described these before that change // are gone from the files that held them (a merge settled by the older side would bring them back), and the // documents name what the code has: the companion a refusal leaves, the verdict's number, the glyph limit with -// its figure beside it. The own folder's place and the floors of its free room are placeholders until a -// Scribe's figures are in: while the documents give them, userfonts.rs must still hold them, so the day one -// is changed the sentence is read again and follows. +// its figure beside it. The own folder's place is a placeholder until a Scribe's figures are in, and the floors +// of its free room are set from the first of them: while the documents give them, userfonts.rs must still hold +// them, so the day one is changed the sentence is read again and follows. #[test] fn the_docs_follow_the_font_checks_verdicts_and_limits() { let mut fails = Vec::new(); @@ -267,6 +267,74 @@ fn the_docs_follow_the_font_checks_verdicts_and_limits() { none_failed(&fails, "your own fonts: verdicts and limits"); } +// Grimoire's own folder on the Kindle (issue #201): what stands at its name and is not Grimoire's own is moved aside +// and the folder made afresh. Each rule sentence of the documents is held with the line of userfonts.rs that makes +// it true: the job alone moves, at the first level only, at most once a run (the UI gives the first check of a run +// alone leave to make the folder, or make it afresh, and a later check makes nothing), to a name of Grimoire's own, +// by a rename that never replaces an entry, looking at the entry without following it. The sentence that says what +// is never done to the entry is held with the whole function that moves it, so that any change to that function has +// the sentence read again. The floors of free room stand on the Scribe's answers and are no longer placeholders. +#[test] +fn the_docs_follow_the_own_folders_move_aside() { + let mut fails = Vec::new(); + let at_base = "Err(NOT_OWN) if afresh => base_afresh(&parent, name)?,"; + let below = "let tag = own_dir_at(&base, &root_tag(&store_root()), make)?;"; + let name = "format!(\"{name}.not-own-{secs}-{pid}\")"; + let run = "let arg = if with(|st| !std::mem::replace(&mut st.afresh_given, true)) { format!(\"{USERFONTS_AFRESH}{arg}\") } else { arg.to_string() };"; + let made = "let base = match own_dir_at(&parent, name, afresh) {"; + let line = "it is moved aside as it was, and the folder is made afresh; every font is checked once more"; + // the parent holds the name and nothing more: it is opened by its path and held to nothing, and what it holds at + // the name is tested at every open (the words that said Grimoire never relies on it said more than that) + let relies = "Grimoire relies on the parent (`/var/local` on the Kindle) only to hold the own folder's name: it tests the folder it opens at every open, and never trusts the parent's owner or mode."; + told_on(&mut fails, DESIGN, relies, USERFONTS, "let parent = open_dir(if parent.is_empty() { \"/\" } else { parent }, false).ok_or(NOT_THERE)?;"); + told_on(&mut fails, DESIGN, relies, USERFONTS, "if own_dir(&fd) {"); + gone(&mut fails, DESIGN, "Grimoire never relies on the parent"); + told_on(&mut fails, DESIGN, "When the entry at the own folder's name is not Grimoire's own, the first check of a run moves it aside within the parent", USERFONTS, at_base); + told_on(&mut fails, TESTING, "Grimoire never relies on /var/local itself: it tests its own folder at every open and moves aside an entry at its name that is not its own", USERFONTS, at_base); + told_on(&mut fails, KINDLE, "When the entry at `/var/local/grimoire` is not Grimoire's own (a link, a file, or a folder of another user or that others can write), the first check after Grimoire starts moves it aside as it is, never opened", USERFONTS, at_base); + told_on(&mut fails, DESIGN, "The folders below the own folder are its own, which no one else can write, and are never moved.", USERFONTS, below); + told_on(&mut fails, DESIGN, "to `grimoire.not-own--`, and makes the folder afresh, at most once a run", USERFONTS, name); + told_on(&mut fails, KINDLE, "to `/var/local/grimoire.not-own--`, and makes the folder afresh", USERFONTS, name); + // once a run: the UI's leave, from its memory, for the first check of a run alone; a later check makes nothing + told_on(&mut fails, DESIGN, "gives the first check of a run alone leave to make the own folder or make it afresh, from its own memory, never from a file", USERFONTS, run); + told_on(&mut fails, DESIGN, "(the own folder itself by the first check of a run alone)", USERFONTS, made); + told_on(&mut fails, DESIGN, "or, after the first check of a run, it is gone or not Grimoire's own", USERFONTS, made); + let later = "That is done once a run: a later check of the same run that finds such an entry there, or nothing, keeps nothing, and no font is checked until Grimoire starts again."; + told_on(&mut fails, KINDLE, later, USERFONTS, run); + told_on(&mut fails, KINDLE, later, USERFONTS, made); + told_on(&mut fails, KINDLE, "at most once a run, by its first check, which then goes on in the fresh folder", USERFONTS, run); + told_on(&mut fails, TESTING, "Once a run: with Grimoire running", USERFONTS, run); + // under the Kindle's setgid /var/local the folders Grimoire makes are 02700 and of its group: own_stat asks only + // the write bits of group and others, so they are Grimoire's own + let own = "(st.st_mode & libc::S_IFMT) == libc::S_IFDIR && st.st_uid == unsafe { libc::geteuid() } && st.st_mode & 0o022 == 0"; + told_on(&mut fails, TESTING, "under a /var/local with the setgid bit, as in step 1, the folders show `drwx--S---` and its group, which they take from it and which has no rights in them", USERFONTS, own); + told_on(&mut fails, DESIGN, "by a rename that never replaces an entry", USERFONTS, "rename_noreplace(parent.as_raw_fd(), &c, parent.as_raw_fd(), &to)"); + told_on(&mut fails, DESIGN, "and is never followed, opened, read, listed or deleted", USERFONTS, "libc::fstatat(parent.as_raw_fd(), c.as_ptr(), &mut st, libc::AT_SYMLINK_NOFOLLOW)"); + // (renamed by its name from the parent's descriptor: what was looked at is what the log describes, and an entry + // swapped in at that moment is the one moved; the entry is never held by a descriptor of its own) + told_on_code(&mut fails, DESIGN, "The entry is renamed by its name, from the parent's descriptor, as it stands, by a rename that never replaces an entry, and is never followed, opened, read, listed or deleted; its kind, owner and mode, as looked at just before the rename, are logged (an entry another writer of the parent puts at the name in between is the one moved, and is left as it is too).", USERFONTS, "fn base_afresh(", BASE_AFRESH); + gone(&mut fails, DESIGN, "The entry is renamed by descriptor as it stands"); + told_on_code(&mut fails, KINDLE, "An entry moved aside is left too: Grimoire never opens or removes it.", USERFONTS, "fn base_afresh(", BASE_AFRESH); + // the log line the device check and the log's list quote is the one the code writes, and so are the two lines of + // a move that cannot be made or a fresh folder that is not Grimoire's own either + told_on(&mut fails, TESTING, line, USERFONTS, line); + told_on(&mut fails, KINDLE, line, USERFONTS, line); + told_on(&mut fails, KINDLE, "When the move cannot be made, `... is not its own (...) and cannot be moved aside ()`", USERFONTS, "fonts: the entry at the name of Grimoire's own folder is not its own (%s, owner %u, mode %04o) and cannot be moved aside (%s)"); + let either = "fonts: the folder made afresh is not Grimoire's own either"; + told_on(&mut fails, KINDLE, &format!("`{either} (); nothing more is moved aside`"), USERFONTS, &format!("{either} (%s); nothing more is moved aside")); + // the floors: set from step 1's answers, no longer placeholders + told_on(&mut fails, TESTING, "The floors stand on these numbers: `STATE_FREE_FLOOR`, 4 MB, and `STATE_FILES_FLOOR`, 512 files", USERFONTS, "const STATE_FREE_FLOOR: u64 = 4 << 20;"); + told_on(&mut fails, TESTING, "The floors stand on these numbers: `STATE_FREE_FLOOR`, 4 MB, and `STATE_FILES_FLOOR`, 512 files", USERFONTS, "const STATE_FILES_FLOOR: u64 = 512;"); + gone(&mut fails, USERFONTS, "Placeholders until the Scribe's figures are in"); + gone(&mut fails, TESTING, "which step 1 answers"); + gone(&mut fails, TESTING, "must be root's, with no write bit for group or others"); + none_failed(&fails, "Grimoire's own folder: what is not its own is moved aside"); +} + +/// `base_afresh` in userfonts.rs, its comments aside (`code_of`): what DESIGN.md's sentence on the entry moved aside +/// stands on. +const BASE_AFRESH: &str = r#"fn base_afresh(parent: &OwnedFd, name: &str) -> Result { let c = CString::new(name).map_err(|_| NOT_OWN)?; let mut st: libc::stat = unsafe { std::mem::zeroed() }; if unsafe { libc::fstatat(parent.as_raw_fd(), c.as_ptr(), &mut st, libc::AT_SYMLINK_NOFOLLOW) } != 0 { return Err(NOT_OWN); } if own_stat(&st) || MOVED_ASIDE.swap(true, std::sync::atomic::Ordering::Relaxed) { return Err(NOT_OWN); } let (kind, owner, mode) = (kind_of(st.st_mode), st.st_uid, (st.st_mode & 0o7777) as u32); let aside = aside_name(name, wall_time(), std::process::id()); let moved = match CString::new(aside.as_str()) { Ok(to) if fsview_name_check(b"", aside.as_bytes(), true).is_ok() => rename_noreplace(parent.as_raw_fd(), &c, parent.as_raw_fd(), &to), _ => Err(libc::ENAMETOOLONG), }; if let Err(e) = moved { qlog!("fonts: the entry at the name of Grimoire's own folder is not its own (%s, owner %u, mode %04o) and cannot be moved aside (%s)", kind, owner, mode, &strerror(e)); return Err(NOT_OWN); } qlog!("fonts: the entry at the name of Grimoire's own folder was not its own (%s, owner %u, mode %04o): it is moved aside as it was, and the folder is made afresh; every font is checked once more", kind, owner, mode); #[cfg(any(test, feature = "sim"))] if let Some(f) = ASIDE_SEAM.lock().unwrap_or_else(|e| e.into_inner()).take() { f(); } own_dir_at(parent, name, true).inspect_err(|why| qlog!("fonts: the folder made afresh is not Grimoire's own either (%s); nothing more is moved aside", *why)) }"#; + // ---- Cargo.lock against deny.toml and the skrifa pin ---- /// A package of Cargo.lock: its name, its version and what it depends on (a name, with a version when the lock @@ -768,7 +836,7 @@ fn a_font_added_from_a_file_is_told_as_it_is() { // passing verdict's, and guarded_face, the one maker of a face from them. A preview of it in Files' Covers view // is the sandboxed worker's sample (font_preview, by font_sample's worker). Those sentences are held with that // code; the words that said a copied font is "not sampled" and has "no preview" are gone (Covers makes one). - const LOAD_FILE: &str = r#"fn load_file(st: &FsStorage, rel: &str, dpi: f32, room: usize) -> Result { if !userfont_rel_ok(rel) { return Err(LoadErr::Missing); } let sp = StoragePath::new(rel.as_bytes()).ok_or(LoadErr::Missing)?; let s = st.stat(&sp, false).map_err(|_| LoadErr::Missing)?; let l = Listed { rel: rel.to_string(), size: s.size, mtime: s.mtime, key: key_of(rel, s.size, s.mtime) }; if name_kind(rel.rsplit('/').next().unwrap_or("")) != NameKind::Font || l.size == 0 || l.size > FONT_FILE_MAX as u64 || guard_out(l.key) { return Err(LoadErr::Refused); } let Some(v) = VerdictDir::open(false).ok().and_then(|vd| verdict_for(&vd, l.key, l.size)) else { let bad = std::path::Path::new(&cache_file(l.key, "bad")).exists(); if bad { return Err(LoadErr::Refused); } queue_check("", dpi); return Err(LoadErr::Checking); }; let VerdictKind::Passed(facts) = v.what else { return Err(LoadErr::Refused) }; if l.size as usize > room { return Err(LoadErr::Memory); } let data = read_listed(st, &l).ok_or(LoadErr::Missing)?; if font_hash(&data) != v.hash { qlog!("font: a font file is not the one that was checked: checking it again"); queue_check(&format!("recheck:{:016x}", l.key), dpi); return Err(LoadErr::Checking); } Ok(Loaded { data: Rc::new(data), facts: Rc::new(facts), key: l.key }) }"#; + const LOAD_FILE: &str = r#"fn load_file(st: &FsStorage, rel: &str, dpi: f32, room: usize) -> Result { if !userfont_rel_ok(rel) { return Err(LoadErr::Missing); } let sp = StoragePath::new(rel.as_bytes()).ok_or(LoadErr::Missing)?; let s = st.stat(&sp, false).map_err(|_| LoadErr::Missing)?; let l = Listed { rel: rel.to_string(), size: s.size, mtime: s.mtime, key: key_of(rel, s.size, s.mtime) }; if name_kind(rel.rsplit('/').next().unwrap_or("")) != NameKind::Font || l.size == 0 || l.size > FONT_FILE_MAX as u64 || guard_out(l.key) { return Err(LoadErr::Refused); } let Some(v) = VerdictDir::open(false, false).ok().and_then(|vd| verdict_for(&vd, l.key, l.size)) else { let bad = std::path::Path::new(&cache_file(l.key, "bad")).exists(); if bad { return Err(LoadErr::Refused); } queue_check("", dpi); return Err(LoadErr::Checking); }; let VerdictKind::Passed(facts) = v.what else { return Err(LoadErr::Refused) }; if l.size as usize > room { return Err(LoadErr::Memory); } let data = read_listed(st, &l).ok_or(LoadErr::Missing)?; if font_hash(&data) != v.hash { qlog!("font: a font file is not the one that was checked: checking it again"); queue_check(&format!("recheck:{:016x}", l.key), dpi); return Err(LoadErr::Checking); } Ok(Loaded { data: Rc::new(data), facts: Rc::new(facts), key: l.key }) }"#; const GUARDED_FACE: &str = r#"fn guarded_face(l: &Loaded, bold: bool) -> Option { if !guard_arm_keys(&[l.key]) { qlog!("font: the guard's mark could not be written: your font is not used"); return None; } let face = SafeFace::new(l.data.clone(), l.facts.clone(), bold); if face.is_none() && !bold { guard_disarm_keys(&[l.key]); } face }"#; const FONT_PREVIEW: &str = r#"fn font_preview(data: &[u8]) -> Result { use crate::fontcheck::*; match font_sample(data, PREVIEW_W, PREVIEW_H, mono_ms() + preview_limit_ms(PreviewKind::Font)) { Ok(px) => gray_of(&px, PREVIEW_W, PREVIEW_H, 1), Err(FontOut::Refused(_)) => Err(Fail::Refused), Err(FontOut::Slow | FontOut::SlowGlyphs { .. }) => Err(Fail::Strike(NoPreview::Slow)), Err(FontOut::Dead(_)) => Err(Fail::Strike(NoPreview::Ended)), Err(FontOut::NoWorker(_)) => Err(Fail::Later(NO_WORKER)), Err(_) => Err(Fail::Later("out of memory")), } }"#; let covers = "a preview of it in Files' Covers view is made by the sandboxed worker, as for any font in Files (issue #145)";