Skip to content

uapi16: fixes and checks from the 0.1.0 review - #3

Merged
brauner merged 5 commits into
mainfrom
consistency
Nov 23, 2026
Merged

brauner merged 5 commits into
mainfrom
consistency

Conversation

@brauner

@brauner brauner commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Fixes and checks the review of 0.1.0 turned up. 0.1.0 isn't published
yet, so all of it goes into 0.1.0, together with the two fixes stacked
on top in #4.

The first commit fixes an underflow in set_contents(). A contents object
whose offset lies beyond its data made it panic in a debug build and
fill in a wrapped size in a release build. Only set_contents() gets
there: the reader and the writer check every contents object first.

The other commits:

  • eq_ignore_filled_in() compares objects without their FilledIn marks.
    == keeps comparing them.
  • The reader and the writer hash each literal once and refuse one that
    contradicts the hashes the manifest gives.
  • set_contents() fills in only from contents objects a reader takes.
    Nothing follows from one that contradicts itself or that has a value a
    reader refuses.

Note that the last two change behavior: manifests and objects that were
taken before are refused or filled in differently now.

Every commit builds and passes clippy and the tests with rustc 1.85 and
the current stable, and for the cross targets, musl and i686.

@brauner brauner changed the title uapi16: add eq_ignore_filled_in() and check literals against their hashes uapi16: fixes and checks from the 0.1.0 review Nov 23, 2026
@brauner
brauner added this pull request to stack #5 November 23, 2026 20:52
set_contents() panics in a debug build when a contents object has its
offset beyond its data:

    let mut contents = ContentsObject::new(ContentsSource::File(a));
    contents.set_offset(6);
    contents.set_original_size(Some(5));
    object.set_contents(vec![contents]);

    attempt to subtract with overflow

The check of the file object takes the rest of the data of a contents
object from its offset on as total - offset. That the offset is inside
the data is only stated in a comment, since
ContentsObject::check_consistency() refuses an offset beyond the data.
The reader and the writer only get to the check of the file object once
every contents object passed its own check, so for them it holds. But
set_contents() runs the check of the file object on the fields of a
contents object that failed its own check as well, and so the
subtraction underflows. A release build wraps around instead and fills
in a size such as 18446744073709551615, marked as filled in.

So subtract with checked_sub() and refuse such a contents object with
the error its own check gives. That error moves into
Error::offset_beyond_data() so that both checks share it. set_contents()
then fills in neither a size nor a hash for the object.

The test goes through the ways to get there: an offset beyond the data
with and without a size given, httpHeaders without a URL, an encoded
literal, a second contents object behind one that agrees, the offset
u64::MAX, and an offset set on an object a reader read. Each of them
panicked in the debug build of the tests.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The derived == of FileObject, RootObject, InodeAttributes and
ContentsObject compares the FilledIn marks too. A file object read from

    {"name":"e","size":0}

and one built with

    set_size(Some(0))
    set_sha256(Some(Sha256::EMPTY))

return the same from every getter but compare unequal. The reader
filled in the hash of no data and marked it as filled in while the built
object states it. Their Debug output differs only in filled_in.

The marks matter to the writer though. Under "features": ["size"] the
writer refuses the built object with UnlistedFeature because it states a
sha256 the manifest doesn't record. The read object is written just fine
because the writer leaves the filled-in hash out. So a == b currently
means the same record and the same verdict from the writer and we keep
it that way.

Instead, add eq_ignore_filled_in() to all four types. It compares what
the getters return and ignores the marks. The name follows
str::eq_ignore_ascii_case(). Each method names every field when it
destructures the object, so a field that is added later breaks the
build until it is compared. The test builds every object with all of
its fields named and changes them one at a time.

Document on the types and on FilledIn that == includes the marks and
point to eq_ignore_filled_in().

The fixture test can use it now. So with_stated_xattrs() goes and so
does the fixture edit that took the hash of no data out of the empty
file of the special case. That record is back to what systemd-manifest
wrote.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Currently, the consistency checks hold a literal against the sizes the
manifest gives and nothing else. Nothing hashes it. So a file object
with the literal "hello" and the hash of "world"

    {"contents":[{"literal":"aGVsbG8="}],"name":"x","sha256":"486e..."}

is written and read back without an error. Only copy_data() refuses it,
with Data(HashMismatch), and only after it handed the bytes to the sink.
data_location() hands out such a literal without complaint, and the
promise of set_contents() that a writer refuses an object whose fields
contradict each other doesn't hold.

A literal is the data as it is stored. So hash it in check_consistency,
which the reader and the writer share, and refuse a contradiction as
Manifest(Contradiction) at Field::Literal, like the size check does:

- Without an encoding the literal is held against encodedSha256 and
  originalSha256. With an encoding it is held against encodedSha256
  only. originalSha256 is the hash of the decoded data and checking it
  would need decoding.

- The part of an unencoded literal that is the data of the file object,
  from the offset on and as long as the object, is held against the
  hash of the object. That is the hash the object states or the one
  that follows from another contents object.

Each literal is hashed once. ContentsObject::check_consistency() hashes
it where its contents object gives a hash, and the check of the file
object only hashes what that didn't cover: a literal without a hash of
its own, or a part of one. The writer used to call
ContentsObject::check_consistency() a second time for the check of the
file object. It now keeps what the first call returned and hands it
along. So FileObject::check_consistency() takes the data of the contents
objects instead of a closure that computes it. The cost is linear in
the size of the record, which MAX_RECORD_SIZE caps. For 32 records with
a 1 MiB literal each and both hashes given, reading takes one hash of
each literal more than before, about 4.5% more instructions, and
writing takes one more.

The new checks come after the other checks of a contents object and of
a file object, so what those refused before keeps its error.

This is a behavior change for readers and writers alike. A manifest
with such a literal is now refused when it is read and when it is
written. The tests that copied the data of such objects now expect the
read to fail, and they copy the data of objects that were changed after
they were read instead, which copy_data() still checks.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
set_contents() fills in what follows from the contents objects, as a
reader does. But fill_in_from_contents() only leaves out the contents
objects whose own check fails. The check of the file object then still
runs on their fields as they are and fills in the size and the hash of
the file from them.

So set_contents() with the literal "hello", the original size 5 and the
hash of "world" leaves the object with the size 5 and the hash of
"world", both marked as filled in. A reader refuses the record of that
object, a writer refuses the object since the previous commit, and
copy_data() fails.

Such a contents object is also what made set_contents() panic in a
debug build. With the file "a" at offset 6 and the original size 5 the
check of the file object subtracted the offset from the size and
underflowed. "uapi16: fix an underflow in set_contents()" made that
subtraction checked, but that only turns it into an error.

The docs of fill_in_from_contents() already promise that nothing
follows from fields that contradict each other. So fill in nothing for
the object, neither its size nor its hash, when one of its contents
objects fails its own check. Contents objects that agree with themselves
are still filled in. That keeps set_contents() from taking a contents
object that failed its own check into the check of the file object at
all, and the checked subtraction stays as a guard.

The reader doesn't have that problem. A contents object that fails its
own check makes the reader refuse the whole record, and the size and the
hash of an object are only filled in for a record without an error.

The test holds set_contents() to what a reader reads from the same
records, marks and all, and checks the case above. Running
set_contents() over 707200 generated cases before and after this change
gives 53034 objects that differ. Each of them has a contents object that
contradicts itself, and in each the object keeps what it states and
nothing more. Where the writer takes the object nothing changes, and the
record it writes reads back with the values set_contents() gave it.

This changes what set_contents() does, not its API.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Before it fills in what follows from them, set_contents() runs
ContentsObject::check_consistency() on each contents object and nothing
else. A reader also checks the value of each field on its own: the URL
has to be http or https, the encoding can't be empty or identity, and
the names and values of HTTP header fields have rules of their own, as
do the strings of a manifest. So set_contents() with the URL
"ftp://example.com/x" and the original size 5 still leaves the object
with the size 5 and the contents object with the encoded size 5, both
marked as filled in. The same goes for the encoding "identity" on a
literal and for the HTTP header field "bad name". A reader refuses the
records of all three, and a writer refuses the objects.

So follow the rule a reader follows and fill in only from contents
objects a reader takes. set_contents() runs the checks of the values
as well, and a contents object that fails them fills in nothing, for
itself or for the file object, just like one that contradicts itself
since the previous commit. The checks are the ones the writer already
runs as a reader does, ContentsObject::check_strings() and
ContentsObject::check(). The new ContentsObject::check_values() runs
the two.

A sweep holds set_contents() against a reader of the record with the
same fields, for 2704000 generated file objects with up to two contents
objects, with and without a value a reader refuses. For every record a
reader takes, set_contents() gives the object the reader reads, marks
and all. For every record it refuses, set_contents() fills in nothing
for the object. Every contents object is filled in as a reader fills it
in on its own, or not at all where a reader refuses it, and the writer
takes exactly the objects whose records a reader takes. Before this
change 30808 of them got a size or a hash filled in from a record a
reader refuses, and 72560 had a contents object filled in that a reader
refuses. Where a reader takes the record nothing changes.

This changes what set_contents() does, not its API.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
@brauner
brauner merged commit 8a48c10 into main Nov 23, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant