Skip to content

Fonts: Grimoire's own folder on the Kindle is made afresh when the entry at its name is not its own (#201) - #202

Merged
ganten1998 merged 5 commits into
mainfrom
201-font-state-dir-afresh
Nov 20, 2026
Merged

ganten1998 merged 5 commits into
mainfrom
201-font-state-dir-afresh

Conversation

@ganten1998

@ganten1998 ganten1998 commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Fonts: Grimoire's own folder on the Kindle (/var/local/grimoire, where what a font's check found is kept) is made afresh when the entry at its name is not Grimoire's own (#201).

What it does

  • The background job that checks fonts opens Grimoire's own folder by descriptor, from its parent, and holds it to "a folder of this user's that no one else may write", as before (own_dir_at, own_dir, own_stat in userfonts.rs). When the entry at that name does not pass, and looked at without following it (fstatat with AT_SYMLINK_NOFOLLOW) is not such a folder (a link, a file, a pipe, a folder of another user or one that others may write), the job moves it aside within the parent and makes the folder afresh (base_afresh).
  • The move is fsview's rename that never replaces an entry (Files' storage layer: a rename that never replaces, the trash with its record, restore, and a copy into Grimoire's folders #128, rename_noreplace): renameat2 with RENAME_NOREPLACE, or where the kernel or the file system refuses the flag, the reservation of the new name first. The new name is <name>.not-own-<seconds>-<pid> (grimoire.not-own-1763500000-1234 on the Kindle). It is held to fsview_name_check before the move, and it cannot replace anything: an entry already at that name makes the rename fail with EEXIST and nothing moves. The entry is renamed by its name, from the parent's descriptor, as it stands: never followed, opened, read, listed or deleted.
  • The folder is then made afresh (mkdirat 0700), opened and held to own_dir exactly as before. It holds no verdict, so every font is checked once more, as after rm -r /var/local/grimoire.
  • Once a run. The app forks a process for each check, so the decision is the UI's, from its own memory: the first check it queues in a run carries the mark USERFONTS_AFRESH at the head of its argument, and only a check with that mark may make Grimoire's own folder when it is missing or move aside what stands at its name (VerdictDir::open's afresh). A later check of the run needs the folder there and Grimoire's own; otherwise it keeps nothing and ends with status 6, "what the check finds cannot be kept (it is not Grimoire's own)" or "(it is not there)", and the session checks no more fonts until Grimoire starts again, said once. No file on the storage or in the parent takes part in that decision. A process also tries the move at most once.
  • When the move fails (the parent takes no write, the name is taken) or the fresh folder is not Grimoire's own either (another entry came to the name between the move and the make), nothing is kept, as before: status 6, and the session checks no more fonts.
  • Only the first level. The folders inside (a data root's folder and its fonts) are inside Grimoire's own 0700 folder, which no one else can write; one of them that does not pass keeps nothing, as before, and is never moved.
  • A folder of this user's that no one else may write and still did not open (out of descriptors, say) is not moved.
  • Under a parent with the setgid bit, as the Kindle's /var/local is, the folders Grimoire makes come out 02700 with the parent's group. own_stat asks only the write bits of group and others, so they are Grimoire's own: the setgid bit, like the sticky bit, lets no one else write the folder, and the group has no rights in it. The comment on own_stat says so.
  • The log says once what the entry was, its kind, owner and mode as looked at just before the rename, never its name or what is in it: fonts: the entry at the name of Grimoire's own folder was not its own (a folder, owner 0, mode 2775): it is moved aside as it was, and the folder is made afresh; every font is checked once more. When it gives up: ... is not its own (...) and cannot be moved aside (<why>), or fonts: the folder made afresh is not Grimoire's own either (<why>); nothing more is moved aside.

No new user-visible string (log lines only); po/ is untouched.

Changes after review

  • 43f6976a: once a run, as above. Before it, each check could move an entry aside (once a process, and each check is a process), and a check that ended with more fonts left queued the next one, which made a missing folder anew; with a folder put back at the name, or taken away, after every check, the checks went on from one to the next. Now the run's later checks end with status 6 and the session stops checking. The review's helper (a writer of the parent racing the app, three fonts): 2 checks and 1 move with a writable folder put back after every check, 2 checks with the folder taken away each time.
  • 74b74700: the setgid parent, said in own_stat's comment and tested (a unit test of the rule by mode; the Linux root test now uses a 02775 parent of another user and group).
  • 8343c32b: DESIGN.md says exactly how the entry is renamed and what the log describes, and that Grimoire relies on the parent only to hold the folder's name (it tests the folder it opens at every open and never trusts the parent's owner or mode); KINDLE.md's remaining rule sentences are pinned.

The device's answers, and the floors

docs/TESTING.md's fonts section, step 1, gives what a Scribe answered (firmware 5.20.1.0.2, Grimoire 2.1.0): /var/local is a file system of its own, ext4, mounted read and write, 996,780 KB with 868,764 KB free and 65,536 inodes with 63,718 free; /var/local itself is mode 2775, of a system user and group. The step's rule is what the code needs: a file system of its own that keeps its files, owners and modes, with the room the floors ask. Grimoire tests its own folder at every open and moves aside an entry at its name that is not its own, so the owner and mode of /var/local ask nothing of a release. STATE_FREE_FLOOR (4 MB) and STATE_FILES_FLOOR (512 files) stand on those numbers (a check keeps a few hundred bytes and one file a font, at most 200 for a data folder) and are no longer called placeholders, in the docs and in the code's comment; the glyph limit and the draw size stay placeholders.

Docs

  • docs/TESTING.md: step 1's answers and rule as above; step 2 expects no moved aside line; step 3 says what a folder made under a setgid /var/local shows (drwx--S---, its group with no rights); step 12 checks the move aside on the device (Grimoire closed, mv /var/local/grimoire /var/local/grimoire.test && mkdir -m 775 /var/local/grimoire, start, the log line, the fresh folder, every font checked once more, a second start moves nothing), then once a run (the same with Grimoire running and a new font copied in: nothing more is moved aside, the checks stop, the next start moves it).
  • docs/KINDLE.md: where the verdicts are kept; the first check after Grimoire starts makes the folder afresh when the entry at its name is not Grimoire's own, once a run; an entry moved aside is left for removal by hand; the log lines.
  • docs/DESIGN.md: the verdict store's rules.
  • CHANGELOG.md: one sentence in the existing "Your own fonts: a stricter background check" entry.
  • docs/BACKLOG.md: the entry on the untested parent goes; removing by hand covers what was moved aside.

Tests

rust/tests/test_sec_font_verdict.rs (each case through the real job and a real worker, in this process unless said):

  • what_is_not_grimoires_own_is_moved_aside: a folder with group write (0775), one with other write (0757), a link to a folder of this user's outside the parent, a link to nothing, a file, a pipe. Each is moved aside under the new name with the same inode and mode, what it holds unchanged, a link still a link to the same target, the folder it names untouched (names, bytes, times), a pipe never opened; the log line once with its kind, owner and mode and no path; the fresh folder 0700 with the font's verdict in it, and the font loads; a second job, in this process and forked as the app forks it, moves nothing; the other entry in the parent is as it was.
  • a_run_makes_its_own_folder_afresh_once: the run's first look queues a check (forked, as the app forks it) that moves aside and checks the font; with a writable folder put back at the name, and then with nothing there, later checks of the run end with status 6, move nothing, make nothing and mark nothing, and say why; the next run moves once more and the font loads.
  • a_writer_of_the_parent_costs_one_move_a_run: the app with three fonts against a writer of the parent racing it (a thread that takes Grimoire's folder away whenever it is at the name, and puts a writable folder there, or leaves nothing). One move a run at most, three checks at most, the checks stopped once; then a start without that writer moves aside once and the font draws. Here: 2 checks, 1 move; and 2 checks, no move.
  • moved_aside_at_most_once_a_process: a process given leave tries once: after a move, the fresh folder renamed away and a 0777 folder put at the name, the same process moves nothing and keeps nothing (6). A fresh process, with an empty folder at every name it could take in the next 30 seconds, moves nothing (EEXIST) and every entry is as it was. Then it moves, and the font is checked once more.
  • an_entry_back_at_once_is_not_moved_again: a test seam puts a 0777 folder at the name between the move and the make: the job gives up (6), says why, marks nothing, one entry moved aside.
  • a_parent_that_takes_no_write_moves_nothing: the parent 0555 (not run as root): the move fails, the job gives up (6), the parent is as it was; with nothing at the name, "it cannot be made", as before.
  • the_app_moves_aside_what_is_not_its_own: the app started with a 0775 folder at the name: the check it forks moves it aside, checks the font, and the font is in use; the log line once, no path.
  • Changed: a_folder_that_is_not_grimoires_own_keeps_nothing keeps its cases below the first level (and checks nothing is moved aside). the_app_says_once_that_nothing_can_be_kept makes the data root's folder inside Grimoire's own a file (still "not Grimoire's own", never moved), so it still shows the once-only notice. The direct job calls that expect a move pass USERFONTS_AFRESH, as the first check of a run has it.

userfonts.rs unit tests: an_entry_moved_aside_has_a_name_of_grimoires_own (the name's form, the name rules, the longest the Kindle's can be) and own_stat_asks_only_who_may_write (0700, 0750, 0755, 02700, 02750, 02755, 01700, 01755 and 03700 are Grimoire's own; a write bit for group or others is not, with or without the setgid or sticky bit; nor a folder of another user, a link or a file).

rust/tests/test_sandbox_linux.rs, root only (CI's sandbox job, Ubuntu): a_folder_of_another_user_at_the_own_folders_name_is_moved_aside, in a parent like the Kindle's (another user's and another group's, mode 02775): a folder of uid 65534, mode 0700, at the name is moved aside with the same inode and mode, still that user's, what it holds unchanged; the fresh folder is root's, of the parent's group, mode 02700; a second check takes it and moves nothing; a process of that group, not root, cannot make a file in it. It compiles for x86_64 Linux here (cargo check --target x86_64-unknown-linux-gnu); it runs only in that job.

How each was shown to fail (the code changed in a copy, the test run, the file put back):

change to userfonts.rs what failed
every check given leave (the UI's once-a-run mark always on) a_run_makes_its_own_folder_afresh_once (6 checks); a_writer_of_the_parent_costs_one_move_a_run: 74 checks and 74 moves in 2.5 s, and 74 checks with the folder taken away
every check makes a missing folder (as before this change), the move still once a run a_run_makes_its_own_folder_afresh_once (2 checks); the app case with the folder taken away: 74 checks
own_stat refuses the setgid bit, or the sticky bit own_stat_asks_only_who_may_write (02700, 01700), and the pin on step 3
no move (base_afresh never called) 36 checks: every kind, the once case, the seam case, and the app case
no once-a-process flag moved_aside_at_most_once_a_process (the same process tries again)
a plain renameat in place of the no-replace rename moved_aside_at_most_once_a_process: an empty folder at the new name is replaced, 5 checks
the folders below moved too a_folder_that_is_not_grimoires_own_keeps_nothing (4) and the_app_says_once_that_nothing_can_be_kept (7)
a second move after the seam's entry an_entry_back_at_once_is_not_moved_again, 7 checks
fstatat following a link the two link kinds of what_is_not_grimoires_own_is_moved_aside, 6 checks

(The last six rows were run on the first version of this change.)

rust/tests/test_sec_invariants.rs, the_docs_follow_the_own_folders_move_aside: a pin for each rule sentence of DESIGN.md, KINDLE.md and TESTING.md, held with the line of userfonts.rs that makes it true (the first-level match, the base's open with afresh, the UI's once-a-run line, the folders below, the name, the no-replace rename, the fstatat that follows no link, own_stat's rule for step 3, the parent opened and held to nothing, the log lines, the two floors), the sentences on what is never done to the entry held with the whole of base_afresh, and gone pins for the old wording. Each new pin failed once with its code line changed and once with its sentence reworded.

Not tested: a folder of this user's that no one else may write but that did not open (it needs a process out of descriptors).

What was run

On head 8343c32, on macOS (aarch64) with the release's toolchain (Rust 1.97.1, zig 0.16.0):

  • test_sec_font_verdict (362 checks), test_sec_invariants, the userfonts.rs unit tests, test_fontcheck, test_fontguard, test_addfont, test_file_previews, test_sec_work_links: passed.
  • The 16 font scenarios (tests/scenarios/*font*.qs): ok.
  • make check, the full gate: make's last line is CHECK PASSED (559 scenario runs and the 8 run again 400 ms late, all ok; l10n-check: every language fits (en es ja pl); the unit tests of the host and the sim build; the clean builds give the same bytes twice).
  • The Kindle binary is 11,405,264 bytes (the guard is 13,000,000); the zip is 5,946,925 bytes (the guard is 7,000,000).
  • The test for Linux was compiled here for x86_64 Linux (cargo check --target x86_64-unknown-linux-gnu --features sim --test test_sandbox_linux).

Nothing ran on Linux here, and nothing on a Kindle. CI's sandbox job (Ubuntu, as root) ran the root-only test on this head: ok, with the line (a folder, owner 65534, mode 0700): it is moved aside as it was, ....

Device check

Not run on a Scribe beyond step 1's answers. docs/TESTING.md, "Hardening round (2026-11), fonts", step 12 checks the move aside and the once-a-run rule on the device.

Closes #201

🤖 Generated with Claude Code

https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ

ganten1998 and others added 2 commits November 19, 2026 17:50
…s own is moved aside, and the folder made afresh (#201)

The job that checks fonts opens Grimoire's own folder (/var/local/grimoire on the Kindle) by descriptor and holds
it to "a folder of this user's that no one else may write". When the entry at that name does not pass (a link, a
file, a pipe, a folder of another user or one others may write), the job now renames it within its parent to
<name>.not-own-<seconds>-<pid> with the rename that never replaces an entry (renameat2's no-replace flag, or
fsview's reservation where the flag is refused), never following, opening, reading, listing or deleting it, logs
its kind, owner and mode once, and makes the folder afresh (mkdirat 0700), which is opened and checked as before.
Every font is then checked once more. A process tries this at most once; when the move fails or the fresh folder
is not Grimoire's own either, nothing is kept, as before, and the log says why. The folders below are Grimoire's
own and are never moved. The UI's opens move nothing.

Tests: test_sec_font_verdict's new cases (each kind moved aside as it was, at most once a process, never onto a
taken name, an entry back at the name between the move and the make, a parent that takes no write, the app end
to end); the base-level legs of the not-own case move to them, and the app's "says once" case now uses a data
root's folder that is a file. A unit test of the name, and a root-only test in test_sandbox_linux for a folder of
another user (CI's sandbox job).

Refs #201

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ
…the floors set from them (#201)

TESTING.md's fonts section: step 1 now gives what a Scribe answered (/var/local is a file system of its own,
ext4, read and write, 996,780 KB with 868,764 KB free and 65,536 inodes with 63,718 free; /var/local itself is
mode 2775, of a system user and group), and its rule is what the code needs: a file system that keeps files,
owners and modes, with the room the floors ask; 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. STATE_FREE_FLOOR (4 MB) and
STATE_FILES_FLOOR (512 files) are set from those numbers and are no longer placeholders, in the docs and in the
code's comment; the glyph limit and the draw size stay placeholders. Step 3 says what a folder made under a
setgid /var/local shows, and a new step 12 checks the move aside on a device.

KINDLE.md says where the verdicts are kept and that the folder is made afresh when the entry at its name is not
Grimoire's own, with the new log lines; DESIGN.md's verdict store says the same rules; the changelog's fonts
entry gains one sentence. BACKLOG: the entry on the untested parent goes (Grimoire no longer relies on it), and
removing by hand covers what was moved aside.

test_sec_invariants: a pin for each new rule sentence, held with the line of userfonts.rs that makes it true
(the whole of base_afresh for what is never done to the entry), and gone pins for the old step 1 rule and the
placeholder wording. Each was shown to fail once, by changing the code lines and by rewording the sentences.

Refs #201

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ
@ganten1998 ganten1998 added the claude Claude is working on this (assigned to ganten1998, whose account Claude uses) label Nov 20, 2026
@ganten1998 ganten1998 self-assigned this Nov 20, 2026
ganten1998 and others added 3 commits November 19, 2026 19:32
…the run's first check (#201)

Each font check is a process of its own, so the move aside's once-a-process flag let every check of a run move
an entry aside: a writer of the parent that put a folder at the name after every check got one move and one
more check of every font each time, without end. A writer that only took the folder away got the same loop
already, since every check made a missing folder anew and a check that ended with more fonts left queued the
next one.

The UI now decides, from its own memory: the first check it queues in a run carries the mark USERFONTS_AFRESH
at the head of its argument, and only a check with that mark may make Grimoire's own folder when it is missing
or move aside what stands at its name (VerdictDir::open's new `afresh`). A later check of the run needs the
folder there and Grimoire's own; otherwise it keeps nothing and ends with status 6 as before, after which the
session checks no more fonts until Grimoire starts again, said once. Nothing outside the UI's memory decides
it: no file on the storage or in the parent. The folders below Grimoire's own are made as before. A process
still tries the move at most once.

Tests (test_sec_font_verdict): a run's first look moves aside and checks; with a folder others can write put
back, and then with nothing there, later checks of the run end 6, move nothing and make nothing; the next run
moves once more. The app against a writer of the parent racing it (taking the folder after every check and
putting a writable folder there, or leaving nothing), three fonts: one move a run at most and three checks at
most, then a start without that writer moves once and the font draws. Reverted (every check given the mark,
or every check making the folder as before) the app case runs 74 checks in 2.5 s. The direct job calls that
expect a move pass the mark; the Linux root test does too.

DESIGN.md, KINDLE.md and TESTING.md step 12 say once a run, with pins on the UI's line and the base's open
(the load_file pin follows its two-argument open).

Refs #201

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ
…wn, said and tested (#201)

The Kindle's /var/local has the setgid bit and a group that may write it, so every folder Grimoire makes there
comes out 02700, with that group. own_stat takes it because it asks only the write bits of group and others;
its comment now says so, and why: the setgid bit, like the sticky bit, lets no one else write the folder, and
the group it brings has no rights in it.

Tests. A unit test of own_stat's rule by mode (userfonts.rs): 0700, 0750, 0755, 02700, 02750, 02755, 01700,
01755 and 03700 are Grimoire's own; anything with a write bit for group or others is not, setgid or sticky bit
or not; nor is a folder of another user, a link or a file. Refusing the setgid bit, or the sticky bit, in
own_stat fails it. The Linux root test (test_sandbox_linux, CI's sandbox job) now uses a parent like the
Kindle's: another user's and another group's, mode 02775. The fresh folder must be root's, of that group, mode
02700; a second check given leave to move takes it and moves nothing; and a process of that group, not root,
cannot make a file in it. It was compiled here for x86_64 Linux; it runs in CI only.

TESTING.md step 3's sentence on the folders under a setgid /var/local is pinned with own_stat's line.

Refs #201

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ
…inned (#201)

DESIGN.md said the entry at the own folder's name is "renamed by descriptor as it stands", which reads as if the
entry were held by a descriptor; it is renamed by its name from the parent's descriptor, and the kind, owner and
mode logged are those looked at just before the rename, so an entry another writer of the parent puts there in
between is the one moved, and is left as it is too. The sentence now says that, and so does base_afresh's
comment. "Grimoire never relies on the parent" said more than the code does: the parent holds the own folder's
name, the folder opened there is tested at every open, and the parent's owner and mode are never trusted. That
is what it says now.

test_sec_invariants: the reworded sentences are held with what they stand on (the parent opened and held to
nothing, own_dir_at's test, the whole of base_afresh), with gone pins for the old wording; KINDLE.md's "An entry
moved aside is left too: Grimoire never opens or removes it." is held with base_afresh, and its two lines for a
move that cannot be made and a fresh folder that is not Grimoire's own either with the log lines the code
writes. Each was shown to fail once, by rewording the sentences and by changing the code they stand on.

Refs #201

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uoe9ZJNyEZYoc4bsjqbSyQ
@ganten1998
ganten1998 merged commit bfe9f07 into main Nov 20, 2026
18 checks passed
@ganten1998
ganten1998 deleted the 201-font-state-dir-afresh branch November 20, 2026 02:08
@ganten1998 ganten1998 removed the claude Claude is working on this (assigned to ganten1998, whose account Claude uses) label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fonts: Grimoire's own folder on the Kindle is made afresh when the entry at its name is not its own

1 participant