From 2238203a02e149a55c43e6769fc9dca4b32a4fda Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 23 Nov 2026 19:15:34 +0100 Subject: [PATCH 1/2] uapi16: refuse a contents object that refers to the root A writer takes a contents object whose file is the name of the root and writes {"contents":[{"file":""}],"name":"x"} which a reader refuses: manifest object 1: contents file reference: name is empty: '' Every name is one a reader takes for a file but the root's, which is empty, and ContentsSource::File() can hold it: Name::ROOT, Name::default() and the parent of a name at the top are all the name of the root. So refuse such a contents object in ContentsObject::check(), which checks the values of a contents object as a reader does, with the error a reader gives for the record. That error moves into Error::invalid_file_reference() so that the reader and the writer share it. The message only differs in the name of the object, which a reader doesn't know yet when it reads the contents objects. Since the previous commit set_contents() runs ContentsObject::check() as well, through ContentsObject::check_values(). So it now fills in nothing from such a contents object either. Before, the file "" with the original size 5 gave the object the size 5 and the contents object the encoded size 5, both marked as filled in. The sweep of the previous commit agrees once it has the name of the root among its files. Before this change 15718 of its file objects with one or two contents objects didn't agree with a reader, and the writer took 9741 objects whose records a reader refuses. Now all of them agree, and where a reader takes the record nothing changes. Signed-off-by: Christian Brauner (Amutable) --- src/uapi16/decode.rs | 31 +++++++++++++++-------------- src/uapi16/error.rs | 17 +++++++++++++++- src/uapi16/writer.rs | 47 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 19 deletions(-) diff --git a/src/uapi16/decode.rs b/src/uapi16/decode.rs index aa90fac..e43e778 100644 --- a/src/uapi16/decode.rs +++ b/src/uapi16/decode.rs @@ -1862,19 +1862,11 @@ impl<'a> Part<'a> for ContentsElement { match field { Field::File => { if let Some(file) = decoder.string(field, value) { - match Name::new(file.as_ref()) { - Ok(file) => { - contents.source = ContentsSource::File(file); - places += 1; - } - Err(reason) => decoder.note_error(Problem::InvalidContents, field, || { - format!( - "{}: contents file reference: {}: '{}'", - subject, - reason, - file.escape_debug() - ) - }), + let name = Name::new(file.as_ref()) + .map_err(|reason| Error::invalid_file_reference(subject, reason, &file)); + if let Some(name) = decoder.note_err(name) { + contents.source = ContentsSource::File(name); + places += 1; } } } @@ -3101,8 +3093,9 @@ mod tests { } } // A contents object with a value a reader refuses: a URL that is - // neither http nor https, the encoding that stands for none and a - // name that is not one of an HTTP header field. + // neither http nor https, the encoding that stands for none, a name + // that is not one of an HTTP header field, and the name of the root + // as the file. let ftp = || ContentsObject::new(ContentsSource::Url("ftp://example.com/x".into())); let https = || ContentsObject::new(ContentsSource::Url("https://example.com/x".into())); for (record, contents, field) in [ @@ -3132,6 +3125,14 @@ mod tests { }, Field::HttpHeaders, ), + ( + r#"{"name":"x","contents":[{"file":"","originalSize":5}]}"#, + ContentsObject { + original_size: Some(5), + ..ContentsObject::new(ContentsSource::File(Name::ROOT)) + }, + Field::File, + ), ] { let e = refused(record); assert_eq!( diff --git a/src/uapi16/error.rs b/src/uapi16/error.rs index 886d2c1..3d7a377 100644 --- a/src/uapi16/error.rs +++ b/src/uapi16/error.rs @@ -7,7 +7,7 @@ use std::io; use crate::decode::Subject; use crate::object::FileObject; use crate::strict::{ParseError, ParseErrorKind}; -use crate::{MAX_CONTENTS, MAX_RECORD_SIZE}; +use crate::{NameProblem, MAX_CONTENTS, MAX_RECORD_SIZE}; /// What is wrong with a manifest. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -637,6 +637,21 @@ impl Error { .at(Field::Offset) } + /// A contents object of the file object `subject` names whose `file` + /// is `file`, which is no name of a file for the reason `reason`. + pub(crate) fn invalid_file_reference(subject: Subject<'_>, reason: NameProblem, file: &str) -> Error { + Error::manifest( + Problem::InvalidContents, + format!( + "{}: contents file reference: {}: '{}'", + subject, + reason, + file.escape_debug() + ), + ) + .at(Field::File) + } + /// `object`, which `subject` names, states the flag of the field /// `field`, which it cannot have, see [`FileObject::unpermitted_flag`]. pub(crate) fn unpermitted_flag(object: &FileObject, field: Field, subject: Subject<'_>) -> Error { diff --git a/src/uapi16/writer.rs b/src/uapi16/writer.rs index bda6bf7..ee0a793 100644 --- a/src/uapi16/writer.rs +++ b/src/uapi16/writer.rs @@ -20,8 +20,8 @@ use crate::object::{ }; use crate::sequence::SequenceCheck; use crate::{ - Features, Name, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END, RECORD_SEPARATOR, - SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE, + Features, Name, NameProblem, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END, + RECORD_SEPARATOR, SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE, }; /// The most room for a record that an [`Encoder`] keeps from one record to @@ -729,6 +729,12 @@ impl ContentsObject { if let Some(encoding) = &self.encoding { ContentsObject::check_encoding(encoding, subject)?; } + // Every name is one a reader takes for a file, but the root's: it is + // empty. + if let Some(file) = self.file().filter(|file| file.is_root()) { + let reason = NameProblem::Empty; + return Err(Error::invalid_file_reference(subject, reason, file.as_str())); + } // A reader checks the header fields in the order they are written. if !self.http_headers.is_empty() { ContentsObject::check_headers(self.sorted_headers(), subject)?; @@ -1785,7 +1791,7 @@ mod tests { ..GptPartition::default() })) } - let cases: [(Give, Problem, Field); 19] = [ + let cases: [(Give, Problem, Field); 20] = [ (|o| o.inode.uid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Uid), (|o| o.inode.gid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Gid), // -1 as a 16-bit ID is no ID either. @@ -1856,6 +1862,11 @@ mod tests { Problem::InvalidContents, Field::Contents, ), + ( + |o| o.contents = vec![ContentsObject::new(ContentsSource::File(Name::ROOT))], + Problem::InvalidContents, + Field::File, + ), ]; let (root, _) = recording_all(); for (give, problem, field) in cases { @@ -1870,6 +1881,36 @@ mod tests { object ); } + // A contents object can't refer to the root. Name::ROOT, the default + // name and the parent of a name at the top are all the name of the + // root. A reader refuses the record a writer would write for such an + // object with the same error, only without the name of the object, + // which it doesn't know yet. + for file in [Name::ROOT, Name::default(), name("a").parent().unwrap()] { + let mut object = FileObject::new(name("x")); + object.contents = vec![ContentsObject::new(ContentsSource::File(file))]; + let mut writer = Writer::begin(Vec::new(), &root).unwrap(); + let e = writer.file(&object).unwrap_err(); + assert_eq!( + (e.kind(), e.field(), e.to_string()), + ( + ErrorKind::Manifest(Problem::InvalidContents), + Some(Field::File), + "manifest object 1 (x): contents file reference: name is empty: ''".to_string() + ) + ); + } + let (_, root_text) = recording_all(); + let record = r#"{"contents":[{"file":""}],"name":"x"}"#; + let e = read_whole(&sequence(&[&root_text, record, TRAILER])).unwrap_err(); + assert_eq!( + (e.kind(), e.field(), e.to_string()), + ( + ErrorKind::Manifest(Problem::InvalidContents), + Some(Field::File), + "manifest object 1: contents file reference: name is empty: ''".to_string() + ) + ); // A field of a feature the manifest does not record, also one of // those the specification added last. let mut object = FileObject::new(name("x")); From d731833b0ff41129bdf2583fd5a71a10baaabb4e Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 23 Nov 2026 21:42:28 +0100 Subject: [PATCH 2/2] uapi16: fill in nothing from more than MAX_CONTENTS contents objects A reader refuses the record of a file object with more than MAX_CONTENTS contents objects: manifest object 1: more than 256 contents entries and so does a writer. But set_contents() still fills in from them: 257 contents objects with the file "a" and the original size 5 give each of them the encoded size 5 and the object the size 5, all marked as filled in. With 256 of them a reader takes the record, and set_contents() gives the object the reader reads. Nothing follows from contents objects a reader doesn't take. So with more than MAX_CONTENTS of them set_contents() fills in nothing at all, neither for the object nor for any of them, and doesn't check them either. Of the 3168800 file objects of the sweep, 4000 have 256 or 257 contents objects. Before this change 42 of them didn't agree with a reader. Now none of the file objects of the sweep disagrees, and where a reader takes the record nothing changes. Signed-off-by: Christian Brauner (Amutable) --- src/uapi16/decode.rs | 33 ++++++++++++++++++++++++++++++++- src/uapi16/object.rs | 3 ++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/uapi16/decode.rs b/src/uapi16/decode.rs index e43e778..cdfd476 100644 --- a/src/uapi16/decode.rs +++ b/src/uapi16/decode.rs @@ -471,8 +471,15 @@ impl FileObject { /// [`ContentsObject::check_values`], and whose fields agree with each /// other. Where one of them does not, nothing is filled in for the /// object either, as a reader refuses its record and a writer refuses - /// the object. + /// the object. Where there are more than [`MAX_CONTENTS`] of them, + /// nothing is filled in at all, neither for the object nor for any of + /// them. pub(crate) fn fill_in_from_contents(&mut self) { + // A reader refuses the record of an object with more than + // MAX_CONTENTS contents objects, and nothing follows from them. + if self.contents.len() > MAX_CONTENTS { + return; + } // The object is in no record, and what is wrong with it is not // shown, so the number of the record does not matter. let subject = Subject::new(self, ROOT_RECORD); @@ -3146,6 +3153,30 @@ mod tests { assert!(object.filled_in().is_empty(), "{}", record); assert_eq!(object.contents(), [contents], "{}", record); } + // As many contents objects as a reader takes, and one more, for + // which nothing is filled in at all. + let one = ContentsObject { + original_size: Some(5), + ..file() + }; + let many = |n: usize| { + let record = vec![r#"{"file":"a","originalSize":5}"#; n].join(","); + ( + format!(r#"{{"name":"x","contents":[{}]}}"#, record), + set(None, None, vec![one.clone(); n]), + ) + }; + let (record, object) = many(MAX_CONTENTS); + assert_eq!(object.size(), Some(5)); + assert_eq!(object, self::object(&record)); + let (record, object) = many(MAX_CONTENTS + 1); + assert_eq!( + refused(&record).to_string(), + "manifest object 1: more than 256 contents entries" + ); + assert_eq!((object.size(), object.sha256()), (None, None)); + assert!(object.filled_in().is_empty()); + assert!(object.contents().iter().all(|contents| *contents == one)); } #[test] diff --git a/src/uapi16/object.rs b/src/uapi16/object.rs index e01ad81..4eec52e 100644 --- a/src/uapi16/object.rs +++ b/src/uapi16/object.rs @@ -1826,7 +1826,8 @@ impl FileObject { /// reader filled in that no longer follows is dropped, and what the /// object states stays. A writer refuses an object whose fields /// contradict each other or that has such a value, and more than - /// [`MAX_CONTENTS`] contents objects. + /// [`MAX_CONTENTS`] contents objects, for which nothing is filled in at + /// all. /// /// [`MAX_CONTENTS`]: crate::MAX_CONTENTS pub fn set_contents(&mut self, contents: Vec) {